forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.go
252 lines (218 loc) · 6.2 KB
/
splitter.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
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
// 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
import (
"github.com/adamlenda/engine/math32"
"github.com/adamlenda/engine/window"
)
// Splitter is a GUI element that splits two panels and can be adjusted
type Splitter struct {
Panel // Embedded panel
P0 Panel // Left/Top panel
P1 Panel // Right/Bottom panel
styles *SplitterStyles // pointer to current styles
spacer Panel // spacer panel
horiz bool // horizontal or vertical splitter
pos float32 // relative position of the center of the spacer panel (0 to 1)
posLast float32 // last position in pixels of the mouse cursor when dragging
pressed bool // mouse button is pressed and dragging
mouseOver bool // mouse is over the spacer panel
}
// SplitterStyle contains the styling of a Splitter
type SplitterStyle struct {
SpacerBorderColor math32.Color4
SpacerColor math32.Color4
SpacerSize float32
}
// SplitterStyles contains a SplitterStyle for each valid GUI state
type SplitterStyles struct {
Normal SplitterStyle
Over SplitterStyle
Drag SplitterStyle
}
// NewHSplitter creates and returns a pointer to a new horizontal splitter
// widget with the specified initial dimensions
func NewHSplitter(width, height float32) *Splitter {
return newSplitter(true, width, height)
}
// NewVSplitter creates and returns a pointer to a new vertical splitter
// widget with the specified initial dimensions
func NewVSplitter(width, height float32) *Splitter {
return newSplitter(false, width, height)
}
// newSpliter creates and returns a pointer of a new splitter with
// the specified orientation and initial dimensions.
func newSplitter(horiz bool, width, height float32) *Splitter {
s := new(Splitter)
s.horiz = horiz
s.styles = &StyleDefault().Splitter
s.Panel.Initialize(s, width, height)
// Initialize left/top panel
s.P0.Initialize(&s.P0, 0, 0)
s.Panel.Add(&s.P0)
// Initialize right/bottom panel
s.P1.Initialize(&s.P1, 0, 0)
s.Panel.Add(&s.P1)
// Initialize spacer panel
s.spacer.Initialize(&s.spacer, 0, 0)
s.Panel.Add(&s.spacer)
if horiz {
s.spacer.SetBorders(0, 1, 0, 1)
s.pos = 0.5
} else {
s.spacer.SetBorders(1, 0, 1, 0)
s.pos = 0.5
}
s.Subscribe(OnResize, s.onResize)
s.spacer.Subscribe(OnMouseDown, s.onMouse)
s.spacer.Subscribe(OnMouseUp, s.onMouse)
s.spacer.Subscribe(OnCursor, s.onCursor)
s.spacer.Subscribe(OnCursorEnter, s.onCursor)
s.spacer.Subscribe(OnCursorLeave, s.onCursor)
s.update()
s.recalc()
return s
}
// SetSplit sets the position of the splitter bar.
// It accepts a value from 0.0 to 1.0
func (s *Splitter) SetSplit(pos float32) {
s.setSplit(pos)
s.recalc()
}
// Split returns the current position of the splitter bar.
// It returns a value from 0.0 to 1.0
func (s *Splitter) Split() float32 {
return s.pos
}
// onResize receives subscribed resize events for the whole splitter panel
func (s *Splitter) onResize(evname string, ev interface{}) {
s.recalc()
}
// onMouse receives subscribed mouse events over the spacer panel
func (s *Splitter) onMouse(evname string, ev interface{}) {
mev := ev.(*window.MouseEvent)
if mev.Button != window.MouseButtonLeft {
return
}
switch evname {
case OnMouseDown:
s.pressed = true
if s.horiz {
s.posLast = mev.Xpos
} else {
s.posLast = mev.Ypos
}
Manager().SetCursorFocus(&s.spacer)
case OnMouseUp:
s.pressed = false
window.Get().SetCursor(window.ArrowCursor)
Manager().SetCursorFocus(nil)
}
}
// onCursor receives subscribed cursor events over the spacer panel
func (s *Splitter) onCursor(evname string, ev interface{}) {
if evname == OnCursorEnter {
if s.horiz {
window.Get().SetCursor(window.HResizeCursor)
} else {
window.Get().SetCursor(window.VResizeCursor)
}
s.mouseOver = true
s.update()
} else if evname == OnCursorLeave {
window.Get().SetCursor(window.ArrowCursor)
s.mouseOver = false
s.update()
} else if evname == OnCursor {
if !s.pressed {
return
}
cev := ev.(*window.CursorEvent)
var delta float32
pos := s.pos
if s.horiz {
delta = cev.Xpos - s.posLast
s.posLast = cev.Xpos
pos += delta / s.ContentWidth()
} else {
delta = cev.Ypos - s.posLast
s.posLast = cev.Ypos
pos += delta / s.ContentHeight()
}
s.setSplit(pos)
s.recalc()
}
}
// setSplit sets the validated and clamped split position from the received value.
func (s *Splitter) setSplit(pos float32) {
if pos < 0 {
s.pos = 0
} else if pos > 1 {
s.pos = 1
} else {
s.pos = pos
}
}
// update updates the splitter visual state
func (s *Splitter) update() {
if s.pressed {
s.applyStyle(&s.styles.Drag)
return
}
if s.mouseOver {
s.applyStyle(&s.styles.Over)
return
}
s.applyStyle(&s.styles.Normal)
}
// applyStyle applies the specified splitter style
func (s *Splitter) applyStyle(ss *SplitterStyle) {
s.spacer.SetBordersColor4(&ss.SpacerBorderColor)
s.spacer.SetColor4(&ss.SpacerColor)
if s.horiz {
s.spacer.SetWidth(ss.SpacerSize)
} else {
s.spacer.SetHeight(ss.SpacerSize)
}
}
// recalc relcalculates the position and sizes of the internal panels
func (s *Splitter) recalc() {
width := s.ContentWidth()
height := s.ContentHeight()
if s.horiz {
// Calculate x position for spacer panel
spx := width*s.pos - s.spacer.Width()/2
if spx < 0 {
spx = 0
} else if spx > width-s.spacer.Width() {
spx = width - s.spacer.Width()
}
// Left panel
s.P0.SetPosition(0, 0)
s.P0.SetSize(spx, height)
// Spacer panel
s.spacer.SetPosition(spx, 0)
s.spacer.SetHeight(height)
// Right panel
s.P1.SetPosition(spx+s.spacer.Width(), 0)
s.P1.SetSize(width-spx-s.spacer.Width(), height)
} else {
// Calculate y position for spacer panel
spy := height*s.pos - s.spacer.Height()/2
if spy < 0 {
spy = 0
} else if spy > height-s.spacer.Height() {
spy = height - s.spacer.Height()
}
// Top panel
s.P0.SetPosition(0, 0)
s.P0.SetSize(width, spy)
// Spacer panel
s.spacer.SetPosition(0, spy)
s.spacer.SetWidth(width)
// Bottom panel
s.P1.SetPosition(0, spy+s.spacer.Height())
s.P1.SetSize(width, height-spy-s.spacer.Height())
}
}