From 4f34027d957447639bff8e3b8f848d2dbb95a079 Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 24 Dec 2013 07:39:54 +0000 Subject: [PATCH] DGL: Add App IdleCallback, use "_" prefix for private calls --- dgl/App.hpp | 18 ++++++++++++++---- dgl/Window.hpp | 7 ++++--- dgl/src/App.cpp | 32 +++++++++++++++++++++++++++----- dgl/src/Widget.cpp | 4 ++-- dgl/src/Window.cpp | 44 ++++++++++++++++++++++---------------------- 5 files changed, 69 insertions(+), 36 deletions(-) diff --git a/dgl/App.hpp b/dgl/App.hpp index 790f2f04..754575cb 100644 --- a/dgl/App.hpp +++ b/dgl/App.hpp @@ -28,6 +28,13 @@ class Window; class App { public: + class IdleCallback + { + public: + ~IdleCallback() {} + virtual void idleCallback() = 0; + }; + App(); ~App(); @@ -36,15 +43,18 @@ public: void quit(); bool isQuiting() const; + void addIdleCallback(IdleCallback* const callback); + void removeIdleCallback(IdleCallback* const callback); + private: struct PrivateData; PrivateData* const pData; friend class Window; - void addWindow(Window* const window); - void removeWindow(Window* const window); - void oneShown(); - void oneHidden(); + void _addWindow(Window* const window); + void _removeWindow(Window* const window); + void _oneShown(); + void _oneHidden(); }; // ----------------------------------------------------------------------- diff --git a/dgl/Window.hpp b/dgl/Window.hpp index 1cd5b225..2a3282ae 100644 --- a/dgl/Window.hpp +++ b/dgl/Window.hpp @@ -46,7 +46,6 @@ public: void exec(bool lockWait = false); void focus(); - void idle(); void repaint(); bool isVisible() const noexcept; @@ -72,10 +71,12 @@ public: private: class PrivateData; PrivateData* const pData; + friend class App; friend class Widget; - void addWidget(Widget* const widget); - void removeWidget(Widget* const widget); + void _addWidget(Widget* const widget); + void _removeWidget(Widget* const widget); + void _idle(); }; // ----------------------------------------------------------------------- diff --git a/dgl/src/App.cpp b/dgl/src/App.cpp index 9bbdf71f..d7568550 100644 --- a/dgl/src/App.cpp +++ b/dgl/src/App.cpp @@ -27,6 +27,7 @@ struct App::PrivateData { bool doLoop; unsigned visibleWindows; std::list windows; + std::list idleCallbacks; PrivateData() : doLoop(false), @@ -43,6 +44,7 @@ App::App() App::~App() { pData->windows.clear(); + pData->idleCallbacks.clear(); delete pData; } @@ -51,7 +53,13 @@ void App::idle() for (std::list::iterator it = pData->windows.begin(); it != pData->windows.end(); ++it) { Window* const window(*it); - window->idle(); + window->_idle(); + } + + for (std::list::iterator it = pData->idleCallbacks.begin(); it != pData->idleCallbacks.end(); ++it) + { + IdleCallback* const idleCallback(*it); + idleCallback->idleCallback(); } } @@ -82,25 +90,39 @@ bool App::isQuiting() const // ----------------------------------------------------------------------- -void App::addWindow(Window* const window) +void App::addIdleCallback(IdleCallback* const callback) +{ + if (callback != nullptr) + pData->idleCallbacks.push_back(callback); +} + +void App::removeIdleCallback(IdleCallback* const callback) +{ + if (callback != nullptr) + pData->idleCallbacks.remove(callback); +} + +// ----------------------------------------------------------------------- + +void App::_addWindow(Window* const window) { if (window != nullptr) pData->windows.push_back(window); } -void App::removeWindow(Window* const window) +void App::_removeWindow(Window* const window) { if (window != nullptr) pData->windows.remove(window); } -void App::oneShown() +void App::_oneShown() { if (++pData->visibleWindows == 1) pData->doLoop = true; } -void App::oneHidden() +void App::_oneHidden() { if (--pData->visibleWindows == 0) pData->doLoop = false; diff --git a/dgl/src/Widget.cpp b/dgl/src/Widget.cpp index 47e5587e..0b20d536 100644 --- a/dgl/src/Widget.cpp +++ b/dgl/src/Widget.cpp @@ -29,12 +29,12 @@ Widget::Widget(Window& parent) : fParent(parent), fVisible(true) { - fParent.addWidget(this); + fParent._addWidget(this); } Widget::~Widget() { - fParent.removeWidget(this); + fParent._removeWidget(this); } bool Widget::isVisible() const noexcept diff --git a/dgl/src/Window.cpp b/dgl/src/Window.cpp index 474919ae..930583df 100644 --- a/dgl/src/Window.cpp +++ b/dgl/src/Window.cpp @@ -132,7 +132,7 @@ public: init(); DBG("Embed window always visible\n"); - fApp.oneShown(); + fApp._oneShown(); fFirstInit = false; } @@ -173,7 +173,7 @@ public: // process any initial events puglProcessEvents(fView); - fApp.addWindow(fSelf); + fApp._addWindow(fSelf); } ~PrivateData() @@ -185,7 +185,7 @@ public: if (fSelf != nullptr && fView != nullptr) { - fApp.removeWindow(fSelf); + fApp._removeWindow(fSelf); puglDestroy(fView); } @@ -201,7 +201,7 @@ public: if (! fFirstInit) { - fApp.oneHidden(); + fApp._oneHidden(); fFirstInit = true; } } @@ -250,17 +250,9 @@ public: #endif } - void idle() - { - puglProcessEvents(fView); - - if (fVisible && fModal.enabled && fModal.parent != nullptr) - fModal.parent->idle(); - } - void repaint() { - DBG("Window repaint\n"); + //DBG("Window repaint\n"); puglPostRedisplay(fView); } @@ -317,7 +309,7 @@ public: { if (fFirstInit) { - fApp.oneShown(); + fApp._oneShown(); fFirstInit = false; } } @@ -471,6 +463,14 @@ public: fWidgets.remove(widget); } + void idle() + { + puglProcessEvents(fView); + + if (fVisible && fModal.enabled && fModal.parent != nullptr) + fModal.parent->idle(); + } + // ------------------------------------------------------------------- void exec_init() @@ -522,7 +522,7 @@ public: protected: void onDisplay() { - DBG("PUGL: onDisplay\n"); + //DBG("PUGL: onDisplay\n"); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -783,11 +783,6 @@ void Window::focus() pData->focus(); } -void Window::idle() -{ - pData->idle(); -} - void Window::repaint() { pData->repaint(); @@ -860,16 +855,21 @@ intptr_t Window::getWindowId() const return pData->getWindowId(); } -void Window::addWidget(Widget* const widget) +void Window::_addWidget(Widget* const widget) { pData->addWidget(widget); } -void Window::removeWidget(Widget* const widget) +void Window::_removeWidget(Widget* const widget) { pData->removeWidget(widget); } +void Window::_idle() +{ + pData->idle(); +} + // ----------------------------------------------------------------------- END_NAMESPACE_DGL