-
Notifications
You must be signed in to change notification settings - Fork 2
/
shiny.go
231 lines (216 loc) · 4.7 KB
/
shiny.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
package shrew
import (
"image"
"image/color"
"image/draw"
"sync"
"github.com/as/frame/font"
"github.com/as/ui"
"github.com/golang/freetype/truetype"
"golang.org/x/exp/shiny/screen"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/mouse"
"golang.org/x/mobile/event/paint"
)
type ShinyScreen struct {
dev *ui.Dev
K chan Kbd
M chan Mouse
ft *truetype.Font
Bitmap
}
func (s *ShinyScreen) AllocImage(r image.Rectangle) Bitmap {
b := s.dev.NewBuffer(r.Size())
bmp := &ShinyBitmap{
sp: r.Min,
size: r.Size(),
b: b,
w: s.dev.Window(),
ctl: make(chan Msg, 11),
ctl2: make(chan Msg),
}
go bmp.run()
return bmp
}
func (s *ShinyScreen) Bounds() image.Rectangle { return image.Rect(0, 0, 2500, 1400) }
func (s *ShinyScreen) Kbd() chan Kbd { return s.K }
func (s *ShinyScreen) Mouse() chan Mouse { return s.M }
func ShinyClient() *ShinyScreen {
dev, err := ui.Init(nil)
if err != nil {
panic(err)
}
w := dev.Window()
K := make(chan Kbd, 25)
M := make(chan Mouse, 25)
sc := &ShinyScreen{
dev: dev,
K: K,
M: M,
}
sc.fontinit()
//sc.Bitmap = sc.AllocImage(image.Rect(0, 0, 1024, 768))
mstate := Mouse{}
go func() {
for {
switch e := w.NextEvent().(type) {
case mouse.Event:
if e.Direction == 1 {
mstate.Button |= 1 << uint(e.Button-1)
} else if e.Direction == 2 {
mstate.Button &^= 1 << uint(e.Button-1)
}
mstate.X = int(e.X)
mstate.Y = int(e.Y)
M <- mstate
case key.Event:
press := 0
if e.Direction == 1 || e.Direction == 0 {
press = 1
}
if e.Rune == 13 {
e.Rune = 10
}
K <- Kbd{
Rune: e.Rune,
Press: press,
}
case paint.Event:
case interface{}:
}
}
}()
return sc
}
type ShinyBitmap struct {
sp image.Point
size image.Point
b screen.Buffer
w screen.Window
ctl chan Msg // for drawing
ctl2 chan Msg // for wsys
wg sync.WaitGroup
refresh chan Msg
draw chan Msg
//ff font.Face
}
func (s *ShinyBitmap) Set(x, y int, v color.Color) {
s.b.RGBA().Set(x, y, v)
}
func (s *ShinyBitmap) At(x, y int) color.Color {
return s.b.RGBA().At(x, y)
}
func (s *ShinyBitmap) ColorModel() color.Model {
return s.b.RGBA().ColorModel()
}
type Msg struct {
string
Kind string
Sp image.Point
Name string
kind byte
dst draw.Image
p image.Point
r image.Rectangle
src image.Image
sp image.Point
op draw.Op
replyc chan error
replyint chan int
data []byte
ft *font.Font
bg image.Image
bgp image.Point
}
func (m *Msg) Canon(s *ShinyBitmap) {
if m.dst == s || m.dst == nil {
m.dst = s.b.RGBA()
}
if m.src == s || m.src == nil {
m.src = s.b.RGBA()
}
if m.bg == s {
m.bg = s.b.RGBA()
}
}
func (s *ShinyBitmap) Move(sp image.Point) {
s.ctl2 <- Msg{
string: "move",
sp: sp,
}
}
func (s *ShinyBitmap) run() {
draw.Draw(s.b.RGBA(), s.b.Bounds(), image.NewUniform(color.RGBA{77, 77, 77, 255}), image.ZP, draw.Src)
s.w.Upload(s.sp, s.b, s.b.Bounds())
for {
select {
case Msg := <-s.ctl2:
if Msg.string == "move" {
s.sp = Msg.sp
}
case Msg := <-s.ctl:
(&Msg).Canon(s)
switch Msg.kind {
case 'd', 'x':
if Msg.kind == 'd' {
draw.Draw(Msg.dst, Msg.r, Msg.src, Msg.sp, Msg.op)
} else {
Msg.replyint <- s.stringBG(Msg.dst, Msg.p, Msg.src, Msg.sp, Msg.ft, Msg.data, Msg.bg, Msg.bgp)
//s.drawBytes(msg.dst, msg.sp, msg.src, msg.data)
}
case 'f':
r := Msg.r
dp := s.sp.Add(r.Min)
s.w.Upload(dp, s.b, r)
}
}
}
}
func (s *ShinyBitmap) Bounds() image.Rectangle {
return s.b.Bounds() //.Add(s.sp)
}
func (s *ShinyBitmap) Draw(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point, op draw.Op) {
s.ctl <- Msg{
kind: 'd',
dst: dst,
r: r,
src: src,
sp: sp,
op: op,
}
}
func (s *ShinyBitmap) DrawBytes(dst draw.Image, dot image.Point, src image.Image, data []byte) {
s.ctl <- Msg{
kind: 'x',
dst: dst,
src: src,
sp: dot,
data: data,
}
}
func (s *ShinyBitmap) stringBG(dst draw.Image, p image.Point, src image.Image, sp image.Point, ft *font.Font, data []byte, bg image.Image, bgp image.Point) int {
return font.StringBG(dst, p, src, sp, ft, data, bg, bgp)
}
func (s *ShinyBitmap) StringBG(dst draw.Image, p image.Point, src image.Image, sp image.Point, ft *font.Font, data []byte, bg image.Image, bgp image.Point) int {
replyint := make(chan int)
s.ctl <- Msg{
kind: 'x',
dst: dst,
p: p,
src: src,
sp: p,
ft: ft,
data: data,
bg: bg,
bgp: bgp,
replyint: replyint,
}
return <-replyint
}
func (s *ShinyBitmap) Flush(r image.Rectangle) error {
s.ctl <- Msg{
kind: 'f',
r: r,
}
return nil
}