UNPKG

8.7 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 * @return Current instance
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 * Helper to create a shape as a graphic
75 * @param name - Name of the shape
76 * @param props - Properties to pass to the shape
77 * @param stateProps - Properties to pass to the shape relative to the state of the current object
78 * @return Current instace
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 * Helper to create a Text as a graphic
86 * @param name - Name of the text
87 * @param props - Properties to pass to the text
88 * @param stateProps - Properties to pass to the text relative to the state of the current object
89 * @return Current instance
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 * Remove a graphic object by its name
97 * @param name - Name of the graphic to remove
98 * @returns Current instance
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 * Update the properties of a graphic object
110 * @param name - Name of the graphic to update
111 * @param drawDefaultBefore - If true, it will draw the default theme if the state is different
112 * @returns Current instance
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 * Get the graphic item by its name
123 * @param name - Name of the graphic to get
124 * @return The graphic item
125 */
126 Graphic.prototype.getGraphicItem = function (name) {
127 var graphic = this.graphics[name];
128 return graphic && graphic.item;
129 };
130 /**
131 * Change the state of the graphic
132 * @param state - The next state of the graphic
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 * Show the graphic
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 * Hide the graphic
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 * Toggle the graphic
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 * Disable the graphic
170 * @returns Current instance
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 * Active the graphic
180 * @returns Current instance
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 /* EVENTS */
191 /**
192 * When "disabled" property has changed
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 * When "activable" property has changed
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 * Event fired on hover
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 * Event fired after hover
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 * Event fired on click
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));
246exports.Graphic = Graphic;