From 9fc07b15f1b051eecd662802162772804dc77b8e Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 14 Apr 2012 17:39:14 +0300 Subject: Rename class InventoryFilter to RadioGroup. Add WidgetGroup and TabStrip classes for grouping controls. --- src/gui/widgets/button.cpp | 53 +++++++++++++++-------- src/gui/widgets/button.h | 10 +++++ src/gui/widgets/inventoryfilter.cpp | 61 -------------------------- src/gui/widgets/inventoryfilter.h | 45 ------------------- src/gui/widgets/radiogroup.cpp | 39 +++++++++++++++++ src/gui/widgets/radiogroup.h | 36 ++++++++++++++++ src/gui/widgets/tabstrip.cpp | 65 ++++++++++++++++++++++++++++ src/gui/widgets/tabstrip.h | 39 +++++++++++++++++ src/gui/widgets/widgetgroup.cpp | 86 +++++++++++++++++++++++++++++++++++++ src/gui/widgets/widgetgroup.h | 59 +++++++++++++++++++++++++ 10 files changed, 370 insertions(+), 123 deletions(-) delete mode 100644 src/gui/widgets/inventoryfilter.cpp delete mode 100644 src/gui/widgets/inventoryfilter.h create mode 100644 src/gui/widgets/radiogroup.cpp create mode 100644 src/gui/widgets/radiogroup.h create mode 100644 src/gui/widgets/tabstrip.cpp create mode 100644 src/gui/widgets/tabstrip.h create mode 100644 src/gui/widgets/widgetgroup.cpp create mode 100644 src/gui/widgets/widgetgroup.h (limited to 'src/gui/widgets') 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/inventoryfilter.cpp b/src/gui/widgets/inventoryfilter.cpp deleted file mode 100644 index 515682cb6..000000000 --- a/src/gui/widgets/inventoryfilter.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 . - */ - -#include "gui/widgets/inventoryfilter.h" - -#include "gui/widgets/horizontcontainer.h" -#include "gui/widgets/radiobutton.h" - -#include "logger.h" - -#include "debug.h" - -InventoryFilter::InventoryFilter(std::string group, int height, int spacing): - HorizontContainer(height, spacing), - mGroup(group) -{ -} - -void InventoryFilter::addButton(std::string tag) -{ - addButton(tag, tag); -} - -void InventoryFilter::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); -} - -void InventoryFilter::action(const gcn::ActionEvent &event) -{ - ActionListenerIterator iter; - for (iter = mActionListeners.begin(); - iter != mActionListeners.end(); ++iter) - { - (*iter)->action(event); - } -} diff --git a/src/gui/widgets/inventoryfilter.h b/src/gui/widgets/inventoryfilter.h deleted file mode 100644 index c4f2b6242..000000000 --- a/src/gui/widgets/inventoryfilter.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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. - * - * 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 . - */ - -#ifndef GUI_INVENTORYFILTER_H -#define GUI_INVENTORYFILTER_H - -#include -#include - -#include "gui/widgets/horizontcontainer.h" - -class InventoryFilter : public HorizontContainer, public gcn::ActionListener -{ - public: - InventoryFilter(std::string group, int height, int spacing); - - void addButton(std::string tag); - - void addButton(std::string text, std::string tag); - - void action(const gcn::ActionEvent &event); - - private: - std::string mGroup; -}; - -#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 . + */ + +#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 . + */ + +#ifndef GUI_RADIOGROUP_H +#define GUI_RADIOGROUP_H + +#include "gui/widgets/widgetgroup.h" + +#include + +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 . + */ + +#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(widget)->isPressed2()) + { + WidgetListConstIterator iter; + for (iter = mWidgets.begin(); iter != mWidgets.end(); ++ iter) + { + if (*iter != widget) + { + Button *button = static_cast(*iter); + button->setPressed(false); + } + } + } + } +} diff --git a/src/gui/widgets/tabstrip.h b/src/gui/widgets/tabstrip.h new file mode 100644 index 000000000..588833606 --- /dev/null +++ b/src/gui/widgets/tabstrip.h @@ -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 . + */ + +#ifndef GUI_TABSTRIP_H +#define GUI_TABSTRIP_H + +#include "gui/widgets/widgetgroup.h" + +#include +#include + +class TabStrip : public WidgetGroup +{ + public: + TabStrip(std::string group, int height, int spacing); + + gcn::Widget *createWidget(std::string name); + + void action(const gcn::ActionEvent &event); +}; + +#endif diff --git a/src/gui/widgets/widgetgroup.cpp b/src/gui/widgets/widgetgroup.cpp new file mode 100644 index 000000000..b2b513850 --- /dev/null +++ b/src/gui/widgets/widgetgroup.cpp @@ -0,0 +1,86 @@ +/* + * 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 . + */ + +#include "gui/widgets/widgetgroup.h" + +#include "logger.h" + +#include "debug.h" + +WidgetGroup::WidgetGroup(std::string group, int height, int spacing) : + mSpacing(spacing), + mCount(0), + mGroup(group), + mLastX(spacing) +{ + setHeight(height); + addWidgetListener(this); +} + +void WidgetGroup::addButton(std::string tag) +{ + addButton(tag, tag); +} + +void WidgetGroup::addButton(std::string text, std::string tag) +{ + if (text.empty() || tag.empty()) + return; + + Widget *widget = createWidget(text); + if (widget) + { + widget->setActionEventId(mActionEventId + tag); + widget->addActionListener(this); + add(widget, mSpacing); + } +} + +void WidgetGroup::action(const gcn::ActionEvent &event) +{ + ActionListenerIterator iter; + for (iter = mActionListeners.begin(); + iter != mActionListeners.end(); ++iter) + { + (*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 . + */ + +#ifndef GUI_WIDGETGROUP_H +#define GUI_WIDGETGROUP_H + +#include "gui/widgets/container.h" + +#include +#include + +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 -- cgit v1.2.3-70-g09d2