-
Notifications
You must be signed in to change notification settings - Fork 1
/
square.go
87 lines (75 loc) · 1.99 KB
/
square.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
package main
import (
"fmt"
"image"
"9fans.net/go/draw"
"github.com/mjl-/duit"
)
type square struct {
dirty bool
cleanColor, borderColor, dirtyColor *draw.Image
b1, b2, b3 func()
lowdpiSize, size image.Point
m draw.Mouse
}
var _ duit.UI = &square{}
func (ui *square) Layout(dui *duit.DUI, self *duit.Kid, sizeAvail image.Point, force bool) {
x := dui.Scale(ui.lowdpiSize.X)
y := dui.Scale(ui.lowdpiSize.Y)
ui.size = image.Pt(x, y)
self.Layout = duit.Clean
self.Draw = duit.Dirty
self.R = image.Rect(0, 0, x, y)
}
func (ui *square) Draw(dui *duit.DUI, self *duit.Kid, img *draw.Image, orig image.Point, m draw.Mouse, force bool) {
var bg *draw.Image
if ui.dirty {
bg = ui.dirtyColor
} else {
bg = ui.cleanColor
}
img.Draw(self.R.Add(orig), bg, nil, image.ZP)
img.Border(self.R.Add(orig), dui.Scale(1), ui.borderColor, image.ZP)
}
func (ui *square) Mouse(dui *duit.DUI, self *duit.Kid, m draw.Mouse, origM draw.Mouse, orig image.Point) (r duit.Result) {
r.Hit = ui
om := ui.m
ui.m = m
if om.Buttons != 0 && m.Buttons == 0 {
switch om.Buttons {
case duit.Button1:
ui.b1()
case duit.Button2:
ui.b2()
case duit.Button3:
ui.b3()
default:
return
}
dui.Call <- func() {
dui.Focus(ui)
}
r.Consumed = true
}
return
}
func (ui *square) Key(dui *duit.DUI, self *duit.Kid, k rune, m draw.Mouse, orig image.Point) (r duit.Result) {
r.Hit = ui
return
}
func (ui *square) FirstFocus(dui *duit.DUI, self *duit.Kid) (warp *image.Point) {
p := ui.size.Div(2)
return &p
}
func (ui *square) Focus(dui *duit.DUI, self *duit.Kid, o duit.UI) (warp *image.Point) {
if ui != o {
return nil
}
return ui.FirstFocus(dui, self)
}
func (ui *square) Mark(self *duit.Kid, o duit.UI, forLayout bool) (marked bool) {
return self.Mark(o, forLayout)
}
func (ui *square) Print(self *duit.Kid, indent int) {
duit.PrintUI(fmt.Sprintf("square dirty=%v", ui.dirty), self, indent)
}