From cd8e82c64e0ad7215a174f9acf84e4db66b6e693 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Thu, 21 Oct 2021 18:37:24 +0200 Subject: [PATCH] FontAtlasProssesor: refactoring - add FontAtlas type - put font atlas in context - make all FontAtlasProssesor functions a methods of FontAtlas --- Canvas.go | 2 +- ClickableWidgets.go | 14 ++-- CodeEditor.go | 2 +- Context.go | 1 + ExtraWidgets.go | 6 +- FontAtlasProsessor.go | 107 ++++++++++++------------ MasterWindow.go | 10 ++- Plot.go | 18 ++-- Popups.go | 4 +- ProgressIndicator.go | 2 +- SliderWidgets.go | 12 +-- Style.go | 6 +- TableWidgets.go | 2 +- TextWidgets.go | 18 ++-- Widgets.go | 24 +++--- Window.go | 2 +- examples/multiplefonts/multiplefonts.go | 4 +- examples/pushfont/main.go | 2 +- 18 files changed, 121 insertions(+), 115 deletions(-) diff --git a/Canvas.go b/Canvas.go index 0870c17e..603df6f2 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), Context.FontAtlas.tStr(text)) } // AddBezierCubic draws bezier cubic. diff --git a/ClickableWidgets.go b/ClickableWidgets.go index 8f586578..b2405c8e 100644 --- a/ClickableWidgets.go +++ b/ClickableWidgets.go @@ -27,7 +27,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(Context.FontAtlas.tStr(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil { b.onClick() } } @@ -130,7 +130,7 @@ func SmallButtonf(format string, args ...interface{}) *SmallButtonWidget { // Build implements Widget interface. func (b *SmallButtonWidget) Build() { - if imgui.SmallButton(tStr(b.id)) && b.onClick != nil { + if imgui.SmallButton(Context.FontAtlas.tStr(b.id)) && b.onClick != nil { b.onClick() } } @@ -170,7 +170,7 @@ func InvisibleButton() *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(Context.FontAtlas.tStr(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil { b.onClick() } } @@ -325,7 +325,7 @@ type CheckboxWidget struct { // Build implements Widget interface. func (c *CheckboxWidget) Build() { - if imgui.Checkbox(tStr(c.text), c.selected) && c.onChange != nil { + if imgui.Checkbox(Context.FontAtlas.tStr(c.text), c.selected) && c.onChange != nil { c.onChange() } } @@ -355,7 +355,7 @@ type RadioButtonWidget struct { // Build implements Widget interface. func (r *RadioButtonWidget) Build() { - if imgui.RadioButton(tStr(r.text), r.active) && r.onChange != nil { + if imgui.RadioButton(Context.FontAtlas.tStr(r.text), r.active) && r.onChange != nil { r.onChange() } } @@ -434,7 +434,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(Context.FontAtlas.tStr(s.label), s.selected, int(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil { s.onClick() } @@ -454,7 +454,7 @@ type TreeNodeWidget struct { func TreeNode(label string) *TreeNodeWidget { return &TreeNodeWidget{ - label: tStr(label), + label: Context.FontAtlas.tStr(label), flags: 0, layout: nil, eventHandler: nil, diff --git a/CodeEditor.go b/CodeEditor.go index 366be398..442f843a 100644 --- a/CodeEditor.go +++ b/CodeEditor.go @@ -192,7 +192,7 @@ func (ce *CodeEditorWidget) Build() { s := ce.getState() // register text in font atlas - tStr(s.editor.GetText()) + Context.FontAtlas.tStr(s.editor.GetText()) // build editor s.editor.Render(ce.title, imgui.Vec2{X: ce.width, Y: ce.height}, ce.border) diff --git a/Context.go b/Context.go index 4ff53811..6498084c 100644 --- a/Context.go +++ b/Context.go @@ -31,6 +31,7 @@ type context struct { state sync.Map InputHandler InputHandler + FontAtlas FontAtlas } func (c *context) GetRenderer() imgui.Renderer { diff --git a/ExtraWidgets.go b/ExtraWidgets.go index b97f3dfa..1f537796 100644 --- a/ExtraWidgets.go +++ b/ExtraWidgets.go @@ -194,10 +194,10 @@ func (ttr *TreeTableRowWidget) Build() { open := false if len(ttr.children) > 0 { - open = imgui.TreeNodeV(tStr(ttr.label), int(ttr.flags)) + open = imgui.TreeNodeV(Context.FontAtlas.tStr(ttr.label), int(ttr.flags)) } else { ttr.flags |= TreeNodeFlagsLeaf | TreeNodeFlagsNoTreePushOnOpen - imgui.TreeNodeV(tStr(ttr.label), int(ttr.flags)) + imgui.TreeNodeV(Context.FontAtlas.tStr(ttr.label), int(ttr.flags)) } for _, w := range ttr.layout { @@ -544,7 +544,7 @@ func (d *DatePickerWidget) Build() { const yearButtonSize = 25 Row( - Label(tStr(" Year")), + Label(Context.FontAtlas.tStr(" 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 85f99982..7aeb722a 100644 --- a/FontAtlasProsessor.go +++ b/FontAtlasProsessor.go @@ -11,14 +11,6 @@ import ( "github.com/AllenDang/imgui-go" ) -var ( - shouldRebuildFontAtlas bool - stringMap sync.Map // key is rune, value indicates whether it's a new rune. - defaultFonts []FontInfo - extraFonts []FontInfo - extraFontMap map[string]*imgui.Font -) - const ( preRegisterString = " \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" windows = "windows" @@ -36,36 +28,45 @@ func (f *FontInfo) String() string { return fmt.Sprintf("%s:%.2f", f.fontName, f.size) } -func init() { - extraFontMap = make(map[string]*imgui.Font) +type FontAtlas struct { + shouldRebuildFontAtlas bool + stringMap sync.Map // key is rune, value indicates whether it's a new rune. + defaultFonts []FontInfo + extraFonts []FontInfo + extraFontMap map[string]*imgui.Font +} + +func newFontAtlas() FontAtlas { + result := FontAtlas{ + extraFontMap: make(map[string]*imgui.Font), + } // Pre register numbers - tStr(preRegisterString) + result.tStr(preRegisterString) // Pre-register fonts - os := runtime.GOOS - switch os { + switch runtime.GOOS { case "darwin": // English font - registerDefaultFont("Menlo", 14) + result.registerDefaultFont("Menlo", 14) // Chinese font - registerDefaultFont("STHeiti", 13) + result.registerDefaultFont("STHeiti", 13) // Jananese font - registerDefaultFont("ヒラギノ角ゴシック W0", 17) + result.registerDefaultFont("ヒラギノ角ゴシック W0", 17) // Korean font - registerDefaultFont("AppleSDGothicNeo", 16) + result.registerDefaultFont("AppleSDGothicNeo", 16) case windows: // English font - registerDefaultFont("Calibri", 16) + result.registerDefaultFont("Calibri", 16) // Chinese font - registerDefaultFont("MSYH", 16) + result.registerDefaultFont("MSYH", 16) // Japanese font - registerDefaultFont("MSGOTHIC", 16) + result.registerDefaultFont("MSGOTHIC", 16) // Korean font - registerDefaultFont("MALGUNSL", 16) + result.registerDefaultFont("MALGUNSL", 16) case "linux": // English fonts - registerDefaultFonts([]FontInfo{ + result.registerDefaultFonts([]FontInfo{ { fontName: "FreeSans.ttf", size: 15, @@ -76,10 +77,12 @@ func init() { }, }) } + + return result } // SetDefaultFont changes default font. -func SetDefaultFont(fontName string, size float32) { +func (a *FontAtlas) SetDefaultFont(fontName string, size float32) { fontPath, err := findfont.Find(fontName) if err != nil { log.Fatalf("Cannot find font %s", fontName) @@ -87,22 +90,22 @@ func SetDefaultFont(fontName string, size float32) { } fontInfo := FontInfo{fontName: fontName, fontPath: fontPath, size: size} - defaultFonts = append([]FontInfo{fontInfo}, defaultFonts...) + a.defaultFonts = append([]FontInfo{fontInfo}, a.defaultFonts...) } // SetDefaultFontFromBytes changes default font by bytes of the font file. -func SetDefaultFontFromBytes(fontBytes []byte, size float32) { - defaultFonts = append([]FontInfo{ +func (a *FontAtlas) SetDefaultFontFromBytes(fontBytes []byte, size float32) { + a.defaultFonts = append([]FontInfo{ { fontByte: fontBytes, size: size, }, - }, defaultFonts...) + }, a.defaultFonts...) } // AddFont adds font by name, if the font is found, return *FontInfo, otherwise return nil. // To use added font, use giu.Style().SetFont(...). -func AddFont(fontName string, size float32) *FontInfo { +func (a *FontAtlas) AddFont(fontName string, size float32) *FontInfo { fontPath, err := findfont.Find(fontName) if err != nil { fmt.Printf("[Warning]Cannot find font %s at system, related text will not be rendered.\n", fontName) @@ -115,35 +118,35 @@ func AddFont(fontName string, size float32) *FontInfo { size: size, } - extraFonts = append(extraFonts, fi) + a.extraFonts = append(a.extraFonts, fi) return &fi } // AddFontFromBytes does similar to AddFont, but using data from memory. -func AddFontFromBytes(fontName string, fontBytes []byte, size float32) *FontInfo { +func (a *FontAtlas) AddFontFromBytes(fontName string, fontBytes []byte, size float32) *FontInfo { fi := FontInfo{ fontName: fontName, fontByte: fontBytes, size: size, } - extraFonts = append(extraFonts, fi) + a.extraFonts = append(a.extraFonts, fi) return &fi } -func registerDefaultFont(fontName string, size float32) { +func (a *FontAtlas) registerDefaultFont(fontName string, size float32) { fontPath, err := findfont.Find(fontName) if err != nil { return } fontInfo := FontInfo{fontName: fontName, fontPath: fontPath, size: size} - defaultFonts = append(defaultFonts, fontInfo) + a.defaultFonts = append(a.defaultFonts, fontInfo) } -func registerDefaultFonts(fontInfos []FontInfo) { +func (a *FontAtlas) registerDefaultFonts(fontInfos []FontInfo) { var firstFoundFont *FontInfo for _, fi := range fontInfos { fontPath, err := findfont.Find(fi.fontName) @@ -154,17 +157,17 @@ func registerDefaultFonts(fontInfos []FontInfo) { } if firstFoundFont != nil { - defaultFonts = append(defaultFonts, *firstFoundFont) + a.defaultFonts = append(a.defaultFonts, *firstFoundFont) } } // Register string to font atlas builder. // Note only register strings that will be displayed on the UI. -func tStr(str string) string { +func (a *FontAtlas) tStr(str string) string { for _, s := range str { - if _, ok := stringMap.Load(s); !ok { - stringMap.Store(s, false) - shouldRebuildFontAtlas = true + if _, ok := a.stringMap.Load(s); !ok { + a.stringMap.Store(s, false) + a.shouldRebuildFontAtlas = true } } @@ -173,22 +176,22 @@ func tStr(str string) string { // Register 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 (a *FontAtlas) tStrPtr(str *string) *string { + a.tStr(*str) return str } -func tStrSlice(str []string) []string { +func (a *FontAtlas) tStrSlice(str []string) []string { for _, s := range str { - tStr(s) + a.tStr(s) } return str } // Rebuild font atlas when necessary. -func rebuildFontAtlas() { - if !shouldRebuildFontAtlas { +func (a *FontAtlas) rebuildFontAtlas() { + if !a.shouldRebuildFontAtlas { return } @@ -197,8 +200,8 @@ func rebuildFontAtlas() { var sb strings.Builder - stringMap.Range(func(k, v interface{}) bool { - stringMap.Store(k, true) + a.stringMap.Range(func(k, v interface{}) bool { + a.stringMap.Store(k, true) if ks, ok := k.(rune); ok { sb.WriteRune(ks) } @@ -218,13 +221,13 @@ func rebuildFontAtlas() { builder.BuildRanges(ranges) - if len(defaultFonts) > 0 { + if len(a.defaultFonts) > 0 { fontConfig := imgui.NewFontConfig() fontConfig.SetOversampleH(2) fontConfig.SetOversampleV(2) fontConfig.SetRasterizerMultiply(1.5) - for i, fontInfo := range defaultFonts { + for i, fontInfo := range a.defaultFonts { if i > 0 { fontConfig.SetMergeMode(true) } @@ -250,7 +253,7 @@ func rebuildFontAtlas() { } // Add extra fonts - for _, fontInfo := range extraFonts { + for _, fontInfo := range a.extraFonts { // Scale font size with DPI scale factor if runtime.GOOS == windows { fontInfo.size *= Context.GetPlatform().GetContentScale() @@ -263,11 +266,11 @@ func rebuildFontAtlas() { } else { f = fonts.AddFontFromMemoryTTFV(fontInfo.fontByte, fontInfo.size, imgui.DefaultFontConfig, ranges.Data()) } - extraFontMap[fontInfo.String()] = &f + a.extraFontMap[fontInfo.String()] = &f } fontTextureImg := fonts.TextureDataRGBA32() Context.renderer.SetFontTexture(fontTextureImg) - shouldRebuildFontAtlas = false + a.shouldRebuildFontAtlas = false } diff --git a/MasterWindow.go b/MasterWindow.go index 280da5ae..5dd9d0c1 100644 --- a/MasterWindow.go +++ b/MasterWindow.go @@ -75,14 +75,16 @@ func NewMasterWindow(title string, width, height int, flags MasterWindowFlags) * // Create context Context.renderer = r + Context.FontAtlas = newFontAtlas() + // Create font - if len(defaultFonts) == 0 { + if len(Context.FontAtlas.defaultFonts) == 0 { io.Fonts().AddFontDefault() fontAtlas := io.Fonts().TextureDataRGBA32() r.SetFontTexture(fontAtlas) } else { - shouldRebuildFontAtlas = true - rebuildFontAtlas() + Context.FontAtlas.shouldRebuildFontAtlas = true + Context.FontAtlas.rebuildFontAtlas() } mw := &MasterWindow{ @@ -189,7 +191,7 @@ func (w *MasterWindow) sizeChange(width, height int) { func (w *MasterWindow) render() { Context.invalidAllState() - rebuildFontAtlas() + Context.FontAtlas.rebuildFontAtlas() p := w.platform r := w.renderer diff --git a/Plot.go b/Plot.go index 67922266..9cffd484 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)), + Context.FontAtlas.tStr(p.title), Context.FontAtlas.tStr(p.xLabel), + Context.FontAtlas.tStr(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), Context.FontAtlas.tStr(p.y2Label), Context.FontAtlas.tStr(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(Context.FontAtlas.tStr(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(Context.FontAtlas.tStr(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(Context.FontAtlas.tStr(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(Context.FontAtlas.tStrSlice(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(Context.FontAtlas.tStr(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(Context.FontAtlas.tStr(p.label), p.xs, p.ys, p.offset) } diff --git a/Popups.go b/Popups.go index fadfd1aa..d2a85e22 100644 --- a/Popups.go +++ b/Popups.go @@ -22,7 +22,7 @@ type PopupWidget struct { func Popup(name string) *PopupWidget { return &PopupWidget{ - name: tStr(name), + name: Context.FontAtlas.tStr(name), flags: 0, layout: nil, } @@ -57,7 +57,7 @@ type PopupModalWidget struct { func PopupModal(name string) *PopupModalWidget { return &PopupModalWidget{ - name: tStr(name), + name: Context.FontAtlas.tStr(name), open: nil, flags: WindowFlagsNoResize, layout: nil, diff --git a/ProgressIndicator.go b/ProgressIndicator.go index 009005b2..db9b63d5 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(Context.FontAtlas.tStr(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 d07f72ff..c070a3dc 100644 --- a/SliderWidgets.go +++ b/SliderWidgets.go @@ -47,7 +47,7 @@ func (s *SliderIntWidget) OnChange(onChange func()) *SliderIntWidget { } func (s *SliderIntWidget) Label(label string) *SliderIntWidget { - s.label = tStr(label) + s.label = Context.FontAtlas.tStr(label) return s } @@ -62,7 +62,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(Context.FontAtlas.tStr(s.label), s.value, s.min, s.max, s.format) && s.onChange != nil { s.onChange() } } @@ -115,7 +115,7 @@ func (vs *VSliderIntWidget) OnChange(onChange func()) *VSliderIntWidget { } func (vs *VSliderIntWidget) Label(label string) *VSliderIntWidget { - vs.label = tStr(label) + vs.label = Context.FontAtlas.tStr(label) return vs } @@ -126,7 +126,7 @@ func (vs *VSliderIntWidget) Labelf(format string, args ...interface{}) *VSliderI // Build implements Widget interface. func (vs *VSliderIntWidget) Build() { if imgui.VSliderIntV( - tStr(vs.label), + Context.FontAtlas.tStr(vs.label), imgui.Vec2{X: vs.width, Y: vs.height}, vs.value, vs.min, @@ -179,7 +179,7 @@ func (sf *SliderFloatWidget) Size(width float32) *SliderFloatWidget { } func (sf *SliderFloatWidget) Label(label string) *SliderFloatWidget { - sf.label = tStr(label) + sf.label = Context.FontAtlas.tStr(label) return sf } @@ -194,7 +194,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(Context.FontAtlas.tStr(sf.label), sf.value, sf.min, sf.max, sf.format, 1.0) && sf.onChange != nil { sf.onChange() } } diff --git a/Style.go b/Style.go index 381f237c..e0681cf1 100644 --- a/Style.go +++ b/Style.go @@ -14,7 +14,7 @@ func PushFont(font *FontInfo) bool { return false } - if f, ok := extraFontMap[font.String()]; ok { + if f, ok := Context.FontAtlas.extraFontMap[font.String()]; ok { imgui.PushFont(*f) return true } @@ -397,12 +397,12 @@ func (ss *StyleSetter) SetFontSize(size float32) *StyleSetter { if ss.font != nil { font = *ss.font } else { - font = defaultFonts[0] + font = Context.FontAtlas.defaultFonts[0] } font.size = size - extraFonts = append(extraFonts, font) + Context.FontAtlas.extraFonts = append(Context.FontAtlas.extraFonts, font) ss.font = &font diff --git a/TableWidgets.go b/TableWidgets.go index 338bb56f..b76cfa3c 100644 --- a/TableWidgets.go +++ b/TableWidgets.go @@ -71,7 +71,7 @@ type TableColumnWidget struct { func TableColumn(label string) *TableColumnWidget { return &TableColumnWidget{ - label: tStr(label), + label: Context.FontAtlas.tStr(label), flags: 0, innerWidthOrWeight: 0, userID: 0, diff --git a/TextWidgets.go b/TextWidgets.go index 1db4a483..bbd04b73 100644 --- a/TextWidgets.go +++ b/TextWidgets.go @@ -48,8 +48,8 @@ func (i *InputTextMultilineWidget) Labelf(format string, args ...interface{}) *I // Build implements Widget interface. func (i *InputTextMultilineWidget) Build() { if imgui.InputTextMultilineV( - tStr(i.label), - tStrPtr(i.text), + Context.FontAtlas.tStr(i.label), + Context.FontAtlas.tStrPtr(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: Context.FontAtlas.tStr(text), } } @@ -159,7 +159,7 @@ func InputText(value *string) *InputTextWidget { } func (i *InputTextWidget) Label(label string) *InputTextWidget { - i.label = tStr(label) + i.label = Context.FontAtlas.tStr(label) return i } @@ -175,7 +175,7 @@ func (i *InputTextWidget) AutoComplete(candidates []string) *InputTextWidget { } func (i *InputTextWidget) Hint(hint string) *InputTextWidget { - i.hint = tStr(hint) + i.hint = Context.FontAtlas.tStr(hint) return i } @@ -217,7 +217,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, Context.FontAtlas.tStrPtr(i.value), int(i.flags), i.cb) if isChanged && i.onChange != nil { i.onChange() @@ -277,7 +277,7 @@ func InputInt(value *int32) *InputIntWidget { } func (i *InputIntWidget) Label(label string) *InputIntWidget { - i.label = tStr(label) + i.label = Context.FontAtlas.tStr(label) return i } @@ -335,7 +335,7 @@ func InputFloat(value *float32) *InputFloatWidget { } func (i *InputFloatWidget) Label(label string) *InputFloatWidget { - i.label = tStr(label) + i.label = Context.FontAtlas.tStr(label) return i } @@ -385,7 +385,7 @@ type LabelWidget struct { func Label(label string) *LabelWidget { return &LabelWidget{ - label: tStr(label), + label: Context.FontAtlas.tStr(label), wrapped: false, } } diff --git a/Widgets.go b/Widgets.go index 8c8f5600..47a6d143 100644 --- a/Widgets.go +++ b/Widgets.go @@ -124,7 +124,7 @@ type ComboCustomWidget struct { func ComboCustom(label, previewValue string) *ComboCustomWidget { return &ComboCustomWidget{ label: GenAutoID(label), - previewValue: tStr(previewValue), + previewValue: Context.FontAtlas.tStr(previewValue), width: 0, flags: 0, layout: nil, @@ -156,7 +156,7 @@ func (cc *ComboCustomWidget) Build() { defer imgui.PopItemWidth() } - if imgui.BeginComboV(tStr(cc.label), cc.previewValue, int(cc.flags)) { + if imgui.BeginComboV(Context.FontAtlas.tStr(cc.label), cc.previewValue, int(cc.flags)) { cc.layout.Build() imgui.EndCombo() } @@ -180,8 +180,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: Context.FontAtlas.tStr(previewValue), + items: Context.FontAtlas.tStrSlice(items), selected: selected, flags: 0, width: 0, @@ -196,7 +196,7 @@ func (c *ComboWidget) Build() { defer imgui.PopItemWidth() } - if imgui.BeginComboV(tStr(c.label), c.previewValue, int(c.flags)) { + if imgui.BeginComboV(Context.FontAtlas.tStr(c.label), c.previewValue, int(c.flags)) { for i, item := range c.items { if imgui.Selectable(item) { *c.selected = int32(i) @@ -301,7 +301,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(Context.FontAtlas.tStr(d.label), d.value, d.speed, d.min, d.max, d.format) } var _ Widget = &ColumnWidget{} @@ -416,7 +416,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(Context.FontAtlas.tStr(m.label), "", m.selected, m.enabled) && m.onClick != nil { m.onClick() } } @@ -453,7 +453,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(Context.FontAtlas.tStr(m.label), m.enabled) { m.layout.Build() imgui.EndMenu() } @@ -483,7 +483,7 @@ func (p *ProgressBarWidget) Size(width, height float32) *ProgressBarWidget { } func (p *ProgressBarWidget) Overlay(overlay string) *ProgressBarWidget { - p.overlay = tStr(overlay) + p.overlay = Context.FontAtlas.tStr(overlay) return p } @@ -549,7 +549,7 @@ type TabItemWidget struct { func TabItem(label string) *TabItemWidget { return &TabItemWidget{ - label: tStr(label), + label: Context.FontAtlas.tStr(label), open: nil, flags: 0, layout: nil, @@ -645,7 +645,7 @@ func (t *TooltipWidget) Build() { func Tooltip(tip string) *TooltipWidget { return &TooltipWidget{ - tip: tStr(tip), + tip: Context.FontAtlas.tStr(tip), layout: nil, } } @@ -720,7 +720,7 @@ func (ce *ColorEditWidget) Build() { } if imgui.ColorEdit4V( - tStr(ce.label), + Context.FontAtlas.tStr(ce.label), &col, int(ce.flags), ) { diff --git a/Window.go b/Window.go index 5a343c8d..bcff573e 100644 --- a/Window.go +++ b/Window.go @@ -133,7 +133,7 @@ func (w *WindowWidget) Layout(widgets ...Widget) { }), ) - showed := imgui.BeginV(tStr(w.title), w.open, int(w.flags)) + showed := imgui.BeginV(Context.FontAtlas.tStr(w.title), w.open, int(w.flags)) if showed { Layout(widgets).Build() diff --git a/examples/multiplefonts/multiplefonts.go b/examples/multiplefonts/multiplefonts.go index 391aa66c..b2566027 100644 --- a/examples/multiplefonts/multiplefonts.go +++ b/examples/multiplefonts/multiplefonts.go @@ -33,10 +33,10 @@ func loop() { func main() { // Change the default font - g.SetDefaultFont("Arial.ttf", 12) + g.Context.FontAtlas.SetDefaultFont("Arial.ttf", 12) // Add a new font and manually set it when needed - bigFont = g.AddFont("Menlo.ttc", 24) + bigFont = g.Context.FontAtlas.AddFont("Menlo.ttc", 24) wnd := g.NewMasterWindow("Multiple fonts", 600, 400, g.MasterWindowFlagsNotResizable) wnd.Run(loop) diff --git a/examples/pushfont/main.go b/examples/pushfont/main.go index 1c136d81..b7ecd85a 100644 --- a/examples/pushfont/main.go +++ b/examples/pushfont/main.go @@ -25,7 +25,7 @@ func loop() { func main() { wnd := giu.NewMasterWindow("example", 640, 480, 0) - font = giu.AddFont("NotoSansSinhala-Regular.ttf", 20) + font = giu.Context.FontAtlas.AddFont("NotoSansSinhala-Regular.ttf", 20) fmt.Println(font) wnd.Run(loop) }