/** * FF Typescript Foundation Library * Copyright 2019 Ralph Wiedemeier, Frame Factory GmbH * * License: MIT */ import { TypeOf, PropOf, enumToArray, Dictionary } from "@ff/core/types"; import * as dayjs from 'dayjs'; // enable dayjs() call (->'now') in Javascript: type TDayjsFactory = (date?: dayjs.ConfigType) => dayjs.Dayjs; const createDayjs = ((dayjs as unknown as { default?: TDayjsFactory }).default || dayjs as unknown as TDayjsFactory); //////////////////////////////////////////////////////////////////////////////// export interface IPropertyTemplate { path: string; schema: IPropertySchema; } export interface IPropertySchema { preset: T; min?: number; max?: number; step?: number; // increment/decrement step speed?: number; // steps per pixel precision?: number; bar?: boolean; percent?: boolean; options?: string[]; enum?: any; labels?: string[]; objectType?: TypeOf; multi?: boolean; event?: boolean; static?: boolean; // not linkable if true semantic?: string; } export type SchemaProps = Partial> | T; type Vector = T[]; type Matrix = T[]; export const labels = { xyzw: [ "X", "Y", "Z", "W" ], rgba: [ "R", "G", "B", "A" ], }; const parseProps = function(props: SchemaProps) { if (props === undefined || (typeof props === "object" && !Array.isArray(props))) { return props; } return { preset: props } as SchemaProps; }; export const makeType = function(schema: IPropertySchema, path: string, props: SchemaProps) { props = parseProps(props); return { path, schema: props ? Object.assign({}, schema, props) : schema } as IPropertyTemplate; }; export const makeEnumType = function(enumeration: T, path: string, props: SchemaProps>) { props = parseProps(props); const schema = { enum: enumeration, options: enumToArray(enumeration), preset: 0 as any as PropOf }; return { path, schema: props ? Object.assign({}, schema, props) : schema } as IPropertyTemplate>; }; export const makeOptionType = function(options: string[], path: string, props: SchemaProps) { props = parseProps(props); const schema = { options, preset: 0 }; return { path, schema: props ? Object.assign({}, schema, props) : schema } as IPropertyTemplate; }; export const makeObjectType = function(type: TypeOf, path: string, props: SchemaProps) { props = parseProps(props); const schema = { preset: null, objectType: type }; return { path, schema: props ? Object.assign({}, schema, props) : schema } as IPropertyTemplate; }; export const schemas: Dictionary = { Number: { preset: 0 }, Integer: { preset: 0, step: 1, speed: 0.34, precision: 0 }, Natural: { preset: 0, step: 1, speed: 0.34, precision: 0, min: 0 }, Unit: { preset: 0, min: 0, max: 1, bar: true }, Percent: { preset: 0, min: 0, max: 1, bar: true, percent: true }, Vector2: { preset: [0, 0] }, Vector3: { preset: [0, 0, 0] }, Vector4: { preset: [0, 0, 0, 0] }, Matrix2: { preset: [1, 0, 0, 1] }, Matrix3: { preset: [1, 0, 0, 0, 1, 0, 0, 0, 1] }, Matrix4: { preset: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] }, Scale: { preset: 1 }, Scale2: { preset: [1, 1] }, Scale3: { preset: [1, 1, 1] }, IntVec2: { preset: [0, 0], step: 1, speed: 0.34, precision: 0 }, IntVec3: { preset: [0, 0, 0], step: 1, speed: 0.34, precision: 0 }, ColorRGB: { preset: [1, 1, 1], semantic: "color", labels: labels.rgba, min: 0, max: 1, bar: true }, ColorRGBA: { preset: [1, 1, 1, 1], semantic: "color", labels: labels.rgba, min: 0, max: 1, bar: true }, Boolean: { preset: false }, String: { preset: "" }, DateTime: { preset: createDayjs(), semantic: "datetime" }, AssetPath: { preset: "", semantic: "asset-path" }, Object: { preset: null, objectType: Object }, Event: { preset: 0, event: true } }; export const types = { Property: (path: string, props?: SchemaProps) => makeType(undefined, path, props), Number: (path: string, props?: SchemaProps) => makeType(schemas.Number, path, props), Integer: (path: string, props?: SchemaProps) => makeType(schemas.Integer, path, props), Natural: (path: string, props?: SchemaProps) => makeType(schemas.Natural, path, props), Unit: (path: string, props?: SchemaProps) => makeType(schemas.Unit, path, props), Percent: (path: string, props?: SchemaProps) => makeType(schemas.Percent, path, props), Vector2: (path: string, props?: SchemaProps) => makeType(schemas.Vector2, path, props), Vector3: (path: string, props?: SchemaProps) => makeType(schemas.Vector3, path, props), Vector4: (path: string, props?: SchemaProps) => makeType(schemas.Vector4, path, props), IntVec2: (path: string, props?: SchemaProps) => makeType(schemas.IntVec2, path, props), IntVec3: (path: string, props?: SchemaProps) => makeType(schemas.IntVec3, path, props), Matrix2: (path: string, props?: SchemaProps) => makeType(schemas.Matrix2, path, props), Matrix3: (path: string, props?: SchemaProps) => makeType(schemas.Matrix3, path, props), Matrix4: (path: string, props?: SchemaProps) => makeType(schemas.Matrix4, path, props), Scale: (path: string, props?: SchemaProps) => makeType(schemas.Scale, path, props), Scale2: (path: string, props?: SchemaProps) => makeType(schemas.Scale2, path, props), Scale3: (path: string, props?: SchemaProps) => makeType(schemas.Scale3, path, props), ColorRGB: (path: string, props?: SchemaProps) => makeType(schemas.ColorRGB, path, props), ColorRGBA: (path: string, props?: SchemaProps) => makeType(schemas.ColorRGBA, path, props), Boolean: (path: string, props?: SchemaProps) => makeType(schemas.Boolean, path, props), String: (path: string, props?: SchemaProps) => makeType(schemas.String, path, props), DateTime: (path: string, props?: SchemaProps) => makeType(schemas.DateTime, path, props), AssetPath: (path: string, props?: SchemaProps) => makeType(schemas.AssetPath, path, props), Enum: (path: string, enumeration: T, props?: SchemaProps>) => makeEnumType(enumeration, path, props), Option: (path: string, options: string[], props?: SchemaProps) => makeOptionType(options, path, props), Object: (path: string, type: TypeOf, props?: SchemaProps) => makeObjectType(type, path, props), Event: (path: string, props?: SchemaProps) => makeType(schemas.Event, path, props) };