forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docklayout.go
94 lines (85 loc) · 2.17 KB
/
docklayout.go
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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gui
// DockLayout is the layout for docking panels to the internal edges of their parent.
type DockLayout struct {
}
// DockLayoutParams specifies the edge to dock to.
type DockLayoutParams struct {
Edge int
}
// The different types of docking.
const (
DockTop = iota + 1
DockRight
DockBottom
DockLeft
DockCenter
)
// NewDockLayout returns a pointer to a new DockLayout.
func NewDockLayout() *DockLayout {
return new(DockLayout)
}
// Recalc (which satisfies the ILayout interface) recalculates the positions and sizes of the children panels.
func (dl *DockLayout) Recalc(ipan IPanel) {
pan := ipan.GetPanel()
width := pan.Width()
topY := float32(0)
bottomY := pan.Height()
leftX := float32(0)
rightX := width
// Top and bottom first
for _, iobj := range pan.Children() {
child := iobj.(IPanel).GetPanel()
if child.layoutParams == nil {
continue
}
params := child.layoutParams.(*DockLayoutParams)
if params.Edge == DockTop {
child.SetPosition(0, topY)
topY += child.Height()
child.SetWidth(width)
continue
}
if params.Edge == DockBottom {
child.SetPosition(0, bottomY-child.Height())
bottomY -= child.Height()
child.SetWidth(width)
continue
}
}
// Left and right
for _, iobj := range pan.Children() {
child := iobj.(IPanel).GetPanel()
if child.layoutParams == nil {
continue
}
params := child.layoutParams.(*DockLayoutParams)
if params.Edge == DockLeft {
child.SetPosition(leftX, topY)
leftX += child.Width()
child.SetHeight(bottomY - topY)
continue
}
if params.Edge == DockRight {
child.SetPosition(rightX-child.Width(), topY)
rightX -= child.Width()
child.SetHeight(bottomY - topY)
continue
}
}
// Center (only the first found)
for _, iobj := range pan.Children() {
child := iobj.(IPanel).GetPanel()
if child.layoutParams == nil {
continue
}
params := child.layoutParams.(*DockLayoutParams)
if params.Edge == DockCenter {
child.SetPosition(leftX, topY)
child.SetSize(rightX-leftX, bottomY-topY)
break
}
}
}