-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
346 lines (301 loc) · 9.15 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"flag"
"image"
"image/color"
"log"
"os"
"runtime/pprof"
"time"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget/material"
"github.com/loov/hrtime"
)
const Version = "v1.0.3"
const (
hudTimeout = 2.5
delta = 0.022
windowSizeX = 1280
windowSizeY = 820
defaultWindowWidth = 1024
defaultWindowHeigth = 640
)
var (
windowWidth = defaultWindowWidth
windowHeight = defaultWindowHeigth
// App related variables
hud *Hud
cloth *Cloth
mouse *Mouse
clothW int
clothH int
clothSpacing = 6
// Gio Ops related variables
ops op.Ops
initTime time.Time
deltaTime time.Duration
mouseScrollY unit.Dp
mouseDrag bool
// pprof related variables
profile string
file *os.File
err error
)
func main() {
flag.StringVar(&profile, "debug-cpuprofile", "", "write CPU profile to this file")
flag.Parse()
if profile != "" {
file, err = os.Create(profile)
if err != nil {
log.Fatal(err)
}
}
hud = NewHud()
mouse = &Mouse{maxScrollY: unit.Dp(maxFocusArea)}
mouse.setScrollY(defFocusArea)
go func() {
w := app.NewWindow(
app.Title("Gio - 2D Cloth Simulation"),
app.Size(windowSizeX, windowSizeY),
)
if err := loop(w); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
func loop(w *app.Window) error {
var keyTag struct{}
if profile != "" {
defer pprof.StopCPUProfile()
}
defaultColor := color.NRGBA{R: 0x9a, G: 0x9a, B: 0x9a, A: 0xff}
th := material.NewTheme()
th.Shaper = text.NewShaper(text.WithCollection(gofont.Collection()))
th.TextSize = unit.Sp(12)
th.Palette.ContrastBg = defaultColor
th.FingerSize = 15
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case system.DestroyEvent:
return e.Err
case system.FrameEvent:
start := hrtime.Now()
gtx := layout.NewContext(&ops, e)
hud.panelWidth = gtx.Dp(unit.Dp(windowSizeX))
hud.btnSize = gtx.Dp(unit.Dp(40))
hud.closeBtn = gtx.Dp(unit.Dp(30))
if hud.isActive {
if !hud.panelInit.IsZero() {
dt := time.Since(hud.panelInit).Seconds()
if dt > hudTimeout {
hud.isActive = false
}
}
} else {
hud.panelInit = time.Time{}
}
if profile != "" {
pprof.StartCPUProfile(file)
}
// Cloth is not initialized yet.
if cloth == nil {
clothW = gtx.Dp(unit.Dp(windowWidth))
clothH = gtx.Dp(unit.Dp(windowHeight) * 0.33)
clothSpacing = func() int { // different cloth spacing for hi-res devices.
if clothW <= windowWidth {
return clothSpacing
}
return 2 * clothSpacing
}()
cloth = NewCloth(clothW, clothH, clothSpacing, defaultColor)
width := gtx.Constraints.Max.X
height := gtx.Constraints.Max.Y
startX := int(unit.Dp(width-clothW) / 2)
startY := int(unit.Dp(height) * 0.2)
cloth.Init(startX, startY, hud)
}
key.InputOp{
Tag: &keyTag,
Keys: key.NameEscape + "|" + key.NameCtrl + "|" + key.NameAlt + "|" + key.NameSpace + "|" + key.NameF1,
}.Add(gtx.Ops)
if mouse.getLeftButton() {
deltaTime = time.Since(initTime)
mouse.setForce(deltaTime.Seconds() * 5)
}
for _, ev := range gtx.Queue.Events(&keyTag) {
if e, ok := ev.(key.Event); ok {
if e.State == key.Press {
switch e.Name {
case key.NameSpace:
width := gtx.Constraints.Max.X
height := gtx.Constraints.Max.Y
startX := (width - clothW) / 2
startY := int(unit.Dp(height) * 0.2)
cloth.width = clothW
cloth.height = clothH
cloth.Reset(startX, startY, hud)
case key.NameF1:
hud.showHelpPanel = !hud.showHelpPanel
hud.isActive = false
}
}
if e.Name == key.NameEscape {
hud.showHelpPanel = false
}
}
}
// Reset the window offsets on resize.
hud.winOffsetX = 0
hud.winOffsetY = 0
if defaultWindowWidth != windowWidth {
hud.winOffsetX = float64(e.Size.X-windowWidth) * 0.5
}
if defaultWindowHeigth != windowHeight {
hud.winOffsetY = float64(e.Size.Y-windowHeight) * 0.25
}
if e.Size.X != windowWidth || e.Size.Y != windowHeight {
cloth.Init(windowWidth, windowHeight, hud)
windowWidth = e.Size.X
windowHeight = e.Size.Y
cloth.width = windowWidth
cloth.height = windowHeight
if e.Size.X < defaultWindowWidth {
hud.showHelpPanel = false
}
if e.Size.Y < defaultWindowHeigth {
hud.showHelpPanel = false
}
}
// Fill background
paint.ColorOp{Color: color.NRGBA{R: 0xf2, G: 0xf2, B: 0xf2, A: 0xff}}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
layout.Stack{}.Layout(gtx,
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
// Push a new clip area here so that we can attach a pointer input handler.
// We listen for these canvas interactions here because we don't want to make
// this input area the root of the input tree. If it's the root, it will receive
// copies of all pointer input from its children, and we don't want that.
defer clip.Rect{Max: gtx.Constraints.Max}.Push(gtx.Ops).Pop()
// Add a pointer listener for all of the events that affect the cloth.
pointer.InputOp{
Tag: w,
Types: pointer.Scroll | pointer.Move | pointer.Press | pointer.Drag | pointer.Release | pointer.Type(pointer.ButtonPrimary) | pointer.Type(pointer.ButtonSecondary),
ScrollBounds: image.Rectangle{
Min: image.Point{
X: 0,
Y: -30,
},
Max: image.Point{
X: 0,
Y: 30,
},
},
}.Add(gtx.Ops)
// Process pointer events from previous frame.
for _, ev := range gtx.Queue.Events(w) {
switch ev := ev.(type) {
case pointer.Event:
// We should reset the key focus back to the cloth canvas each time a mouse pointer
// activity is detected. This is required because if the checkbox or reset button is
// activated on the slider panel, the focus will be hold on them indefinitely.
key.FocusOp{Tag: keyTag}.Add(gtx.Ops)
switch ev.Type {
case pointer.Scroll:
mouseScrollY += unit.Dp(ev.Scroll.Y)
if mouseScrollY < minFocusArea {
mouseScrollY = minFocusArea
} else if mouseScrollY > mouse.maxScrollY {
mouseScrollY = mouse.maxScrollY
}
mouse.setScrollY(mouseScrollY)
case pointer.Move:
pos := mouse.getCurrentPosition(ev)
mouse.updatePosition(float64(pos.X), float64(pos.Y))
case pointer.Press:
if ev.Modifiers == key.ModCtrl {
mouse.setCtrlDown(true)
}
mouse.setLeftButton()
initTime = time.Now()
hud.showHelpPanel = false
case pointer.Release:
mouseDrag = false
mouse.resetForce()
mouse.releaseLeftButton()
mouse.releaseRightButton()
mouse.setDragging(mouseDrag)
mouse.setCtrlDown(false)
case pointer.Drag:
mouseDrag = true
}
switch ev.Buttons {
case pointer.ButtonPrimary:
mouse.setLeftButton()
pos := mouse.getCurrentPosition(ev)
mouse.updatePosition(float64(pos.X), float64(pos.Y))
mouse.setDragging(mouseDrag)
case pointer.ButtonSecondary:
mouse.setRightButton()
pos := mouse.getCurrentPosition(ev)
mouse.updatePosition(float64(pos.X), float64(pos.Y))
}
}
}
cloth.Update(gtx, mouse, hud, delta)
return layout.Dimensions{}
}),
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
if hud.debug.Value {
layout.Stack{}.Layout(gtx,
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
op.Offset(image.Pt(10, 10)).Add(gtx.Ops)
return layout.E.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
m := material.Label(th, unit.Sp(15), hrtime.Since(start).String())
m.Color = defaultColor
return m.Layout(gtx)
})
}),
)
}
if hud.isActive {
hud.showHelpPanel = false
for _, ev := range gtx.Queue.Events(&hud.hudTag) {
switch ev := ev.(type) {
case pointer.Event:
switch ev.Type {
case pointer.Leave:
if hud.panelInit.IsZero() {
hud.panelInit = time.Now()
}
case pointer.Move:
hud.panelInit = time.Time{}
}
}
}
}
hud.DrawCtrlBtn(gtx, th, hud.isActive)
hud.ShowControlPanel(gtx, th, hud.isActive)
hud.ShowHelpDialog(gtx, th, hud.showHelpPanel)
return layout.Dimensions{}
}),
)
op.InvalidateOp{}.Add(gtx.Ops)
e.Frame(gtx.Ops)
}
}
}
}