-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7943b35
commit c25962d
Showing
10 changed files
with
257 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2021-2023 Darby Johnston | ||
// All rights reserved. | ||
|
||
#include <tlUI/ButtonGroup.h> | ||
|
||
namespace tl | ||
{ | ||
namespace ui | ||
{ | ||
struct ButtonGroup::Private | ||
{ | ||
ButtonGroupType type = ButtonGroupType::Click; | ||
std::vector<std::shared_ptr<IButton> > buttons; | ||
int radio = -1; | ||
std::function<void(int)> clickedCallback; | ||
std::function<void(int, bool)> checkedCallback; | ||
}; | ||
|
||
void ButtonGroup::_init( | ||
ButtonGroupType type, | ||
const std::shared_ptr<system::Context>& context) | ||
{ | ||
TLRENDER_P(); | ||
p.type = type; | ||
} | ||
|
||
ButtonGroup::ButtonGroup() : | ||
_p(new Private) | ||
{} | ||
|
||
ButtonGroup::~ButtonGroup() | ||
{} | ||
|
||
std::shared_ptr<ButtonGroup> ButtonGroup::create( | ||
ButtonGroupType type, | ||
const std::shared_ptr<system::Context>& context) | ||
{ | ||
auto out = std::shared_ptr<ButtonGroup>(new ButtonGroup); | ||
out->_init(type, context); | ||
return out; | ||
} | ||
|
||
void ButtonGroup::addButton(const std::shared_ptr<IButton>& button) | ||
{ | ||
TLRENDER_P(); | ||
switch (p.type) | ||
{ | ||
case ButtonGroupType::Click: | ||
button->setCheckable(false); | ||
break; | ||
case ButtonGroupType::Radio: | ||
button->setCheckable(true); | ||
break; | ||
case ButtonGroupType::Toggle: | ||
button->setCheckable(true); | ||
break; | ||
} | ||
const size_t index = p.buttons.size(); | ||
p.buttons.push_back(button); | ||
button->setClickedCallback( | ||
[this, index]() | ||
{ | ||
if (_p->clickedCallback) | ||
{ | ||
_p->clickedCallback(index); | ||
} | ||
}); | ||
button->setCheckedCallback( | ||
[this, index](bool value) | ||
{ | ||
switch (_p->type) | ||
{ | ||
case ButtonGroupType::Radio: | ||
for (size_t i = 0; i < _p->buttons.size(); ++i) | ||
{ | ||
_p->buttons[i]->setChecked(i == index); | ||
} | ||
if (_p->checkedCallback && index != _p->radio) | ||
{ | ||
_p->checkedCallback(index, true); | ||
} | ||
_p->radio = index; | ||
break; | ||
case ButtonGroupType::Toggle: | ||
for (size_t i = 0; i < _p->buttons.size(); ++i) | ||
{ | ||
if (i != index) | ||
{ | ||
_p->buttons[i]->setChecked(false); | ||
} | ||
} | ||
if (_p->checkedCallback) | ||
{ | ||
_p->checkedCallback(index, value); | ||
} | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
void ButtonGroup::clearButtons() | ||
{ | ||
TLRENDER_P(); | ||
p.buttons.clear(); | ||
} | ||
|
||
void ButtonGroup::setClickedCallback(const std::function<void(int)>& value) | ||
{ | ||
_p->clickedCallback = value; | ||
} | ||
|
||
void ButtonGroup::setCheckedCallback(const std::function<void(int, bool)>& value) | ||
{ | ||
_p->checkedCallback = value; | ||
} | ||
} | ||
} |
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,57 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// Copyright (c) 2021-2023 Darby Johnston | ||
// All rights reserved. | ||
|
||
#pragma once | ||
|
||
#include <tlUI/IButton.h> | ||
|
||
namespace tl | ||
{ | ||
namespace ui | ||
{ | ||
//! Button group type. | ||
enum class ButtonGroupType | ||
{ | ||
Click, | ||
Radio, | ||
Toggle | ||
}; | ||
|
||
//! Button group. | ||
class ButtonGroup : public std::enable_shared_from_this<ButtonGroup> | ||
{ | ||
TLRENDER_NON_COPYABLE(ButtonGroup); | ||
|
||
protected: | ||
void _init( | ||
ButtonGroupType, | ||
const std::shared_ptr<system::Context>&); | ||
|
||
ButtonGroup(); | ||
|
||
public: | ||
~ButtonGroup(); | ||
|
||
//! Create a new button group. | ||
static std::shared_ptr<ButtonGroup> create( | ||
ButtonGroupType, | ||
const std::shared_ptr<system::Context>&); | ||
|
||
//! Add a button to the group. | ||
void addButton(const std::shared_ptr<IButton>&); | ||
|
||
//! Clear the buttons in the group. | ||
void clearButtons(); | ||
|
||
//! Set the clicked callback. | ||
void setClickedCallback(const std::function<void(int)>&); | ||
|
||
//! Set the checked callback. | ||
void setCheckedCallback(const std::function<void(int, bool)>&); | ||
|
||
private: | ||
TLRENDER_PRIVATE(); | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.