@urso/core 0.4.17 → 0.4.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urso/core",
3
- "version": "0.4.17",
3
+ "version": "0.4.18",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
@@ -7,19 +7,15 @@ class ModulesObjectsModelsSlider extends Urso.Core.Modules.Objects.BaseModel {
7
7
  this._sliderHandle = null;
8
8
  this._baseObject = null;
9
9
  this._handleIsPulling = false;
10
-
11
- this._setVariables();
10
+ this._points = [];
12
11
  this._addBaseObject();
13
- this._createSliderTextures();
14
- this._createValueText();
15
- this._setDefaultValue();
16
12
  }
17
13
 
18
14
  setupParams(params) {
19
15
  super.setupParams(params);
20
16
  this.contents = [];
21
17
  this.points = Urso.helper.recursiveGet('points', params, [0, 1]);
22
- this.defaultValue = Urso.helper.recursiveGet('defaultValue', params, this.points[0]);
18
+ this.defaultValue = Urso.helper.recursiveGet('defaultValue', params, false);
23
19
  this.bgTexture = Urso.helper.recursiveGet('bgTexture', params, false);
24
20
  this.fillTexture = Urso.helper.recursiveGet('fillTexture', params, false);
25
21
  this.handleTexture = Urso.helper.recursiveGet('handleTexture', params, false);
@@ -30,19 +26,19 @@ class ModulesObjectsModelsSlider extends Urso.Core.Modules.Objects.BaseModel {
30
26
  }
31
27
 
32
28
  _setVariables() {
33
- if(this.isVertical) {
29
+ if (this.isVertical) {
34
30
  this.positionKey = 'y';
35
- this.targetObjParam = 'height';
36
- }else {
31
+ this.sizeKey = 'height';
32
+ } else {
37
33
  this.positionKey = 'x';
38
- this.targetObjParam = 'width';
34
+ this.sizeKey = 'width';
39
35
  }
40
36
  }
41
37
 
42
38
  _createSliderTextures() {
43
39
  this._sliderBg = this._createTexture(this.bgTexture);
44
40
 
45
- if(this.fillTexture)
41
+ if (this.fillTexture)
46
42
  this._fillTexture = this._createFillTexture(this.fillTexture);
47
43
 
48
44
  this._sliderHandle = this._createTexture(this.handleTexture);
@@ -51,13 +47,13 @@ class ModulesObjectsModelsSlider extends Urso.Core.Modules.Objects.BaseModel {
51
47
  this._setEvents(this._sliderHandle._baseObject);
52
48
  }
53
49
 
54
- _createFillTexture(model){
50
+ _createFillTexture(model) {
55
51
  const fillTexture = this._createTexture(model);
56
52
  const { width, height } = fillTexture._baseObject;
57
53
  this._fillMask = Urso.objects.create({
58
54
  type: Urso.types.objects.GRAPHICS,
59
55
  figure: {
60
- rectangle: [0, 0, width, height]
56
+ rectangle: [0, 0, width, height]
61
57
  },
62
58
  x: -width * fillTexture.anchorX,
63
59
  y: -height * fillTexture.anchorY,
@@ -70,15 +66,15 @@ class ModulesObjectsModelsSlider extends Urso.Core.Modules.Objects.BaseModel {
70
66
  _createValueText() {
71
67
  if (this.minValueTextModel) {
72
68
  this.minValueText = Urso.objects.create(this.minValueTextModel, this);
73
- this.minValueText.text = this.points[0];
69
+ this.minValueText.text = this._points[0];
74
70
  }
75
71
 
76
72
  if (this.maxValueTextModel) {
77
73
  this.maxValueText = Urso.objects.create(this.maxValueTextModel, this);
78
- this.maxValueText.text = this.points.length <= 2 ? '100' : this.points[this.points.length - 1];
74
+ this.maxValueText.text = this._points.length <= 2 ? '100' : this._points[this._points.length - 1];
79
75
  }
80
76
 
81
- if(this.currentValueTextModel){
77
+ if (this.currentValueTextModel) {
82
78
  this.currentValueText = Urso.objects.create(this.currentValueTextModel, this);
83
79
  }
84
80
  }
@@ -118,6 +114,12 @@ class ModulesObjectsModelsSlider extends Urso.Core.Modules.Objects.BaseModel {
118
114
 
119
115
  _addBaseObject() {
120
116
  this._baseObject = new PIXI.Container();
117
+
118
+ this._setPoints();
119
+ this._setVariables();
120
+ this._createSliderTextures();
121
+ this._createValueText();
122
+ this._setDefaultValue();
121
123
  };
122
124
 
123
125
  _onPointerDown(obj) {
@@ -130,15 +132,23 @@ class ModulesObjectsModelsSlider extends Urso.Core.Modules.Objects.BaseModel {
130
132
  return
131
133
 
132
134
  const value = this.isVertical ? y : x;
135
+ const globalPosition = this.toGlobal(this[this.positionKey])[this.positionKey];
133
136
 
134
- if (value < this[this.positionKey])
137
+ if (value < globalPosition)
135
138
  this._sliderHandle[this.positionKey] = 0;
136
- else if (value >= this[this.positionKey] + this._sliderBg._baseObject[this.targetObjParam])
137
- this._sliderHandle[this.positionKey] = this._sliderBg._baseObject[this.targetObjParam];
139
+ else if (value >= globalPosition + this._sliderBg._baseObject[this.sizeKey])
140
+ this._sliderHandle[this.positionKey] = this._sliderBg._baseObject[this.sizeKey];
138
141
  else
139
- this._sliderHandle[this.positionKey] = value - this[this.positionKey];
142
+ this._sliderHandle[this.positionKey] = value - globalPosition
140
143
 
141
- this._setFillMask();
144
+ this._updateValueOnMove()
145
+ }
146
+
147
+ _updateValueOnMove() {
148
+ const { value } = this._calculateClosestPoint(this._sliderHandle);
149
+
150
+ if (this.currentValueText)
151
+ this.currentValueText.text = value;
142
152
 
143
153
  const data = { class: this.class, name: this.name, position: this._sliderHandle[this.positionKey] };
144
154
  this.emit(Urso.events.MODULES_OBJECTS_SLIDER_HANDLE_MOVE, data)
@@ -153,62 +163,82 @@ class ModulesObjectsModelsSlider extends Urso.Core.Modules.Objects.BaseModel {
153
163
  else
154
164
  targetObj = this._sliderHandle;
155
165
 
156
- const {x, y} = targetObj;
157
- const value = this.isVertical ? y : x;
158
- this._dropHandle(value);
166
+ const { coord, value } = this._calculateClosestPoint(targetObj);
167
+ this._dropHandle(coord, value);
168
+ }
169
+
170
+ _setPoints() {
171
+ if (this.points.length > 1) {
172
+ this._points = [...this.points];
173
+ return;
174
+ }
175
+
176
+ const firstPoint = this.points[0] > 0 ? 0 : this.points[0];
177
+ const lastPoint = this.points[0] > 0 ? this.points[0] : 0;
178
+
179
+ for (let i = firstPoint; i <= lastPoint; i++)
180
+ this._points.push(i);
159
181
  }
160
182
 
161
- _setDefaultValue(){
162
- if(!this.points.includes(this.defaultValue))
163
- this.defaultValue = this.points[0];
183
+ _setDefaultValue() {
184
+ if(!this.defaultValue)
185
+ this.defaultValue = this._points[0];
164
186
 
165
- let value = this.points.indexOf(this.defaultValue) *
166
- this._sliderBg._baseObject[this.targetObjParam] / (this.points.length - 1);
187
+ if (!this._points.includes(this.defaultValue))
188
+ this.defaultValue = this._points[0];
189
+
190
+ let value = this._points.indexOf(this.defaultValue) *
191
+ this._sliderBg._baseObject[this.sizeKey] / (this._points.length - 1);
167
192
 
168
193
  this._setNewValue(value, this.defaultValue);
169
194
  }
170
195
 
171
- _dropHandle(givenValue) {
172
- let value;
173
- let coord;
196
+ _dropHandle(coord, value) {
197
+ const data = { class: this.class, name: this.name, position: coord, value: value };
198
+
199
+ this.emit(Urso.events.MODULES_OBJECTS_SLIDER_HANDLE_DROP, data);
200
+ this._setNewValue(coord, value);
201
+ }
202
+
203
+ _calculateClosestPoint({ x, y }) {
204
+ const givenValue = this.isVertical ? y : x;
205
+ let value, coord;
174
206
 
175
- if(this.points.length <= 2){
207
+ if (this._points.length <= 2) {
176
208
  coord = givenValue;
177
- value = ~~(100 / this._sliderBg._baseObject[this.targetObjParam] * givenValue);
209
+ value = ~~(100 / this._sliderBg._baseObject[this.sizeKey] * givenValue);
178
210
  }
179
211
  // calculate closest point
180
- else{
181
- for (let i = 0; i < this.points.length; i++) {
182
- let pointCoord = i * this._sliderBg._baseObject[this.targetObjParam] / (this.points.length - 1);
183
-
212
+ else {
213
+ for (let i = 0; i < this._points.length; i++) {
214
+ let pointCoord = i * this._sliderBg._baseObject[this.sizeKey] / (this._points.length - 1);
215
+
184
216
  if (typeof (coord) === 'number' && givenValue - pointCoord < coord - givenValue) {
185
217
  coord = coord;
186
218
  } else {
187
219
  coord = pointCoord;
188
- value = this.points[i];
220
+ value = this._points[i];
189
221
  }
190
222
  }
191
223
  }
192
- const data = { class: this.class, name: this.name, position: coord, value: value };
193
-
194
- this.emit(Urso.events.MODULES_OBJECTS_SLIDER_HANDLE_DROP, data);
195
- this._setNewValue(coord, value);
224
+
225
+ return { coord, value };
196
226
  }
197
227
 
198
- _setFillMask(){
199
- if(!this._fillMask)
228
+ _setFillMask() {
229
+ if (!this._fillMask)
200
230
  return
201
231
 
202
- const progress = (this._sliderHandle[this.positionKey] - this._sliderBg[this.positionKey]) *
203
- 100 / this._fillTexture._baseObject[this.targetObjParam] * 0.01;
204
-
232
+ const progress = (this._sliderHandle[this.positionKey] - this._sliderBg[this.positionKey]) *
233
+ 100 / this._fillTexture._baseObject[this.sizeKey] * 0.01;
234
+
205
235
  const scaleKey = this.isVertical ? 'scaleY' : 'scaleX';
206
236
  this._fillMask[scaleKey] = progress;
207
237
  }
208
238
 
209
- _setNewValue(coord, value){
239
+ _setNewValue(coord, value) {
210
240
  this._sliderHandle[this.positionKey] = coord;
211
-
241
+
212
242
  if (this.currentValueText)
213
243
  this.currentValueText.text = value;
214
244