Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export EffectDesc and related types to users #331

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading