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 65fccdc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 59 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

0 comments on commit 65fccdc

Please sign in to comment.