diff options
author | Andrei Karas <akaras@inbox.ru> | 2014-03-02 12:15:59 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2014-03-02 12:15:59 +0300 |
commit | e710a36d3905b50861edf21f4b5ac5a79cb5ee05 (patch) | |
tree | 8662ff9b0427f4a4c26f030c8aab480137c1da46 | |
parent | 6709f3df268223662c9ae1622db3bbc93a83cf8f (diff) | |
download | plus-e710a36d3905b50861edf21f4b5ac5a79cb5ee05.tar.gz plus-e710a36d3905b50861edf21f4b5ac5a79cb5ee05.tar.bz2 plus-e710a36d3905b50861edf21f4b5ac5a79cb5ee05.tar.xz plus-e710a36d3905b50861edf21f4b5ac5a79cb5ee05.zip |
Join gui classes in one.
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/events/mouseevent.h | 2 | ||||
-rw-r--r-- | src/gui/base/gui.cpp | 763 | ||||
-rw-r--r-- | src/gui/base/gui.hpp | 515 | ||||
-rw-r--r-- | src/gui/gui.cpp | 561 | ||||
-rw-r--r-- | src/gui/gui.h | 331 |
7 files changed, 889 insertions, 1287 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8b4a052cf..066a481f7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -796,7 +796,6 @@ SET(SRCS listeners/deathlistener.h events/event.h listeners/focuslistener.h - gui/base/gui.hpp events/inputguievent.h input/key.h events/keyevent.h @@ -824,7 +823,6 @@ SET(SRCS gui/widgets/basiccontainer.cpp gui/cliprect.cpp gui/color.cpp - gui/base/gui.cpp input/key.cpp gui/rect.cpp gui/widgets/widget.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 1e372d667..065184849 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -159,7 +159,6 @@ manaplus_SOURCES += events/actionevent.h \ events/event.h \ listeners/focuslistener.h \ render/graphics.h \ - gui/base/gui.hpp \ events/inputguievent.h \ input/key.h \ events/keyevent.h \ @@ -187,7 +186,6 @@ manaplus_SOURCES += events/actionevent.h \ gui/widgets/basiccontainer.cpp \ gui/cliprect.cpp \ gui/color.cpp \ - gui/base/gui.cpp \ input/key.cpp \ gui/rect.cpp \ gui/widgets/widget.cpp \ diff --git a/src/events/mouseevent.h b/src/events/mouseevent.h index a717980f9..8ef65cca4 100644 --- a/src/events/mouseevent.h +++ b/src/events/mouseevent.h @@ -235,7 +235,7 @@ class MouseEvent: public InputGuiEvent * to widget the mouse listener receiving the events have registered * to. */ - friend class gcn::Gui; + friend class Gui; }; #endif // EVENTS_MOUSEEVENT_H diff --git a/src/gui/base/gui.cpp b/src/gui/base/gui.cpp deleted file mode 100644 index 8985ec062..000000000 --- a/src/gui/base/gui.cpp +++ /dev/null @@ -1,763 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2011-2014 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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 - * 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, see <http://www.gnu.org/licenses/>. - */ - -/* _______ __ __ __ ______ __ __ _______ __ __ - * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ - * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / - * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / - * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / - * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / - * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ - * - * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson - * - * - * Per Larsson a.k.a finalman - * Olof Naessén a.k.a jansem/yakslem - * - * Visit: http://guichan.sourceforge.net - * - * License: (BSD) - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name of Guichan nor the names of its contributors may - * be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * For comments regarding functions please see the header file. - */ - -#include "gui/base/gui.hpp" - -#include "gui/widgets/widget.h" - -#include "gui/focushandler.h" - -#include "input/mouseinput.h" - -#include "listeners/keylistener.h" -#include "listeners/mouselistener.h" - -#include "debug.h" - -namespace gcn -{ - Gui::Gui() : - mTop(nullptr), - mGraphics(nullptr), - mInput(nullptr), - mFocusHandler(new FocusHandler), - mTabbing(true), - mKeyListeners(), - mShiftPressed(false), - mMetaPressed(false), - mControlPressed(false), - mAltPressed(false), - mLastMousePressButton(0), - mLastMousePressTimeStamp(0), - mLastMouseX(0), - mLastMouseY(0), - mClickCount(1), - mLastMouseDragButton(0), - mWidgetWithMouseQueue() - { - } - - Gui::~Gui() - { - if (Widget::widgetExists(mTop)) - setTop(nullptr); - - delete mFocusHandler; - mFocusHandler = nullptr; - } - - void Gui::setTop(Widget* top) - { - if (mTop) - mTop->_setFocusHandler(nullptr); - if (top) - top->_setFocusHandler(mFocusHandler); - - mTop = top; - } - - Widget* Gui::getTop() const - { - return mTop; - } - - void Gui::setGraphics(Graphics* graphics) - { - mGraphics = graphics; - } - - Graphics* Gui::getGraphics() const - { - return mGraphics; - } - - void Gui::setInput(SDLInput* input) - { - mInput = input; - } - - SDLInput* Gui::getInput() const - { - return mInput; - } - - void Gui::logic() - { - } - - void Gui::draw() - { - } - - void Gui::focusNone() - { - mFocusHandler->focusNone(); - } - - void Gui::setTabbingEnabled(bool tabbing) - { - mTabbing = tabbing; - } - - bool Gui::isTabbingEnabled() - { - return mTabbing; - } - - void Gui::addGlobalKeyListener(KeyListener* keyListener) - { - mKeyListeners.push_back(keyListener); - } - - void Gui::removeGlobalKeyListener(KeyListener* keyListener) - { - mKeyListeners.remove(keyListener); - } - - void Gui::handleMouseInput() - { - } - - void Gui::handleKeyInput() - { - } - - void Gui::handleMouseMoved(const MouseInput& mouseInput) - { - // Check if the mouse leaves the application window. - if (!mWidgetWithMouseQueue.empty() && (mouseInput.getX() < 0 - || mouseInput.getY() < 0 || !mTop->getDimension().isPointInRect( - mouseInput.getX(), mouseInput.getY()))) - { - // Distribute an event to all widgets in the - // "widget with mouse" queue. - while (!mWidgetWithMouseQueue.empty()) - { - Widget *const widget = mWidgetWithMouseQueue.front(); - - if (Widget::widgetExists(widget)) - { - distributeMouseEvent(widget, - MouseEvent::EXITED, - mouseInput.getButton(), - mouseInput.getX(), - mouseInput.getY(), - true, - true); - } - - mWidgetWithMouseQueue.pop_front(); - } - - return; - } - - // Check if there is a need to send mouse exited events by - // traversing the "widget with mouse" queue. - bool widgetWithMouseQueueCheckDone = mWidgetWithMouseQueue.empty(); - while (!widgetWithMouseQueueCheckDone) - { - unsigned int iterations = 0; - for (std::deque<Widget*>::iterator - iter = mWidgetWithMouseQueue.begin(); - iter != mWidgetWithMouseQueue.end(); - ++ iter) - { - Widget *const widget = *iter; - - // If a widget in the "widget with mouse queue" doesn't - // exists anymore it should be removed from the queue. - if (!Widget::widgetExists(widget)) - { - mWidgetWithMouseQueue.erase(iter); - break; - } - else - { - int x, y; - widget->getAbsolutePosition(x, y); - - if (x > mouseInput.getX() - || y > mouseInput.getY() - || x + widget->getWidth() <= mouseInput.getX() - || y + widget->getHeight() <= mouseInput.getY() - || !widget->isVisible()) - { - distributeMouseEvent(widget, - MouseEvent::EXITED, - mouseInput.getButton(), - mouseInput.getX(), - mouseInput.getY(), - true, - true); - mClickCount = 1; - mLastMousePressTimeStamp = 0; - mWidgetWithMouseQueue.erase(iter); - break; - } - } - - iterations++; - } - - widgetWithMouseQueueCheckDone = - (iterations == mWidgetWithMouseQueue.size()); - } - - // Check all widgets below the mouse to see if they are - // present in the "widget with mouse" queue. If a widget - // is not then it should be added and an entered event should - // be sent to it. - Widget* parent = getMouseEventSource( - mouseInput.getX(), mouseInput.getY()); - Widget* widget = parent; - - // If a widget has modal mouse input focus then it will - // always be returned from getMouseEventSource, but we only wan't to - // send mouse entered events if the mouse has actually entered the - // widget with modal mouse input focus, hence we need to check if - // that's the case. If it's not we should simply ignore to send any - // mouse entered events. - if (mFocusHandler->getModalMouseInputFocused() - && widget == mFocusHandler->getModalMouseInputFocused() - && Widget::widgetExists(widget)) - { - int x, y; - widget->getAbsolutePosition(x, y); - - if (x > mouseInput.getX() || y > mouseInput.getY() - || x + widget->getWidth() <= mouseInput.getX() - || y + widget->getHeight() <= mouseInput.getY()) - { - parent = nullptr; - } - } - - while (parent) - { - parent = widget->getParent(); - - // Check if the widget is present in the "widget with mouse" queue. - bool widgetIsPresentInQueue = false; - FOR_EACH (std::deque<Widget*>::const_iterator, - iter, mWidgetWithMouseQueue) - { - if (*iter == widget) - { - widgetIsPresentInQueue = true; - break; - } - } - - // Widget is not present, send an entered event and add - // it to the "widget with mouse" queue. - if (!widgetIsPresentInQueue - && Widget::widgetExists(widget)) - { - distributeMouseEvent(widget, - MouseEvent::ENTERED, - mouseInput.getButton(), - mouseInput.getX(), - mouseInput.getY(), - true, - true); - mWidgetWithMouseQueue.push_front(widget); - } - - const Widget *const swap = widget; - widget = parent; - parent = swap->getParent(); - } - - if (mFocusHandler->getDraggedWidget()) - { - distributeMouseEvent(mFocusHandler->getDraggedWidget(), - MouseEvent::DRAGGED, - mLastMouseDragButton, - mouseInput.getX(), - mouseInput.getY()); - } - else - { - Widget *const sourceWidget = getMouseEventSource( - mouseInput.getX(), mouseInput.getY()); - - distributeMouseEvent(sourceWidget, - MouseEvent::MOVED, - mouseInput.getButton(), - mouseInput.getX(), - mouseInput.getY()); - } - } - - void Gui::handleMouseWheelMovedDown(const MouseInput& mouseInput) - { - Widget* sourceWidget = getMouseEventSource( - mouseInput.getX(), mouseInput.getY()); - - if (mFocusHandler->getDraggedWidget()) - sourceWidget = mFocusHandler->getDraggedWidget(); - - int sourceWidgetX, sourceWidgetY; - sourceWidget->getAbsolutePosition(sourceWidgetX, sourceWidgetY); - - distributeMouseEvent(sourceWidget, - MouseEvent::WHEEL_MOVED_DOWN, - mouseInput.getButton(), - mouseInput.getX(), - mouseInput.getY()); - } - - void Gui::handleMouseWheelMovedUp(const MouseInput& mouseInput) - { - Widget* sourceWidget = getMouseEventSource( - mouseInput.getX(), mouseInput.getY()); - - if (mFocusHandler->getDraggedWidget()) - sourceWidget = mFocusHandler->getDraggedWidget(); - - int sourceWidgetX, sourceWidgetY; - sourceWidget->getAbsolutePosition(sourceWidgetX, sourceWidgetY); - - distributeMouseEvent(sourceWidget, - MouseEvent::WHEEL_MOVED_UP, - mouseInput.getButton(), - mouseInput.getX(), - mouseInput.getY()); - } - - Widget* Gui::getWidgetAt(int x, int y) - { - // If the widget's parent has no child then we have found the widget.. - Widget* parent = mTop; - Widget* child = mTop; - - while (child) - { - Widget *const swap = child; - int parentX, parentY; - parent->getAbsolutePosition(parentX, parentY); - child = parent->getWidgetAt(x - parentX, y - parentY); - parent = swap; - } - - return parent; - } - - Widget* Gui::getMouseEventSource(int x, int y) - { - Widget *const widget = getWidgetAt(x, y); - - // +++ possible nullpointer - if (mFocusHandler->getModalMouseInputFocused() - && !widget->isModalMouseInputFocused()) - { - return mFocusHandler->getModalMouseInputFocused(); - } - - return widget; - } - - Widget* Gui::getKeyEventSource() - { - Widget* widget = mFocusHandler->getFocused(); - - // +++ possible nullpointer - while (widget->_getInternalFocusHandler() - && widget->_getInternalFocusHandler()->getFocused()) - { - widget = widget->_getInternalFocusHandler()->getFocused(); - } - - return widget; - } - - void Gui::distributeMouseEvent(Widget* source, - int type, - int button, - int x, - int y, - bool force, - bool toSourceOnly) - { - Widget* parent = source; - Widget* widget = source; - - if (mFocusHandler->getModalFocused() - && !widget->isModalFocused() - && !force) - { - return; - } - - if (mFocusHandler->getModalMouseInputFocused() - && !widget->isModalMouseInputFocused() - && !force) - { - return; - } - - MouseEvent mouseEvent(source, - mShiftPressed, - mControlPressed, - mAltPressed, - mMetaPressed, - type, - button, - x, - y, - mClickCount); - - while (parent) - { - // If the widget has been removed due to input - // cancel the distribution. - if (!Widget::widgetExists(widget)) - break; - - parent = widget->getParent(); - - if (widget->isEnabled() || force) - { - int widgetX, widgetY; - widget->getAbsolutePosition(widgetX, widgetY); - - mouseEvent.mX = x - widgetX; - mouseEvent.mY = y - widgetY; - - std::list<MouseListener*> mouseListeners - = widget->_getMouseListeners(); - - // Send the event to all mouse listeners of the widget. - for (std::list<MouseListener*>::const_iterator - it = mouseListeners.begin(); - it != mouseListeners.end(); - ++it) - { - switch (mouseEvent.getType()) - { - case MouseEvent::ENTERED: - (*it)->mouseEntered(mouseEvent); - break; - case MouseEvent::EXITED: - (*it)->mouseExited(mouseEvent); - break; - case MouseEvent::MOVED: - (*it)->mouseMoved(mouseEvent); - break; - case MouseEvent::PRESSED: - (*it)->mousePressed(mouseEvent); - break; - case MouseEvent::RELEASED: - (*it)->mouseReleased(mouseEvent); - break; - case MouseEvent::WHEEL_MOVED_UP: - (*it)->mouseWheelMovedUp(mouseEvent); - break; - case MouseEvent::WHEEL_MOVED_DOWN: - (*it)->mouseWheelMovedDown(mouseEvent); - break; - case MouseEvent::DRAGGED: - (*it)->mouseDragged(mouseEvent); - break; - case MouseEvent::CLICKED: - (*it)->mouseClicked(mouseEvent); - break; - default: - break; - } - } - - if (toSourceOnly) - break; - } - - const Widget *const swap = widget; - widget = parent; - parent = swap->getParent(); - - // If a non modal focused widget has been reach - // and we have modal focus cancel the distribution. - if (mFocusHandler->getModalFocused() - && !widget->isModalFocused()) - { - break; - } - - // If a non modal mouse input focused widget has been reach - // and we have modal mouse input focus cancel the distribution. - if (mFocusHandler->getModalMouseInputFocused() - && !widget->isModalMouseInputFocused()) - { - break; - } - } - } - - void Gui::distributeKeyEvent(KeyEvent& keyEvent) - { - Widget* parent = keyEvent.getSource(); - Widget* widget = keyEvent.getSource(); - - if (mFocusHandler->getModalFocused() - && !widget->isModalFocused()) - { - return; - } - - if (mFocusHandler->getModalMouseInputFocused() - && !widget->isModalMouseInputFocused()) - { - return; - } - - while (parent) - { - // If the widget has been removed due to input - // cancel the distribution. - if (!Widget::widgetExists(widget)) - break; - - parent = widget->getParent(); - - if (widget->isEnabled()) - { - std::list<KeyListener*> keyListeners - = widget->_getKeyListeners(); - - // Send the event to all key listeners of the source widget. - for (std::list<KeyListener*>::const_iterator - it = keyListeners.begin(); - it != keyListeners.end(); - ++it) - { - switch (keyEvent.getType()) - { - case KeyEvent::PRESSED: - (*it)->keyPressed(keyEvent); - break; - case KeyEvent::RELEASED: - (*it)->keyReleased(keyEvent); - break; - default: - break; - } - } - } - - const Widget *const swap = widget; - widget = parent; - parent = swap->getParent(); - - // If a non modal focused widget has been reach - // and we have modal focus cancel the distribution. - if (mFocusHandler->getModalFocused() - && !widget->isModalFocused()) - { - break; - } - } - } - - void Gui::distributeKeyEventToGlobalKeyListeners(KeyEvent& keyEvent) - { - for (KeyListenerListIterator it = mKeyListeners.begin(); - it != mKeyListeners.end(); ++ it) - { - switch (keyEvent.getType()) - { - case KeyEvent::PRESSED: - (*it)->keyPressed(keyEvent); - break; - case KeyEvent::RELEASED: - (*it)->keyReleased(keyEvent); - break; - default: - break; - } - - if (keyEvent.isConsumed()) - break; - } - } - - void Gui::handleModalMouseInputFocus() - { - BLOCK_START("Gui::handleModalMouseInputFocus") - // Check if modal mouse input focus has been gained by a widget. - if ((mFocusHandler->getLastWidgetWithModalMouseInputFocus() - != mFocusHandler->getModalMouseInputFocused()) - && (!mFocusHandler->getLastWidgetWithModalMouseInputFocus())) - { - handleModalFocusGained(); - mFocusHandler->setLastWidgetWithModalMouseInputFocus( - mFocusHandler->getModalMouseInputFocused()); - } - // Check if modal mouse input focus has been released. - else if ((mFocusHandler->getLastWidgetWithModalMouseInputFocus() - != mFocusHandler->getModalMouseInputFocused()) - && (mFocusHandler->getLastWidgetWithModalMouseInputFocus())) - { - handleModalFocusReleased(); - mFocusHandler->setLastWidgetWithModalMouseInputFocus(nullptr); - } - BLOCK_END("Gui::handleModalMouseInputFocus") - } - - void Gui::handleModalFocus() - { - BLOCK_START("Gui::handleModalFocus") - // Check if modal focus has been gained by a widget. - if ((mFocusHandler->getLastWidgetWithModalFocus() - != mFocusHandler->getModalFocused()) - && (!mFocusHandler->getLastWidgetWithModalFocus())) - { - handleModalFocusGained(); - mFocusHandler->setLastWidgetWithModalFocus( - mFocusHandler->getModalFocused()); - } - // Check if modal focus has been released. - else if ((mFocusHandler->getLastWidgetWithModalFocus() - != mFocusHandler->getModalFocused()) - && (mFocusHandler->getLastWidgetWithModalFocus())) - { - handleModalFocusReleased(); - mFocusHandler->setLastWidgetWithModalFocus(nullptr); - } - BLOCK_END("Gui::handleModalFocus") - } - - void Gui::handleModalFocusGained() - { - // Distribute an event to all widgets in the "widget with mouse" queue. - while (!mWidgetWithMouseQueue.empty()) - { - Widget *const widget = mWidgetWithMouseQueue.front(); - - if (Widget::widgetExists(widget)) - { - distributeMouseEvent(widget, - MouseEvent::EXITED, - mLastMousePressButton, - mLastMouseX, - mLastMouseY, - true, - true); - } - - mWidgetWithMouseQueue.pop_front(); - } - - mFocusHandler->setLastWidgetWithModalMouseInputFocus( - mFocusHandler->getModalMouseInputFocused()); - } - - void Gui::handleModalFocusReleased() - { - // Check all widgets below the mouse to see if they are - // present in the "widget with mouse" queue. If a widget - // is not then it should be added and an entered event should - // be sent to it. - Widget* widget = getMouseEventSource(mLastMouseX, mLastMouseY); - Widget* parent = widget; - - while (parent) - { - parent = widget->getParent(); - - // Check if the widget is present in the "widget with mouse" queue. - bool widgetIsPresentInQueue = false; - FOR_EACH (std::deque<Widget*>::const_iterator, - iter, mWidgetWithMouseQueue) - { - if (*iter == widget) - { - widgetIsPresentInQueue = true; - break; - } - } - - // Widget is not present, send an entered event and add - // it to the "widget with mouse" queue. - if (!widgetIsPresentInQueue && Widget::widgetExists(widget)) - { - distributeMouseEvent(widget, - MouseEvent::ENTERED, - mLastMousePressButton, - mLastMouseX, - mLastMouseY, - false, - true); - mWidgetWithMouseQueue.push_front(widget); - } - - const Widget *const swap = widget; - widget = parent; - parent = swap->getParent(); - } - } -} // namespace gcn diff --git a/src/gui/base/gui.hpp b/src/gui/base/gui.hpp deleted file mode 100644 index 4ea923400..000000000 --- a/src/gui/base/gui.hpp +++ /dev/null @@ -1,515 +0,0 @@ -/* - * The ManaPlus Client - * Copyright (C) 2011-2014 The ManaPlus Developers - * - * This file is part of The ManaPlus Client. - * - * 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 - * 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, see <http://www.gnu.org/licenses/>. - */ - -/* _______ __ __ __ ______ __ __ _______ __ __ - * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ - * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / - * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / - * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / - * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / - * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ - * - * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson - * - * - * Per Larsson a.k.a finalman - * Olof Naessén a.k.a jansem/yakslem - * - * Visit: http://guichan.sourceforge.net - * - * License: (BSD) - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name of Guichan nor the names of its contributors may - * be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef GCN_GUI_HPP -#define GCN_GUI_HPP - -#include <list> -#include <deque> - -#include "localconsts.h" - -class Event; -class FocusHandler; -class Graphics; -class KeyEvent; -class KeyListener; -class MouseInput; -class SDLInput; -class Widget; - -namespace gcn -{ - // The following comment will appear in the doxygen main page. - /** - * @mainpage - * @section Introduction - * This documentation is mostly intended as a reference to the API. - * If you want to get started with Guichan, we suggest you check out - * the programs in the examples directory of the Guichan release. - * @n - * @n - * This documentation is, and will always be, work in progress. - * If you find any errors, typos or inconsistencies, or if you feel - * something needs to be explained in more detail - don't hesitate to - * tell us. - */ - - /** - * Contains a Guichan GUI. This is the core class of Guichan to which - * implementations of back ends are passed, to make Guichan work with - * a specific library, and to where the top widget (root widget of GUI) - * is added. If you want to be able to have more then one widget in your - * GUI, the top widget should be a container. - * - * A Gui object cannot work properly without passing back end - * implementations to it. A Gui object must have an implementation of a - * Graphics and an implementation of Input. - * - * NOTE: A complete GUI also must have the ability to load images. - * Images are loaded with the Image class, so to make Guichan - * able to load images an implementation of ImageLoader must be - * passed to Image. - * - * @see Graphics, Input, Image - */ - class Gui - { - public: - /** - * Constructor. - */ - Gui(); - - A_DELETE_COPY(Gui) - - /** - * Destructor. - */ - virtual ~Gui(); - - /** - * Sets the top widget. The top widget is the root widget - * of the GUI. If you want a GUI to be able to contain more - * than one widget the top widget should be a container. - * - * @param top The top widget. - * @see Container - * @since 0.1.0 - */ - virtual void setTop(Widget* top); - - /** - * Gets the top widget. The top widget is the root widget - * of the GUI. - * - * @return The top widget. NULL if no top widget has been set. - * @since 0.1.0 - */ - virtual Widget* getTop() const A_WARN_UNUSED; - - /** - * Sets the graphics object to use for drawing. - * - * @param graphics The graphics object to use for drawing. - * @see getGraphics, AllegroGraphics, HGEGraphics, - * OpenLayerGraphics, OpenGLGraphics, SDLGraphics - * @since 0.1.0 - */ - virtual void setGraphics(Graphics* graphics); - - /** - * Gets the graphics object used for drawing. - * - * @return The graphics object used for drawing. NULL if no - * graphics object has been set. - * @see setGraphics, AllegroGraphics, HGEGraphics, - * OpenLayerGraphics, OpenGLGraphics, SDLGraphics - * @since 0.1.0 - */ - virtual Graphics* getGraphics() const A_WARN_UNUSED; - - /** - * Sets the input object to use for input handling. - * - * @param input The input object to use for input handling. - * @see getInput, AllegroInput, HGEInput, OpenLayerInput, - * SDLInput - * @since 0.1.0 - */ - virtual void setInput(SDLInput* input); - - /** - * Gets the input object being used for input handling. - * - * @return The input object used for handling input. NULL if no - * input object has been set. - * @see setInput, AllegroInput, HGEInput, OpenLayerInput, - * SDLInput - * @since 0.1.0 - */ - virtual SDLInput* getInput() const A_WARN_UNUSED; - - /** - * Performs logic of the GUI. By calling this function all logic - * functions down in the GUI heirarchy will be called. When logic - * is called for Gui, user input will be handled. - * - * @see Widget::logic - * @since 0.1.0 - */ - virtual void logic(); - - /** - * Draws the GUI. By calling this funcion all draw functions - * down in the GUI hierarchy will be called. When draw is called - * the used Graphics object will be initialised and drawing of - * the top widget will commence. - * - * @see Widget::draw - * @since 0.1.0 - */ - virtual void draw(); - - /** - * Focuses none of the widgets in the Gui. - * - * @since 0.1.0 - */ - virtual void focusNone(); - - /** - * Sets tabbing enabled, or not. Tabbing is the usage of - * changing focus by utilising the tab key. - * - * @param tabbing True if tabbing should be enabled, false - * otherwise. - * @see isTabbingEnabled - * @since 0.1.0 - */ - virtual void setTabbingEnabled(bool tabbing); - - /** - * Checks if tabbing is enabled. - * - * @return True if tabbing is enabled, false otherwise. - * @see setTabbingEnabled - * @since 0.1.0 - */ - virtual bool isTabbingEnabled(); - - /** - * Adds a global key listener to the Gui. A global key listener - * will receive all key events generated from the GUI and global - * key listeners will receive the events before key listeners - * of widgets. - * - * @param keyListener The key listener to add. - * @see removeGlobalKeyListener - * @since 0.5.0 - */ - virtual void addGlobalKeyListener(KeyListener* keyListener); - - /** - * Removes global key listener from the Gui. - * - * @param keyListener The key listener to remove. - * @throws Exception if the key listener hasn't been added. - * @see addGlobalKeyListener - * @since 0.5.0 - */ - virtual void removeGlobalKeyListener(KeyListener* keyListener); - - protected: - /** - * Handles all mouse input. - * - * @since 0.6.0 - */ - virtual void handleMouseInput(); - - /** - * Handles key input. - * - * @since 0.6.0 - */ - virtual void handleKeyInput(); - - /** - * Handles mouse moved input. - * - * @param mouseInput The mouse input to handle. - * @since 0.6.0 - */ - virtual void handleMouseMoved(const MouseInput& mouseInput); - - /** - * - * Handles mouse wheel moved down input. - * - * @param mouseInput The mouse input to handle. - * @since 0.6.0 - */ - virtual void handleMouseWheelMovedDown(const MouseInput& mouseInput); - - /** - * Handles mouse wheel moved up input. - * - * @param mouseInput The mouse input to handle. - * @since 0.6.0 - */ - virtual void handleMouseWheelMovedUp(const MouseInput& mouseInput); - - /** - * Handles modal focus. Modal focus needs to be checked at - * each logic iteration as it might be necessary to distribute - * mouse entered or mouse exited events. - * - * @since 0.8.0 - */ - virtual void handleModalFocus(); - - /** - * Handles modal mouse input focus. Modal mouse input focus needs - * to be checked at each logic iteration as it might be necessary to - * distribute mouse entered or mouse exited events. - * - * @since 0.8.0 - */ - virtual void handleModalMouseInputFocus(); - - /** - * Handles modal focus gained. If modal focus has been gained it might - * be necessary to distribute mouse entered or mouse exited events. - * - * @since 0.8.0 - */ - virtual void handleModalFocusGained(); - - /** - * Handles modal mouse input focus gained. If modal focus has been - * gained it might be necessary to distribute mouse entered or mouse - * exited events. - * - * @since 0.8.0 - */ - virtual void handleModalFocusReleased(); - - /** - * Distributes a mouse event. - * - * @param type The type of the event to distribute, - * @param button The button of the event (if any used) to distribute. - * @param x The x coordinate of the event. - * @param y The y coordinate of the event. - * @param fource indicates whether the distribution should be forced or not. - * A forced distribution distributes the event even if a widget - * is not enabled, not visible, another widget has modal - * focus or another widget has modal mouse input focus. - * Default value is false. - * @param toSourceOnly indicates whether the distribution should be to the - * source widget only or to it's parent's mouse listeners - * as well. - * - * @since 0.6.0 - */ - virtual void distributeMouseEvent(Widget* source, - int type, - int button, - int x, - int y, - bool force = false, - bool toSourceOnly = false); - - /** - * Distributes a key event. - * - * @param keyEvent The key event to distribute. - - * @since 0.6.0 - */ - virtual void distributeKeyEvent(KeyEvent& keyEvent); - - /** - * Distributes a key event to the global key listeners. - * - * @param keyEvent The key event to distribute. - * - * @since 0.6.0 - */ - virtual void distributeKeyEventToGlobalKeyListeners(KeyEvent& - keyEvent); - - /** - * Gets the widget at a certain position. - * - * @return The widget at a certain position. - * @since 0.6.0 - */ - virtual Widget* getWidgetAt(int x, int y) A_WARN_UNUSED; - - /** - * Gets the source of the mouse event. - * - * @return The source widget of the mouse event. - * @since 0.6.0 - */ - virtual Widget* getMouseEventSource(int x, int y) A_WARN_UNUSED; - - /** - * Gets the source of the key event. - * - * @return The source widget of the key event. - * @since 0.6.0 - */ - virtual Widget* getKeyEventSource() A_WARN_UNUSED; - - /** - * Holds the top widget. - */ - Widget* mTop; - - /** - * Holds the graphics implementation used. - */ - Graphics* mGraphics; - - /** - * Holds the input implementation used. - */ - SDLInput* mInput; - - /** - * Holds the focus handler for the Gui. - */ - FocusHandler* mFocusHandler; - - /** - * True if tabbing is enabled, false otherwise. - */ - bool mTabbing; - - /** - * Typedef. - */ - typedef std::list<KeyListener*> KeyListenerList; - - /** - * Typedef. - */ - typedef KeyListenerList::iterator KeyListenerListIterator; - - /** - * Holds the global key listeners of the Gui. - */ - KeyListenerList mKeyListeners; - - /** - * True if shift is pressed, false otherwise. - */ - bool mShiftPressed; - - /** - * True if meta is pressed, false otherwise. - */ - bool mMetaPressed; - - /** - * True if control is pressed, false otherwise. - */ - bool mControlPressed; - - /** - * True if alt is pressed, false otherwise. - */ - bool mAltPressed; - - /** - * Holds the last mouse button pressed. - */ - unsigned int mLastMousePressButton; - - /** - * Holds the last mouse press time stamp. - */ - int mLastMousePressTimeStamp; - - /** - * Holds the last mouse x coordinate. - */ - int mLastMouseX; - - /** - * Holds the last mouse y coordinate. - */ - int mLastMouseY; - - /** - * Holds the current click count. Used to keep track - * of clicks for a the last pressed button. - */ - int mClickCount; - - /** - * Holds the last button used when a drag of a widget - * was initiated. Used to be able to release a drag - * when the same button is released. - */ - int mLastMouseDragButton; - - /** - * Holds a stack with all the widgets with the mouse. - * Used to properly distribute mouse events. - */ - std::deque<Widget*> mWidgetWithMouseQueue; - }; -} // namespace gcn - -#endif // end GCN_GUI_HPP - -/* yakslem - "Women, it's a constant struggle." - * finalman - "Yes, but sometimes they succeed with their guesses." - * yaklsem - "...eh...I was talking about love." - * finalman - "Oh...ok..." - * An awkward silence followed. - */ diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 5914e43d9..31722797f 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -20,6 +20,49 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +/* _______ __ __ __ ______ __ __ _______ __ __ + * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ + * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / + * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / + * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / + * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / + * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ + * + * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson + * + * + * Per Larsson a.k.a finalman + * Olof Naessén a.k.a jansem/yakslem + * + * Visit: http://guichan.sourceforge.net + * + * License: (BSD) + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of Guichan nor the names of its contributors may + * be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include "gui/gui.h" #include "gui/focushandler.h" @@ -41,6 +84,8 @@ #include "events/keyevent.h" #include "listeners/focuslistener.h" +#include "listeners/keylistener.h" +#include "listeners/mouselistener.h" #include "input/keydata.h" #include "input/keyinput.h" @@ -91,7 +136,23 @@ class GuiConfigListener final : public ConfigListener }; Gui::Gui() : - gcn::Gui(), + mTop(nullptr), + mGraphics(nullptr), + mInput(nullptr), + mFocusHandler(new FocusHandler), + mTabbing(true), + mKeyListeners(), + mShiftPressed(false), + mMetaPressed(false), + mControlPressed(false), + mAltPressed(false), + mLastMousePressButton(0), + mLastMousePressTimeStamp(0), + mLastMouseX(0), + mLastMouseY(0), + mClickCount(1), + mLastMouseDragButton(0), + mWidgetWithMouseQueue(), mConfigListener(new GuiConfigListener(this)), mGuiFont(), mInfoParticleFont(), @@ -262,6 +323,12 @@ Gui::~Gui() guiInput = nullptr; Theme::deleteInstance(); + + if (Widget::widgetExists(mTop)) + setTop(nullptr); + + delete mFocusHandler; + mFocusHandler = nullptr; } void Gui::logic() @@ -527,7 +594,171 @@ void Gui::setUseCustomCursor(const bool customCursor) void Gui::handleMouseMoved(const MouseInput &mouseInput) { - gcn::Gui::handleMouseMoved(mouseInput); + // Check if the mouse leaves the application window. + if (!mWidgetWithMouseQueue.empty() && (mouseInput.getX() < 0 + || mouseInput.getY() < 0 || !mTop->getDimension().isPointInRect( + mouseInput.getX(), mouseInput.getY()))) + { + // Distribute an event to all widgets in the + // "widget with mouse" queue. + while (!mWidgetWithMouseQueue.empty()) + { + Widget *const widget = mWidgetWithMouseQueue.front(); + + if (Widget::widgetExists(widget)) + { + distributeMouseEvent(widget, + MouseEvent::EXITED, + mouseInput.getButton(), + mouseInput.getX(), + mouseInput.getY(), + true, + true); + } + + mWidgetWithMouseQueue.pop_front(); + } + + mMouseInactivityTimer = 0; + return; + } + + // Check if there is a need to send mouse exited events by + // traversing the "widget with mouse" queue. + bool widgetWithMouseQueueCheckDone = mWidgetWithMouseQueue.empty(); + while (!widgetWithMouseQueueCheckDone) + { + unsigned int iterations = 0; + for (std::deque<Widget*>::iterator + iter = mWidgetWithMouseQueue.begin(); + iter != mWidgetWithMouseQueue.end(); + ++ iter) + { + Widget *const widget = *iter; + + // If a widget in the "widget with mouse queue" doesn't + // exists anymore it should be removed from the queue. + if (!Widget::widgetExists(widget)) + { + mWidgetWithMouseQueue.erase(iter); + break; + } + else + { + int x, y; + widget->getAbsolutePosition(x, y); + + if (x > mouseInput.getX() + || y > mouseInput.getY() + || x + widget->getWidth() <= mouseInput.getX() + || y + widget->getHeight() <= mouseInput.getY() + || !widget->isVisible()) + { + distributeMouseEvent(widget, + MouseEvent::EXITED, + mouseInput.getButton(), + mouseInput.getX(), + mouseInput.getY(), + true, + true); + mClickCount = 1; + mLastMousePressTimeStamp = 0; + mWidgetWithMouseQueue.erase(iter); + break; + } + } + + iterations++; + } + + widgetWithMouseQueueCheckDone = + (iterations == mWidgetWithMouseQueue.size()); + } + + // Check all widgets below the mouse to see if they are + // present in the "widget with mouse" queue. If a widget + // is not then it should be added and an entered event should + // be sent to it. + Widget* parent = getMouseEventSource( + mouseInput.getX(), mouseInput.getY()); + Widget* widget = parent; + + // If a widget has modal mouse input focus then it will + // always be returned from getMouseEventSource, but we only wan't to + // send mouse entered events if the mouse has actually entered the + // widget with modal mouse input focus, hence we need to check if + // that's the case. If it's not we should simply ignore to send any + // mouse entered events. + if (mFocusHandler->getModalMouseInputFocused() + && widget == mFocusHandler->getModalMouseInputFocused() + && Widget::widgetExists(widget)) + { + int x, y; + widget->getAbsolutePosition(x, y); + + if (x > mouseInput.getX() || y > mouseInput.getY() + || x + widget->getWidth() <= mouseInput.getX() + || y + widget->getHeight() <= mouseInput.getY()) + { + parent = nullptr; + } + } + + while (parent) + { + parent = widget->getParent(); + + // Check if the widget is present in the "widget with mouse" queue. + bool widgetIsPresentInQueue = false; + FOR_EACH (std::deque<Widget*>::const_iterator, + iter, mWidgetWithMouseQueue) + { + if (*iter == widget) + { + widgetIsPresentInQueue = true; + break; + } + } + + // Widget is not present, send an entered event and add + // it to the "widget with mouse" queue. + if (!widgetIsPresentInQueue + && Widget::widgetExists(widget)) + { + distributeMouseEvent(widget, + MouseEvent::ENTERED, + mouseInput.getButton(), + mouseInput.getX(), + mouseInput.getY(), + true, + true); + mWidgetWithMouseQueue.push_front(widget); + } + + const Widget *const swap = widget; + widget = parent; + parent = swap->getParent(); + } + + if (mFocusHandler->getDraggedWidget()) + { + distributeMouseEvent(mFocusHandler->getDraggedWidget(), + MouseEvent::DRAGGED, + mLastMouseDragButton, + mouseInput.getX(), + mouseInput.getY()); + } + else + { + Widget *const sourceWidget = getMouseEventSource( + mouseInput.getX(), mouseInput.getY()); + + distributeMouseEvent(sourceWidget, + MouseEvent::MOVED, + mouseInput.getButton(), + mouseInput.getX(), + mouseInput.getY()); + } mMouseInactivityTimer = 0; } @@ -893,3 +1124,329 @@ uint32_t Gui::getMouseState(int *const x, int *const y) (*y) /= scale; return res; } + +void Gui::setTop(Widget *const top) +{ + if (mTop) + mTop->_setFocusHandler(nullptr); + if (top) + top->_setFocusHandler(mFocusHandler); + + mTop = top; +} + +void Gui::setGraphics(Graphics *const graphics) +{ + mGraphics = graphics; +} + +Graphics* Gui::getGraphics() const +{ + return mGraphics; +} + +void Gui::setInput(SDLInput *const input) +{ + mInput = input; +} + +SDLInput* Gui::getInput() const +{ + return mInput; +} + +void Gui::addGlobalKeyListener(KeyListener *const keyListener) +{ + mKeyListeners.push_back(keyListener); +} + +void Gui::removeGlobalKeyListener(KeyListener *const keyListener) +{ + mKeyListeners.remove(keyListener); +} + +void Gui::handleMouseWheelMovedDown(const MouseInput& mouseInput) +{ + Widget* sourceWidget = getMouseEventSource( + mouseInput.getX(), mouseInput.getY()); + + if (mFocusHandler->getDraggedWidget()) + sourceWidget = mFocusHandler->getDraggedWidget(); + + int sourceWidgetX = 0; + int sourceWidgetY = 0; + sourceWidget->getAbsolutePosition(sourceWidgetX, sourceWidgetY); + + distributeMouseEvent(sourceWidget, + MouseEvent::WHEEL_MOVED_DOWN, + mouseInput.getButton(), + mouseInput.getX(), + mouseInput.getY()); +} + +void Gui::handleMouseWheelMovedUp(const MouseInput& mouseInput) +{ + Widget* sourceWidget = getMouseEventSource( + mouseInput.getX(), mouseInput.getY()); + + if (mFocusHandler->getDraggedWidget()) + sourceWidget = mFocusHandler->getDraggedWidget(); + + int sourceWidgetX, sourceWidgetY; + sourceWidget->getAbsolutePosition(sourceWidgetX, sourceWidgetY); + + distributeMouseEvent(sourceWidget, + MouseEvent::WHEEL_MOVED_UP, + mouseInput.getButton(), + mouseInput.getX(), + mouseInput.getY()); +} + +Widget* Gui::getWidgetAt(int x, int y) +{ + // If the widget's parent has no child then we have found the widget.. + Widget* parent = mTop; + Widget* child = mTop; + + while (child) + { + Widget *const swap = child; + int parentX, parentY; + parent->getAbsolutePosition(parentX, parentY); + child = parent->getWidgetAt(x - parentX, y - parentY); + parent = swap; + } + + return parent; +} + +Widget* Gui::getMouseEventSource(int x, int y) +{ + Widget *const widget = getWidgetAt(x, y); + if (!widget) + return nullptr; + + if (mFocusHandler && mFocusHandler->getModalMouseInputFocused() + && !widget->isModalMouseInputFocused()) + { + return mFocusHandler->getModalMouseInputFocused(); + } + + return widget; +} + +Widget* Gui::getKeyEventSource() +{ + Widget* widget = mFocusHandler->getFocused(); + + while (widget && widget->_getInternalFocusHandler() + && widget->_getInternalFocusHandler()->getFocused()) + { + widget = widget->_getInternalFocusHandler()->getFocused(); + } + + return widget; +} + +void Gui::distributeKeyEvent(KeyEvent& keyEvent) +{ + Widget* parent = keyEvent.getSource(); + Widget* widget = keyEvent.getSource(); + + if (mFocusHandler->getModalFocused() + && !widget->isModalFocused()) + { + return; + } + + if (mFocusHandler->getModalMouseInputFocused() + && !widget->isModalMouseInputFocused()) + { + return; + } + + while (parent) + { + // If the widget has been removed due to input + // cancel the distribution. + if (!Widget::widgetExists(widget)) + break; + + parent = widget->getParent(); + + if (widget->isEnabled()) + { + std::list<KeyListener*> keyListeners + = widget->_getKeyListeners(); + + // Send the event to all key listeners of the source widget. + for (std::list<KeyListener*>::const_iterator + it = keyListeners.begin(); + it != keyListeners.end(); + ++ it) + { + switch (keyEvent.getType()) + { + case KeyEvent::PRESSED: + (*it)->keyPressed(keyEvent); + break; + case KeyEvent::RELEASED: + (*it)->keyReleased(keyEvent); + break; + default: + break; + } + } + } + + const Widget *const swap = widget; + widget = parent; + parent = swap->getParent(); + + // If a non modal focused widget has been reach + // and we have modal focus cancel the distribution. + if (mFocusHandler->getModalFocused() + && !widget->isModalFocused()) + { + break; + } + } +} + +void Gui::distributeKeyEventToGlobalKeyListeners(KeyEvent& keyEvent) +{ + for (KeyListenerListIterator it = mKeyListeners.begin(); + it != mKeyListeners.end(); ++ it) + { + switch (keyEvent.getType()) + { + case KeyEvent::PRESSED: + (*it)->keyPressed(keyEvent); + break; + case KeyEvent::RELEASED: + (*it)->keyReleased(keyEvent); + break; + default: + break; + } + + if (keyEvent.isConsumed()) + break; + } +} + +void Gui::handleModalMouseInputFocus() +{ + BLOCK_START("Gui::handleModalMouseInputFocus") + // Check if modal mouse input focus has been gained by a widget. + if ((mFocusHandler->getLastWidgetWithModalMouseInputFocus() + != mFocusHandler->getModalMouseInputFocused()) + && (!mFocusHandler->getLastWidgetWithModalMouseInputFocus())) + { + handleModalFocusGained(); + mFocusHandler->setLastWidgetWithModalMouseInputFocus( + mFocusHandler->getModalMouseInputFocused()); + } + // Check if modal mouse input focus has been released. + else if ((mFocusHandler->getLastWidgetWithModalMouseInputFocus() + != mFocusHandler->getModalMouseInputFocused()) + && (mFocusHandler->getLastWidgetWithModalMouseInputFocus())) + { + handleModalFocusReleased(); + mFocusHandler->setLastWidgetWithModalMouseInputFocus(nullptr); + } + BLOCK_END("Gui::handleModalMouseInputFocus") +} + +void Gui::handleModalFocus() +{ + BLOCK_START("Gui::handleModalFocus") + // Check if modal focus has been gained by a widget. + if ((mFocusHandler->getLastWidgetWithModalFocus() + != mFocusHandler->getModalFocused()) + && (!mFocusHandler->getLastWidgetWithModalFocus())) + { + handleModalFocusGained(); + mFocusHandler->setLastWidgetWithModalFocus( + mFocusHandler->getModalFocused()); + } + // Check if modal focus has been released. + else if ((mFocusHandler->getLastWidgetWithModalFocus() + != mFocusHandler->getModalFocused()) + && (mFocusHandler->getLastWidgetWithModalFocus())) + { + handleModalFocusReleased(); + mFocusHandler->setLastWidgetWithModalFocus(nullptr); + } + BLOCK_END("Gui::handleModalFocus") +} + +void Gui::handleModalFocusGained() +{ + // Distribute an event to all widgets in the "widget with mouse" queue. + while (!mWidgetWithMouseQueue.empty()) + { + Widget *const widget = mWidgetWithMouseQueue.front(); + + if (Widget::widgetExists(widget)) + { + distributeMouseEvent(widget, + MouseEvent::EXITED, + mLastMousePressButton, + mLastMouseX, + mLastMouseY, + true, + true); + } + + mWidgetWithMouseQueue.pop_front(); + } + + mFocusHandler->setLastWidgetWithModalMouseInputFocus( + mFocusHandler->getModalMouseInputFocused()); +} + +void Gui::handleModalFocusReleased() +{ + // Check all widgets below the mouse to see if they are + // present in the "widget with mouse" queue. If a widget + // is not then it should be added and an entered event should + // be sent to it. + Widget* widget = getMouseEventSource(mLastMouseX, mLastMouseY); + Widget* parent = widget; + + while (parent) + { + parent = widget->getParent(); + + // Check if the widget is present in the "widget with mouse" queue. + bool widgetIsPresentInQueue = false; + FOR_EACH (std::deque<Widget*>::const_iterator, + iter, mWidgetWithMouseQueue) + { + if (*iter == widget) + { + widgetIsPresentInQueue = true; + break; + } + } + + // Widget is not present, send an entered event and add + // it to the "widget with mouse" queue. + if (!widgetIsPresentInQueue && Widget::widgetExists(widget)) + { + distributeMouseEvent(widget, + MouseEvent::ENTERED, + mLastMousePressButton, + mLastMouseX, + mLastMouseY, + false, + true); + mWidgetWithMouseQueue.push_front(widget); + } + + const Widget *const swap = widget; + widget = parent; + parent = swap->getParent(); + } +} diff --git a/src/gui/gui.h b/src/gui/gui.h index 4038bbec4..ba1fa2e6b 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -20,23 +20,72 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +/* _______ __ __ __ ______ __ __ _______ __ __ + * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\ + * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / / + * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / / + * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / / + * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ / + * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/ + * + * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson + * + * + * Per Larsson a.k.a finalman + * Olof Naessén a.k.a jansem/yakslem + * + * Visit: http://guichan.sourceforge.net + * + * License: (BSD) + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of Guichan nor the names of its contributors may + * be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #ifndef GUI_GUI_H #define GUI_GUI_H #include "gui/color.h" -#include "gui/base/gui.hpp" +#include <deque> +#include <list> #include "localconsts.h" +class Event; +class FocusHandler; class FocusListener; class Graphics; class GuiConfigListener; class ImageSet; +class KeyEvent; +class KeyListener; class MouseEvent; class MouseInput; class Font; class SDLInput; +class Widget; class Window; /** @@ -52,7 +101,7 @@ class Window; * * \ingroup GUI */ -class Gui final : public gcn::Gui +class Gui final { public: /** @@ -168,6 +217,91 @@ class Gui final : public gcn::Gui static uint32_t getMouseState(int *const x, int *const y); + /** + * Sets the top widget. The top widget is the root widget + * of the GUI. If you want a GUI to be able to contain more + * than one widget the top widget should be a container. + * + * @param top The top widget. + * @see Container + * @since 0.1.0 + */ + void setTop(Widget *const top); + + /** + * Gets the top widget. The top widget is the root widget + * of the GUI. + * + * @return The top widget. NULL if no top widget has been set. + * @since 0.1.0 + */ + Widget* getTop() const A_WARN_UNUSED + { return mTop; } + + /** + * Sets the graphics object to use for drawing. + * + * @param graphics The graphics object to use for drawing. + * @see getGraphics, AllegroGraphics, HGEGraphics, + * OpenLayerGraphics, OpenGLGraphics, SDLGraphics + * @since 0.1.0 + */ + void setGraphics(Graphics *const graphics); + + /** + * Gets the graphics object used for drawing. + * + * @return The graphics object used for drawing. NULL if no + * graphics object has been set. + * @see setGraphics, AllegroGraphics, HGEGraphics, + * OpenLayerGraphics, OpenGLGraphics, SDLGraphics + * @since 0.1.0 + */ + Graphics* getGraphics() const A_WARN_UNUSED; + + /** + * Sets the input object to use for input handling. + * + * @param input The input object to use for input handling. + * @see getInput, AllegroInput, HGEInput, OpenLayerInput, + * SDLInput + * @since 0.1.0 + */ + void setInput(SDLInput *const input); + + /** + * Gets the input object being used for input handling. + * + * @return The input object used for handling input. NULL if no + * input object has been set. + * @see setInput, AllegroInput, HGEInput, OpenLayerInput, + * SDLInput + * @since 0.1.0 + */ + SDLInput* getInput() const A_WARN_UNUSED; + + /** + * Adds a global key listener to the Gui. A global key listener + * will receive all key events generated from the GUI and global + * key listeners will receive the events before key listeners + * of widgets. + * + * @param keyListener The key listener to add. + * @see removeGlobalKeyListener + * @since 0.5.0 + */ + void addGlobalKeyListener(KeyListener *const keyListener); + + /** + * Removes global key listener from the Gui. + * + * @param keyListener The key listener to remove. + * @throws Exception if the key listener hasn't been added. + * @see addGlobalKeyListener + * @since 0.5.0 + */ + void removeGlobalKeyListener(KeyListener *const keyListener); + protected: void handleMouseMoved(const MouseInput &mouseInput); @@ -181,7 +315,200 @@ class Gui final : public gcn::Gui int x, int y, bool force = false, bool toSourceOnly = false); + /** + * + * Handles mouse wheel moved down input. + * + * @param mouseInput The mouse input to handle. + * @since 0.6.0 + */ + void handleMouseWheelMovedDown(const MouseInput& mouseInput); + + /** + * Handles mouse wheel moved up input. + * + * @param mouseInput The mouse input to handle. + * @since 0.6.0 + */ + void handleMouseWheelMovedUp(const MouseInput& mouseInput); + + /** + * Gets the widget at a certain position. + * + * @return The widget at a certain position. + * @since 0.6.0 + */ + Widget* getWidgetAt(int x, int y) A_WARN_UNUSED; + + /** + * Gets the source of the mouse event. + * + * @return The source widget of the mouse event. + * @since 0.6.0 + */ + Widget* getMouseEventSource(int x, int y) A_WARN_UNUSED; + + /** + * Gets the source of the key event. + * + * @return The source widget of the key event. + * @since 0.6.0 + */ + Widget* getKeyEventSource() A_WARN_UNUSED; + + /** + * Distributes a key event. + * + * @param keyEvent The key event to distribute. + + * @since 0.6.0 + */ + void distributeKeyEvent(KeyEvent& keyEvent); + + /** + * Distributes a key event to the global key listeners. + * + * @param keyEvent The key event to distribute. + * + * @since 0.6.0 + */ + void distributeKeyEventToGlobalKeyListeners(KeyEvent& keyEvent); + + /** + * Handles modal mouse input focus. Modal mouse input focus needs + * to be checked at each logic iteration as it might be necessary to + * distribute mouse entered or mouse exited events. + * + * @since 0.8.0 + */ + void handleModalMouseInputFocus(); + + /** + * Handles modal focus. Modal focus needs to be checked at + * each logic iteration as it might be necessary to distribute + * mouse entered or mouse exited events. + * + * @since 0.8.0 + */ + void handleModalFocus(); + + /** + * Handles modal focus gained. If modal focus has been gained it might + * be necessary to distribute mouse entered or mouse exited events. + * + * @since 0.8.0 + */ + void handleModalFocusGained(); + + /** + * Handles modal mouse input focus gained. If modal focus has been + * gained it might be necessary to distribute mouse entered or mouse + * exited events. + * + * @since 0.8.0 + */ + void handleModalFocusReleased(); + private: + /** + * Holds the top widget. + */ + Widget* mTop; + + /** + * Holds the graphics implementation used. + */ + Graphics* mGraphics; + + /** + * Holds the input implementation used. + */ + SDLInput* mInput; + + /** + * Holds the focus handler for the Gui. + */ + FocusHandler* mFocusHandler; + + /** + * True if tabbing is enabled, false otherwise. + */ + bool mTabbing; + + /** + * Typedef. + */ + typedef std::list<KeyListener*> KeyListenerList; + + /** + * Typedef. + */ + typedef KeyListenerList::iterator KeyListenerListIterator; + + /** + * Holds the global key listeners of the Gui. + */ + KeyListenerList mKeyListeners; + + /** + * True if shift is pressed, false otherwise. + */ + bool mShiftPressed; + + /** + * True if meta is pressed, false otherwise. + */ + bool mMetaPressed; + + /** + * True if control is pressed, false otherwise. + */ + bool mControlPressed; + + /** + * True if alt is pressed, false otherwise. + */ + bool mAltPressed; + + /** + * Holds the last mouse button pressed. + */ + unsigned int mLastMousePressButton; + + /** + * Holds the last mouse press time stamp. + */ + int mLastMousePressTimeStamp; + + /** + * Holds the last mouse x coordinate. + */ + int mLastMouseX; + + /** + * Holds the last mouse y coordinate. + */ + int mLastMouseY; + + /** + * Holds the current click count. Used to keep track + * of clicks for a the last pressed button. + */ + int mClickCount; + + /** + * Holds the last button used when a drag of a widget + * was initiated. Used to be able to release a drag + * when the same button is released. + */ + int mLastMouseDragButton; + + /** + * Holds a stack with all the widgets with the mouse. + * Used to properly distribute mouse events. + */ + std::deque<Widget*> mWidgetWithMouseQueue; + GuiConfigListener *mConfigListener; Font *mGuiFont; /**< The global GUI font */ Font *mInfoParticleFont; /**< Font for Info Particles */ |