-
Notifications
You must be signed in to change notification settings - Fork 1
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. WIP. Closes #47
- Loading branch information
1 parent
e030c5a
commit 4e43966
Showing
59 changed files
with
978 additions
and
415 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
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: unknown) => 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,11 @@ | ||
import {Field, FieldType} from "./field"; | ||
|
||
export class FlagsField extends Field<number> { | ||
constructor(name: string, | ||
label: string, | ||
value?: number, | ||
isOptional: boolean = false, | ||
defaultValue?: number) { | ||
super(FieldType.flags, 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 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,12 @@ | ||
import {Field, FieldType} from "./field"; | ||
import {Size} from "../math"; | ||
|
||
export class SizeField extends Field<Size> { | ||
constructor(name: string, | ||
label: string, | ||
value?: Size, | ||
isOptional: boolean = false, | ||
defaultValue?: Size) { | ||
super(FieldType.size, 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 StringField extends Field<string> { | ||
constructor(name: string, | ||
label: string, | ||
value?: string, | ||
isOptional: boolean = false, | ||
defaultValue?: string) { | ||
super(FieldType.string, 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,8 @@ | ||
export function hasFunction(obj: object | null, fnName: string): boolean { | ||
while ((obj = Reflect.getPrototypeOf(obj as object)) !== null) { | ||
if (Reflect.ownKeys(obj).find((key) => key === fnName)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
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
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
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
Oops, something went wrong.