From e14056d2ba7a80bf2a1e6b42b42a67512f75280b Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Thu, 24 Jun 2010 13:28:49 -0400 Subject: [PATCH] GUIScript: Add ability to call callbacks with an int parameter. Signed-off-by: Tom Prince --- gemrb/core/Callback.cpp | 5 +++++ gemrb/core/Callback.h | 1 + gemrb/plugins/GUIScript/PythonHelpers.cpp | 27 +++++++++++++++++++++++++-- gemrb/plugins/GUIScript/PythonHelpers.h | 2 ++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/gemrb/core/Callback.cpp b/gemrb/core/Callback.cpp index 930592c7e..8cfb7bfa4 100644 --- a/gemrb/core/Callback.cpp +++ b/gemrb/core/Callback.cpp @@ -26,3 +26,8 @@ bool Callback::call() { return true; } + +bool Callback::call(int) +{ + return true; +} diff --git a/gemrb/core/Callback.h b/gemrb/core/Callback.h index 90e53edd2..208e6286e 100644 --- a/gemrb/core/Callback.h +++ b/gemrb/core/Callback.h @@ -27,6 +27,7 @@ class GEM_EXPORT Callback : public Held { public: virtual ~Callback(); virtual bool call (); + virtual bool call (int); }; typedef Holder EventHandler; diff --git a/gemrb/plugins/GUIScript/PythonHelpers.cpp b/gemrb/plugins/GUIScript/PythonHelpers.cpp index f00407f5e..e7ce62741 100644 --- a/gemrb/plugins/GUIScript/PythonHelpers.cpp +++ b/gemrb/plugins/GUIScript/PythonHelpers.cpp @@ -20,12 +20,13 @@ #include "PythonHelpers.h" -static bool CallPython(PyObject *Function) +static bool CallPython(PyObject *Function, PyObject *args = NULL) { if (!Function) { return false; } - PyObject *ret = PyObject_CallObject(Function, NULL); + PyObject *ret = PyObject_CallObject(Function, args); + Py_XDECREF( args ); if (ret == NULL) { if (PyErr_Occurred()) { PyErr_Print(); @@ -36,6 +37,15 @@ static bool CallPython(PyObject *Function) return true; } +static bool CallPython(PyObject *Function, int arg) +{ + if (!Function) { + return false; + } + PyObject *args = Py_BuildValue("(i)", arg); + return CallPython(Function, args); +} + StringCallback::StringCallback(const char *str) : Name(PyString_FromString(const_cast(str))) { @@ -72,6 +82,11 @@ bool StringCallback::call() return CallPython(GetFunction()); } +bool StringCallback::call(int arg) +{ + return CallPython(GetFunction(), arg); +} + PythonCallback::PythonCallback(PyObject *Function) : Function(Function) { @@ -96,3 +111,11 @@ bool PythonCallback::call () } return CallPython(Function); } + +bool PythonCallback::call (int arg) +{ + if (!Function || !Py_IsInitialized()) { + return false; + } + return CallPython(Function, arg); +} diff --git a/gemrb/plugins/GUIScript/PythonHelpers.h b/gemrb/plugins/GUIScript/PythonHelpers.h index 69f74eed2..fa6cee4ec 100644 --- a/gemrb/plugins/GUIScript/PythonHelpers.h +++ b/gemrb/plugins/GUIScript/PythonHelpers.h @@ -34,6 +34,7 @@ public: StringCallback(PyObject*); ~StringCallback(); bool call(); + bool call(int); private: PyObject *GetFunction(); PyObject* Name; @@ -44,6 +45,7 @@ public: PythonCallback(PyObject *Function); ~PythonCallback(); bool call(); + bool call(int); private: PyObject *Function; }; -- 2.11.4.GIT