Skip to content

Commit

Permalink
Export EffectDesc and related types to users
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-weindel committed Jul 8, 2024
1 parent 316532d commit a418b9f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 62 deletions.
61 changes: 2 additions & 59 deletions src/core/renderers/webgl/shaders/DynamicShader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { ExtractProps } from '../../../CoreTextureManager.js';
import type { WebGlCoreRenderer } from '../WebGlCoreRenderer.js';
import {
WebGlCoreShader,
Expand All @@ -27,70 +26,14 @@ import type { UniformInfo } from '../internal/ShaderUtils.js';
import type { WebGlCoreCtxTexture } from '../WebGlCoreCtxTexture.js';
import {
ShaderEffect,
type EffectDescUnion,
type ShaderEffectUniform,
type ShaderEffectValueMap,
type BaseEffectDesc,
} from './effects/ShaderEffect.js';
import type { EffectMap } from '../../../CoreShaderManager.js';
import { assertTruthy } from '../../../../utils.js';

export interface BaseEffectDesc {
name?: string;
type: keyof EffectMap;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
props: Record<string, any>;
}

export interface EffectDesc<
T extends { name?: string; type: keyof EffectMap } = {
name?: string;
type: keyof EffectMap;
},
> extends BaseEffectDesc {
name?: T['name'];
type: T['type'];
props: ExtractProps<EffectMap[T['type']]>;
}

/**
* Allows the `keyof EffectMap` to be mapped over and form an discriminated
* union of all the EffectDescs structures individually.
*
* @remarks
* When used like the following:
* ```
* MapEffectDescs<keyof EffectMap>[]
* ```
* The resultant type will be a discriminated union like so:
* ```
* (
* {
* name: 'effect1',
* type: 'radius',
* props?: {
* radius?: number | number[];
* }
* } |
* {
* name: 'effect2',
* type: 'border',
* props?: {
* width?: number;
* color?: number;
* }
* } |
* // ...
* )[]
* ```
* Which means TypeScript will now base its type checking on the `type` field
* and will know exactly what the `props` field should be based on the `type`
* field.
*/
type MapEffectDescs<T extends keyof EffectMap> = T extends keyof EffectMap
? EffectDesc<{ type: T; name: string }>
: never;

export type EffectDescUnion = MapEffectDescs<keyof EffectMap>;

export interface DynamicShaderProps
extends DimensionsShaderProp,
AlphaShaderProp {
Expand Down
60 changes: 60 additions & 0 deletions src/core/renderers/webgl/shaders/effects/ShaderEffect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { EffectMap } from '../../../../CoreShaderManager.js';
import type { ExtractProps } from '../../../../CoreTextureManager.js';
import type {
AlphaShaderProp,
DimensionsShaderProp,
Expand All @@ -7,6 +9,64 @@ import type {
UniformMethodMap,
} from '../../internal/ShaderUtils.js';

export interface BaseEffectDesc {
name?: string;
type: keyof EffectMap;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
props: Record<string, any>;
}

export interface EffectDesc<
T extends { name?: string; type: keyof EffectMap } = {
name?: string;
type: keyof EffectMap;
},
> extends BaseEffectDesc {
name?: T['name'];
type: T['type'];
props: ExtractProps<EffectMap[T['type']]>;
}

/**
* Allows the `keyof EffectMap` to be mapped over and form an discriminated
* union of all the EffectDescs structures individually.
*
* @remarks
* When used like the following:
* ```
* MapEffectDescs<keyof EffectMap>[]
* ```
* The resultant type will be a discriminated union like so:
* ```
* (
* {
* name: 'effect1',
* type: 'radius',
* props?: {
* radius?: number | number[];
* }
* } |
* {
* name: 'effect2',
* type: 'border',
* props?: {
* width?: number;
* color?: number;
* }
* } |
* // ...
* )[]
* ```
* Which means TypeScript will now base its type checking on the `type` field
* and will know exactly what the `props` field should be based on the `type`
* field.
*/
type MapEffectDescs<T extends keyof EffectMap> = T extends keyof EffectMap
? EffectDesc<{ type: T; name: string }>
: never;

export type EffectDescUnion = MapEffectDescs<keyof EffectMap>;

export interface ShaderEffectUniform {
value: number | number[] | boolean | string;
type: string;
Expand Down
2 changes: 1 addition & 1 deletion src/main-api/DynamicShaderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ShaderMap,
} from '../core/CoreShaderManager.js';
import type { ExtractProps } from '../core/CoreTextureManager.js';
import type { EffectDesc } from '../core/renderers/webgl/shaders/DynamicShader.js';
import type { EffectDesc } from '../core/renderers/webgl/shaders/effects/ShaderEffect.js';
import type { BaseShaderController } from './ShaderController.js';

type OptionalName<T> = T extends string ? T : never;
Expand Down
4 changes: 2 additions & 2 deletions src/main-api/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { Inspector } from './Inspector.js';
import { assertTruthy, isProductionEnvironment } from '../utils.js';
import { Stage } from '../core/Stage.js';
import { CoreNode, type CoreNodeProps } from '../core/CoreNode.js';
import { CoreTextNode, type CoreTextNodeProps } from '../core/CoreTextNode.js';
import { type CoreTextNodeProps } from '../core/CoreTextNode.js';
import type {
BaseShaderController,
ShaderController,
Expand All @@ -42,7 +42,7 @@ import type {
import type {
EffectDesc,
EffectDescUnion,
} from '../core/renderers/webgl/shaders/DynamicShader.js';
} from '../core/renderers/webgl/shaders/effects/ShaderEffect.js';
import type { TextureMemoryManagerSettings } from '../core/TextureMemoryManager.js';

/**
Expand Down

0 comments on commit a418b9f

Please sign in to comment.