textmode.js 0.2.0 → 0.2.1-beta.2
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.
- package/dist/textmode.esm.js +1566 -1294
- package/dist/textmode.esm.min.js +1662 -1390
- package/dist/textmode.umd.js +10 -26
- package/dist/textmode.umd.min.js +9 -25
- package/dist/types/assets/shaders-minified/frag/ascii.d.ts +14 -0
- package/dist/types/assets/shaders-minified/frag/copy-mrt.d.ts +14 -0
- package/dist/types/assets/shaders-minified/frag/instanced-ascii-mrt.d.ts +14 -0
- package/dist/types/assets/shaders-minified/index.d.ts +5 -0
- package/dist/types/assets/shaders-minified/vert/instanced-ascii-mrt.d.ts +14 -0
- package/dist/types/assets/shaders-minified/vert/shader.d.ts +14 -0
- package/dist/types/export/svg/SVGContentGenerator.d.ts +0 -6
- package/dist/types/index.d.ts +2 -0
- package/dist/types/rendering/index.d.ts +2 -0
- package/dist/types/rendering/webgl/Framebuffer.d.ts +8 -8
- package/dist/types/rendering/webgl/InstanceData.d.ts +1 -1
- package/dist/types/rendering/webgl/RenderPipeline.d.ts +10 -3
- package/dist/types/rendering/webgl/RenderState.d.ts +11 -11
- package/dist/types/rendering/webgl/Renderer.d.ts +26 -2
- package/dist/types/rendering/webgl/Shader.d.ts +6 -7
- package/dist/types/rendering/webgl/ShaderManager.d.ts +65 -1
- package/dist/types/rendering/webgl/index.d.ts +1 -0
- package/dist/types/rendering/webgl/types/GeometryTypes.d.ts +7 -7
- package/dist/types/rendering/webgl/types/ShaderTypes.d.ts +35 -0
- package/dist/types/textmode/Textmodifier.d.ts +8 -2
- package/dist/types/textmode/managers/KeyboardManager.d.ts +123 -0
- package/dist/types/textmode/managers/MouseManager.d.ts +130 -0
- package/dist/types/textmode/managers/index.d.ts +4 -0
- package/dist/types/textmode/mixins/KeyboardMixin.d.ts +39 -0
- package/dist/types/textmode/mixins/MouseMixin.d.ts +27 -0
- package/dist/types/textmode/mixins/RenderingMixin.d.ts +104 -0
- package/dist/types/textmode/mixins/TextmodifierMixin.d.ts +6 -0
- package/dist/types/textmode/mixins/index.d.ts +4 -0
- package/package.json +3 -2
|
@@ -2,6 +2,10 @@ import type { IGeometry } from './types/GeometryTypes';
|
|
|
2
2
|
import { GeometryType } from './types/GeometryTypes';
|
|
3
3
|
import type { RenderContext } from './types/RenderTypes';
|
|
4
4
|
import type { DrawCommand } from './types/DrawCommand';
|
|
5
|
+
import { GLShader } from './Shader';
|
|
6
|
+
interface IShaderProvider {
|
|
7
|
+
$getCopyShader(): GLShader;
|
|
8
|
+
}
|
|
5
9
|
/**
|
|
6
10
|
* Execute draw commands exactly in the order they were enqueued while
|
|
7
11
|
* still batching consecutive commands of the same geometry type to minimize
|
|
@@ -10,13 +14,16 @@ import type { DrawCommand } from './types/DrawCommand';
|
|
|
10
14
|
export declare class RenderPipeline {
|
|
11
15
|
private readonly _vaoMgr;
|
|
12
16
|
private readonly _gl;
|
|
13
|
-
private
|
|
17
|
+
private readonly _renderer;
|
|
14
18
|
private _tempRectFBO;
|
|
15
19
|
private _tempRectFBOSize;
|
|
16
|
-
constructor(gl: WebGL2RenderingContext);
|
|
20
|
+
constructor(gl: WebGL2RenderingContext, renderer: IShaderProvider);
|
|
17
21
|
$execute(context: RenderContext, commands: Iterable<DrawCommand>, geometries: Map<GeometryType, IGeometry>): void;
|
|
18
|
-
/**
|
|
22
|
+
/** Execute a custom-shaded rectangle using provided shader/uniforms */
|
|
19
23
|
private _drawCustomRect;
|
|
24
|
+
/** Draw a rectangle with the specified shader, uniforms, and viewport handling */
|
|
25
|
+
private _drawRectangleWithShader;
|
|
20
26
|
private _getCopyShader;
|
|
21
27
|
private _getTempRectFBO;
|
|
22
28
|
}
|
|
29
|
+
export {};
|
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
* Represents a snapshot of the current rendering state
|
|
3
3
|
*/
|
|
4
4
|
export interface IRenderState {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
_lineWeight: number;
|
|
6
|
+
_rotationX: number;
|
|
7
|
+
_rotationY: number;
|
|
8
|
+
_rotationZ: number;
|
|
9
|
+
_character: [number, number, number];
|
|
10
|
+
_charColor: [number, number, number, number];
|
|
11
|
+
_cellColor: [number, number, number, number];
|
|
12
|
+
_flipHorizontally: boolean;
|
|
13
|
+
_flipVertically: boolean;
|
|
14
|
+
_invert: boolean;
|
|
15
|
+
_charRotation: [number, number];
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Manages rendering state and provides push/pop functionality for state management
|
|
@@ -2,12 +2,14 @@ import { GLFramebuffer } from "./Framebuffer";
|
|
|
2
2
|
import type { FramebufferOptions } from '../webgl/Framebuffer';
|
|
3
3
|
import { GLShader } from "./Shader";
|
|
4
4
|
import { RenderState } from "./RenderState";
|
|
5
|
+
import type { ShaderSource } from './types/ShaderTypes';
|
|
5
6
|
/**
|
|
6
7
|
* Core WebGL renderer that manages the WebGL context and provides high-level rendering operations
|
|
7
8
|
*/
|
|
8
9
|
export declare class GLRenderer {
|
|
9
10
|
private _gl;
|
|
10
11
|
private _currentShader;
|
|
12
|
+
private _shaderManager;
|
|
11
13
|
private _userShader;
|
|
12
14
|
private _userUniforms;
|
|
13
15
|
private _ndcQuadBuffer;
|
|
@@ -15,6 +17,7 @@ export declare class GLRenderer {
|
|
|
15
17
|
private readonly _renderPipeline;
|
|
16
18
|
private readonly _drawQueue;
|
|
17
19
|
private _renderState;
|
|
20
|
+
private _stateStack;
|
|
18
21
|
constructor(gl: WebGL2RenderingContext);
|
|
19
22
|
/** Get or create a geometry instance */
|
|
20
23
|
private _getGeometry;
|
|
@@ -22,7 +25,19 @@ export declare class GLRenderer {
|
|
|
22
25
|
* Set the current shader
|
|
23
26
|
*/
|
|
24
27
|
$shader(shader: GLShader): void;
|
|
25
|
-
$createShader(vertexSource:
|
|
28
|
+
$createShader(vertexSource: ShaderSource, fragmentSource: ShaderSource): GLShader;
|
|
29
|
+
/**
|
|
30
|
+
* Get the shared MRT copy shader
|
|
31
|
+
*/
|
|
32
|
+
$getCopyShader(): GLShader;
|
|
33
|
+
/**
|
|
34
|
+
* Get the main MRT draw shader
|
|
35
|
+
*/
|
|
36
|
+
$getMainDrawShader(): GLShader;
|
|
37
|
+
/**
|
|
38
|
+
* Get the ASCII conversion shader
|
|
39
|
+
*/
|
|
40
|
+
$getConversionShader(): GLShader;
|
|
26
41
|
/**
|
|
27
42
|
* Set a custom user shader for subsequent rendering operations
|
|
28
43
|
*/
|
|
@@ -30,7 +45,7 @@ export declare class GLRenderer {
|
|
|
30
45
|
/**
|
|
31
46
|
* Set a uniform value for the current user shader
|
|
32
47
|
*/
|
|
33
|
-
$
|
|
48
|
+
$setUniform(name: string, value: any): void;
|
|
34
49
|
/**
|
|
35
50
|
* Set multiple uniform values for the current user shader
|
|
36
51
|
*/
|
|
@@ -39,6 +54,11 @@ export declare class GLRenderer {
|
|
|
39
54
|
* Create a filter shader using the standard instanced vertex shader
|
|
40
55
|
*/
|
|
41
56
|
$createFilterShader(fragmentSource: string): GLShader;
|
|
57
|
+
/**
|
|
58
|
+
* Enqueue an image blit from a source MRT framebuffer to the current render target.
|
|
59
|
+
* This uses the internal copy shader and preserves draw ordering within the pipeline.
|
|
60
|
+
*/
|
|
61
|
+
$imageRect(src: GLFramebuffer, x: number, y: number, width: number, height: number): void;
|
|
42
62
|
/**
|
|
43
63
|
* Draw a quad covering the pixel rectangle (x, y, width, height) on the canvas.
|
|
44
64
|
* The quad is converted to NDC and rendered with the current shader using only a_position.
|
|
@@ -109,6 +129,10 @@ export declare class GLRenderer {
|
|
|
109
129
|
*/
|
|
110
130
|
get context(): WebGLRenderingContext;
|
|
111
131
|
get state(): RenderState;
|
|
132
|
+
/** Internal: push a specific render state as current (used by user FBO begin) */
|
|
133
|
+
$pushState(state: RenderState): void;
|
|
134
|
+
/** Internal: pop the render state after user FBO end */
|
|
135
|
+
$popState(): void;
|
|
112
136
|
/**
|
|
113
137
|
* Flush all batched instances for instanced rendering.
|
|
114
138
|
* This must be called at the end of each frame to actually render the batched geometry.
|
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
import { GLFramebuffer } from './Framebuffer';
|
|
2
|
+
import type { ShaderSource } from './types/ShaderTypes';
|
|
2
3
|
/**
|
|
3
4
|
* Supported uniform value types
|
|
4
5
|
*/
|
|
5
6
|
type UniformValue = number | boolean | number[] | Float32Array | Int32Array | GLFramebuffer | WebGLTexture;
|
|
6
7
|
/**
|
|
7
8
|
* WebGL implementation of the shader abstraction.
|
|
8
|
-
* Provides GPU shader program management with automatic uniform handling.
|
|
9
|
+
* Provides GPU shader program management with automatic uniform handling and minification support.
|
|
9
10
|
*/
|
|
10
11
|
export declare class GLShader {
|
|
11
12
|
private _gl;
|
|
12
13
|
private _program;
|
|
13
14
|
private _uniformLocations;
|
|
14
|
-
private _uniformTypes;
|
|
15
15
|
private _textureUnitCounter;
|
|
16
|
-
private _maxTextureUnits;
|
|
17
16
|
/**
|
|
18
17
|
* Creates a new GLShader instance.
|
|
19
18
|
* @param gl The WebGL rendering context.
|
|
20
|
-
* @param vertexSource The source code for the vertex shader.
|
|
21
|
-
* @param fragmentSource The source code for the fragment shader.
|
|
19
|
+
* @param vertexSource The source code for the vertex shader (or ShaderData object).
|
|
20
|
+
* @param fragmentSource The source code for the fragment shader (or ShaderData object).
|
|
22
21
|
* @ignore
|
|
23
22
|
*/
|
|
24
|
-
constructor(gl: WebGLRenderingContext, vertexSource:
|
|
23
|
+
constructor(gl: WebGLRenderingContext, vertexSource: ShaderSource, fragmentSource: ShaderSource);
|
|
25
24
|
private _cacheLocations;
|
|
26
25
|
private _createProgram;
|
|
27
26
|
private _createShader;
|
|
@@ -39,7 +38,7 @@ export declare class GLShader {
|
|
|
39
38
|
*/
|
|
40
39
|
$hasUniform(name: string): boolean;
|
|
41
40
|
/**
|
|
42
|
-
* Set a single uniform value with automatic texture unit management
|
|
41
|
+
* Set a single uniform value with automatic texture unit management and minification support
|
|
43
42
|
*/
|
|
44
43
|
$setUniform(name: string, value: UniformValue): void;
|
|
45
44
|
private _getNextTextureUnit;
|
|
@@ -1 +1,65 @@
|
|
|
1
|
-
|
|
1
|
+
import { GLShader } from './Shader';
|
|
2
|
+
import type { ShaderSource } from './types/ShaderTypes';
|
|
3
|
+
/**
|
|
4
|
+
* Registry keys for built-in shaders
|
|
5
|
+
*/
|
|
6
|
+
export declare enum BuiltInShader {
|
|
7
|
+
MRT_DRAW = "mrt-draw",
|
|
8
|
+
MRT_COPY = "mrt-copy",
|
|
9
|
+
ASCII_CONVERSION = "ascii-conversion"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Centralized shader management system for textmode.js
|
|
13
|
+
*
|
|
14
|
+
* Provides a registry pattern for managing shared shaders, eliminating
|
|
15
|
+
* duplication and providing a single source of truth for shader lifecycle.
|
|
16
|
+
*
|
|
17
|
+
* Key benefits:
|
|
18
|
+
* - Eliminates shader duplication (especially copy shader)
|
|
19
|
+
* - Centralizes vertex shader imports
|
|
20
|
+
* - Provides lazy initialization
|
|
21
|
+
* - Supports future extensibility (shader variants, hot-reloading)
|
|
22
|
+
* - Consistent resource management
|
|
23
|
+
*/
|
|
24
|
+
export declare class ShaderManager {
|
|
25
|
+
private _shaders;
|
|
26
|
+
private _gl;
|
|
27
|
+
constructor(gl: WebGL2RenderingContext);
|
|
28
|
+
/**
|
|
29
|
+
* Get or create a shader with lazy initialization
|
|
30
|
+
* @param key Unique identifier for the shader
|
|
31
|
+
* @param factory Function that creates the shader if it doesn't exist
|
|
32
|
+
* @returns The shader instance
|
|
33
|
+
*/
|
|
34
|
+
$getOrCreateShader(key: string, factory: () => GLShader): GLShader;
|
|
35
|
+
/**
|
|
36
|
+
* Get the shared MRT copy shader used for framebuffer compositing
|
|
37
|
+
* This shader handles copying 5-attachment MRT data with proper transparency
|
|
38
|
+
*/
|
|
39
|
+
$getCopyShader(): GLShader;
|
|
40
|
+
/**
|
|
41
|
+
* Get the main MRT draw shader used for standard geometry rendering
|
|
42
|
+
* This shader outputs to all 5 MRT attachments for textmode rendering
|
|
43
|
+
*/
|
|
44
|
+
$getMainDrawShader(): GLShader;
|
|
45
|
+
/**
|
|
46
|
+
* Get the ASCII conversion shader used for final display output
|
|
47
|
+
* This shader converts MRT data to final ASCII characters
|
|
48
|
+
*/
|
|
49
|
+
$getConversionShader(): GLShader;
|
|
50
|
+
/**
|
|
51
|
+
* Create a custom filter shader using the standard instanced vertex shader
|
|
52
|
+
* These shaders are not cached as they are user-specific
|
|
53
|
+
*/
|
|
54
|
+
$createFilterShader(fragmentSource: string): GLShader;
|
|
55
|
+
/**
|
|
56
|
+
* Create a custom shader with arbitrary vertex and fragment sources
|
|
57
|
+
* These shaders are not cached as they are user-specific
|
|
58
|
+
*/
|
|
59
|
+
$createCustomShader(vertexSource: ShaderSource, fragmentSource: ShaderSource): GLShader;
|
|
60
|
+
/**
|
|
61
|
+
* Dispose of all managed shaders and clear the registry
|
|
62
|
+
* This method is idempotent and safe to call multiple times
|
|
63
|
+
*/
|
|
64
|
+
$dispose(): void;
|
|
65
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './Framebuffer';
|
|
2
2
|
export * from './Renderer';
|
|
3
3
|
export * from './Shader';
|
|
4
|
+
export * from './ShaderManager';
|
|
4
5
|
export * from './StateCache';
|
|
5
6
|
export { GeometryType } from './types/GeometryTypes';
|
|
6
7
|
export type { IGeometry as IInstancedGeometry, UnitGeometryData, RectangleParams, LineParams, EllipseParams, ArcParams, TriangleParams, BezierCurveParams, GeometryParams } from './types/GeometryTypes';
|
|
@@ -19,20 +19,20 @@ export declare enum GeometryType {
|
|
|
19
19
|
*/
|
|
20
20
|
export interface UnitGeometryData {
|
|
21
21
|
/** Vertex data as Float32Array (position + texCoord interleaved) */
|
|
22
|
-
|
|
22
|
+
_vertices: Float32Array;
|
|
23
23
|
/** Number of vertices in the geometry */
|
|
24
|
-
|
|
24
|
+
_vertexCount: number;
|
|
25
25
|
/** WebGL primitive type (gl.TRIANGLES, gl.LINES, etc.) */
|
|
26
|
-
|
|
26
|
+
_primitiveType: number;
|
|
27
27
|
/** Stride in bytes between vertices */
|
|
28
|
-
|
|
28
|
+
_stride: number;
|
|
29
29
|
/** Attribute configuration for position and texCoord */
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
_attributes: {
|
|
31
|
+
_position: {
|
|
32
32
|
size: number;
|
|
33
33
|
offset: number;
|
|
34
34
|
};
|
|
35
|
-
|
|
35
|
+
_texCoord: {
|
|
36
36
|
size: number;
|
|
37
37
|
offset: number;
|
|
38
38
|
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for shader data with minification support
|
|
3
|
+
*/
|
|
4
|
+
export interface ShaderData {
|
|
5
|
+
/** The shader source code (minified in production, original in development) */
|
|
6
|
+
sourceCode: string;
|
|
7
|
+
/** Original unminified source code (available in development builds) */
|
|
8
|
+
originalCode?: string;
|
|
9
|
+
/** Mapping of original uniform names to minified names and types */
|
|
10
|
+
uniforms: Record<string, {
|
|
11
|
+
/** Minified uniform name used in the shader */
|
|
12
|
+
variableName: string;
|
|
13
|
+
/** GLSL type of the uniform (vec2, sampler2D, etc.) */
|
|
14
|
+
variableType: string;
|
|
15
|
+
}>;
|
|
16
|
+
/** Mapping of const variables (if any) */
|
|
17
|
+
consts: Record<string, {
|
|
18
|
+
/** Substitution value for the const */
|
|
19
|
+
variableName: string;
|
|
20
|
+
/** GLSL type of the const */
|
|
21
|
+
variableType: string;
|
|
22
|
+
}>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Union type for shader imports - either ShaderData or plain string (for backward compatibility)
|
|
26
|
+
*/
|
|
27
|
+
export type ShaderSource = ShaderData | string;
|
|
28
|
+
/**
|
|
29
|
+
* Helper to normalize shader source to ShaderData format
|
|
30
|
+
*/
|
|
31
|
+
export declare function normalizeShaderData(source: ShaderSource): ShaderData;
|
|
32
|
+
/**
|
|
33
|
+
* Extract just the source code from ShaderSource
|
|
34
|
+
*/
|
|
35
|
+
export declare function getShaderCode(source: ShaderSource): string;
|
|
@@ -3,11 +3,15 @@ import { TextmodeFont } from './font';
|
|
|
3
3
|
import { TextmodeGrid } from './Grid';
|
|
4
4
|
import { TextmodeCanvas } from './Canvas';
|
|
5
5
|
import { AnimationController } from './AnimationController';
|
|
6
|
+
import { MouseManager } from './managers';
|
|
7
|
+
import { KeyboardManager } from './managers';
|
|
6
8
|
import { type TextmodifierContext } from './mixins';
|
|
7
9
|
import type { RenderingCapabilities } from './mixins/RenderingMixin';
|
|
8
10
|
import type { ExportCapabilities } from './mixins/ExportMixin';
|
|
9
11
|
import type { FontCapabilities } from './mixins/FontMixin';
|
|
10
12
|
import type { AnimationCapabilities } from './mixins/AnimationMixin';
|
|
13
|
+
import type { MouseCapabilities } from './mixins/MouseMixin';
|
|
14
|
+
import type { KeyboardCapabilities } from './mixins/KeyboardMixin';
|
|
11
15
|
import type { GLFramebuffer, Shader } from '../rendering';
|
|
12
16
|
/**
|
|
13
17
|
* Options for creating a {@link Textmodifier} instance.
|
|
@@ -41,6 +45,8 @@ declare class TextmodifierCore implements TextmodifierContext {
|
|
|
41
45
|
_canvas: TextmodeCanvas;
|
|
42
46
|
_grid: TextmodeGrid;
|
|
43
47
|
_animationController: AnimationController;
|
|
48
|
+
_mouseManager: MouseManager;
|
|
49
|
+
_keyboardManager: KeyboardManager;
|
|
44
50
|
_textmodeDrawShader: Shader;
|
|
45
51
|
_textmodeDrawFramebuffer: GLFramebuffer;
|
|
46
52
|
_textmodeConversionShader: Shader;
|
|
@@ -211,8 +217,8 @@ export declare class Textmodifier extends Textmodifier_base {
|
|
|
211
217
|
/** Check if the instance has been disposed/destroyed. */
|
|
212
218
|
get isDisposed(): boolean;
|
|
213
219
|
/** Get the draw framebuffer used for offscreen rendering. @ignore */
|
|
214
|
-
get drawFramebuffer():
|
|
220
|
+
get drawFramebuffer(): GLFramebuffer;
|
|
215
221
|
}
|
|
216
|
-
export interface Textmodifier extends RenderingCapabilities, ExportCapabilities, FontCapabilities, AnimationCapabilities {
|
|
222
|
+
export interface Textmodifier extends RenderingCapabilities, ExportCapabilities, FontCapabilities, AnimationCapabilities, MouseCapabilities, KeyboardCapabilities {
|
|
217
223
|
}
|
|
218
224
|
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key event data passed to event handlers
|
|
3
|
+
*/
|
|
4
|
+
export interface KeyboardEventData {
|
|
5
|
+
/** The key that was pressed/released (e.g., 'a', 'Enter', 'ArrowLeft') */
|
|
6
|
+
key: string;
|
|
7
|
+
/** The key code (for compatibility) */
|
|
8
|
+
keyCode: number;
|
|
9
|
+
/** Whether Ctrl key is held down */
|
|
10
|
+
ctrlKey: boolean;
|
|
11
|
+
/** Whether Shift key is held down */
|
|
12
|
+
shiftKey: boolean;
|
|
13
|
+
/** Whether Alt key is held down */
|
|
14
|
+
altKey: boolean;
|
|
15
|
+
/** Whether Meta key (Windows/Cmd) is held down */
|
|
16
|
+
metaKey: boolean;
|
|
17
|
+
/** Whether this key is currently being held down (for keyPressed) or was released (for keyReleased) */
|
|
18
|
+
isPressed: boolean;
|
|
19
|
+
/** Original DOM keyboard event */
|
|
20
|
+
originalEvent: KeyboardEvent;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Keyboard event handler function type
|
|
24
|
+
*/
|
|
25
|
+
export type KeyboardEventHandler = (data: KeyboardEventData) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Key state information
|
|
28
|
+
*/
|
|
29
|
+
export interface KeyState {
|
|
30
|
+
/** Whether the key is currently pressed */
|
|
31
|
+
isPressed: boolean;
|
|
32
|
+
/** Timestamp when the key was last pressed */
|
|
33
|
+
lastPressTime: number;
|
|
34
|
+
/** Timestamp when the key was last released */
|
|
35
|
+
lastReleaseTime: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Manages all keyboard interaction for a Textmodifier instance.
|
|
39
|
+
* Handles event listeners, key state tracking, and event dispatching.
|
|
40
|
+
*
|
|
41
|
+
* Provides p5.js-like keyboard functionality including:
|
|
42
|
+
* - keyPressed() and keyReleased() callbacks
|
|
43
|
+
* - Current key state tracking
|
|
44
|
+
* - Special key handling (arrows, function keys, etc.)
|
|
45
|
+
* - Modifier key support (Ctrl, Shift, Alt, Meta)
|
|
46
|
+
*/
|
|
47
|
+
export declare class KeyboardManager {
|
|
48
|
+
private _keyStates;
|
|
49
|
+
private _lastKeyPressed;
|
|
50
|
+
private _lastKeyReleased;
|
|
51
|
+
private _keyDownListener;
|
|
52
|
+
private _keyUpListener;
|
|
53
|
+
private _areListenersSetup;
|
|
54
|
+
private _keyPressedCallback?;
|
|
55
|
+
private _keyReleasedCallback?;
|
|
56
|
+
private readonly _specialKeyMap;
|
|
57
|
+
constructor();
|
|
58
|
+
/**
|
|
59
|
+
* Setup keyboard event listeners.
|
|
60
|
+
*/
|
|
61
|
+
$setupListeners(): void;
|
|
62
|
+
/**
|
|
63
|
+
* Remove keyboard event listeners.
|
|
64
|
+
*/
|
|
65
|
+
$cleanupListeners(): void;
|
|
66
|
+
/**
|
|
67
|
+
* Set a callback function that will be called when a key is pressed down.
|
|
68
|
+
* @param callback The function to call when a key is pressed
|
|
69
|
+
*/
|
|
70
|
+
$setPressedCallback(callback: KeyboardEventHandler): void;
|
|
71
|
+
/**
|
|
72
|
+
* Set a callback function that will be called when a key is released.
|
|
73
|
+
* @param callback The function to call when a key is released
|
|
74
|
+
*/
|
|
75
|
+
$setReleasedCallback(callback: KeyboardEventHandler): void;
|
|
76
|
+
/**
|
|
77
|
+
* Check if a specific key is currently being pressed.
|
|
78
|
+
* @param key The key to check (e.g., 'a', 'Enter', 'ArrowLeft', or special constants like 'SPACE')
|
|
79
|
+
* @returns true if the key is currently pressed, false otherwise
|
|
80
|
+
*/
|
|
81
|
+
$isKeyPressed(key: string): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Get the last key that was pressed.
|
|
84
|
+
* @returns The key string of the last pressed key, or null if no key has been pressed
|
|
85
|
+
*/
|
|
86
|
+
$getLastKeyPressed(): string | null;
|
|
87
|
+
/**
|
|
88
|
+
* Get the last key that was released.
|
|
89
|
+
* @returns The key string of the last released key, or null if no key has been released
|
|
90
|
+
*/
|
|
91
|
+
$getLastKeyReleased(): string | null;
|
|
92
|
+
/**
|
|
93
|
+
* Get all currently pressed keys.
|
|
94
|
+
* @returns Array of key strings that are currently pressed
|
|
95
|
+
*/
|
|
96
|
+
$getPressedKeys(): string[];
|
|
97
|
+
/**
|
|
98
|
+
* Check if any modifier key is currently pressed.
|
|
99
|
+
* @returns Object with boolean properties for each modifier key
|
|
100
|
+
*/
|
|
101
|
+
$getModifierState(): {
|
|
102
|
+
ctrl: boolean;
|
|
103
|
+
shift: boolean;
|
|
104
|
+
alt: boolean;
|
|
105
|
+
meta: boolean;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Clear all key states (useful for focus loss scenarios).
|
|
109
|
+
*/
|
|
110
|
+
$clearKeyStates(): void;
|
|
111
|
+
/**
|
|
112
|
+
* Handle key down events
|
|
113
|
+
*/
|
|
114
|
+
private _handleKeyDown;
|
|
115
|
+
/**
|
|
116
|
+
* Handle key up events
|
|
117
|
+
*/
|
|
118
|
+
private _handleKeyUp;
|
|
119
|
+
/**
|
|
120
|
+
* Normalize key names for consistency (map to p5.js-like constants where applicable)
|
|
121
|
+
*/
|
|
122
|
+
private _normalizeKey;
|
|
123
|
+
}
|
|
@@ -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,4 @@
|
|
|
1
|
+
export { KeyboardManager } from '../managers/KeyboardManager';
|
|
2
|
+
export type { KeyboardEventData, KeyboardEventHandler, KeyState } from '../managers/KeyboardManager';
|
|
3
|
+
export { MouseManager } from '../managers/MouseManager';
|
|
4
|
+
export type { MousePosition, MouseEventData, MouseEventHandler } from '../managers/MouseManager';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Mixin } from './TextmodifierMixin';
|
|
2
|
+
import type { KeyboardEventHandler } from '../managers';
|
|
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>;
|