-
Notifications
You must be signed in to change notification settings - Fork 68
/
Slider.js
1536 lines (1384 loc) · 38.2 KB
/
Slider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Slider Stimulus.
*
* @author Alain Pitiot
* @copyright (c) 2017-2020 Ilixa Ltd. (http://ilixa.com) (c) 2020-2024 Open Science Tools Ltd. (https://opensciencetools.org)
* @license Distributed under the terms of the MIT License
*/
import * as PIXI from "pixi.js-legacy";
import { PsychoJS } from "../core/PsychoJS.js";
import { WindowMixin } from "../core/WindowMixin.js";
import { Clock } from "../util/Clock.js";
import { Color } from "../util/Color.js";
import { ColorMixin } from "../util/ColorMixin.js";
import { to_pixiPoint } from "../util/Pixi.js";
import * as util from "../util/Util.js";
import { VisualStim } from "./VisualStim.js";
/**
* Slider stimulus.
*
* @extends module:visual.VisualStim
* @mixes module:util.ColorMixin
*
* @todo check that parameters are valid, e.g. ticks are an array of numbers, etc.
* @todo readOnly
* @todo complete setters, for instance setTicks should change this._isCategorical
* @todo flesh out the skin approach
*/
export class Slider extends util.mix(VisualStim).with(ColorMixin, WindowMixin)
{
/**
* @memberOf module:visual
* @param {Object} options
* @param {String} options.name - the name used when logging messages from this stimulus
* @param {module:core.Window} options.win - the associated Window
* @param {number[]} [options.pos= [0, 0]] - the position of the center of the slider
* @param {number[]} options.size - the size of the slider, e.g. [1, 0.1] for an horizontal slider
* @param {number} [options.ori = 0.0] - the orientation (in degrees)
* @param {string} [options.units= 'height'] - the units of the Slider position, and font size
*
* @param {Color} [options.color= Color('LightGray')] the color of the slider
* @param {number} [options.contrast= 1.0] - the contrast of the slider
* @param {number} [options.opacity= 1.0] - the opacity of the slider
*
* @param {string} [options.style= [Slider.Style.RATING]] - the slider style
* @param {number[]} [options.ticks= [1,2,3,4,5]] - the array of ticks
* @param {number[]} [options.labels= []] - the array of labels
* @param {number} [options.granularity= 0] - the granularity
* @param {boolean} [options.flip= false] - whether or not to flip the position of the marker, ticks,
* and labels with respect to the central bar
* @param {boolean} [options.readOnly= false] - whether or not the slider is read only
*
* @param {string} [options.font= 'Arial'] - the text font
* @param {boolean} [options.bold= true] - whether or not the font of the labels is bold
* @param {boolean} [options.italic= false] - whether or not the font of the labels is italic
* @param {number} [options.fontSize] - the font size of the labels (in pixels), the default fontSize depends on the
* Slider's units: 14 for 'pix', 0.03 otherwise
*
* @param {boolean} [options.compact= false] - whether or not the slider is compact, i.e. whether all graphical
* elements (e.g. labels) fit within its size
*
* @param {PIXI.Graphics} options.clipMask - the clip mask
* @param {boolean} [options.autoDraw= false] - whether or not the stimulus should be automatically drawn on every
* frame flip
* @param {boolean} [options.autoLog= false] - whether or not to log
*
* @param {core.MinimalStim[]} [options.dependentStims = [] ] - the list of dependent stimuli,
* which must be updated when this Slider is updated, e.g. a Form.
*/
constructor(
{
name,
win,
pos,
size,
ori,
units,
color,
markerColor,
lineColor,
contrast,
opacity,
depth,
style,
ticks,
labels,
startValue,
granularity,
flip,
readOnly,
font,
bold,
italic,
fontSize,
compact,
clipMask,
autoDraw,
autoLog,
dependentStims,
} = {},
)
{
super({ name, win, units, ori, opacity, depth, pos, size, clipMask, autoDraw, autoLog });
this._needMarkerUpdate = false;
// slider skin:
this._skin = {};
Object.assign(this._skin, Slider.Skin);
// callback to deal with input sanitising:
const onChange = (withPixi = false, withBoundingBox = false, withSanitize = false) =>
{
const visualOnChange = this._onChange(withPixi, withBoundingBox);
return () =>
{
if (withSanitize)
{
this._sanitizeAttributes();
}
visualOnChange();
};
};
this._addAttribute(
"style",
style,
[Slider.Style.RATING],
onChange(true, true, true),
);
this._addAttribute(
"ticks",
ticks,
[1, 2, 3, 4, 5],
onChange(true, true, true),
);
this._addAttribute(
"labels",
labels,
[],
onChange(true, true, true),
);
this._addAttribute(
"startValue",
startValue,
undefined
);
this._addAttribute(
"granularity",
granularity,
0,
this._onChange(false, false),
);
this._addAttribute(
"readOnly",
readOnly,
false,
);
this._addAttribute(
"compact",
compact,
false,
this._onChange(true, true),
);
// font:
this._addAttribute(
"font",
font,
"Arial",
this._onChange(true, true),
);
this._addAttribute(
"fontSize",
fontSize,
(this._units === "pix") ? 14 : 0.03,
this._onChange(true, true),
);
this._addAttribute(
"bold",
bold,
true,
this._onChange(true, true),
);
this._addAttribute(
"italic",
italic,
false,
this._onChange(true, true),
);
this._addAttribute(
"flip",
flip,
false,
this._onChange(true, true),
);
// color:
this._addAttribute(
"color",
color,
"lightgray",
this._onChange(true, false),
);
this._addAttribute(
"lineColor",
lineColor,
"lightgray",
this._onChange(true, false),
);
this._addAttribute(
"markerColor",
markerColor,
"red",
this._onChange(true, false),
);
this._addAttribute(
"contrast",
contrast,
1.0,
this._onChange(true, false),
);
this._addAttribute(
"dependentStims",
dependentStims,
[],
this._onChange(false, false),
);
// slider rating (which might be different from the visible marker rating):
this._addAttribute("rating", undefined);
// visible marker rating (which might be different from the actual rating):
this._addAttribute("markerPos", undefined);
// full history of ratings and response times:
this._addAttribute("history", []);
// various graphical components:
this._addAttribute("lineAspectRatio", 0.01);
// check for attribute conflicts, missing values, etc.:
this._sanitizeAttributes();
// estimate the bounding box:
this._estimateBoundingBox();
// the internal response clock, used to time the marker change events:
this._responseClock = new Clock();
this._pixiLabels = [];
if (this._autoLog)
{
this._psychoJS.experimentLogger.exp(`Created ${this.name} = ${this.toString()}`);
}
this._handlePointerDownBinded = this._handlePointerDown.bind(this);
this._handlePointerUpBinded = this._handlePointerUp.bind(this);
this._handlePointerMoveBinded = this._handlePointerMove.bind(this);
}
/**
* Force a refresh of the stimulus.
*/
refresh()
{
super.refresh();
this._needMarkerUpdate = true;
}
/**
* Reset the slider.
*/
reset()
{
this.psychoJS.logger.debug("reset Slider: ", this._name);
this._markerPos = undefined;
this._history = [];
this._rating = undefined;
this._responseClock.reset();
this.status = PsychoJS.Status.NOT_STARTED;
this._needPixiUpdate = true;
this._needUpdate = true;
// the marker should be invisible when markerPos is undefined:
if (typeof this._marker !== "undefined")
{
this._marker.alpha = 0;
}
}
/**
* Query whether or not the marker is currently being dragged.
*
* @returns {boolean} whether or not the marker is being dragged
*/
isMarkerDragging()
{
return this._markerDragging;
}
/**
* Get the current value of the rating.
*
* @returns {number | undefined} the rating or undefined if there is none
*/
getRating()
{
const historyLength = this._history.length;
if (historyLength > 0)
{
return this._history[historyLength - 1].rating;
}
else
{
return undefined;
}
}
/**
* Get the response time of the most recent change to the rating.
*
* @returns {number | undefined} the response time (in second) or undefined if there is none
*/
getRT()
{
const historyLength = this._history.length;
if (historyLength > 0)
{
return this._history[historyLength - 1].responseTime;
}
else
{
return undefined;
}
}
/**
* Setter for the readOnly attribute.
*
* <p>Read-only sliders are half-opaque and do not provide responses.</p>
*
* @param {boolean} [readOnly= true] - whether or not the slider is read-only
* @param {boolean} [log= false] - whether of not to log
*/
setReadOnly(readOnly = true, log = false)
{
const hasChanged = this._setAttribute("readOnly", readOnly, log);
if (hasChanged)
{
// halve the opacity:
if (readOnly)
{
this._opacity /= 2.0;
}
else
{
this._opacity *= 2.0;
}
this._needUpdate = true;
}
}
/**
* Setter for the markerPos attribute.
*
* <p>Setting markerPos changes the visible position of the marker to the specified rating
* but does not change the actual rating returned by the slider.</p>
*
* @param {number} displayedRating - the displayed rating
* @param {boolean} [log= false] - whether of not to log
*/
setMarkerPos(displayedRating, log = false)
{
const previousMarkerPos = this._markerPos;
this._markerPos = this._granularise(displayedRating);
// if the displayed rating has changed, we need to update the pixi representation:
if (previousMarkerPos !== this._markerPos)
{
this._needMarkerUpdate = true;
this._needUpdate = true;
}
}
/**
* Setter for the rating attribute.
*
* <p>Setting the rating does not change the visible position of the marker.</p>
*
* @param {number} rating - the rating
* @param {boolean} [log= false] - whether of not to log
*/
setRating(rating, log = false)
{
rating = this._granularise(rating);
this._markerPos = rating;
if (this._isCategorical)
{
rating = this._labels[Math.round(rating)];
}
this._setAttribute("rating", rating, log);
}
/**
* Setter for the orientation attribute.
*
* @param {number} ori - the orientation in degree with 0 as the vertical position, positive values rotate clockwise.
* @param {boolean} [log= false] - whether of not to log
*/
setOri (ori = 0, log = false)
{
const oriChanged = this._setAttribute("ori", ori, log);
if (oriChanged)
{
this._pixi.rotation = -this._ori * Math.PI / 180;
let i;
for (i = 0; i < this._pixiLabels.length; ++i)
{
this._pixiLabels[i].rotation = -(this._ori + this._labelOri) * Math.PI / 180;
}
}
}
/**
* Setter for the anchor attribute.
*
* @param {string} anchor - anchor of the stim
* @param {boolean} [log= false] - whether or not to log
*/
setAnchor (anchor = "center", log = false)
{
this._setAttribute("anchor", anchor, log);
if (this._pixi !== undefined)
{
// container has origin at [0, 0], subtracting 0.5 from anchorNum vals to get a desired effect.
const anchorNum = this._anchorTextToNum(this._anchor);
this._pixi.pivot.x = (anchorNum[0] - 0.5) * this._pixi.scale.x * this._pixi.width;
this._pixi.pivot.y = (anchorNum[1] - 0.5) * this._pixi.scale.y * this._pixi.height;
}
}
/** Let `borderColor` alias `lineColor` to parallel PsychoPy */
set borderColor(color)
{
this.lineColor = color;
}
setBorderColor(color)
{
this.setLineColor(color);
}
get borderColor()
{
return this.lineColor;
}
getBorderColor()
{
return this.getLineColor();
}
/** Let `fillColor` alias `markerColor` to parallel PsychoPy */
set fillColor(color)
{
this.markerColor = color;
}
setFillColor(color)
{
this.setMarkerColor(color);
}
get fillColor()
{
return this.markerColor;
}
getFillColor()
{
return this.getMarkerColor();
}
/**
* Estimate the bounding box.
*
* @note this method calculates the position of the labels, since that is necessary to the estimation of
* the bounding box.
*
* @override
* @protected
*/
_estimateBoundingBox()
{
// setup the slider's style (taking into account the Window dimension, etc.):
this._setupStyle();
// calculate various values in pixel units:
this._tickSize_px = util.to_px(this._tickSize, this._units, this._win);
this._fontSize_px = this._getLengthPix(this._fontSize);
this._barSize_px = util.to_px(this._barSize, this._units, this._win, true).map((v) => Math.max(1, v));
this._markerSize_px = util.to_px(this._markerSize, this._units, this._win, true);
const pos_px = util.to_px(this._pos, this._units, this._win);
const size_px = util.to_px(this._size, this._units, this._win);
// calculate the position of the ticks:
const tickPositions = this._ratingToPos(this._ticks);
this._tickPositions_px = tickPositions.map((p) => util.to_px(p, this._units, this._win));
// left, top, right, bottom limits:
const limits_px = [0, 0, size_px[0], size_px[1]];
// Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY
// estimate the position of the labels:
this._labelPositions_px = new Array(this._labels.length);
const labelTextStyle = this._getTextStyle();
let prevLabelBounds = null;
let prevNonOverlapOffset = 0;
const tolerance = 10;
for (let l = 0; l < this._labels.length; ++l)
{
const tickPositionIndex = Math.round(l / (this._labels.length - 1) * (this._ticks.length - 1));
this._labelPositions_px[l] = this._tickPositions_px[tickPositionIndex];
const labelBounds = PIXI.TextMetrics.measureText(this._labels[l].toString(), labelTextStyle);
// horizontal slider:
if (this._isHorizontal())
{
if (this._flip)
{
this._labelPositions_px[l][1] -= labelBounds.height + this._tickSize_px[1];
}
else
{
this._labelPositions_px[l][1] += this._tickSize_px[1];
}
if (this._style.indexOf(Slider.Style.LABELS_45) === -1)
{
this._labelPositions_px[l][0] -= labelBounds.width / 2;
if (this._compact)
{
this._labelPositions_px[l][0] = Math.min(size_px[0] / 2 - labelBounds.width, Math.max(-size_px[0] / 2, this._labelPositions_px[l][0]));
}
// ensure that that labels are not overlapping:
if (
prevLabelBounds
&& (this._labelPositions_px[l - 1][0] + prevLabelBounds.width + tolerance >= this._labelPositions_px[l][0])
)
{
if (prevNonOverlapOffset === 0)
{
prevNonOverlapOffset = prevLabelBounds.height;
this._labelPositions_px[l][1] += prevNonOverlapOffset;
}
else
{
prevNonOverlapOffset = 0;
}
}
prevLabelBounds = labelBounds;
}
}
// vertical slider:
else
{
this._labelPositions_px[l][1] -= labelBounds.height / 2;
if (this._compact)
{
this._labelPositions_px[l][1] = Math.min(size_px[1] / 2 - labelBounds.width, Math.max(-size_px[1] / 2, this._labelPositions_px[l][1]));
}
if (this._flip)
{
this._labelPositions_px[l][0] += this._tickSize_px[0] * 2;
}
else if (this._labelOri === 0)
{
this._labelPositions_px[l][0] -= labelBounds.width + this._tickSize_px[0] * 2;
}
else
{
this._labelPositions_px[l][0] -= this._tickSize_px[0];
}
}
// update limits:
limits_px[0] = Math.min(limits_px[0], this._labelPositions_px[l][0]);
limits_px[1] = Math.min(limits_px[1], this._labelPositions_px[l][1]);
limits_px[2] = Math.max(limits_px[2], this._labelPositions_px[l][0] + labelBounds.width);
limits_px[3] = Math.max(limits_px[3], this._labelPositions_px[l][1] + labelBounds.height);
}
// adjust the limits by taking into account the ticks:
if (this._isHorizontal())
{
limits_px[1] -= this._tickSize_px[1] * 2;
}
else
{
// TODO vertical
}
// calculate the bounding box, in the Slider's coordinates:
const position_px = this._getPosition_px();
this._boundingBox = new PIXI.Rectangle(
this._getLengthUnits(position_px.x + limits_px[0]),
this._getLengthUnits(position_px.y + limits_px[1]),
this._getLengthUnits(limits_px[2] - limits_px[0]),
this._getLengthUnits(limits_px[3] - limits_px[1]),
);
}
/**
* Sanitize the slider attributes: check for attribute conflicts, missing values, etc.
*
* @protected
*/
_sanitizeAttributes()
{
this._isSliderStyle = false;
this._frozenMarker = false;
// convert potential string styles into Symbols:
this._style.forEach((style, index) =>
{
if (typeof style === "string")
{
this._style[index] = Symbol.for(style.toUpperCase());
}
});
// TODO: non-empty ticks, RADIO is also categorical, etc.
// SLIDER style: two ticks, first one is zero, second one is > 1
if (this._style.indexOf(Slider.Style.SLIDER) > -1)
{
this._isSliderStyle = true;
// more than 2 ticks: cut to two
if (this._ticks.length > 2)
{
this.psychoJS.logger.warn(`Slider "${this._name}" has style: SLIDER and more than two ticks. We cut the ticks to 2.`);
this._ticks = this._ticks.slice(0, 2);
}
// less than 2 ticks: error
if (this._ticks.length < 2)
{
throw {
origin: "Slider._sanitizeAttributes",
context: "when sanitizing the attributes of Slider: " + this._name,
error: "less than 2 ticks were given for a slider of type: SLIDER"
}
}
// first tick different from zero: change it to zero
if (this._ticks[0] !== 0)
{
this.psychoJS.logger.warn(`Slider "${this._name}" has style: SLIDER but the first tick is not 0. We changed it to 0.`);
this._ticks[0] = 0;
}
// second tick smaller than 1: change it to 1
if (this._ticks[1] < 1)
{
this.psychoJS.logger.warn(`Slider "${this._name}" has style: SLIDER but the second tick is less than 1. We changed it to 1.`);
this._ticks[1] = 1;
}
// second tick is 1: the marker is frozen
if (this._ticks[1] === 1)
{
this._frozenMarker = true;
}
}
// deal with categorical sliders:
this._isCategorical = (this._ticks.length === 0);
if (this._isCategorical)
{
this._ticks = [...Array(this._labels.length)].map((_, i) => i);
this._granularity = 1.0;
}
}
/**
* Set the current rating.
*
* <p>Setting the rating does also change the visible position of the marker.</p>
*
* @param {number} rating - the rating
* @param {number} [responseTime] - the reaction time
* @param {boolean} [log= false] - whether of not to log
*/
recordRating(rating, responseTime = undefined, log = false)
{
// get response time:
if (typeof responseTime === "undefined")
{
responseTime = this._responseClock.getTime();
}
// set rating:
// rating = this._granularise(rating);
// this._setAttribute('rating', rating, log);
this.setRating(rating, log);
// add rating and response time to history:
this._history.push({ rating: this._rating, responseTime });
this.psychoJS.logger.debug("record a new rating: ", this._rating, "with response time: ", responseTime, "for Slider: ", this._name);
// update slider:
this._needMarkerUpdate = true;
this._needUpdate = true;
}
/**
* Release the PIXI representation, if there is one.
*
* @param {boolean} [log= false] - whether or not to log
*/
release (log = false)
{
this._removeEventListeners();
super.release(log);
}
/**
* Update the stimulus, if necessary.
*
* @protected
*/
_updateIfNeeded()
{
if (!this._needUpdate)
{
return;
}
this._needUpdate = false;
this._estimateBoundingBox();
this._setupSlider();
this._updateMarker();
this._pixi.scale.x = 1;
this._pixi.scale.y = -1;
this._pixi.rotation = -this._ori * Math.PI / 180;
this._pixi.position = this._getPosition_px();
this._pixi.alpha = this._opacity;
this._pixi.zIndex = -this._depth;
this.anchor = this._anchor;
// make sure that the dependent Stimuli are also updated:
for (const dependentStim of this._dependentStims)
{
dependentStim.draw();
}
}
/**
* Estimate the position of the slider, taking the compactness into account.
*
* @return {number[]} - the position of the slider, in pixels
* @protected
*/
_getPosition_px()
{
const position = to_pixiPoint(this.pos, this.units, this.win, true);
if (
this._compact
&& (this._style.indexOf(Slider.Style.RADIO) > -1 || this._style.indexOf(Slider.Style.RATING) > -1)
)
{
if (this._isHorizontal())
{
position.y -= this._getLengthPix(this._tickSize[1]) * (this._flip ? -1 : 1);
}
else
{
position.x += this._getLengthPix(this._tickSize[0]) * (this._flip ? -1 : 1);
}
}
return position;
}
/**
* Update the position of the marker if necessary.
*
* @name module:visual.Slider#_updateMarker
* @protected
*/
_updateMarker()
{
if (!this._needMarkerUpdate)
{
return;
}
this._needMarkerUpdate = false;
if (typeof this._marker !== "undefined")
{
if (typeof this._markerPos !== "undefined")
{
const visibleMarkerPos = this._ratingToPos([this._markerPos]);
this._marker.position = to_pixiPoint(visibleMarkerPos[0], this.units, this.win, true);
this._marker.alpha = 1;
}
else
{
this._marker.alpha = 0;
}
}
}
/**
* Handle pointerdown event.
*
* @protected
*/
_handlePointerDown (e) {
if (e.data.pointerType === "mouse" && e.data.button !== 0)
{
return;
}
this._markerDragging = true;
if (!this._frozenMarker)
{
const mouseLocalPos_px = e.data.getLocalPosition(this._pixi);
const rating = this._posToRating([mouseLocalPos_px.x, mouseLocalPos_px.y]);
this.setMarkerPos(rating);
}
e.stopPropagation();
}
/**
* Handle pointermove event.
*
* @protected
*/
_handlePointerMove (e)
{
if (this._markerDragging)
{
if (!this._frozenMarker)
{
const mouseLocalPos_px = e.data.getLocalPosition(this._pixi);
const rating = this._posToRating([mouseLocalPos_px.x, mouseLocalPos_px.y]);
this.setMarkerPos(rating);
}
e.stopPropagation();
}
}
/**
* Handle pointerup event.
*
* @protected
*/
_handlePointerUp (e)
{
if (this._markerDragging)
{
this._markerDragging = false;
if (!this._frozenMarker)
{
const mouseLocalPos_px = e.data.getLocalPosition(this._pixi);
const rating = this._posToRating([mouseLocalPos_px.x, mouseLocalPos_px.y]);
this.recordRating(rating);
}
e.stopPropagation();
}
}
/**
* Add event listeners.
*
* @protected
*/
_addEventListeners ()
{
this._pixi.on("pointerdown", this._handlePointerDownBinded);
this._win._rootContainer.on("pointermove", this._handlePointerMoveBinded);
this._win._rootContainer.on("pointerup", this._handlePointerUpBinded);
}
/**
* Remove event listeners.
*
* @protected
*/
_removeEventListeners ()
{
if (this._pixi)
{
this._pixi.off("pointerdown", this._handlePointerDownBinded);
}
this._win._rootContainer.off("pointermove", this._handlePointerMoveBinded);
this._win._rootContainer.off("pointerup", this._handlePointerUpBinded);
}
/**
* Setup the PIXI components of the slider (bar, ticks, labels, marker, etc.).
*
* @protected
*/
_setupSlider()
{
if (!this._needPixiUpdate)
{
return;
}
this._needPixiUpdate = false;
this._setupStyle();
// calculate various values in pixel units:
this._tickSize_px = util.to_px(this._tickSize, this._units, this._win);
this._fontSize_px = this._getLengthPix(this._fontSize);
this._barSize_px = util.to_px(this._barSize, this._units, this._win, true).map((v) => Math.max(1, v));
this._markerSize_px = util.to_px(this._markerSize, this._units, this._win, true);
const tickPositions = this._ratingToPos(this._ticks);
this._tickPositions_px = tickPositions.map((p) => util.to_px(p, this._units, this._win));
if (typeof this._pixi !== "undefined")
{
this._removeEventListeners();
this._pixi.destroy(true);
}
this._pixi = new PIXI.Container();
this._pixi.interactive = true;
// apply the clip mask:
this._pixi.mask = this._clipMask;
this._body = new PIXI.Graphics();
this._body.interactive = true;
this._pixi.addChild(this._body);
// ensure that pointer events will be captured along the slider body, even outside of
// marker and labels:
if (this._tickType === Slider.Shape.DISC)
{
const maxTickSize_px = Math.max(this._tickSize_px[0], this._tickSize_px[1]);
this._body.hitArea = new PIXI.Rectangle(
-(this._barSize_px[0] + maxTickSize_px) * 0.5,
-(this._barSize_px[1] + maxTickSize_px) * 0.5,
this._barSize_px[0] + maxTickSize_px,
this._barSize_px[1] + maxTickSize_px,
);
}
else
{
this._body.hitArea = new PIXI.Rectangle(
-this._barSize_px[0] / 2 - this._tickSize_px[0] / 2,
-this._barSize_px[1] / 2 - this._tickSize_px[1] / 2,
this._barSize_px[0] + this._tickSize_px[0],
this._barSize_px[1] + this._tickSize_px[1],
);
}
// central bar:
this._setupBar();
// ticks:
this._setupTicks();
// labels:
this._setupLabels();
// markers:
this._setupMarker();
this._addEventListeners();
}
/**
* Setup the central bar.
*
* @protected