generated from roman-mazur/architecture-lab-3
/
window.go
160 lines (136 loc) · 3.26 KB
/
window.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
package ui
import (
"fmt"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/imageutil"
"golang.org/x/exp/shiny/screen"
"golang.org/x/image/draw"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/mouse"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
"image"
"image/color"
"log"
)
type Visualizer struct {
Title string
Debug bool
OnScreenReady func(s screen.Screen)
w screen.Window
tx chan screen.Texture
done chan struct{}
sz size.Event
pos image.Rectangle
mouseCoords image.Point
}
func (pw *Visualizer) Main() {
pw.tx = make(chan screen.Texture)
pw.done = make(chan struct{})
pw.pos.Max.X = 200
pw.pos.Max.Y = 200
pw.mouseCoords = image.Point{X: 400, Y: 400}
driver.Main(pw.run)
}
func (pw *Visualizer) Update(t screen.Texture) {
pw.tx <- t
}
func (pw *Visualizer) run(s screen.Screen) {
w, err := s.NewWindow(&screen.NewWindowOptions{
Title: pw.Title,
Width: 800,
Height: 800,
})
if pw.OnScreenReady != nil {
pw.OnScreenReady(s)
}
if err != nil {
log.Fatal("Failed to initialize the app window:", err)
}
defer func() {
w.Release()
close(pw.done)
}()
pw.w = w
events := make(chan any)
go func() {
for {
e := w.NextEvent()
if pw.Debug {
log.Printf("new event: %v", e)
}
if detectTerminate(e) {
close(events)
break
}
events <- e
}
}()
var t screen.Texture
for {
select {
case e, ok := <-events:
if !ok {
return
}
pw.handleEvent(e, t)
case t = <-pw.tx:
w.Send(paint.Event{})
}
}
}
func detectTerminate(e any) bool {
switch e := e.(type) {
case lifecycle.Event:
if e.To == lifecycle.StageDead {
return true // Window destroy initiated.
}
case key.Event:
if e.Code == key.CodeEscape {
return true // Esc pressed.
}
}
return false
}
func (pw *Visualizer) handleEvent(e any, t screen.Texture) {
switch e := e.(type) {
case size.Event: // Оновлення даних про розмір вікна.
pw.sz = e
case error:
log.Printf("ERROR: %s", e)
case mouse.Event:
if t == nil {
if e.Button == mouse.ButtonLeft && e.Direction == mouse.DirPress {
pw.mouseCoords = image.Point{
X: int(e.X),
Y: int(e.Y),
}
pw.w.Send(paint.Event{})
fmt.Println("X, Y: ", e.X, e.Y)
}
}
case paint.Event:
// Малювання контенту вікна.
if t == nil {
pw.drawDefaultUI()
} else {
// Використання текстури отриманої через виклик Update.
pw.w.Scale(pw.sz.Bounds(), t, t.Bounds(), draw.Src, nil)
}
pw.w.Publish()
}
}
func (pw *Visualizer) drawDefaultUI() {
pw.w.Fill(pw.sz.Bounds(), color.White, draw.Src) // Фон.
x, y := pw.mouseCoords.X, pw.mouseCoords.Y
c := color.RGBA{R: 219, G: 208, B: 48, A: 1}
pw.w.Fill(image.Rect(x-200, y+100, x+200, y-100), c, draw.Src)
pw.w.Fill(image.Rect(x-100, y-200, x+100, y+200), c, draw.Src)
//pw.w.Fill(image.Rect(200, 500, 600, 300), color.RGBA{R: 219, G: 208, B: 48, A: 1}, draw.Src)
//pw.w.Fill(image.Rect(300, 200, 500, 600), color.RGBA{R: 219, G: 208, B: 48, A: 1}, draw.Src)
// Малювання білої рамки.
for _, br := range imageutil.Border(pw.sz.Bounds(), 10) {
pw.w.Fill(br, color.White, draw.Src)
}
}