diff --git a/src/smooth_ui_toolkit.h b/src/smooth_ui_toolkit.h index 76c5123..3f8dbf1 100644 --- a/src/smooth_ui_toolkit.h +++ b/src/smooth_ui_toolkit.h @@ -28,6 +28,8 @@ #include "widgets/base/base.h" #include "widgets/selector/base/option.h" #include "widgets/selector/base/selelctor.h" +#include "widgets/selector/smooth_selector/smooth_option.h" +#include "widgets/selector/smooth_selector/smooth_selector.h" #include "widgets/smooth_widget/smooth_widget.h" #include "misc/water_wave_generator/water_wave_generator.h" diff --git a/src/widgets/selector/base/option.h b/src/widgets/selector/base/option.h index fab3496..2f25128 100644 --- a/src/widgets/selector/base/option.h +++ b/src/widgets/selector/base/option.h @@ -9,9 +9,6 @@ * */ #pragma once -#include "../../../core/transition4d/transition4d.h" -#include "../../base/base.h" -#include namespace SmoothUIToolKit { diff --git a/src/widgets/selector/base/selector.cpp b/src/widgets/selector/base/selector.cpp index 4519317..a4c9edb 100644 --- a/src/widgets/selector/base/selector.cpp +++ b/src/widgets/selector/base/selector.cpp @@ -12,11 +12,27 @@ using namespace SmoothUIToolKit::Widgets::Selector; +SmoothUIToolKit::Widgets::WidgetBase* SelectorBase::getSelectedWidget() +{ + if (_selector_base_data.current_widget == nullptr) + return nullptr; + if (_selector_base_data.selected_option_index < 0) + return nullptr; + return _selector_base_data.current_widget->getChildren()[_selector_base_data.selected_option_index]; +} + void SelectorBase::enter(WidgetBase* widget) { if (widget == nullptr) return; + + // Set current widget _selector_base_data.current_widget = widget; + onEnter(); + + // Select first widget + if (!_selector_base_data.current_widget->isLeaf()) + goTo(0); } bool SelectorBase::back() @@ -24,12 +40,13 @@ bool SelectorBase::back() if (_selector_base_data.current_widget->isRoot()) return false; _selector_base_data.current_widget = _selector_base_data.current_widget->getParent(); + onBack(); return true; } void SelectorBase::goLast() { - if (getOptionNum() <= 0) + if (getOptionNum() == 0) return; int new_index = _selector_base_data.selected_option_index - 1; @@ -42,7 +59,7 @@ void SelectorBase::goLast() void SelectorBase::goNext() { - if (getOptionNum() <= 0) + if (getOptionNum() == 0) return; int new_index = _selector_base_data.selected_option_index + 1; @@ -55,7 +72,7 @@ void SelectorBase::goNext() void SelectorBase::goTo(int optionIndex) { - if (getOptionNum() <= 0) + if (getOptionNum() == 0) return; if (optionIndex < 0 || optionIndex > (getOptionNum() - 1)) return; diff --git a/src/widgets/selector/base/selelctor.h b/src/widgets/selector/base/selelctor.h index 499ce8d..69c92e4 100644 --- a/src/widgets/selector/base/selelctor.h +++ b/src/widgets/selector/base/selelctor.h @@ -9,7 +9,6 @@ * */ #pragma once -#include "../../../core/transition4d/transition4d.h" #include "../../base/base.h" #include "option.h" #include @@ -32,7 +31,7 @@ namespace SmoothUIToolKit { WidgetBase* current_widget = nullptr; bool move_in_loop = true; - int selected_option_index = 0; + int selected_option_index = -1; }; SelectorBaseData_t _selector_base_data; @@ -40,6 +39,7 @@ namespace SmoothUIToolKit inline void moveInloop(bool moveInLoop) { _selector_base_data.move_in_loop = moveInLoop; } inline size_t getOptionNum() { return _selector_base_data.current_widget->getChildren().size(); } inline int getSelectedOptionIndex() { return _selector_base_data.selected_option_index; } + WidgetBase* getSelectedWidget(); public: /** @@ -64,6 +64,8 @@ namespace SmoothUIToolKit void goTo(int optionIndex); public: + virtual void onEnter() {} + virtual void onBack() {} virtual void onGoLast() {} virtual void onGoNext() {} virtual void onGoTo() {} diff --git a/src/widgets/selector/smooth_selector/smooth_option.h b/src/widgets/selector/smooth_selector/smooth_option.h new file mode 100644 index 0000000..a19675e --- /dev/null +++ b/src/widgets/selector/smooth_selector/smooth_option.h @@ -0,0 +1,26 @@ +/** + * @file smooth_option.h + * @author Forairaaaaa + * @brief + * @version 0.1 + * @date 2024-05-20 + * + * @copyright Copyright (c) 2024 + * + */ +#pragma once +#include "../../smooth_widget/smooth_widget.h" +#include "../base/option.h" + +namespace SmoothUIToolKit +{ + namespace Widgets + { + namespace Selector + { + class SmoothOption : public SmoothWidgetBase, public OptionBase + { + }; + } // namespace Selector + } // namespace Widgets +} // namespace SmoothUIToolKit diff --git a/src/widgets/selector/smooth_selector/smooth_selector.cpp b/src/widgets/selector/smooth_selector/smooth_selector.cpp new file mode 100644 index 0000000..ddeb9b7 --- /dev/null +++ b/src/widgets/selector/smooth_selector/smooth_selector.cpp @@ -0,0 +1,21 @@ +/** + * @file smooth_selector.cpp + * @author Forairaaaaa + * @brief + * @version 0.1 + * @date 2024-05-20 + * + * @copyright Copyright (c) 2024 + * + */ +#include "smooth_selector.h" +#include "smooth_option.h" + +using namespace SmoothUIToolKit::Widgets::Selector; + +void SmoothSelector::onGoTo() +{ + // Get selected widget frame + auto target_frame = ((SmoothOption*)getSelectedWidget())->getTransition().getTargetPoint(); + getTransition().moveTo(target_frame); +} diff --git a/src/widgets/selector/smooth_selector/smooth_selector.h b/src/widgets/selector/smooth_selector/smooth_selector.h new file mode 100644 index 0000000..35c231f --- /dev/null +++ b/src/widgets/selector/smooth_selector/smooth_selector.h @@ -0,0 +1,27 @@ +/** + * @file smooth_selector.h + * @author Forairaaaaa + * @brief + * @version 0.1 + * @date 2024-05-20 + * + * @copyright Copyright (c) 2024 + * + */ +#pragma once +#include "../../smooth_widget/smooth_widget.h" +#include "../base/selelctor.h" + +namespace SmoothUIToolKit +{ + namespace Widgets + { + namespace Selector + { + class SmoothSelector : public SmoothWidgetBase, public SelectorBase + { + void onGoTo() override; + }; + } // namespace Selector + } // namespace Widgets +} // namespace SmoothUIToolKit