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