mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2025-10-30 08:11:25 +00:00
Gui renderer shit
This commit is contained in:
parent
e8cd086e80
commit
b35c71755a
9 changed files with 142 additions and 10 deletions
17
src/Main.hx
17
src/Main.hx
|
|
@ -1,5 +1,6 @@
|
|||
package;
|
||||
|
||||
import gui.MainMenuGui;
|
||||
import hxd.res.DefaultFont;
|
||||
import h2d.Text;
|
||||
import src.Mission;
|
||||
|
|
@ -47,6 +48,8 @@ class Main extends hxd.App {
|
|||
var world:MarbleWorld;
|
||||
var dtsObj:DtsObject;
|
||||
|
||||
var mmg:MainMenuGui;
|
||||
|
||||
var fpsCounter:Text;
|
||||
|
||||
override function init() {
|
||||
|
|
@ -59,10 +62,13 @@ class Main extends hxd.App {
|
|||
var mission = new Mission();
|
||||
mission.root = mis.root;
|
||||
|
||||
world = new MarbleWorld(s3d, s2d, mission);
|
||||
mmg = new MainMenuGui();
|
||||
mmg.init(s2d);
|
||||
|
||||
world.init();
|
||||
world.start();
|
||||
// world = new MarbleWorld(s3d, s2d, mission);
|
||||
|
||||
// world.init();
|
||||
// world.start();
|
||||
|
||||
fpsCounter = new Text(DefaultFont.get(), s2d);
|
||||
fpsCounter.y = 40;
|
||||
|
|
@ -71,12 +77,13 @@ class Main extends hxd.App {
|
|||
|
||||
override function update(dt:Float) {
|
||||
super.update(dt);
|
||||
world.update(dt);
|
||||
mmg.update(dt);
|
||||
// world.update(dt);
|
||||
fpsCounter.text = 'FPS: ${this.engine.fps}';
|
||||
}
|
||||
|
||||
override function render(e:h3d.Engine) {
|
||||
this.world.render(e);
|
||||
// this.world.render(e);
|
||||
super.render(e);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,12 +15,12 @@ class GuiAnim extends GuiControl {
|
|||
}
|
||||
|
||||
public override function render(scene2d:Scene) {
|
||||
super.render(scene2d);
|
||||
var renderRect = this.getRenderRectangle();
|
||||
anim.setPosition(renderRect.position.x, renderRect.position.y);
|
||||
anim.scaleX = renderRect.extent.x / anim.getFrame().width;
|
||||
anim.scaleY = renderRect.extent.y / anim.getFrame().height;
|
||||
if (!scene2d.contains(anim))
|
||||
scene2d.addChild(anim);
|
||||
super.render(scene2d);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
29
src/gui/GuiButton.hx
Normal file
29
src/gui/GuiButton.hx
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package gui;
|
||||
|
||||
import hxd.Key;
|
||||
import gui.GuiControl.MouseState;
|
||||
import hxd.Window;
|
||||
import h2d.Tile;
|
||||
|
||||
class GuiButton extends GuiAnim {
|
||||
// 0 is normal
|
||||
// 1 is hover
|
||||
// 2 is pressed
|
||||
// 3 is disabled
|
||||
public function new(anim:Array<Tile>) {
|
||||
super(anim);
|
||||
}
|
||||
|
||||
public override function update(dt:Float, mouseState:MouseState) {
|
||||
var renderRect = getRenderRectangle();
|
||||
if (renderRect.inRect(mouseState.position)) {
|
||||
if (Key.isDown(Key.MOUSE_LEFT)) {
|
||||
this.anim.currentFrame = 2;
|
||||
} else {
|
||||
this.anim.currentFrame = 1;
|
||||
}
|
||||
} else
|
||||
this.anim.currentFrame = 0;
|
||||
super.update(dt, mouseState);
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,10 @@ enum VertSizing {
|
|||
Relative;
|
||||
}
|
||||
|
||||
typedef MouseState = {
|
||||
var position:Vector;
|
||||
}
|
||||
|
||||
@:publicFields
|
||||
class GuiControl {
|
||||
var horizSizing:HorizSizing = Right;
|
||||
|
|
@ -41,6 +45,12 @@ class GuiControl {
|
|||
}
|
||||
}
|
||||
|
||||
public function update(dt:Float, mouseState:MouseState) {
|
||||
for (c in children) {
|
||||
c.update(dt, mouseState);
|
||||
}
|
||||
}
|
||||
|
||||
public function getRenderRectangle() {
|
||||
var rect = new Rect(this.position, this.extent);
|
||||
var parentRect:Rect = null;
|
||||
|
|
|
|||
|
|
@ -14,12 +14,12 @@ class GuiImage extends GuiControl {
|
|||
}
|
||||
|
||||
public override function render(scene2d:Scene) {
|
||||
super.render(scene2d);
|
||||
var renderRect = this.getRenderRectangle();
|
||||
bmp.setPosition(renderRect.position.x, renderRect.position.y);
|
||||
bmp.scaleX = renderRect.extent.x / bmp.tile.width;
|
||||
bmp.scaleY = renderRect.extent.y / bmp.tile.height;
|
||||
if (!scene2d.contains(bmp))
|
||||
scene2d.addChild(bmp);
|
||||
super.render(scene2d);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ class GuiText extends GuiControl {
|
|||
}
|
||||
|
||||
public override function render(scene2d:Scene) {
|
||||
super.render(scene2d);
|
||||
var renderRect = this.getRenderRectangle();
|
||||
if (justify == Left) {
|
||||
text.setPosition(renderRect.position.x, renderRect.position.y);
|
||||
|
|
@ -37,5 +36,6 @@ class GuiText extends GuiControl {
|
|||
}
|
||||
if (!scene2d.contains(text))
|
||||
scene2d.addChild(text);
|
||||
super.render(scene2d);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
81
src/gui/MainMenuGui.hx
Normal file
81
src/gui/MainMenuGui.hx
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
package gui;
|
||||
|
||||
import hxd.Window;
|
||||
import gui.GuiControl.MouseState;
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
var versionText = new GuiText(bfont);
|
||||
versionText.horizSizing = Center;
|
||||
versionText.vertSizing = Top;
|
||||
versionText.position = new Vector(289, 457);
|
||||
versionText.extent = new Vector(62, 18);
|
||||
versionText.text.text = "1.0.0";
|
||||
mainCtrl.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);
|
||||
|
||||
function loadButtonImages(path:String) {
|
||||
var normal = ResourceLoader.getImage('${path}_n.png').toTile();
|
||||
var hover = ResourceLoader.getImage('${path}_h.png').toTile();
|
||||
var pressed = ResourceLoader.getImage('${path}_d.png').toTile();
|
||||
return [normal, hover, pressed];
|
||||
}
|
||||
|
||||
var playButton = new GuiButton(loadButtonImages("data/ui/home/play"));
|
||||
playButton.position = new Vector(50, 113);
|
||||
playButton.extent = new Vector(270, 95);
|
||||
homebase.addChild(playButton);
|
||||
|
||||
var helpButton = new GuiButton(loadButtonImages("data/ui/home/help"));
|
||||
helpButton.position = new Vector(59, 200);
|
||||
helpButton.extent = new Vector(242, 84);
|
||||
homebase.addChild(helpButton);
|
||||
|
||||
var optionsButton = new GuiButton(loadButtonImages("data/ui/home/options"));
|
||||
optionsButton.position = new Vector(55, 279);
|
||||
optionsButton.extent = new Vector(253, 83);
|
||||
homebase.addChild(optionsButton);
|
||||
|
||||
var exitButton = new GuiButton(loadButtonImages("data/ui/home/exit"));
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -308,7 +308,7 @@ class PlayGui {
|
|||
public function setAlertText(text:String) {
|
||||
this.alertTextForeground.text.text = text;
|
||||
this.alertTextBackground.text.text = text;
|
||||
alertTextBackground.render(scene2d);
|
||||
// alertTextBackground.render(scene2d);
|
||||
// alertTextForeground.x = scene2d.width / 2 - alertTextForeground.textWidth / 2;
|
||||
// alertTextForeground.y = scene2d.height - 102;
|
||||
// alertTextBackground.x = scene2d.width / 2 - alertTextBackground.textWidth / 2 + 1;
|
||||
|
|
@ -318,7 +318,7 @@ class PlayGui {
|
|||
public function setHelpText(text:String) {
|
||||
this.helpTextForeground.text.text = text;
|
||||
this.helpTextBackground.text.text = text;
|
||||
helpTextBackground.render(scene2d);
|
||||
// helpTextBackground.render(scene2d);
|
||||
// helpTextForeground.x = scene2d.width / 2 - helpTextForeground.textWidth / 2;
|
||||
// helpTextForeground.y = scene2d.height * 0.45;
|
||||
// helpTextBackground.x = scene2d.width / 2 - helpTextBackground.textWidth / 2 + 1;
|
||||
|
|
|
|||
|
|
@ -11,4 +11,9 @@ class Rect {
|
|||
this.position = position.clone();
|
||||
this.extent = extent.clone();
|
||||
}
|
||||
|
||||
public function inRect(point:Vector) {
|
||||
return (position.x <= point.x && (position.x + extent.x) >= point.x)
|
||||
&& (position.y <= point.y && (position.y + extent.y) >= point.y);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue