Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficient FlxKey#toString, FlxTileblock#setTile, overridable FlxState#open/close #1300

Merged
merged 10 commits into from
Sep 20, 2014
4 changes: 2 additions & 2 deletions flixel/FlxState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class FlxState extends FlxGroup
}
}

public inline function openSubState(SubState:FlxSubState):Void
public function openSubState(SubState:FlxSubState):Void
{
_requestSubStateReset = true;
_requestedSubState = SubState;
Expand All @@ -74,7 +74,7 @@ class FlxState extends FlxGroup
/**
* Closes the substate of this state, if one exists.
*/
public inline function closeSubState():Void
public function closeSubState():Void
{
_requestSubStateReset = true;
}
Expand Down
113 changes: 102 additions & 11 deletions flixel/input/keyboard/FlxKey.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package flixel.input.keyboard;
@:enum
abstract FlxKey(Int) from Int to Int
{
public static var keyNameMap:Map<String, FlxKey> = [
public static var fromStringMap:Map<String, FlxKey> = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... this is getting a bit excessive with two maps, should probably use a macro for this. The FlxColor macro already does something very similar.

Btw, FlxAndroidKey would need the same fix.

"ANY" => ANY,
"A" => A,
"B" => B,
Expand Down Expand Up @@ -104,6 +104,104 @@ abstract FlxKey(Int) from Int to Int
"NUMPADMULTIPLY" => NUMPADMULTIPLY
];

public static var toStringMap:Map<FlxKey, String> = [
ANY => "ANY",
A => "A",
B => "B",
C => "C",
D => "D",
E => "E",
F => "F",
G => "G",
H => "H",
I => "I",
J => "J",
K => "K",
L => "L",
M => "M",
N => "N",
O => "O",
P => "P",
Q => "Q",
R => "R",
S => "S",
T => "T",
U => "U",
V => "V",
W => "W",
X => "X",
Y => "Y",
Z => "Z",
ZERO => "ZERO",
ONE => "ONE",
TWO => "TWO",
THREE => "THREE",
FOUR => "FOUR",
FIVE => "FIVE",
SIX => "SIX",
SEVEN => "SEVEN",
EIGHT => "EIGHT",
NINE => "NINE",
PAGEUP => "PAGEUP",
PAGEDOWN => "PAGEDOWN",
HOME => "HOME",
END => "END",
INSERT => "INSERT",
ESCAPE => "ESCAPE",
MINUS => "MINUS",
PLUS => "PLUS",
DELETE => "DELETE",
BACKSPACE => "BACKSPACE",
LBRACKET => "LBRACKET",
RBRACKET => "RBRACKET",
BACKSLASH => "BACKSLASH",
CAPSLOCK => "CAPSLOCK",
SEMICOLON => "SEMICOLON",
QUOTE => "QUOTE",
ENTER => "ENTER",
SHIFT => "SHIFT",
COMMA => "COMMA",
PERIOD => "PERIOD",
SLASH => "SLASH",
NUMPADSLASH => "NUMPADSLASH",
GRAVEACCENT => "GRAVEACCENT",
CONTROL => "CONTROL",
ALT => "ALT",
SPACE => "SPACE",
UP => "UP",
DOWN => "DOWN",
LEFT => "LEFT",
RIGHT => "RIGHT",
TAB => "TAB",
PRINTSCREEN => "PRINTSCREEN",
F1 => "F1",
F2 => "F2",
F3 => "F3",
F4 => "F4",
F5 => "F5",
F6 => "F6",
F7 => "F7",
F8 => "F8",
F9 => "F9",
F10 => "F10",
F11 => "F11",
F12 => "F12",
NUMPADZERO => "NUMPADZERO",
NUMPADONE => "NUMPADONE",
NUMPADTWO => "NUMPADTWO",
NUMPADTHREE => "NUMPADTHREE",
NUMPADFOUR => "NUMPADFOUR",
NUMPADFIVE => "NUMPADFIVE",
NUMPADSIX => "NUMPADSIX",
NUMPADSEVEN => "NUMPADSEVEN",
NUMPADEIGHT => "NUMPADEIGHT",
NUMPADNINE => "NUMPADNINE",
NUMPADMINUS => "NUMPADMINUS",
NUMPADPLUS => "NUMPADPLUS",
NUMPADPERIOD => "NUMPADPERIOD",
NUMPADMULTIPLY => "NUMPADMULTIPLY"
];

// Key Indicies
var ANY = -2;
var NONE = -1;
Expand Down Expand Up @@ -206,19 +304,12 @@ abstract FlxKey(Int) from Int to Int
public static inline function fromString(s:String)
{
s = s.toUpperCase();
return keyNameMap.exists(s) ? keyNameMap.get(s) : NONE;
return fromStringMap.exists(s) ? fromStringMap.get(s) : NONE;
}

@:to
public static function toString(i:Int):String
public inline function toString():String
{
for (key in keyNameMap.keys())
{
if (i == keyNameMap.get(key))
{
return key;
}
}
return "NONE";
return toStringMap.get(this);
}
}
2 changes: 1 addition & 1 deletion flixel/input/keyboard/FlxKeyboard.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FlxKeyboard extends FlxKeyManager<FlxKey, FlxKeyList>
preventDefaultKeys = [FlxKey.UP, FlxKey.DOWN, FlxKey.LEFT, FlxKey.RIGHT, FlxKey.TAB, FlxKey.SPACE];
#end

for (code in FlxKey.keyNameMap)
for (code in FlxKey.fromStringMap)
{
if (code != FlxKey.ANY && code != FlxKey.NONE)
{
Expand Down
33 changes: 23 additions & 10 deletions flixel/tile/FlxTileblock.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import flixel.system.FlxAssets.FlxGraphicAsset;
import flixel.system.layer.DrawStackItem;
import flixel.math.FlxAngle;
import flixel.math.FlxRandom;
import flixel.util.FlxDestroyUtil;
import flixel.util.FlxSpriteUtil;

/**
Expand All @@ -15,6 +16,8 @@ import flixel.util.FlxSpriteUtil;
*/
class FlxTileblock extends FlxSprite
{
private var tileSprite:FlxSprite;

/**
* Creates a new FlxBlock object with the specified position and size.
*
Expand All @@ -32,6 +35,11 @@ class FlxTileblock extends FlxSprite
moves = false;
}

override public function destroy():Void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ newline.

tileSprite = FlxDestroyUtil.destroy(tileSprite);
super.destroy();
}

/**
* Fills the block with a randomly arranged selection of graphics from the image provided.
*
Expand All @@ -48,21 +56,21 @@ class FlxTileblock extends FlxSprite
}

// First create a tile brush
var sprite:FlxSprite = new FlxSprite().loadGraphic(TileGraphic, true, TileWidth, TileHeight);
var spriteWidth:Int = Std.int(sprite.width);
var spriteHeight:Int = Std.int(sprite.height);
var total:Int = sprite.frames + Empties;
tileSprite = new FlxSprite().loadGraphic(TileGraphic, true, TileWidth, TileHeight);
var spriteWidth:Int = Std.int(tileSprite.width);
var spriteHeight:Int = Std.int(tileSprite.height);
var total:Int = tileSprite.frames + Empties;

// Then prep the "canvas" as it were (just doublechecking that the size is on tile boundaries)
var regen:Bool = false;

if (width % sprite.width != 0)
if (width % tileSprite.width != 0)
{
width = Std.int((width / spriteWidth + 1)) * spriteWidth;
regen = true;
}

if (height % sprite.height != 0)
if (height % tileSprite.height != 0)
{
height = Std.int((height / spriteHeight + 1)) * spriteHeight;
regen = true;
Expand Down Expand Up @@ -94,9 +102,9 @@ class FlxTileblock extends FlxSprite
{
if (FlxG.random.float() * total > Empties)
{
sprite.animation.randomFrame();
sprite.drawFrame();
stamp(sprite, destinationX, destinationY);
tileSprite.animation.randomFrame();
tileSprite.drawFrame();
stamp(tileSprite, destinationX, destinationY);
}

destinationX += spriteWidth;
Expand All @@ -107,8 +115,13 @@ class FlxTileblock extends FlxSprite
row++;
}

sprite.destroy();
dirty = true;
return this;
}

public function setTile(x:Int, y:Int, index:Int):Void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ newline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usefulness of this function might be a bit limited considering that FlxTilemap functions like getTile(), getTileByIndex() etc are missing.

Useful nevertheless. :)

tileSprite.animation.frameIndex = index;
stamp(tileSprite, x * Std.int(tileSprite.width), y * Std.int(tileSprite.height));
dirty = true;
}
}