Lua Interface - support Strongly typing System.Drawing.Color, and strongly type the color parameters of gui.drawEllipse()

This commit is contained in:
adelikat 2014-06-02 00:43:25 +00:00
parent 83ada011e8
commit 58b5163715
4 changed files with 83 additions and 49 deletions

View File

@ -68,8 +68,13 @@ namespace BizHawk.Client.EmuHawk
private DisplaySurface _luaSurface; private DisplaySurface _luaSurface;
// TODO: obsolete this method and strongly type all colors
private static Color GetColor(object color) private static Color GetColor(object color)
{ {
if (color is Color)
{
return (Color)color;
}
if (color is double) if (color is double)
{ {
return Color.FromArgb(int.Parse(long.Parse(color.ToString()).ToString("X"), NumberStyles.HexNumber)); return Color.FromArgb(int.Parse(long.Parse(color.ToString()).ToString("X"), NumberStyles.HexNumber));
@ -233,14 +238,14 @@ namespace BizHawk.Client.EmuHawk
"drawEllipse", "drawEllipse",
"Draws an ellipse at the given coordinates and the given width and height. Line is the color of the ellipse. Background is the optional fill color" "Draws an ellipse at the given coordinates and the given width and height. Line is the color of the ellipse. Background is the optional fill color"
)] )]
public void DrawEllipse(int x, int y, int width, int height, object line, object background = null) public void DrawEllipse(int x, int y, int width, int height, Color? line, Color? background = null)
{ {
GlobalWin.DisplayManager.NeedsToPaint = true; GlobalWin.DisplayManager.NeedsToPaint = true;
using (var g = GetGraphics()) using (var g = GetGraphics())
{ {
try try
{ {
g.DrawEllipse(GetPen(line ?? "white"), x, y, width, height); g.DrawEllipse(GetPen(line ?? Color.White), x, y, width, height);
if (background != null) if (background != null)
{ {
var brush = GetBrush(background); var brush = GetBrush(background);

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Reflection; using System.Reflection;
using Lua511; using Lua511;
@ -40,8 +41,8 @@ namespace LuaInterface
extractValues.Add(typeof(LuaFunction).TypeHandle.Value.ToInt64(), new ExtractValue(getAsFunction)); extractValues.Add(typeof(LuaFunction).TypeHandle.Value.ToInt64(), new ExtractValue(getAsFunction));
extractValues.Add(typeof(LuaTable).TypeHandle.Value.ToInt64(), new ExtractValue(getAsTable)); extractValues.Add(typeof(LuaTable).TypeHandle.Value.ToInt64(), new ExtractValue(getAsTable));
extractValues.Add(typeof(LuaUserData).TypeHandle.Value.ToInt64(), new ExtractValue(getAsUserdata)); extractValues.Add(typeof(LuaUserData).TypeHandle.Value.ToInt64(), new ExtractValue(getAsUserdata));
extractValues.Add(typeof(Color).TypeHandle.Value.ToInt64(), new ExtractValue(getAsColor));
extractNetObject = new ExtractValue(getAsNetObject); extractNetObject = new ExtractValue(getAsNetObject);
} }
/* /*
@ -108,59 +109,63 @@ namespace LuaInterface
if (LuaDLL.lua_isboolean(luaState, stackPos)) if (LuaDLL.lua_isboolean(luaState, stackPos))
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
} }
else if (paramType == typeof(string) || paramType == typeof (char [])) else if (paramType == typeof(Color))
{ {
if (LuaDLL.lua_isstring(luaState, stackPos)) return extractValues[runtimeHandleValue];
}
else if (paramType == typeof(string) || paramType == typeof(char[]))
{
if (LuaDLL.lua_isstring(luaState, stackPos))
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
else if (luatype == LuaTypes.LUA_TNIL) else if (luatype == LuaTypes.LUA_TNIL)
return extractNetObject; // kevinh - silently convert nil to a null string pointer return extractNetObject; // kevinh - silently convert nil to a null string pointer
} }
else if (paramType == typeof(LuaTable)) else if (paramType == typeof(LuaTable))
{ {
if (luatype == LuaTypes.LUA_TTABLE) if (luatype == LuaTypes.LUA_TTABLE)
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
} }
else if (paramType == typeof(LuaUserData)) else if (paramType == typeof(LuaUserData))
{ {
if (luatype == LuaTypes.LUA_TUSERDATA) if (luatype == LuaTypes.LUA_TUSERDATA)
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
} }
else if (paramType == typeof(LuaFunction)) else if (paramType == typeof(LuaFunction))
{ {
if (luatype == LuaTypes.LUA_TFUNCTION) if (luatype == LuaTypes.LUA_TFUNCTION)
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
} }
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION) else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
{ {
return new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated); return new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated);
} }
else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE) else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
{ {
return new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated); return new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated);
} }
else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL) else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
{ {
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
return extractNetObject; return extractNetObject;
} }
else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
{ {
if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index")) if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
{ {
object obj = translator.getNetObject(luaState, -1); object obj = translator.getNetObject(luaState, -1);
LuaDLL.lua_settop(luaState, -2); LuaDLL.lua_settop(luaState, -2);
if (obj != null && paramType.IsAssignableFrom(obj.GetType())) if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject; return extractNetObject;
} }
else else
return null; return null;
} }
else else
{ {
object obj = translator.getNetObject(luaState, stackPos); object obj = translator.getNetObject(luaState, stackPos);
if (obj != null && paramType.IsAssignableFrom(obj.GetType())) if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject; return extractNetObject;
} }
return null; return null;
} }
@ -252,6 +257,29 @@ namespace LuaInterface
if(retVal=="" && !LuaDLL.lua_isstring(luaState,stackPos)) return null; if(retVal=="" && !LuaDLL.lua_isstring(luaState,stackPos)) return null;
return retVal; return retVal;
} }
private object getAsColor(IntPtr luaState, int stackPos)
{
try
{
if (LuaDLL.lua_isnumber(luaState, stackPos))
{
Color retVal = Color.FromArgb((int)LuaDLL.lua_tonumber(luaState, stackPos));
return retVal;
}
else if (LuaDLL.lua_isstring(luaState, stackPos))
{
Color retVal = Color.FromName(LuaDLL.lua_tostring(luaState, stackPos));
return retVal;
}
return null;
}
catch (Exception)
{
return null;
}
}
private object getAsTable(IntPtr luaState,int stackPos) private object getAsTable(IntPtr luaState,int stackPos)
{ {
return translator.getTable(luaState,stackPos); return translator.getTable(luaState,stackPos);

View File

@ -72,6 +72,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

Binary file not shown.