1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
---|---|
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #include "GUI/BsGUIToggleGroup.h" |
4 | #include "GUI/BsGUIToggle.h" |
5 | |
6 | namespace bs |
7 | { |
8 | GUIToggleGroup::GUIToggleGroup(bool allowAllOff) |
9 | :mAllowAllOff(allowAllOff) |
10 | { } |
11 | |
12 | GUIToggleGroup::~GUIToggleGroup() |
13 | { |
14 | for(auto& button : mButtons) |
15 | { |
16 | button->_setToggleGroup(nullptr); |
17 | } |
18 | } |
19 | |
20 | void GUIToggleGroup::initialize(const SPtr<GUIToggleGroup>& sharedPtr) |
21 | { |
22 | mThis = sharedPtr; |
23 | } |
24 | |
25 | void GUIToggleGroup::_add(GUIToggle* toggle) |
26 | { |
27 | auto iterFind = std::find(begin(mButtons), end(mButtons), toggle); |
28 | if(iterFind != end(mButtons)) |
29 | return; |
30 | |
31 | mButtons.push_back(toggle); |
32 | toggle->_setToggleGroup(mThis.lock()); |
33 | } |
34 | |
35 | void GUIToggleGroup::_remove(GUIToggle* toggle) |
36 | { |
37 | auto sharedPtr = mThis.lock(); // Make sure we keep a reference because calling _setToggleGroup(nullptr) |
38 | // may otherwise clear the last reference and cause us to destruct |
39 | |
40 | auto iterFind = std::find(begin(mButtons), end(mButtons), toggle); |
41 | if(iterFind == end(mButtons)) |
42 | return; |
43 | |
44 | (*iterFind)->_setToggleGroup(nullptr); |
45 | mButtons.erase(iterFind); |
46 | } |
47 | } |