From ae50ac918822883712f0490694f58a149c5ba20a Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 19 May 2023 11:41:19 -0500 Subject: [PATCH 1/4] add FlxTypedUIGroup and some docs --- flixel/addons/ui/FlxUIGroup.hx | 172 +++++++++----------- flixel/addons/ui/interfaces/IFlxUIWidget.hx | 5 +- 2 files changed, 78 insertions(+), 99 deletions(-) diff --git a/flixel/addons/ui/FlxUIGroup.hx b/flixel/addons/ui/FlxUIGroup.hx index 0d37b09e..27c55533 100644 --- a/flixel/addons/ui/FlxUIGroup.hx +++ b/flixel/addons/ui/FlxUIGroup.hx @@ -1,51 +1,45 @@ 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; +typedef FlxUIGroup = FlxTypedUIGroup; /** - * 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 extends FlxTypedSpriteGroup 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(); @@ -53,100 +47,84 @@ class FlxUIGroup extends FlxSpriteGroup implements IFlxUIWidget 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) { - 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 (rect != null) rect.set(); + return width = height = 0; + } + + 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) + { + 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; } } @@ -155,12 +133,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); } } } diff --git a/flixel/addons/ui/interfaces/IFlxUIWidget.hx b/flixel/addons/ui/interfaces/IFlxUIWidget.hx index 0bfded8a..416b2d0d 100644 --- a/flixel/addons/ui/interfaces/IFlxUIWidget.hx +++ b/flixel/addons/ui/interfaces/IFlxUIWidget.hx @@ -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; } From c8706cf55de320a0daa2100bba44b26e8b724f0c Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 19 May 2023 12:12:22 -0500 Subject: [PATCH 2/4] fix errors --- flixel/addons/ui/FlxUI.hx | 59 ++++++++++++++++---------------- flixel/addons/ui/FlxUIGroup.hx | 3 +- flixel/addons/ui/FlxUITabMenu.hx | 2 +- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/flixel/addons/ui/FlxUI.hx b/flixel/addons/ui/FlxUI.hx index 92cbc4db..24c8a80e 100644 --- a/flixel/addons/ui/FlxUI.hx +++ b/flixel/addons/ui/FlxUI.hx @@ -1,22 +1,34 @@ package flixel.addons.ui; +import flash.Lib; import flash.display.BitmapData; import flash.errors.Error; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; -import flash.Lib; -import flixel.addons.ui.FlxUI.MaxMinSize; + +import flixel.FlxG; +import flixel.FlxObject; +import flixel.FlxSprite; +import flixel.FlxState; +import flixel.group.FlxSpriteGroup; +import flixel.system.FlxAssets; +import flixel.text.FlxText; +import flixel.ui.FlxBar; +import flixel.util.FlxArrayUtil; +import flixel.util.FlxColor; +import flixel.math.FlxPoint; +import flixel.util.FlxStringUtil; + 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.BorderDef; -import flixel.addons.ui.FlxUILine.LineAxis; -import flixel.addons.ui.FlxUIRadioGroup.CheckStyle; -import flixel.addons.ui.FlxUITooltipManager.FlxUITooltipData; +import flixel.addons.ui.FlxUIBar; +import flixel.addons.ui.FlxUICursor; +import flixel.addons.ui.FlxUIDropDownMenu; +import flixel.addons.ui.FlxUIGroup; +import flixel.addons.ui.FlxUILine; +import flixel.addons.ui.FlxUIRadioGroup; +import flixel.addons.ui.FlxUITooltipManager; import flixel.addons.ui.FontDef; import flixel.addons.ui.interfaces.IEventGetter; import flixel.addons.ui.interfaces.IFireTongue; @@ -27,18 +39,7 @@ import flixel.addons.ui.interfaces.IFlxUIWidget; import flixel.addons.ui.interfaces.IHasParams; import flixel.addons.ui.interfaces.ILabeled; import flixel.addons.ui.interfaces.IResizable; -import flixel.FlxG; -import flixel.FlxObject; -import flixel.FlxSprite; -import flixel.FlxState; -import flixel.group.FlxSpriteGroup; -import flixel.system.FlxAssets; -import flixel.text.FlxText; -import flixel.ui.FlxBar.FlxBarFillDirection; -import flixel.util.FlxArrayUtil; -import flixel.util.FlxColor; -import flixel.math.FlxPoint; -import flixel.util.FlxStringUtil; + import openfl.Assets; import openfl.text.TextFormat; #if haxe4 @@ -132,17 +133,17 @@ class FlxUI extends FlxUIGroup implements IEventGetter */ private function _tongueSet(list:Array, 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 = 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; } } } diff --git a/flixel/addons/ui/FlxUIGroup.hx b/flixel/addons/ui/FlxUIGroup.hx index 27c55533..119e28dd 100644 --- a/flixel/addons/ui/FlxUIGroup.hx +++ b/flixel/addons/ui/FlxUIGroup.hx @@ -83,8 +83,9 @@ class FlxTypedUIGroup extends FlxTypedSpriteGroup implements IFl { if (members == null || members.length == 0) { + width = height = 0; if (rect != null) rect.set(); - return width = height = 0; + return; } var left:Float = Math.POSITIVE_INFINITY; diff --git a/flixel/addons/ui/FlxUITabMenu.hx b/flixel/addons/ui/FlxUITabMenu.hx index c3279b26..ac395767 100644 --- a/flixel/addons/ui/FlxUITabMenu.hx +++ b/flixel/addons/ui/FlxUITabMenu.hx @@ -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); From 0a129dd5c9ba245d6ef529272bbba8777f1c3a4b Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 20 May 2023 11:57:28 -0500 Subject: [PATCH 3/4] throw error for incompatible flixel versions --- flixel/addons/ui/FlxUIGroup.hx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/flixel/addons/ui/FlxUIGroup.hx b/flixel/addons/ui/FlxUIGroup.hx index 119e28dd..12702777 100644 --- a/flixel/addons/ui/FlxUIGroup.hx +++ b/flixel/addons/ui/FlxUIGroup.hx @@ -5,6 +5,14 @@ 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 is not compatile with flixel-ui versions 2.6.0 or higher. Update to flixel 5.4.0 or later"; +#end + typedef FlxUIGroup = FlxTypedUIGroup; /** * A cheap extension of FlxSpriteGroup that lets you move all the children around From df08770feaf9213e743ef8eba3727af6381071ad Mon Sep 17 00:00:00 2001 From: George FunBook Date: Sat, 20 May 2023 12:00:20 -0500 Subject: [PATCH 4/4] update error message --- flixel/addons/ui/FlxUIGroup.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/addons/ui/FlxUIGroup.hx b/flixel/addons/ui/FlxUIGroup.hx index 12702777..625e64cb 100644 --- a/flixel/addons/ui/FlxUIGroup.hx +++ b/flixel/addons/ui/FlxUIGroup.hx @@ -10,7 +10,7 @@ import flixel.addons.ui.interfaces.IFlxUIWidget; * 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 is not compatile with flixel-ui versions 2.6.0 or higher. Update to flixel 5.4.0 or later"; + #error "This version of flixel-ui is not compatible with flixel versions less than 5.4.0"; #end typedef FlxUIGroup = FlxTypedUIGroup;