-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinteraction.go
261 lines (250 loc) · 9.03 KB
/
interaction.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
package ui
import (
"context"
"fmt"
"github.com/Yeicor/sdfx-ui/internal"
"github.com/barkimedes/go-deepcopy"
"github.com/deadsy/sdfx/vec/conv"
v2 "github.com/deadsy/sdfx/vec/v2"
"github.com/deadsy/sdfx/vec/v2i"
v3 "github.com/deadsy/sdfx/vec/v3"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil"
"github.com/hajimehoshi/ebiten/text"
"image/color"
"math"
"strconv"
"time"
)
// onUpdateInputs handles inputs
func (r *Renderer) onUpdateInputs() {
r.implLock.RLock()
defer r.implLock.RUnlock()
r.onUpdateInputsCommon()
// SDF2/SDF3-SPECIFIC CONTROLS
r.implStateLock.RLock()
implDimCache := r.implDimCache
r.implStateLock.RUnlock()
switch implDimCache {
case 2:
r.onUpdateInputsSDF2()
case 3:
r.onUpdateInputsSDF3()
default:
panic("RendererState.onUpdateInputs not implemented for " + strconv.Itoa(r.implDimCache) + " dimensions")
}
}
func (r *Renderer) onUpdateInputsCommon() {
// SHARED CONTROLS
if inpututil.IsKeyJustPressed(ebiten.KeyKPAdd) || inpututil.IsKeyJustPressed(ebiten.KeyEqual) {
r.implStateLock.Lock()
r.implState.ResInv /= 2
if r.implState.ResInv < 1 {
r.implState.ResInv = 1
}
r.implStateLock.Unlock()
r.rerender()
}
if inpututil.IsKeyJustPressed(ebiten.KeyKPSubtract) || inpututil.IsKeyJustPressed(ebiten.KeyMinus) {
r.implStateLock.Lock()
r.implState.ResInv *= 2
if r.implState.ResInv > 64 {
r.implState.ResInv = 64
}
r.implStateLock.Unlock()
r.rerender()
}
if inpututil.IsKeyJustPressed(ebiten.KeyB) {
r.implStateLock.Lock()
r.implState.DrawBbs = !r.implState.DrawBbs
r.implStateLock.Unlock()
r.rerender()
}
// Color
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
r.implStateLock.Lock()
r.implState.ColorMode = (r.implState.ColorMode + 1) % r.impl.ColorModes()
r.implStateLock.Unlock()
r.rerender()
}
if r.smoothCamera {
r.implStateLock.RLock()
if r.translateFrom.X != math.MaxInt {
r.rerenderOpt(false) // Trigger mid-movement rerender
}
r.implStateLock.RUnlock()
}
}
func (r *Renderer) onUpdateInputsSDF2() {
// Zooming
_, wheelUpDown := ebiten.Wheel()
if wheelUpDown != 0 {
r.implStateLock.Lock()
scale := 1 - wheelUpDown*r.implState.Bb.Size().Length2()*0.02 // Scale depending on current scale
scale = math.Max(1/r.zoomFactor, math.Min(r.zoomFactor, scale)) // Apply zoom limits
r.implState.Bb = r.implState.Bb.ScaleAboutCenter(scale)
r.implStateLock.Unlock()
r.rerender()
}
// Translation
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonMiddle) || len(inpututil.JustPressedTouchIDs()) > 0 {
// Save the cursor's position for previsualization and applying the final translation
cx, cy := getCursor()
r.implStateLock.Lock()
if r.translateFrom.X == math.MaxInt { // Only if not already moving...
r.translateFrom = v2i.Vec{X: cx, Y: cy}
}
r.implStateLock.Unlock()
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonMiddle) || inpututil.IsTouchJustReleased(0) {
// Actually apply the translation and force a rerender
cx, cy := getCursor()
r.implStateLock.Lock()
if r.translateFrom.X != math.MaxInt { // Only if already moving...
r.implState = r.apply2DCameraMoveTo(cx, cy)
// Keep displacement until rerender is complete (avoid jump) using callback below + extra variable set here
r.translateFromStop = v2i.Vec{X: cx, Y: cy}
}
if r.smoothCamera {
r.translateFrom = v2i.Vec{X: math.MaxInt, Y: math.MaxInt}
}
r.implStateLock.Unlock()
r.rerender(func(err error) {
r.implStateLock.Lock()
if !r.smoothCamera {
r.translateFrom = v2i.Vec{X: math.MaxInt, Y: math.MaxInt}
}
r.translateFromStop = v2i.Vec{X: math.MaxInt, Y: math.MaxInt}
r.implStateLock.Unlock()
})
}
// Reset camera transform (100% of surface)
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
r.implStateLock.Lock()
r.implState.Bb = toBox2(r.impl.BoundingBox()) // 100% zoom (impl2 will fix aspect ratio)
r.implStateLock.Unlock()
r.rerender()
}
}
func (r *Renderer) apply2DCameraMoveTo(cx int, cy int) *internal.RendererState {
newVal := deepcopy.MustAnything(r.implState).(*internal.RendererState)
newVal.Bb = r.implState.Bb.Translate(
conv.V2iToV2(r.translateFrom).Sub(conv.V2iToV2(v2i.Vec{X: cx, Y: cy})).Mul(v2.Vec{X: 1, Y: -1}). // Invert Y
Div(conv.V2iToV2(r.screenSize)).Mul(r.implState.Bb.Size()))
return newVal
}
func getCursor() (int, int) {
cx, cy := ebiten.CursorPosition()
if tX, tY := ebiten.TouchPosition(0); tX != 0 && tY != 0 { // Override cursor with touch if available
cx, cy = tX, tY // FIXME: Probably 0 does not exist anymore
}
return cx, cy
}
func (r *Renderer) onUpdateInputsSDF3() {
// Zooming
_, wheelUpDown := ebiten.Wheel()
if wheelUpDown != 0 {
r.implStateLock.Lock()
scale := 1 - wheelUpDown*100
scale = math.Max(1/r.zoomFactor, math.Min(r.zoomFactor, scale)) // Apply zoom limits
r.implState.CamDist *= scale
r.implStateLock.Unlock()
r.rerender()
}
r.onUpdateInputsSDF3RotTrans()
// Reset camera transform
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
r.implStateLock.Lock()
resetCam3(r.implState, r)
r.implStateLock.Unlock()
r.rerender()
}
}
func (r *Renderer) onUpdateInputsSDF3RotTrans() {
// Rotation + Translation
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonMiddle) || len(inpututil.JustPressedTouchIDs()) > 0 {
// Save the cursor's position for previsualization and applying the final translation
cx, cy := getCursor()
r.implStateLock.Lock()
if r.translateFrom.X == math.MaxInt { // Only if not already moving...
r.translateFrom = v2i.Vec{X: cx, Y: cy}
}
r.implStateLock.Unlock()
}
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonMiddle) || inpututil.IsTouchJustReleased(0) {
// Actually apply the translation and force a rerender
cx, cy := getCursor()
r.implStateLock.Lock()
if r.translateFrom.X != math.MaxInt { // Only if already moving...
r.implState = r.apply3DCameraMoveTo(cx, cy)
// Keep displacement until rerender is complete (avoid jump) using callback below + extra variable set here
r.translateFromStop = v2i.Vec{X: cx, Y: cy}
}
if r.smoothCamera {
r.translateFrom = v2i.Vec{X: math.MaxInt, Y: math.MaxInt}
}
r.implStateLock.Unlock()
r.rerender(func(err error) {
r.implStateLock.Lock()
if !r.smoothCamera {
r.translateFrom = v2i.Vec{X: math.MaxInt, Y: math.MaxInt}
}
r.translateFromStop = v2i.Vec{X: math.MaxInt, Y: math.MaxInt}
r.implStateLock.Unlock()
})
}
}
func (r *Renderer) apply3DCameraMoveTo(cx int, cy int) *internal.RendererState {
newVal := deepcopy.MustAnything(r.implState).(*internal.RendererState)
delta := conv.V2iToV2(v2i.Vec{X: cx, Y: cy}).Sub(conv.V2iToV2(r.translateFrom))
if ebiten.IsKeyPressed(ebiten.KeyShift) { // Translation
// Move on the plane perpendicular to the camera's direction
camViewMatrix := cam3MatrixNoTranslation(r.implState)
camPos := r.implState.CamCenter.Add(camViewMatrix.MulPosition(v3.Vec{Y: -r.implState.CamDist}))
camDir := r.implState.CamCenter.Sub(camPos).Normalize()
planeZero := r.implState.CamCenter
planeRight := v3.Vec{Z: 1}.Cross(camDir).Normalize()
planeUp := camDir.Cross(planeRight).Normalize()
newPos := planeZero. // TODO: Proper projection on plane delta computation
Add(planeRight.MulScalar(delta.X * r.implState.CamDist / 200)).
Add(planeUp.MulScalar(delta.Y * r.implState.CamDist / 200))
newVal.CamCenter = newPos
//log.Println("New camera pivot (center", r.implState.CamCenter, ")")
} else { // Rotation
newVal.CamYaw -= delta.X / 100 // TODO: Proper delta computation
if newVal.CamYaw < -math.Pi {
newVal.CamYaw += 2 * math.Pi // Limits (wrap around)
} else if newVal.CamYaw > math.Pi {
newVal.CamYaw -= 2 * math.Pi // Limits (wrap around)
}
newVal.CamPitch -= delta.Y / 100
newVal.CamPitch = math.Max(-(math.Pi/2 - 1e-5), math.Min(math.Pi/2-1e-5, newVal.CamPitch))
//log.Println("New camera rotation (pitch", r.implState.CamPitch, "yaw", r.implState.CamYaw, ")")
}
return newVal
}
// ControlsText returns the help text
func (r *Renderer) drawUI(screen *ebiten.Image) {
// Notify when rendering
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Millisecond)
defer cancelFunc()
if r.renderingLock.RTryLock(ctx) {
r.renderingLock.RUnlock()
} else {
drawDefaultTextWithShadow(screen, "Rendering...", 5, 5+12, color.RGBA{R: 255, A: 255})
}
// Draw current state and controls
r.implStateLock.RLock()
defer r.implStateLock.RUnlock()
msgFmt := "TPS: %0.2f/%d\nResolution: %.2f [+ or = / -]\nColor: %d [C]\nBoxes: %t [B]\nReset camera [R]"
msgValues := []interface{}{ebiten.CurrentTPS(), ebiten.MaxTPS(), 1 / float64(r.implState.ResInv), r.implState.ColorMode, r.implState.DrawBbs}
switch r.implDimCache {
case 2:
msgFmt = "SDF2 Renderer\n=============\n" + msgFmt + "\nTranslate cam [MiddleMouse]\nZoom cam [MouseWheel]"
case 3:
msgFmt = "SDF3 Renderer\n=============\n" + msgFmt + "\nRotate cam [MiddleMouse]\nTranslate cam [Shift+MiddleMouse]\nZoom cam [MouseWheel]"
}
msg := fmt.Sprintf(msgFmt, msgValues...)
boundString := text.BoundString(defaultFont, msg)
drawDefaultTextWithShadow(screen, msg, 5, r.screenSize.Y-boundString.Size().Y+10, color.RGBA{G: 255, A: 255})
}