1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var Module_1 = require("./../Module");
|
14 | var Tool_1 = require("./../Tool");
|
15 | var index_1 = require("./../index");
|
16 | var Graphic = (function (_super) {
|
17 | __extends(Graphic, _super);
|
18 |
|
19 | function Graphic() {
|
20 | var _this = _super.call(this) || this;
|
21 | |
22 |
|
23 |
|
24 | _this.graphics = {};
|
25 | |
26 |
|
27 |
|
28 | _this.state = Tool_1.Enum.STATE.DEFAULT;
|
29 | |
30 |
|
31 |
|
32 | _this.container = new index_1.PIXI.Graphics();
|
33 | _this.signals.stateChange = new Tool_1.SignalEvent();
|
34 | _this.signals.propChange.bind("disabled", _this.onDisabledChange.bind(_this));
|
35 | _this.signals.propChange.bind("activable", _this.onActivableChange.bind(_this));
|
36 | return _this;
|
37 | }
|
38 | Graphic.prototype.initialize = function (props) {
|
39 | _super.prototype.initialize.call(this, props);
|
40 | this.onDisabledChange();
|
41 | this.onActivableChange();
|
42 | };
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | Graphic.prototype.graphic = function (name, Item, props, stateProps, afterCallback) {
|
54 | if (stateProps === void 0) { stateProps = {}; }
|
55 | var item = this.graphics[name] ? this.graphics[name].item : new Item();
|
56 | if (typeof props === "function") {
|
57 | props = props(item);
|
58 | }
|
59 | if (!this.graphics[name]) {
|
60 | this.graphics[name] = {
|
61 | item: this.add(item),
|
62 | default: Object.assign({}, props)
|
63 | };
|
64 | }
|
65 | var graphic = this.graphics[name];
|
66 | if (props) {
|
67 | graphic.default = Object.assign({}, graphic.default, props);
|
68 | }
|
69 | if (stateProps) {
|
70 | Object.keys(stateProps).forEach(function (state) { return graphic[state] = Object.assign({}, graphic[state] || {}, stateProps[state]); });
|
71 | }
|
72 | this.updateGraphic(name);
|
73 | if (afterCallback) {
|
74 | afterCallback(graphic.item);
|
75 | }
|
76 | return this;
|
77 | };
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | Graphic.prototype.shape = function (name, props, stateProps, afterCallback) {
|
87 | if (stateProps === void 0) { stateProps = {}; }
|
88 | return this.graphic(name, Module_1.Shape, props, stateProps, afterCallback);
|
89 | };
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | Graphic.prototype.text = function (name, props, stateProps, afterCallback) {
|
99 | if (stateProps === void 0) { stateProps = {}; }
|
100 | return this.graphic(name, Module_1.Text, props, stateProps, afterCallback);
|
101 | };
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 | Graphic.prototype.removeGraphic = function (name) {
|
108 | var item = this.graphics[name];
|
109 | if (item) {
|
110 | item.item.kill();
|
111 | delete this.graphics[name];
|
112 | }
|
113 | return this;
|
114 | };
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | Graphic.prototype.updateGraphic = function (name) {
|
122 | var graphic = this.graphics[name], props = graphic && (this.state === Tool_1.Enum.STATE.DEFAULT ? graphic[this.state] : Object.assign({}, graphic.default, graphic[this.state]));
|
123 | if (props) {
|
124 | Object.keys(props).forEach(function (key) { return graphic.item.props[key] = props[key]; });
|
125 | }
|
126 | return this;
|
127 | };
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 | Graphic.prototype.getGraphicItem = function (name) {
|
134 | var graphic = this.graphics[name];
|
135 | return graphic && graphic.item;
|
136 | };
|
137 | |
138 |
|
139 |
|
140 |
|
141 | Graphic.prototype.setState = function (state) {
|
142 | this.state = state || Tool_1.Enum.STATE.DEFAULT;
|
143 | Object.keys(this.graphics).forEach(this.updateGraphic.bind(this));
|
144 | this.signals.stateChange.dispatch(this.state);
|
145 | };
|
146 | |
147 |
|
148 |
|
149 | Graphic.prototype.show = function (duration) {
|
150 | if (duration === void 0) { duration = 250; }
|
151 | this.addTransition("opacity", duration, {
|
152 | to: 1
|
153 | });
|
154 | };
|
155 | |
156 |
|
157 |
|
158 | Graphic.prototype.hide = function (duration) {
|
159 | if (duration === void 0) { duration = 250; }
|
160 | this.addTransition("opacity", duration, {
|
161 | to: 0
|
162 | });
|
163 | };
|
164 | |
165 |
|
166 |
|
167 | Graphic.prototype.toggle = function () {
|
168 | if (this.props.opacity > 0.5) {
|
169 | this.hide();
|
170 | }
|
171 | else {
|
172 | this.show();
|
173 | }
|
174 | };
|
175 | |
176 |
|
177 |
|
178 |
|
179 | Graphic.prototype.disable = function () {
|
180 | this.props.isActive = false;
|
181 | this.props.isDisabled = true;
|
182 | this.setState(Tool_1.Enum.STATE.DISABLED);
|
183 | return this;
|
184 | };
|
185 | |
186 |
|
187 |
|
188 |
|
189 | Graphic.prototype.active = function () {
|
190 | this.props.isDisabled = false;
|
191 | this.props.isActive = this.props.activable;
|
192 | if (this.props.activable) {
|
193 | this.setState(Tool_1.Enum.STATE.ACTIVE);
|
194 | }
|
195 | return this;
|
196 | };
|
197 |
|
198 | |
199 |
|
200 |
|
201 | Graphic.prototype.onDisabledChange = function () {
|
202 | if (this.props.isDisabled) {
|
203 | this.signals.hoverStart.remove(this._onHoverStartEvent.bind(this));
|
204 | this.signals.hoverEnd.remove(this._onHoverEndEvent.bind(this));
|
205 | this.disable();
|
206 | }
|
207 | else {
|
208 | this.signals.hoverStart.add(this._onHoverStartEvent.bind(this));
|
209 | this.signals.hoverEnd.add(this._onHoverEndEvent.bind(this));
|
210 | if (this.state === Tool_1.Enum.STATE.DISABLED) {
|
211 | this.setState(Tool_1.Enum.STATE.DEFAULT);
|
212 | }
|
213 | }
|
214 | };
|
215 | |
216 |
|
217 |
|
218 | Graphic.prototype.onActivableChange = function () {
|
219 | if (this.props.activable) {
|
220 | this.signals.click.add(this._onClickEvent.bind(this));
|
221 | }
|
222 | else {
|
223 | this.signals.click.remove(this._onClickEvent.bind(this));
|
224 | }
|
225 | };
|
226 | |
227 |
|
228 |
|
229 | Graphic.prototype._onHoverStartEvent = function () {
|
230 | if (!this.props.isDisabled && this.state !== Tool_1.Enum.STATE.ACTIVE) {
|
231 | this.setState(Tool_1.Enum.STATE.HOVER);
|
232 | }
|
233 | };
|
234 | |
235 |
|
236 |
|
237 | Graphic.prototype._onHoverEndEvent = function () {
|
238 | if (!this.props.isDisabled && this.state !== Tool_1.Enum.STATE.ACTIVE) {
|
239 | this.setState(Tool_1.Enum.STATE.DEFAULT);
|
240 | }
|
241 | };
|
242 | |
243 |
|
244 |
|
245 | Graphic.prototype._onClickEvent = function () {
|
246 | if (!this.props.isDisabled) {
|
247 | this.props.isActive = this.props.activable ? !this.props.isActive : false;
|
248 | this.setState(this.props.isActive ? Tool_1.Enum.STATE.ACTIVE : Tool_1.Enum.STATE.DEFAULT);
|
249 | }
|
250 | };
|
251 | return Graphic;
|
252 | }(Module_1.Module));
|
253 | exports.Graphic = Graphic;
|