move the gui library to its own function (all libraries are separate objects now!), add lua function: gui.clearGraphics()

This commit is contained in:
adelikat 2013-10-31 18:43:01 +00:00
parent 4bb60ec8c9
commit ae34ccce0a
4 changed files with 117 additions and 108 deletions

View File

@ -8,14 +8,95 @@ using BizHawk.Client.Common;
namespace BizHawk.MultiClient
{
public partial class EmuLuaLibrary
public class GuiLuaLibrary : LuaLibraryBase
{
#region Gui Library Helpers
public override string Name { get { return "gui"; } }
public override string[] Functions
{
get
{
return new[]
{
"addmessage",
"alert",
"clearGraphics",
"cleartext",
"drawBezier",
"drawBox",
"drawEllipse",
"drawIcon",
"drawImage",
"drawLine",
"drawPie",
"drawPixel",
"drawPolygon",
"drawRectangle",
"drawString",
"drawText",
"text",
};
}
}
#region Gui API
public void Dispose()
{
foreach (var brush in SolidBrushes.Values) brush.Dispose();
foreach (var brush in Pens.Values) brush.Dispose();
}
public bool SurfaceIsNull
{
get
{
return luaSurface == null;
}
}
/// <summary>
/// sets the current drawing context to a new surface.
/// you COULD pass these back to lua to use as a target in rendering jobs, instead of setting it as current here.
/// could be more powerful.
/// performance test may reveal that repeatedly calling GetGraphics could be slow.
/// we may want to make a class here in LuaImplementation that wraps a DisplaySurface and a Graphics which would be created once
/// </summary>
public void DrawNew()
{
luaSurface = GlobalWinF.DisplayManager.GetLuaSurfaceNative();
}
public void DrawNewEmu()
{
luaSurface = GlobalWinF.DisplayManager.GetLuaEmuSurfaceEmu();
}
/// <summary>
/// finishes the current drawing and submits it to the display manager (at native [host] resolution pre-osd)
/// you would probably want some way to specify which surface to set it to, when there are other surfaces.
/// most notably, the client output [emulated] resolution
/// </summary>
public void DrawFinish()
{
GlobalWinF.DisplayManager.SetLuaSurfaceNativePreOSD(luaSurface);
luaSurface = null;
}
public void DrawFinishEmu()
{
GlobalWinF.DisplayManager.SetLuaSurfaceEmu(luaSurface);
luaSurface = null;
}
#endregion
#region Helpers
private DisplaySurface luaSurface;
private readonly Dictionary<Color, SolidBrush> SolidBrushes = new Dictionary<Color, SolidBrush>();
private readonly Dictionary<Color, Pen> Pens = new Dictionary<Color, Pen>();
public Color GetColor(object color)
private static Color GetColor(object color)
{
if (color is double)
{
@ -27,7 +108,7 @@ namespace BizHawk.MultiClient
}
}
public SolidBrush GetBrush(object color)
private SolidBrush GetBrush(object color)
{
Color c = GetColor(color);
SolidBrush b;
@ -39,7 +120,7 @@ namespace BizHawk.MultiClient
return b;
}
public Pen GetPen(object color)
private Pen GetPen(object color)
{
Color c = GetColor(color);
Pen p;
@ -51,47 +132,7 @@ namespace BizHawk.MultiClient
return p;
}
public void gui_clearGraphics()
{
GlobalWinF.DisplayManager.NeedsToPaint = true;
luaSurface.Clear();
}
/// <summary>
/// sets the current drawing context to a new surface.
/// you COULD pass these back to lua to use as a target in rendering jobs, instead of setting it as current here.
/// could be more powerful.
/// performance test may reveal that repeatedly calling GetGraphics could be slow.
/// we may want to make a class here in LuaImplementation that wraps a DisplaySurface and a Graphics which would be created once
/// </summary>
public void gui_drawNew()
{
luaSurface = GlobalWinF.DisplayManager.GetLuaSurfaceNative();
}
public void gui_drawNewEmu()
{
luaSurface = GlobalWinF.DisplayManager.GetLuaEmuSurfaceEmu();
}
/// <summary>
/// finishes the current drawing and submits it to the display manager (at native [host] resolution pre-osd)
/// you would probably want some way to specify which surface to set it to, when there are other surfaces.
/// most notably, the client output [emulated] resolution
/// </summary>
public void gui_drawFinish()
{
GlobalWinF.DisplayManager.SetLuaSurfaceNativePreOSD(luaSurface);
luaSurface = null;
}
public void gui_drawFinishEmu()
{
GlobalWinF.DisplayManager.SetLuaSurfaceEmu(luaSurface);
luaSurface = null;
}
Graphics GetGraphics()
private Graphics GetGraphics()
{
var g = luaSurface.GetGraphics();
int tx = Global.Emulator.CoreComm.ScreenLogicalOffsetX;
@ -105,9 +146,7 @@ namespace BizHawk.MultiClient
return g;
}
public DisplaySurface luaSurface;
private void do_gui_text(object luaX, object luaY, object luaStr, bool alert, object background = null,
private void DoGuiText(object luaX, object luaY, object luaStr, bool alert, object background = null,
object forecolor = null, object anchor = null)
{
if (!alert)
@ -160,10 +199,16 @@ namespace BizHawk.MultiClient
public void gui_alert(object luaX, object luaY, object luaStr, object anchor = null)
{
do_gui_text(luaX, luaY, luaStr, true, null, null, anchor);
DoGuiText(luaX, luaY, luaStr, true, null, null, anchor);
}
public void gui_cleartext()
public void gui_clearGraphics()
{
GlobalWinF.DisplayManager.NeedsToPaint = true;
luaSurface.Clear();
}
public static void gui_cleartext()
{
GlobalWinF.OSD.ClearGUIText();
}
@ -421,10 +466,8 @@ namespace BizHawk.MultiClient
}
}
public void gui_drawString(object X, object Y, object message, object color = null, object fontsize = null, object fontfamily = null, object fontstyle = null)
{
GlobalWinF.DisplayManager.NeedsToPaint = true;
gui_drawText(X, Y, message, color, fontsize, fontfamily, fontstyle);
}
@ -484,7 +527,7 @@ namespace BizHawk.MultiClient
public void gui_text(object luaX, object luaY, object luaStr, object background = null, object forecolor = null,
object anchor = null)
{
do_gui_text(luaX, luaY, luaStr, false, background, forecolor, anchor);
DoGuiText(luaX, luaY, luaStr, false, background, forecolor, anchor);
}
}
}

View File

@ -13,12 +13,21 @@ namespace BizHawk.MultiClient
private Lua currThread;
private FormsLuaLibrary _formsLibrary = new FormsLuaLibrary();
private EventLuaLibrary _eventLibrary = new EventLuaLibrary();
private GuiLuaLibrary _guiLibrary = new GuiLuaLibrary();
public LuaDocumentation Docs = new LuaDocumentation();
public bool IsRunning;
public EventWaitHandle LuaWait;
public bool FrameAdvanceRequested;
public GuiLuaLibrary GuiLibrary
{
get
{
return _guiLibrary;
}
}
public void WindowClosed(IntPtr handle)
{
_formsLibrary.WindowClosed(handle);
@ -60,32 +69,9 @@ namespace BizHawk.MultiClient
public void Close()
{
_lua = new Lua();
foreach (var brush in SolidBrushes.Values) brush.Dispose();
foreach (var brush in Pens.Values) brush.Dispose();
_guiLibrary.Dispose();
}
#region Register Library Functions
public static string[] GuiFunctions = new[]
{
"addmessage",
"alert",
"cleartext",
"drawBezier",
"drawBox",
"drawEllipse",
"drawIcon",
"drawImage",
"drawLine",
"drawPie",
"drawPixel",
"drawPolygon",
"drawRectangle",
"drawString",
"drawText",
"text",
};
public void LuaRegister(Lua lua)
{
lua.RegisterFunction("print", this, GetType().GetMethod("print"));
@ -101,6 +87,7 @@ namespace BizHawk.MultiClient
_eventLibrary.LuaRegister(lua, Docs);
_formsLibrary.LuaRegister(lua, Docs);
_guiLibrary.LuaRegister(lua, Docs);
new InputLuaLibrary(_lua).LuaRegister(lua, Docs);
new JoypadLuaLibrary(_lua).LuaRegister(lua, Docs);
new MemoryLuaLibrary().LuaRegister(lua, Docs);
@ -110,20 +97,9 @@ namespace BizHawk.MultiClient
new SavestateLuaLibrary().LuaRegister(lua, Docs);
new SNESLuaLibrary().LuaRegister(lua, Docs);
lua.NewTable("gui");
foreach (string t in GuiFunctions)
{
lua.RegisterFunction("gui." + t, this, GetType().GetMethod("gui_" + t));
Docs.Add("gui", t, GetType().GetMethod("gui_" + t));
}
Docs.Sort();
}
#endregion
#region Library Helpers
public Lua SpawnCoroutine(string File)
{
var t = _lua.NewThread();
@ -175,7 +151,5 @@ namespace BizHawk.MultiClient
GlobalWinF.DisplayManager.NeedsToPaint = true;
currThread.Yield(0);
}
#endregion
}
}

View File

@ -16,12 +16,7 @@ namespace BizHawk.MultiClient
{
string func = Name + "." + methodName;
var method = GetType().GetMethod(Name + "_" + methodName);
lua.RegisterFunction(
Name + "." + methodName,
this,
GetType().GetMethod(Name + "_" + methodName)
);
lua.RegisterFunction(func, this, method);
if (docs != null)
{

View File

@ -800,9 +800,9 @@ namespace BizHawk.MultiClient
{
if (luaList != null && luaList.Count > 0)
{
if (LuaImp.luaSurface == null)
if (LuaImp.GuiLibrary.SurfaceIsNull)
{
LuaImp.gui_drawNewEmu();
LuaImp.GuiLibrary.DrawNewEmu();
}
foreach (var lf in luaList)
{
@ -853,24 +853,21 @@ namespace BizHawk.MultiClient
{
if (luaList != null && luaList.Count > 0)
{
if (LuaImp.luaSurface == null)
LuaImp.gui_drawNewEmu();
if (LuaImp.GuiLibrary.SurfaceIsNull)
{
LuaImp.GuiLibrary.DrawNewEmu();
}
}
}
public void EndLuaDrawing()
{
if (luaList != null && luaList.Count > 0)
if (luaList != null && luaList.Any())
{
LuaImp.gui_drawFinishEmu();
LuaImp.GuiLibrary.DrawFinishEmu();
}
}
public bool IsRunning()
{
return true;
}
public bool WaitOne(int timeout)
{
if (!IsHandleCreated || IsDisposed)