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 | Graphic.prototype.graphic = function (name, Item, props, stateProps) {
|
53 | if (stateProps === void 0) { stateProps = {}; }
|
54 | var item = this.graphics[name] ? this.graphics[name].item : new Item();
|
55 | if (typeof props === "function") {
|
56 | props = props(item);
|
57 | }
|
58 | if (!this.graphics[name]) {
|
59 | this.graphics[name] = {
|
60 | item: this.add(item),
|
61 | default: Object.assign({}, props)
|
62 | };
|
63 | }
|
64 | var graphic = this.graphics[name];
|
65 | if (props) {
|
66 | graphic.default = Object.assign({}, graphic.default, props);
|
67 | }
|
68 | if (stateProps) {
|
69 | Object.keys(stateProps).forEach(function (state) { return graphic[state] = Object.assign({}, graphic[state] || {}, stateProps[state]); });
|
70 | }
|
71 | return this.updateGraphic(name);
|
72 | };
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | Graphic.prototype.shape = function (name, props, stateProps) {
|
81 | if (stateProps === void 0) { stateProps = {}; }
|
82 | return this.graphic(name, Module_1.Shape, props, stateProps);
|
83 | };
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | Graphic.prototype.text = function (name, props, stateProps) {
|
92 | if (stateProps === void 0) { stateProps = {}; }
|
93 | return this.graphic(name, Module_1.Text, props, stateProps);
|
94 | };
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 | Graphic.prototype.removeGraphic = function (name) {
|
101 | var item = this.graphics[name];
|
102 | if (item) {
|
103 | item.item.kill();
|
104 | delete this.graphics[name];
|
105 | }
|
106 | return this;
|
107 | };
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | Graphic.prototype.updateGraphic = function (name) {
|
115 | var graphic = this.graphics[name], props = graphic && (this.state === Tool_1.Enum.STATE.DEFAULT ? graphic[this.state] : Object.assign({}, graphic.default, graphic[this.state]));
|
116 | if (props) {
|
117 | Object.keys(props).forEach(function (key) { return graphic.item.props[key] = props[key]; });
|
118 | }
|
119 | return this;
|
120 | };
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 | Graphic.prototype.getGraphicItem = function (name) {
|
127 | var graphic = this.graphics[name];
|
128 | return graphic && graphic.item;
|
129 | };
|
130 | |
131 |
|
132 |
|
133 |
|
134 | Graphic.prototype.setState = function (state) {
|
135 | this.state = state || Tool_1.Enum.STATE.DEFAULT;
|
136 | Object.keys(this.graphics).forEach(this.updateGraphic.bind(this));
|
137 | this.signals.stateChange.dispatch(this.state);
|
138 | };
|
139 | |
140 |
|
141 |
|
142 | Graphic.prototype.show = function (duration) {
|
143 | if (duration === void 0) { duration = 250; }
|
144 | this.addTransition("opacity", duration, {
|
145 | to: 1
|
146 | });
|
147 | };
|
148 | |
149 |
|
150 |
|
151 | Graphic.prototype.hide = function (duration) {
|
152 | if (duration === void 0) { duration = 250; }
|
153 | this.addTransition("opacity", duration, {
|
154 | to: 0
|
155 | });
|
156 | };
|
157 | |
158 |
|
159 |
|
160 | Graphic.prototype.toggle = function () {
|
161 | if (this.props.opacity > 0.5) {
|
162 | this.hide();
|
163 | }
|
164 | else {
|
165 | this.show();
|
166 | }
|
167 | };
|
168 | |
169 |
|
170 |
|
171 |
|
172 | Graphic.prototype.disable = function () {
|
173 | this.props.isActive = false;
|
174 | this.props.isDisabled = true;
|
175 | this.setState(Tool_1.Enum.STATE.DISABLED);
|
176 | return this;
|
177 | };
|
178 | |
179 |
|
180 |
|
181 |
|
182 | Graphic.prototype.active = function () {
|
183 | this.props.isDisabled = false;
|
184 | this.props.isActive = this.props.activable;
|
185 | if (this.props.activable) {
|
186 | this.setState(Tool_1.Enum.STATE.ACTIVE);
|
187 | }
|
188 | return this;
|
189 | };
|
190 |
|
191 | |
192 |
|
193 |
|
194 | Graphic.prototype.onDisabledChange = function () {
|
195 | if (this.props.isDisabled) {
|
196 | this.signals.hoverStart.remove(this._onHoverStartEvent.bind(this));
|
197 | this.signals.hoverEnd.remove(this._onHoverEndEvent.bind(this));
|
198 | this.disable();
|
199 | }
|
200 | else {
|
201 | this.signals.hoverStart.add(this._onHoverStartEvent.bind(this));
|
202 | this.signals.hoverEnd.add(this._onHoverEndEvent.bind(this));
|
203 | if (this.state === Tool_1.Enum.STATE.DISABLED) {
|
204 | this.setState(Tool_1.Enum.STATE.DEFAULT);
|
205 | }
|
206 | }
|
207 | };
|
208 | |
209 |
|
210 |
|
211 | Graphic.prototype.onActivableChange = function () {
|
212 | if (this.props.activable) {
|
213 | this.signals.click.add(this._onClickEvent.bind(this));
|
214 | }
|
215 | else {
|
216 | this.signals.click.remove(this._onClickEvent.bind(this));
|
217 | }
|
218 | };
|
219 | |
220 |
|
221 |
|
222 | Graphic.prototype._onHoverStartEvent = function () {
|
223 | if (!this.props.isDisabled && this.state !== Tool_1.Enum.STATE.ACTIVE) {
|
224 | this.setState(Tool_1.Enum.STATE.HOVER);
|
225 | }
|
226 | };
|
227 | |
228 |
|
229 |
|
230 | Graphic.prototype._onHoverEndEvent = function () {
|
231 | if (!this.props.isDisabled && this.state !== Tool_1.Enum.STATE.ACTIVE) {
|
232 | this.setState(Tool_1.Enum.STATE.DEFAULT);
|
233 | }
|
234 | };
|
235 | |
236 |
|
237 |
|
238 | Graphic.prototype._onClickEvent = function () {
|
239 | if (!this.props.isDisabled) {
|
240 | this.props.isActive = this.props.activable ? !this.props.isActive : false;
|
241 | this.setState(this.props.isActive ? Tool_1.Enum.STATE.ACTIVE : Tool_1.Enum.STATE.DEFAULT);
|
242 | }
|
243 | };
|
244 | return Graphic;
|
245 | }(Module_1.Module));
|
246 | exports.Graphic = Graphic;
|