diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-04-14 17:39:14 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-04-14 20:37:31 +0300 |
commit | 9fc07b15f1b051eecd662802162772804dc77b8e (patch) | |
tree | d100d50693fc19edb32d01b9189603bdd3254bd2 /src/gui/widgets | |
parent | 78ae06d2c7b565afe67c5032c872d68a74ea210a (diff) | |
download | mv-9fc07b15f1b051eecd662802162772804dc77b8e.tar.gz mv-9fc07b15f1b051eecd662802162772804dc77b8e.tar.bz2 mv-9fc07b15f1b051eecd662802162772804dc77b8e.tar.xz mv-9fc07b15f1b051eecd662802162772804dc77b8e.zip |
Rename class InventoryFilter to RadioGroup.
Add WidgetGroup and TabStrip classes for grouping controls.
Diffstat (limited to 'src/gui/widgets')
-rw-r--r-- | src/gui/widgets/button.cpp | 53 | ||||
-rw-r--r-- | src/gui/widgets/button.h | 10 | ||||
-rw-r--r-- | src/gui/widgets/radiogroup.cpp | 39 | ||||
-rw-r--r-- | src/gui/widgets/radiogroup.h | 36 | ||||
-rw-r--r-- | src/gui/widgets/tabstrip.cpp | 65 | ||||
-rw-r--r-- | src/gui/widgets/tabstrip.h (renamed from src/gui/widgets/inventoryfilter.h) | 22 | ||||
-rw-r--r-- | src/gui/widgets/widgetgroup.cpp (renamed from src/gui/widgets/inventoryfilter.cpp) | 55 | ||||
-rw-r--r-- | src/gui/widgets/widgetgroup.h | 59 |
8 files changed, 293 insertions, 46 deletions
diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index 9519c144f..88d9311b9 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -79,7 +79,9 @@ Button::Button() : mXOffset(0), mYOffset(0), mImages(nullptr), - mImageSet(nullptr) + mImageSet(nullptr), + mStick(false), + mPressed(false) { init(); adjustSize(); @@ -99,7 +101,9 @@ Button::Button(const std::string &caption, const std::string &actionEventId, mImages(nullptr), mImageSet(nullptr), mImageWidth(0), - mImageHeight(0) + mImageHeight(0), + mStick(false), + mPressed(false) { init(); adjustSize(); @@ -125,7 +129,9 @@ Button::Button(const std::string &caption, const std::string &imageName, mImages(nullptr), mImageSet(nullptr), mImageWidth(imageWidth), - mImageHeight(imageHeight) + mImageHeight(imageHeight), + mStick(false), + mPressed(false) { init(); loadImage(imageName); @@ -151,7 +157,9 @@ Button::Button(const std::string &imageName, int imageWidth, int imageHeight, mImages(nullptr), mImageSet(nullptr), mImageWidth(imageWidth), - mImageHeight(imageHeight) + mImageHeight(imageHeight), + mStick(false), + mPressed(false) { init(); loadImage(imageName); @@ -286,7 +294,7 @@ void Button::draw(gcn::Graphics *graphics) if (!isEnabled()) mode = BUTTON_DISABLED; - else if (isPressed()) + else if (isPressed2()) mode = BUTTON_PRESSED; else if (mHasMouse || isFocused()) mode = BUTTON_HIGHLIGHTED; @@ -399,18 +407,22 @@ void Button::draw(gcn::Graphics *graphics) void Button::mouseReleased(gcn::MouseEvent& mouseEvent) { - if (mouseEvent.getButton() == gcn::MouseEvent::LEFT - && mMousePressed && mHasMouse) - { - mMousePressed = false; - mClickCount = mouseEvent.getClickCount(); - distributeActionEvent(); - mouseEvent.consume(); - } - else if (mouseEvent.getButton() == gcn::MouseEvent::LEFT) + if (mouseEvent.getButton() == gcn::MouseEvent::LEFT) { - mMousePressed = false; - mClickCount = 0; + if (mStick) + mPressed = !mPressed; + + if (mMousePressed && mHasMouse) + { + mMousePressed = false; + mClickCount = mouseEvent.getClickCount(); + distributeActionEvent(); + } + else + { + mMousePressed = false; + mClickCount = 0; + } mouseEvent.consume(); } } @@ -446,7 +458,6 @@ void Button::adjustSize() void Button::setCaption(const std::string& caption) { mCaption = caption; -// adjustSize(); } void Button::keyPressed(gcn::KeyEvent& keyEvent) @@ -467,7 +478,15 @@ void Button::keyReleased(gcn::KeyEvent& keyEvent) if (key.getValue() == gcn::Key::SPACE && mKeyPressed) { mKeyPressed = false; + if (mStick) + mPressed = !mPressed; distributeActionEvent(); keyEvent.consume(); } } + + +bool Button::isPressed2() +{ + return (mPressed || isPressed()); +} diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h index 8e4cdd9e6..c2d8b0b52 100644 --- a/src/gui/widgets/button.h +++ b/src/gui/widgets/button.h @@ -103,6 +103,12 @@ class Button : public gcn::Button, public gcn::WidgetListener int getTag() const { return mTag; } + void setStick(bool b) + { mStick = b; } + + void setPressed(bool b) + { mPressed = b; } + void widgetResized(const gcn::Event &event); void widgetMoved(const gcn::Event &event); @@ -117,6 +123,8 @@ class Button : public gcn::Button, public gcn::WidgetListener void keyReleased(gcn::KeyEvent &keyEvent); + bool isPressed2(); + private: void init(); @@ -138,6 +146,8 @@ class Button : public gcn::Button, public gcn::WidgetListener ImageSet *mImageSet; int mImageWidth; int mImageHeight; + bool mStick; + bool mPressed; }; #endif diff --git a/src/gui/widgets/radiogroup.cpp b/src/gui/widgets/radiogroup.cpp new file mode 100644 index 000000000..24813f6e2 --- /dev/null +++ b/src/gui/widgets/radiogroup.cpp @@ -0,0 +1,39 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2012 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/>. + */ + +#include "gui/widgets/radiogroup.h" + +#include "gui/widgets/radiobutton.h" + +#include "logger.h" + +#include "debug.h" + +RadioGroup::RadioGroup(std::string group, int height, int spacing) : + WidgetGroup(group, height, spacing) +{ +} + +gcn::Widget *RadioGroup::createWidget(std::string text) +{ + RadioButton *widget = new RadioButton(text, mGroup, mCount == 0); + widget->adjustSize(); + return widget; +} diff --git a/src/gui/widgets/radiogroup.h b/src/gui/widgets/radiogroup.h new file mode 100644 index 000000000..07f2a8e34 --- /dev/null +++ b/src/gui/widgets/radiogroup.h @@ -0,0 +1,36 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2012 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/>. + */ + +#ifndef GUI_RADIOGROUP_H +#define GUI_RADIOGROUP_H + +#include "gui/widgets/widgetgroup.h" + +#include <guichan/widget.hpp> + +class RadioGroup : public WidgetGroup +{ + public: + RadioGroup(std::string group, int height, int spacing); + + gcn::Widget *createWidget(std::string name); +}; + +#endif diff --git a/src/gui/widgets/tabstrip.cpp b/src/gui/widgets/tabstrip.cpp new file mode 100644 index 000000000..6dcf02008 --- /dev/null +++ b/src/gui/widgets/tabstrip.cpp @@ -0,0 +1,65 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2012 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/>. + */ + +#include "gui/widgets/tabstrip.h" + +#include "gui/widgets/button.h" +#include "gui/widgets/tab.h" + +#include "logger.h" + +#include "debug.h" + +TabStrip::TabStrip(std::string group, int height, int spacing) : + WidgetGroup(group, height, spacing) +{ +} + +gcn::Widget *TabStrip::createWidget(std::string text) +{ + Button *widget = new Button(); + widget->setStick(true); + widget->setCaption(text); + widget->adjustSize(); + if (!mCount) + widget->setPressed(true); + return widget; +} + +void TabStrip::action(const gcn::ActionEvent &event) +{ + WidgetGroup::action(event); + if (event.getSource()) + { + gcn::Widget *widget = event.getSource(); + if (static_cast<Button*>(widget)->isPressed2()) + { + WidgetListConstIterator iter; + for (iter = mWidgets.begin(); iter != mWidgets.end(); ++ iter) + { + if (*iter != widget) + { + Button *button = static_cast<Button*>(*iter); + button->setPressed(false); + } + } + } + } +} diff --git a/src/gui/widgets/inventoryfilter.h b/src/gui/widgets/tabstrip.h index c4f2b6242..588833606 100644 --- a/src/gui/widgets/inventoryfilter.h +++ b/src/gui/widgets/tabstrip.h @@ -1,6 +1,5 @@ /* * The ManaPlus Client - * Copyright (C) 2009-2010 The Mana Developers * Copyright (C) 2011-2012 The ManaPlus Developers * * This file is part of The ManaPlus Client. @@ -19,27 +18,22 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef GUI_INVENTORYFILTER_H -#define GUI_INVENTORYFILTER_H +#ifndef GUI_TABSTRIP_H +#define GUI_TABSTRIP_H -#include <guichan/actionlistener.hpp> -#include <guichan/widgetlistener.hpp> +#include "gui/widgets/widgetgroup.h" -#include "gui/widgets/horizontcontainer.h" +#include <guichan/actionevent.hpp> +#include <guichan/widget.hpp> -class InventoryFilter : public HorizontContainer, public gcn::ActionListener +class TabStrip : public WidgetGroup { public: - InventoryFilter(std::string group, int height, int spacing); + TabStrip(std::string group, int height, int spacing); - void addButton(std::string tag); - - void addButton(std::string text, std::string tag); + gcn::Widget *createWidget(std::string name); void action(const gcn::ActionEvent &event); - - private: - std::string mGroup; }; #endif diff --git a/src/gui/widgets/inventoryfilter.cpp b/src/gui/widgets/widgetgroup.cpp index 515682cb6..b2b513850 100644 --- a/src/gui/widgets/inventoryfilter.cpp +++ b/src/gui/widgets/widgetgroup.cpp @@ -18,39 +18,42 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "gui/widgets/inventoryfilter.h" - -#include "gui/widgets/horizontcontainer.h" -#include "gui/widgets/radiobutton.h" +#include "gui/widgets/widgetgroup.h" #include "logger.h" #include "debug.h" -InventoryFilter::InventoryFilter(std::string group, int height, int spacing): - HorizontContainer(height, spacing), - mGroup(group) +WidgetGroup::WidgetGroup(std::string group, int height, int spacing) : + mSpacing(spacing), + mCount(0), + mGroup(group), + mLastX(spacing) { + setHeight(height); + addWidgetListener(this); } -void InventoryFilter::addButton(std::string tag) +void WidgetGroup::addButton(std::string tag) { addButton(tag, tag); } -void InventoryFilter::addButton(std::string text, std::string tag) +void WidgetGroup::addButton(std::string text, std::string tag) { if (text.empty() || tag.empty()) return; - RadioButton *radio = new RadioButton(text, mGroup, mCount == 0); - radio->adjustSize(); - radio->setActionEventId(mActionEventId + tag); - radio->addActionListener(this); - HorizontContainer::add(radio); + Widget *widget = createWidget(text); + if (widget) + { + widget->setActionEventId(mActionEventId + tag); + widget->addActionListener(this); + add(widget, mSpacing); + } } -void InventoryFilter::action(const gcn::ActionEvent &event) +void WidgetGroup::action(const gcn::ActionEvent &event) { ActionListenerIterator iter; for (iter = mActionListeners.begin(); @@ -59,3 +62,25 @@ void InventoryFilter::action(const gcn::ActionEvent &event) (*iter)->action(event); } } + +void WidgetGroup::add(gcn::Widget *widget, int spacing) +{ + if (!widget) + return; + + Container::add(widget); + widget->setPosition(mLastX, spacing); + mCount++; + mLastX += widget->getWidth() + 2 * mSpacing; +} + +void WidgetGroup::clear() +{ + Container::clear(); + + mCount = 0; +} + +void WidgetGroup::widgetResized(const gcn::Event &event A_UNUSED) +{ +} diff --git a/src/gui/widgets/widgetgroup.h b/src/gui/widgets/widgetgroup.h new file mode 100644 index 000000000..7b8abd8e9 --- /dev/null +++ b/src/gui/widgets/widgetgroup.h @@ -0,0 +1,59 @@ +/* + * The ManaPlus Client + * Copyright (C) 2011-2012 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/>. + */ + +#ifndef GUI_WIDGETGROUP_H +#define GUI_WIDGETGROUP_H + +#include "gui/widgets/container.h" + +#include <guichan/actionlistener.hpp> +#include <guichan/widgetlistener.hpp> + +class WidgetGroup : public Container, + public gcn::WidgetListener, + public gcn::ActionListener +{ + public: + WidgetGroup(std::string group, int height, int spacing); + + virtual void addButton(std::string tag); + + virtual void addButton(std::string text, std::string tag); + + void action(const gcn::ActionEvent &event); + + virtual void add(gcn::Widget *widget, int spacing); + + virtual void clear(); + + void widgetResized(const gcn::Event &event); + + virtual Widget *createWidget(std::string name) = 0; + + protected: + int mSpacing; + int mCount; + std::string mGroup; + + private: + int mLastX; +}; + +#endif |