-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
113 lines (93 loc) · 2.35 KB
/
client.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
package mvm
import (
"fmt"
"github.com/mafik/mvm/matrix"
"github.com/mafik/mvm/ui"
"github.com/mafik/mvm/vec2"
)
type Client interface {
Call(request string) (Event, error)
}
type ClientUI struct {
Client Client
editing map[ui.Editable]bool
focus *Shell
size vec2.Vec2
Touches map[int]*ui.Touch
MenuLayer ui.MenuLayer
}
var clients map[Client]*ClientUI = make(map[Client]*ClientUI)
func MakeClientUI(c Client) *ClientUI {
if ui, ok := clients[c]; ok {
return ui
}
ui := ClientUI{c, make(map[ui.Editable]bool), TheVM.root, vec2.Vec2{0, 0}, make(map[int]*ui.Touch), ui.MakeMenuLayer()}
clients[c] = &ui
return &ui
}
func (c *ClientUI) ToggleEditing(i ui.Editable) {
if c.IsEditing(i) {
delete(c.editing, i)
} else {
c.editing[i] = true
}
}
func (c *ClientUI) IsEditing(i ui.Editable) bool {
_, found := c.editing[i]
return found
}
func (c *ClientUI) GetMenuLayer() *ui.MenuLayer {
return &c.MenuLayer
}
func (c *ClientUI) Focus() *Shell {
return c.focus
}
func (c *ClientUI) MeasureText(text string) float64 {
request := fmt.Sprintf("[\"measureText\",%q]", text)
result, err := c.Client.Call(request)
if err != nil {
panic("Bad result of MeasureText: " + result.Type)
}
return result.Width
}
type GoUp struct {
*ClientUI
}
func (GoUp) Name() string { return "Go up" }
func (GoUp) Keycode() string { return "KeyW" }
func (gu GoUp) Activate(ui.TouchContext) ui.Action {
if gu.focus.parent != nil {
gu.focus = gu.focus.parent
}
return nil
}
type Quit struct{}
func (Quit) Name() string { return "Quit" }
func (Quit) Keycode() string { return "Escape" }
func (Quit) Activate(ui.TouchContext) ui.Action {
err := SaveImage()
if err != nil {
fmt.Println(err)
}
keep_running = false
return nil
}
func (c *ClientUI) Options(vec2.Vec2) []ui.Option {
return []ui.Option{Quit{}, GoUp{c}}
}
func (c *ClientUI) Children() (children []interface{}) {
children = append(children, Background{c.size})
if graphic, ok := c.focus.object.(GraphicObject); ok {
children = append(children, graphic.MakeWidget(c.focus))
}
children = append(children, &c.MenuLayer)
return
}
func (c *ClientUI) Transform(ui.TextMeasurer) matrix.Matrix {
return matrix.Translate(vec2.Scale(c.size, 0.5))
}
type Background struct{ vec2.Vec2 }
func (b Background) Draw(ctx *ui.Context2D) {
ctx.FillStyle("#ccc")
ctx.FillRect(-b.X/2, -b.Y/2, b.X, b.Y)
}