Skip to content

Commit cb579cb

Browse files
committed
UIManager for game tutorials
1 parent feedc33 commit cb579cb

File tree

6 files changed

+83
-2
lines changed

6 files changed

+83
-2
lines changed

src/components/UIButton.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import UIComponentPrototype from "./UIComponentPrototype";
2+
import UIManager from "../manager/UIManager";
23

34
const HIT_ZONE = "HIT_ZONE";
45

@@ -200,7 +201,9 @@ export default class UIButton extends UIComponentPrototype {
200201
this._isPressed = false;
201202
this.doState();
202203
if (isClicked) {
203-
this._onClick();
204+
if (UIManager.check(this.lockId)) {
205+
this._onClick();
206+
}
204207
}
205208
}
206209

src/components/UIButtonDraggable.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import UIButton from "./UIButton";
2+
import UIManager from "../manager/UIManager";
23

34
const _EVENT_DRAG = "event_drag";
45

@@ -117,6 +118,9 @@ export default class UIButtonDraggable extends UIButton {
117118
* @protected
118119
*/
119120
_onDrag(pointer, gameObject, dragX, dragY) {
121+
if (!UIManager.check(this.lockId)){
122+
return;
123+
}
120124
if (!this._dragZone || this._dragZone !== gameObject || this.clip) {
121125
return;
122126
}

src/components/UIComponentPrototype.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Phaser from "phaser";
2+
import UIManager from "../manager/UIManager";
23

34
const _EVENT_STATE = "event_state";
45

@@ -16,6 +17,7 @@ const _EVENT_STATE = "event_state";
1617
* at current state or at all, nothing bad happens.*
1718
* @inheritDoc
1819
* @extends Phaser.Events.EventEmitter
20+
* @property {String} lockId Used by UIManager, see {@link PhaserComps.UIManager}
1921
* @param {PhaserComps.UIComponents.UIComponentPrototype} [parent] UIComponentPrototype instance to find clip inside
2022
* @param {String} [key] key to find clip inside parent
2123
*/
@@ -26,6 +28,11 @@ export default class UIComponentPrototype extends Phaser.Events.EventEmitter {
2628
constructor(parent, key) {
2729
super();
2830

31+
/**
32+
* @type {String}
33+
*/
34+
this.lockId = null;
35+
2936
/**
3037
*
3138
* @type {UIComponentPrototype}

src/components/UIScrollBar.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import UIComponentPrototype from "./UIComponentPrototype";
22
import UIButton from "./UIButton";
33
import UIButtonDraggable from "./UIButtonDraggable";
4+
import UIManager from "../manager/UIManager";
45

56
const _EVENT_CHANGE = "event_change";
67

@@ -198,6 +199,9 @@ export default class UIScrollBar extends UIComponentPrototype {
198199
* @protected
199200
*/
200201
onPrevClick() {
202+
if (!UIManager.check(this.lockId)) {
203+
return;
204+
}
201205
this.value -= this._buttonStep;
202206
}
203207

@@ -206,6 +210,9 @@ export default class UIScrollBar extends UIComponentPrototype {
206210
* @protected
207211
*/
208212
onNextClick() {
213+
if (!UIManager.check(this.lockId)) {
214+
return;
215+
}
209216
this.value += this._buttonStep;
210217
}
211218

@@ -256,6 +263,9 @@ export default class UIScrollBar extends UIComponentPrototype {
256263
if (this._trackLength === 0) {
257264
return;
258265
}
266+
if (!UIManager.check(this.lockId)) {
267+
return;
268+
}
259269
let barPosition = this._vertical ? positionY : positionX;
260270
let newValue = (barPosition - this._trackStart) / this._trackLength;
261271
let v = newValue * (this._maxValue - this._minValue);

src/manager/UIManager.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
let isLock = false;
2+
const enabledIds = [];
3+
4+
/**
5+
* @namespace PhaserComps.UIManager
6+
* @memberOf PhaserComps
7+
* @classdesc Allows to lock all ui, except for provided lock ids.
8+
* For this, you must set `lockId` property to UIComponentPrototype instances you want to enable,
9+
* and then switch theirs availability by UIManager's
10+
* {@link lock} and {@link unlock} methods
11+
*
12+
* For example, locked UIButton will still interact to mouse events, but will not emit click event.
13+
*
14+
* This can be useful in game tutorials.
15+
*/
16+
export default class UIManager {
17+
18+
/**
19+
* @memberOf PhaserComps.UIManager
20+
* @description Makes only components with provided ids list (or one id string) to emit UI events
21+
*
22+
* *Note, that method rewrites current enabled ids list every call.*
23+
* @param {String | Array<String>} id component's lock id, or Array of lock ids to be only enabled
24+
*/
25+
static lock(id) {
26+
this.unlock();
27+
if (typeof id === "string") {
28+
enabledIds.push(id);
29+
} else {
30+
id.forEach(value => enabledIds.push(value));
31+
}
32+
isLock = true;
33+
}
34+
35+
/**
36+
* @memberOf PhaserComps.UIManager
37+
* @description Releases all components
38+
*/
39+
static unlock() {
40+
enabledIds.length = 0;
41+
isLock = false;
42+
}
43+
44+
/**
45+
* @memberOf PhaserComps.UIManager
46+
* @description called from component to check, if it's allowed to emit UI event.
47+
* @param {String} id
48+
*/
49+
static check(id) {
50+
if (!isLock) {
51+
return true;
52+
}
53+
return enabledIds.indexOf(id) !== -1;
54+
}
55+
}

src/phasercomps.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import ComponentClip from "./clip/ComponentClip";
66
import UIComponents from "./components/UIComponents";
77
import Plugin from "./plugin/Plugin";
8+
import UIManager from "./manager/UIManager";
89

910
var PhaserComps = {
1011
ComponentClip: ComponentClip,
1112
UIComponents: UIComponents,
12-
Plugin: Plugin
13+
Plugin: Plugin,
14+
UIManager: UIManager
1315
};
1416

1517
module.exports = PhaserComps;

0 commit comments

Comments
 (0)