2013-10-28 19:13:01 +00:00
using System ;
using System.Collections.Generic ;
using System.Drawing ;
using System.Globalization ;
2013-10-29 16:03:06 +00:00
2013-10-28 19:13:01 +00:00
using BizHawk.Client.Common ;
2014-01-26 16:15:45 +00:00
using LuaInterface ;
2013-10-28 19:13:01 +00:00
2013-11-03 03:54:37 +00:00
namespace BizHawk.Client.EmuHawk
2013-10-28 19:13:01 +00:00
{
2013-10-31 18:43:01 +00:00
public class GuiLuaLibrary : LuaLibraryBase
2013-10-28 19:13:01 +00:00
{
2013-10-31 18:43:01 +00:00
public override string Name { get { return "gui" ; } }
2013-10-28 19:13:01 +00:00
2013-10-31 18:43:01 +00:00
#region Gui API
public void Dispose ( )
2013-10-28 19:13:01 +00:00
{
2014-01-26 16:15:45 +00:00
foreach ( var brush in _solidBrushes . Values )
{
brush . Dispose ( ) ;
}
foreach ( var brush in _pens . Values )
{
brush . Dispose ( ) ;
}
2013-10-28 19:13:01 +00:00
}
2013-10-31 18:43:01 +00:00
public bool SurfaceIsNull
2013-10-28 19:13:01 +00:00
{
2013-10-31 18:43:01 +00:00
get
2013-10-28 19:13:01 +00:00
{
2014-01-26 16:15:45 +00:00
return _luaSurface = = null ;
2013-10-28 19:13:01 +00:00
}
}
2014-02-14 00:55:18 +00:00
[ LuaMethodAttributes (
"DrawNew" ,
"Changes drawing target to the specified lua surface name. This may clobber any previous drawing to this surface."
) ]
public void DrawNew ( string name )
2013-10-28 19:13:01 +00:00
{
2014-02-14 00:55:18 +00:00
DrawFinish ( ) ;
_luaSurface = GlobalWin . DisplayManager . LockLuaSurface ( name ) ;
2013-10-28 19:13:01 +00:00
}
2013-10-31 18:43:01 +00:00
public void DrawFinish ( )
2013-10-28 19:13:01 +00:00
{
2014-02-14 00:55:18 +00:00
if ( _luaSurface ! = null )
GlobalWin . DisplayManager . UnlockLuaSurface ( _luaSurface ) ;
2014-01-26 16:15:45 +00:00
_luaSurface = null ;
2013-10-28 19:13:01 +00:00
}
2013-10-31 18:43:01 +00:00
#endregion
#region Helpers
2014-01-26 16:15:45 +00:00
private readonly Dictionary < Color , SolidBrush > _solidBrushes = new Dictionary < Color , SolidBrush > ( ) ;
private readonly Dictionary < Color , Pen > _pens = new Dictionary < Color , Pen > ( ) ;
private readonly Bitmap _nullGraphicsBitmap = new Bitmap ( 1 , 1 ) ;
private DisplaySurface _luaSurface ;
2013-10-31 18:43:01 +00:00
private static Color GetColor ( object color )
{
if ( color is double )
{
return Color . FromArgb ( int . Parse ( long . Parse ( color . ToString ( ) ) . ToString ( "X" ) , NumberStyles . HexNumber ) ) ;
}
else
{
return Color . FromName ( color . ToString ( ) . ToLower ( ) ) ;
}
}
private SolidBrush GetBrush ( object color )
{
2014-01-26 16:15:45 +00:00
var c = GetColor ( color ) ;
2013-10-31 18:43:01 +00:00
SolidBrush b ;
2014-01-26 16:15:45 +00:00
if ( ! _solidBrushes . TryGetValue ( c , out b ) )
2013-10-31 18:43:01 +00:00
{
b = new SolidBrush ( c ) ;
2014-01-26 16:15:45 +00:00
_solidBrushes [ c ] = b ;
2013-10-31 18:43:01 +00:00
}
2014-01-26 16:15:45 +00:00
2013-10-31 18:43:01 +00:00
return b ;
}
private Pen GetPen ( object color )
{
2014-01-26 16:15:45 +00:00
var c = GetColor ( color ) ;
2013-10-31 18:43:01 +00:00
Pen p ;
2014-01-26 16:15:45 +00:00
if ( ! _pens . TryGetValue ( c , out p ) )
2013-10-31 18:43:01 +00:00
{
p = new Pen ( c ) ;
2014-01-26 16:15:45 +00:00
_pens [ c ] = p ;
2013-10-31 18:43:01 +00:00
}
2014-01-26 16:15:45 +00:00
2013-10-31 18:43:01 +00:00
return p ;
}
private Graphics GetGraphics ( )
2013-10-28 19:13:01 +00:00
{
2014-01-26 16:15:45 +00:00
var g = _luaSurface = = null ? Graphics . FromImage ( _nullGraphicsBitmap ) : _luaSurface . GetGraphics ( ) ;
var tx = Global . Emulator . CoreComm . ScreenLogicalOffsetX ;
var ty = Global . Emulator . CoreComm . ScreenLogicalOffsetY ;
2013-10-28 19:13:01 +00:00
if ( tx ! = 0 | | ty ! = 0 )
{
var transform = g . Transform ;
transform . Translate ( - tx , - ty ) ;
g . Transform = transform ;
}
2014-01-26 16:15:45 +00:00
2013-10-28 19:13:01 +00:00
return g ;
}
#endregion
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"addmessage" ,
2014-01-27 03:16:05 +00:00
"Adds a message to the OSD's message area"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 20:19:08 +00:00
public void AddMessage ( string message )
2013-10-28 19:13:01 +00:00
{
2014-01-27 20:19:08 +00:00
GlobalWin . OSD . AddMessage ( message ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"clearGraphics" ,
2014-01-27 03:16:05 +00:00
"clears all lua drawn graphics from the screen"
2014-01-26 16:15:45 +00:00
) ]
public void ClearGraphics ( )
2013-10-31 18:43:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2014-01-26 16:15:45 +00:00
_luaSurface . Clear ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"cleartext" ,
"TODO"
) ]
public static void ClearText ( )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . OSD . ClearGUIText ( ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawBezier" ,
2014-01-27 03:16:05 +00:00
"Draws a Bezier curve using the table of coordinates provided in the given color"
2014-01-26 16:15:45 +00:00
) ]
public void DrawBezier ( LuaTable points , object color )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
try
{
2014-01-26 16:15:45 +00:00
var pointsArr = new Point [ 4 ] ;
var i = 0 ;
2013-10-28 19:13:01 +00:00
foreach ( LuaTable point in points . Values )
{
2014-01-26 16:15:45 +00:00
pointsArr [ i ] = new Point ( LuaInt ( point [ 1 ] ) , LuaInt ( point [ 2 ] ) ) ;
2013-10-28 19:13:01 +00:00
i + + ;
if ( i > = 4 )
2014-01-26 16:15:45 +00:00
{
2013-10-28 19:13:01 +00:00
break ;
2014-01-26 16:15:45 +00:00
}
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
g . DrawBezier ( GetPen ( color ) , pointsArr [ 0 ] , pointsArr [ 1 ] , pointsArr [ 2 ] , pointsArr [ 3 ] ) ;
2013-10-28 19:13:01 +00:00
}
catch ( Exception )
{
return ;
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawBox" ,
2014-01-27 03:16:05 +00:00
"Draws a rectangle on screen from x1/y1 to x2/y2. Same as drawRectangle except it receives two points intead of a point and width/height"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public void DrawBox ( int x , int y , int x2 , int y2 , object line = null , object background = null )
2013-10-28 19:13:01 +00:00
{
using ( var g = GetGraphics ( ) )
{
try
{
2014-01-27 03:16:05 +00:00
if ( x < x2 )
2013-10-28 19:13:01 +00:00
{
2014-01-27 03:16:05 +00:00
x2 = Math . Abs ( x - x2 ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2014-01-27 03:16:05 +00:00
x2 = x - x2 ;
x - = x2 ;
2013-10-28 19:13:01 +00:00
}
2014-01-27 03:16:05 +00:00
if ( y < y2 )
2013-10-28 19:13:01 +00:00
{
2014-01-27 03:16:05 +00:00
y2 = Math . Abs ( y - y2 ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2014-01-27 03:16:05 +00:00
y2 = y - y2 ;
y - = y2 ;
2013-10-28 19:13:01 +00:00
}
2014-01-27 03:16:05 +00:00
g . DrawRectangle ( GetPen ( line ? ? "white" ) , x , y , x2 , y2 ) ;
2013-10-28 19:13:01 +00:00
if ( background ! = null )
{
2014-01-27 03:16:05 +00:00
g . FillRectangle ( GetBrush ( background ) , x , y , x2 , y2 ) ;
2013-10-28 19:13:01 +00:00
}
}
catch ( Exception )
{
// need to stop the script from here
return ;
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawEllipse" ,
2014-01-27 03:16:05 +00:00
"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"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public void DrawEllipse ( int x , int y , int width , int height , object line , object background = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
try
{
2014-01-27 03:16:05 +00:00
g . DrawEllipse ( GetPen ( line ? ? "white" ) , x , y , width , height ) ;
2013-10-28 19:13:01 +00:00
if ( background ! = null )
{
var brush = GetBrush ( background ) ;
2014-01-27 03:16:05 +00:00
g . FillEllipse ( brush , x , y , width , height ) ;
2013-10-28 19:13:01 +00:00
}
}
catch ( Exception )
{
// need to stop the script from here
return ;
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawIcon" ,
2014-01-27 03:16:05 +00:00
"draws an Icon (.ico) file from the given path at the given coordinate. width and height are optional. If specified, it will resize the image accordingly"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public void DrawIcon ( string path , int x , int y , int? width = null , int? height = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
try
{
Icon icon ;
2014-01-27 03:16:05 +00:00
if ( width . HasValue & & height . HasValue )
2013-10-28 19:13:01 +00:00
{
2014-01-27 03:16:05 +00:00
icon = new Icon ( path , width . Value , height . Value ) ;
2013-10-28 19:13:01 +00:00
}
else
{
2014-01-26 16:15:45 +00:00
icon = new Icon ( path ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-27 03:16:05 +00:00
g . DrawIcon ( icon , x , y ) ;
2013-10-28 19:13:01 +00:00
}
catch ( Exception )
{
return ;
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawImage" ,
2014-01-27 03:16:05 +00:00
"draws an image file from the given path at the given coordinate. width and height are optional. If specified, it will resize the image accordingly"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public void DrawImage ( string path , int x , int y , int? width = null , int? height = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
2014-01-27 03:16:05 +00:00
var img = Image . FromFile ( path ) ;
g . DrawImage ( img , x , y , width ? ? img . Width , height ? ? img . Height ) ;
2013-10-28 19:13:01 +00:00
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawLine" ,
2014-01-27 03:16:05 +00:00
"Draws a line from the first coordinate pair to the 2nd. Color is optional (if not specified it will be drawn black)"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public void DrawLine ( int x1 , int y1 , int x2 , int y2 , object color = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
2014-01-27 03:16:05 +00:00
g . DrawLine ( GetPen ( color ? ? "white" ) , x1 , y1 , x2 , y2 ) ;
2013-10-28 19:13:01 +00:00
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawPie" ,
2014-01-27 03:16:05 +00:00
"draws a Pie shape at the given coordinates and the given width and height"
2014-01-26 16:15:45 +00:00
) ]
public void DrawPie (
2014-01-27 03:16:05 +00:00
int x ,
int y ,
int width ,
int height ,
int startangle ,
int sweepangle ,
2014-01-26 16:15:45 +00:00
object line ,
object background = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
2014-01-27 03:16:05 +00:00
g . DrawPie ( GetPen ( line ) , x , y , width , height , startangle , sweepangle ) ;
if ( background ! = null )
2013-10-28 19:13:01 +00:00
{
2014-01-27 03:16:05 +00:00
var brush = GetBrush ( background ) ;
g . FillPie ( brush , x , y , width , height , startangle , sweepangle ) ;
2013-10-28 19:13:01 +00:00
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawPixel" ,
2014-01-27 03:16:05 +00:00
"Draws a single pixel at the given coordinates in the given color. Color is optional (if not specified it will be drawn black)"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public void DrawPixel ( int x , int y , object color = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
try
{
2014-01-27 03:16:05 +00:00
g . DrawLine ( GetPen ( color ? ? "white" ) , x , y , x + 0.1F , y ) ;
2013-10-28 19:13:01 +00:00
}
catch ( Exception )
{
return ;
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawPolygon" ,
2014-01-27 03:16:05 +00:00
"Draws a polygon using the table of coordinates specified in points. Line is the color of the polygon. Background is the optional fill color"
2014-01-26 16:15:45 +00:00
) ]
public void DrawPolygon ( LuaTable points , object line , object background = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2014-01-26 16:15:45 +00:00
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
try
{
2014-01-26 16:15:45 +00:00
var pointsArr = new Point [ points . Values . Count ] ;
var i = 0 ;
2013-10-28 19:13:01 +00:00
foreach ( LuaTable point in points . Values )
{
2014-01-26 16:15:45 +00:00
pointsArr [ i ] = new Point ( LuaInt ( point [ 1 ] ) , LuaInt ( point [ 2 ] ) ) ;
2013-10-28 19:13:01 +00:00
i + + ;
}
2014-01-26 16:15:45 +00:00
g . DrawPolygon ( GetPen ( line ) , pointsArr ) ;
2013-10-28 19:13:01 +00:00
if ( background ! = null )
{
2014-01-26 16:15:45 +00:00
g . FillPolygon ( GetBrush ( background ) , pointsArr ) ;
2013-10-28 19:13:01 +00:00
}
}
catch ( Exception )
{
return ;
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawRectangle" ,
2014-01-27 03:16:05 +00:00
"Draws a rectangle at the given coordinate and the given width and height. Line is the color of the box. Background is the optional fill color"
2014-01-26 16:15:45 +00:00
) ]
2014-01-27 03:16:05 +00:00
public void DrawRectangle ( int x , int y , int width , int height , object line , object background = null )
2013-10-28 19:13:01 +00:00
{
using ( var g = GetGraphics ( ) )
{
2014-01-27 03:16:05 +00:00
g . DrawRectangle ( GetPen ( line ? ? "white" ) , x , y , width , height ) ;
if ( background ! = null )
2013-10-28 19:13:01 +00:00
{
2014-01-27 03:16:05 +00:00
g . FillRectangle ( GetBrush ( background ) , x , y , width , height ) ;
2013-10-28 19:13:01 +00:00
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawString" ,
2014-01-27 03:16:05 +00:00
"Alias of gui.drawText()"
2014-01-26 16:15:45 +00:00
) ]
public void DrawString (
2014-01-27 03:16:05 +00:00
int x ,
int y ,
2014-01-26 16:15:45 +00:00
string message ,
object color = null ,
2014-01-27 03:16:05 +00:00
int? fontsize = null ,
2014-01-26 16:15:45 +00:00
string fontfamily = null ,
string fontstyle = null )
2013-10-28 19:13:01 +00:00
{
2014-01-26 16:15:45 +00:00
DrawText ( x , y , message , color , fontsize , fontfamily , fontstyle ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"drawText" ,
2014-01-27 03:16:05 +00:00
"Draws the given message in the emulator screen space (like all draw functions) at the given x,y coordinates and the given color. The default color is white. A fontfamily can be specified and is monospace generic if none is specified (font family options are the same as the .NET FontFamily class. The fontsize default is 12. The default font style. Font style options are regular, bold, italic, strikethrough, underline"
2014-01-26 16:15:45 +00:00
) ]
public void DrawText (
2014-01-27 03:16:05 +00:00
int x ,
int y ,
2014-01-26 16:15:45 +00:00
string message ,
object color = null ,
2014-01-27 03:16:05 +00:00
int? fontsize = null ,
2014-01-26 16:15:45 +00:00
string fontfamily = null ,
string fontstyle = null )
2013-10-28 19:13:01 +00:00
{
2013-11-03 16:07:58 +00:00
GlobalWin . DisplayManager . NeedsToPaint = true ;
2013-10-28 19:13:01 +00:00
using ( var g = GetGraphics ( ) )
{
try
{
2014-01-26 16:15:45 +00:00
var family = FontFamily . GenericMonospace ;
2013-10-28 19:13:01 +00:00
if ( fontfamily ! = null )
{
2014-01-26 16:15:45 +00:00
family = new FontFamily ( fontfamily ) ;
2013-10-28 19:13:01 +00:00
}
2014-01-26 16:15:45 +00:00
var fstyle = FontStyle . Regular ;
2013-10-28 19:13:01 +00:00
if ( fontstyle ! = null )
{
2014-01-26 16:15:45 +00:00
switch ( fontstyle . ToLower ( ) )
2013-10-28 19:13:01 +00:00
{
default :
case "regular" :
break ;
case "bold" :
fstyle = FontStyle . Bold ;
break ;
case "italic" :
fstyle = FontStyle . Italic ;
break ;
case "strikethrough" :
fstyle = FontStyle . Strikeout ;
break ;
case "underline" :
fstyle = FontStyle . Underline ;
break ;
}
}
2014-01-27 03:16:05 +00:00
var font = new Font ( family , fontsize ? ? 12 , fstyle , GraphicsUnit . Pixel ) ;
2014-03-23 17:24:06 +00:00
g . DrawString ( message , font , GetBrush ( color ? ? "white" ) , x , y ) ;
2013-10-28 19:13:01 +00:00
}
catch ( Exception )
{
return ;
}
}
}
2014-01-26 16:15:45 +00:00
[ LuaMethodAttributes (
"text" ,
2014-01-27 03:16:05 +00:00
"Displays the given text on the screen at the given coordinates. Optional Foreground and background colors. The optional anchor flag anchors the text to one of the four corners. Anchor flag parameters: topleft, topright, bottomleft, bottomright"
2014-01-26 16:15:45 +00:00
) ]
public void Text (
2014-01-27 03:16:05 +00:00
int x ,
int y ,
2014-01-26 16:15:45 +00:00
string message ,
object background = null ,
object forecolor = null ,
2014-02-17 01:57:25 +00:00
string anchor = null )
2013-10-28 19:13:01 +00:00
{
2014-03-23 17:39:07 +00:00
var a = 0 ;
if ( ! string . IsNullOrEmpty ( anchor ) )
{
switch ( anchor )
{
case "0" :
case "topleft" :
a = 0 ;
break ;
case "1" :
case "topright" :
a = 1 ;
break ;
case "2" :
case "bottomleft" :
a = 2 ;
break ;
case "3" :
case "bottomright" :
a = 3 ;
break ;
}
}
else
{
x - = Global . Emulator . CoreComm . ScreenLogicalOffsetX ;
y - = Global . Emulator . CoreComm . ScreenLogicalOffsetY ;
}
// blah hacks
x * = EmuHawkLuaLibrary . GetWindowSize ( ) ;
y * = EmuHawkLuaLibrary . GetWindowSize ( ) ;
GlobalWin . OSD . AddGUIText ( message , x , y , false , GetColor ( background ? ? "black" ) , GetColor ( forecolor ? ? "white" ) , a ) ;
2013-10-28 19:13:01 +00:00
}
}
}