Frame field "Layer" controls render order and prevents event bubbling.

Reworked GUI events.
This commit is contained in:
Simon Holmberg
2012-10-19 23:27:51 +02:00
parent 0c923357b4
commit 9d92736932
15 changed files with 533 additions and 16 deletions
+5 -2
View File
@@ -27,7 +27,8 @@ namespace DepthsBelow
public EntityManager EntityManager;
public TurnManager TurnManager;
public Camera Camera;
public Camera Camera;
public Interface Interface;
public static MouseInput MouseInput;
public static KeyboardInput KeyboardInput;
@@ -71,7 +72,9 @@ namespace DepthsBelow
EntityManager = new EntityManager();
TurnManager = new TurnManager(new string[] { "Player", "Computer" });
Camera = new Camera(this);
Camera = new Camera(this);
Interface = new Interface(this);
Squad = new List<Soldier>();
Swarm = new List<SmallEnemy>();
Volley = new List<Shot>();
+4 -4
View File
@@ -14,12 +14,12 @@ namespace DepthsBelow.GUI
/// </summary>
public class Button : Frame
{
public delegate void OnClickHandler(Point pos);
//public delegate void OnClickHandler(Point pos);
/// <summary>
/// Handler of OnClick events.
/// </summary>
public OnClickHandler OnClick;
//public OnClickHandler OnClick;
private MouseState lastMouseState;
@@ -39,7 +39,7 @@ namespace DepthsBelow.GUI
{
base.Update(gameTime);
MouseState ms = Mouse.GetState();
/*MouseState ms = Mouse.GetState();
if (ms.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
{
@@ -58,7 +58,7 @@ namespace DepthsBelow.GUI
}
}
lastMouseState = ms;
lastMouseState = ms;*/
}
}
}
+112 -2
View File
@@ -5,6 +5,7 @@ using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace DepthsBelow.GUI
{
@@ -46,6 +47,34 @@ namespace DepthsBelow.GUI
get { return this.Rectangle.Y; }
set { this.Rectangle.Y = value; }
}
/// <summary>
/// The x-coordinate of the left side of the frame.
/// </summary>
public int Left
{
get { return this.Rectangle.Left; }
}
/// <summary>
/// The y-coordinate of the top side of the frame.
/// </summary>
public int Top
{
get { return this.Rectangle.Top; }
}
/// <summary>
/// The x-coordinate of the right side of the frame.
/// </summary>
public int Right
{
get { return this.Rectangle.Right; }
}
/// <summary>
/// The y-coordinate of the bottom side of the frame.
/// </summary>
public int Bottom
{
get { return this.Rectangle.Bottom; }
}
/// <summary>
/// Rectangle of the frame.
@@ -77,6 +106,23 @@ namespace DepthsBelow.GUI
/// </summary>
public bool Visible = true;
/// <summary>
/// The layer of the frame.
/// Bigger number means closer to the screen.
/// </summary>
public int Layer = 0;
/// <summary>
/// Gets the sum of this frame and all parent layers.
/// </summary>
public int LayerSum
{
get { return (Parent != null) ? this.Layer + Parent.LayerSum : this.Layer; }
}
/// <summary>
/// The unique identifier of the frame.
/// </summary>
public int UID { get; private set; }
/// <summary>
/// Potential parent frame.
/// </summary>
@@ -84,7 +130,46 @@ namespace DepthsBelow.GUI
/// <summary>
/// Potential children frames.
/// </summary>
public List<Frame> Children;
public List<Frame> Children;
#region Events
/// <summary>
/// Occurs when the frame is clicked inside it's bounding rectangle.
/// </summary>
public event OnClickHandler OnClick;
/// <summary>
/// <see cref="OnClickHandler" /> event arguments.
/// </summary>
public class OnClickArgs : EventArgs
{
/// <summary>
/// The position of the click relative to the frame position.
/// </summary>
public Point Position;
/// <summary>
/// The time of the click, since the start of the program.
/// </summary>
public TimeSpan Time;
}
/// <summary>
/// Handler for click events.
/// </summary>
/// <param name="frame">The clicked frame.</param>
/// <param name="e">The <see cref="OnClickArgs" /> instance containing the event data.</param>
public delegate void OnClickHandler(Frame frame, OnClickArgs e);
/// <summary>
/// Raise an <see cref="OnClick" /> event on the frame.
/// </summary>
/// <param name="e">The <see cref="OnClickArgs" /> instance containing the event data.</param>
public void Click(OnClickArgs e)
{
if (OnClick != null)
OnClick(this, e);
}
#endregion
private MouseState lastMouseState;
/// <summary>
/// Create a blank frame.
@@ -97,7 +182,7 @@ namespace DepthsBelow.GUI
this.Children = new List<Frame>();
GUIManager.Add(this);
UID = GUIManager.Add(this);
}
/// <summary>
@@ -153,6 +238,31 @@ namespace DepthsBelow.GUI
public virtual void Update(GameTime gameTime)
{
/*MouseState ms = Mouse.GetState();
if (ms.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
{
// HACK: This should be handled globally somewhere else to prevent event bubbling
if (
ms.X >= AbsoluteRectangle.Left
&& ms.X < AbsoluteRectangle.Right
&& ms.Y >= AbsoluteRectangle.Top
&& ms.Y < AbsoluteRectangle.Bottom)
{
if (OnClick != null)
{
var args = new OnClickArgs()
{
Position = new Point(ms.X - AbsoluteRectangle.X, ms.Y - AbsoluteRectangle.Y),
Time = gameTime.TotalGameTime
};
OnClick(this, args);
}
}
}
lastMouseState = ms;*/
foreach (var child in Children)
child.Update(gameTime);
}
+77 -3
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -10,11 +11,13 @@ namespace DepthsBelow
{
static class GUIManager
{
public static List<Frame> Frames = new List<Frame>();
public static List<Frame> Frames = new List<Frame>();
private static int uidIndex = 0;
public static void Add(Frame frame)
public static int Add(Frame frame)
{
Frames.Add(frame);
return uidIndex++;
}
public static void Remove(Frame frame)
@@ -22,6 +25,47 @@ namespace DepthsBelow
Frames.Remove(frame);
}
public static void Click(Point position, TimeSpan time)
{
var clickRectangle = new Rectangle(position.X, position.Y, 1, 1);
var intersections = new List<Frame>();
foreach (var frame in Frames)
{
if (clickRectangle.Intersects(frame.AbsoluteRectangle))
intersections.Add(frame);
}
var topFrame = GetTopFrame(intersections);
if (topFrame != null)
{
var e = new Frame.OnClickArgs()
{
Position = position,
Time = time
};
topFrame.Click(e);
}
}
/// <summary>
/// Gets the top frame in the frame stack.
/// </summary>
/// <param name="frames">A list of frames to check.</param>
/// <returns>Returns the topmost frame.</returns>
private static Frame GetTopFrame(List<Frame> frames)
{
if (frames.Count == 0)
return null;
if (frames.Count == 1)
return frames.First();
return frames.OrderByDescending(f => f, new FrameSort()).First();
}
public static void Update(GameTime gameTime)
{
foreach (var frame in Frames)
@@ -30,8 +74,38 @@ namespace DepthsBelow
public static void Draw(SpriteBatch spriteBatch)
{
foreach (var frame in Frames)
foreach (var frame in Frames.OrderBy(f => f, new FrameSort()))
frame.Draw(spriteBatch);
}
/// <summary>
/// Sort frames based on layer, using UID as tie breaker.
/// </summary>
private class FrameSort : IComparer<Frame>
{
public int Compare(Frame a, Frame b)
{
var aLayerSum = a.LayerSum;
var bLayerSum = b.LayerSum;
// Compare the layer sum
if (aLayerSum > bLayerSum)
return 1;
if (aLayerSum < bLayerSum)
return -1;
// If the result is indecisive, pick the frame with the biggest UID
if (aLayerSum == bLayerSum)
{
if (a.UID > b.UID)
return 1;
if (a.UID < b.UID)
return -1;
}
return 0;
}
}
}
}
+122 -1
View File
@@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DepthsBelow.GUI;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
namespace DepthsBelow
{
class Interface
public class Interface
{
Core core;
@@ -36,6 +37,126 @@ namespace DepthsBelow
var text = new GUI.Text(frame);
text.SetFont("Arial");
text.Value = "Hello world!";*/
// Create some unit frames
/*var panicFrame = CreatePanicFrame();
var unit1 = CreateUnitFrame("Flight captain Rainbow");
unit1.X = panicFrame.X;
unit1.Y = panicFrame.Y + panicFrame.Height;
unit1.OnClick += delegate(Frame frame, Frame.OnClickArgs args)
{
frame.Color = Color.Red;
};
var unit2 = CreateUnitFrame("Mr. Sparkle");
unit2.X = unit1.X;
unit2.Y = unit1.Y + unit1.Height;
unit2.OnClick += delegate(Frame frame, Frame.OnClickArgs args)
{
frame.Color = Color.Red;
};*/
var frame1 = new GUI.Frame();
frame1.Layer = 0;
frame1.SetTexture("images/Enter");
frame1.Color = Color.Red;
frame1.Width = 100;
frame1.Height = 100;
frame1.OnClick += delegate(Frame frame, Frame.OnClickArgs args)
{
if (frame.Color == Color.Red)
frame.Color = Color.Green;
else
frame.Color = Color.Red;
};
var frame1_child = new GUI.Frame(frame1);
frame1_child.SetTexture("images/Enter");
frame1_child.Color = Color.Purple;
frame1_child.Width = 60;
frame1_child.Height = 60;
frame1_child.X = 20;
frame1_child.Y = 20;
frame1_child.OnClick += delegate(Frame frame, Frame.OnClickArgs args)
{
if (frame.Color == Color.Purple)
frame.Color = Color.Pink;
else
frame.Color = Color.Purple;
};
var frame2 = new GUI.Frame();
frame2.Layer = 0;
frame2.SetTexture("images/Enter");
frame2.Color = Color.Blue;
frame2.Width = 100;
frame2.Height = 100;
frame2.X = 50;
frame2.Y = 50;
frame2.OnClick += delegate(Frame frame, Frame.OnClickArgs args)
{
if (frame.Color == Color.Blue)
frame.Color = Color.Yellow;
else
frame.Color = Color.Blue;
};
}
private GUI.Frame CreatePanicFrame()
{
var frame = new GUI.Frame();
frame.SetTexture("images/GUI/unit_background");
frame.Color = Color.Black;
frame.Width = 164;
frame.Height = 19;
var healthBg = new GUI.Frame(frame);
healthBg.SetTexture("images/GUI/health_background");
healthBg.Width = 136;
healthBg.Height = 13;
healthBg.X = 3;
healthBg.Y = 3;
var healthBar = new GUI.Frame(healthBg);
healthBar.SetTexture("images/GUI/bar_solid");
healthBar.Color = new Color(87, 55, 253);
healthBar.Width = (int)(healthBg.Width * 0.5);
healthBar.Height = 11;
healthBar.X = 1;
healthBar.Y = 1;
return frame;
}
private GUI.Frame CreateUnitFrame(string unitName)
{
var frame = new GUI.Frame();
frame.SetTexture("images/GUI/unit_background");
frame.Color = Color.Black;
frame.Width = 164;
frame.Height = 35;
var nameText = new GUI.Text(frame);
nameText.SetFont("fonts/UnitName");
nameText.Value = unitName;
nameText.X = 4;
nameText.Y = 1;
var healthBg = new GUI.Frame(frame);
healthBg.SetTexture("images/GUI/health_background");
healthBg.Width = 136;
healthBg.Height = 13;
healthBg.X = 3;
healthBg.Y = 19;
var healthBar = new GUI.Frame(healthBg);
healthBar.SetTexture("images/GUI/bar_solid");
healthBar.Color = Color.Red;
healthBar.Width = (int)(healthBg.Width * 0.5f);
healthBar.Height = 11;
healthBar.X = 1;
healthBar.Y = 1;
return frame;
}
}
}
+7
View File
@@ -26,6 +26,13 @@ namespace DepthsBelow
{
KeyboardState ks = Keyboard.GetState();
// Lua reload scripts
if (ks.IsKeyUp(Keys.R) && lastKeyboardState.IsKeyDown(Keys.R))
{
core.Lua.Reload();
}
// Next turn
if (ks.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Enter))
{
if (core.TurnManager.CurrentTurn == core.TurnManager["Player"])
+31 -4
View File
@@ -53,10 +53,12 @@ namespace DepthsBelow
{
private LuaInterface.Lua lua;
private List<string> filesToRun;
public Lua()
{
ResetContext();
ExposeLibraries();
filesToRun = new List<string>();
Initialize();
/*lua.DoString(@"
local frame = CreateFrame('Frame');
@@ -71,6 +73,15 @@ namespace DepthsBelow
");*/
}
private void Initialize()
{
if (lua == null)
{
lua = new LuaInterface.Lua();
ExposeLibraries();
}
}
public void ExposeLibraries()
{
lua.NewTable("Console");
@@ -120,7 +131,13 @@ namespace DepthsBelow
if (lua != null)
lua.Dispose();
lua = new LuaInterface.Lua();
Initialize();
}
public void Reload()
{
ResetContext();
LoadScripts();
}
public void LoadScripts()
@@ -128,7 +145,7 @@ namespace DepthsBelow
// Load scripts from script folder
// HACK: This needs to load files on the fly instead of loading precompiled files by the content manager.
// http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_2 maybe?
var scripts = GameServices.GetService<ContentManager>().LoadContent<string>("scripts");
/*var scripts = GameServices.GetService<ContentManager>().LoadContent<string>("scripts");
foreach (var script in scripts)
{
try
@@ -140,9 +157,19 @@ namespace DepthsBelow
Console.WriteLine("Lua error: " + e.Message);
}
}*/
foreach (var fileName in filesToRun)
{
lua.DoFile(GameServices.GetService<ContentManager>().RootDirectory + "/" + fileName);
}
}
public void AddFile(string fileName)
{
filesToRun.Add(fileName);
}
public T GetObject<T>(string obj)
{
return (T)lua[obj];
+13
View File
@@ -43,6 +43,11 @@ namespace DepthsBelow
gridTexture.SetData(new Color[] { Color.White });
}
public void OnClick(MouseState ms, GameTime gameTime)
{
GUIManager.Click(new Point(ms.X, ms.Y), gameTime.TotalGameTime);
}
public void Update(GameTime gameTime)
{
MouseState ms = Mouse.GetState();
@@ -50,6 +55,14 @@ namespace DepthsBelow
Rectangle mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);
KeyboardState ks = Keyboard.GetState();
// OnClick event
if (
(ms.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed)
|| (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
)
OnClick(ms, gameTime);
// Tooltip stuff
var tooltip = core.Lua.GetObject<GUI.Frame>("ToolTip");
if (tooltip != null)
@@ -139,16 +139,48 @@
<Compile Include="scripts\gui.lua">
<Name>gui</Name>
<Importer>LuaImporter</Importer>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Include="scripts\test.lua">
<Name>test</Name>
<Importer>LuaImporter</Importer>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="scripts\tooltip.lua">
<Name>tooltip</Name>
<Importer>LuaImporter</Importer>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="images\GUI\bar_solid.png">
<Name>bar_solid</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
<Compile Include="images\GUI\bar_transparent.png">
<Name>bar_transparent</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
<Compile Include="images\GUI\health_background.png">
<Name>health_background</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
<Compile Include="images\GUI\unit_background.png">
<Name>unit_background</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="fonts\UnitName.spritefont">
<Name>UnitName</Name>
<Importer>FontDescriptionImporter</Importer>
<Processor>FontDescriptionProcessor</Processor>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
+60
View File
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
<Asset Type="Graphics:FontDescription">
<!--
Modify this string to change the font that will be imported.
-->
<FontName>Arial</FontName>
<!--
Size is a float value, measured in points. Modify this value to change
the size of the font.
-->
<Size>10</Size>
<!--
Spacing is a float value, measured in pixels. Modify this value to change
the amount of spacing in between characters.
-->
<Spacing>0</Spacing>
<!--
UseKerning controls the layout of the font. If this value is true, kerning information
will be used when placing characters.
-->
<UseKerning>true</UseKerning>
<!--
Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
and "Bold, Italic", and are case sensitive.
-->
<Style>Regular</Style>
<!--
If you uncomment this line, the default character will be substituted if you draw
or measure text that contains characters which were not included in the font.
-->
<!-- <DefaultCharacter>*</DefaultCharacter> -->
<!--
CharacterRegions control what letters are available in the font. Every
character from Start to End will be built and made available for drawing. The
default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
character set. The characters are ordered according to the Unicode standard.
See the documentation for more information.
-->
<CharacterRegions>
<CharacterRegion>
<Start>&#32;</Start>
<End>&#126;</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
Binary file not shown.

After

Width:  |  Height:  |  Size: 977 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 983 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1008 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

@@ -1,3 +1,6 @@
luanet.load_assembly("Microsoft.Xna.Framework")
Color = luanet.import_type("Microsoft.Xna.Framework.Color")
local frame = CreateFrame('Frame');
frame.X = 100;
@@ -9,3 +12,70 @@ button.OnClick = function(clickPos)
Console.WriteLine(clickPos.X .. ' ' .. clickPos.Y)
end
function CreatePanicFrame(x, y)
local frame = CreateFrame("Frame")
frame:SetTexture("images/GUI/unit_background")
frame.Width = 154
frame.Height = 19
frame.X = x
frame.Y = y
local healthBg = CreateFrame("Frame", frame)
healthBg:SetTexture("images/GUI/health_background")
healthBg.Width = 136
healthBg.Height = 13
healthBg.X = 3
healthBg.Y = 3
local healthBar = CreateFrame("Frame", healthBg)
healthBar:SetTexture("images/GUI/bar_solid")
healthBar.Color = Color(87, 55, 253)
healthBar.Width = healthBg.Width * 0.5
healthBar.Height = 11
healthBar.X = 1
healthBar.Y = 1
return frame
end
function CreateUnitFrame(name, x, y)
-- Frames for one unit
local frame = CreateFrame("Frame")
frame:SetTexture("images/GUI/unit_background")
frame.Width = 164;
frame.Height = 35;
frame.X = x
frame.Y = y
local nameText = CreateFrame("Text", frame)
nameText:SetFont("fonts/UnitName")
nameText.Value = name
nameText.X = 4
nameText.Y = 1
local healthBg = CreateFrame("Frame", frame)
healthBg:SetTexture("images/GUI/health_background")
healthBg.Width = 136
healthBg.Height = 13
healthBg.X = 3
healthBg.Y = 19
local healthBar = CreateFrame("Frame", healthBg)
healthBar:SetTexture("images/GUI/bar_solid")
healthBar.Color = Color.Red
healthBar.Width = healthBg.Width * 0.5
healthBar.Height = 11
healthBar.X = 1
healthBar.Y = 1
return frame
end
--[[local frame;
frame = CreatePanicFrame(0, 0)
frame = CreateUnitFrame("Flight captain Rainbow Dash", frame.X, frame.Y + frame.Height)
CreateUnitFrame("Mr. Sparkle", frame.X, frame.Y + frame.Height)]]--
local testFrame = CreateTestFrame()
testFrame:SetWidth(1337)