@remvst/configurable-react 1.3.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,8 +2,10 @@ import React from 'react';
2
2
  import { CompositeConfigurable } from '@remvst/configurable';
3
3
  import { ComponentProps } from './component-props';
4
4
  import { ConfigurableToComponent } from './mapping';
5
+ import { TextureType } from './textured-enum-component';
5
6
  interface Props extends ComponentProps<CompositeConfigurable> {
6
7
  readonly mapper: ConfigurableToComponent;
8
+ readonly getTexture?: (enumToken: any, item: any) => [string, TextureType] | null;
7
9
  }
8
10
  export default class CompositeConfigurableComponent extends React.Component<Props> {
9
11
  onClickLabel(event: React.MouseEvent): void;
@@ -9,12 +9,12 @@ class CompositeConfigurableComponent extends react_1.default.Component {
9
9
  event.target.parentElement?.querySelector('input,select')?.focus();
10
10
  }
11
11
  render() {
12
- const { configurable, mapper } = this.props;
12
+ const { configurable, mapper, getTexture } = this.props;
13
13
  const subComponents = configurable.entries.map(subConfigurable => {
14
14
  const id = `id-${Math.floor(Math.random() * 100000)}`;
15
15
  return (react_1.default.createElement("div", { key: subConfigurable.key, className: "composite-configurable-row" },
16
16
  react_1.default.createElement("label", { onClick: event => this.onClickLabel(event) }, subConfigurable.key),
17
- mapper(subConfigurable.configurable, mapper)));
17
+ mapper({ 'configurable': subConfigurable.configurable, mapper, getTexture })));
18
18
  });
19
19
  return (react_1.default.createElement("div", { className: "composite-configurable" }, subComponents));
20
20
  }
@@ -2,8 +2,10 @@ import React from 'react';
2
2
  import { GroupConfigurable } from '@remvst/configurable';
3
3
  import { ComponentProps } from './component-props';
4
4
  import { ConfigurableToComponent } from './mapping';
5
+ import { TextureType } from './textured-enum-component';
5
6
  interface Props extends ComponentProps<GroupConfigurable> {
6
7
  readonly mapper: ConfigurableToComponent;
8
+ readonly getTexture?: (enumToken: any, item: any) => [string, TextureType] | null;
7
9
  }
8
10
  export default class CompositeConfigurableComponent extends React.Component<Props> {
9
11
  render(): JSX.Element;
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const react_1 = __importDefault(require("react"));
7
7
  class CompositeConfigurableComponent extends react_1.default.Component {
8
8
  render() {
9
- const { configurable, mapper } = this.props;
9
+ const { configurable, mapper, getTexture } = this.props;
10
10
  const subComponents = configurable.entries.map((subConfigurable, i) => {
11
- return (react_1.default.createElement("div", { key: i, className: "group-configurable-item" }, mapper(subConfigurable, mapper)));
11
+ return (react_1.default.createElement("div", { key: i, className: "group-configurable-item" }, mapper({ 'configurable': subConfigurable, mapper, getTexture })));
12
12
  });
13
13
  return (react_1.default.createElement("div", { className: "group-configurable" }, subComponents));
14
14
  }
package/lib/index.d.ts CHANGED
@@ -1,7 +1,16 @@
1
1
  import { ReactElement } from 'react';
2
2
  import { Configurable } from '@remvst/configurable';
3
3
  import CompositeConfigurableComponent from './composite-configurable-component';
4
+ import { TextureType } from './textured-enum-component';
4
5
  import { ConfigurableToComponent } from './mapping';
5
- export declare function defaultComponents(configurable: Configurable, mapper: ConfigurableToComponent): ReactElement;
6
- export declare function configurableToComponents(configurable: () => Configurable, mapper?: ConfigurableToComponent): JSX.Element;
7
- export { CompositeConfigurableComponent, ConfigurableToComponent, };
6
+ export declare function defaultComponents(opts: {
7
+ configurable: Configurable;
8
+ mapper: ConfigurableToComponent;
9
+ getTexture?: (enumToken: any, item: any) => [string, TextureType] | null;
10
+ }): ReactElement;
11
+ export declare function configurableToComponents(opts: {
12
+ configurable: () => Configurable;
13
+ mapper: ConfigurableToComponent;
14
+ getTexture?: (enumToken: any, item: any) => [string, TextureType] | null;
15
+ }): JSX.Element;
16
+ export { CompositeConfigurableComponent, ConfigurableToComponent, TextureType, };
package/lib/index.js CHANGED
@@ -1,9 +1,32 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CompositeConfigurableComponent = exports.configurableToComponents = exports.defaultComponents = void 0;
29
+ exports.TextureType = exports.CompositeConfigurableComponent = exports.configurableToComponents = exports.defaultComponents = void 0;
7
30
  const react_1 = __importDefault(require("react"));
8
31
  const configurable_1 = require("@remvst/configurable");
9
32
  const composite_configurable_component_1 = __importDefault(require("./composite-configurable-component"));
@@ -16,12 +39,15 @@ const enum_configurable_component_1 = __importDefault(require("./enum-configurab
16
39
  const button_configurable_component_1 = __importDefault(require("./button-configurable-component"));
17
40
  const group_configurable_component_1 = __importDefault(require("./group-configurable-component"));
18
41
  const invalidatable_component_1 = __importDefault(require("./invalidatable-component"));
19
- function defaultComponents(configurable, mapper) {
42
+ const textured_enum_component_1 = __importStar(require("./textured-enum-component"));
43
+ Object.defineProperty(exports, "TextureType", { enumerable: true, get: function () { return textured_enum_component_1.TextureType; } });
44
+ function defaultComponents(opts) {
45
+ const { configurable, mapper } = opts;
20
46
  if (configurable instanceof configurable_1.CompositeConfigurable) {
21
- return (react_1.default.createElement(composite_configurable_component_1.default, { configurable: configurable, mapper: mapper }));
47
+ return (react_1.default.createElement(composite_configurable_component_1.default, { configurable: configurable, mapper: mapper, getTexture: opts.getTexture }));
22
48
  }
23
49
  if (configurable instanceof configurable_1.GroupConfigurable) {
24
- return (react_1.default.createElement(group_configurable_component_1.default, { configurable: configurable, mapper: mapper }));
50
+ return (react_1.default.createElement(group_configurable_component_1.default, { configurable: configurable, mapper: mapper, getTexture: opts.getTexture }));
25
51
  }
26
52
  if (configurable instanceof configurable_1.ColorConfigurable) {
27
53
  return (react_1.default.createElement(color_configurable_component_1.default, { configurable: configurable }));
@@ -36,6 +62,9 @@ function defaultComponents(configurable, mapper) {
36
62
  return (react_1.default.createElement(boolean_configurable_component_1.default, { configurable: configurable }));
37
63
  }
38
64
  if (configurable instanceof configurable_1.EnumConfigurable) {
65
+ if (configurable.enumToken) {
66
+ return (react_1.default.createElement(textured_enum_component_1.default, { configurable: configurable, getTexture: opts.getTexture }));
67
+ }
39
68
  return (react_1.default.createElement(enum_configurable_component_1.default, { configurable: configurable }));
40
69
  }
41
70
  if (configurable instanceof configurable_1.ButtonConfigurable) {
@@ -44,8 +73,8 @@ function defaultComponents(configurable, mapper) {
44
73
  throw new Error(`Unrecognized component type ${configurable.constructor.name}`);
45
74
  }
46
75
  exports.defaultComponents = defaultComponents;
47
- function configurableToComponents(configurable, mapper = defaultComponents) {
48
- return react_1.default.createElement("div", { className: 'configurable' },
49
- react_1.default.createElement(invalidatable_component_1.default, { configurable: configurable, mapper: mapper }));
76
+ function configurableToComponents(opts) {
77
+ return (react_1.default.createElement("div", { className: 'configurable' },
78
+ react_1.default.createElement(invalidatable_component_1.default, { ...opts })));
50
79
  }
51
80
  exports.configurableToComponents = configurableToComponents;
@@ -1,9 +1,11 @@
1
1
  import React from "react";
2
2
  import { Configurable } from '@remvst/configurable';
3
3
  import { ConfigurableToComponent } from "./mapping";
4
+ import { TextureType } from "./textured-enum-component";
4
5
  interface Props {
5
6
  readonly configurable: () => Configurable;
6
7
  readonly mapper: ConfigurableToComponent;
8
+ readonly getTexture?: (enumToken: any, item: any) => ([string, TextureType] | null);
7
9
  }
8
10
  interface State {
9
11
  readonly configurable: Configurable;
@@ -22,7 +22,11 @@ class InvalidatableComponent extends react_1.default.Component {
22
22
  return configurable;
23
23
  }
24
24
  render() {
25
- return (react_1.default.createElement("div", { key: this.state.refreshCount }, this.props.mapper(this.state.configurable, this.props.mapper)));
25
+ return (react_1.default.createElement("div", { key: this.state.refreshCount }, this.props.mapper({
26
+ 'configurable': this.state.configurable,
27
+ 'mapper': this.props.mapper,
28
+ 'getTexture': this.props.getTexture,
29
+ })));
26
30
  }
27
31
  }
28
32
  exports.default = InvalidatableComponent;
package/lib/mapping.d.ts CHANGED
@@ -1,3 +1,8 @@
1
1
  import { ReactElement } from 'react';
2
2
  import { Configurable } from '@remvst/configurable';
3
- export declare type ConfigurableToComponent = (configurable: Configurable, mapper: ConfigurableToComponent) => ReactElement;
3
+ import { TextureType } from './textured-enum-component';
4
+ export declare type ConfigurableToComponent = (opts: {
5
+ configurable: Configurable;
6
+ mapper: ConfigurableToComponent;
7
+ getTexture?: (enumToken: any, item: any) => [string, TextureType] | null;
8
+ }) => ReactElement;
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ import { EnumConfigurable } from '@remvst/configurable';
3
+ export declare enum TextureType {
4
+ TILED = 0,
5
+ SINGLE = 1
6
+ }
7
+ export default function TexturedEnumComponent<T>(props: {
8
+ configurable: EnumConfigurable<T>;
9
+ getTexture?: (enumToken: any, item: T) => [string, TextureType] | null;
10
+ }): JSX.Element;
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.TextureType = void 0;
27
+ const react_1 = __importStar(require("react"));
28
+ var TextureType;
29
+ (function (TextureType) {
30
+ TextureType[TextureType["TILED"] = 0] = "TILED";
31
+ TextureType[TextureType["SINGLE"] = 1] = "SINGLE";
32
+ })(TextureType = exports.TextureType || (exports.TextureType = {}));
33
+ function TexturedEnumComponent(props) {
34
+ const [currentValue, setCurrentValue] = (0, react_1.useState)(props.configurable.read());
35
+ return (react_1.default.createElement("div", null, Array.from(props.configurable.categories).map(category => {
36
+ return (react_1.default.createElement(CategoryComponent, { key: category.label, category: category, currentValue: currentValue, getTexture: props.getTexture, enumToken: props.configurable.enumToken, onSelectValue: (value) => {
37
+ props.configurable.write(value);
38
+ setCurrentValue(value);
39
+ } }));
40
+ })));
41
+ }
42
+ exports.default = TexturedEnumComponent;
43
+ function CategoryComponent(props) {
44
+ const items = props.category.items.map(item => {
45
+ const texture = props.getTexture ? props.getTexture(props.enumToken, item.value) : null;
46
+ return react_1.default.createElement(TextureItemComponent, { key: item.key, title: item.key, src: texture ? texture[0] : null, textureType: texture ? texture[1] : null, enumToken: props.enumToken, selected: item.value === props.currentValue, onClick: () => props.onSelectValue(item.value) });
47
+ });
48
+ return (react_1.default.createElement("div", { className: 'group-configurable' }, items));
49
+ }
50
+ function TextureItemComponent(props) {
51
+ const [size, setSize] = (0, react_1.useState)({ 'width': 0, 'height': 0 });
52
+ (0, react_1.useEffect)(() => {
53
+ if (size.width !== 0 || !props.src) {
54
+ return;
55
+ }
56
+ const img = new Image();
57
+ img.onload = () => setSize({ width: img.width, height: img.height });
58
+ img.src = props.src;
59
+ });
60
+ let backgroundSize = 'contain';
61
+ switch (props.textureType) {
62
+ case TextureType.SINGLE:
63
+ backgroundSize = 'contain';
64
+ break;
65
+ case TextureType.TILED:
66
+ backgroundSize = `${size.width * 2}px ${size.height * 2}px`;
67
+ break;
68
+ }
69
+ return (react_1.default.createElement("div", { title: props.title, style: {
70
+ 'outline': props.selected ? '1px solid white' : 'none',
71
+ 'width': '20px',
72
+ 'height': '20px',
73
+ 'margin': '2px',
74
+ 'backgroundImage': props.src ? `url("${props.src}")` : 'none',
75
+ 'backgroundRepeat': 'no-repeat',
76
+ 'backgroundPosition': 'center',
77
+ 'imageRendering': 'pixelated',
78
+ backgroundSize,
79
+ }, onClick: () => props.onClick() }));
80
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remvst/configurable-react",
3
- "version": "1.3.0",
3
+ "version": "1.5.1",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "exports": {
@@ -13,7 +13,7 @@
13
13
  "author": "",
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
- "@remvst/configurable": "^1.4.0",
16
+ "@remvst/configurable": "^1.5.0",
17
17
  "react": "^18.2.0",
18
18
  "react-dom": "^18.2.0",
19
19
  "@types/react-dom": "^18.0.6",
@@ -2,9 +2,11 @@ import React from 'react';
2
2
  import { CompositeConfigurable } from '@remvst/configurable';
3
3
  import { ComponentProps } from './component-props';
4
4
  import { ConfigurableToComponent } from './mapping';
5
+ import { TextureType } from './textured-enum-component';
5
6
 
6
7
  interface Props extends ComponentProps<CompositeConfigurable> {
7
8
  readonly mapper: ConfigurableToComponent;
9
+ readonly getTexture?: (enumToken: any, item: any) => [string, TextureType] | null;
8
10
  }
9
11
 
10
12
  export default class CompositeConfigurableComponent extends React.Component<Props> {
@@ -13,14 +15,14 @@ export default class CompositeConfigurableComponent extends React.Component<Prop
13
15
  }
14
16
 
15
17
  render() {
16
- const { configurable, mapper } = this.props;
18
+ const { configurable, mapper, getTexture } = this.props;
17
19
 
18
20
  const subComponents = configurable.entries.map(subConfigurable => {
19
21
  const id = `id-${Math.floor(Math.random() * 100000)}`;
20
22
  return (
21
23
  <div key={subConfigurable.key} className="composite-configurable-row">
22
24
  <label onClick={event => this.onClickLabel(event)}>{subConfigurable.key}</label>
23
- {mapper(subConfigurable.configurable, mapper)}
25
+ {mapper({'configurable': subConfigurable.configurable, mapper, getTexture})}
24
26
  </div>
25
27
  );
26
28
  });
@@ -2,19 +2,21 @@ import React from 'react';
2
2
  import { GroupConfigurable } from '@remvst/configurable';
3
3
  import { ComponentProps } from './component-props';
4
4
  import { ConfigurableToComponent } from './mapping';
5
+ import { TextureType } from './textured-enum-component';
5
6
 
6
7
  interface Props extends ComponentProps<GroupConfigurable> {
7
8
  readonly mapper: ConfigurableToComponent;
9
+ readonly getTexture?: (enumToken: any, item: any) => [string, TextureType] | null;
8
10
  }
9
11
 
10
12
  export default class CompositeConfigurableComponent extends React.Component<Props> {
11
13
  render() {
12
- const { configurable, mapper } = this.props;
14
+ const { configurable, mapper, getTexture } = this.props;
13
15
 
14
16
  const subComponents = configurable.entries.map((subConfigurable, i) => {
15
17
  return (
16
18
  <div key={i} className="group-configurable-item">
17
- {mapper(subConfigurable, mapper)}
19
+ {mapper({'configurable': subConfigurable, mapper, getTexture})}
18
20
  </div>
19
21
  );
20
22
  });
package/src/index.tsx CHANGED
@@ -9,14 +9,21 @@ import EnumConfigurableComponent from './enum-configurable-component';
9
9
  import ButtonConfigurableComponent from './button-configurable-component';
10
10
  import GroupConfigurableComponent from './group-configurable-component';
11
11
  import InvalidatableComponent from './invalidatable-component';
12
+ import TexturedEnumComponent, { TextureType } from './textured-enum-component';
12
13
  import { ConfigurableToComponent } from './mapping';
13
14
 
14
- export function defaultComponents(configurable: Configurable, mapper: ConfigurableToComponent): ReactElement {
15
+ export function defaultComponents(opts: {
16
+ configurable: Configurable,
17
+ mapper: ConfigurableToComponent,
18
+ getTexture?: (enumToken: any, item: any) => [string, TextureType] | null,
19
+ }): ReactElement {
20
+ const { configurable, mapper } = opts;
21
+
15
22
  if (configurable instanceof CompositeConfigurable) {
16
- return (<CompositeConfigurableComponent configurable={configurable} mapper={mapper} />);
23
+ return (<CompositeConfigurableComponent configurable={configurable} mapper={mapper} getTexture={opts.getTexture} />);
17
24
  }
18
25
  if (configurable instanceof GroupConfigurable) {
19
- return (<GroupConfigurableComponent configurable={configurable} mapper={mapper} />);
26
+ return (<GroupConfigurableComponent configurable={configurable} mapper={mapper} getTexture={opts.getTexture} />);
20
27
  }
21
28
  if (configurable instanceof ColorConfigurable) {
22
29
  return (<ColorConfigurableComponent configurable={configurable} />);
@@ -31,6 +38,9 @@ export function defaultComponents(configurable: Configurable, mapper: Configurab
31
38
  return (<BooleanConfigurableComponent configurable={configurable} />);
32
39
  }
33
40
  if (configurable instanceof EnumConfigurable) {
41
+ if (configurable.enumToken) {
42
+ return (<TexturedEnumComponent configurable={configurable} getTexture={opts.getTexture} />);
43
+ }
34
44
  return (<EnumConfigurableComponent configurable={configurable} />);
35
45
  }
36
46
  if (configurable instanceof ButtonConfigurable) {
@@ -39,14 +49,20 @@ export function defaultComponents(configurable: Configurable, mapper: Configurab
39
49
  throw new Error(`Unrecognized component type ${configurable.constructor.name}`);
40
50
  }
41
51
 
42
- export function configurableToComponents(
52
+ export function configurableToComponents(opts: {
43
53
  configurable: () => Configurable,
44
- mapper: ConfigurableToComponent = defaultComponents,
45
- ) {
46
- return <div className='configurable'><InvalidatableComponent configurable={configurable} mapper={mapper} /></div>;
54
+ mapper: ConfigurableToComponent,
55
+ getTexture?: (enumToken: any, item: any) => [string, TextureType] | null,
56
+ }) {
57
+ return (
58
+ <div className='configurable'>
59
+ <InvalidatableComponent {...opts} />
60
+ </div>
61
+ );
47
62
  }
48
63
 
49
64
  export {
50
65
  CompositeConfigurableComponent,
51
66
  ConfigurableToComponent,
67
+ TextureType,
52
68
  }
@@ -1,12 +1,12 @@
1
1
  import React from "react";
2
- import { ComponentProps } from "./component-props";
3
-
4
2
  import { Configurable } from '@remvst/configurable';
5
3
  import { ConfigurableToComponent } from "./mapping";
4
+ import { TextureType } from "./textured-enum-component";
6
5
 
7
6
  interface Props {
8
7
  readonly configurable: () => Configurable;
9
8
  readonly mapper: ConfigurableToComponent;
9
+ readonly getTexture?: (enumToken: any, item: any) => ([string, TextureType] | null),
10
10
  }
11
11
 
12
12
  interface State {
@@ -36,6 +36,12 @@ export default class InvalidatableComponent extends React.Component<Props, State
36
36
  }
37
37
 
38
38
  render(): React.ReactNode {
39
- return (<div key={this.state.refreshCount}>{this.props.mapper(this.state.configurable, this.props.mapper)}</div>);
39
+ return (<div key={this.state.refreshCount}>
40
+ {this.props.mapper({
41
+ 'configurable': this.state.configurable,
42
+ 'mapper': this.props.mapper,
43
+ 'getTexture': this.props.getTexture,
44
+ })}
45
+ </div>);
40
46
  }
41
47
  }
package/src/mapping.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { ReactElement } from 'react';
2
2
  import { Configurable } from '@remvst/configurable';
3
+ import { TextureType } from './textured-enum-component';
3
4
 
4
- export type ConfigurableToComponent = (
5
+ export type ConfigurableToComponent = (opts: {
5
6
  configurable: Configurable,
6
7
  mapper: ConfigurableToComponent,
7
- ) => ReactElement;
8
+ getTexture?: (enumToken: any, item: any) => [string, TextureType] | null,
9
+ }) => ReactElement;
@@ -0,0 +1,105 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import { EnumCategory, EnumConfigurable } from '@remvst/configurable';
3
+
4
+ export enum TextureType {
5
+ TILED,
6
+ SINGLE,
7
+ }
8
+
9
+ export default function TexturedEnumComponent<T>(props: {
10
+ configurable: EnumConfigurable<T>,
11
+ getTexture?: (enumToken: any, item: T) => [string, TextureType] | null,
12
+ }) {
13
+ const [currentValue, setCurrentValue] = useState(props.configurable.read());
14
+
15
+ return (<div>
16
+ {Array.from(props.configurable.categories).map(category => {
17
+ return (
18
+ <CategoryComponent
19
+ key={category.label}
20
+ category={category}
21
+ currentValue={currentValue}
22
+ getTexture={props.getTexture}
23
+ enumToken={props.configurable.enumToken}
24
+ onSelectValue={(value: T) => {
25
+ props.configurable.write(value);
26
+ setCurrentValue(value);
27
+ }} />
28
+ );
29
+ })}
30
+ </div>)
31
+ }
32
+
33
+ function CategoryComponent<T>(props: {
34
+ category: EnumCategory<T>
35
+ currentValue: T,
36
+ enumToken: any,
37
+ getTexture?: (enumToken: any, item: T) => [string, TextureType] | null
38
+ onSelectValue: (item: T) => void
39
+ }) {
40
+ const items = props.category.items.map(item => {
41
+ const texture = props.getTexture ? props.getTexture(props.enumToken, item.value) : null;
42
+
43
+ return <TextureItemComponent
44
+ key={item.key}
45
+ title={item.key}
46
+ src={texture ? texture[0] : null}
47
+ textureType={texture ? texture[1] : null}
48
+ enumToken={props.enumToken}
49
+ selected={item.value === props.currentValue}
50
+ onClick={() => props.onSelectValue(item.value)} />
51
+ });
52
+
53
+ return (<div className='group-configurable'>
54
+ {items}
55
+ </div>);
56
+ }
57
+
58
+ function TextureItemComponent(props: {
59
+ title: string,
60
+ src: string | null,
61
+ textureType: TextureType | null,
62
+ enumToken: any,
63
+ selected: boolean,
64
+ onClick: () => void,
65
+ }) {
66
+ const [size, setSize] = useState({'width': 0, 'height': 0});
67
+
68
+ useEffect(() => {
69
+ if (size.width !== 0 || !props.src) {
70
+ return;
71
+ }
72
+
73
+ const img = new Image();
74
+ img.onload = () => setSize({width: img.width, height: img.height});
75
+ img.src = props.src;
76
+ });
77
+
78
+ let backgroundSize: string = 'contain';
79
+ switch (props.textureType) {
80
+ case TextureType.SINGLE:
81
+ backgroundSize = 'contain';
82
+ break;
83
+ case TextureType.TILED:
84
+ backgroundSize =`${size.width * 2}px ${size.height * 2}px`;
85
+ break;
86
+ }
87
+
88
+ return (
89
+ <div
90
+ title={props.title}
91
+ style={{
92
+ 'outline': props.selected ? '1px solid white' : 'none',
93
+ 'width': '20px',
94
+ 'height': '20px',
95
+ 'margin': '2px',
96
+ 'backgroundImage': props.src ? `url("${props.src}")` : 'none',
97
+ 'backgroundRepeat': 'no-repeat',
98
+ 'backgroundPosition': 'center',
99
+ 'imageRendering': 'pixelated',
100
+ backgroundSize,
101
+ }}
102
+ onClick={() => props.onClick()} />
103
+ );
104
+ }
105
+