UNPKG

9.02 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 SideralObject_1 = require("./../SideralObject");
17var Graphic = (function (_super) {
18 __extends(Graphic, _super);
19 /* LIFECYCLE */
20 function Graphic() {
21 var _this = _super.call(this) || this;
22 /**
23 * Graphics object
24 */
25 _this.graphics = {};
26 /**
27 * State of the graphic
28 */
29 _this.state = Tool_1.Enum.STATE.DEFAULT;
30 /**
31 * The PIXI Container of a Graphics
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 /* METHODS */
45 /**
46 * Add a new graphic chaine
47 * @param name - Name of the graphic
48 * @param Item - Class of the item to be instanciated if the graphic doesnot exists
49 * @param props - Properties to pass to the graphic
50 * @param stateProps - Properties to pass to the graphic relative to the state of the current object
51 * @param afterCallback - Function to be called after the item was created
52 * @return Current instance
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 * Helper to create a shape as a graphic
81 * @param name - Name of the shape
82 * @param props - Properties to pass to the shape
83 * @param stateProps - Properties to pass to the shape relative to the state of the current object
84 * @param afterCallback - Function to be called after the item was created
85 * @return Current instace
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 * Helper to create a Text as a graphic
93 * @param name - Name of the text
94 * @param props - Properties to pass to the text
95 * @param stateProps - Properties to pass to the text relative to the state of the current object
96 * @param afterCallback - Function to be called after the item was created
97 * @return Current instance
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 * Remove a graphic object by its name
105 * @param name - Name of the graphic to remove
106 * @returns Current instance
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 * Update the properties of a graphic object
118 * @param name - Name of the graphic to update
119 * @param drawDefaultBefore - If true, it will draw the default theme if the state is different
120 * @returns Current instance
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 * Get the graphic item by its name
131 * @param name - Name of the graphic to get
132 * @return The graphic item
133 */
134 Graphic.prototype.getGraphicItem = function (name) {
135 var graphic = this.graphics[name];
136 return graphic && graphic.item;
137 };
138 /**
139 * Change the state of the graphic
140 * @param state - The next state of the graphic
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 * Show the graphic
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 * Hide the graphic
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 * Toggle the graphic
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 * Disable the graphic
178 * @returns Current instance
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 * Active the graphic
188 * @returns Current instance
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 /* EVENTS */
199 /**
200 * When "disabled" property has changed
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 * When "activable" property has changed
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 * Event fired on hover
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 * Event fired after hover
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 * Event fired on click
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));
254exports.Graphic = Graphic;