Skip to content

Commit

Permalink
font atlas: expose tStr and related
Browse files Browse the repository at this point in the history
fix #507
  • Loading branch information
gucio321 committed May 17, 2022
1 parent 4014052 commit 391fb77
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions ClickableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion CodeEditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions ExtraWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions FontAtlasProsessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions Plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions Popups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ProgressIndicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions SliderWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()
}
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand All @@ -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()
}
}
2 changes: 1 addition & 1 deletion TableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions TextWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -111,7 +111,7 @@ type BulletTextWidget struct {
// BulletText creates bulletTextWidget.
func BulletText(text string) *BulletTextWidget {
return &BulletTextWidget{
text: tStr(text),
text: RegisterString(text),
}
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
}
}
Expand Down
Loading

0 comments on commit 391fb77

Please sign in to comment.