Skip to content

Commit

Permalink
add FlxTypedUIGroup and some docs (#246)
Browse files Browse the repository at this point in the history
* add FlxTypedUIGroup and some docs

* fix errors

* throw error for incompatible flixel versions

* update error message
  • Loading branch information
Geokureli authored Oct 18, 2023
1 parent 440210f commit 3bbac0f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 110 deletions.
18 changes: 8 additions & 10 deletions flixel/addons/ui/FlxUI.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import openfl.geom.Matrix;
import openfl.geom.Point;
import openfl.geom.Rectangle;
import openfl.Lib;
import flixel.addons.ui.FlxUI.MaxMinSize;
import flixel.addons.ui.ButtonLabelStyle;
import flixel.addons.ui.FlxUI.Rounding;
import flixel.addons.ui.FlxUI.VarValue;
import flixel.addons.ui.FlxUIBar.FlxBarStyle;
import flixel.addons.ui.FlxUICursor.WidgetList;
import flixel.addons.ui.FlxUIDropDownMenu;
import flixel.addons.ui.FlxUIGroup;
import flixel.addons.ui.BorderDef;
import flixel.addons.ui.FlxUILine.LineAxis;
import flixel.addons.ui.FlxUIRadioGroup.CheckStyle;
Expand Down Expand Up @@ -132,17 +130,17 @@ class FlxUI extends FlxUIGroup implements IEventGetter
*/
private function _tongueSet(list:Array<FlxSprite>, tongue:IFireTongue):Void
{
for (fs in list)
for (sprite in list)
{
if ((fs is FlxUIGroup))
if (sprite is FlxTypedUIGroup)
{
var g:FlxUIGroup = cast(fs, FlxUIGroup);
_tongueSet(g.members, tongue);
final group:FlxTypedUIGroup<FlxSprite> = cast sprite;
_tongueSet(group.members, tongue);
}
else if ((fs is FlxUI))
else if (sprite is FlxUI)
{
var fu:FlxUI = cast(fs, FlxUI);
fu.tongue = tongue;
final ui:FlxUI = cast sprite;
ui.tongue = tongue;
}
}
}
Expand Down
181 changes: 83 additions & 98 deletions flixel/addons/ui/FlxUIGroup.hx
Original file line number Diff line number Diff line change
@@ -1,152 +1,139 @@
package flixel.addons.ui;

import flixel.addons.ui.interfaces.IFlxUIWidget;
import flixel.FlxSprite;
import flixel.group.FlxSpriteGroup;
import flixel.math.FlxRect;
import flixel.addons.ui.interfaces.IFlxUIWidget;

#if (flixel < "5.4.0" && FLX_NO_POINT_POOL)
/* This is a weird haxe bug I haven't figured out, fixed in 5.4.0
* via https://github.com/HaxeFlixel/flixel/pull/2808
* Note: this is only the case when FLX_NO_POINT_POOL is defined.
*/
#error "This version of flixel-ui is not compatible with flixel versions less than 5.4.0";
#end

typedef FlxUIGroup = FlxTypedUIGroup<FlxSprite>;
/**
* A cheap extension of FlxUIGroup that lets you move all the children around
* A cheap extension of FlxSpriteGroup that lets you move all the children around
* without having to call reset()
* @author Lars Doucet
*/
class FlxUIGroup extends FlxSpriteGroup implements IFlxUIWidget
class FlxTypedUIGroup<T:FlxSprite> extends FlxTypedSpriteGroup<T> implements IFlxUIWidget
{
/***PUBLIC VARS***/
// a handy string handler name for this thing
/** a handy string handler name for this thing */
public var name:String;

/** If true, will issue FlxUI.event() and FlxUI.request() calls */
public var broadcastToFlxUI:Bool = true;

/***PUBLIC GETTER/SETTERS***/
// public var velocity:FlxPoint;
/** Will automatically adjust the width and height to the members, on add/remove calls */
public var autoBounds:Bool = true;

/***PUBLIC FUNCTIONS***/
public function new(X:Float = 0, Y:Float = 0)
public function new(x = 0.0, Y = 0.0)
{
super(X, Y);
super(x, y);
}

public override function destroy():Void
override function add(sprite:T):T
{
super.destroy();
}

public override function add(Object:FlxSprite):FlxSprite
{
var obj = super.add(Object);
final obj = super.add(sprite);
if (autoBounds)
{
calcBounds();
}
return obj;
return sprite;
}

public override function remove(Object:FlxSprite, Splice:Bool = false):FlxSprite
public override function remove(sprite:T, splice:Bool = false):T
{
var obj = super.remove(Object, Splice);
final obj = super.remove(sprite, splice);
if (autoBounds)
{
calcBounds();
}
return obj;
}

public function setScrollFactor(X:Float, Y:Float):Void
public function setScrollFactor(x:Float, y:Float):Void
{
for (obj in members)
for (sprite in members)
{
if (obj != null)
if (sprite != null)
{
obj.scrollFactor.set(X, Y);
sprite.scrollFactor.set(x, y);
}
}
}

public function hasThis(Object:FlxSprite):Bool
/**
* Whether this group contains the sprite.
*/
@:deprecated("Use contains, instead")
inline public function hasThis(sprite:T):Bool
{
for (obj in members)
{
if (obj == Object)
{
return true;
}
}
return false;
return contains(sprite);
}

/**
* Whether this group contains the sprite.
*/
public function contains(sprite:T):Bool
{
return members.contains(sprite);
}

/**
* Calculates the bounds of the group and sets width/height
* @param rect (optional) -- if supplied, populates this with the boundaries of the group
* @param rect If supplied, populates this with the boundaries of the group
*/
public function calcBounds(rect:FlxRect = null)
public function calcBounds(?rect:FlxRect)
{
if (members != null && members.length > 0)
if (members == null || members.length == 0)
{
width = height = 0;
if (rect != null) rect.set();
return;
}

var left:Float = Math.POSITIVE_INFINITY;
var right:Float = Math.NEGATIVE_INFINITY;
var top:Float = Math.POSITIVE_INFINITY;
var bottom:Float = Math.NEGATIVE_INFINITY;
for (sprite in members)
{
var left:Float = Math.POSITIVE_INFINITY;
var right:Float = Math.NEGATIVE_INFINITY;
var top:Float = Math.POSITIVE_INFINITY;
var bottom:Float = Math.NEGATIVE_INFINITY;
for (fb in members)
if (sprite != null)
{
if (fb != null)
if (sprite.x < left)
{
if ((fb is IFlxUIWidget))
{
var flui:FlxSprite = cast fb;
if (flui.x < left)
{
left = flui.x;
}
if (flui.x + flui.width > right)
{
right = flui.x + flui.width;
}
if (flui.y < top)
{
top = flui.y;
}
if (flui.y + flui.height > bottom)
{
bottom = flui.y + flui.height;
}
}
else if ((fb is FlxSprite))
{
var flxi:FlxSprite = cast fb;
if (flxi.x < left)
{
left = flxi.x;
}
if (flxi.x > right)
{
right = flxi.x;
}
if (flxi.y < top)
{
top = flxi.y;
}
if (flxi.y > bottom)
{
bottom = flxi.y;
}
}
left = sprite.x;
}

if (sprite.x + sprite.width > right)
{
right = sprite.x + sprite.width;
}

if (sprite.y < top)
{
top = sprite.y;
}

if (sprite.y + sprite.height > bottom)
{
bottom = sprite.y + sprite.height;
}
}
width = (right - left);
height = (bottom - top);
if (rect != null)
{
rect.x = left;
rect.y = top;
rect.width = width;
rect.height = height;
}
}
else

width = (right - left);
height = (bottom - top);
if (rect != null)
{
width = height = 0;
rect.x = left;
rect.y = top;
rect.width = width;
rect.height = height;
}
}

Expand All @@ -155,12 +142,10 @@ class FlxUIGroup extends FlxSpriteGroup implements IFlxUIWidget
*/
public function floorAll():Void
{
var fs:FlxSprite = null;
for (fb in members)
for (sprite in members)
{
fs = cast fb;
fs.x = Math.floor(fs.x);
fs.y = Math.floor(fs.y);
sprite.x = Math.floor(sprite.x);
sprite.y = Math.floor(sprite.y);
}
}
}
2 changes: 1 addition & 1 deletion flixel/addons/ui/FlxUITabMenu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class FlxUITabMenu extends FlxUIGroup implements IResizable implements IFlxUICli
return; // DO NOT ADD A GROUP TO ITSELF
}

if (!hasThis(g))
if (!contains(g))
{ // ONLY ADD IF IT DOESN'T EXIST
g.y = (_back.y - y);
add(g);
Expand Down
5 changes: 4 additions & 1 deletion flixel/addons/ui/interfaces/IFlxUIWidget.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ interface IFlxUIWidget extends IFlxSprite
public var width(get, set):Float;
public var height(get, set):Float;

public var broadcastToFlxUI:Bool; // if false, does not issue FlxUI.event() and FlxUI.request() calls
/**
* If true, will issue FlxUI.event() and FlxUI.request() calls
*/
public var broadcastToFlxUI:Bool;
}

0 comments on commit 3bbac0f

Please sign in to comment.