From 391fb77219531c5e01c0730273a959e3549f07c0 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Tue, 17 May 2022 11:55:35 +0200 Subject: [PATCH] font atlas: expose tStr and related fix #507 --- Canvas.go | 2 +- ClickableWidgets.go | 14 +++++++------- CodeEditor.go | 2 +- ExtraWidgets.go | 6 +++--- FontAtlasProsessor.go | 17 +++++++++-------- Markdown.go | 2 +- Plot.go | 18 +++++++++--------- Popups.go | 4 ++-- ProgressIndicator.go | 2 +- SliderWidgets.go | 12 ++++++------ TableWidgets.go | 2 +- TextWidgets.go | 18 +++++++++--------- Widgets.go | 24 ++++++++++++------------ Window.go | 2 +- 14 files changed, 63 insertions(+), 62 deletions(-) diff --git a/Canvas.go b/Canvas.go index 5963e991..29b542b5 100644 --- a/Canvas.go +++ b/Canvas.go @@ -75,7 +75,7 @@ func (c *Canvas) AddRectFilled(pMin, pMax image.Point, col color.Color, rounding // AddText draws text. func (c *Canvas) AddText(pos image.Point, col color.Color, text string) { - c.drawlist.AddText(ToVec2(pos), ToVec4Color(col), tStr(text)) + c.drawlist.AddText(ToVec2(pos), ToVec4Color(col), RegisterString(text)) } // AddBezierCubic draws bezier cubic. diff --git a/ClickableWidgets.go b/ClickableWidgets.go index 9fe611aa..69f2ec4d 100644 --- a/ClickableWidgets.go +++ b/ClickableWidgets.go @@ -63,7 +63,7 @@ func (b *ButtonWidget) Build() { defer imgui.EndDisabled() } - if imgui.ButtonV(tStr(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil { + if imgui.ButtonV(RegisterString(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil { b.onClick() } } @@ -135,7 +135,7 @@ func (b *SmallButtonWidget) OnClick(onClick func()) *SmallButtonWidget { // Build implements Widget interface. func (b *SmallButtonWidget) Build() { - if imgui.SmallButton(tStr(b.id)) && b.onClick != nil { + if imgui.SmallButton(RegisterString(b.id)) && b.onClick != nil { b.onClick() } } @@ -182,7 +182,7 @@ func (b *InvisibleButtonWidget) ID(id string) *InvisibleButtonWidget { // Build implements Widget interface. func (b *InvisibleButtonWidget) Build() { - if imgui.InvisibleButton(tStr(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil { + if imgui.InvisibleButton(RegisterString(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil { b.onClick() } } @@ -371,7 +371,7 @@ func (c *CheckboxWidget) OnChange(onChange func()) *CheckboxWidget { // Build implements Widget interface. func (c *CheckboxWidget) Build() { - if imgui.Checkbox(tStr(c.text), c.selected) && c.onChange != nil { + if imgui.Checkbox(RegisterString(c.text), c.selected) && c.onChange != nil { c.onChange() } } @@ -404,7 +404,7 @@ func (r *RadioButtonWidget) OnChange(onChange func()) *RadioButtonWidget { // Build implements Widget interface. func (r *RadioButtonWidget) Build() { - if imgui.RadioButton(tStr(r.text), r.active) && r.onChange != nil { + if imgui.RadioButton(RegisterString(r.text), r.active) && r.onChange != nil { r.onChange() } } @@ -479,7 +479,7 @@ func (s *SelectableWidget) Build() { s.flags |= SelectableFlagsAllowDoubleClick } - if imgui.SelectableV(tStr(s.label), s.selected, int(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil { + if imgui.SelectableV(RegisterString(s.label), s.selected, int(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil { s.onClick() } @@ -503,7 +503,7 @@ type TreeNodeWidget struct { // TreeNode creates a new tree node widget. func TreeNode(label string) *TreeNodeWidget { return &TreeNodeWidget{ - label: tStr(label), + label: RegisterString(label), flags: 0, layout: nil, eventHandler: nil, diff --git a/CodeEditor.go b/CodeEditor.go index cf3b3178..84921e0a 100644 --- a/CodeEditor.go +++ b/CodeEditor.go @@ -197,7 +197,7 @@ func (ce *CodeEditorWidget) Build() { s := ce.getState() // register text in font atlas - tStr(s.editor.GetText()) + RegisterString(s.editor.GetText()) // build editor s.editor.Render(ce.title, imgui.Vec2{X: ce.width, Y: ce.height}, ce.border) diff --git a/ExtraWidgets.go b/ExtraWidgets.go index 03ff248b..31e50b5a 100644 --- a/ExtraWidgets.go +++ b/ExtraWidgets.go @@ -192,10 +192,10 @@ func (ttr *TreeTableRowWidget) BuildTreeTableRow() { open := false if len(ttr.children) > 0 { - open = imgui.TreeNodeV(tStr(ttr.label), int(ttr.flags)) + open = imgui.TreeNodeV(RegisterString(ttr.label), int(ttr.flags)) } else { ttr.flags |= TreeNodeFlagsLeaf | TreeNodeFlagsNoTreePushOnOpen - imgui.TreeNodeV(tStr(ttr.label), int(ttr.flags)) + imgui.TreeNodeV(RegisterString(ttr.label), int(ttr.flags)) } for _, w := range ttr.layout { @@ -571,7 +571,7 @@ func (d *DatePickerWidget) Build() { const yearButtonSize = 25 Row( - Label(tStr(" Year")), + Label(RegisterString(" Year")), Labelf("%14d", d.date.Year()), Button("-##"+d.id+"year").OnClick(func() { *d.date = d.date.AddDate(-1, 0, 0) diff --git a/FontAtlasProsessor.go b/FontAtlasProsessor.go index 8d05bccb..cf82ecb5 100644 --- a/FontAtlasProsessor.go +++ b/FontAtlasProsessor.go @@ -57,7 +57,7 @@ func initFontAtlasProcessor() { extraFontMap = make(map[string]*imgui.Font) // Pre register numbers - tStr(preRegisterString) + RegisterString(preRegisterString) // Pre-register fonts os := runtime.GOOS @@ -188,9 +188,9 @@ func registerDefaultFonts(fontInfos []FontInfo) { } } -// Register string to font atlas builder. +// RegisterString register string to font atlas builder. // Note only register strings that will be displayed on the UI. -func tStr(str string) string { +func RegisterString(str string) string { for _, s := range str { if _, ok := stringMap.Load(s); !ok { stringMap.Store(s, false) @@ -201,16 +201,17 @@ func tStr(str string) string { return str } -// Register string pointer to font atlas builder. +// RegisterStringPointer registers string pointer to font atlas builder. // Note only register strings that will be displayed on the UI. -func tStrPtr(str *string) *string { - tStr(*str) +func RegisterStringPointer(str *string) *string { + RegisterString(*str) return str } -func tStrSlice(str []string) []string { +// RegisterStringSlice calls RegisterString for each slice element +func RegisterStringSlice(str []string) []string { for _, s := range str { - tStr(s) + RegisterString(s) } return str diff --git a/Markdown.go b/Markdown.go index 018f12c7..aa86e662 100644 --- a/Markdown.go +++ b/Markdown.go @@ -59,7 +59,7 @@ func (m *MarkdownWidget) Header(level int, font *FontInfo, separator bool) *Mark // Build implements Widget interface. func (m *MarkdownWidget) Build() { - imgui.Markdown(tStrPtr(m.md), m.linkCb, loadImage, m.headers) + imgui.Markdown(RegisterStringPointer(m.md), m.linkCb, loadImage, m.headers) } func loadImage(path string) imgui.MarkdownImageData { diff --git a/Plot.go b/Plot.go index 67922266..f085369e 100644 --- a/Plot.go +++ b/Plot.go @@ -176,11 +176,11 @@ func (p *PlotCanvasWidget) Build() { } if imgui.ImPlotBegin( - tStr(p.title), tStr(p.xLabel), - tStr(p.yLabel), ToVec2(image.Pt(p.width, p.height)), + RegisterString(p.title), RegisterString(p.xLabel), + RegisterString(p.yLabel), ToVec2(image.Pt(p.width, p.height)), imgui.ImPlotFlags(p.flags), imgui.ImPlotAxisFlags(p.xFlags), imgui.ImPlotAxisFlags(p.yFlags), imgui.ImPlotAxisFlags(p.y2Flags), - imgui.ImPlotAxisFlags(p.y3Flags), tStr(p.y2Label), tStr(p.y3Label), + imgui.ImPlotAxisFlags(p.y3Flags), RegisterString(p.y2Label), RegisterString(p.y3Label), ) { for _, plot := range p.plots { plot.Plot() @@ -273,7 +273,7 @@ func (p *PlotBarHWidget) Offset(offset int) *PlotBarHWidget { // Plot implements plot interface. func (p *PlotBarHWidget) Plot() { - imgui.ImPlotBarsH(tStr(p.title), p.data, p.height, p.shift, p.offset) + imgui.ImPlotBarsH(RegisterString(p.title), p.data, p.height, p.shift, p.offset) } // PlotLineWidget represents a plot line (linear chart). @@ -323,7 +323,7 @@ func (p *PlotLineWidget) Offset(offset int) *PlotLineWidget { // Plot implements Plot interface. func (p *PlotLineWidget) Plot() { imgui.ImPlotSetPlotYAxis(imgui.ImPlotYAxis(p.yAxis)) - imgui.ImPlotLine(tStr(p.title), p.values, p.xScale, p.x0, p.offset) + imgui.ImPlotLine(RegisterString(p.title), p.values, p.xScale, p.x0, p.offset) } // PlotLineXYWidget adds XY plot line. @@ -359,7 +359,7 @@ func (p *PlotLineXYWidget) Offset(offset int) *PlotLineXYWidget { // Plot implements Plot interface. func (p *PlotLineXYWidget) Plot() { imgui.ImPlotSetPlotYAxis(imgui.ImPlotYAxis(p.yAxis)) - imgui.ImPlotLineXY(tStr(p.title), p.xs, p.ys, p.offset) + imgui.ImPlotLineXY(RegisterString(p.title), p.xs, p.ys, p.offset) } // PlotPieChartWidget represents a pie chart. @@ -403,7 +403,7 @@ func (p *PlotPieChartWidget) Angle0(a float64) *PlotPieChartWidget { } func (p *PlotPieChartWidget) Plot() { - imgui.ImPlotPieChart(tStrSlice(p.labels), p.values, p.x, p.y, p.radius, p.normalize, p.labelFormat, p.angle0) + imgui.ImPlotPieChart(RegisterStringSlice(p.labels), p.values, p.x, p.y, p.radius, p.normalize, p.labelFormat, p.angle0) } type PlotScatterWidget struct { @@ -439,7 +439,7 @@ func (p *PlotScatterWidget) Offset(offset int) *PlotScatterWidget { } func (p *PlotScatterWidget) Plot() { - imgui.ImPlotScatter(tStr(p.label), p.values, p.xscale, p.x0, p.offset) + imgui.ImPlotScatter(RegisterString(p.label), p.values, p.xscale, p.x0, p.offset) } type PlotScatterXYWidget struct { @@ -463,5 +463,5 @@ func (p *PlotScatterXYWidget) Offset(offset int) *PlotScatterXYWidget { } func (p *PlotScatterXYWidget) Plot() { - imgui.ImPlotScatterXY(tStr(p.label), p.xs, p.ys, p.offset) + imgui.ImPlotScatterXY(RegisterString(p.label), p.xs, p.ys, p.offset) } diff --git a/Popups.go b/Popups.go index 95d7e84d..87763139 100644 --- a/Popups.go +++ b/Popups.go @@ -29,7 +29,7 @@ type PopupWidget struct { // Popup creates new popup widget. func Popup(name string) *PopupWidget { return &PopupWidget{ - name: tStr(name), + name: RegisterString(name), flags: 0, layout: nil, } @@ -69,7 +69,7 @@ type PopupModalWidget struct { // PopupModal creates new popup modal widget. func PopupModal(name string) *PopupModalWidget { return &PopupModalWidget{ - name: tStr(name), + name: RegisterString(name), open: nil, flags: WindowFlagsNoResize, layout: nil, diff --git a/ProgressIndicator.go b/ProgressIndicator.go index 009005b2..cf83c4a9 100644 --- a/ProgressIndicator.go +++ b/ProgressIndicator.go @@ -95,7 +95,7 @@ func (p *ProgressIndicatorWidget) Build() { // Draw text if len(p.label) > 0 { - labelWidth, _ := CalcTextSize(tStr(p.label)) + labelWidth, _ := CalcTextSize(RegisterString(p.label)) labelPos := centerPt.Add(image.Pt(-1*int(labelWidth/2), int(p.radius+p.radius/5+8))) canvas.AddText(labelPos, rgba, p.label) } diff --git a/SliderWidgets.go b/SliderWidgets.go index 109d5c6a..80a94da9 100644 --- a/SliderWidgets.go +++ b/SliderWidgets.go @@ -55,7 +55,7 @@ func (s *SliderIntWidget) OnChange(onChange func()) *SliderIntWidget { // Label sets slider label (id). func (s *SliderIntWidget) Label(label string) *SliderIntWidget { - s.label = tStr(label) + s.label = RegisterString(label) return s } @@ -71,7 +71,7 @@ func (s *SliderIntWidget) Build() { defer PopItemWidth() } - if imgui.SliderIntV(tStr(s.label), s.value, s.min, s.max, s.format) && s.onChange != nil { + if imgui.SliderIntV(RegisterString(s.label), s.value, s.min, s.max, s.format) && s.onChange != nil { s.onChange() } } @@ -131,7 +131,7 @@ func (vs *VSliderIntWidget) OnChange(onChange func()) *VSliderIntWidget { // Label sets slider's label (id). func (vs *VSliderIntWidget) Label(label string) *VSliderIntWidget { - vs.label = tStr(label) + vs.label = RegisterString(label) return vs } @@ -143,7 +143,7 @@ func (vs *VSliderIntWidget) Labelf(format string, args ...any) *VSliderIntWidget // Build implements Widget interface. func (vs *VSliderIntWidget) Build() { if imgui.VSliderIntV( - tStr(vs.label), + RegisterString(vs.label), imgui.Vec2{X: vs.width, Y: vs.height}, vs.value, vs.min, @@ -204,7 +204,7 @@ func (sf *SliderFloatWidget) Size(width float32) *SliderFloatWidget { // Label sets slider's label (id). func (sf *SliderFloatWidget) Label(label string) *SliderFloatWidget { - sf.label = tStr(label) + sf.label = RegisterString(label) return sf } @@ -220,7 +220,7 @@ func (sf *SliderFloatWidget) Build() { defer PopItemWidth() } - if imgui.SliderFloatV(tStr(sf.label), sf.value, sf.min, sf.max, sf.format, 1.0) && sf.onChange != nil { + if imgui.SliderFloatV(RegisterString(sf.label), sf.value, sf.min, sf.max, sf.format, 1.0) && sf.onChange != nil { sf.onChange() } } diff --git a/TableWidgets.go b/TableWidgets.go index fae228cd..dd5e6330 100644 --- a/TableWidgets.go +++ b/TableWidgets.go @@ -67,7 +67,7 @@ type TableColumnWidget struct { func TableColumn(label string) *TableColumnWidget { return &TableColumnWidget{ - label: tStr(label), + label: RegisterString(label), flags: 0, innerWidthOrWeight: 0, userID: 0, diff --git a/TextWidgets.go b/TextWidgets.go index 7179ca24..8ed52c1c 100644 --- a/TextWidgets.go +++ b/TextWidgets.go @@ -72,8 +72,8 @@ func (i *InputTextMultilineWidget) Size(width, height float32) *InputTextMultili // Build implements Widget interface. func (i *InputTextMultilineWidget) Build() { if imgui.InputTextMultilineV( - tStr(i.label), - tStrPtr(i.text), + RegisterString(i.label), + RegisterStringPointer(i.text), imgui.Vec2{ X: i.width, Y: i.height, @@ -111,7 +111,7 @@ type BulletTextWidget struct { // BulletText creates bulletTextWidget. func BulletText(text string) *BulletTextWidget { return &BulletTextWidget{ - text: tStr(text), + text: RegisterString(text), } } @@ -165,7 +165,7 @@ func InputText(value *string) *InputTextWidget { // Label adds label (alternatively you can use it to set widget's id). func (i *InputTextWidget) Label(label string) *InputTextWidget { - i.label = tStr(label) + i.label = RegisterString(label) return i } @@ -183,7 +183,7 @@ func (i *InputTextWidget) AutoComplete(candidates []string) *InputTextWidget { // Hint sets hint text. func (i *InputTextWidget) Hint(hint string) *InputTextWidget { - i.hint = tStr(hint) + i.hint = RegisterString(hint) return i } @@ -229,7 +229,7 @@ func (i *InputTextWidget) Build() { defer PopItemWidth() } - isChanged := imgui.InputTextWithHint(i.label, i.hint, tStrPtr(i.value), int(i.flags), i.cb) + isChanged := imgui.InputTextWithHint(i.label, i.hint, RegisterStringPointer(i.value), int(i.flags), i.cb) if isChanged && i.onChange != nil { i.onChange() @@ -295,7 +295,7 @@ func InputInt(value *int32) *InputIntWidget { // Label sets label (id). func (i *InputIntWidget) Label(label string) *InputIntWidget { - i.label = tStr(label) + i.label = RegisterString(label) return i } @@ -360,7 +360,7 @@ func InputFloat(value *float32) *InputFloatWidget { // Label sets label of input field. func (i *InputFloatWidget) Label(label string) *InputFloatWidget { - i.label = tStr(label) + i.label = RegisterString(label) return i } @@ -417,7 +417,7 @@ type LabelWidget struct { // Label constructs label widget. func Label(label string) *LabelWidget { return &LabelWidget{ - label: tStr(label), + label: RegisterString(label), wrapped: false, } } diff --git a/Widgets.go b/Widgets.go index 50ef53ae..2ce624da 100644 --- a/Widgets.go +++ b/Widgets.go @@ -130,7 +130,7 @@ type ComboCustomWidget struct { func ComboCustom(label, previewValue string) *ComboCustomWidget { return &ComboCustomWidget{ label: GenAutoID(label), - previewValue: tStr(previewValue), + previewValue: RegisterString(previewValue), width: 0, flags: 0, layout: nil, @@ -162,7 +162,7 @@ func (cc *ComboCustomWidget) Build() { defer imgui.PopItemWidth() } - if imgui.BeginComboV(tStr(cc.label), cc.previewValue, int(cc.flags)) { + if imgui.BeginComboV(RegisterString(cc.label), cc.previewValue, int(cc.flags)) { cc.layout.Build() imgui.EndCombo() } @@ -186,8 +186,8 @@ type ComboWidget struct { func Combo(label, previewValue string, items []string, selected *int32) *ComboWidget { return &ComboWidget{ label: GenAutoID(label), - previewValue: tStr(previewValue), - items: tStrSlice(items), + previewValue: RegisterString(previewValue), + items: RegisterStringSlice(items), selected: selected, flags: 0, width: 0, @@ -202,7 +202,7 @@ func (c *ComboWidget) Build() { defer imgui.PopItemWidth() } - if imgui.BeginComboV(tStr(c.label), c.previewValue, int(c.flags)) { + if imgui.BeginComboV(RegisterString(c.label), c.previewValue, int(c.flags)) { for i, item := range c.items { if imgui.Selectable(item) { *c.selected = int32(i) @@ -307,7 +307,7 @@ func (d *DragIntWidget) Format(format string) *DragIntWidget { // Build implements Widget interface. func (d *DragIntWidget) Build() { - imgui.DragIntV(tStr(d.label), d.value, d.speed, d.min, d.max, d.format) + imgui.DragIntV(RegisterString(d.label), d.value, d.speed, d.min, d.max, d.format) } var _ Widget = &ColumnWidget{} @@ -422,7 +422,7 @@ func (m *MenuItemWidget) OnClick(onClick func()) *MenuItemWidget { // Build implements Widget interface. func (m *MenuItemWidget) Build() { - if imgui.MenuItemV(tStr(m.label), "", m.selected, m.enabled) && m.onClick != nil { + if imgui.MenuItemV(RegisterString(m.label), "", m.selected, m.enabled) && m.onClick != nil { m.onClick() } } @@ -459,7 +459,7 @@ func (m *MenuWidget) Layout(widgets ...Widget) *MenuWidget { // Build implements Widget interface. func (m *MenuWidget) Build() { - if imgui.BeginMenuV(tStr(m.label), m.enabled) { + if imgui.BeginMenuV(RegisterString(m.label), m.enabled) { m.layout.Build() imgui.EndMenu() } @@ -489,7 +489,7 @@ func (p *ProgressBarWidget) Size(width, height float32) *ProgressBarWidget { } func (p *ProgressBarWidget) Overlay(overlay string) *ProgressBarWidget { - p.overlay = tStr(overlay) + p.overlay = RegisterString(overlay) return p } @@ -553,7 +553,7 @@ type TabItemWidget struct { func TabItem(label string) *TabItemWidget { return &TabItemWidget{ - label: tStr(label), + label: RegisterString(label), open: nil, flags: 0, layout: nil, @@ -649,7 +649,7 @@ func (t *TooltipWidget) Build() { func Tooltip(tip string) *TooltipWidget { return &TooltipWidget{ - tip: tStr(tip), + tip: RegisterString(tip), layout: nil, } } @@ -724,7 +724,7 @@ func (ce *ColorEditWidget) Build() { } if imgui.ColorEdit4V( - tStr(ce.label), + RegisterString(ce.label), &col, int(ce.flags), ) { diff --git a/Window.go b/Window.go index 0dcb66a6..18e071e7 100644 --- a/Window.go +++ b/Window.go @@ -134,7 +134,7 @@ func (w *WindowWidget) Layout(widgets ...Widget) { }), ) - showed := imgui.BeginV(tStr(w.title), w.open, int(w.flags)) + showed := imgui.BeginV(RegisterString(w.title), w.open, int(w.flags)) if showed { Layout(widgets).Build()