Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into font-atlas
Browse files Browse the repository at this point in the history
# Conflicts:
#	Canvas.go
#	ClickableWidgets.go
#	CodeEditor.go
#	ExtraWidgets.go
#	FontAtlasProsessor.go
#	Markdown.go
#	Plot.go
#	Popups.go
#	ProgressIndicator.go
#	SliderWidgets.go
#	TableWidgets.go
#	TextWidgets.go
#	Widgets.go
#	Window.go
  • Loading branch information
gucio321 committed Jun 5, 2022
2 parents 524b17a + e5d8974 commit 9fcf375
Show file tree
Hide file tree
Showing 32 changed files with 190 additions and 107 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), Context.FontAtlas.tStr(text))
c.drawlist.AddText(ToVec2(pos), ToVec4Color(col), Context.FontAtlas.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(Context.FontAtlas.tStr(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil {
if imgui.ButtonV(Context.FontAtlas.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(Context.FontAtlas.tStr(b.id)) && b.onClick != nil {
if imgui.SmallButton(Context.FontAtlas.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(Context.FontAtlas.tStr(b.id), imgui.Vec2{X: b.width, Y: b.height}) && b.onClick != nil {
if imgui.InvisibleButton(Context.FontAtlas.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(Context.FontAtlas.tStr(c.text), c.selected) && c.onChange != nil {
if imgui.Checkbox(Context.FontAtlas.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(Context.FontAtlas.tStr(r.text), r.active) && r.onChange != nil {
if imgui.RadioButton(Context.FontAtlas.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(Context.FontAtlas.tStr(s.label), s.selected, int(s.flags), imgui.Vec2{X: s.width, Y: s.height}) && s.onClick != nil {
if imgui.SelectableV(Context.FontAtlas.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: Context.FontAtlas.tStr(label),
label: Context.FontAtlas.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
Context.FontAtlas.tStr(s.editor.GetText())
Context.FontAtlas.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(Context.FontAtlas.tStr(ttr.label), int(ttr.flags))
open = imgui.TreeNodeV(Context.FontAtlas.RegisterString(ttr.label), int(ttr.flags))
} else {
ttr.flags |= TreeNodeFlagsLeaf | TreeNodeFlagsNoTreePushOnOpen
imgui.TreeNodeV(Context.FontAtlas.tStr(ttr.label), int(ttr.flags))
imgui.TreeNodeV(Context.FontAtlas.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(Context.FontAtlas.tStr(" Year")),
Label(Context.FontAtlas.RegisterString(" Year")),
Labelf("%14d", d.date.Year()),
Button("-##"+d.id+"year").OnClick(func() {
*d.date = d.date.AddDate(-1, 0, 0)
Expand Down
18 changes: 9 additions & 9 deletions FontAtlasProsessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
const (
preRegisterString = " \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
windows = "windows"
defaultFontSize = 14
)

// FontInfo represents a giu implementation of imgui font.
Expand Down Expand Up @@ -61,7 +60,7 @@ func newFontAtlas() FontAtlas {
}

// Pre register numbers
result.tStr(preRegisterString)
result.RegisterString(preRegisterString)

// Pre-register fonts
switch runtime.GOOS {
Expand Down Expand Up @@ -193,9 +192,9 @@ func (a *FontAtlas) 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 (a *FontAtlas) tStr(str string) string {
func (a *FontAtlas) RegisterString(str string) string {
for _, s := range str {
if _, ok := a.stringMap.Load(s); !ok {
a.stringMap.Store(s, false)
Expand All @@ -206,16 +205,17 @@ func (a *FontAtlas) 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 (a *FontAtlas) tStrPtr(str *string) *string {
a.tStr(*str)
func (a *FontAtlas) RegisterStringPointer(str *string) *string {
a.RegisterString(*str)
return str
}

func (a *FontAtlas) tStrSlice(str []string) []string {
// RegisterStringSlice calls RegisterString for each slice element
func (a *FontAtlas) RegisterStringSlice(str []string) []string {
for _, s := range str {
a.tStr(s)
a.RegisterString(s)
}

return str
Expand Down
26 changes: 19 additions & 7 deletions ImageWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ImageWidget struct {
texture *Texture
width float32
height float32
uv0, uv1 image.Point
uv0, uv1 imgui.Vec2
tintColor, borderColor color.Color
onClick func()
}
Expand All @@ -32,16 +32,16 @@ func Image(texture *Texture) *ImageWidget {
texture: texture,
width: 100,
height: 100,
uv0: image.Point{X: 0, Y: 0},
uv1: image.Point{X: 1, Y: 1},
uv0: imgui.Vec2{X: 0, Y: 0},
uv1: imgui.Vec2{X: 1, Y: 1},
tintColor: color.RGBA{255, 255, 255, 255},
borderColor: color.RGBA{0, 0, 0, 0},
}
}

// Uv allows to specify uv parameters.
func (i *ImageWidget) Uv(uv0, uv1 image.Point) *ImageWidget {
i.uv0, i.uv1 = uv0, uv1
func (i *ImageWidget) Uv(uv0X, uv0Y, uv1X, uv1Y float32) *ImageWidget {
i.uv0.X, i.uv0.Y, i.uv1.X, i.uv1.Y = uv0X, uv0Y, uv1X, uv1Y
return i
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func (i *ImageWidget) Build() {
}

// trick: detect click event
if i.onClick != nil && IsMouseClicked(MouseButtonLeft) {
if i.onClick != nil && IsMouseClicked(MouseButtonLeft) && IsWindowFocused(0) {
cursorPos := GetCursorScreenPos()
mousePos := GetMousePos()
mousePos.Add(cursorPos)
Expand All @@ -98,7 +98,7 @@ func (i *ImageWidget) Build() {
}
}

imgui.ImageV(i.texture.id, size, ToVec2(i.uv0), ToVec2(i.uv1), ToVec4Color(i.tintColor), ToVec4Color(i.borderColor))
imgui.ImageV(i.texture.id, size, i.uv0, i.uv1, ToVec4Color(i.tintColor), ToVec4Color(i.borderColor))
}

type imageState struct {
Expand Down Expand Up @@ -138,6 +138,12 @@ func ImageWithRgba(rgba image.Image) *ImageWithRgbaWidget {
}
}

// ID sets the interval id of ImageWithRgba widgets.
func (i *ImageWithRgbaWidget) ID(id string) *ImageWithRgbaWidget {
i.id = id
return i
}

// Size sets image's size.
func (i *ImageWithRgbaWidget) Size(width, height float32) *ImageWithRgbaWidget {
i.img.Size(width, height)
Expand Down Expand Up @@ -195,6 +201,12 @@ func ImageWithFile(imgPath string) *ImageWithFileWidget {
}
}

// ID sets the interval id of ImageWithFile widgets.
func (i *ImageWithFileWidget) ID(id string) *ImageWithFileWidget {
i.id = id
return i
}

// Size sets image's size.
func (i *ImageWithFileWidget) Size(width, height float32) *ImageWithFileWidget {
i.img.Size(width, height)
Expand Down
10 changes: 8 additions & 2 deletions InputHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
LocalShortcut ShortcutType = false
)

type InputHandlerHandleCallback func(Key, Modifier, Action)

// InputHandler is an interface which needs to be implemented
// by user-definied input handlers.
type InputHandler interface {
Expand All @@ -37,7 +39,7 @@ type InputHandler interface {
// UnregisterKeyboardShortcuts removes iwndow shourtcuts from input handler
UnregisterWindowShortcuts()
// Handle handles a shortcut
Handle(Key, Modifier)
Handle(Key, Modifier, Action)
}

// --- Default implementation of giu input manager ---
Expand Down Expand Up @@ -79,7 +81,11 @@ func (i *inputHandler) UnregisterWindowShortcuts() {
}
}

func (i *inputHandler) Handle(key Key, mod Modifier) {
func (i *inputHandler) Handle(key Key, mod Modifier, a Action) {
if a != Press {
return
}

for combo, cb := range i.shortcuts {
if combo.key != key || combo.modifier != mod {
continue
Expand Down
6 changes: 3 additions & 3 deletions InputHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ func Test_InputHandler_Handle(t *testing.T) {

i.RegisterKeyboardShortcuts(sh...)

i.Handle(Key(0), Modifier(0))
i.Handle(Key(0), Modifier(0), Press)
a.False(shortcut1, "Shortcut 1 was handled, but shouldn't.")
a.False(shortcut2, "Shortcut 2 was handled, but shouldn't.")
i.Handle(Key(5), Modifier(0))
i.Handle(Key(5), Modifier(0), Press)
a.True(shortcut1, "Shortcut 1 was not handled, but shouldn be.")
a.False(shortcut2, "Shortcut 2 was handled, but shouldn't.")
i.Handle(Key(8), Modifier(2))
i.Handle(Key(8), Modifier(2), Press)
a.True(shortcut1, "Shortcut 1 was not handled, but shouldn be.")
a.True(shortcut2, "Shortcut 2 was not handled, but shouldn be.")
}
8 changes: 8 additions & 0 deletions Keycode.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ const (
ModCapsLock Modifier = Modifier(glfw.ModCapsLock)
ModNumLock Modifier = Modifier(glfw.ModNumLock)
)

type Action glfw.Action

const (
Release Action = Action(glfw.Release)
Press Action = Action(glfw.Press)
Repeat Action = Action(glfw.Repeat)
)
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(Context.FontAtlas.tStrPtr(m.md), m.linkCb, loadImage, m.headers)
imgui.Markdown(Context.FontAtlas.RegisterStringPointer(m.md), m.linkCb, loadImage, m.headers)
}

func loadImage(path string) imgui.MarkdownImageData {
Expand Down
16 changes: 14 additions & 2 deletions MasterWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type MasterWindow struct {
context *imgui.Context
io *imgui.IO
updateFunc func()

// possibility to expend InputHandler's stuff
// See SetAdditionalInputHandler
additionalInputCallback InputHandlerHandleCallback
}

// NewMasterWindow creates a new master window and initializes GLFW.
Expand Down Expand Up @@ -376,8 +380,16 @@ func (w *MasterWindow) SetShouldClose(v bool) {
func (w *MasterWindow) SetInputHandler(handler InputHandler) {
Context.InputHandler = handler
w.platform.SetInputCallback(func(key glfw.Key, modifier glfw.ModifierKey, action glfw.Action) {
if action == glfw.Press {
handler.Handle(Key(key), Modifier(modifier))
k, m, a := Key(key), Modifier(modifier), Action(action)
handler.Handle(k, m, a)
if w.additionalInputCallback != nil {
w.additionalInputCallback(k, m, a)
}
})
}

// SetAdditionalInputHandlerCallback allows to set an input callback to handle more events (not only these from giu.inputHandler).
// See examples/issue-501.
func (w *MasterWindow) SetAdditionalInputHandlerCallback(cb InputHandlerHandleCallback) {
w.additionalInputCallback = cb
}
Loading

0 comments on commit 9fcf375

Please sign in to comment.