UNPKG

9.1 kBJavaScriptView Raw
1"use strict";
2var __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})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var Module_1 = require("./../Module");
14var Tool_1 = require("./../Tool");
15var index_1 = require("./../index");
16var Graphic = (function (_super) {
17 __extends(Graphic, _super);
18 /* LIFECYCLE */
19 function Graphic() {
20 var _this = _super.call(this) || this;
21 /**
22 * Graphics object
23 */
24 _this.graphics = {};
25 /**
26 * State of the graphic
27 */
28 _this.state = Tool_1.Enum.STATE.DEFAULT;
29 /**
30 * The PIXI Container of a Graphics
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 /* METHODS */
44 /**
45 * Add a new graphic chaine
46 * @param name - Name of the graphic
47 * @param Item - Class of the item to be instanciated if the graphic doesnot exists
48 * @param props - Properties to pass to the graphic
49 * @param stateProps - Properties to pass to the graphic relative to the state of the current object
50 * @param afterCallback - Function to be called after the item was created
51 * @return Current instance
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 * Helper to create a shape as a graphic
80 * @param name - Name of the shape
81 * @param props - Properties to pass to the shape
82 * @param stateProps - Properties to pass to the shape relative to the state of the current object
83 * @param afterCallback - Function to be called after the item was created
84 * @return Current instace
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 * Helper to create a Text as a graphic
92 * @param name - Name of the text
93 * @param props - Properties to pass to the text
94 * @param stateProps - Properties to pass to the text relative to the state of the current object
95 * @param afterCallback - Function to be called after the item was created
96 * @return Current instance
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 * Remove a graphic object by its name
104 * @param name - Name of the graphic to remove
105 * @returns Current instance
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 * Update the properties of a graphic object
117 * @param name - Name of the graphic to update
118 * @param drawDefaultBefore - If true, it will draw the default theme if the state is different
119 * @returns Current instance
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 * Get the graphic item by its name
130 * @param name - Name of the graphic to get
131 * @return The graphic item
132 */
133 Graphic.prototype.getGraphicItem = function (name) {
134 var graphic = this.graphics[name];
135 return graphic && graphic.item;
136 };
137 /**
138 * Change the state of the graphic
139 * @param state - The next state of the graphic
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 * Show the graphic
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 * Hide the graphic
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 * Toggle the graphic
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 * Disable the graphic
177 * @returns Current instance
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 * Active the graphic
187 * @returns Current instance
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 /* EVENTS */
198 /**
199 * When "disabled" property has changed
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 * When "activable" property has changed
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 * Event fired on hover
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 * Event fired after hover
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 * Event fired on click
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));
253exports.Graphic = Graphic;