Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #588 from Vultraz/gui2_widget_groups
gui2: implement widget groups
- Loading branch information
Showing
4 changed files
with
148 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
Copyright (C) 2008 - 2016 The Battle for Wesnoth Project http://www.wesnoth.org/ | ||
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 | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#ifndef GUI_WIDGETS_GROUP_HPP_INCLUDED | ||
#define GUI_WIDGETS_GROUP_HPP_INCLUDED | ||
|
||
#include "gui/auxiliary/event/dispatcher.hpp" | ||
#include "gui/widgets/selectable.hpp" | ||
#include "gui/widgets/widget.hpp" | ||
#include "utils/foreach.tpp" | ||
|
||
#include <vector> | ||
#include <boost/bind.hpp> | ||
|
||
namespace gui2 | ||
{ | ||
|
||
template <class T> | ||
class tgroup | ||
{ | ||
public: | ||
typedef typename std::vector<std::pair<tselectable_*, T> > group_list; | ||
typedef typename group_list::iterator group_iterator; | ||
typedef typename group_list::const_iterator group_iterator_const; | ||
|
||
/** | ||
* Adds a widget/value pair to the group vector. A callback is set | ||
* that sets all members' toggle states to false when clicked. This | ||
* happens before individual widget handlers fire, meaning that the | ||
* clicked widget will remain the only one selected. | ||
*/ | ||
void add_member(tselectable_* widget, const T& value) | ||
{ | ||
members_.push_back(std::make_pair(widget, value)); | ||
|
||
dynamic_cast<twidget*>(widget)->connect_signal<event::LEFT_BUTTON_CLICK>(boost::bind( | ||
&tgroup::group_operator, this), event::tdispatcher::front_child); | ||
} | ||
|
||
/** | ||
* Removes a member from the group vector. | ||
* | ||
* TODO: implement | ||
void remove_member(tselectable_* widget) | ||
{ | ||
}*/ | ||
|
||
/** | ||
* Group member getters | ||
*/ | ||
std::pair<group_iterator, group_iterator> members() | ||
{ | ||
return std::make_pair(members_.begin(), members_.end()); | ||
} | ||
|
||
std::pair<group_iterator_const, group_iterator_const> members() const | ||
{ | ||
return std::make_pair(members_.begin(), members_.end()); | ||
} | ||
|
||
/** | ||
* The default actions to take when clicking on one of the widgets | ||
* in the group. | ||
*/ | ||
void group_operator() | ||
{ | ||
FOREACH(AUTO& member, members()) | ||
{ | ||
member.first->set_value(false); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the value paired with the currently activiely toggled member | ||
* of the group. | ||
*/ | ||
T get_active_member_value() | ||
{ | ||
FOREACH(AUTO& member, members()) | ||
{ | ||
if(member.first->get_value_bool()) { | ||
return member.second; | ||
} | ||
} | ||
|
||
return T(); | ||
} | ||
|
||
/** | ||
* Sets the toggle values for all widgets besides the one associated | ||
* with the specified value to false. | ||
*/ | ||
void set_member_states(const T& value) | ||
{ | ||
FOREACH(AUTO& member, members()) | ||
{ | ||
member.first->set_value(member.second == value); | ||
} | ||
} | ||
|
||
private: | ||
group_list members_; | ||
|
||
}; | ||
|
||
} // namespace gui2 | ||
|
||
#endif |