@m2c2kit/addons 0.3.26 → 0.3.28

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/LICENSE CHANGED
@@ -1,21 +1,13 @@
1
- MIT License
1
+ Copyright 2023 Scott T. Yabiku
2
2
 
3
- Copyright (c) 2023 Scott T. Yabiku
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
4
6
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
7
+ http://www.apache.org/licenses/LICENSE-2.0
11
8
 
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @m2c2kit/addons
2
2
 
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
3
+ [![License: Apache-2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/license/apache-2-0)
4
4
  [![CI/CD](https://github.com/m2c2-project/m2c2kit/actions/workflows/ci.yml/badge.svg)](https://github.com/m2c2-project/m2c2kit/actions/workflows/ci.yml)
5
5
  [![npm version](https://img.shields.io/npm/v/@m2c2kit/addons.svg)](https://www.npmjs.com/package/@m2c2kit/addons)
6
6
 
package/dist/index.d.ts CHANGED
@@ -1252,4 +1252,119 @@ declare class LocalePicker extends Composite {
1252
1252
  duplicate(newName?: string): LocalePicker;
1253
1253
  }
1254
1254
 
1255
- export { Button, type ButtonOptions, CountdownScene, type CountdownSceneOptions, CountdownTimer, type CountdownTimerEvent, type CountdownTimerOptions, Dialog, type DialogEvent, type DialogOptions, DialogResult, DrawPad, type DrawPadEvent, DrawPadEventType, type DrawPadItem, type DrawPadItemEvent, DrawPadItemEventType, type DrawPadOptions, type DrawPadStroke, Grid, type GridChild, type GridOptions, type InstructionScene, Instructions, type InstructionsOptions, type KeyConfiguration, type KeyTapMetadata, type LocaleOption, LocalePicker, type LocalePickerEvent, type LocalePickerIcon, type LocalePickerOptions, type LocalePickerResult, type StrokeInteraction, type TimerShape, VirtualKeyboard, type VirtualKeyboardEvent, type VirtualKeyboardOptions, type VirtualKeyboardRow };
1255
+ interface SliderOptions extends CompositeOptions {
1256
+ /** The size of the track that the thumb moves along. */
1257
+ trackSize?: Size;
1258
+ /** The color of the track. */
1259
+ trackColor?: RgbaColor;
1260
+ /** The size of the thumb that the user drags. */
1261
+ thumbSize?: Size;
1262
+ /** The color of the thumb. */
1263
+ thumbColor?: RgbaColor;
1264
+ /** The minimum value of the slider. */
1265
+ min?: number;
1266
+ /** The maximum value of the slider. */
1267
+ max?: number;
1268
+ /** The initial value of the slider. */
1269
+ value?: number;
1270
+ }
1271
+ interface SliderEvent extends CompositeEvent {
1272
+ type: "Composite";
1273
+ compositeType: "Slider";
1274
+ compositeEventType: "SliderValueChanged";
1275
+ target: Slider | string;
1276
+ value: number;
1277
+ }
1278
+ declare class Slider extends Composite implements SliderOptions {
1279
+ readonly compositeType = "Slider";
1280
+ private originalOptions;
1281
+ private _trackSize;
1282
+ private _trackColor;
1283
+ private _thumbSize;
1284
+ private _thumbColor;
1285
+ private _min;
1286
+ private _max;
1287
+ private _value;
1288
+ private thumbLabel?;
1289
+ private _thumbShape?;
1290
+ private get thumbShape();
1291
+ private set thumbShape(value);
1292
+ /**
1293
+ * A slider to select a value from a range by dragging a thumb along a track.
1294
+ *
1295
+ * @experimental Slider is a work in progress and will change in future versions.
1296
+ *
1297
+ * @param options - {@link SliderOptions}
1298
+ */
1299
+ constructor(options: SliderOptions);
1300
+ get completeNodeOptions(): {
1301
+ /** The size of the track that the thumb moves along. */
1302
+ trackSize?: Size;
1303
+ /** The color of the track. */
1304
+ trackColor?: RgbaColor;
1305
+ /** The size of the thumb that the user drags. */
1306
+ thumbSize?: Size;
1307
+ /** The color of the thumb. */
1308
+ thumbColor?: RgbaColor;
1309
+ /** The minimum value of the slider. */
1310
+ min?: number;
1311
+ /** The maximum value of the slider. */
1312
+ max?: number;
1313
+ /** The initial value of the slider. */
1314
+ value?: number;
1315
+ name?: string;
1316
+ position?: _m2c2kit_core.Point;
1317
+ scale?: number;
1318
+ alpha?: number;
1319
+ zRotation?: number;
1320
+ isUserInteractionEnabled?: boolean;
1321
+ draggable?: boolean;
1322
+ hidden?: boolean;
1323
+ layout?: _m2c2kit_core.Layout;
1324
+ uuid?: string;
1325
+ suppressEvents?: boolean;
1326
+ anchorPoint?: _m2c2kit_core.Point;
1327
+ zPosition?: number;
1328
+ };
1329
+ initialize(): void;
1330
+ updateThumbLabel(): void;
1331
+ /**
1332
+ * Executes a callback when the slider value changes.
1333
+ *
1334
+ * @param callback - function to execute
1335
+ * @param options
1336
+ */
1337
+ onValueChanged(callback: (sliderEvent: SliderEvent) => void, options?: CallbackOptions): void;
1338
+ private addSliderEventListener;
1339
+ get trackSize(): Size;
1340
+ set trackSize(value: Size);
1341
+ get trackColor(): RgbaColor;
1342
+ set trackColor(value: RgbaColor);
1343
+ get thumbSize(): Size;
1344
+ set thumbSize(value: Size);
1345
+ get thumbColor(): RgbaColor;
1346
+ set thumbColor(value: RgbaColor);
1347
+ get value(): number;
1348
+ set value(value: number);
1349
+ get min(): number;
1350
+ set min(value: number);
1351
+ get max(): number;
1352
+ set max(value: number);
1353
+ /**
1354
+ * Duplicates a node using deep copy.
1355
+ *
1356
+ * @remarks This is a deep recursive clone (node and children).
1357
+ * The uuid property of all duplicated nodes will be newly created,
1358
+ * because uuid must be unique.
1359
+ *
1360
+ * @param newName - optional name of the new, duplicated node. If not
1361
+ * provided, name will be the new uuid
1362
+ */
1363
+ duplicate(newName?: string | undefined): Slider;
1364
+ update(): void;
1365
+ draw(canvas: Canvas): void;
1366
+ warmup(canvas: Canvas): void;
1367
+ }
1368
+
1369
+ export { Button, CountdownScene, CountdownTimer, Dialog, DialogResult, DrawPad, DrawPadEventType, DrawPadItemEventType, Grid, Instructions, LocalePicker, Slider, VirtualKeyboard };
1370
+ export type { ButtonOptions, CountdownSceneOptions, CountdownTimerEvent, CountdownTimerOptions, DialogEvent, DialogOptions, DrawPadEvent, DrawPadItem, DrawPadItemEvent, DrawPadOptions, DrawPadStroke, GridChild, GridOptions, InstructionScene, InstructionsOptions, KeyConfiguration, KeyTapMetadata, LocaleOption, LocalePickerEvent, LocalePickerIcon, LocalePickerOptions, LocalePickerResult, SliderEvent, SliderOptions, StrokeInteraction, TimerShape, VirtualKeyboardEvent, VirtualKeyboardOptions, VirtualKeyboardRow };
package/dist/index.js CHANGED
@@ -3453,7 +3453,299 @@ M2c2KitHelpers.registerM2NodeClass(
3453
3453
  CountdownTimer
3454
3454
  );
3455
3455
 
3456
- console.log("\u26AA @m2c2kit/addons version 0.3.26 (1ed4ac2a)");
3456
+ class Slider extends Composite {
3457
+ /**
3458
+ * A slider to select a value from a range by dragging a thumb along a track.
3459
+ *
3460
+ * @experimental Slider is a work in progress and will change in future versions.
3461
+ *
3462
+ * @param options - {@link SliderOptions}
3463
+ */
3464
+ constructor(options) {
3465
+ super(options);
3466
+ this.compositeType = "Slider";
3467
+ this._trackSize = { width: 250, height: 10 };
3468
+ this._trackColor = WebColors.Black;
3469
+ this._thumbSize = { width: 20, height: 40 };
3470
+ this._thumbColor = WebColors.DarkGray;
3471
+ this._min = 0;
3472
+ this._max = 100;
3473
+ this._value = (this.max - this.min) / 2;
3474
+ this.originalOptions = JSON.parse(JSON.stringify(options));
3475
+ if (options.trackSize) {
3476
+ this.trackSize = options.trackSize;
3477
+ }
3478
+ if (options.trackColor) {
3479
+ this.trackColor = options.trackColor;
3480
+ }
3481
+ if (options.thumbSize) {
3482
+ this.thumbSize = options.thumbSize;
3483
+ }
3484
+ if (options.thumbColor) {
3485
+ this.thumbColor = options.thumbColor;
3486
+ }
3487
+ if (options.min !== void 0) {
3488
+ this.min = options.min;
3489
+ }
3490
+ if (options.max !== void 0) {
3491
+ this.max = options.max;
3492
+ }
3493
+ if (options.value !== void 0) {
3494
+ this.value = options.value;
3495
+ }
3496
+ this.saveNodeNewEvent();
3497
+ }
3498
+ get thumbShape() {
3499
+ if (this._thumbShape === void 0) {
3500
+ throw new Error("thumbShape is not defined.");
3501
+ }
3502
+ return this._thumbShape;
3503
+ }
3504
+ set thumbShape(value) {
3505
+ this._thumbShape = value;
3506
+ }
3507
+ get completeNodeOptions() {
3508
+ return {
3509
+ ...this.options,
3510
+ ...this.getNodeOptions(),
3511
+ ...this.getDrawableOptions(),
3512
+ ...this.originalOptions
3513
+ };
3514
+ }
3515
+ initialize() {
3516
+ this.removeAllChildren();
3517
+ const trackShape = new Shape({
3518
+ rect: {
3519
+ width: this.trackSize.width,
3520
+ height: this.trackSize.height
3521
+ },
3522
+ cornerRadius: 8,
3523
+ fillColor: this.trackColor
3524
+ });
3525
+ this.addChild(trackShape);
3526
+ this.thumbShape = new Shape({
3527
+ rect: {
3528
+ width: this.thumbSize.width,
3529
+ height: this.thumbSize.height
3530
+ },
3531
+ cornerRadius: 8,
3532
+ fillColor: this.thumbColor,
3533
+ isUserInteractionEnabled: true,
3534
+ draggable: true,
3535
+ zPosition: 1,
3536
+ position: {
3537
+ x: this.value * this.trackSize.width / (this.max - this.min) - this.trackSize.width / 2,
3538
+ y: 0
3539
+ }
3540
+ });
3541
+ trackShape.addChild(this.thumbShape);
3542
+ const trackZoneShape = new Shape({
3543
+ rect: {
3544
+ width: this.trackSize.width,
3545
+ height: this.thumbSize.height
3546
+ },
3547
+ // during development, it is useful to make this visible
3548
+ // fillColor: WebColors.Red,
3549
+ // alpha: 0.05,
3550
+ alpha: 0,
3551
+ isUserInteractionEnabled: true,
3552
+ zPosition: 0
3553
+ });
3554
+ trackShape.addChild(trackZoneShape);
3555
+ trackZoneShape.onTapDown((e) => {
3556
+ this.thumbShape.position.x = e.point.x - trackShape.size.width / 2;
3557
+ this.updateThumbLabel();
3558
+ });
3559
+ const thumbZoneShape = new Shape({
3560
+ rect: {
3561
+ width: this.trackSize.width,
3562
+ /**
3563
+ * The thumbZoneShape is twice the height of the parent scene in case
3564
+ * the slider is placed at the bottom or top of the screen. This
3565
+ * ensures that thumbZoneShape is large enough to capture pointer
3566
+ * events that are outside the bounds of the slider and on the parent
3567
+ * scene.
3568
+ */
3569
+ height: this.parentSceneAsNode.size.height * 2
3570
+ },
3571
+ // during development, it is useful to make this visible
3572
+ // fillColor: WebColors.Black,
3573
+ // alpha: 0.008,
3574
+ alpha: 0,
3575
+ isUserInteractionEnabled: true
3576
+ });
3577
+ this.addChild(thumbZoneShape);
3578
+ thumbZoneShape.onPointerMove(() => {
3579
+ this.thumbShape.draggable = true;
3580
+ });
3581
+ thumbZoneShape.onPointerLeave(() => {
3582
+ this.thumbShape.draggable = false;
3583
+ });
3584
+ this.thumbShape.onTapDown((e) => {
3585
+ e.handled = true;
3586
+ if (e.point.y !== 0) {
3587
+ this.thumbShape.position.y = 0;
3588
+ }
3589
+ if (e.point.x < -this.trackSize.width / 2) {
3590
+ this.thumbShape.position.x = -this.trackSize.width / 2;
3591
+ }
3592
+ if (e.point.x > this.trackSize.width / 2) {
3593
+ this.thumbShape.position.x = this.trackSize.width / 2;
3594
+ }
3595
+ this.updateThumbLabel();
3596
+ });
3597
+ this.thumbShape.onDrag((e) => {
3598
+ if (e.position.y !== 0) {
3599
+ this.thumbShape.position.y = 0;
3600
+ }
3601
+ if (e.position.x < -this.trackSize.width / 2) {
3602
+ this.thumbShape.position.x = -this.trackSize.width / 2;
3603
+ }
3604
+ if (e.position.x > this.trackSize.width / 2) {
3605
+ this.thumbShape.position.x = this.trackSize.width / 2;
3606
+ }
3607
+ this.updateThumbLabel();
3608
+ });
3609
+ this.thumbShape.onDragEnd(() => {
3610
+ const value = Math.round(
3611
+ (this.thumbShape.position.x + this.trackSize.width / 2) / this.trackSize.width * (this.max - this.min)
3612
+ );
3613
+ this.thumbShape.position.x = value / (this.max - this.min) * this.trackSize.width - this.trackSize.width / 2;
3614
+ this.updateThumbLabel();
3615
+ });
3616
+ this.needsInitialization = false;
3617
+ }
3618
+ updateThumbLabel() {
3619
+ const value = (this.thumbShape.position.x + this.trackSize.width / 2) / this.trackSize.width * (this.max - this.min);
3620
+ if (!this.thumbLabel) {
3621
+ this.thumbLabel = new Label({
3622
+ text: value.toString()
3623
+ });
3624
+ this.addChild(this.thumbLabel);
3625
+ }
3626
+ this.thumbLabel.text = Math.round(value).toString();
3627
+ this.thumbLabel.position = {
3628
+ x: this.thumbShape.position.x,
3629
+ y: this.thumbShape.position.y - 30
3630
+ };
3631
+ if (this.thumbLabel) {
3632
+ this.thumbLabel.position = {
3633
+ x: this.thumbShape.position.x,
3634
+ y: this.thumbShape.position.y - 30
3635
+ };
3636
+ }
3637
+ const sliderEvent = {
3638
+ type: M2EventType.Composite,
3639
+ compositeType: "Slider",
3640
+ compositeEventType: "SliderValueChanged",
3641
+ target: this,
3642
+ value,
3643
+ ...M2c2KitHelpers.createTimestamps()
3644
+ };
3645
+ this.handleCompositeEvent(sliderEvent);
3646
+ this.saveEvent(sliderEvent);
3647
+ if (this.eventListeners.length > 0) {
3648
+ this.eventListeners.filter(
3649
+ (listener) => listener.type === M2EventType.Composite && listener.compositeType === this.compositeType && listener.compositeEventType === "SliderValueChanged"
3650
+ ).forEach((listener) => {
3651
+ listener.callback(sliderEvent);
3652
+ });
3653
+ }
3654
+ }
3655
+ /**
3656
+ * Executes a callback when the slider value changes.
3657
+ *
3658
+ * @param callback - function to execute
3659
+ * @param options
3660
+ */
3661
+ onValueChanged(callback, options) {
3662
+ const eventListener = {
3663
+ type: M2EventType.Composite,
3664
+ compositeEventType: "SliderValueChanged",
3665
+ compositeType: this.compositeType,
3666
+ nodeUuid: this.uuid,
3667
+ callback
3668
+ };
3669
+ this.addSliderEventListener(eventListener, options);
3670
+ }
3671
+ addSliderEventListener(eventListener, options) {
3672
+ if (options?.replaceExisting) {
3673
+ this.eventListeners = this.eventListeners.filter(
3674
+ (listener) => !(listener.nodeUuid === eventListener.nodeUuid && listener.type === eventListener.type && listener.compositeType === eventListener.compositeType)
3675
+ );
3676
+ }
3677
+ this.eventListeners.push(eventListener);
3678
+ }
3679
+ get trackSize() {
3680
+ return this._trackSize;
3681
+ }
3682
+ set trackSize(value) {
3683
+ this._trackSize = value;
3684
+ }
3685
+ get trackColor() {
3686
+ return this._trackColor;
3687
+ }
3688
+ set trackColor(value) {
3689
+ this._trackColor = value;
3690
+ }
3691
+ get thumbSize() {
3692
+ return this._thumbSize;
3693
+ }
3694
+ set thumbSize(value) {
3695
+ this._thumbSize = value;
3696
+ }
3697
+ get thumbColor() {
3698
+ return this._thumbColor;
3699
+ }
3700
+ set thumbColor(value) {
3701
+ this._thumbColor = value;
3702
+ }
3703
+ get value() {
3704
+ return this._value;
3705
+ }
3706
+ set value(value) {
3707
+ this._value = value;
3708
+ }
3709
+ get min() {
3710
+ return this._min;
3711
+ }
3712
+ set min(value) {
3713
+ this._min = value;
3714
+ }
3715
+ get max() {
3716
+ return this._max;
3717
+ }
3718
+ set max(value) {
3719
+ this._max = value;
3720
+ }
3721
+ /**
3722
+ * Duplicates a node using deep copy.
3723
+ *
3724
+ * @remarks This is a deep recursive clone (node and children).
3725
+ * The uuid property of all duplicated nodes will be newly created,
3726
+ * because uuid must be unique.
3727
+ *
3728
+ * @param newName - optional name of the new, duplicated node. If not
3729
+ * provided, name will be the new uuid
3730
+ */
3731
+ duplicate(newName) {
3732
+ throw new Error(`Method not implemented. ${newName}`);
3733
+ }
3734
+ update() {
3735
+ super.update();
3736
+ }
3737
+ draw(canvas) {
3738
+ super.drawChildren(canvas);
3739
+ }
3740
+ warmup(canvas) {
3741
+ this.initialize();
3742
+ this.children.filter((child) => child.isDrawable).forEach((child) => {
3743
+ child.warmup(canvas);
3744
+ });
3745
+ }
3746
+ }
3747
+
3748
+ console.log("\u26AA @m2c2kit/addons version 0.3.28 (d1ad307f)");
3457
3749
 
3458
- export { Button, CountdownScene, CountdownTimer, Dialog, DialogResult, DrawPad, DrawPadEventType, DrawPadItemEventType, Grid, Instructions, LocalePicker, VirtualKeyboard };
3750
+ export { Button, CountdownScene, CountdownTimer, Dialog, DialogResult, DrawPad, DrawPadEventType, DrawPadItemEventType, Grid, Instructions, LocalePicker, Slider, VirtualKeyboard };
3459
3751
  //# sourceMappingURL=index.js.map