Lua tooltips

This commit is contained in:
2012-10-17 11:27:31 +02:00
parent bcbddf1b2c
commit 00159095ed
7 changed files with 90 additions and 15 deletions
+12 -6
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace DepthsBelow.GUI
@@ -11,25 +12,30 @@ namespace DepthsBelow.GUI
{
public String Value;
public SpriteFont Font;
public Color Color;
public Text(SpriteFont font)
public Text()
: base()
{
this.Font = font;
this.Color = Color.Black;
this.Color = Color.White;
}
public Text(Frame parent, SpriteFont font) : this(font)
public Text(Frame parent)
: this()
{
this.Parent = parent;
}
public void SetFont(string spriteFontName)
{
Font = GameServices.GetService<ContentManager>().Load<SpriteFont>(spriteFontName);
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
spriteBatch.DrawString(Font, Value, new Vector2(Rectangle.X + Parent.Rectangle.X, Rectangle.Y + Parent.Rectangle.Y), Color);
if (Parent.Visible && this.Visible && Font != null)
spriteBatch.DrawString(Font, Value, new Vector2(Rectangle.X + Parent.Rectangle.X, Rectangle.Y + Parent.Rectangle.Y), Color);
}
}
}
+4 -3
View File
@@ -17,7 +17,7 @@ namespace DepthsBelow
this.core = core;
// Test GUI frames
var frame = new GUI.Frame()
/*var frame = new GUI.Frame()
{
Y = 100
};
@@ -33,8 +33,9 @@ namespace DepthsBelow
}
};
var text = new GUI.Text(frame, core.Content.Load<SpriteFont>("Arial"));
text.Value = "Hello world!";
var text = new GUI.Text(frame);
text.SetFont("Arial");
text.Value = "Hello world!";*/
}
}
}
+30
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using DepthsBelow.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
@@ -28,6 +29,24 @@ namespace DepthsBelow
{
return new GUI.Button(parent);
}
public static GUI.Text CreateText()
{
return new Text();
}
public static GUI.Text CreateText(GUI.Frame parent)
{
return new GUI.Text(parent);
}
}
static class LuaMouse
{
public static Point GetPos()
{
var ms = Mouse.GetState();
return new Point(ms.X, ms.Y);
}
}
public class Lua
@@ -58,11 +77,17 @@ namespace DepthsBelow
lua.RegisterFunction("Console.Write", null, typeof(System.Console).GetMethod("Write", new Type[] { typeof(string) }));
lua.RegisterFunction("Console.WriteLine", null, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
lua.NewTable("Mouse");
lua.RegisterFunction("Mouse.GetPos", null, typeof(LuaMouse).GetMethod("GetPos"));
// Register GUI creation functions
lua.RegisterFunction("_CreateFrame", null, typeof(LuaGUI).GetMethod("CreateFrame", new Type[] { }));
lua.RegisterFunction("_CreateFrameAsChild", null, typeof(LuaGUI).GetMethod("CreateFrame", new Type[] { typeof(GUI.Frame) }));
lua.RegisterFunction("_CreateButton", null, typeof(LuaGUI).GetMethod("CreateButton", new Type[] { }));
lua.RegisterFunction("_CreateButtonAsChild", null, typeof(LuaGUI).GetMethod("CreateButton", new Type[] { typeof(GUI.Frame) }));
lua.RegisterFunction("_CreateText", null, typeof(LuaGUI).GetMethod("CreateText", new Type[] { }));
lua.RegisterFunction("_CreateTextAsChild", null, typeof(LuaGUI).GetMethod("CreateText", new Type[] { typeof(GUI.Frame) }));
lua.NewTable("GUI");
/*
* Because Lua can't dynamically call overloads of registered functions, a generic Lua function needs to do the decision making.
@@ -107,5 +132,10 @@ namespace DepthsBelow
}
}
public T GetObject<T>(string obj)
{
return (T)lua[obj];
}
}
}
+28 -2
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
@@ -24,7 +25,6 @@ namespace DepthsBelow
bool checkingDirection = false;
bool readjust = false;
public MouseInput(Core core)
{
@@ -34,7 +34,6 @@ namespace DepthsBelow
gridRectangle = Rectangle.Empty;
}
public void LoadContent()
{
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
@@ -48,7 +47,34 @@ namespace DepthsBelow
{
MouseState ms = Mouse.GetState();
Vector2 mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y));
Rectangle mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);
KeyboardState ks = Keyboard.GetState();
// Tooltip stuff
var tooltip = core.Lua.GetObject<GUI.Frame>("ToolTip");
if (tooltip != null)
{
tooltip.Visible = false;
foreach(var entity in core.Squad)
{
if (mouseWorldRectangle.Intersects(entity.GetComponent<Component.Collision>().Rectangle))
{
tooltip.Visible = true;
break;
}
}
if (tooltip.Visible)
{
tooltip.X = ms.X + 10;
tooltip.Y = ms.Y + 15;
}
}
else
{
throw new Exception("Tooltip is null D:");
}
if (core.PlayerTurn == true)
{
// Selection rectangle
@@ -145,6 +145,12 @@
<Importer>LuaImporter</Importer>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="scripts\tooltip.lua">
<Name>tooltip</Name>
<Importer>LuaImporter</Importer>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
@@ -1,10 +1,11 @@
local frame = CreateFrame('Frame');
frame.X = 100;
frame.X = 100;
local button = CreateFrame('Button', frame);
button:SetTexture('images/Enter');
button.Width = 100;
button.Height = 50;
button.OnClick = function(clickPos)
Console.WriteLine(clickPos.X .. ' ' .. clickPos.Y);
end
button.OnClick = function(clickPos)
Console.WriteLine(clickPos.X .. ' ' .. clickPos.Y)
end
+5
View File
@@ -0,0 +1,5 @@
ToolTip = CreateFrame("Frame")
local text = CreateFrame("Text", ToolTip)
text:SetFont("Arial")
text.Value = "99%"