2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined (INCLUDED_SELECTIONLIB_H)
23 #define INCLUDED_SELECTIONLIB_H
25 #include "iselection.h"
26 #include "generic/callback.h"
30 class SelectableBool : public Selectable
38 void setSelected(bool select = true)
42 bool isSelected() const
48 class ObservedSelectable : public Selectable
50 SelectionChangeCallback m_onchanged;
53 ObservedSelectable(const SelectionChangeCallback& onchanged) : m_onchanged(onchanged), m_selected(false)
56 ObservedSelectable(const ObservedSelectable& other) : Selectable(other), m_onchanged(other.m_onchanged), m_selected(false)
58 setSelected(other.isSelected());
60 ObservedSelectable& operator=(const ObservedSelectable& other)
62 setSelected(other.isSelected());
70 void setSelected(bool select)
72 if(select ^ m_selected)
79 bool isSelected() const
85 class SelectableInstance : public scene::Instance
89 InstanceTypeCastTable m_casts;
93 InstanceContainedCast<SelectableInstance, Selectable>::install(m_casts);
95 InstanceTypeCastTable& get()
101 ObservedSelectable m_selectable;
104 typedef LazyStatic<TypeCasts> StaticTypeCasts;
106 SelectableInstance(const scene::Path& path, scene::Instance* parent, void* instance = 0, InstanceTypeCastTable& casts = StaticTypeCasts::instance().get()) :
107 Instance(path, parent, instance != 0 ? instance : this, casts),
108 m_selectable(SelectedChangedCaller(*this))
112 Selectable& get(NullType<Selectable>)
117 Selectable& getSelectable()
121 const Selectable& getSelectable() const
126 void selectedChanged(const Selectable& selectable)
128 GlobalSelectionSystem().getObserver(SelectionSystem::ePrimitive)(selectable);
129 GlobalSelectionSystem().onSelectedChanged(*this, selectable);
131 Instance::selectedChanged();
133 typedef MemberCaller1<SelectableInstance, const Selectable&, &SelectableInstance::selectedChanged> SelectedChangedCaller;
137 template<typename Iterator>
138 inline bool range_check(Iterator start, Iterator finish, Iterator iter)
140 for(;start != finish; ++start)
152 template<typename Selected>
155 typedef std::list<Selected*> List;
158 typedef typename List::iterator iterator;
159 typedef typename List::const_iterator const_iterator;
163 return m_selection.begin();
165 const_iterator begin() const
167 return m_selection.begin();
171 return m_selection.end();
173 const_iterator end() const
175 return m_selection.end();
179 return m_selection.empty();
181 std::size_t size() const
183 return m_selection.size();
187 return *m_selection.back();
189 Selected& back() const
191 return *m_selection.back();
193 void append(Selected& selected)
195 m_selection.push_back(&selected);
197 void erase(Selected& selected)
199 typename List::reverse_iterator i = std::find(m_selection.rbegin(), m_selection.rend(), &selected);
200 ASSERT_MESSAGE(i != m_selection.rend(), "selection-tracking error");
201 ASSERT_MESSAGE(range_check(m_selection.begin(), m_selection.end(), --i.base()), "selection-tracking error");
202 m_selection.erase(--i.base());