forked from anomalyco/opentui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmouse-interaction-demo.ts
More file actions
428 lines (373 loc) · 11.5 KB
/
mouse-interaction-demo.ts
File metadata and controls
428 lines (373 loc) · 11.5 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env bun
import {
CliRenderer,
createCliRenderer,
RGBA,
TextAttributes,
FrameBufferRenderable,
TextRenderable,
t,
type MouseEvent,
OptimizedBuffer,
BoxRenderable,
createTimeline,
engine,
Box,
type ProxiedVNode,
type BoxOptions,
Text,
type VChild,
} from "../index"
import { setupCommonDemoKeys } from "./lib/standalone-keys"
interface TrailCell {
x: number
y: number
timestamp: number
isDrag?: boolean
}
let demoContainer: MouseInteractionFrameBuffer | null = null
let titleText: TextRenderable | null = null
let instructionsText: TextRenderable | null = null
let draggableBoxes: ProxiedVNode<typeof BoxRenderable>[] = []
let nextZIndex = 101
function DraggableBox(
props: BoxOptions & {
x: number
y: number
width: number
height: number
color: RGBA
label: string
},
children?: VChild,
) {
const bgColor = RGBA.fromValues(props.color.r, props.color.g, props.color.b, 0.8)
const borderColor = RGBA.fromValues(props.color.r * 1.2, props.color.g * 1.2, props.color.b * 1.2, 1.0)
let isDragging = false
let gotText = ""
let scrollText = ""
let scrollTimestamp = 0
let dragOffsetX = 0
let dragOffsetY = 0
let bounceScale = { value: 1 }
let baseWidth: number = props.width
let baseHeight: number = props.height
let originalBg: RGBA = bgColor
let dragBg: RGBA = RGBA.fromValues(props.color.r, props.color.g, props.color.b, 0.3)
let originalBorderColor: RGBA = borderColor
let dragBorderColor: RGBA = RGBA.fromValues(props.color.r * 1.2, props.color.g * 1.2, props.color.b * 1.2, 0.5)
return Box(
{
...props,
position: "absolute",
left: props.x,
top: props.y,
width: props.width,
height: props.height,
backgroundColor: bgColor,
borderColor: borderColor,
borderStyle: "rounded",
title: props.label,
titleAlignment: "center",
border: true,
zIndex: 100,
renderAfter(buffer, deltaTime) {
const currentTime = Date.now()
if (scrollText && currentTime - scrollTimestamp > 2000) {
scrollText = ""
}
const baseCenterX = this.x + Math.floor(this.width / 2)
const baseCenterY = this.y + Math.floor(this.height / 2)
let textLines = 0
if (isDragging) textLines++
if (scrollText) textLines++
if (gotText) textLines += 2
let currentY = textLines > 1 ? baseCenterY - Math.floor(textLines / 2) : baseCenterY
if (isDragging) {
const centerX = baseCenterX - 2
buffer.drawText("drag", centerX, currentY, RGBA.fromInts(64, 224, 208))
currentY++
}
if (scrollText) {
const age = currentTime - scrollTimestamp
const fadeRatio = Math.max(0, 1 - age / 2000)
const alpha = Math.round(255 * fadeRatio)
const centerX = baseCenterX - Math.floor(scrollText.length / 2)
buffer.drawText(scrollText, centerX, currentY, RGBA.fromInts(255, 255, 0, alpha))
currentY++
}
if (gotText) {
const gotX = baseCenterX - 2
const gotTextX = baseCenterX - Math.floor(gotText.length / 2)
buffer.drawText("got", gotX, currentY, RGBA.fromInts(255, 182, 193))
currentY++
buffer.drawText(gotText, gotTextX, currentY, RGBA.fromInts(147, 226, 255))
}
},
onMouse(event: MouseEvent): void {
switch (event.type) {
case "down":
gotText = ""
isDragging = true
dragOffsetX = event.x - this.x
dragOffsetY = event.y - this.y
this.zIndex = nextZIndex++
this.backgroundColor = dragBg
this.borderColor = dragBorderColor
event.stopPropagation()
break
case "drag-end":
if (isDragging) {
isDragging = false
this.zIndex = 100
this.backgroundColor = originalBg
this.borderColor = originalBorderColor
event.stopPropagation()
}
break
case "drag":
if (isDragging) {
const newX = event.x - dragOffsetX
const newY = event.y - dragOffsetY
const boundedX = Math.max(0, Math.min(newX, this._ctx.width - this.width))
const boundedY = Math.max(4, Math.min(newY, this._ctx.height - this.height))
this.x = boundedX
this.y = boundedY
event.stopPropagation()
}
break
case "over":
gotText = "over " + (event.source?.id || "")
break
case "out":
gotText = "out"
break
case "drop":
gotText = event.source?.id || ""
const timeline = createTimeline()
timeline.add(bounceScale, {
value: 1.5,
duration: 200,
ease: "outExpo",
onUpdate: (values) => {
const scale = values.targets[0].value
this.width = Math.round(baseWidth * scale)
this.height = Math.round(baseHeight * scale)
},
})
timeline.add(
bounceScale,
{
value: 1.0,
duration: 400,
ease: "outExpo",
onUpdate: (values) => {
const scale = values.targets[0].value
this.width = Math.round(baseWidth * scale)
this.height = Math.round(baseHeight * scale)
},
},
200,
)
break
case "scroll":
if (event.scroll) {
scrollText = `scroll ${event.scroll.direction}`
scrollTimestamp = Date.now()
event.stopPropagation()
}
break
}
},
},
children,
)
}
class MouseInteractionFrameBuffer extends FrameBufferRenderable {
private readonly trailCells = new Map<string, TrailCell>()
private readonly activatedCells = new Set<string>()
private readonly TRAIL_FADE_DURATION = 3000
private readonly TRAIL_COLOR = RGBA.fromInts(64, 224, 208, 255)
private readonly DRAG_COLOR = RGBA.fromInts(255, 165, 0, 255)
private readonly ACTIVATED_COLOR = RGBA.fromInts(255, 20, 147, 255)
private readonly BACKGROUND_COLOR = RGBA.fromInts(15, 15, 35, 255)
private readonly CURSOR_COLOR = RGBA.fromInts(255, 255, 255, 255)
constructor(id: string, renderer: CliRenderer) {
super(renderer, {
id,
width: renderer.terminalWidth,
height: renderer.terminalHeight,
zIndex: 0,
})
}
protected renderSelf(buffer: OptimizedBuffer): void {
const currentTime = Date.now()
this.frameBuffer.clear(this.BACKGROUND_COLOR)
for (const [key, cell] of this.trailCells.entries()) {
if (currentTime - cell.timestamp > this.TRAIL_FADE_DURATION) {
this.trailCells.delete(key)
}
}
for (const [, cell] of this.trailCells.entries()) {
const age = currentTime - cell.timestamp
const fadeRatio = 1 - age / this.TRAIL_FADE_DURATION
if (fadeRatio > 0) {
const baseColor = cell.isDrag ? this.DRAG_COLOR : this.TRAIL_COLOR
const smoothAlpha = fadeRatio
const fadedColor = RGBA.fromValues(baseColor.r, baseColor.g, baseColor.b, smoothAlpha)
this.frameBuffer.setCellWithAlphaBlending(cell.x, cell.y, "█", fadedColor, this.BACKGROUND_COLOR)
}
}
for (const cellKey of this.activatedCells) {
const [x, y] = cellKey.split(",").map(Number)
this.frameBuffer.drawText("█", x, y, this.ACTIVATED_COLOR, this.BACKGROUND_COLOR)
}
const recentTrails = Array.from(this.trailCells.values())
.filter((cell) => currentTime - cell.timestamp < 100)
.sort((a, b) => b.timestamp - a.timestamp)
if (recentTrails.length > 0) {
const latest = recentTrails[0]
this.frameBuffer.setCellWithAlphaBlending(latest.x, latest.y, "+", this.CURSOR_COLOR, this.BACKGROUND_COLOR)
}
super.renderSelf(buffer)
}
protected onMouseEvent(event: MouseEvent): void {
if (event.propagationStopped) return
const cellKey = `${event.x},${event.y}`
switch (event.type) {
case "move":
this.trailCells.set(cellKey, {
x: event.x,
y: event.y,
timestamp: Date.now(),
isDrag: false,
})
this.requestRender()
break
case "drag":
this.trailCells.set(cellKey, {
x: event.x,
y: event.y,
timestamp: Date.now(),
isDrag: true,
})
this.requestRender()
break
case "down":
if (this.activatedCells.has(cellKey)) {
this.activatedCells.delete(cellKey)
} else {
this.activatedCells.add(cellKey)
}
this.requestRender()
break
}
}
public clearState(): void {
this.trailCells.clear()
this.activatedCells.clear()
}
}
export function run(renderer: CliRenderer): void {
renderer.start()
const backgroundColor = RGBA.fromInts(15, 15, 35, 255)
renderer.setBackgroundColor(backgroundColor)
engine.attach(renderer)
const mainGroup = new BoxRenderable(renderer, {
id: "mouse-demo-main-group",
zIndex: 10,
})
renderer.root.add(mainGroup)
titleText = new TextRenderable(renderer, {
id: "mouse_demo_title",
content: "Mouse Interaction Demo with Draggable Objects",
width: "100%",
position: "absolute",
left: 2,
top: 1,
fg: RGBA.fromInts(72, 209, 204),
attributes: TextAttributes.BOLD,
zIndex: 1000,
})
mainGroup.add(titleText)
instructionsText = new TextRenderable(renderer, {
id: "mouse_demo_instructions",
content: t`Drag boxes around • Move mouse: turquoise trails
Hold + move: orange drag trails • Click cells: toggle pink
Scroll on boxes: shows direction • Escape: menu`,
position: "absolute",
left: 2,
top: 2,
width: renderer.width - 4,
height: 3,
fg: RGBA.fromInts(176, 196, 222),
zIndex: 1000,
})
mainGroup.add(instructionsText)
demoContainer = new MouseInteractionFrameBuffer("mouse-demo-buffer", renderer)
mainGroup.add(demoContainer)
draggableBoxes = [
DraggableBox({
id: "drag-box-1",
x: 10,
y: 8,
width: 20,
height: 10,
color: RGBA.fromInts(200, 100, 150),
label: "Box 1",
}),
DraggableBox({
id: "drag-box-2",
x: 30,
y: 12,
width: 18,
height: 10,
color: RGBA.fromInts(100, 200, 150),
label: "Box 2",
}),
DraggableBox({
id: "drag-box-3",
x: 50,
y: 15,
width: 20,
height: 11,
color: RGBA.fromInts(150, 150, 200),
label: "Box 3",
}),
DraggableBox(
{
id: "drag-box-4",
x: 15,
y: 20,
width: 18,
height: 11,
color: RGBA.fromInts(200, 200, 100),
label: "O hidden",
overflow: "hidden",
},
Text({
id: "overflow-hidden-box",
content: "This should be cut off to the right",
width: 25,
height: 25,
onMouse: (event: MouseEvent) => {
console.log("mouse", event.type)
},
}),
),
]
for (const box of draggableBoxes) {
mainGroup.add(box)
}
}
export function destroy(renderer: CliRenderer): void {
renderer.clearFrameCallbacks()
renderer.root.getRenderable("mouse-demo-main-group")?.destroyRecursively()
}
if (import.meta.main) {
const renderer = await createCliRenderer({
exitOnCtrlC: true,
})
run(renderer)
setupCommonDemoKeys(renderer)
}