From eaa323aa65a8d1befd18b3482921fe0111bd6094 Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Sat, 11 Oct 2014 20:38:48 -0700 Subject: [PATCH] display: Migrate the Python window func stuff to an extension. --- makepanda/makepanda.py | 2 + panda/src/display/CMakeLists.txt | 15 ++---- panda/src/display/Sources.pp | 1 + panda/src/display/graphicsWindow.cxx | 48 ------------------ panda/src/display/graphicsWindow.h | 11 +---- panda/src/display/graphicsWindow_ext.cxx | 63 ++++++++++++++++++++++++ panda/src/display/graphicsWindow_ext.h | 42 ++++++++++++++++ 7 files changed, 114 insertions(+), 68 deletions(-) create mode 100644 panda/src/display/graphicsWindow_ext.cxx create mode 100644 panda/src/display/graphicsWindow_ext.h diff --git a/makepanda/makepanda.py b/makepanda/makepanda.py index f729f0b355..5eb8b493cb 100755 --- a/makepanda/makepanda.py +++ b/makepanda/makepanda.py @@ -3276,6 +3276,7 @@ if (not RUNTIME): TargetAdd('libp3display.in', opts=['IMOD:panda3d.core', 'ILIB:libp3display', 'SRCDIR:panda/src/display']) TargetAdd('libp3display_igate.obj', input='libp3display.in', opts=["DEPENDENCYONLY"]) TargetAdd('p3display_graphicsStateGuardian_ext.obj', opts=OPTS, input='graphicsStateGuardian_ext.cxx') + TargetAdd('p3display_graphicsWindow_ext.obj', opts=OPTS, input='graphicsWindow_ext.cxx') if RTDIST and GetTarget() == 'darwin': OPTS=['DIR:panda/src/display'] @@ -3551,6 +3552,7 @@ if (not RUNTIME): TargetAdd('libpanda.dll', input='p3gobj_internalName_ext.obj') TargetAdd('libpanda.dll', input='p3pgraph_ext_composite.obj') TargetAdd('libpanda.dll', input='p3display_graphicsStateGuardian_ext.obj') + TargetAdd('libpanda.dll', input='p3display_graphicsWindow_ext.obj') if PkgSkip("FREETYPE")==0: TargetAdd('libpanda.dll', input="p3pnmtext_composite1.obj") diff --git a/panda/src/display/CMakeLists.txt b/panda/src/display/CMakeLists.txt index 31027d89ea..79f3d935e9 100644 --- a/panda/src/display/CMakeLists.txt +++ b/panda/src/display/CMakeLists.txt @@ -71,19 +71,12 @@ set(P3DISPLAY_SOURCES set(P3DISPLAY_IGATEEXT graphicsStateGuardian_ext.cxx graphicsStateGuardian_ext.h + graphicsWindow_ext.cxx + graphicsWindow_ext.h + pythonGraphicsWindowProc.cxx + pythonGraphicsWindowProc.h ) -if(HAVE_PYTHON) - set(P3DISPLAY_HEADERS - ${P3DISPLAY_HEADERS} - pythonGraphicsWindowProc.h - ) - set(P3DISPLAY_SOURCES - ${P3DISPLAY_SOURCES} - pythonGraphicsWindowProc.cxx - ) -endif() - if(APPLE) set(P3DISPLAY_HEADERS ${P3DISPLAY_HEADERS} diff --git a/panda/src/display/Sources.pp b/panda/src/display/Sources.pp index dfad1153c1..118e87be77 100644 --- a/panda/src/display/Sources.pp +++ b/panda/src/display/Sources.pp @@ -31,6 +31,7 @@ graphicsStateGuardian_ext.cxx graphicsStateGuardian_ext.h \ graphicsThreadingModel.I graphicsThreadingModel.h \ graphicsWindow.I graphicsWindow.h \ + graphicsWindow_ext.cxx graphicsWindow_ext.h \ graphicsWindowInputDevice.I \ graphicsWindowInputDevice.h \ graphicsWindowProc.h \ diff --git a/panda/src/display/graphicsWindow.cxx b/panda/src/display/graphicsWindow.cxx index cd4e3c3e43..caee3ea4ed 100644 --- a/panda/src/display/graphicsWindow.cxx +++ b/panda/src/display/graphicsWindow.cxx @@ -903,54 +903,6 @@ mouse_mode_absolute() { } -#ifdef HAVE_PYTHON - -//////////////////////////////////////////////////////////////////// -// Function: GraphicsWindow::add_custom_event_handler -// Access: Published -// Description: Adds a python event handler to be called -// when a window event occurs. -// -//////////////////////////////////////////////////////////////////// -void GraphicsWindow:: -add_python_event_handler(PyObject* handler, PyObject* name){ - PythonGraphicsWindowProc* pgwp = new PythonGraphicsWindowProc(handler, name); - _python_window_proc_classes.insert(pgwp); - add_window_proc(pgwp); -} - -//////////////////////////////////////////////////////////////////// -// Function: GraphicsWindow::remove_custom_event_handler -// Access: Published -// Description: Removes the specified python event handler. -// -//////////////////////////////////////////////////////////////////// -void GraphicsWindow:: -remove_python_event_handler(PyObject* name){ - list toRemove; - PythonWinProcClasses::iterator iter; - for (iter = _python_window_proc_classes.begin(); iter != _python_window_proc_classes.end(); ++iter) { - PythonGraphicsWindowProc* pgwp = *iter; - if (PyObject_RichCompareBool(pgwp->get_name(), name, Py_EQ) == 1) { - toRemove.push_back(pgwp); - } -#if PY_MAJOR_VERSION < 3 - else if (PyObject_Compare(pgwp->get_name(), name) == 0) { - toRemove.push_back(pgwp); - } -#endif - } - list::iterator iter2; - for (iter2 = toRemove.begin(); iter2 != toRemove.end(); ++iter2) { - PythonGraphicsWindowProc* pgwp = *iter2; - remove_window_proc(pgwp); - _python_window_proc_classes.erase(pgwp); - delete pgwp; - } -} - -#endif // HAVE_PYTHON - //////////////////////////////////////////////////////////////////// // Function: GraphicsWindow::is_touch_event // Access: Published, Virtual diff --git a/panda/src/display/graphicsWindow.h b/panda/src/display/graphicsWindow.h index 2dfe1c3f60..f30d721075 100644 --- a/panda/src/display/graphicsWindow.h +++ b/panda/src/display/graphicsWindow.h @@ -21,9 +21,6 @@ #include "graphicsWindowInputDevice.h" #include "graphicsWindowProc.h" #include "graphicsWindowProcCallbackData.h" -#ifdef HAVE_PYTHON -#include "pythonGraphicsWindowProc.h" -#endif #include "windowProperties.h" #include "mouseData.h" #include "modifierButtons.h" @@ -94,11 +91,6 @@ PUBLISHED: virtual bool move_pointer(int device, int x, int y); virtual void close_ime(); -#ifdef HAVE_PYTHON - void add_python_event_handler(PyObject* handler, PyObject* name); - void remove_python_event_handler(PyObject* name); -#endif - public: // No need to publish these. bool has_button_event(int device) const; @@ -169,7 +161,7 @@ private: bool _unexposed_draw; #ifdef HAVE_PYTHON - typedef pset PythonWinProcClasses; + typedef pset PythonWinProcClasses; PythonWinProcClasses _python_window_proc_classes; #endif @@ -191,6 +183,7 @@ private: static TypeHandle _type_handle; friend class GraphicsEngine; + friend class Extension; }; #include "graphicsWindow.I" diff --git a/panda/src/display/graphicsWindow_ext.cxx b/panda/src/display/graphicsWindow_ext.cxx new file mode 100644 index 0000000000..da7b4d231e --- /dev/null +++ b/panda/src/display/graphicsWindow_ext.cxx @@ -0,0 +1,63 @@ +// Filename: graphicsWindow_ext.cxx +// Created by: CFSworks (11Oct14) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "graphicsWindow_ext.h" + +#ifdef HAVE_PYTHON + +//////////////////////////////////////////////////////////////////// +// Function: Extension::add_custom_event_handler +// Access: Published +// Description: Adds a python event handler to be called +// when a window event occurs. +// +//////////////////////////////////////////////////////////////////// +void Extension:: +add_python_event_handler(PyObject* handler, PyObject* name){ + PythonGraphicsWindowProc* pgwp = new PythonGraphicsWindowProc(handler, name); + _this->_python_window_proc_classes.insert(pgwp); + _this->add_window_proc(pgwp); +} + +//////////////////////////////////////////////////////////////////// +// Function: Extension::remove_custom_event_handler +// Access: Published +// Description: Removes the specified python event handler. +// +//////////////////////////////////////////////////////////////////// +void Extension:: +remove_python_event_handler(PyObject* name){ + list toRemove; + GraphicsWindow::PythonWinProcClasses::iterator iter; + for (iter = _this->_python_window_proc_classes.begin(); iter != _this->_python_window_proc_classes.end(); ++iter) { + PythonGraphicsWindowProc* pgwp = (PythonGraphicsWindowProc*)*iter; + if (PyObject_RichCompareBool(pgwp->get_name(), name, Py_EQ) == 1) { + toRemove.push_back(pgwp); + } +#if PY_MAJOR_VERSION < 3 + else if (PyObject_Compare(pgwp->get_name(), name) == 0) { + toRemove.push_back(pgwp); + } +#endif + } + list::iterator iter2; + for (iter2 = toRemove.begin(); iter2 != toRemove.end(); ++iter2) { + PythonGraphicsWindowProc* pgwp = *iter2; + _this->remove_window_proc(pgwp); + _this->_python_window_proc_classes.erase(pgwp); + delete pgwp; + } +} + +#endif // HAVE_PYTHON diff --git a/panda/src/display/graphicsWindow_ext.h b/panda/src/display/graphicsWindow_ext.h new file mode 100644 index 0000000000..c96ce3c445 --- /dev/null +++ b/panda/src/display/graphicsWindow_ext.h @@ -0,0 +1,42 @@ +// Filename: renderState_ext.h +// Created by: CFSworks (11Oct14) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef GRAPHICSWINDOW_EXT_H +#define GRAPHICSWINDOW_EXT_H + +#include "dtoolbase.h" + +#ifdef HAVE_PYTHON + +#include "extension.h" +#include "graphicsWindow.h" +#include "pythonGraphicsWindowProc.h" +#include "py_panda.h" + +//////////////////////////////////////////////////////////////////// +// Class : Extension +// Description : This class defines the extension methods for +// GraphicsWindow, which are called instead of +// any C++ methods with the same prototype. +//////////////////////////////////////////////////////////////////// +template<> +class Extension : public ExtensionBase { +public: + void add_python_event_handler(PyObject* handler, PyObject* name); + void remove_python_event_handler(PyObject* name); +}; + +#endif // HAVE_PYTHON + +#endif // GRAPHICSWINDOW_EXT_H