-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define properties of widgets as fields. Define <fig-*-field> components to bind widget's field by type. Update <fig-*-properties> to bind widget's fields using field components. WIP. Closes #47
- Loading branch information
1 parent
e030c5a
commit 7576359
Showing
96 changed files
with
1,855 additions
and
1,168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export class ArrayField extends Field<unknown[]> { | ||
constructor(name: string, | ||
label: string, | ||
value?: unknown[], | ||
isOptional: boolean = false, | ||
defaultValue?: unknown[]) { | ||
super(FieldType.array, name, label, value, isOptional, defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export class BoolField extends Field<boolean> { | ||
constructor(name: string, | ||
label: string, | ||
value?: boolean, | ||
isOptional: boolean = false, | ||
defaultValue?: boolean) { | ||
super(FieldType.bool, name, label, value, isOptional, defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {Field, FieldType} from "./field"; | ||
import {Color} from "../math"; | ||
|
||
export class ColorField extends Field<Color> { | ||
constructor(name: string, | ||
label: string, | ||
value?: Color, | ||
isOptional: boolean = false, | ||
defaultValue?: Color) { | ||
super(FieldType.color, name, label, value, isOptional, defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export type EnumFieldType = string | number; | ||
|
||
export interface EnumOption { | ||
readonly value: number; | ||
readonly label: string; | ||
} | ||
|
||
export class EnumField<T extends EnumFieldType> extends Field<T> { | ||
public readonly options: EnumOption[]; | ||
|
||
constructor(name: string, | ||
label: string, | ||
options: EnumOption[], | ||
value?: T, | ||
isOptional: boolean = false, | ||
defaultValue?: T) { | ||
super(FieldType.enum, name, label, value, isOptional, defaultValue); | ||
this.options = options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export enum FieldType { | ||
bool, | ||
integer, | ||
float, | ||
number, | ||
string, | ||
array, | ||
size, | ||
flags, | ||
color, | ||
enum, | ||
} | ||
|
||
export type FieldCallback = (value: any) => void; | ||
|
||
export class Field<T = unknown> { | ||
readonly type: FieldType; | ||
readonly name: string; | ||
readonly label: string; | ||
readonly isOptional: boolean; | ||
|
||
value?: T; | ||
defaultValue?: T; | ||
|
||
private readonly listeners: FieldCallback[]; | ||
|
||
protected constructor(type: FieldType, | ||
name: string, | ||
label: string, | ||
value?: T, | ||
isOptional: boolean = false, | ||
defaultValue?: T) { | ||
this.type = type; | ||
this.name = name; | ||
this.label = label; | ||
this.value = value; | ||
this.isOptional = isOptional; | ||
this.defaultValue = defaultValue; | ||
|
||
this.listeners = []; | ||
} | ||
|
||
get isRequired(): boolean { | ||
return !this.isOptional; | ||
} | ||
|
||
public addListener(fn: FieldCallback): void { | ||
this.listeners.push(fn); | ||
} | ||
|
||
public removeListener(fn: FieldCallback): void { | ||
const index: number = this.listeners.findIndex((listener) => listener === fn); | ||
|
||
if (index !== -1) { | ||
this.listeners.splice(index, 1); | ||
} | ||
} | ||
|
||
public emit(): void { | ||
for (const listener of this.listeners) { | ||
listener(this.value); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {Field, FieldType} from "./field"; | ||
import {EnumFieldType} from "./enum.field"; | ||
|
||
export interface FlagOption { | ||
readonly value: number; | ||
readonly label: string; | ||
} | ||
|
||
export function getOptions(flags: Record<EnumFieldType, EnumFieldType>): FlagOption[] { | ||
return Object.keys(flags) | ||
.filter((key: EnumFieldType) => !isNaN(Number(key))) | ||
.map((key: EnumFieldType) => { | ||
return { | ||
value: Number.parseInt(key as string), | ||
label: flags[key] | ||
} as FlagOption; | ||
}); | ||
} | ||
|
||
export class FlagsField extends Field<number> { | ||
readonly options: FlagOption[]; | ||
|
||
constructor(name: string, | ||
label: string, | ||
options: FlagOption[], | ||
value?: number, | ||
isOptional: boolean = false, | ||
defaultValue?: number) { | ||
super(FieldType.flags, name, label, value, isOptional, defaultValue); | ||
this.options = options; | ||
} | ||
|
||
public disable(mask: number): void { | ||
if (this.value === undefined) { | ||
return; | ||
} | ||
if ((this.value & mask) === mask) { | ||
this.value ^= mask; | ||
} | ||
this.emit(); | ||
} | ||
|
||
public enable(mask: number): void { | ||
if (this.value === undefined) { | ||
return; | ||
} | ||
this.value |= mask; | ||
this.emit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export class FloatField extends Field<number> { | ||
constructor(name: string, | ||
label: string, | ||
value?: number, | ||
isOptional: boolean = false, | ||
defaultValue?: number) { | ||
super(FieldType.float, name, label, value, isOptional, defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export class IntegerField extends Field<number> { | ||
constructor(name: string, | ||
label: string, | ||
value?: number, | ||
isOptional: boolean = false, | ||
defaultValue?: number) { | ||
super(FieldType.integer, name, label, value, isOptional, defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export class NumberField extends Field<number> { | ||
constructor(name: string, | ||
label: string, | ||
value?: number, | ||
isOptional: boolean = false, | ||
defaultValue?: number) { | ||
super(FieldType.number, name, label, value, isOptional, defaultValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {Field, FieldType} from "./field"; | ||
import {Size} from "../math"; | ||
|
||
export class SizeField extends Field<Size> { | ||
readonly acceptRelative: boolean; | ||
|
||
constructor(name: string, | ||
label: string, | ||
acceptRelative: boolean = false, | ||
value?: Size, | ||
isOptional: boolean = false, | ||
defaultValue?: Size) { | ||
super(FieldType.size, name, label, value, isOptional, defaultValue); | ||
this.acceptRelative = acceptRelative; | ||
} | ||
|
||
public get isPercentage(): boolean { | ||
if (!this.value) { | ||
return false; | ||
} | ||
return (this.value.width > 0.0 && this.value.width <= 1.0) || | ||
(this.value.height > 0.0 && this.value.height <= 1.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export class StringField extends Field<string> { | ||
constructor(name: string, | ||
label: string, | ||
value?: string, | ||
isOptional: boolean = false, | ||
defaultValue?: string) { | ||
super(FieldType.string, name, label, value, isOptional, defaultValue); | ||
} | ||
} |
Oops, something went wrong.