From: <wjp...@us...> - 2009-01-31 13:22:01
|
Revision: 5589 http://gemrb.svn.sourceforge.net/gemrb/?rev=5589&view=rev Author: wjpalenstijn Date: 2009-01-31 13:21:56 +0000 (Sat, 31 Jan 2009) Log Message: ----------- wrapper Python classes for use in GUIScript GUI manipulation. Unused and barely tested for now Modified Paths: -------------- gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.cpp gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.h Added Paths: ----------- gemrb/trunk/gemrb/GUIScripts/GUIClasses.py gemrb/trunk/gemrb/GUIScripts/MetaClasses.py Added: gemrb/trunk/gemrb/GUIScripts/GUIClasses.py =================================================================== --- gemrb/trunk/gemrb/GUIScripts/GUIClasses.py (rev 0) +++ gemrb/trunk/gemrb/GUIScripts/GUIClasses.py 2009-01-31 13:21:56 UTC (rev 5589) @@ -0,0 +1,157 @@ +#-*-python-*- +#GemRB - Infinity Engine Emulator +#Copyright (C) 2009 The GemRB Project +# +#This program is free software; you can redistribute it and/or +#modify it under the terms of the GNU General Public License +#as published by the Free Software Foundation; either version 2 +#of the License, or (at your option) any later version. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with this program; if not, write to the Free Software +#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +# $Id$ + +import GemRB + +from MetaClasses import metaIDWrapper, metaControl +#from exceptions import RuntimeError + +class GTable: + __metaclass__ = metaIDWrapper + methods = { + 'GetValue': GemRB.GetTableValue, + 'FindValue': GemRB.FindTableValue, + 'GetRowIndex': GemRB.GetTableRowIndex, + 'GetRowName': GemRB.GetTableRowName, + 'GetColumnIndex': GemRB.GetTableColumnIndex, + 'GetColumnName': GemRB.GetTableColumnName, + 'GetRowCount': GemRB.GetTableRowCount, + 'GetColumnCount': GemRB.GetTableColumnCount + } + def __del__(self): + GemRB.UnloadTable(self.ID) + +class GSymbol: + __metaclass__ = metaIDWrapper + methods = { + 'GetValue': GemRB.GetSymbolValue, + 'Unload': GemRB.UnloadSymbol + } + +class GWindow: + __metaclass__ = metaIDWrapper + methods = { + 'SetSize': GemRB.SetWindowSize, + 'SetFrame': GemRB.SetWindowFrame, + 'SetPicture': GemRB.SetWindowPicture, + 'SetPos': GemRB.SetWindowPos, + 'HasControl': GemRB.HasControl, + 'DeleteControl': GemRB.DeleteControl, + 'Unload': GemRB.UnloadWindow, + 'SetupEquipmentIcons': GemRB.SetupEquipmentIcons, + 'SetupSpellIcons': GemRB.SetupSpellIcons, + 'SetupControls': GemRB.SetupControls, + 'SetVisible': GemRB.SetVisible, + 'ShowModal': GemRB.ShowModal, + 'Invalidate': GemRB.InvalidateWindow + } + def GetControl(self, control): + return GemRB.GetControlObject(self.ID, control) + def CreateWorldMapControl(self, control, *args): + GemRB.CreateWorldMapControl(self.ID, control, *args) + return GemRB.GetControlObject(self.ID, control) + def CreateMapControl(self, control, *args): + GemRB.CreateMapControl(self.ID, control, *args) + return GemRB.GetControlObject(self.ID, control) + def CreateLabel(self, control, *args): + GemRB.CreateLabel(self.ID, control, *args) + return GemRB.GetControlObject(self.ID, control) + + +class GControl: + __metaclass__ = metaControl + methods = { + 'SetVarAssoc': GemRB.SetVarAssoc, + 'SetPos': GemRB.SetControlPos, + 'SetSize': GemRB.SetControlSize, + 'SetDefaultScrollBar': GemRB.SetDefaultScrollBar, + 'SetAnimationPalette': GemRB.SetAnimationPalette, + 'SetAnimation': GemRB.SetAnimation, + 'QueryText': GemRB.QueryText, + 'SetText': GemRB.SetText, + 'SetTooltip': GemRB.SetTooltip, + 'SetEvent': GemRB.SetEvent, + 'SetStatus': GemRB.SetControlStatus, + } + def AttachScrollBar(self, scrollbar): + if self.WinID != scrollbar.WinID: + raise RuntimeError, "Scrollbar must be in same Window as Control" + return GemRB.AttachScrollBar(self.WinID, self.ID, scrollbar.ID) + +class GLabel(GControl): + __metaclass__ = metaControl + methods = { + 'SetUseRGB': GemRB.SetLabelUseRGB + } + +class GTextArea(GControl): + __metaclass__ = metaControl + methods = { + 'Rewind': GemRB.RewindTA, + 'SetHistory': GemRB.SetTAHistory, + 'Append': GemRB.TextAreaAppend, + 'Clear': GemRB.TextAreaClear, + 'Scroll': GemRB.TextAreaScroll, + 'SetFlags': GemRB.SetTextAreaFlags + } + def MoveText(self, other): + GemRB.MoveTAText(self.WinID, self.ID, other.WinID, other.ID) + +class GTextEdit(GControl): + __metaclass__ = metaControl + methods = { + 'SetBufferLength': GemRB.SetBufferLength + } + +class GButton(GControl): + __metaclass__ = metaControl + methods = { + 'SetSprites': GemRB.SetButtonSprites, + 'SetOverlay': GemRB.SetButtonOverlay, + 'SetBorder': GemRB.SetButtonBorder, + 'EnableBorder': GemRB.EnableButtonBorder, + 'SetFont': GemRB.SetButtonFont, + 'SetTextColor': GemRB.SetButtonTextColor, + 'SetFlags': GemRB.SetButtonFlags, + 'SetState': GemRB.SetButtonState, + 'SetPictureClipping': GemRB.SetButtonPictureClipping, + 'SetPicture': GemRB.SetButtonPicture, + 'SetMOS': GemRB.SetButtonMOS, + 'SetPLT': GemRB.SetButtonPLT, + 'SetBAM': GemRB.SetButtonBAM, + 'SetSaveGamePortrait': GemRB.SetSaveGamePortrait, + 'SetSaveGamePreview': GemRB.SetSaveGamePreview, + 'SetGamePreview': GemRB.SetGamePreview, + 'SetGamePortraitPreview': GemRB.SetGamePortraitPreview, + 'SetSpellIcon': GemRB.SetSpellIcon, + 'SetItemIcon': GemRB.SetItemIcon, + } + def CreateLabelOnButton(self, control, *args): + GemRB.CreateLabelOnButton(self.WinID, self.ID, control, *args) + return GemRB.GetControlObject(self.WinID, control) + +class GWorldMap(GControl): + __metaclass__ = metaControl + methods = { + 'AdjustScrolling': GemRB.AdjustScrolling, + 'GetDestinationArea': GemRB.GetDestinationArea, + 'SetTextColor': GemRB.SetWorldMapTextColor + } + Property changes on: gemrb/trunk/gemrb/GUIScripts/GUIClasses.py ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Id Added: svn:eol-style + native Added: gemrb/trunk/gemrb/GUIScripts/MetaClasses.py =================================================================== --- gemrb/trunk/gemrb/GUIScripts/MetaClasses.py (rev 0) +++ gemrb/trunk/gemrb/GUIScripts/MetaClasses.py 2009-01-31 13:21:56 UTC (rev 5589) @@ -0,0 +1,70 @@ +#-*-python-*- +#GemRB - Infinity Engine Emulator +#Copyright (C) 2009 The GemRB Project +# +#This program is free software; you can redistribute it and/or +#modify it under the terms of the GNU General Public License +#as published by the Free Software Foundation; either version 2 +#of the License, or (at your option) any later version. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with this program; if not, write to the Free Software +#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +# $Id$ + +# The metaclasses below are used to define objects that call +# functions in the GemRB module. +# +# Example: +# class GTable: +# __metaclass__ = metaIDWrapper +# methods = { +# 'GetValue': GemRB.GetTableValue, +# } +# +# x = GTable(5) +# +# Calling +# x.GetValue("Row", "Col") +# will then execute +# GemRB.GetTableValue(5, "Row", "Col") + +def make_caller_lambda_ID(M): + return lambda self, *args: M(self.ID, *args) +class metaIDWrapper(type): + def __new__(cls, classname, bases, classdict): + def __init__(self, ID): + self.ID = ID + newdict = { '__slots__':['ID'], '__init__':__init__, } + methods = classdict['methods'] + for key in methods: + newdict[key] = make_caller_lambda_ID(methods[key]) + for key in classdict: + if key != 'methods': + newdict[key] = classdict[key] + return type.__new__(cls, classname, bases, newdict) + + +# metaControl has two extra arguments: WinID and ID +def make_caller_lambda_Control(M): + return lambda self, *args: M(self.WinID, self.ID, *args) +class metaControl(type): + def __new__(cls, classname, bases, classdict): + def __init__(self, WinID, ID): + self.WinID = WinID + self.ID = ID + newdict = { '__slots__':['WinID', 'ID'], '__init__':__init__, } + methods = classdict['methods'] + for key in methods: + newdict[key] = make_caller_lambda_Control(methods[key]) + for key in classdict: + if key != 'methods': + newdict[key] = classdict[key] + return type.__new__(cls, classname, bases, newdict) + Property changes on: gemrb/trunk/gemrb/GUIScripts/MetaClasses.py ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Id Added: svn:eol-style + native Modified: gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.cpp =================================================================== --- gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.cpp 2009-01-31 13:05:54 UTC (rev 5588) +++ gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.cpp 2009-01-31 13:21:56 UTC (rev 5589) @@ -632,6 +632,36 @@ return PyInt_FromLong( ret ); } +PyDoc_STRVAR( GemRB_LoadWindowObject__doc, +"LoadWindowObject(WindowID) => GWindow\n\n" +"Returns a Window as an object." ); + +static PyObject* GemRB_LoadWindowObject(PyObject * self, PyObject* args) +{ + int WindowID; + if (!PyArg_ParseTuple( args, "i", &WindowID )) { + return AttributeError( GemRB_LoadWindowObject__doc ); + } + + PyObject* win = GemRB_LoadWindow(self, args); + if (!PyObject_TypeCheck( win, &PyInt_Type )) + return win; // exception + + PyObject* wintuple = PyTuple_New(1); + PyTuple_SET_ITEM(wintuple, 0, win); + + GUIScript *gs = (GUIScript *) core->GetGUIScriptEngine(); + PyObject* ret = gs->ConstructObject("GWindow", wintuple); + Py_DECREF(wintuple); + if (!ret) { + char buf[256]; + snprintf( buf, sizeof( buf ), "Couldn't construct Window object for window %d!", WindowID ); + return RuntimeError(buf); + } + return ret; +} + + PyDoc_STRVAR( GemRB_SetWindowSize__doc, "SetWindowSize(WindowIndex, Width, Height)\n\n" "Resizes a Window."); @@ -1209,6 +1239,7 @@ return AttributeError( GemRB_GetControl__doc ); } + int ret = core->GetControl( WindowIndex, ControlID ); if (ret == -1) { return RuntimeError( "Control is not found" ); @@ -1217,6 +1248,67 @@ return PyInt_FromLong( ret ); } +PyDoc_STRVAR( GemRB_GetControlObject__doc, +"GetControlObject(WindowID, ControlID) => GControl, or\n" +"Window.GetControl(ControlID) => GControl\n\n" +"Returns a control as on object." ); + +static PyObject* GemRB_GetControlObject(PyObject * self, PyObject* args) +{ + int WindowIndex, ControlID; + + if (!PyArg_ParseTuple( args, "ii", &WindowIndex, &ControlID )) { + return AttributeError( GemRB_GetControlObject__doc ); + } + + PyObject* control = GemRB_GetControl( self, args ); + if (!PyObject_TypeCheck( control, &PyInt_Type )) + return control; // exception + + PyObject* ctrltuple = PyTuple_New(2); + PyTuple_SET_ITEM(ctrltuple, 0, PyInt_FromLong(WindowIndex)); + PyTuple_SET_ITEM(ctrltuple, 1, control); + + PyObject* ret = 0; + // TODO: get this from 'control' python variable + int ctrlindex = core->GetControl(WindowIndex, ControlID); + Control *ctrl = GetControl(WindowIndex, ctrlindex, -1); + if (!ctrl) { + // GetControl will already have raised an exception + return 0; + } + const char* type = "GControl"; + switch(ctrl->ControlType) { + case IE_GUI_LABEL: + type = "GLabel"; + break; + case IE_GUI_EDIT: + type = "GTextEdit"; + break; + case IE_GUI_TEXTAREA: + type = "GTextArea"; + break; + case IE_GUI_BUTTON: + type = "GButton"; + break; + case IE_GUI_WORLDMAP: + type = "GWorldMap"; + break; + default: + break; + } + GUIScript *gs = (GUIScript *) core->GetGUIScriptEngine(); + ret = gs->ConstructObject(type, ctrltuple); + + if (!ret) { + char buf[256]; + snprintf( buf, sizeof( buf ), "Couldn't construct Control object for control %d in window %d!", ControlID, WindowIndex ); + return RuntimeError(buf); + } + Py_DECREF(ctrltuple); + return ret; +} + PyDoc_STRVAR( GemRB_HasControl__doc, "HasControl(WindowIndex, ControlID[,ControlType]) => bool\n\n" "Returns true if the control exists." ); @@ -8672,6 +8764,7 @@ METHOD(GetContainer, METH_VARARGS), METHOD(GetContainerItem, METH_VARARGS), METHOD(GetControl, METH_VARARGS), + METHOD(GetControlObject, METH_VARARGS), METHOD(GetCurrentArea, METH_NOARGS), METHOD(GetDestinationArea, METH_VARARGS), METHOD(GetEquippedAmmunition, METH_VARARGS), @@ -8744,6 +8837,7 @@ METHOD(LoadTable, METH_VARARGS), METHOD(LoadWindowPack, METH_VARARGS), METHOD(LoadWindow, METH_VARARGS), + METHOD(LoadWindowObject, METH_VARARGS), METHOD(LoadWindowFrame, METH_VARARGS), METHOD(MemorizeSpell, METH_VARARGS), METHOD(ModifyEffect, METH_VARARGS), @@ -8988,6 +9082,13 @@ printf("Check if %s/GUIDefines.py exists! ", path); return false; } + + if (PyRun_SimpleString( "from GUIClasses import *" ) == -1) { + printMessage( "GUIScript", " ", RED ); + printf("Check if %s/GUIClasses.py exists! ", path); + return false; + } + PyObject *pMainMod = PyImport_AddModule( "__main__" ); /* pMainMod is a borrowed reference */ pMainDic = PyModule_GetDict( pMainMod ); @@ -9111,3 +9212,22 @@ free( newstr ); } } + +PyObject* GUIScript::ConstructObject(const char* classname, PyObject* pArgs) +{ +// PyObject* cname = PyString_InternFromString(classname); +// PyObject* cobj = PyObject_GetAttr(pDict, cname); +// Py_DECREF(cname); + PyObject* cobj = PyDict_GetItemString( pDict, classname ); + if (!cobj) { + fprintf(stderr, "Failed to lookup name '%s'\n", classname); + return 0; + } + PyObject* ret = PyObject_Call(cobj, pArgs, NULL); + Py_DECREF(cobj); + if (!ret) { + fprintf(stderr, "Failed to call constructor\n"); + return 0; + } + return ret; +} Modified: gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.h =================================================================== --- gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.h 2009-01-31 13:05:54 UTC (rev 5588) +++ gemrb/trunk/gemrb/plugins/GUIScript/GUIScript.h 2009-01-31 13:21:56 UTC (rev 5589) @@ -56,6 +56,7 @@ void ExecString(const char* string); /** lets hope this one can be here without screwing up the general interface */ PyObject *CallbackFunction(const char* fname, PyObject* pArgs); + PyObject* ConstructObject(const char* classname, PyObject* pArgs); public: void release(void) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |