/
gosui.go
411 lines (345 loc) · 10.5 KB
/
gosui.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
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
package gosui
import (
"container/list"
"image"
"image/color"
"log"
"sort"
)
const (
maxInt = int(^uint(0) >> 1)
)
// Color is actually image/color.RGBA
type Color color.RGBA
// Paint holds infomation about the stroke (border) and the colors
type Paint struct {
FillColor Color
StrokeWidth int
StrokeColor Color
}
// DrawBackend is the one that actually draws things on the window
type DrawBackend interface {
DrawRect(image.Rectangle, [4]int, Paint)
DrawText(image.Point, *TextShape, Paint) (int, int)
}
type RenderBackend interface {
DrawBackend
Init(int, int)
DrawElementsInArea(DrawPriorityList, image.Rectangle)
}
// MakeRect receives coordinates of the min and max points (x1, y1, x2, y2) and returns a Rectangle of type image.Rectangle
func MakeRect(x1, y1, x2, y2 int) image.Rectangle {
return image.Rectangle{image.Point{x1, y1}, image.Point{x2, y2}}
}
// MakeRectWH receives X, Y, width, height and returns an image.Rectangle
func MakeRectWH(x, y, w, h int) image.Rectangle {
return MakeRect(x, y, x+w, y+h)
}
// NoStroke returns a Paint with no stroke (and has the specified fillColor)
func NoStroke(fillColor Color) (p Paint) {
p.FillColor = fillColor
p.StrokeWidth = 0
return p
}
// The render interface for renderable things
type shape interface {
render(IElement, DrawBackend)
}
// RectShape holds information specific to displayed rectangles (rounded).
// Currently it holds corner radius of 4 corners
type RectShape struct {
cornerRadiis [4]int
}
type EventHandler interface{}
// Element holds information about an element in the window
type Element struct {
parent *AbstractElement
treeLev int // The number that represents this element's depth in the tree, the root element has tier=0
Area image.Rectangle
zIndex float32
Paint
cData map[string]interface{}
Handler EventHandler
}
// IElement is the common interface for AbstractElement and ConcreteElement
type IElement interface {
BaseElement() *Element // Returns the Element subcomponent
IsConcrete() bool
AllConcreteDescns() *list.List // Get all concrete descendants
}
// ConcreteElement is the type of Element that can have a shape (apperance)
// and be rendered.
// It cannot have children.
type ConcreteElement struct {
Element
shape shape
}
// AbstractElement is for grouping ConcreteElement's.
// It doesn't have a shape and cannot be displayed.
type AbstractElement struct {
Element
children []IElement
}
type FontStyle struct {
Bold, Italic bool
}
var (
BoldItalic = FontStyle{true, true}
Bold = FontStyle{true, false}
Italic = FontStyle{false, true}
Regular = FontStyle{false, false}
)
type Font struct {
Family string
Size int
Style FontStyle
}
type TextShape struct {
Content string
Font Font
Editable bool
origin image.Point
}
func (e *ConcreteElement) TextShape() *TextShape {
return e.shape.(*TextShape)
}
func (e *ConcreteElement) UpdateSize(w, h int) {
e.Area.Max = image.Point{e.Area.Min.X + w, e.Area.Min.Y + h}
}
// SetData is used by something to add data to the element
// for its own purpose, like for an algorithm
func (e *Element) SetData(key string, data interface{}) {
if e.cData == nil {
e.cData = make(map[string]interface{})
}
e.cData[key] = data
}
func (e *Element) GetData(key string) interface{} {
return e.cData[key]
}
// RectShape method is a helper that casts element's shape to a RectShape and return it
func (e *ConcreteElement) RectShape() *RectShape {
return e.shape.(*RectShape)
}
// ZIndex gets the z-index of the Element
func (e *Element) ZIndex() float32 {
return e.zIndex
}
// SetZIndex on an AbstractElement means increasing all its descendants's zindex by that value
func (e *AbstractElement) SetZIndex(z float32) {
li := e.AllConcreteDescns()
for o := li.Front(); o != nil; o = o.Next() {
o.Value.(*ConcreteElement).zIndex += z
}
}
// SetZIndex on a ConcreteElement
func (e *ConcreteElement) SetZIndex(z float32) {
e.zIndex = z
}
// IsConcrete on AbstractElement returns false
func (e *AbstractElement) IsConcrete() bool {
return false
}
// IsConcrete on ConcreteElement returns true
func (e *ConcreteElement) IsConcrete() bool {
return true
}
func (r *RectShape) render(ei IElement, backend DrawBackend) {
e := ei.(*ConcreteElement)
backend.DrawRect(e.Area, r.cornerRadiis, e.Paint)
}
func (s *TextShape) render(ei IElement, backend DrawBackend) {
e := ei.(*ConcreteElement)
w, h := backend.DrawText(s.origin, s, e.Paint)
e.Area.Min = image.Point{s.origin.X, s.origin.Y - h}
e.UpdateSize(w, h)
}
// X method returns element's top-left x coordinate
func (e *Element) X() int { return e.Area.Min.X }
// Y method returns element's top-left y coordinate
func (e *Element) Y() int { return e.Area.Min.Y }
// W method returns element's width
func (e *Element) W() int { return e.Area.Dx() }
// H method returns element's height
func (e *Element) H() int { return e.Area.Dy() }
// IsBehind checks whether the element is behind the other element
func (e *ConcreteElement) IsBehind(e2 *ConcreteElement) bool {
if e.zIndex == e2.zIndex {
return e.treeLev < e2.treeLev
}
return e.zIndex < e2.zIndex
}
// BaseElement returns the Element subcomponent of the element
func (e *AbstractElement) BaseElement() *Element {
return &e.Element
}
// BaseElement returns the Element subcomponent of the element
func (e *ConcreteElement) BaseElement() *Element {
return &e.Element
}
func (e *AbstractElement) fetchConcreteDescns(li *list.List) *list.List {
for _, oi := range e.children {
if o, isConcrete := oi.(*ConcreteElement); isConcrete {
li.PushBack(o)
continue
}
o := oi.(*AbstractElement)
o.fetchConcreteDescns(li)
}
return li
}
// AllConcreteDescns fetches and returns a list.List of all the element's descendants
func (e *AbstractElement) AllConcreteDescns() (li *list.List) {
li = list.New()
li = e.fetchConcreteDescns(li)
return li
}
// AllConcreteDescns on a ConcreteElement just returns itself in a list
func (e *ConcreteElement) AllConcreteDescns() (li *list.List) {
li = list.New()
li.PushBack(e)
return li
}
// Data for algorithm purposes
type algData struct {
addedToRedraw bool
}
// OverlappedAlgorithm encapsulates the algorithm to get overlapping elements, used for lazy redrawing
type OverlappedAlgorithm struct{}
// InitOverlappedAlgorithm initializes the algorithm.
// All changes by the algorithm are reset.
func InitOverlappedAlgorithm(root *AbstractElement) (alg OverlappedAlgorithm) {
l := root.AllConcreteDescns()
for o := l.Front(); o != nil; o = o.Next() {
e := o.Value.(*ConcreteElement)
e.BaseElement().SetData("addedToRedraw", false)
}
return alg
}
func (alg OverlappedAlgorithm) addToRedrawList(e *ConcreteElement, li *list.List) {
e.BaseElement().SetData("addedToRedraw", true)
li.PushBack(e)
}
func (alg OverlappedAlgorithm) hasAdded(e *Element) bool {
v := e.GetData("addedToRedraw")
return v != nil && v.(bool)
}
func (alg OverlappedAlgorithm) fetchOverlappingConcreteElems(target IElement, e *AbstractElement, root *AbstractElement, li *list.List) *list.List {
for _, oi := range e.children {
o := oi.BaseElement()
t := target.BaseElement()
if t.Area.Overlaps(o.Area) && (o != t) && (!alg.hasAdded(o)) {
if oi.IsConcrete() {
alg.addToRedrawList(oi.(*ConcreteElement), li)
} else {
li = alg.fetchOverlappingConcreteElems(target, oi.(*AbstractElement), root, li)
}
}
}
return li
}
type InputHandler struct {
e *ConcreteElement
}
func (h InputHandler) OnMouseEvent(evt *MouseEvent) bool {
log.Printf("Mouse clicked on %v at %v\n", h.e.Area, evt.Pos)
return true
}
func NewTextInputElement(parent *AbstractElement, x, y int, font Font) *ConcreteElement {
te := NewTextElement(parent, x, y, font, true)
te.Handler = InputHandler{te}
return te
}
func NewTextElement(parent *AbstractElement, x, y int, font Font, editable bool) *ConcreteElement {
e := new(ConcreteElement)
parent.AddChild(e)
ts := new(TextShape)
ts.Editable = editable
ts.Font = font
ts.origin = image.Point{x, y}
e.shape = ts
return e
}
// NewRectElement creates a new rsectangle concrete element
func NewRectElement(parent *AbstractElement, area image.Rectangle) *ConcreteElement {
e := new(ConcreteElement)
parent.AddChild(e)
e.shape = new(RectShape)
e.Area = area
return e
}
// SetAllCornerRadiusTo sets all 4 corners of the RectShape to have same radius rad
func (r *RectShape) SetAllCornerRadiusTo(rad int) {
for i := 0; i < 4; i++ {
r.cornerRadiis[i] = rad
}
}
type RectCornersRad struct {
TopLeft, TopRight, BotLeft, BotRight int
}
func (r *RectShape) SetCornerRadiis(conf RectCornersRad) {
r.cornerRadiis[0], r.cornerRadiis[1] = conf.TopLeft, conf.TopRight
r.cornerRadiis[2], r.cornerRadiis[3] = conf.BotLeft, conf.BotRight
}
// NewRootElement creates and returns the root element
func NewRootElement() (r *AbstractElement) {
r = new(AbstractElement)
r.treeLev = 0
r.zIndex = 0
r.Area = MakeRect(0, 0, maxInt, maxInt)
return r
}
// NewAbstractElement creates and returns a new AbstractElement.
// It adds the element as a child.
func NewAbstractElement(parent *AbstractElement, area image.Rectangle) (r *AbstractElement) {
r = new(AbstractElement)
r.Area = area
parent.AddChild(r)
return r
}
// AddChild makes an element the child of an AbstractElement
func (e *AbstractElement) AddChild(child IElement) {
e.children = append(e.children, child)
c := child.BaseElement()
c.parent = e
c.treeLev = e.treeLev + 1
c.zIndex = 0
}
// Draw the element and all its descendants
func (e *AbstractElement) Draw(backend DrawBackend) {
l := makeDrawPriorityList(e.AllConcreteDescns())
sort.Sort(l)
for _, o := range l {
o.Draw(backend)
}
}
// Draw the element
func (e *ConcreteElement) Draw(backend DrawBackend) {
e.shape.render(e, backend)
}
type DrawPriorityList [](*ConcreteElement)
func makeDrawPriorityList(li *list.List) DrawPriorityList {
l := make([](*ConcreteElement), li.Len())
for o, i := li.Front(), 0; o != nil; o, i = o.Next(), i+1 {
l[i] = o.Value.(*ConcreteElement)
}
return DrawPriorityList(l)
}
func (l DrawPriorityList) Len() int { return len(l) }
func (l DrawPriorityList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l DrawPriorityList) Less(i, j int) bool {
return l[i].IsBehind(l[j])
}
// Redraw the element
func Redraw(e IElement, backend RenderBackend, root *AbstractElement) {
alg := InitOverlappedAlgorithm(root)
itemsToRedraw := list.New()
d := e.AllConcreteDescns()
for o := d.Front(); o != nil; o = o.Next() {
alg.addToRedrawList(o.Value.(*ConcreteElement), itemsToRedraw)
}
alg.fetchOverlappingConcreteElems(e, root, root, itemsToRedraw)
l := makeDrawPriorityList(itemsToRedraw)
sort.Sort(l)
backend.DrawElementsInArea(l, e.BaseElement().Area)
}