From e0e4109bde54ea66313b8da5ee44e239333b1f84 Mon Sep 17 00:00:00 2001 From: andreas Date: Fri, 11 Jun 2021 13:13:11 +0200 Subject: [PATCH] commiting changes to separate branch for later use --- hGameTest/src/game/ui/UIContianer.hx | 27 --- hGameTest/src/game/ui/UIElement.hx | 12 -- hGameTest/src/game/ui/UIPane.hx | 263 ++++++++++++++++++++++- hGameTest/src/game/ui/console/Console.hx | 38 +++- 4 files changed, 281 insertions(+), 59 deletions(-) delete mode 100644 hGameTest/src/game/ui/UIContianer.hx delete mode 100644 hGameTest/src/game/ui/UIElement.hx diff --git a/hGameTest/src/game/ui/UIContianer.hx b/hGameTest/src/game/ui/UIContianer.hx deleted file mode 100644 index 35696022..00000000 --- a/hGameTest/src/game/ui/UIContianer.hx +++ /dev/null @@ -1,27 +0,0 @@ -package game.ui; - -import openfl.geom.Point; -import openfl.display.Sprite; - - -class UIContianer extends Sprite{ - public static var UIDrawList:Array=[]; - public var anchor:Point; - public function new(panchor,scale){ - super(); - } - public function onResize(){ - - } - public function hide(){ - - } - public function show(){ - - } - public function getParent() - { - return parent; - } - -} diff --git a/hGameTest/src/game/ui/UIElement.hx b/hGameTest/src/game/ui/UIElement.hx deleted file mode 100644 index 5b1ff09b..00000000 --- a/hGameTest/src/game/ui/UIElement.hx +++ /dev/null @@ -1,12 +0,0 @@ -package game.ui; - -import openfl.geom.Point; -import openfl.display.Sprite; - -class UIElement extends Sprite -{ - public function new(){ - super(); - } - -} \ No newline at end of file diff --git a/hGameTest/src/game/ui/UIPane.hx b/hGameTest/src/game/ui/UIPane.hx index 873491d2..77f9b731 100644 --- a/hGameTest/src/game/ui/UIPane.hx +++ b/hGameTest/src/game/ui/UIPane.hx @@ -1,15 +1,258 @@ package game.ui; -class UIPane{ - public static var paneList:Array=[]; - public var x:Int; - public var y:Int; - public var width:Int; - public var height:Int; - public var parent:UIPane; - - public function new(){ +import openfl.display.Sprite; +import openfl.display.BitmapData; +import openfl.display.DisplayObject; +import openfl.text.TextField; +typedef P2d = { + x:Float, + y:Float +} +enum PaneLayout{ + HORIZONTAL; + VERTICAL; +} +enum PaneAnchor{ + LEFT; + TOPLEFT; + TOP; + TOPRIGHT; + RIGHT; + BOTTOMLEFT; + BOTTOM; + BOTTOMRIGHT; +} +enum ExpandBehavior{ + FACTOR; + ABSOLUTE; + STRETCH; + FILL; + FIT; + NONE; +} +enum PaneAlign{ + START; + END; +} + +typedef PaneDimensions = { + var width:Float; + var height:Float; + @:optional var expandBehavior:ExpandBehavior; + @:optional var minWidth:Float; + @:optional var minHeight:Float; + @:optional var maxWidth:Float; + @:optional var maxHeight:Float; +} + +typedef PaneProps = { + dimensions:PaneDimensions, + layout:PaneLayout +} + +class UIPane{ + public var name:String; + public var sprite:Sprite; + public var x(get, set):Float; + public var y(get, set):Float; + function get_x(){ return sprite.x; } + function set_x(x){ return sprite.x = x; } + function get_y(){ return sprite.y; } + function set_y(y){ return sprite.y = y; } + public var dimensions:PaneDimensions; + public var width(get,set):Float; + function get_width(){ + return dimensions.width; + } + function set_width(width){ + dimensions.width = width; + onResize(); + return width; + } + public var height(get,set):Float; + function get_height(){ + return dimensions.height; + } + function set_height(height){ + dimensions.height = height; + onResize(); + return height; + } + public var layout:PaneLayout; + public var autoArrange:Bool = true; + public var autoArrangeChildren = true; + public var expand:Bool; + public var children:Array = []; + public var parent:UIPane = null; + public var align:PaneAlign = START; + public var maskSprite:Sprite; + public function new(_name:String, _dimensions:PaneDimensions) + { + // Set name + name = _name; + // Set dimensions + if(_dimensions != null){ + dimensions = _dimensions; + } + else{ + dimensions = { + width: 0, + height: 0, + minWidth: 0, + minHeight: 0, + maxWidth: 0, + maxHeight: 0 + }; + } + + // Initialize Sprite + initSprite(); + // Draw debug pane for visualizing + //drawDebugPane(); + } + public function getAnchorOffset(anchor:PaneAnchor):P2d + { + switch(anchor){ + case LEFT: + return {x:0,y:(height/2)}; + case TOPLEFT: + return {x:0, y:0}; + case TOP: + return {x:width/2,y:0}; + case TOPRIGHT: + return {x:width, y:0}; + case RIGHT: + return {x:width, y:height/2} + case BOTTOMRIGHT: + return {x:width, y:height}; + case BOTTOM: + return {x:width/2, y:height}; + case BOTTOMLEFT: + return {x:0, y:height}; + } + } + public function initSprite():Void + { + + // Construct Sprite object + sprite = new Sprite(); + + // Draw mask + //maskSprite = new Sprite(); + //drawMask(); + //sprite.addChild(maskSprite); + //sprite.mask = maskSprite; } -} \ No newline at end of file + public function drawMask():Void + { + maskSprite.graphics.clear(); + maskSprite.graphics.beginFill(0xffffff); + maskSprite.graphics.drawRect(0,0,width,height); + } + + + public function drawDebugPane() + { + // Clear graphics + sprite.graphics.clear(); + // Create objects we need to draw. + var label:TextField = new TextField(); + var bmp = new BitmapData(Std.int(width),Std.int(height),true,0x00000000); + // Set textfield text to the panel name + label.text = name; + // Draw textfield to bitmap + bmp.draw(label); + // Draw graphics + sprite.graphics.beginFill(Std.int(Math.random()*0xffffff),0.5); + sprite.graphics.drawRect(0,0,dimensions.width,dimensions.height); + sprite.graphics.beginBitmapFill(bmp,null); + sprite.graphics.drawRect(0,0,dimensions.width,dimensions.height); + + if(true){ + sprite.graphics.beginFill(Std.int(Math.random()*0xffffff),0.5); + sprite.graphics.drawCircle(getAnchorOffset(TOPLEFT).x,getAnchorOffset(TOPLEFT).y,5); + sprite.graphics.drawCircle(getAnchorOffset(TOP).x,getAnchorOffset(TOP).y,5); + sprite.graphics.drawCircle(getAnchorOffset(TOPRIGHT).x,getAnchorOffset(TOPRIGHT).y,5); + sprite.graphics.drawCircle(getAnchorOffset(RIGHT).x,getAnchorOffset(RIGHT).y,5); + sprite.graphics.drawCircle(getAnchorOffset(BOTTOMRIGHT).x,getAnchorOffset(BOTTOMRIGHT).y,5); + sprite.graphics.drawCircle(getAnchorOffset(BOTTOM).x,getAnchorOffset(BOTTOM).y,5); + sprite.graphics.drawCircle(getAnchorOffset(BOTTOMLEFT).x,getAnchorOffset(BOTTOMLEFT).y,5); + sprite.graphics.drawCircle(getAnchorOffset(LEFT).x,getAnchorOffset(LEFT).y,5); + } + + } + public var endOffset:Float = 0; + public var startOffset:Float = 0; + public function addChild(child:UIPane) + { + children.push(child); + sprite.addChild(child.sprite); + child.parent = this; + if(child.autoArrange && autoArrangeChildren){ + arrangeChild(child); + } + return child; + } + public function redraw():Void + { + //drawDebugPane(); + //drawMask(); + } + public function onResize(){ + redraw(); + if(autoArrangeChildren){ + arrangeChildren(); + } + } + private function arrangeChild(child:UIPane){ + if(child.autoArrange){ + switch(layout){ + case HORIZONTAL: + if(endOffset == 0) endOffset = width; + switch(child.dimensions.expandBehavior){ + case STRETCH: + child.width = endOffset-startOffset; + child.height = height; + default: + child.height = height; + // + } + switch(child.align){ + case START: + child.x = startOffset; + startOffset += child.width; + case END: + child.x = endOffset-= child.width; + } + case VERTICAL: + if(endOffset == 0) endOffset = height; + switch(child.dimensions.expandBehavior){ + case STRETCH: + child.height = endOffset-startOffset; + child.width = width; + default: + child.width = width; + // + } + switch(child.align){ + case START: + child.y = startOffset; + startOffset += child.height; + case END: + child.y = endOffset-=child.height; + } + } + + } + } + public function arrangeChildren(){ + startOffset = 0; + endOffset = 0; + for(child in children){ + arrangeChild(child); + } + } + +} diff --git a/hGameTest/src/game/ui/console/Console.hx b/hGameTest/src/game/ui/console/Console.hx index cd2c75a4..b9dbb55e 100644 --- a/hGameTest/src/game/ui/console/Console.hx +++ b/hGameTest/src/game/ui/console/Console.hx @@ -1,7 +1,5 @@ package game.ui.console; -import engine.enums.CVarFlag; -import game.ui.text.TextFormats; import assets.Fonts; import openfl.Lib; import openfl.events.Event; @@ -11,10 +9,15 @@ import openfl.display.Sprite; import openfl.Assets; import openfl.text.TextFormat; import openfl.text.TextFieldType; +// Engine Imports +import engine.enums.CVarFlag; import engine.typedefs.CVar; -import game.ui.ConVar; import engine.typedefs.CCmd; +// Game imports +import game.ui.ConVar; +import game.ui.text.TextFormats; import game.ui.console.elements.ConsoleInput; +import game.ui.UIPane; @@ -31,8 +34,20 @@ class Console extends Sprite{ consoleInstance = this; - - graphics.beginFill(0x555555); + var consolePane:UIPane = new UIPane("console pane",{width: 800, height: 600}); + var topBar:UIPane = new UIPane("top bar", {width: 800, height: 32}); + var bottomBar:UIPane = new UIPane("input bar", {width: 800, height: 32}); + var outputPane:UIPane = new UIPane("output pane", {width: 800, height: 1}); + outputPane.dimensions.expandBehavior = STRETCH; + bottomBar.align = END; + outputPane.align = END; + consolePane.layout = VERTICAL; + topBar.align = START; + consolePane.addChild(topBar); + consolePane.addChild(bottomBar); + consolePane.addChild(outputPane); + addChild(consolePane.sprite); + //graphics.beginFill(0x555555); graphics.drawRect(0,0,800,600); cInput = new ConsoleInput(); @@ -55,11 +70,14 @@ class Console extends Sprite{ cOut.multiline = true; cOut.background = true; cOut.backgroundColor = Std.parseInt(cvar_mat_consolebg.value); - cOut.width = 800 - 24; - cOut.height = 600 - cIn.height - 38; - cOut.y = 12; - cOut.x = 12; + cOut.width = outputPane.dimensions.width; + cOut.height = outputPane.dimensions.height; + cOut.y = 0; + cOut.x = 0; + + + cAutoComp = new TextField(); cAutoComp.text = ""; cAutoComp.defaultTextFormat = TextFormats.getFormats().cInputFmt; @@ -81,7 +99,7 @@ class Console extends Sprite{ cOut.addEventListener(Event.CHANGE, onOutputTextChange); cIn.addEventListener(Event.CHANGE, onInputTextChange); //cOut.addEventListener() - this.addChild(cOut); + outputPane.sprite.addChild(cOut); this.addChild(cInput); this.addChild(cAutoComp); //ConVar.registerCVar("echo", CVarType.CCmd, null, devMsg())