textmode.js 0.2.0 → 0.2.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ import type { Mixin } from './TextmodifierMixin';
2
+ import type { KeyboardEventHandler } from '../keyboard';
3
+ /**
4
+ * Capabilities provided by the KeyboardMixin
5
+ */
6
+ export interface KeyboardCapabilities {
7
+ /** Check if a specific key is currently pressed */
8
+ isKeyPressed(key: string): boolean;
9
+ /** Get the last key that was pressed */
10
+ readonly lastKeyPressed: string | null;
11
+ /** Get the last key that was released */
12
+ readonly lastKeyReleased: string | null;
13
+ /** Get all currently pressed keys */
14
+ readonly pressedKeys: string[];
15
+ /** Get current modifier key states */
16
+ readonly modifierState: {
17
+ ctrl: boolean;
18
+ shift: boolean;
19
+ alt: boolean;
20
+ meta: boolean;
21
+ };
22
+ /** Set a callback for when a key is pressed down */
23
+ keyPressed(callback: KeyboardEventHandler): void;
24
+ /** Set a callback for when a key is released */
25
+ keyReleased(callback: KeyboardEventHandler): void;
26
+ /** Clear all key states */
27
+ clearKeyStates(): void;
28
+ }
29
+ /**
30
+ * Mixin that adds keyboard interaction capabilities to Textmodifier.
31
+ *
32
+ * This is a thin wrapper around KeyboardManager that provides the public API
33
+ * for keyboard interaction. All the actual implementation is handled by the
34
+ * KeyboardManager instance in the TextmodifierContext.
35
+ *
36
+ * Provides p5.js-like keyboard functionality including key state tracking,
37
+ * event callbacks, and special key handling.
38
+ */
39
+ export declare const KeyboardMixin: Mixin<KeyboardCapabilities>;
@@ -0,0 +1,27 @@
1
+ import type { Mixin } from './TextmodifierMixin';
2
+ import type { MousePosition, MouseEventHandler } from '../mouse';
3
+ /**
4
+ * Capabilities provided by the MouseMixin
5
+ */
6
+ export interface MouseCapabilities {
7
+ /** Get the current mouse position in grid coordinates */
8
+ readonly mouse: MousePosition;
9
+ /** Set a callback for when the mouse is clicked */
10
+ mouseClicked(callback: MouseEventHandler): void;
11
+ /** Set a callback for when the mouse is pressed down */
12
+ mousePressed(callback: MouseEventHandler): void;
13
+ /** Set a callback for when the mouse is released */
14
+ mouseReleased(callback: MouseEventHandler): void;
15
+ /** Set a callback for when the mouse moves */
16
+ mouseMoved(callback: MouseEventHandler): void;
17
+ /** Set a callback for when the mouse wheel is scrolled */
18
+ mouseScrolled(callback: MouseEventHandler): void;
19
+ }
20
+ /**
21
+ * Mixin that adds mouse tracking capabilities to Textmodifier.
22
+ *
23
+ * This is a thin wrapper around MouseManager that provides the public API
24
+ * for mouse interaction. All the actual implementation is handled by the
25
+ * MouseManager instance in the TextmodifierContext.
26
+ */
27
+ export declare const MouseMixin: Mixin<MouseCapabilities>;
@@ -304,6 +304,25 @@ export interface RenderingCapabilities {
304
304
  * ```
305
305
  */
306
306
  rect(x: number, y: number, width?: number, height?: number): void;
307
+ /**
308
+ * Draw a single point at (x, y) with the current settings.
309
+ * @param x X-coordinate of the point
310
+ * @param y Y-coordinate of the point
311
+ *
312
+ * @example
313
+ * ```javascript
314
+ * const t = textmode.create({
315
+ * width: 800,
316
+ * height: 600,
317
+ * })
318
+ *
319
+ * t.draw(() => {
320
+ * t.background(0);
321
+ * t.point(10, 10);
322
+ * });
323
+ * ```
324
+ */
325
+ point(x: number, y: number): void;
307
326
  /**
308
327
  * Draw a line from point (x1, y1) to point (x2, y2) with the settings.
309
328
  * @param x1 X-coordinate of the line start point
@@ -7,6 +7,8 @@ import type { TextmodeCanvas } from '../Canvas';
7
7
  import type { TextmodeGrid } from '../Grid';
8
8
  import type { AnimationController } from '../AnimationController';
9
9
  import type { GLFramebuffer, Shader } from '../../rendering';
10
+ import type { MouseManager } from '../mouse';
11
+ import type { KeyboardManager } from '../keyboard';
10
12
  /**
11
13
  * Constructor type for mixin pattern
12
14
  */
@@ -30,6 +32,10 @@ export interface TextmodifierContext {
30
32
  readonly _grid: TextmodeGrid;
31
33
  /** Animation controller for managing rendering loop @ignore */
32
34
  readonly _animationController: AnimationController;
35
+ /** Mouse interaction manager @ignore */
36
+ readonly _mouseManager: MouseManager;
37
+ /** Keyboard interaction manager @ignore */
38
+ readonly _keyboardManager: KeyboardManager;
33
39
  /** Draw shader that contains content drawn by the user @ignore */
34
40
  readonly _textmodeDrawShader: Shader;
35
41
  /** Framebuffer used for offscreen rendering @ignore */
@@ -3,3 +3,7 @@ export { RenderingMixin, type RenderingCapabilities } from './RenderingMixin';
3
3
  export { ExportMixin, type ExportCapabilities } from './ExportMixin';
4
4
  export { FontMixin, type FontCapabilities } from './FontMixin';
5
5
  export { AnimationMixin, type AnimationCapabilities } from './AnimationMixin';
6
+ export { MouseMixin, type MouseCapabilities } from './MouseMixin';
7
+ export { KeyboardMixin, type KeyboardCapabilities } from './KeyboardMixin';
8
+ export type { MousePosition, MouseEventData, MouseEventHandler } from '../mouse';
9
+ export type { KeyboardEventData, KeyboardEventHandler, KeyState } from '../keyboard';
@@ -0,0 +1,130 @@
1
+ import type { TextmodeCanvas } from '../Canvas';
2
+ import type { TextmodeGrid } from '../Grid';
3
+ /**
4
+ * Mouse coordinates in grid space
5
+ */
6
+ export interface MousePosition {
7
+ /** Grid X coordinate (column), -1 if mouse is outside grid */
8
+ x: number;
9
+ /** Grid Y coordinate (row), -1 if mouse is outside grid */
10
+ y: number;
11
+ }
12
+ /**
13
+ * Mouse event data passed to event handlers
14
+ */
15
+ export interface MouseEventData {
16
+ /** Current mouse position in grid coordinates */
17
+ position: MousePosition;
18
+ /** Previous mouse position in grid coordinates */
19
+ previousPosition: MousePosition;
20
+ /** Mouse button that triggered the event (for click events) */
21
+ button?: number;
22
+ /** Scroll delta for wheel events */
23
+ delta?: {
24
+ x: number;
25
+ y: number;
26
+ };
27
+ /** Original DOM event */
28
+ originalEvent: MouseEvent | WheelEvent;
29
+ }
30
+ /**
31
+ * Mouse event handler function type
32
+ */
33
+ export type MouseEventHandler = (data: MouseEventData) => void;
34
+ /**
35
+ * Manages all mouse interaction for a Textmodifier instance.
36
+ * Handles event listeners, coordinate conversion, and event dispatching.
37
+ */
38
+ export declare class MouseManager {
39
+ private _canvas;
40
+ private _grid;
41
+ private _mousePosition;
42
+ private _previousMousePosition;
43
+ private _lastClientCoordinates;
44
+ private _mouseMoveListener;
45
+ private _mouseLeaveListener;
46
+ private _mouseDownListener;
47
+ private _mouseUpListener;
48
+ private _clickListener;
49
+ private _wheelListener;
50
+ private _areListenersSetup;
51
+ private _mouseClickedCallback?;
52
+ private _mousePressedCallback?;
53
+ private _mouseReleasedCallback?;
54
+ private _mouseMovedCallback?;
55
+ private _mouseScrolledCallback?;
56
+ constructor(canvas: TextmodeCanvas);
57
+ /**
58
+ * Update the grid reference (useful when grid changes after font loading)
59
+ */
60
+ $initialize(grid: TextmodeGrid): void;
61
+ /**
62
+ * Setup mouse event listeners on the canvas.
63
+ */
64
+ $setupListeners(): void;
65
+ /**
66
+ * Remove mouse event listeners from the canvas.
67
+ */
68
+ $cleanupListeners(): void;
69
+ /**
70
+ * Force an immediate update of the mouse position.
71
+ * This is useful when grid dimensions change (font size, window resize, etc.)
72
+ * and we need to recalculate the mouse coordinates without waiting for a mouse event.
73
+ */
74
+ $updatePosition(): void;
75
+ /**
76
+ * Set a callback function that will be called when the mouse is clicked.
77
+ * @param callback The function to call when the mouse is clicked
78
+ */
79
+ $setClickedCallback(callback: MouseEventHandler): void;
80
+ /**
81
+ * Set a callback function that will be called when the mouse is pressed down.
82
+ * @param callback The function to call when the mouse is pressed
83
+ */
84
+ $setPressedCallback(callback: MouseEventHandler): void;
85
+ /**
86
+ * Set a callback function that will be called when the mouse is released.
87
+ * @param callback The function to call when the mouse is released
88
+ */
89
+ $setReleasedCallback(callback: MouseEventHandler): void;
90
+ /**
91
+ * Set a callback function that will be called when the mouse moves.
92
+ * @param callback The function to call when the mouse moves
93
+ */
94
+ $setMovedCallback(callback: MouseEventHandler): void;
95
+ /**
96
+ * Set a callback function that will be called when the mouse wheel is scrolled.
97
+ * @param callback The function to call when the mouse wheel is scrolled
98
+ */
99
+ $setScrolledCallback(callback: MouseEventHandler): void;
100
+ /**
101
+ * Get the current mouse position in grid coordinates.
102
+ * Returns a copy to prevent external modification.
103
+ */
104
+ $getPosition(): MousePosition;
105
+ /**
106
+ * Handle mouse moved events
107
+ */
108
+ private _handleMouseMoved;
109
+ /**
110
+ * Handle mouse pressed events
111
+ */
112
+ private _handleMousePressed;
113
+ /**
114
+ * Handle mouse released events
115
+ */
116
+ private _handleMouseReleased;
117
+ /**
118
+ * Handle mouse clicked events
119
+ */
120
+ private _handleMouseClicked;
121
+ /**
122
+ * Handle mouse scrolled events
123
+ */
124
+ private _handleMouseScrolled;
125
+ /**
126
+ * Update mouse position based on mouse event.
127
+ * Converts pixel coordinates to grid coordinates.
128
+ */
129
+ private _updateMousePosition;
130
+ }
@@ -0,0 +1,2 @@
1
+ export { MouseManager } from './MouseManager';
2
+ export type { MousePosition, MouseEventData, MouseEventHandler } from './MouseManager';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "textmode.js",
3
- "version": "0.2.0",
3
+ "version": "0.2.1-beta.1",
4
4
  "description": "textmode.js is a lightweight creative coding library for creating real-time ASCII art on the web.",
5
5
  "type": "module",
6
6
  "types": "./dist/types/index.d.ts",