Skip to content

Commit

Permalink
added number and alpha property editor
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Aug 30, 2021
1 parent be3a27a commit bff4587
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/typescript.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"import './${TM_FILENAME_BASE}.scss';",
"",
"export class ${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/} extends HTMLElement {",
"\tpublic static readonly tagName = 'phed-${TM_FILENAME_BASE}';",
"\tpublic static readonly tagName: string = 'phed-${TM_FILENAME_BASE}';",
"",
"\tpublic connectedCallback() {}",
"}",
Expand Down
2 changes: 1 addition & 1 deletion src/ui/actions/actions-toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Widget } from 'ui/widget/widget';
import './actions-toolbar.scss'

export class ActionsToolbar extends Widget {
public static readonly tagName = 'phred-actions-toolbar';
public static readonly tagName: string = 'phred-actions-toolbar';
}

customElements.define(ActionsToolbar.tagName, ActionsToolbar);
5 changes: 5 additions & 0 deletions src/ui/properties-editors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { AlphaPropertyEditor } from './properties/editors/number/alpha-property-editor';
import { NumberPropertyEditor } from './properties/editors/number/number-property-editor';
import { StringPropertyEditor } from './properties/editors/string/string-property-editor';

// TODO this name is awful. Please find a better one
class PropertiesEditorsClass {
private readonly editors = {
string: StringPropertyEditor.tagName,
number: NumberPropertyEditor.tagName,
alpha: AlphaPropertyEditor.tagName,
default: StringPropertyEditor.tagName,
};

Expand Down
15 changes: 15 additions & 0 deletions src/ui/properties/editors/number/alpha-property-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NumberPropertyEditor } from './number-property-editor';

export class AlphaPropertyEditor extends NumberPropertyEditor {
public static readonly tagName: string = 'phed-alpha-property-editor';

protected createInput(value: number, propertyId: string) {
const el = super.createInput(value, propertyId);
el.setAttribute('min', '0');
el.setAttribute('max', '1');
el.setAttribute('step', '0.1');
return el;
}
}

customElements.define(AlphaPropertyEditor.tagName, AlphaPropertyEditor);
Empty file.
29 changes: 29 additions & 0 deletions src/ui/properties/editors/number/number-property-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { PropertyEditor } from '../property-editor';
import './number-property-editor.scss';

export class NumberPropertyEditor extends PropertyEditor<number> {
public static readonly tagName: string = 'phed-number-property-editor';

protected createContent(value: number, propertyId: string) {
const propContent = document.createElement('div');
propContent.classList.add('property-content');

const input = this.createInput(value, propertyId);
propContent.append(input);

return propContent;
}

protected createInput(value: number, propertyId: string) {
value = value === null || isNaN(value) ? 0 : value;
const input = document.createElement('input');
input.id = propertyId;

input.setAttribute('type', 'number');
input.setAttribute('value', value.toString());

return input;
}
}

customElements.define(NumberPropertyEditor.tagName, NumberPropertyEditor);
2 changes: 1 addition & 1 deletion src/ui/properties/editors/property-editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $property-foreground: $on-background-color;
flex: 1;
color: $property-foreground;

input[type="text"] {
input[type="text"], input[type="number"] {
width: 100%;
font-size: 16px;
background-color: $property-input-background;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/properties/editors/property-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ export abstract class PropertyEditor<T> extends HTMLElement {
return label;
}

protected abstract createValue(value: T, propertyId: string): HTMLElement;
protected abstract createContent(value: T, propertyId: string): HTMLElement;

public setContent(name: string, value: T) {
const propId = `prop-${name}`;
const title = this.createLabel(name, propId);
const content = this.createValue(value, propId);
const content = this.createContent(value, propId);
this.appendChild(title);
this.appendChild(content);
}
Expand Down
20 changes: 12 additions & 8 deletions src/ui/properties/editors/string/string-property-editor.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { PropertyEditor } from '../property-editor';
// import './string-property-editor.scss';

export class StringPropertyEditor extends PropertyEditor<string> {
public static readonly tagName = 'phed-string-property-editor';
public static readonly tagName: string = 'phed-string-property-editor';

protected createValue(value: string, propertyId: string) {
protected createContent(value: string, propertyId: string) {
const propContent = document.createElement('div');
propContent.classList.add('property-content');

const propValue = document.createElement('input');
propValue.id = propertyId;
propValue.setAttribute('type', 'text');
propValue.setAttribute('value', value ?? '');
propContent.append(propValue);
const input = this.createInput(value, propertyId);
propContent.append(input);

return propContent;
}

protected createInput(value: string, propertyId: string) {
const input = document.createElement('input');
input.id = propertyId;
input.setAttribute('type', 'text');
input.setAttribute('value', value ?? '');
return input;
}
}

customElements.define(StringPropertyEditor.tagName, StringPropertyEditor);
2 changes: 1 addition & 1 deletion src/ui/properties/panel/properties-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PropertyEditor } from '../editors/property-editor';
import './properties-panel.scss';

export class PropertiesPanel extends Widget {
public static readonly tagName = 'phed-properties-panel';
public static readonly tagName: string = 'phed-properties-panel';

private content: HTMLElement;

Expand Down
2 changes: 1 addition & 1 deletion src/ui/properties/toolbar/properties-toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PropertiesPanel } from '../panel/properties-panel';
import './properties-toolbar.scss';

export class PropertiesToolbar extends Widget {
public static readonly tagName = 'phred-properties-toolbar';
public static readonly tagName: string = 'phred-properties-toolbar';
private readonly panels: PropertiesPanel[] = []; // TODO depend on an interface or abstract class here

public connectedCallback() {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Widget } from './widget/widget';
import './stage.scss';

export class Stage extends Widget {
public static readonly tagName = 'phred-stage';
public static readonly tagName: string = 'phred-stage';

private _game: Phaser.Game;
private actions: ActionsToolbar;
Expand Down

0 comments on commit bff4587

Please sign in to comment.