From fbdf659723a95ef05a55bd0463d93a949bb9d0f7 Mon Sep 17 00:00:00 2001 From: The Dax Date: Mon, 5 May 2014 23:39:57 -0400 Subject: [PATCH] Add the ability for Sliders and their corresponding Screens to have the ability to move in steps greater than 1. No float support, only the integer screens are affected. --- ui/ui_screen.cpp | 13 +++++++++---- ui/ui_screen.h | 9 +++++++-- ui/view.cpp | 11 +++++++---- ui/view.h | 3 +++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ui/ui_screen.cpp b/ui/ui_screen.cpp index 64ce3d140a..bbb2fe21bb 100644 --- a/ui/ui_screen.cpp +++ b/ui/ui_screen.cpp @@ -271,13 +271,18 @@ PopupSliderChoice::PopupSliderChoice(int *value, int minValue, int maxValue, con OnClick.Handle(this, &PopupSliderChoice::HandleClick); } +PopupSliderChoice::PopupSliderChoice(int *value, int minValue, int maxValue, const std::string &text, int step, ScreenManager *screenManager, LayoutParams *layoutParams) +: Choice(text, "", false, layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), step_(step), screenManager_(screenManager) { + OnClick.Handle(this, &PopupSliderChoice::HandleClick); +} + PopupSliderChoiceFloat::PopupSliderChoiceFloat(float *value, float minValue, float maxValue, const std::string &text, ScreenManager *screenManager, LayoutParams *layoutParams) : Choice(text, "", false, layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), screenManager_(screenManager) { OnClick.Handle(this, &PopupSliderChoiceFloat::HandleClick); } EventReturn PopupSliderChoice::HandleClick(EventParams &e) { - SliderPopupScreen *popupScreen = new SliderPopupScreen(value_, minValue_, maxValue_, text_); + SliderPopupScreen *popupScreen = new SliderPopupScreen(value_, minValue_, maxValue_, text_, step_); popupScreen->OnChange.Handle(this, &PopupSliderChoice::HandleChange); screenManager_->push(popupScreen); return EVENT_DONE; @@ -327,13 +332,13 @@ void PopupSliderChoiceFloat::Draw(UIContext &dc) { } EventReturn SliderPopupScreen::OnDecrease(EventParams ¶ms) { - sliderValue_--; + sliderValue_ -= step_; slider_->Clamp(); return EVENT_DONE; } EventReturn SliderPopupScreen::OnIncrease(EventParams ¶ms) { - sliderValue_++; + sliderValue_ += step_; slider_->Clamp(); return EVENT_DONE; } @@ -342,7 +347,7 @@ void SliderPopupScreen::CreatePopupContents(UI::ViewGroup *parent) { using namespace UI; sliderValue_ = *value_; LinearLayout *lin = parent->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(UI::Margins(10, 5)))); - slider_ = new Slider(&sliderValue_, minValue_, maxValue_, new LinearLayoutParams(1.0f)); + slider_ = new Slider(&sliderValue_, minValue_, maxValue_, step_, new LinearLayoutParams(1.0f)); lin->Add(slider_); lin->Add(new Button(" - "))->OnClick.Handle(this, &SliderPopupScreen::OnDecrease); lin->Add(new Button(" + "))->OnClick.Handle(this, &SliderPopupScreen::OnIncrease); diff --git a/ui/ui_screen.h b/ui/ui_screen.h index ecd2378ece..888ac0c3f8 100644 --- a/ui/ui_screen.h +++ b/ui/ui_screen.h @@ -114,7 +114,8 @@ namespace UI { class SliderPopupScreen : public PopupScreen { public: - SliderPopupScreen(int *value, int minValue, int maxValue, const std::string &title) : PopupScreen(title, "OK", "Cancel"), value_(value), minValue_(minValue), maxValue_(maxValue) {} + SliderPopupScreen(int *value, int minValue, int maxValue, const std::string &title, int step = 1) + : PopupScreen(title, "OK", "Cancel"), value_(value), minValue_(minValue), maxValue_(maxValue), step_(step) {} void CreatePopupContents(ViewGroup *parent); Event OnChange; @@ -128,11 +129,13 @@ private: int sliderValue_; int minValue_; int maxValue_; + float step_; }; class SliderFloatPopupScreen : public PopupScreen { public: - SliderFloatPopupScreen(float *value, float minValue, float maxValue, const std::string &title) : PopupScreen(title, "OK", "Cancel"), value_(value), minValue_(minValue), maxValue_(maxValue) {} + SliderFloatPopupScreen(float *value, float minValue, float maxValue, const std::string &title) + : PopupScreen(title, "OK", "Cancel"), value_(value), minValue_(minValue), maxValue_(maxValue) {} void CreatePopupContents(UI::ViewGroup *parent); Event OnChange; @@ -182,6 +185,7 @@ private: class PopupSliderChoice : public Choice { public: PopupSliderChoice(int *value, int minValue, int maxValue, const std::string &text, ScreenManager *screenManager, LayoutParams *layoutParams = 0); + PopupSliderChoice(int *value, int minValue, int maxValue, const std::string &text, int step, ScreenManager *screenManager, LayoutParams *layoutParams = 0); void Draw(UIContext &dc); @@ -194,6 +198,7 @@ private: int *value_; int minValue_; int maxValue_; + float step_; ScreenManager *screenManager_; }; diff --git a/ui/view.cpp b/ui/view.cpp index 73e30beaf2..0b45e499f7 100644 --- a/ui/view.cpp +++ b/ui/view.cpp @@ -622,18 +622,18 @@ void Slider::Key(const KeyInput &input) { case NKCODE_DPAD_LEFT: case NKCODE_MINUS: case NKCODE_NUMPAD_SUBTRACT: - *value_ -= 1; + *value_ -= step_; break; case NKCODE_DPAD_RIGHT: case NKCODE_PLUS: case NKCODE_NUMPAD_ADD: - *value_ += 1; + *value_ += step_; break; case NKCODE_PAGE_UP: - *value_ -= 10; + *value_ -= step_ * 10; break; case NKCODE_PAGE_DOWN: - *value_ += 10; + *value_ += step_ * 10; break; case NKCODE_MOVE_HOME: *value_ = minValue_; @@ -658,6 +658,9 @@ void Slider::Touch(const TouchInput &input) { void Slider::Clamp() { if (*value_ < minValue_) *value_ = minValue_; else if (*value_ > maxValue_) *value_ = maxValue_; + + // Clamp the value to be a multiple of the nearest step (e.g. if step == 5, value == 293, it'll round down to 290). + *value_ = *value_ - fmodf(*value_, step_); } void Slider::Draw(UIContext &dc) { diff --git a/ui/view.h b/ui/view.h index 38ef9db698..490b12ff4d 100644 --- a/ui/view.h +++ b/ui/view.h @@ -439,6 +439,8 @@ class Slider : public Clickable { public: Slider(int *value, int minValue, int maxValue, LayoutParams *layoutParams = 0) : Clickable(layoutParams), value_(value), showPercent_(false), minValue_(minValue), maxValue_(maxValue), paddingLeft_(5), paddingRight_(70) {} + Slider(int *value, int minValue, int maxValue, int step = 1, LayoutParams *layoutParams = 0) + : Clickable(layoutParams), value_(value), showPercent_(false), minValue_(minValue), maxValue_(maxValue), paddingLeft_(5), paddingRight_(70), step_(step) {} virtual void Draw(UIContext &dc); virtual void Key(const KeyInput &input); virtual void Touch(const TouchInput &input); @@ -454,6 +456,7 @@ private: int maxValue_; float paddingLeft_; float paddingRight_; + float step_; }; class SliderFloat : public Clickable {