Skip to content

Commit

Permalink
Merge pull request #329 from gucio321/alignment-refactoring
Browse files Browse the repository at this point in the history
Alignment refactoring
  • Loading branch information
AllenDang authored Sep 17, 2021
2 parents 78cec52 + ecd581c commit 7de5239
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
28 changes: 10 additions & 18 deletions Alignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,7 @@ type AlignmentSetter struct {
// )
// will print the message two times per frame.
//
// BUG:
// if the source layout (set by (*alignSetter).To(...) contains another Layout
// only the last widget from the embeded layout will be processed.
// Example:
// Align(AlignToRight).To(
// Label("I'm the label"),
// Layout(
// Label("I'm th e other label and I'll not be aligned to right"),
// Label("I'm the next label"),
// ),
// label("I'm the last label"),
// )
// BUG: DatePickerWidget doesn't work properly
func Align(at AlignmentType) *AlignmentSetter {
return &AlignmentSetter{
alignType: at,
Expand Down Expand Up @@ -74,7 +63,7 @@ func (a *AlignmentSetter) Build() {

// render widgets with 0 alpha and store thems widths
imgui.PushStyleVarFloat(imgui.StyleVarID(StyleVarAlpha), 0)
for _, item := range a.layout {
a.layout.Range(func(item Widget) {
var width float32
if item != nil {
item.Build()
Expand All @@ -83,19 +72,20 @@ func (a *AlignmentSetter) Build() {
}

widgetsWidths = append(widgetsWidths, width)
}
})
imgui.PopStyleVar()

// reset cursor pos
SetCursorPos(startPos)

// ALIGN WIDGETS
for i, item := range a.layout {
idx := 0o0
a.layout.Range(func(item Widget) {
if item == nil {
continue
return
}

w := widgetsWidths[i]
w := widgetsWidths[idx]
currentPos := GetCursorPos()
availableW, _ := GetAvailableRegion()
switch a.alignType {
Expand All @@ -110,5 +100,7 @@ func (a *AlignmentSetter) Build() {
}

item.Build()
}

idx++
})
}
24 changes: 23 additions & 1 deletion Layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ type Widget interface {
Build()
}

var _ Widget = Layout{}
var (
_ Widget = Layout{}
_ Splitable = Layout{}
)

type Layout []Widget

Expand All @@ -20,3 +23,22 @@ func (l Layout) Build() {
}
}
}

// Splitable is implemented by widgets, which can be split (ranged)
// Layout implements Splitable.
type Splitable interface {
Range(func(w Widget))
}

// Range ranges ofer the Layout, calling rangeFunc
// on each loop iteration.
func (l Layout) Range(rangeFunc func(Widget)) {
for _, w := range l {
if splitable, canRange := w.(Splitable); canRange {
splitable.Range(rangeFunc)
continue
}

rangeFunc(w)
}
}
9 changes: 9 additions & 0 deletions examples/align/align.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ func loop() {
giu.Label("I'm a alined to right label"),
giu.InputText(&text),
),

giu.Align(giu.AlignRight).To(
giu.Label("I'm the label"),
giu.Layout{
giu.Label("I'm th e other label embeded in another layout"),
giu.Label("I'm the next label"),
},
giu.Label("I'm the last label"),
),
)
}

Expand Down

0 comments on commit 7de5239

Please sign in to comment.