Skip to content

Commit

Permalink
adding selector option widget shit
Browse files Browse the repository at this point in the history
  • Loading branch information
Forairaaaaa committed May 20, 2024
1 parent a6a00e4 commit e2c32f3
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/smooth_ui_toolkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "utils/ring_buffer/ring_buffer.h"

#include "widgets/base/base.h"
#include "widgets/selector/base/option.h"
#include "widgets/selector/base/selelctor.h"
#include "widgets/smooth_widget/smooth_widget.h"

#include "misc/water_wave_generator/water_wave_generator.h"
72 changes: 72 additions & 0 deletions src/widgets/selector/base/option.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @file option.h
* @author Forairaaaaa
* @brief
* @version 0.1
* @date 2024-05-20
*
* @copyright Copyright (c) 2024
*
*/
#pragma once
#include "../../../core/transition4d/transition4d.h"
#include "../../base/base.h"
#include <functional>

namespace SmoothUIToolKit
{
namespace Widgets
{
namespace Selector
{
/**
* @brief Provide selectable option behavior abstraction
*
*/
class OptionBase
{
private:
struct OptionBaseData_t
{
bool isHovering = false;
bool isSelected = false;
};
OptionBaseData_t _option_base_data;

public:
inline bool isHovering() { return _option_base_data.isHovering; }
inline void hover()
{
_option_base_data.isHovering = true;
onHover();
}
inline void unHover()
{
_option_base_data.isHovering = false;
onUnHover();
}

inline bool isSelected() { return _option_base_data.isSelected; }
inline void select()
{
_option_base_data.isSelected = true;
onSelect();
}
inline void unSelect()
{
_option_base_data.isSelected = false;
onUnSelect();
}

public:
virtual void onHover() {}
virtual void onUnHover() {}
virtual void onPressed() {}
virtual void onReleasd() {}
virtual void onClicked() {}
virtual void onSelect() {}
virtual void onUnSelect() {}
};
} // namespace Selector
} // namespace Widgets
} // namespace SmoothUIToolKit
65 changes: 65 additions & 0 deletions src/widgets/selector/base/selector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @file selector.cpp
* @author Forairaaaaa
* @brief
* @version 0.1
* @date 2024-05-20
*
* @copyright Copyright (c) 2024
*
*/
#include "selelctor.h"

using namespace SmoothUIToolKit::Widgets::Selector;

void SelectorBase::enter(WidgetBase* widget)
{
if (widget == nullptr)
return;
_selector_base_data.current_widget = widget;
}

bool SelectorBase::back()
{
if (_selector_base_data.current_widget->isRoot())
return false;
_selector_base_data.current_widget = _selector_base_data.current_widget->getParent();
return true;
}

void SelectorBase::goLast()
{
if (getOptionNum() <= 0)
return;

int new_index = _selector_base_data.selected_option_index - 1;
if (new_index < 0)
new_index = _selector_base_data.move_in_loop ? getOptionNum() - 1 : 0;

goTo(new_index);
onGoLast();
}

void SelectorBase::goNext()
{
if (getOptionNum() <= 0)
return;

int new_index = _selector_base_data.selected_option_index + 1;
if (new_index >= getOptionNum())
new_index = _selector_base_data.move_in_loop ? 0 : getOptionNum() - 1;

goTo(new_index);
onGoNext();
}

void SelectorBase::goTo(int optionIndex)
{
if (getOptionNum() <= 0)
return;
if (optionIndex < 0 || optionIndex > (getOptionNum() - 1))
return;

_selector_base_data.selected_option_index = optionIndex;
onGoTo();
}
73 changes: 73 additions & 0 deletions src/widgets/selector/base/selelctor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @file selelctor.h
* @author Forairaaaaa
* @brief
* @version 0.1
* @date 2024-05-20
*
* @copyright Copyright (c) 2024
*
*/
#pragma once
#include "../../../core/transition4d/transition4d.h"
#include "../../base/base.h"
#include "option.h"
#include <cstddef>
#include <functional>

namespace SmoothUIToolKit
{
namespace Widgets
{
namespace Selector
{
/**
* @brief Provide widget selector behavior abstraction
*
*/
class SelectorBase
{
private:
struct SelectorBaseData_t
{
WidgetBase* current_widget = nullptr;
bool move_in_loop = true;
int selected_option_index = 0;
};
SelectorBaseData_t _selector_base_data;

public:
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; }

public:
/**
* @brief Enter a widget
*
* @param widget
*/
void enter(WidgetBase* widget);

/**
* @brief Go back to last widget
*
* @return true
* @return false
*/
bool back();

void goLast();

void goNext();

void goTo(int optionIndex);

public:
virtual void onGoLast() {}
virtual void onGoNext() {}
virtual void onGoTo() {}
};
} // namespace Selector
} // namespace Widgets
} // namespace SmoothUIToolKit

0 comments on commit e2c32f3

Please sign in to comment.