diff --git a/src/Main.hx b/src/Main.hx index 31f26ffc..03595658 100644 --- a/src/Main.hx +++ b/src/Main.hx @@ -1,5 +1,8 @@ package; +import gui.GuiControl.MouseState; +import hxd.Window; +import gui.Canvas; import gui.MainMenuGui; import hxd.res.DefaultFont; import h2d.Text; @@ -48,7 +51,7 @@ class Main extends hxd.App { var world:MarbleWorld; var dtsObj:DtsObject; - var mmg:MainMenuGui; + var canvas:Canvas; var fpsCounter:Text; @@ -62,8 +65,9 @@ class Main extends hxd.App { var mission = new Mission(); mission.root = mis.root; - mmg = new MainMenuGui(); - mmg.init(s2d); + canvas = new Canvas(s2d); + + canvas.setContent(new MainMenuGui()); // world = new MarbleWorld(s3d, s2d, mission); @@ -77,7 +81,11 @@ class Main extends hxd.App { override function update(dt:Float) { super.update(dt); - mmg.update(dt); + var wnd = Window.getInstance(); + var mouseState:MouseState = { + position: new Vector(wnd.mouseX, wnd.mouseY) + } + canvas.update(dt, mouseState); // world.update(dt); fpsCounter.text = 'FPS: ${this.engine.fps}'; } diff --git a/src/gui/Canvas.hx b/src/gui/Canvas.hx new file mode 100644 index 00000000..bcea05e0 --- /dev/null +++ b/src/gui/Canvas.hx @@ -0,0 +1,29 @@ +package gui; + +import h3d.Vector; +import h2d.Scene; +import gui.GuiControl.MouseState; + +class Canvas extends GuiControl { + var scene2d:Scene; + + public function new(scene) { + super(); + this.scene2d = scene; + + this.position = new Vector(); + this.extent = new Vector(scene.width, scene.height); + this.horizSizing = Width; + this.vertSizing = Height; + } + + public function setContent(content:GuiControl) { + this.dispose(); + this.addChild(content); + this.render(scene2d); + } + + public override function update(dt:Float, mouseState:MouseState) { + super.update(dt, mouseState); + } +} diff --git a/src/gui/GuiAnim.hx b/src/gui/GuiAnim.hx index 2d88afc4..d89c7a3d 100644 --- a/src/gui/GuiAnim.hx +++ b/src/gui/GuiAnim.hx @@ -23,4 +23,9 @@ class GuiAnim extends GuiControl { scene2d.addChild(anim); super.render(scene2d); } + + public override function dispose() { + super.dispose(); + this.anim.remove(); + } } diff --git a/src/gui/GuiControl.hx b/src/gui/GuiControl.hx index 8a9f9a9d..a71f2a5e 100644 --- a/src/gui/GuiControl.hx +++ b/src/gui/GuiControl.hx @@ -108,4 +108,18 @@ class GuiControl { this.children.push(ctrl); ctrl.parent = this; } + + public function removeChildren() { + for (c in this.children) { + c.parent = null; + } + this.children = []; + } + + public function dispose() { + for (c in this.children) { + c.dispose(); + } + this.children = []; + } } diff --git a/src/gui/GuiImage.hx b/src/gui/GuiImage.hx index 5f674e39..0626b699 100644 --- a/src/gui/GuiImage.hx +++ b/src/gui/GuiImage.hx @@ -22,4 +22,9 @@ class GuiImage extends GuiControl { scene2d.addChild(bmp); super.render(scene2d); } + + public override function dispose() { + super.dispose(); + this.bmp.remove(); + } } diff --git a/src/gui/GuiText.hx b/src/gui/GuiText.hx index d4e6993c..0b66a867 100644 --- a/src/gui/GuiText.hx +++ b/src/gui/GuiText.hx @@ -38,4 +38,9 @@ class GuiText extends GuiControl { scene2d.addChild(text); super.render(scene2d); } + + public override function dispose() { + super.dispose(); + this.text.remove(); + } } diff --git a/src/gui/MainMenuGui.hx b/src/gui/MainMenuGui.hx index 2c34204d..2d1e7443 100644 --- a/src/gui/MainMenuGui.hx +++ b/src/gui/MainMenuGui.hx @@ -6,25 +6,17 @@ import hxd.res.BitmapFont; import h3d.Vector; import src.ResourceLoader; -class MainMenuGui { - var scene2d:h2d.Scene; - - var mainCtrl:GuiControl; - - public function new() {} - - public function init(scene2d:h2d.Scene) { - this.scene2d = scene2d; - +class MainMenuGui extends GuiImage { + public function new() { + super(ResourceLoader.getImage("data/ui/background.jpg").toTile()); var fontdata = ResourceLoader.loader.load("data/font/DomCasual32px.fnt"); var bfont = new BitmapFont(fontdata.entry); @:privateAccess bfont.loader = ResourceLoader.loader; - mainCtrl = new GuiImage(ResourceLoader.getImage("data/ui/background.jpg").toTile()); - mainCtrl.horizSizing = Width; - mainCtrl.vertSizing = Height; - mainCtrl.position = new Vector(); - mainCtrl.extent = new Vector(640, 480); + this.horizSizing = Width; + this.vertSizing = Height; + this.position = new Vector(); + this.extent = new Vector(640, 480); var versionText = new GuiText(bfont); versionText.horizSizing = Center; @@ -32,14 +24,14 @@ class MainMenuGui { versionText.position = new Vector(289, 457); versionText.extent = new Vector(62, 18); versionText.text.text = "1.0.0"; - mainCtrl.addChild(versionText); + this.addChild(versionText); var homebase = new GuiImage(ResourceLoader.getImage("data/ui/home/homegui.png").toTile()); homebase.horizSizing = Center; homebase.vertSizing = Center; homebase.extent = new Vector(349, 477); homebase.position = new Vector(145, 1); - mainCtrl.addChild(homebase); + this.addChild(homebase); function loadButtonImages(path:String) { var normal = ResourceLoader.getImage('${path}_n.png').toTile(); @@ -67,15 +59,5 @@ class MainMenuGui { exitButton.position = new Vector(82, 358); exitButton.extent = new Vector(203, 88); homebase.addChild(exitButton); - - mainCtrl.render(scene2d); - } - - public function update(dt:Float) { - var wnd = Window.getInstance(); - var mouseState:MouseState = { - position: new Vector(wnd.mouseX, wnd.mouseY) - } - mainCtrl.update(dt, mouseState); } }