-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicseffects.go
258 lines (198 loc) · 6.38 KB
/
graphicseffects.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
// Copyright 2017 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"math"
)
var (
borderGlowAlpha = []float64{0.2, 0.1, 0.075, 0.05, 0.075}
InteractionEffect WidgetGraphicsEffect
FocusEffect WidgetGraphicsEffect
)
type WidgetGraphicsEffect interface {
Draw(widget Widget, canvas *Canvas) error
}
type widgetGraphicsEffectBase struct {
color Color
bitmap *Bitmap
}
func (wgeb *widgetGraphicsEffectBase) create(color Color) error {
bitmap, err := NewBitmapWithTransparentPixels(Size{12, 12})
if err != nil {
return err
}
canvas, err := NewCanvasFromImage(bitmap)
if err != nil {
return err
}
defer canvas.Dispose()
var succeeded bool
defer func() {
if !succeeded {
bitmap.Dispose()
}
}()
for i := 1; i <= 5; i++ {
bmp, err := NewBitmapWithTransparentPixels(Size{i*2 + 2, i*2 + 2})
if err != nil {
return err
}
defer bmp.Dispose()
bmpCanvas, err := NewCanvasFromImage(bmp)
if err != nil {
return err
}
defer bmpCanvas.Dispose()
color := RGB(
byte(math.Min(1.0, float64(color.R())/255.0-0.1+0.1*float64(i))*255.0),
byte(math.Min(1.0, float64(color.G())/255.0-0.1+0.1*float64(i))*255.0),
byte(math.Min(1.0, float64(color.B())/255.0-0.1+0.1*float64(i))*255.0),
)
brush, err := NewSolidColorBrush(color)
if err != nil {
return err
}
defer brush.Dispose()
if err := bmpCanvas.FillRoundedRectangle(brush, Rectangle{0, 0, i*2 + 2, i*2 + 2}, Size{i * 2, i * 2}); err != nil {
return err
}
bmpCanvas.Dispose()
opacity := byte(borderGlowAlpha[i-1] * 255.0)
canvas.DrawBitmapWithOpacity(bmp, Rectangle{5 - i, 5 - i, i*2 + 2, i*2 + 2}, opacity)
}
succeeded = true
wgeb.color = color
wgeb.bitmap = bitmap
return nil
}
func (wgeb *widgetGraphicsEffectBase) Dispose() {
if wgeb.bitmap != nil {
wgeb.bitmap.Dispose()
wgeb.bitmap = nil
}
}
type BorderGlowEffect struct {
widgetGraphicsEffectBase
}
func NewBorderGlowEffect(color Color) (*BorderGlowEffect, error) {
bge := new(BorderGlowEffect)
if err := bge.create(color); err != nil {
return nil, err
}
return bge, nil
}
func (bge *BorderGlowEffect) Draw(widget Widget, canvas *Canvas) error {
b := widget.Bounds()
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X - 5, b.Y - 5, 5, 5}, Rectangle{0, 0, 5, 5})
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X, b.Y - 5, b.Width, 5}, Rectangle{5 + 1, 0, 1, 5})
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X + b.Width, b.Y - 5, 5, 5}, Rectangle{5 + 2, 0, 5, 5})
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X + b.Width, b.Y, 5, b.Height}, Rectangle{5 + 2, 5 + 1, 5, 1})
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X + b.Width, b.Y + b.Height, 5, 5}, Rectangle{5 + 2, 5 + 2, 5, 5})
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X, b.Y + b.Height, b.Width, 5}, Rectangle{5 + 1, 5 + 2, 1, 5})
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X - 5, b.Y + b.Height, 5, 5}, Rectangle{0, 5 + 2, 5, 5})
canvas.DrawBitmapPart(bge.bitmap, Rectangle{b.X - 5, b.Y, 5, b.Height}, Rectangle{0, 5 + 1, 5, 1})
return nil
}
type DropShadowEffect struct {
widgetGraphicsEffectBase
}
func NewDropShadowEffect(color Color) (*DropShadowEffect, error) {
dse := new(DropShadowEffect)
if err := dse.create(color); err != nil {
return nil, err
}
return dse, nil
}
func (dse *DropShadowEffect) Draw(widget Widget, canvas *Canvas) error {
b := widget.Bounds()
canvas.DrawBitmapPart(dse.bitmap, Rectangle{b.X + b.Width, b.Y + 10 - 5, 5, 5}, Rectangle{5 + 2, 0, 5, 5})
canvas.DrawBitmapPart(dse.bitmap, Rectangle{b.X + b.Width, b.Y + 10, 5, b.Height - 10}, Rectangle{5 + 2, 5 + 1, 5, 1})
canvas.DrawBitmapPart(dse.bitmap, Rectangle{b.X + b.Width, b.Y + b.Height, 5, 5}, Rectangle{5 + 2, 5 + 2, 5, 5})
canvas.DrawBitmapPart(dse.bitmap, Rectangle{b.X + 10, b.Y + b.Height, b.Width - 10, 5}, Rectangle{5 + 1, 5 + 2, 1, 5})
canvas.DrawBitmapPart(dse.bitmap, Rectangle{b.X + 10 - 5, b.Y + b.Height, 5, 5}, Rectangle{0, 5 + 2, 5, 5})
return nil
}
type widgetGraphicsEffectListObserver interface {
onInsertedGraphicsEffect(index int, effect WidgetGraphicsEffect) error
onRemovedGraphicsEffect(index int, effect WidgetGraphicsEffect) error
onClearedGraphicsEffects() error
}
type WidgetGraphicsEffectList struct {
items []WidgetGraphicsEffect
observer widgetGraphicsEffectListObserver
}
func newWidgetGraphicsEffectList(observer widgetGraphicsEffectListObserver) *WidgetGraphicsEffectList {
return &WidgetGraphicsEffectList{observer: observer}
}
func (l *WidgetGraphicsEffectList) Add(effect WidgetGraphicsEffect) error {
if effect == nil {
return newErr("effect == nil")
}
return l.Insert(len(l.items), effect)
}
func (l *WidgetGraphicsEffectList) At(index int) WidgetGraphicsEffect {
return l.items[index]
}
func (l *WidgetGraphicsEffectList) Clear() error {
observer := l.observer
oldItems := l.items
l.items = l.items[:0]
if observer != nil {
if err := observer.onClearedGraphicsEffects(); err != nil {
l.items = oldItems
return err
}
}
return nil
}
func (l *WidgetGraphicsEffectList) Index(effect WidgetGraphicsEffect) int {
for i, item := range l.items {
if item == effect {
return i
}
}
return -1
}
func (l *WidgetGraphicsEffectList) Contains(effect WidgetGraphicsEffect) bool {
return l.Index(effect) > -1
}
func (l *WidgetGraphicsEffectList) insertIntoSlice(index int, effect WidgetGraphicsEffect) {
l.items = append(l.items, nil)
copy(l.items[index+1:], l.items[index:])
l.items[index] = effect
}
func (l *WidgetGraphicsEffectList) Insert(index int, effect WidgetGraphicsEffect) error {
observer := l.observer
l.insertIntoSlice(index, effect)
if observer != nil {
if err := observer.onInsertedGraphicsEffect(index, effect); err != nil {
l.items = append(l.items[:index], l.items[index+1:]...)
return err
}
}
return nil
}
func (l *WidgetGraphicsEffectList) Len() int {
return len(l.items)
}
func (l *WidgetGraphicsEffectList) Remove(effect WidgetGraphicsEffect) error {
index := l.Index(effect)
if index == -1 {
return nil
}
return l.RemoveAt(index)
}
func (l *WidgetGraphicsEffectList) RemoveAt(index int) error {
observer := l.observer
item := l.items[index]
l.items = append(l.items[:index], l.items[index+1:]...)
if observer != nil {
if err := observer.onRemovedGraphicsEffect(index, item); err != nil {
l.insertIntoSlice(index, item)
return err
}
}
return nil
}