Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into context-set-get-s…
Browse files Browse the repository at this point in the history
…tate
  • Loading branch information
gucio321 committed Oct 2, 2024
2 parents b41e102 + 1a9e1dc commit cd898ea
Show file tree
Hide file tree
Showing 28 changed files with 122 additions and 62 deletions.
7 changes: 6 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ linters:
- gocyclo
- err113
- gofmt
#- gofumpt
- gofumpt
- goheader
- goimports
#- gomnd
Expand All @@ -39,14 +39,18 @@ linters:
- prealloc
- predeclared
- promlinter
- reassign
#- revive
- rowserrcheck
- staticcheck
- stylecheck
- tagliatelle
- testifylint
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- wastedassign
- whitespace
- wrapcheck
Expand Down Expand Up @@ -81,6 +85,7 @@ run:
timeout: 5m

issues:
exclude-dirs-use-default: false
skip-dirs:
- .github
- build
Expand Down
1 change: 0 additions & 1 deletion CSS.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func ParseCSSStyleSheet(data []byte) error {
err = panicToErr(func() {
styleColorID = StyleColorIDFromString(styleVarName)
})

if err != nil {
return ErrCSSParse{What: "style variable ID", Value: styleVarName}
}
Expand Down
11 changes: 11 additions & 0 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type context struct {
m *sync.Mutex
}

// CreateContext creates a new giu context.
func CreateContext(b backend.Backend[glfwbackend.GLFWWindowFlags]) *context {
result := context{
cssStylesheet: make(cssStylesheet),
Expand All @@ -91,10 +92,13 @@ func CreateContext(b backend.Backend[glfwbackend.GLFWWindowFlags]) *context {
return &result
}

// IO returns the imgui.IO object.
func (c *context) IO() *imgui.IO {
return imgui.CurrentIO()
}

// invalidAllState should be called before rendering.
// it ensures all states are marked as invalid for that moment.
func (c *context) invalidAllState() {
c.state.Range(func(k, v any) bool {
if s, ok := v.(*state); ok {
Expand All @@ -107,6 +111,8 @@ func (c *context) invalidAllState() {
})
}

// cleanState removes all states that were not marked as valid during rendering.
// should be called after rendering.
func (c *context) cleanState() {
c.state.Range(func(k, v any) bool {
if s, ok := v.(*state); ok {
Expand All @@ -127,18 +133,22 @@ func (c *context) cleanState() {
c.widgetIndex = make(map[string]int)
}

// Backend returns the imgui.backend used by the context.
func (c *context) Backend() backend.Backend[glfwbackend.GLFWWindowFlags] {
return c.backend
}

// SetState is a generic version of Context.SetState.
func SetState[T any, PT genericDisposable[T]](c *context, id ID, data PT) {
c.state.Store(id, &state{valid: true, data: data})
}

// SetState stores data in context by id.
func (c *context) SetState(id ID, data Disposable) {
c.state.Store(id, &state{valid: true, data: data})
}

// GetState is a generic version of Context.GetState.
func GetState[T any, PT genericDisposable[T]](c *context, id ID) PT {
if s, ok := c.load(id); ok {
c.m.Lock()
Expand All @@ -154,6 +164,7 @@ func GetState[T any, PT genericDisposable[T]](c *context, id ID) PT {
return nil
}

// GetState returns previously stored state by id.
func (c *context) GetState(id ID) any {
if s, ok := c.load(id); ok {
c.m.Lock()
Expand Down
2 changes: 1 addition & 1 deletion ImageWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (i *ImageWithURLWidget) Build() {
// Load image from url
client := &http.Client{Timeout: i.downloadTimeout}

req, err := http.NewRequestWithContext(downloadContext, "GET", i.imgURL, http.NoBody)
req, err := http.NewRequestWithContext(downloadContext, http.MethodGet, i.imgURL, http.NoBody)
if err != nil {
errorFn(err)
return
Expand Down
2 changes: 1 addition & 1 deletion MasterWindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (w *MasterWindow) SetShouldClose(v bool) {
func (w *MasterWindow) SetInputHandler(handler InputHandler) {
Context.InputHandler = handler

w.backend.SetKeyCallback(func(key, scanCode, action, modifier int) {
w.backend.SetKeyCallback(func(key, _, action, modifier int) {
k, m, a := keyFromGLFWKey(glfwbackend.GLFWKey(key)), Modifier(modifier), Action(action)
handler.Handle(k, m, a)

Expand Down
21 changes: 21 additions & 0 deletions TableWidgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
"github.com/AllenDang/cimgui-go/imgui"
)

// TableRowWidget represents a row in a table.
type TableRowWidget struct {
flags TableRowFlags
minRowHeight float64
layout Layout
bgColor color.Color
}

// TableRow creates a TbleRowWidget.
// Each widget will be rendered in a separated column.
// NOTE: if you want to put multiple widgets in one cell, enclose them in Layout{}.
func TableRow(widgets ...Widget) *TableRowWidget {
return &TableRowWidget{
flags: 0,
Expand All @@ -22,16 +26,19 @@ func TableRow(widgets ...Widget) *TableRowWidget {
}
}

// BgColor sets the background color of the row.
func (r *TableRowWidget) BgColor(c color.Color) *TableRowWidget {
r.bgColor = c
return r
}

// Flags sets the flags of the row.
func (r *TableRowWidget) Flags(flags TableRowFlags) *TableRowWidget {
r.flags = flags
return r
}

// MinHeight sets the minimum height of the row.
func (r *TableRowWidget) MinHeight(height float64) *TableRowWidget {
r.minRowHeight = height
return r
Expand All @@ -58,13 +65,15 @@ func (r *TableRowWidget) BuildTableRow() {
}
}

// TableColumnWidget allows to configure table columns headers.
type TableColumnWidget struct {
label string
flags TableColumnFlags
innerWidthOrWeight float32
userID uint32
}

// TableColumn creates a new TableColumnWidget.
func TableColumn(label string) *TableColumnWidget {
return &TableColumnWidget{
label: Context.FontAtlas.RegisterString(label),
Expand All @@ -74,16 +83,19 @@ func TableColumn(label string) *TableColumnWidget {
}
}

// Flags sets the flags of the column.
func (c *TableColumnWidget) Flags(flags TableColumnFlags) *TableColumnWidget {
c.flags = flags
return c
}

// InnerWidthOrWeight sets the inner width or weight of the column.
func (c *TableColumnWidget) InnerWidthOrWeight(w float32) *TableColumnWidget {
c.innerWidthOrWeight = w
return c
}

// UserID sets the user id of the column.
func (c *TableColumnWidget) UserID(id uint32) *TableColumnWidget {
c.userID = id
return c
Expand All @@ -96,6 +108,10 @@ func (c *TableColumnWidget) BuildTableColumn() {

var _ Widget = &TableWidget{}

// TableWidget is a table widget.
// - Call Table to create new
// - Then use Rows method to add content
// - Use Columns method to configure columns (optional).
type TableWidget struct {
id ID
flags TableFlags
Expand All @@ -109,6 +125,7 @@ type TableWidget struct {
noHeader bool
}

// Table creates new TableWidget.
func Table() *TableWidget {
return &TableWidget{
id: GenAutoID("Table"),
Expand Down Expand Up @@ -158,21 +175,25 @@ func (t *TableWidget) Columns(cols ...*TableColumnWidget) *TableWidget {
return t
}

// Rows sets the rows of the table.
func (t *TableWidget) Rows(rows ...*TableRowWidget) *TableWidget {
t.rows = rows
return t
}

// Size sets the size of the table.
func (t *TableWidget) Size(width, height float32) *TableWidget {
t.size = imgui.Vec2{X: width, Y: height}
return t
}

// InnerWidth sets the inner width of the table.
func (t *TableWidget) InnerWidth(width float64) *TableWidget {
t.innerWidth = width
return t
}

// Flags sets the flags of the table.
func (t *TableWidget) Flags(flags TableFlags) *TableWidget {
t.flags = flags
return t
Expand Down
48 changes: 28 additions & 20 deletions Widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func SameLine() {

var _ Widget = &ChildWidget{}

// ChildWidget is a container widget. It will have a separated scroll bar.
// Use Child if you want to create a layout of e specific size.
type ChildWidget struct {
id ID
width float32
Expand All @@ -65,36 +67,38 @@ type ChildWidget struct {
layout Layout
}

// Build implements Widget interface.
func (c *ChildWidget) Build() {
if imgui.BeginChildStrV(c.id.String(), imgui.Vec2{X: c.width, Y: c.height}, func() imgui.ChildFlags {
if c.border {
return imgui.ChildFlagsBorders
}

return 0
}(), imgui.WindowFlags(c.flags)) {
c.layout.Build()
// Child creates a new ChildWidget.
func Child() *ChildWidget {
return &ChildWidget{
id: GenAutoID("Child"),
width: 0,
height: 0,
border: true,
flags: 0,
layout: nil,
}

imgui.EndChild()
}

// Border sets whether child should have border
// You can use imgui.ChildFlagsBorders as well.
func (c *ChildWidget) Border(border bool) *ChildWidget {
c.border = border
return c
}

// Size sets child size.
func (c *ChildWidget) Size(width, height float32) *ChildWidget {
c.width, c.height = width, height
return c
}

// Flags allows to specify Child flags.
func (c *ChildWidget) Flags(flags WindowFlags) *ChildWidget {
c.flags = flags
return c
}

// Layout sets widgets that will be rendered inside of the Child.
func (c *ChildWidget) Layout(widgets ...Widget) *ChildWidget {
c.layout = Layout(widgets)
return c
Expand All @@ -106,15 +110,19 @@ func (c *ChildWidget) ID(id ID) *ChildWidget {
return c
}

func Child() *ChildWidget {
return &ChildWidget{
id: GenAutoID("Child"),
width: 0,
height: 0,
border: true,
flags: 0,
layout: nil,
// Build makes a Child.
func (c *ChildWidget) Build() {
if imgui.BeginChildStrV(c.id.String(), imgui.Vec2{X: c.width, Y: c.height}, func() imgui.ChildFlags {
if c.border {
return imgui.ChildFlagsBorders
}

return 0
}(), imgui.WindowFlags(c.flags)) {
c.layout.Build()
}

imgui.EndChild()
}

var _ Widget = &ComboCustomWidget{}
Expand Down
2 changes: 2 additions & 0 deletions examples/CSS-styling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func loop() {

func main() {
wnd := giu.NewMasterWindow("CSS Style [example]", 640, 480, 0)

if err := giu.ParseCSSStyleSheet(cssStyle); err != nil {
panic(err)
}

wnd.Run(loop)
}
6 changes: 3 additions & 3 deletions examples/canvas/canvas.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"image"
"image/color"

Expand All @@ -11,7 +10,6 @@ import (
var texture *g.Texture

func loop() {
fmt.Println(texture.ID())
g.SingleWindow().Layout(
g.Label("Canvas demo"),
g.Custom(func() {
Expand All @@ -32,7 +30,8 @@ func loop() {
p2 := pos.Add(image.Pt(120, 210))
p3 := pos.Add(image.Pt(210, 210))
p4 := pos.Add(image.Pt(210, 150))
// canvas.AddTriangle(p1, p2, p3, col, 2)

canvas.AddTriangle(p1, p2, p3, col, 2)
canvas.AddQuad(p1, p2, p3, p4, col, 1)

p1 = p1.Add(image.Pt(120, 60))
Expand All @@ -41,6 +40,7 @@ func loop() {
p1 = pos.Add(image.Pt(10, 400))
p2 = pos.Add(image.Pt(50, 440))
p3 = pos.Add(image.Pt(200, 500))

canvas.PathLineTo(p1)
canvas.PathLineTo(p2)
canvas.PathBezierCubicCurveTo(p2.Add(image.Pt(40, 0)), p3.Add(image.Pt(-50, 0)), p3, 0)
Expand Down
Loading

0 comments on commit cd898ea

Please sign in to comment.