Skip to content

Commit

Permalink
Merge pull request #898 from gucio321/plot-styling
Browse files Browse the repository at this point in the history
Plot styling
  • Loading branch information
gucio321 authored Oct 30, 2024
2 parents 1d7a4e7 + a599be0 commit c353d6e
Show file tree
Hide file tree
Showing 8 changed files with 620 additions and 64 deletions.
98 changes: 69 additions & 29 deletions CSS.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (e ErrCSSParse) Error() string {
}

// ParseCSSStyleSheet parses CSS stylesheet and stores the rules in giu context.
//
//nolint:gocognit // no
func ParseCSSStyleSheet(data []byte) error {
// css does not support windows formatting
// https://github.com/AllenDang/giu/issues/842
Expand All @@ -58,49 +60,55 @@ func ParseCSSStyleSheet(data []byte) error {
styleVarID, err := StyleVarIDString(styleVarName)

if err == nil {
// the style is StyleVarID - set it
f, err2 := strconv.ParseFloat(styleVarValue, 32)
if err2 == nil {
setter.SetStyleFloat(styleVarID, float32(f))

continue
if err := parseStyleVar(styleVarValue, func(v float32) {
setter.SetStyleFloat(styleVarID, v)
}, func(x, y float32) {
setter.SetStyle(styleVarID, x, y)
}); err != nil {
return err
}

// so maybe it is a vec2 value:
// var-name: x, y;
styleVarValue = strings.ReplaceAll(styleVarValue, " ", "")
vec2 := strings.Split(styleVarValue, ",")
continue
}

if len(vec2) != 2 {
return ErrCSSParse{What: "value (not float or vec2)", Value: styleVarValue}
styleColorID, err := StyleColorIDString(styleVarName)
if err == nil {
col, err := csscolorparser.Parse(styleVarValue)
if err != nil {
return ErrCSSParse{What: "color", Value: styleVarValue, Detail: err}
}

x, err2 := strconv.ParseFloat(vec2[0], 32)
if err2 != nil {
return ErrCSSParse{What: "value (not float)", Value: vec2[0], Detail: err2}
}
setter.SetColor(styleColorID, col)

y, err2 := strconv.ParseFloat(vec2[1], 32)
if err2 != nil {
return ErrCSSParse{What: "value (not float)", Value: vec2[1], Detail: err2}
}
continue
}

setter.SetStyle(styleVarID, float32(x), float32(y))
stylePlotVarID, err := StylePlotVarIDString(styleVarName)
if err == nil {
if err := parseStyleVar(styleVarValue, func(v float32) {
setter.SetPlotStyleFloat(stylePlotVarID, v)
}, func(x, y float32) {
setter.SetPlotStyle(stylePlotVarID, x, y)
}); err != nil {
return err
}

continue
}

styleColorID, err := StyleColorIDString(styleVarName)
if err != nil {
return ErrCSSParse{What: "style variable ID", Value: styleVarName}
}
stylePlotColorID, err := StylePlotColorIDString(styleVarName)
if err == nil {
col, err := csscolorparser.Parse(styleVarValue)
if err != nil {
return ErrCSSParse{What: "color", Value: styleVarValue, Detail: err}
}

col, err := csscolorparser.Parse(styleVarValue)
if err != nil {
return ErrCSSParse{What: "color", Value: styleVarValue, Detail: err}
setter.SetPlotColor(stylePlotColorID, col)

continue
}

setter.SetColor(styleColorID, col)
return ErrCSSParse{What: "style variable name", Value: styleVarName}
}

Context.cssStylesheet[string(rule)] = setter
Expand All @@ -109,6 +117,38 @@ func ParseCSSStyleSheet(data []byte) error {
return nil
}

func parseStyleVar(styleVarValue string, setFloat func(v float32), setVec2 func(x, y float32)) error {
// the style is StyleVarID - set it
f, err2 := strconv.ParseFloat(styleVarValue, 32)
if err2 == nil {
setFloat(float32(f))
return nil
}

// so maybe it is a vec2 value:
// var-name: x, y;
styleVarValue = strings.ReplaceAll(styleVarValue, " ", "")
vec2 := strings.Split(styleVarValue, ",")

if len(vec2) != 2 {
return ErrCSSParse{What: "value (not float or vec2)", Value: styleVarValue}
}

x, err2 := strconv.ParseFloat(vec2[0], 32)
if err2 != nil {
return ErrCSSParse{What: "value (not float)", Value: vec2[0], Detail: err2}
}

y, err2 := strconv.ParseFloat(vec2[1], 32)
if err2 != nil {
return ErrCSSParse{What: "value (not float)", Value: vec2[1], Detail: err2}
}

setVec2(float32(x), float32(y))

return nil
}

// cssStylesheet is a map tag:StyleSetter.
type cssStylesheet map[string]*StyleSetter

Expand Down
97 changes: 96 additions & 1 deletion StyleIDs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package giu

import "github.com/AllenDang/cimgui-go/imgui"
import (
"github.com/AllenDang/cimgui-go/imgui"
"github.com/AllenDang/cimgui-go/implot"
)

// Here are the style IDs for styling imgui apps.
// For details about each of attributes read comment above them.

// go-generate String() andStringToEnum methods
//go:generate go run github.com/dmarkham/enumer@latest -linecomment -type=StyleColorID .
//go:generate go run github.com/dmarkham/enumer@latest -linecomment -type=StyleVarID .
//go:generate go run github.com/dmarkham/enumer@latest -linecomment -type=StylePlotColorID .
//go:generate go run github.com/dmarkham/enumer@latest -linecomment -type=StylePlotVarID .

// StyleColorID identifies a color in the UI style.
type StyleColorID imgui.Col
Expand Down Expand Up @@ -159,3 +164,93 @@ func (i StyleVarID) IsVec2() bool {

return result && ok
}

// StylePlotColorID represents an ID of plot color.
type StylePlotColorID int

// List of plot color IDs.
const (
StylePlotColorLine StylePlotColorID = StylePlotColorID(implot.PlotColLine) // plot-line
StylePlotColorFill StylePlotColorID = StylePlotColorID(implot.PlotColFill) // plot-fill
StylePlotColorMarkerOutline StylePlotColorID = StylePlotColorID(implot.PlotColMarkerOutline) // plot-marker-outline
StylePlotColorMarkerFill StylePlotColorID = StylePlotColorID(implot.PlotColMarkerFill) // plot-Marker-Fill
StylePlotColorErrorBar StylePlotColorID = StylePlotColorID(implot.PlotColErrorBar) // plot-error-bar
StylePlotColorFrameBg StylePlotColorID = StylePlotColorID(implot.PlotColFrameBg) // plot-frame-bg
StylePlotColorPlotBg StylePlotColorID = StylePlotColorID(implot.PlotColPlotBg) // plot-plot-bg
StylePlotColorPlotBorder StylePlotColorID = StylePlotColorID(implot.PlotColPlotBorder) // plot-plot-border
StylePlotColorLegendBg StylePlotColorID = StylePlotColorID(implot.PlotColLegendBg) // plot-legend-bg
StylePlotColorLegendBorder StylePlotColorID = StylePlotColorID(implot.PlotColLegendBorder) // plot-legend-border
StylePlotColorLegendText StylePlotColorID = StylePlotColorID(implot.PlotColLegendText) // plot-legend-text
StylePlotColorTitleText StylePlotColorID = StylePlotColorID(implot.PlotColTitleText) // plot-title-text
StylePlotColorInlayText StylePlotColorID = StylePlotColorID(implot.PlotColInlayText) // plot-inlay-text
StylePlotColorAxisText StylePlotColorID = StylePlotColorID(implot.PlotColAxisText) // plot-axis-text
StylePlotColorAxisGrid StylePlotColorID = StylePlotColorID(implot.PlotColAxisGrid) // plot-axis-grid
StylePlotColorAxisTick StylePlotColorID = StylePlotColorID(implot.PlotColAxisTick) // plot-axis-tick
StylePlotColorAxisBg StylePlotColorID = StylePlotColorID(implot.PlotColAxisBg) // plot-axis-bg
StylePlotColorAxisBgHovered StylePlotColorID = StylePlotColorID(implot.PlotColAxisBgHovered) // plot-axis-bg-hovered
StylePlotColorAxisBgActive StylePlotColorID = StylePlotColorID(implot.PlotColAxisBgActive) // plot-axis-bg-active
StylePlotColorSelection StylePlotColorID = StylePlotColorID(implot.PlotColSelection) // plot-selection
StylePlotColorCrosshairs StylePlotColorID = StylePlotColorID(implot.PlotColCrosshairs) // plot-crosshairs
)

// StylePlotVarID represents an ID of plot style variable.
type StylePlotVarID imgui.StyleVar

// List of plot style variable IDs.
const (
StylePlotVarLineWeight StylePlotVarID = StylePlotVarID(implot.PlotStyleVarLineWeight) // plot-line-weight
StylePlotVarMarker StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMarker) // plot-marker
StylePlotVarMarkerSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMarkerSize) // plot-marker-size
StylePlotVarMarkerWeight StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMarkerWeight) // plot-marker-weight
StylePlotVarFillAlpha StylePlotVarID = StylePlotVarID(implot.PlotStyleVarFillAlpha) // plot-fill-alpha
StylePlotVarErrorBarSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarErrorBarSize) // plot-error-bar-size
StylePlotVarErrorBarWeight StylePlotVarID = StylePlotVarID(implot.PlotStyleVarErrorBarWeight) // plot-error-bar-weight
StylePlotVarDigitalBitHeight StylePlotVarID = StylePlotVarID(implot.PlotStyleVarDigitalBitHeight) // plot-digital-bit-height
StylePlotVarDigitalBitGap StylePlotVarID = StylePlotVarID(implot.PlotStyleVarDigitalBitGap) // plot-digital-bit-gap
StylePlotVarPlotBorderSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarPlotBorderSize) // plot-border-size
StylePlotVarMinorAlpha StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMinorAlpha) // plot-minor-alpha
StylePlotVarMajorTickLen StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMajorTickLen) // plot-major-tick-len
StylePlotVarMinorTickLen StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMinorTickLen) // plot-minor-tick-len
StylePlotVarMajorTickSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMajorTickSize) // plot-major-tick-size
StylePlotVarMinorTickSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMinorTickSize) // plot-minor-tick-size
StylePlotVarMajorGridSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMajorGridSize) // plot-major-grid-size
StylePlotVarMinorGridSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMinorGridSize) // plot-minor-grid-size
StylePlotVarPlotPadding StylePlotVarID = StylePlotVarID(implot.PlotStyleVarPlotPadding) // plot-padding
StylePlotVarLabelPadding StylePlotVarID = StylePlotVarID(implot.PlotStyleVarLabelPadding) // plot-label-padding
StylePlotVarLegendPadding StylePlotVarID = StylePlotVarID(implot.PlotStyleVarLegendPadding) // plot-legend-padding
StylePlotVarLegendInnerPadding StylePlotVarID = StylePlotVarID(implot.PlotStyleVarLegendInnerPadding) // plot-legend-inner-padding
StylePlotVarLegendSpacing StylePlotVarID = StylePlotVarID(implot.PlotStyleVarLegendSpacing) // plot-legend-spacing
StylePlotVarMousePosPadding StylePlotVarID = StylePlotVarID(implot.PlotStyleVarMousePosPadding) // plot-mouse-pos-padding
StylePlotVarAnnotationPadding StylePlotVarID = StylePlotVarID(implot.PlotStyleVarAnnotationPadding) // plot-annotation-padding
StylePlotVarFitPadding StylePlotVarID = StylePlotVarID(implot.PlotStyleVarFitPadding) // plot-fit-padding
StylePlotVarPlotDefaultSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarPlotDefaultSize) // plot-default-size
StylePlotVarPlotMinSize StylePlotVarID = StylePlotVarID(implot.PlotStyleVarPlotMinSize) // plot-min-size
StylePlotVarCOUNT StylePlotVarID = StylePlotVarID(implot.PlotStyleVarCOUNT)
)

// IsVec2 returns true if the style plot var id should be processed as imgui.Vec2
// if not, it is interpreted as float32.
func (i StylePlotVarID) IsVec2() bool {
lookup := map[StylePlotVarID]bool{
StylePlotVarMajorTickLen: true,
StylePlotVarMinorTickLen: true,
StylePlotVarMajorTickSize: true,
StylePlotVarMinorTickSize: true,
StylePlotVarMajorGridSize: true,
StylePlotVarMinorGridSize: true,
StylePlotVarPlotPadding: true,
StylePlotVarLabelPadding: true,
StylePlotVarLegendPadding: true,
StylePlotVarLegendInnerPadding: true,
StylePlotVarLegendSpacing: true,
StylePlotVarMousePosPadding: true,
StylePlotVarAnnotationPadding: true,
StylePlotVarFitPadding: true,
StylePlotVarPlotDefaultSize: true,
StylePlotVarPlotMinSize: true,
}

result, ok := lookup[i]

return result && ok
}
Loading

0 comments on commit c353d6e

Please sign in to comment.