-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathFlxSubState.hx
More file actions
122 lines (106 loc) · 2.4 KB
/
FlxSubState.hx
File metadata and controls
122 lines (106 loc) · 2.4 KB
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
package flixel;
import flixel.system.FlxBGSprite;
import flixel.util.FlxColor;
import flixel.util.FlxDestroyUtil;
/**
* A `FlxSubState` can be opened inside of a `FlxState`.
* By default, it also stops the parent state from updating,
* making it convenient for pause screens or menus.
*
* @see [FlxSubstate snippet](https://snippets.haxeflixel.com/states/flxsubstate/)
* @see [Substate demo](https://haxeflixel.com/demos/SubState/)
*/
class FlxSubState extends FlxState
{
/**
* Callback method for state open/resume event.
* @since 4.3.0
*/
public var openCallback:Void->Void;
/**
* Callback method for state close event.
*/
public var closeCallback:Void->Void;
/**
* Helper sprite object for non-flash targets. Draws the background.
*/
@:noCompletion
var _bgSprite:FlxBGSprite;
/**
* Helper var for `close()` so `closeSubState()` can be called on the parent.
*/
@:allow(flixel.FlxState.resetSubState)
var _parentState:FlxState;
@:noCompletion
var _bgColor:FlxColor;
@:noCompletion
@:allow(flixel.FlxState.resetSubState)
var _created:Bool = false;
/**
* @param bgColor background color for this substate
*/
public function new(bgColor = FlxColor.TRANSPARENT)
{
super();
closeCallback = null;
openCallback = null;
if (FlxG.renderTile)
{
_bgSprite = new FlxBGSprite();
}
this.bgColor = bgColor;
}
override public function draw():Void
{
// Draw background
if (FlxG.renderBlit)
{
for (camera in getCamerasLegacy())
{
camera.fill(bgColor);
}
}
else // FlxG.renderTile
{
if (_bgSprite != null && _bgSprite.visible)
{
_bgSprite.cameras = getCameras();
_bgSprite.draw();
}
}
// Now draw all children
super.draw();
}
override public function destroy():Void
{
super.destroy();
closeCallback = null;
openCallback = null;
_parentState = null;
_bgSprite = FlxDestroyUtil.destroy(_bgSprite);
}
/**
* Closes this substate.
*/
public function close():Void
{
if (_parentState != null && _parentState.subState == this)
_parentState.closeSubState();
}
@:noCompletion
override inline function get_bgColor():FlxColor
{
return _bgColor;
}
@:noCompletion
override function set_bgColor(value:FlxColor):FlxColor
{
if (FlxG.renderTile && _bgSprite != null)
{
_bgSprite.alpha = value.alphaFloat;
_bgSprite.visible = _bgSprite.alpha > 0;
_bgSprite.color = value.rgb;
}
return _bgColor = value;
}
}