-
Notifications
You must be signed in to change notification settings - Fork 57
/
component.tsx
182 lines (159 loc) · 6.51 KB
/
component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import * as React from "react";
import { observer } from "mobx-react";
import { autobind } from "core-decorators";
import { getAccessoryOfType, getIndexOfAccessoryType } from "oni-save-parser";
import { Button, MenuItem } from "@blueprintjs/core";
import { IconNames } from "@blueprintjs/icons";
import { Select, IItemRendererProps } from "@blueprintjs/select";
const StringSelect = Select.ofType<string>();
import { GameObjectModel } from "@/services/save-editor";
import {
ACCESSORIZER_EYE_GUIDS,
ACCESSORIZER_HAIR_GUIDS,
ACCESSORIZER_HEAD_GUIDS,
ACCESSORIZER_MOUTH_GUIDS,
ACCESSORIZER_BODY_GUIDS,
AccessorizerBehavior
} from "oni-save-parser";
import { action } from "mobx";
export interface DuplicantAccessoryPageProps {
duplicant: GameObjectModel;
};
type Props = DuplicantAccessoryPageProps;
@observer
class DuplicantAccessoryPage extends React.Component<Props> {
render() {
const { duplicant } = this.props;
const accessorizer = duplicant.getBehavior(AccessorizerBehavior);
if (!accessorizer) return <div>Error: No AccessorizerBehavior found</div>;
const accessories = accessorizer.templateData.accessories;
const eyesAccessory = getAccessoryOfType(accessories, "eyes");
const headAccessory = getAccessoryOfType(accessories, "headshape");
const hairAccessory = getAccessoryOfType(accessories, "hair");
const mouthAccessory = getAccessoryOfType(accessories, "mouth");
const bodyAccessory = getAccessoryOfType(accessories, "body");
const eyes = eyesAccessory ? eyesAccessory.guid.Guid : "[Error: Accessory Not Found]";
const head = headAccessory ? headAccessory.guid.Guid : "[Error: Accessory Not Found]";
const hair = hairAccessory ? hairAccessory.guid.Guid : "[Error: Accessory Not Found]";
const mouth = mouthAccessory ? mouthAccessory.guid.Guid : "[Error: Accessory Not Found]";
const body = bodyAccessory ? bodyAccessory.guid.Guid : "[Error: Accessory Not Found]";
return (
<div className={`ui-duplicant-accessories fill-parent layout-vertical container-scroll`}>
<AppearanceGroup name="Eyes" value={eyes} accessories={ACCESSORIZER_EYE_GUIDS} onSet={this._setEyes}/>
<AppearanceGroup name="Head" value={head} accessories={ACCESSORIZER_HEAD_GUIDS} onSet={this._setHead}/>
<AppearanceGroup name="Hair" value={hair} accessories={ACCESSORIZER_HAIR_GUIDS} onSet={this._setHair}/>
<AppearanceGroup name="Mouth" value={mouth} accessories={ACCESSORIZER_MOUTH_GUIDS} onSet={this._setMouth}/>
<AppearanceGroup name="Body" value={body} accessories={ACCESSORIZER_BODY_GUIDS} onSet={this._setBody}/>
</div>
);
}
@action.bound
private _setEyes(value: string) {
const { duplicant } = this.props;
const accessorizer = duplicant.getBehavior(AccessorizerBehavior);
if (!accessorizer) return;
const accessories = accessorizer.templateData.accessories;
const index = getIndexOfAccessoryType(accessories, "eyes");
if (index == -1) return;
accessories[index].guid.Guid = value;
}
@action.bound
private _setHead(value: string) {
const { duplicant } = this.props;
const accessorizer = duplicant.getBehavior(AccessorizerBehavior);
if (!accessorizer) return;
const accessories = accessorizer.templateData.accessories;
const index = getIndexOfAccessoryType(accessories, "headshape");
if (index == -1) return;
accessories[index].guid.Guid = value;
}
@action.bound
private _setHair(value: string) {
const { duplicant } = this.props;
const accessorizer = duplicant.getBehavior(AccessorizerBehavior);
if (!accessorizer) return;
const accessories = accessorizer.templateData.accessories;
const index = getIndexOfAccessoryType(accessories, "hair");
if (index == -1) return;
accessories[index].guid.Guid = value;
}
@action.bound
private _setMouth(value: string) {
const { duplicant } = this.props;
const accessorizer = duplicant.getBehavior(AccessorizerBehavior);
if (!accessorizer) return;
const accessories = accessorizer.templateData.accessories;
const index = getIndexOfAccessoryType(accessories, "mouth");
if (index == -1) return;
accessories[index].guid.Guid = value;
}
@action.bound
private _setBody(value: string) {
const { duplicant } = this.props;
const accessorizer = duplicant.getBehavior(AccessorizerBehavior);
if (!accessorizer) return;
const accessories = accessorizer.templateData.accessories;
const index = getIndexOfAccessoryType(accessories, "body");
if (index == -1) return;
accessories[index].guid.Guid = value;
}
}
export default DuplicantAccessoryPage;
interface AppearanceGroupProps {
name: string;
accessories: string[];
value: string | null;
onSet(accessoryID: string): void
}
class AppearanceGroup extends React.Component<AppearanceGroupProps> {
render() {
const {
name,
accessories,
value
} = this.props;
return (
<div className="pt-form-group pt-inline">
<label className="pt-label">
{name}
</label>
<div className="pt-form-content">
<StringSelect
items={accessories}
itemRenderer={this._renderItem}
onItemSelect={this._onSet}
filterable={false}
resetOnClose={true}
resetOnSelect={true}
popoverProps={{ minimal: true }}
>
<Button rightIcon="caret-down" text={value || "[Accessory not Found]"} />
</StringSelect>
</div>
</div>
);
}
@autobind()
private _renderItem(accessoryID: string, itemProps: IItemRendererProps) {
const {
modifiers,
handleClick
} = itemProps;
if (!modifiers.matchesPredicate) {
return null;
}
return (
<MenuItem
active={modifiers.active}
key={accessoryID}
onClick={handleClick}
text={accessoryID}
/>
);
};
@autobind()
private _onSet(accessoryID: string) {
const { onSet } = this.props;
onSet(accessoryID);
}
}