forked from g3n/engine
/
folder.go
179 lines (146 loc) · 4.02 KB
/
folder.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
// 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/g3n/engine/math32"
)
// Folder represents a folder GUI element.
type Folder struct {
Panel // Embedded panel
label Label // Folder label
icon Label // Folder icon
contentPanel IPanel // Content panel
styles *FolderStyles
cursorOver bool
alignRight bool
}
// FolderStyle contains the styling of a Folder.
type FolderStyle struct {
PanelStyle
FgColor math32.Color4
Icons [2]string
}
// FolderStyles contains a FolderStyle for each valid GUI state.
type FolderStyles struct {
Normal FolderStyle
Over FolderStyle
Focus FolderStyle
Disabled FolderStyle
}
// NewFolder creates and returns a pointer to a new folder widget
// with the specified text and initial width.
func NewFolder(text string, width float32, contentPanel IPanel) *Folder {
f := new(Folder)
f.Initialize(text, width, contentPanel)
return f
}
// Initialize initializes the Folder with the specified text and initial width
// It is normally used when the folder is embedded in another object.
func (f *Folder) Initialize(text string, width float32, contentPanel IPanel) {
f.Panel.Initialize(width, 0)
f.styles = &StyleDefault().Folder
// Initialize label
f.label.initialize(text, StyleDefault().Font)
f.Panel.Add(&f.label)
// Create icon
f.icon.initialize("", StyleDefault().FontIcon)
f.icon.SetFontSize(StyleDefault().Label.PointSize * 1.3)
f.Panel.Add(&f.icon)
// Setup content panel
f.contentPanel = contentPanel
contentPanel.GetPanel().bounded = false
contentPanel.GetPanel().SetVisible(false)
f.Panel.Add(f.contentPanel)
// Set event callbacks
f.Panel.Subscribe(OnMouseDown, f.onMouse)
f.Panel.Subscribe(OnCursorEnter, f.onCursor)
f.Panel.Subscribe(OnCursorLeave, f.onCursor)
f.alignRight = true
f.update()
f.recalc()
}
// SetStyles set the folder styles overriding the default style.
func (f *Folder) SetStyles(fs *FolderStyles) {
f.styles = fs
f.update()
}
// SetAlignRight sets the side of the alignment of the content panel
// in relation to the folder.
func (f *Folder) SetAlignRight(state bool) {
f.alignRight = state
f.recalc()
}
// TotalHeight returns this folder total height
// considering the contents panel, if visible.
func (f *Folder) TotalHeight() float32 {
height := f.Height()
if f.contentPanel.GetPanel().Visible() {
height += f.contentPanel.GetPanel().Height()
}
return height
}
// onMouse receives mouse button events over the folder panel.
func (f *Folder) onMouse(evname string, ev interface{}) {
switch evname {
case OnMouseDown:
cont := f.contentPanel.GetPanel()
if !cont.Visible() {
cont.SetVisible(true)
} else {
cont.SetVisible(false)
}
f.update()
f.recalc()
default:
return
}
}
// onCursor receives cursor events over the folder panel
func (f *Folder) onCursor(evname string, ev interface{}) {
switch evname {
case OnCursorEnter:
f.cursorOver = true
f.update()
case OnCursorLeave:
f.cursorOver = false
f.update()
default:
return
}
}
// update updates the folder visual state
func (f *Folder) update() {
if f.cursorOver {
f.applyStyle(&f.styles.Over)
return
}
f.applyStyle(&f.styles.Normal)
}
// applyStyle applies the specified style
func (f *Folder) applyStyle(s *FolderStyle) {
f.Panel.ApplyStyle(&s.PanelStyle)
icode := 0
if f.contentPanel.GetPanel().Visible() {
icode = 1
}
f.icon.SetText(string(s.Icons[icode]))
f.icon.SetColor4(&s.FgColor)
f.label.SetBgColor4(&s.BgColor)
f.label.SetColor4(&s.FgColor)
}
func (f *Folder) recalc() {
// icon position
f.icon.SetPosition(0, 0)
// Label position and width
f.label.SetPosition(f.icon.Width()+4, 0)
f.Panel.SetContentHeight(f.label.Height())
// Sets position of the base folder scroller panel
cont := f.contentPanel.GetPanel()
if f.alignRight {
cont.SetPosition(0, f.Panel.Height())
} else {
dx := cont.Width() - f.Panel.Width()
cont.SetPosition(-dx, f.Panel.Height())
}
}