From b1f2893b6cd1d28253895f0dbf689a87c528c463 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 30 Oct 2024 11:48:35 +0100 Subject: [PATCH 1/6] style setter: add plot style support fix #847 --- StyleIDs.go | 97 +++++++++++++++++++- StyleSetter.go | 89 +++++++++++++++++- styleplotcolorid_enumer.go | 154 +++++++++++++++++++++++++++++++ styleplotvarid_enumer.go | 182 +++++++++++++++++++++++++++++++++++++ 4 files changed, 516 insertions(+), 6 deletions(-) create mode 100644 styleplotcolorid_enumer.go create mode 100644 styleplotvarid_enumer.go diff --git a/StyleIDs.go b/StyleIDs.go index 078d559e..98cfbeb9 100644 --- a/StyleIDs.go +++ b/StyleIDs.go @@ -1,6 +1,9 @@ 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. @@ -8,6 +11,8 @@ import "github.com/AllenDang/cimgui-go/imgui" // 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 @@ -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 +} diff --git a/StyleSetter.go b/StyleSetter.go index d23260c6..a7c04a45 100644 --- a/StyleSetter.go +++ b/StyleSetter.go @@ -4,6 +4,7 @@ import ( "image/color" "github.com/AllenDang/cimgui-go/imgui" + "github.com/AllenDang/cimgui-go/implot" ) var _ Widget = &StyleSetter{} @@ -11,11 +12,15 @@ var _ Widget = &StyleSetter{} // StyleSetter is a user-friendly way to manage imgui styles. // For style IDs see StyleIDs.go, for detailed instruction of using styles, see Styles.go. type StyleSetter struct { - colors map[StyleColorID]color.Color - styles map[StyleVarID]any - font *FontInfo - disabled bool - layout Layout + colors map[StyleColorID]color.Color + styles map[StyleVarID]any + plotColors map[StylePlotColorID]color.Color + plotStyles map[StylePlotVarID]any + font *FontInfo + disabled bool + + layout Layout + plots []PlotWidget // set by imgui.PushFont inside ss.Push() method isFontPushed bool @@ -25,7 +30,9 @@ type StyleSetter struct { func Style() *StyleSetter { var ss StyleSetter ss.colors = make(map[StyleColorID]color.Color) + ss.plotColors = make(map[StylePlotColorID]color.Color) ss.styles = make(map[StyleVarID]any) + ss.plotStyles = make(map[StylePlotVarID]any) return &ss } @@ -50,6 +57,26 @@ func (ss *StyleSetter) SetStyleFloat(varID StyleVarID, value float32) *StyleSett return ss } +// SetPlotColor sets colorID's color. +func (ss *StyleSetter) SetPlotColor(colorID StylePlotColorID, col color.Color) *StyleSetter { + ss.plotColors[colorID] = col + return ss +} + +// SetPlotStyle sets stylePlotVarID to width and height. +func (ss *StyleSetter) SetPlotStyle(varID StylePlotVarID, width, height float32) *StyleSetter { + ss.plotStyles[varID] = imgui.Vec2{X: width, Y: height} + return ss +} + +// SetPlotStyleFloat sets StylePlotVarID to float value. +// NOTE: for float typed values see above in comments over +// StyleVarID's comments. +func (ss *StyleSetter) SetPlotStyleFloat(varID StylePlotVarID, value float32) *StyleSetter { + ss.plotStyles[varID] = value + return ss +} + // SetFont sets font. func (ss *StyleSetter) SetFont(font *FontInfo) *StyleSetter { ss.font = font @@ -84,6 +111,11 @@ func (ss *StyleSetter) To(widgets ...Widget) *StyleSetter { return ss } +func (ss *StyleSetter) Plots(widgets ...PlotWidget) *StyleSetter { + ss.plots = widgets + return ss +} + // Range implements Splitable interface. func (ss *StyleSetter) Range(rangeFunc func(w Widget)) { if ss.layout != nil { @@ -142,6 +174,21 @@ func (ss *StyleSetter) Build() { ss.Pop() } +// Plot implements PlotWidget. +func (ss *StyleSetter) Plot() { + if ss.plots == nil || len(ss.plots) == 0 { + return + } + + ss.Push() + + for _, plot := range ss.plots { + plot.Plot() + } + + ss.Pop() +} + // Push allows to manually activate Styles written inside of StyleSetter // it works like imgui.PushXXX() stuff, but for group of style variables, // just like StyleSetter. @@ -153,6 +200,11 @@ func (ss *StyleSetter) Push() { imgui.PushStyleColorVec4(imgui.Col(k), ToVec4Color(v)) } + // Push plot colors + for k, v := range ss.plotColors { + implot.PlotPushStyleColorVec4(implot.PlotCol(k), ToVec4Color(v)) + } + // push style vars for k, v := range ss.styles { if k.IsVec2() { @@ -178,6 +230,31 @@ func (ss *StyleSetter) Push() { } } + // Push plot colors + for k, v := range ss.plotStyles { + if k.IsVec2() { + var value imgui.Vec2 + switch typed := v.(type) { + case imgui.Vec2: + value = typed + case float32: + value = imgui.Vec2{X: typed, Y: typed} + } + + implot.PlotPushStyleVarVec2(implot.PlotStyleVar(k), value) + } else { + var value float32 + switch typed := v.(type) { + case float32: + value = typed + case imgui.Vec2: + value = typed.X + } + + implot.PlotPushStyleVarFloat(implot.PlotStyleVar(k), value) + } + } + // push font if ss.font != nil { ss.isFontPushed = PushFont(ss.font) @@ -199,5 +276,7 @@ func (ss *StyleSetter) Pop() { } imgui.PopStyleColorV(int32(len(ss.colors))) + implot.PlotPopStyleColorV(int32(len(ss.plotColors))) imgui.PopStyleVarV(int32(len(ss.styles))) + implot.PlotPopStyleVarV(int32(len(ss.plotStyles))) } diff --git a/styleplotcolorid_enumer.go b/styleplotcolorid_enumer.go new file mode 100644 index 00000000..a43f1c72 --- /dev/null +++ b/styleplotcolorid_enumer.go @@ -0,0 +1,154 @@ +// Code generated by "enumer -linecomment -type=StylePlotColorID ."; DO NOT EDIT. + +package giu + +import ( + "fmt" + "strings" +) + +const _StylePlotColorIDName = "plot-lineplot-fillplot-marker-outlineplot-Marker-Fillplot-error-barplot-frame-bgplot-plot-bgplot-plot-borderplot-legend-bgplot-legend-borderplot-legend-textplot-title-textplot-inlay-textplot-axis-textplot-axis-gridplot-axis-tickplot-axis-bgplot-axis-bg-hoveredplot-axis-bg-activeplot-selectionplot-crosshairs" + +var _StylePlotColorIDIndex = [...]uint16{0, 9, 18, 37, 53, 67, 80, 92, 108, 122, 140, 156, 171, 186, 200, 214, 228, 240, 260, 279, 293, 308} + +const _StylePlotColorIDLowerName = "plot-lineplot-fillplot-marker-outlineplot-marker-fillplot-error-barplot-frame-bgplot-plot-bgplot-plot-borderplot-legend-bgplot-legend-borderplot-legend-textplot-title-textplot-inlay-textplot-axis-textplot-axis-gridplot-axis-tickplot-axis-bgplot-axis-bg-hoveredplot-axis-bg-activeplot-selectionplot-crosshairs" + +func (i StylePlotColorID) String() string { + if i < 0 || i >= StylePlotColorID(len(_StylePlotColorIDIndex)-1) { + return fmt.Sprintf("StylePlotColorID(%d)", i) + } + return _StylePlotColorIDName[_StylePlotColorIDIndex[i]:_StylePlotColorIDIndex[i+1]] +} + +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _StylePlotColorIDNoOp() { + var x [1]struct{} + _ = x[StylePlotColorLine-(0)] + _ = x[StylePlotColorFill-(1)] + _ = x[StylePlotColorMarkerOutline-(2)] + _ = x[StylePlotColorMarkerFill-(3)] + _ = x[StylePlotColorErrorBar-(4)] + _ = x[StylePlotColorFrameBg-(5)] + _ = x[StylePlotColorPlotBg-(6)] + _ = x[StylePlotColorPlotBorder-(7)] + _ = x[StylePlotColorLegendBg-(8)] + _ = x[StylePlotColorLegendBorder-(9)] + _ = x[StylePlotColorLegendText-(10)] + _ = x[StylePlotColorTitleText-(11)] + _ = x[StylePlotColorInlayText-(12)] + _ = x[StylePlotColorAxisText-(13)] + _ = x[StylePlotColorAxisGrid-(14)] + _ = x[StylePlotColorAxisTick-(15)] + _ = x[StylePlotColorAxisBg-(16)] + _ = x[StylePlotColorAxisBgHovered-(17)] + _ = x[StylePlotColorAxisBgActive-(18)] + _ = x[StylePlotColorSelection-(19)] + _ = x[StylePlotColorCrosshairs-(20)] +} + +var _StylePlotColorIDValues = []StylePlotColorID{StylePlotColorLine, StylePlotColorFill, StylePlotColorMarkerOutline, StylePlotColorMarkerFill, StylePlotColorErrorBar, StylePlotColorFrameBg, StylePlotColorPlotBg, StylePlotColorPlotBorder, StylePlotColorLegendBg, StylePlotColorLegendBorder, StylePlotColorLegendText, StylePlotColorTitleText, StylePlotColorInlayText, StylePlotColorAxisText, StylePlotColorAxisGrid, StylePlotColorAxisTick, StylePlotColorAxisBg, StylePlotColorAxisBgHovered, StylePlotColorAxisBgActive, StylePlotColorSelection, StylePlotColorCrosshairs} + +var _StylePlotColorIDNameToValueMap = map[string]StylePlotColorID{ + _StylePlotColorIDName[0:9]: StylePlotColorLine, + _StylePlotColorIDLowerName[0:9]: StylePlotColorLine, + _StylePlotColorIDName[9:18]: StylePlotColorFill, + _StylePlotColorIDLowerName[9:18]: StylePlotColorFill, + _StylePlotColorIDName[18:37]: StylePlotColorMarkerOutline, + _StylePlotColorIDLowerName[18:37]: StylePlotColorMarkerOutline, + _StylePlotColorIDName[37:53]: StylePlotColorMarkerFill, + _StylePlotColorIDLowerName[37:53]: StylePlotColorMarkerFill, + _StylePlotColorIDName[53:67]: StylePlotColorErrorBar, + _StylePlotColorIDLowerName[53:67]: StylePlotColorErrorBar, + _StylePlotColorIDName[67:80]: StylePlotColorFrameBg, + _StylePlotColorIDLowerName[67:80]: StylePlotColorFrameBg, + _StylePlotColorIDName[80:92]: StylePlotColorPlotBg, + _StylePlotColorIDLowerName[80:92]: StylePlotColorPlotBg, + _StylePlotColorIDName[92:108]: StylePlotColorPlotBorder, + _StylePlotColorIDLowerName[92:108]: StylePlotColorPlotBorder, + _StylePlotColorIDName[108:122]: StylePlotColorLegendBg, + _StylePlotColorIDLowerName[108:122]: StylePlotColorLegendBg, + _StylePlotColorIDName[122:140]: StylePlotColorLegendBorder, + _StylePlotColorIDLowerName[122:140]: StylePlotColorLegendBorder, + _StylePlotColorIDName[140:156]: StylePlotColorLegendText, + _StylePlotColorIDLowerName[140:156]: StylePlotColorLegendText, + _StylePlotColorIDName[156:171]: StylePlotColorTitleText, + _StylePlotColorIDLowerName[156:171]: StylePlotColorTitleText, + _StylePlotColorIDName[171:186]: StylePlotColorInlayText, + _StylePlotColorIDLowerName[171:186]: StylePlotColorInlayText, + _StylePlotColorIDName[186:200]: StylePlotColorAxisText, + _StylePlotColorIDLowerName[186:200]: StylePlotColorAxisText, + _StylePlotColorIDName[200:214]: StylePlotColorAxisGrid, + _StylePlotColorIDLowerName[200:214]: StylePlotColorAxisGrid, + _StylePlotColorIDName[214:228]: StylePlotColorAxisTick, + _StylePlotColorIDLowerName[214:228]: StylePlotColorAxisTick, + _StylePlotColorIDName[228:240]: StylePlotColorAxisBg, + _StylePlotColorIDLowerName[228:240]: StylePlotColorAxisBg, + _StylePlotColorIDName[240:260]: StylePlotColorAxisBgHovered, + _StylePlotColorIDLowerName[240:260]: StylePlotColorAxisBgHovered, + _StylePlotColorIDName[260:279]: StylePlotColorAxisBgActive, + _StylePlotColorIDLowerName[260:279]: StylePlotColorAxisBgActive, + _StylePlotColorIDName[279:293]: StylePlotColorSelection, + _StylePlotColorIDLowerName[279:293]: StylePlotColorSelection, + _StylePlotColorIDName[293:308]: StylePlotColorCrosshairs, + _StylePlotColorIDLowerName[293:308]: StylePlotColorCrosshairs, +} + +var _StylePlotColorIDNames = []string{ + _StylePlotColorIDName[0:9], + _StylePlotColorIDName[9:18], + _StylePlotColorIDName[18:37], + _StylePlotColorIDName[37:53], + _StylePlotColorIDName[53:67], + _StylePlotColorIDName[67:80], + _StylePlotColorIDName[80:92], + _StylePlotColorIDName[92:108], + _StylePlotColorIDName[108:122], + _StylePlotColorIDName[122:140], + _StylePlotColorIDName[140:156], + _StylePlotColorIDName[156:171], + _StylePlotColorIDName[171:186], + _StylePlotColorIDName[186:200], + _StylePlotColorIDName[200:214], + _StylePlotColorIDName[214:228], + _StylePlotColorIDName[228:240], + _StylePlotColorIDName[240:260], + _StylePlotColorIDName[260:279], + _StylePlotColorIDName[279:293], + _StylePlotColorIDName[293:308], +} + +// StylePlotColorIDString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func StylePlotColorIDString(s string) (StylePlotColorID, error) { + if val, ok := _StylePlotColorIDNameToValueMap[s]; ok { + return val, nil + } + + if val, ok := _StylePlotColorIDNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to StylePlotColorID values", s) +} + +// StylePlotColorIDValues returns all values of the enum +func StylePlotColorIDValues() []StylePlotColorID { + return _StylePlotColorIDValues +} + +// StylePlotColorIDStrings returns a slice of all String values of the enum +func StylePlotColorIDStrings() []string { + strs := make([]string, len(_StylePlotColorIDNames)) + copy(strs, _StylePlotColorIDNames) + return strs +} + +// IsAStylePlotColorID returns "true" if the value is listed in the enum definition. "false" otherwise +func (i StylePlotColorID) IsAStylePlotColorID() bool { + for _, v := range _StylePlotColorIDValues { + if i == v { + return true + } + } + return false +} diff --git a/styleplotvarid_enumer.go b/styleplotvarid_enumer.go new file mode 100644 index 00000000..cd580270 --- /dev/null +++ b/styleplotvarid_enumer.go @@ -0,0 +1,182 @@ +// Code generated by "enumer -linecomment -type=StylePlotVarID ."; DO NOT EDIT. + +package giu + +import ( + "fmt" + "strings" +) + +const _StylePlotVarIDName = "plot-line-weightplot-markerplot-marker-sizeplot-marker-weightplot-fill-alphaplot-error-bar-sizeplot-error-bar-weightplot-digital-bit-heightplot-digital-bit-gapplot-border-sizeplot-minor-alphaplot-major-tick-lenplot-minor-tick-lenplot-major-tick-sizeplot-minor-tick-sizeplot-major-grid-sizeplot-minor-grid-sizeplot-paddingplot-label-paddingplot-legend-paddingplot-legend-inner-paddingplot-legend-spacingplot-mouse-pos-paddingplot-annotation-paddingplot-fit-paddingplot-default-sizeplot-min-sizeStylePlotVarCOUNT" + +var _StylePlotVarIDIndex = [...]uint16{0, 16, 27, 43, 61, 76, 95, 116, 139, 159, 175, 191, 210, 229, 249, 269, 289, 309, 321, 339, 358, 383, 402, 424, 447, 463, 480, 493, 510} + +const _StylePlotVarIDLowerName = "plot-line-weightplot-markerplot-marker-sizeplot-marker-weightplot-fill-alphaplot-error-bar-sizeplot-error-bar-weightplot-digital-bit-heightplot-digital-bit-gapplot-border-sizeplot-minor-alphaplot-major-tick-lenplot-minor-tick-lenplot-major-tick-sizeplot-minor-tick-sizeplot-major-grid-sizeplot-minor-grid-sizeplot-paddingplot-label-paddingplot-legend-paddingplot-legend-inner-paddingplot-legend-spacingplot-mouse-pos-paddingplot-annotation-paddingplot-fit-paddingplot-default-sizeplot-min-sizestyleplotvarcount" + +func (i StylePlotVarID) String() string { + if i < 0 || i >= StylePlotVarID(len(_StylePlotVarIDIndex)-1) { + return fmt.Sprintf("StylePlotVarID(%d)", i) + } + return _StylePlotVarIDName[_StylePlotVarIDIndex[i]:_StylePlotVarIDIndex[i+1]] +} + +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _StylePlotVarIDNoOp() { + var x [1]struct{} + _ = x[StylePlotVarLineWeight-(0)] + _ = x[StylePlotVarMarker-(1)] + _ = x[StylePlotVarMarkerSize-(2)] + _ = x[StylePlotVarMarkerWeight-(3)] + _ = x[StylePlotVarFillAlpha-(4)] + _ = x[StylePlotVarErrorBarSize-(5)] + _ = x[StylePlotVarErrorBarWeight-(6)] + _ = x[StylePlotVarDigitalBitHeight-(7)] + _ = x[StylePlotVarDigitalBitGap-(8)] + _ = x[StylePlotVarPlotBorderSize-(9)] + _ = x[StylePlotVarMinorAlpha-(10)] + _ = x[StylePlotVarMajorTickLen-(11)] + _ = x[StylePlotVarMinorTickLen-(12)] + _ = x[StylePlotVarMajorTickSize-(13)] + _ = x[StylePlotVarMinorTickSize-(14)] + _ = x[StylePlotVarMajorGridSize-(15)] + _ = x[StylePlotVarMinorGridSize-(16)] + _ = x[StylePlotVarPlotPadding-(17)] + _ = x[StylePlotVarLabelPadding-(18)] + _ = x[StylePlotVarLegendPadding-(19)] + _ = x[StylePlotVarLegendInnerPadding-(20)] + _ = x[StylePlotVarLegendSpacing-(21)] + _ = x[StylePlotVarMousePosPadding-(22)] + _ = x[StylePlotVarAnnotationPadding-(23)] + _ = x[StylePlotVarFitPadding-(24)] + _ = x[StylePlotVarPlotDefaultSize-(25)] + _ = x[StylePlotVarPlotMinSize-(26)] + _ = x[StylePlotVarCOUNT-(27)] +} + +var _StylePlotVarIDValues = []StylePlotVarID{StylePlotVarLineWeight, StylePlotVarMarker, StylePlotVarMarkerSize, StylePlotVarMarkerWeight, StylePlotVarFillAlpha, StylePlotVarErrorBarSize, StylePlotVarErrorBarWeight, StylePlotVarDigitalBitHeight, StylePlotVarDigitalBitGap, StylePlotVarPlotBorderSize, StylePlotVarMinorAlpha, StylePlotVarMajorTickLen, StylePlotVarMinorTickLen, StylePlotVarMajorTickSize, StylePlotVarMinorTickSize, StylePlotVarMajorGridSize, StylePlotVarMinorGridSize, StylePlotVarPlotPadding, StylePlotVarLabelPadding, StylePlotVarLegendPadding, StylePlotVarLegendInnerPadding, StylePlotVarLegendSpacing, StylePlotVarMousePosPadding, StylePlotVarAnnotationPadding, StylePlotVarFitPadding, StylePlotVarPlotDefaultSize, StylePlotVarPlotMinSize, StylePlotVarCOUNT} + +var _StylePlotVarIDNameToValueMap = map[string]StylePlotVarID{ + _StylePlotVarIDName[0:16]: StylePlotVarLineWeight, + _StylePlotVarIDLowerName[0:16]: StylePlotVarLineWeight, + _StylePlotVarIDName[16:27]: StylePlotVarMarker, + _StylePlotVarIDLowerName[16:27]: StylePlotVarMarker, + _StylePlotVarIDName[27:43]: StylePlotVarMarkerSize, + _StylePlotVarIDLowerName[27:43]: StylePlotVarMarkerSize, + _StylePlotVarIDName[43:61]: StylePlotVarMarkerWeight, + _StylePlotVarIDLowerName[43:61]: StylePlotVarMarkerWeight, + _StylePlotVarIDName[61:76]: StylePlotVarFillAlpha, + _StylePlotVarIDLowerName[61:76]: StylePlotVarFillAlpha, + _StylePlotVarIDName[76:95]: StylePlotVarErrorBarSize, + _StylePlotVarIDLowerName[76:95]: StylePlotVarErrorBarSize, + _StylePlotVarIDName[95:116]: StylePlotVarErrorBarWeight, + _StylePlotVarIDLowerName[95:116]: StylePlotVarErrorBarWeight, + _StylePlotVarIDName[116:139]: StylePlotVarDigitalBitHeight, + _StylePlotVarIDLowerName[116:139]: StylePlotVarDigitalBitHeight, + _StylePlotVarIDName[139:159]: StylePlotVarDigitalBitGap, + _StylePlotVarIDLowerName[139:159]: StylePlotVarDigitalBitGap, + _StylePlotVarIDName[159:175]: StylePlotVarPlotBorderSize, + _StylePlotVarIDLowerName[159:175]: StylePlotVarPlotBorderSize, + _StylePlotVarIDName[175:191]: StylePlotVarMinorAlpha, + _StylePlotVarIDLowerName[175:191]: StylePlotVarMinorAlpha, + _StylePlotVarIDName[191:210]: StylePlotVarMajorTickLen, + _StylePlotVarIDLowerName[191:210]: StylePlotVarMajorTickLen, + _StylePlotVarIDName[210:229]: StylePlotVarMinorTickLen, + _StylePlotVarIDLowerName[210:229]: StylePlotVarMinorTickLen, + _StylePlotVarIDName[229:249]: StylePlotVarMajorTickSize, + _StylePlotVarIDLowerName[229:249]: StylePlotVarMajorTickSize, + _StylePlotVarIDName[249:269]: StylePlotVarMinorTickSize, + _StylePlotVarIDLowerName[249:269]: StylePlotVarMinorTickSize, + _StylePlotVarIDName[269:289]: StylePlotVarMajorGridSize, + _StylePlotVarIDLowerName[269:289]: StylePlotVarMajorGridSize, + _StylePlotVarIDName[289:309]: StylePlotVarMinorGridSize, + _StylePlotVarIDLowerName[289:309]: StylePlotVarMinorGridSize, + _StylePlotVarIDName[309:321]: StylePlotVarPlotPadding, + _StylePlotVarIDLowerName[309:321]: StylePlotVarPlotPadding, + _StylePlotVarIDName[321:339]: StylePlotVarLabelPadding, + _StylePlotVarIDLowerName[321:339]: StylePlotVarLabelPadding, + _StylePlotVarIDName[339:358]: StylePlotVarLegendPadding, + _StylePlotVarIDLowerName[339:358]: StylePlotVarLegendPadding, + _StylePlotVarIDName[358:383]: StylePlotVarLegendInnerPadding, + _StylePlotVarIDLowerName[358:383]: StylePlotVarLegendInnerPadding, + _StylePlotVarIDName[383:402]: StylePlotVarLegendSpacing, + _StylePlotVarIDLowerName[383:402]: StylePlotVarLegendSpacing, + _StylePlotVarIDName[402:424]: StylePlotVarMousePosPadding, + _StylePlotVarIDLowerName[402:424]: StylePlotVarMousePosPadding, + _StylePlotVarIDName[424:447]: StylePlotVarAnnotationPadding, + _StylePlotVarIDLowerName[424:447]: StylePlotVarAnnotationPadding, + _StylePlotVarIDName[447:463]: StylePlotVarFitPadding, + _StylePlotVarIDLowerName[447:463]: StylePlotVarFitPadding, + _StylePlotVarIDName[463:480]: StylePlotVarPlotDefaultSize, + _StylePlotVarIDLowerName[463:480]: StylePlotVarPlotDefaultSize, + _StylePlotVarIDName[480:493]: StylePlotVarPlotMinSize, + _StylePlotVarIDLowerName[480:493]: StylePlotVarPlotMinSize, + _StylePlotVarIDName[493:510]: StylePlotVarCOUNT, + _StylePlotVarIDLowerName[493:510]: StylePlotVarCOUNT, +} + +var _StylePlotVarIDNames = []string{ + _StylePlotVarIDName[0:16], + _StylePlotVarIDName[16:27], + _StylePlotVarIDName[27:43], + _StylePlotVarIDName[43:61], + _StylePlotVarIDName[61:76], + _StylePlotVarIDName[76:95], + _StylePlotVarIDName[95:116], + _StylePlotVarIDName[116:139], + _StylePlotVarIDName[139:159], + _StylePlotVarIDName[159:175], + _StylePlotVarIDName[175:191], + _StylePlotVarIDName[191:210], + _StylePlotVarIDName[210:229], + _StylePlotVarIDName[229:249], + _StylePlotVarIDName[249:269], + _StylePlotVarIDName[269:289], + _StylePlotVarIDName[289:309], + _StylePlotVarIDName[309:321], + _StylePlotVarIDName[321:339], + _StylePlotVarIDName[339:358], + _StylePlotVarIDName[358:383], + _StylePlotVarIDName[383:402], + _StylePlotVarIDName[402:424], + _StylePlotVarIDName[424:447], + _StylePlotVarIDName[447:463], + _StylePlotVarIDName[463:480], + _StylePlotVarIDName[480:493], + _StylePlotVarIDName[493:510], +} + +// StylePlotVarIDString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func StylePlotVarIDString(s string) (StylePlotVarID, error) { + if val, ok := _StylePlotVarIDNameToValueMap[s]; ok { + return val, nil + } + + if val, ok := _StylePlotVarIDNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to StylePlotVarID values", s) +} + +// StylePlotVarIDValues returns all values of the enum +func StylePlotVarIDValues() []StylePlotVarID { + return _StylePlotVarIDValues +} + +// StylePlotVarIDStrings returns a slice of all String values of the enum +func StylePlotVarIDStrings() []string { + strs := make([]string, len(_StylePlotVarIDNames)) + copy(strs, _StylePlotVarIDNames) + return strs +} + +// IsAStylePlotVarID returns "true" if the value is listed in the enum definition. "false" otherwise +func (i StylePlotVarID) IsAStylePlotVarID() bool { + for _, v := range _StylePlotVarIDValues { + if i == v { + return true + } + } + return false +} From 9c06a72f2cd47e6a266315c7a8512189e20e73a4 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 30 Oct 2024 11:48:53 +0100 Subject: [PATCH 2/6] examples/plot: add style setter usage demo --- examples/plot/main.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/plot/main.go b/examples/plot/main.go index 07cfe16c..80aa9205 100644 --- a/examples/plot/main.go +++ b/examples/plot/main.go @@ -8,6 +8,7 @@ import ( "time" g "github.com/AllenDang/giu" + "golang.org/x/image/colornames" ) var ( @@ -38,14 +39,17 @@ func loop() { g.ScatterXY("Time Scatter 时间散点图", timeDataX, timeScatterY), ), g.Row( - g.Plot("Plot Bars"). - Size(500, 250). - AxisLimits(0, 10, -1.2, 1.2, g.ConditionOnce). - Plots( - g.Bar("Plot Bar 柱状图", bardata), - g.Bar("Plot Bar2", bardata2).Shift(0.2), - g.BarH("Plot Bar H 水平柱状图", bardata3), - ), + g.Style().To( + g.Plot("Plot Bars"). + Size(500, 250). + AxisLimits(0, 10, -1.2, 1.2, g.ConditionOnce). + Plots( + g.Bar("Plot Bar 柱状图", bardata), + g.Bar("Plot Bar2", bardata2).Shift(0.2), + g.BarH("Plot Bar H 水平柱状图", bardata3), + ), + ).SetPlotColor(g.StylePlotColorAxisBg, colornames.Cyan). + SetPlotStyleFloat(g.StylePlotVarMajorGridSize, 5), g.Plot("Pie Chart"). Flags(g.PlotFlagsEqual). Size(250, 250). @@ -53,7 +57,10 @@ func loop() { YAxeFlags(g.PlotAxisFlagsNoDecorations, 0, 0). AxisLimits(0, 1, 0, 1, g.ConditionAlways). Plots( - g.PieChart([]string{"Part 1 图例1", "Part 2", "Part 3"}, []float64{0.22, 0.38, 0.4}, 0.5, 0.5, 0.45), + // StyleSetter works also for plots + g.Style().Plots( + g.PieChart([]string{"Part 1 图例1", "Part 2", "Part 3"}, []float64{0.22, 0.38, 0.4}, 0.5, 0.5, 0.45), + ).SetPlotColor(g.StylePlotColorPlotBg, colornames.Pink), ), ), ) From 77899ff10b8da65885811b80664dd9199f7ad765 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 30 Oct 2024 12:01:49 +0100 Subject: [PATCH 3/6] enable plot styling in CSS --- CSS.go | 60 ++++++++++++++++++++++++++++++---- examples/CSS-styling/main.go | 3 ++ examples/CSS-styling/style.css | 2 ++ 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/CSS.go b/CSS.go index 328db99f..c92d7a93 100644 --- a/CSS.go +++ b/CSS.go @@ -91,16 +91,64 @@ func ParseCSSStyleSheet(data []byte) error { } styleColorID, err := StyleColorIDString(styleVarName) - if err != nil { - return ErrCSSParse{What: "style variable ID", Value: styleVarName} + if err == nil { + col, err := csscolorparser.Parse(styleVarValue) + if err != nil { + return ErrCSSParse{What: "color", Value: styleVarValue, Detail: err} + } + + setter.SetColor(styleColorID, col) + + continue } - col, err := csscolorparser.Parse(styleVarValue) - if err != nil { - return ErrCSSParse{What: "color", Value: styleVarValue, Detail: err} + stylePlotVarID, err := StylePlotVarIDString(styleVarName) + if err == nil { + // the style is StyleVarID - set it + f, err2 := strconv.ParseFloat(styleVarValue, 32) + if err2 == nil { + setter.SetPlotStyleFloat(stylePlotVarID, float32(f)) + + continue + } + + // 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} + } + + setter.SetPlotStyle(stylePlotVarID, float32(x), float32(y)) + + continue + } + + stylePlotColorID, err := StylePlotColorIDString(styleVarName) + if err == nil { + 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 diff --git a/examples/CSS-styling/main.go b/examples/CSS-styling/main.go index 452fdbed..5a2dc990 100644 --- a/examples/CSS-styling/main.go +++ b/examples/CSS-styling/main.go @@ -19,6 +19,9 @@ func loop() { giu.CSSTag("label").To( giu.Label("I'ma normal label"), ), + giu.Plot("styled plot").Plots( + giu.Line("Plot 1", []float64{0, 1, 2, 3, 4, 5}), + ), ) } diff --git a/examples/CSS-styling/style.css b/examples/CSS-styling/style.css index 4b9330be..2eb1563d 100644 --- a/examples/CSS-styling/style.css +++ b/examples/CSS-styling/style.css @@ -1,6 +1,8 @@ main { background-color: blue; frame-padding: 80, 20; + plot-line: red; + plot-line-weight: 5; } label { From f0ef8af4de75a3aa65d8ed23c36e5ed0df88aea9 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 30 Oct 2024 12:15:04 +0100 Subject: [PATCH 4/6] setstyle: remove duplicated code" " --- StyleSetter.go | 77 +++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/StyleSetter.go b/StyleSetter.go index a7c04a45..dc1358f8 100644 --- a/StyleSetter.go +++ b/StyleSetter.go @@ -111,6 +111,7 @@ func (ss *StyleSetter) To(widgets ...Widget) *StyleSetter { return ss } +// Plots allows to set plots to apply style for. func (ss *StyleSetter) Plots(widgets ...PlotWidget) *StyleSetter { ss.plots = widgets return ss @@ -163,7 +164,7 @@ func (ss *StyleSetter) Range(rangeFunc func(w Widget)) { // Build implements Widget. func (ss *StyleSetter) Build() { - if ss.layout == nil || len(ss.layout) == -1 { + if len(ss.layout) == 0 { return } @@ -176,7 +177,7 @@ func (ss *StyleSetter) Build() { // Plot implements PlotWidget. func (ss *StyleSetter) Plot() { - if ss.plots == nil || len(ss.plots) == 0 { + if len(ss.plots) == 0 { return } @@ -207,52 +208,20 @@ func (ss *StyleSetter) Push() { // push style vars for k, v := range ss.styles { - if k.IsVec2() { - var value imgui.Vec2 - switch typed := v.(type) { - case imgui.Vec2: - value = typed - case float32: - value = imgui.Vec2{X: typed, Y: typed} - } - - imgui.PushStyleVarVec2(imgui.StyleVar(k), value) - } else { - var value float32 - switch typed := v.(type) { - case float32: - value = typed - case imgui.Vec2: - value = typed.X - } - + pushVarID(k.IsVec2(), v, func(value float32) { imgui.PushStyleVarFloat(imgui.StyleVar(k), value) - } + }, func(value imgui.Vec2) { + imgui.PushStyleVarVec2(imgui.StyleVar(k), value) + }) } // Push plot colors for k, v := range ss.plotStyles { - if k.IsVec2() { - var value imgui.Vec2 - switch typed := v.(type) { - case imgui.Vec2: - value = typed - case float32: - value = imgui.Vec2{X: typed, Y: typed} - } - - implot.PlotPushStyleVarVec2(implot.PlotStyleVar(k), value) - } else { - var value float32 - switch typed := v.(type) { - case float32: - value = typed - case imgui.Vec2: - value = typed.X - } - + pushVarID(k.IsVec2(), v, func(value float32) { implot.PlotPushStyleVarFloat(implot.PlotStyleVar(k), value) - } + }, func(value imgui.Vec2) { + implot.PlotPushStyleVarVec2(implot.PlotStyleVar(k), value) + }) } // push font @@ -280,3 +249,27 @@ func (ss *StyleSetter) Pop() { imgui.PopStyleVarV(int32(len(ss.styles))) implot.PlotPopStyleVarV(int32(len(ss.plotStyles))) } + +func pushVarID(isVec2 bool, v any, pushFloat func(float32), pushVec2 func(imgui.Vec2)) { + if isVec2 { + var value imgui.Vec2 + switch typed := v.(type) { + case imgui.Vec2: + value = typed + case float32: + value = imgui.Vec2{X: typed, Y: typed} + } + + pushVec2(value) + } else { + var value float32 + switch typed := v.(type) { + case float32: + value = typed + case imgui.Vec2: + value = typed.X + } + + pushFloat(value) + } +} From 669e4afc30599a06ac956b8b8c9d646aec61f7ce Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 30 Oct 2024 12:21:00 +0100 Subject: [PATCH 5/6] CSS: fix some golangci-lint issues --- CSS.go | 100 ++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/CSS.go b/CSS.go index c92d7a93..e3f7de11 100644 --- a/CSS.go +++ b/CSS.go @@ -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 @@ -58,35 +60,14 @@ 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 - } - - // 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} + if err := parseStyleVar(styleVarValue, func(v float32) { + setter.SetStyleFloat(styleVarID, v) + }, func(x, y float32) { + setter.SetStyle(styleVarID, x, y) + }); err != nil { + return err } - setter.SetStyle(styleVarID, float32(x), float32(y)) - continue } @@ -104,35 +85,14 @@ func ParseCSSStyleSheet(data []byte) error { stylePlotVarID, err := StylePlotVarIDString(styleVarName) if err == nil { - // the style is StyleVarID - set it - f, err2 := strconv.ParseFloat(styleVarValue, 32) - if err2 == nil { - setter.SetPlotStyleFloat(stylePlotVarID, float32(f)) - - continue - } - - // 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} + if err := parseStyleVar(styleVarValue, func(v float32) { + setter.SetPlotStyleFloat(stylePlotVarID, v) + }, func(x, y float32) { + setter.SetPlotStyle(stylePlotVarID, x, y) + }); err != nil { + return err } - y, err2 := strconv.ParseFloat(vec2[1], 32) - if err2 != nil { - return ErrCSSParse{What: "value (not float)", Value: vec2[1], Detail: err2} - } - - setter.SetPlotStyle(stylePlotVarID, float32(x), float32(y)) - continue } @@ -157,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 From a599be0ebeb91f226ca3a99964a1db43c0b4f9eb Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 30 Oct 2024 12:32:45 +0100 Subject: [PATCH 6/6] fix examples/plot lint --- examples/plot/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/plot/main.go b/examples/plot/main.go index 80aa9205..ef3a92ca 100644 --- a/examples/plot/main.go +++ b/examples/plot/main.go @@ -7,8 +7,9 @@ import ( "math/rand" "time" - g "github.com/AllenDang/giu" "golang.org/x/image/colornames" + + g "github.com/AllenDang/giu" ) var (