-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiGrid.go
More file actions
500 lines (429 loc) · 13.2 KB
/
Copy pathmultiGrid.go
File metadata and controls
500 lines (429 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package dfx
import (
"fmt"
"github.com/AllenDang/cimgui-go/imgui"
)
// MultiGrid is a flexible component container that separates component management
// from layout strategy. Components are managed as a named collection, and different
// layout strategies can be applied to arrange them.
type MultiGrid struct {
Container
components map[string]Component
layout Layout
}
// Layout defines how components are arranged and how user interaction is handled
type Layout interface {
// Arrange renders the components according to the layout strategy
Arrange(components map[string]Component, state *State)
// HandleInput processes user input for layout-specific interactions (resizing, etc)
HandleInput(state *State)
}
// NewMultiGrid creates a new MultiGrid with no components and no layout
func NewMultiGrid() *MultiGrid {
return &MultiGrid{
Container: Container{Visible: true},
components: make(map[string]Component),
}
}
// AddComponent adds a named component to the collection
func (mg *MultiGrid) AddComponent(id string, component Component) {
mg.components[id] = component
}
// RemoveComponent removes a component from the collection
func (mg *MultiGrid) RemoveComponent(id string) {
delete(mg.components, id)
}
// GetComponent retrieves a component by ID
func (mg *MultiGrid) GetComponent(id string) (Component, bool) {
comp, exists := mg.components[id]
return comp, exists
}
// SetLayout applies a layout strategy to the component collection
func (mg *MultiGrid) SetLayout(layout Layout) {
mg.layout = layout
}
// ComponentIDs returns all component IDs in the collection
func (mg *MultiGrid) ComponentIDs() []string {
ids := make([]string, 0, len(mg.components))
for id := range mg.components {
ids = append(ids, id)
}
return ids
}
// Draw renders the MultiGrid using the current layout strategy
func (mg *MultiGrid) Draw(state *State) {
if !mg.Visible {
return
}
layoutState := &State{
Size: state.Size,
Position: state.Position,
IO: state.IO,
App: state.App,
Parent: mg,
}
// handle input first (for resize operations, etc)
if mg.layout != nil {
mg.layout.HandleInput(layoutState)
}
// arrange components using the current layout
if mg.layout != nil {
mg.layout.Arrange(mg.components, layoutState)
}
drawContainerExtensions(&mg.Container, state)
}
// FlexLayout provides a resizable grid layout similar to the original MultiSurface
type FlexLayout struct {
arrangement [][]string // component IDs arranged in rows/columns
rowHeights []int // heights for each row (0 = auto-size)
colWidths [][]int // widths for each column in each row (0 = auto-size)
// resizing state
dragging bool
dragType DragType
dragRowIndex int
dragColIndex int
dragRowPrev int
dragColPrev int
deltaRow int
deltaCol int
}
type DragType int
const (
DragNone DragType = iota
DragRow
DragColumn
)
const (
multiGridMargin = 2
multiGridSpacing = 4
multiGridSplitWidth = 10
multiGridSplitHeight = 11
)
// NewFlexLayout creates a flexible layout with the given arrangement
func NewFlexLayout(arrangement [][]string) *FlexLayout {
fl := &FlexLayout{
arrangement: arrangement,
rowHeights: make([]int, len(arrangement)),
colWidths: make([][]int, len(arrangement)),
}
// initialize column width slices
for i, row := range arrangement {
fl.colWidths[i] = make([]int, len(row))
}
return fl
}
// RowHeights returns a copy of the current row heights.
func (fl *FlexLayout) RowHeights() []int {
result := make([]int, len(fl.rowHeights))
copy(result, fl.rowHeights)
return result
}
// ColWidths returns a copy of the current column widths for all rows.
func (fl *FlexLayout) ColWidths() [][]int {
result := make([][]int, len(fl.colWidths))
for i, row := range fl.colWidths {
result[i] = make([]int, len(row))
copy(result[i], row)
}
return result
}
// SetRowHeights sets the row heights. the slice length must match the number of rows.
func (fl *FlexLayout) SetRowHeights(heights []int) {
if len(heights) != len(fl.rowHeights) {
return
}
copy(fl.rowHeights, heights)
}
// SetColWidths sets the column widths for all rows. the structure must match the arrangement.
func (fl *FlexLayout) SetColWidths(widths [][]int) {
if len(widths) != len(fl.colWidths) {
return
}
for i, row := range widths {
if len(row) != len(fl.colWidths[i]) {
return
}
}
for i, row := range widths {
copy(fl.colWidths[i], row)
}
}
// HandleInput processes mouse input for resize operations
func (fl *FlexLayout) HandleInput(state *State) {
// handle resize completion
if fl.dragging {
if fl.dragType == DragRow && fl.dragRowIndex >= 0 && fl.dragRowPrev >= 0 {
fl.rowHeights[fl.dragRowIndex] -= fl.deltaRow
fl.rowHeights[fl.dragRowPrev] += fl.deltaRow
} else if fl.dragType == DragColumn && fl.dragRowIndex >= 0 && fl.dragColIndex >= 0 && fl.dragColPrev >= 0 {
fl.colWidths[fl.dragRowIndex][fl.dragColIndex] -= fl.deltaCol
fl.colWidths[fl.dragRowIndex][fl.dragColPrev] += fl.deltaCol
}
fl.dragging = false
fl.dragType = DragNone
fl.deltaRow = 0
fl.deltaCol = 0
}
}
// Arrange renders components in a flexible grid with resizable splitters
func (fl *FlexLayout) Arrange(components map[string]Component, state *State) {
if len(fl.arrangement) == 0 {
return
}
fl.sizeRows(state.Size)
cursor := imgui.CursorPos()
for i, row := range fl.arrangement {
imgui.SetCursorPos(cursor)
rowHeight := fl.rowHeights[i]
rowSize := imgui.Vec2{X: state.Size.X - multiGridSpacing, Y: float32(rowHeight)}
// draw row splitter (except for first row)
if i > 0 {
rowSize.Y -= multiGridSplitHeight
imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{X: 0, Y: 0})
imgui.InvisibleButton(fmt.Sprintf("row_%d_split", i), imgui.Vec2{X: state.Size.X, Y: multiGridSplitWidth})
imgui.PopStyleVar()
if imgui.IsItemHovered() {
imgui.SetMouseCursor(imgui.MouseCursorResizeNS)
}
if imgui.IsItemActive() {
fl.dragging = true
fl.dragType = DragRow
fl.deltaRow = int(imgui.CurrentIO().MouseDelta().Y)
fl.dragRowIndex = i
fl.dragRowPrev = i - 1
}
// draw hover/active highlight
if imgui.IsItemHovered() || imgui.IsItemActive() {
dl := imgui.WindowDrawList()
min := imgui.ItemRectMin()
max := imgui.ItemRectMax()
// draw horizontal line in center of splitter area
centerY := (min.Y + max.Y) / 2
var color imgui.Vec4
if imgui.IsItemActive() {
color = imgui.CurrentStyle().Colors()[imgui.ColButtonActive]
} else {
color = imgui.CurrentStyle().Colors()[imgui.ColButtonHovered]
}
dl.AddLine(
imgui.Vec2{X: min.X, Y: centerY},
imgui.Vec2{X: max.X, Y: centerY},
imgui.ColorConvertFloat4ToU32(color),
)
}
imgui.SetCursorPos(cursor.Add(imgui.Vec2{X: 0, Y: multiGridSplitWidth}))
}
// arrange columns in this row
fl.sizeColumns(state.Size, i)
colCursor := imgui.CursorPos()
for j, componentID := range row {
imgui.SetCursorPos(colCursor)
colWidth := fl.colWidths[i][j]
colSize := imgui.Vec2{X: float32(colWidth - multiGridSpacing), Y: rowSize.Y}
// draw column splitter (except for first column)
if j > 0 {
colSize.X -= multiGridSplitHeight
imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{X: 0, Y: 0})
imgui.InvisibleButton(fmt.Sprintf("row_%d_col_%d_split", i, j), imgui.Vec2{X: multiGridSplitWidth, Y: rowSize.Y})
imgui.PopStyleVar()
if imgui.IsItemHovered() {
imgui.SetMouseCursor(imgui.MouseCursorResizeEW)
}
if imgui.IsItemActive() {
fl.dragging = true
fl.dragType = DragColumn
fl.deltaCol = int(imgui.CurrentIO().MouseDelta().X)
fl.dragRowIndex = i
fl.dragColIndex = j
fl.dragColPrev = j - 1
}
// draw hover/active highlight
if imgui.IsItemHovered() || imgui.IsItemActive() {
dl := imgui.WindowDrawList()
min := imgui.ItemRectMin()
max := imgui.ItemRectMax()
// draw vertical line in center of splitter area
centerX := (min.X + max.X) / 2
var color imgui.Vec4
if imgui.IsItemActive() {
color = imgui.CurrentStyle().Colors()[imgui.ColButtonActive]
} else {
color = imgui.CurrentStyle().Colors()[imgui.ColButtonHovered]
}
dl.AddLine(
imgui.Vec2{X: centerX, Y: min.Y},
imgui.Vec2{X: centerX, Y: max.Y},
imgui.ColorConvertFloat4ToU32(color),
)
}
imgui.SetCursorPos(colCursor.Add(imgui.Vec2{X: multiGridSplitWidth, Y: 0}))
}
// draw the component
if component, exists := components[componentID]; exists {
fl.drawComponent(component, colSize, componentID, state)
}
colCursor.X += float32(colWidth)
}
cursor.Y += float32(rowHeight)
}
}
// drawComponent renders a component in a child window
func (fl *FlexLayout) drawComponent(component Component, size imgui.Vec2, id string, state *State) {
if imgui.BeginChildStrV(fmt.Sprintf("mg_%s", id), size, 0, imgui.WindowFlagsNoScrollbar) {
childState := &State{
Size: size,
Position: imgui.Vec2{},
IO: imgui.CurrentIO(),
App: state.App,
Parent: state.Parent,
}
component.Draw(childState)
}
imgui.EndChild()
}
// sizeRows calculates row heights
func (fl *FlexLayout) sizeRows(size imgui.Vec2) {
if len(fl.rowHeights) == 0 {
return
}
maxY := int(size.Y - multiGridMargin)
var needsHeight []int
allocated := 0
for i, height := range fl.rowHeights {
if height > 0 {
allocated += height
} else {
needsHeight = append(needsHeight, i)
}
}
if len(needsHeight) > 0 {
newHeight := maxY / len(fl.rowHeights)
for _, i := range needsHeight {
fl.rowHeights[i] = newHeight
allocated += newHeight
}
}
// distribute overage/underage
if allocated != maxY {
diff := maxY - allocated
sharePerRow := diff / len(fl.rowHeights)
for i := range fl.rowHeights {
fl.rowHeights[i] += sharePerRow
}
}
}
// sizeColumns calculates column widths for a specific row
func (fl *FlexLayout) sizeColumns(size imgui.Vec2, rowIndex int) {
if rowIndex >= len(fl.colWidths) || len(fl.colWidths[rowIndex]) == 0 {
return
}
maxX := int(size.X - multiGridMargin)
colWidths := fl.colWidths[rowIndex]
var needsWidth []int
allocated := 0
for j, width := range colWidths {
if width > 0 {
allocated += width
} else {
needsWidth = append(needsWidth, j)
}
}
if len(needsWidth) > 0 {
newWidth := maxX / len(colWidths)
for _, j := range needsWidth {
colWidths[j] = newWidth
allocated += newWidth
}
}
// distribute overage/underage
if allocated != maxX {
diff := maxX - allocated
sharePerCol := diff / len(colWidths)
for j := range colWidths {
colWidths[j] += sharePerCol
}
}
}
// GridLayout provides fixed-position grid layout with no interactive resizing
type GridLayout struct {
cells map[string]GridCell // component ID -> grid position
gridWidth int // number of columns
gridHeight int // number of rows
cellSize imgui.Vec2 // size of each grid cell (0 = auto-size)
}
// GridCell defines a component's position in the grid
type GridCell struct {
Row, Col int // grid position (0-based)
RowSpan, ColSpan int // span (1,1 = single cell)
}
// NewGridLayout creates a fixed grid layout
func NewGridLayout(gridWidth, gridHeight int) *GridLayout {
return &GridLayout{
cells: make(map[string]GridCell),
gridWidth: gridWidth,
gridHeight: gridHeight,
}
}
// SetCell positions a component in the grid
func (gl *GridLayout) SetCell(componentID string, row, col int, rowSpan, colSpan int) {
gl.cells[componentID] = GridCell{
Row: row,
Col: col,
RowSpan: rowSpan,
ColSpan: colSpan,
}
}
// HandleInput processes input (no interactive resizing for grid layout)
func (gl *GridLayout) HandleInput(state *State) {
// grid layout is fixed - no interactive resize
// could add drag-and-drop reordering here in the future
}
// Arrange renders components at fixed grid positions
func (gl *GridLayout) Arrange(components map[string]Component, state *State) {
if gl.gridWidth <= 0 || gl.gridHeight <= 0 {
return
}
// calculate cell dimensions
cellWidth := state.Size.X / float32(gl.gridWidth)
cellHeight := state.Size.Y / float32(gl.gridHeight)
// override with fixed cell size if specified
if gl.cellSize.X > 0 {
cellWidth = gl.cellSize.X
}
if gl.cellSize.Y > 0 {
cellHeight = gl.cellSize.Y
}
// render each component at its grid position
for componentID, cell := range gl.cells {
component, exists := components[componentID]
if !exists {
continue
}
// calculate component position and size
posX := float32(cell.Col) * cellWidth
posY := float32(cell.Row) * cellHeight
sizeX := float32(cell.ColSpan) * cellWidth
sizeY := float32(cell.RowSpan) * cellHeight
// ensure component doesn't go outside bounds
if posX+sizeX > state.Size.X {
sizeX = state.Size.X - posX
}
if posY+sizeY > state.Size.Y {
sizeY = state.Size.Y - posY
}
// draw component at calculated position
imgui.SetCursorPos(imgui.Vec2{X: posX, Y: posY})
componentSize := imgui.Vec2{X: sizeX, Y: sizeY}
if imgui.BeginChildStrV(fmt.Sprintf("grid_%s", componentID), componentSize, 0, imgui.WindowFlagsNoScrollbar) {
childState := &State{
Size: componentSize,
Position: imgui.Vec2{X: posX, Y: posY},
IO: imgui.CurrentIO(),
App: state.App,
Parent: state.Parent,
}
component.Draw(childState)
}
imgui.EndChild()
}
}