-
Notifications
You must be signed in to change notification settings - Fork 0
/
messagewindow.go
260 lines (231 loc) · 6.24 KB
/
messagewindow.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
/*
* messagewindow.go
*
* Copyright 2018-2024 Dariusz Sikora <ds@isangeles.dev>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
package mtk
import (
"image/color"
"golang.org/x/image/colornames"
"github.com/gopxl/pixel"
"github.com/gopxl/pixel/pixelgl"
)
// MessageWindow struct represents UI message window.
type MessageWindow struct {
drawArea pixel.Rect
size pixel.Vec
color color.Color
colorDisable color.Color
textbox *Textbox
acceptButton *Button
cancelButton *Button
opened bool
focused bool
accepted bool
dismissed bool
disabled bool
onAccept func(msg *MessageWindow)
onCancel func(msg *MessageWindow)
}
// NewMessageWindow creates new message window instance.
func NewMessageWindow(params Params) *MessageWindow {
mw := new(MessageWindow)
mw.opened = true
// Background.
mw.size = params.Size.MessageWindowSize()
mw.color = params.MainColor
mw.colorDisable = colornames.Darkgrey
// Buttons.
buttonParams := Params{
Size: SizeSmall,
FontSize: SizeSmall,
Shape: ShapeRectangle,
MainColor: params.SecColor,
}
mw.acceptButton = NewButton(buttonParams)
mw.acceptButton.SetOnClickFunc(mw.onAcceptButtonClicked)
// Textbox.
boxSize := pixel.V(mw.Size().X, mw.Size().Y-mw.acceptButton.Size().Y)
boxParams := Params{
SizeRaw: boxSize,
FontSize: params.FontSize,
MainColor: params.MainColor,
AccentColor: params.SecColor,
}
textbox := NewTextbox(boxParams)
mw.textbox = textbox
mw.textbox.SetText(params.Info)
return mw
}
// NewDialogWindow creates new dialog window with message.
func NewDialogWindow(params Params) *MessageWindow {
// Basic message window.
mw := NewMessageWindow(params)
// Buttons.
buttonParams := Params{
Size: SizeSmall,
Shape: ShapeRectangle,
MainColor: params.SecColor,
}
mw.cancelButton = NewButton(buttonParams)
mw.cancelButton.SetOnClickFunc(mw.onCancelButtonClicked)
return mw
}
// Draw draws window.
func (mw *MessageWindow) Draw(t pixel.Target, matrix pixel.Matrix) {
// Calculating draw area.
mw.drawArea = MatrixToDrawArea(matrix, mw.Size())
// Background.
color := mw.color
if mw.Disabled() {
color = mw.colorDisable
}
DrawRect(t, mw.DrawArea(), color)
// Buttons.
acceptButtonPos := MoveBR(mw.Size(), mw.acceptButton.Size())
mw.acceptButton.Draw(t, matrix.Moved(acceptButtonPos))
if mw.cancelButton != nil {
cancelButtonPos := MoveBL(mw.Size(), mw.cancelButton.Size())
mw.cancelButton.Draw(t, matrix.Moved(cancelButtonPos))
}
// Textbox.
boxMove := MoveTC(mw.Size(), mw.textbox.Size())
mw.textbox.Draw(t, matrix.Moved(boxMove))
}
// Update handles key press events.
func (mw *MessageWindow) Update(win *Window) {
if mw.Disabled() {
return
}
if mw.Focused() {
if win.JustPressed(pixelgl.KeyEscape) {
mw.cancel()
}
if win.JustPressed(pixelgl.KeyEnter) {
mw.accept()
}
}
if mw.DrawArea().Contains(win.MousePosition()) {
if win.JustPressed(pixelgl.MouseButtonLeft) {
mw.Focus(true)
}
}
mw.textbox.Update(win)
mw.acceptButton.Update(win)
if mw.cancelButton != nil {
mw.cancelButton.Update(win)
}
}
// Show toggles window visibility.
func (mw *MessageWindow) Show(show bool) {
mw.opened = show
}
// Active toggles message active state.
func (mw *MessageWindow) Active(active bool) {
mw.disabled = !active
}
// Focus sets or removes focus from window.
func (mw *MessageWindow) Focus(focus bool) {
mw.focused = focus
}
// Opened checks whether window should be open.
func (mw *MessageWindow) Opened() bool {
return mw.opened
}
// Focused checks whether window is focused.
func (mw *MessageWindow) Focused() bool {
return mw.focused
}
// Dismissed checks whether window was dismised.
func (mw *MessageWindow) Dismissed() bool {
return mw.dismissed
}
// Accepted checks whether message was accepted.
func (mw *MessageWindow) Accepted() bool {
return mw.accepted
}
// Disabled checks whether message is unactive.
func (mw *MessageWindow) Disabled() bool {
return mw.disabled
}
// Size resturns message window size.
func (mw *MessageWindow) Size() pixel.Vec {
return mw.size
}
// DrawArea returns size of current draw area.
func (mw *MessageWindow) DrawArea() pixel.Rect {
return mw.drawArea
}
// SetAcceptLabel sets label for accept button.
func (mw *MessageWindow) SetAcceptLabel(l string) {
mw.acceptButton.SetLabel(l)
}
// SetCancelLabel sets label for cancel button.
func (mw *MessageWindow) SetCancelLabel(l string) {
if mw.cancelButton == nil {
return
}
mw.cancelButton.SetLabel(l)
}
// SetOnAcceptFunc sets specified function as function triggered after
// message was accepted.
func (mw *MessageWindow) SetOnAcceptFunc(f func(msg *MessageWindow)) {
mw.onAccept = f
}
// SetOnCancelFunc sets specified function as function triggered after
// message was canceled.
func (mw *MessageWindow) SetOnCancelFunc(f func(msg *MessageWindow)) {
mw.onCancel = f
}
// Triggered after accept button clicked.
func (mw *MessageWindow) onAcceptButtonClicked(b *Button) {
if mw.Focused() {
mw.accept()
}
}
// Triggered after cancel button clicked.
func (mw *MessageWindow) onCancelButtonClicked(b *Button) {
if mw.Focused() {
mw.cancel()
}
}
// reset resets window to default state(closed, unfocused).
func (mw *MessageWindow) reset() {
mw.opened = false
mw.focused = false
}
// accept sets message as accepted.
func (mw *MessageWindow) accept() {
mw.reset()
mw.dismissed = true
mw.accepted = true
if mw.onAccept != nil {
mw.onAccept(mw)
}
}
// cancel sets message as canceled.
func (mw *MessageWindow) cancel() {
mw.reset()
mw.dismissed = true
mw.accepted = false
if mw.onCancel != nil {
mw.onCancel(mw)
}
}