Move some popup utility classes here

This commit is contained in:
Henrik Rydgard 2013-09-07 13:38:12 +02:00
parent 0b752abc41
commit 997aae346b
2 changed files with 148 additions and 3 deletions

View File

@ -4,6 +4,7 @@
#include "ui/ui_context.h"
#include "ui/screen.h"
#include "i18n/i18n.h"
#include "gfx_es2/draw_buffer.h"
UIScreen::UIScreen()
: Screen(), root_(0), recreateViews_(true), hatDown_(0) {
@ -189,6 +190,73 @@ UI::EventReturn ListPopupScreen::OnListChoice(UI::EventParams &e) {
return UI::EVENT_DONE;
}
namespace UI {
UI::EventReturn PopupMultiChoice::HandleClick(UI::EventParams &e) {
std::vector<std::string> choices;
for (int i = 0; i < numChoices_; i++) {
choices.push_back(category_ ? category_->T(choices_[i]) : choices_[i]);
}
Screen *popupScreen = new ListPopupScreen(text_, choices, *value_ - minVal_,
std::bind(&PopupMultiChoice::ChoiceCallback, this, placeholder::_1));
screenManager_->push(popupScreen);
return UI::EVENT_DONE;
}
void PopupMultiChoice::UpdateText() {
valueText_ = category_ ? category_->T(choices_[*value_ - minVal_]) : choices_[*value_ - minVal_];
}
void PopupMultiChoice::ChoiceCallback(int num) {
if (num != -1) {
*value_ = num + minVal_;
UpdateText();
UI::EventParams e;
e.v = this;
e.a = num;
OnChoice.Trigger(e);
}
}
void PopupMultiChoice::Draw(UIContext &dc) {
Choice::Draw(dc);
int paddingX = 12;
dc.SetFontStyle(dc.theme->uiFont);
dc.DrawText(valueText_.c_str(), bounds_.x2() - paddingX, bounds_.centerY(), 0xFFFFFFFF, ALIGN_RIGHT | ALIGN_VCENTER);
}
EventReturn PopupSliderChoice::HandleClick(EventParams &e) {
Screen *popupScreen = new SliderPopupScreen(value_, minValue_, maxValue_, text_);
screenManager_->push(popupScreen);
return EVENT_DONE;
}
void PopupSliderChoice::Draw(UIContext &dc) {
Choice::Draw(dc);
char temp[32];
sprintf(temp, "%i", *value_);
dc.SetFontStyle(dc.theme->uiFont);
dc.DrawText(temp, bounds_.x2() - 12, bounds_.centerY(), 0xFFFFFFFF, ALIGN_RIGHT | ALIGN_VCENTER);
}
EventReturn PopupSliderChoiceFloat::HandleClick(EventParams &e) {
Screen *popupScreen = new SliderFloatPopupScreen(value_, minValue_, maxValue_, text_);
screenManager_->push(popupScreen);
return EVENT_DONE;
}
void PopupSliderChoiceFloat::Draw(UIContext &dc) {
Choice::Draw(dc);
char temp[32];
sprintf(temp, "%2.2f", *value_);
dc.SetFontStyle(dc.theme->uiFont);
dc.DrawText(temp, bounds_.x2() - 12, bounds_.centerY(), 0xFFFFFFFF, ALIGN_RIGHT | ALIGN_VCENTER);
}
void SliderPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
using namespace UI;
sliderValue_ = *value_;
@ -212,3 +280,5 @@ void SliderFloatPopupScreen::OnCompleted(DialogResult result) {
if (result == DR_OK)
*value_ = sliderValue_;
}
} // namespace UI

View File

@ -3,6 +3,8 @@
#include "ui/screen.h"
#include "ui/viewgroup.h"
class I18NCategory;
class UIScreen : public Screen {
public:
UIScreen();
@ -102,14 +104,16 @@ private:
// TODO: Need a way to translate OK and Cancel
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) {}
void CreatePopupContents(UI::ViewGroup *parent);
void CreatePopupContents(ViewGroup *parent);
private:
virtual void OnCompleted(DialogResult result);
UI::Slider *slider_;
Slider *slider_;
int *value_;
int sliderValue_;
int minValue_;
@ -128,4 +132,75 @@ private:
float *value_;
float minValue_;
float maxValue_;
};
};
// Reads and writes value to determine the current selection.
class PopupMultiChoice : public UI::Choice {
public:
PopupMultiChoice(int *value, const std::string &text, const char **choices, int minVal, int numChoices,
I18NCategory *category, ScreenManager *screenManager, UI::LayoutParams *layoutParams = 0)
: UI::Choice(text, "", false, layoutParams), value_(value), choices_(choices), minVal_(minVal), numChoices_(numChoices),
category_(category), screenManager_(screenManager) {
if (*value >= numChoices+minVal) *value = numChoices+minVal-1;
if (*value < minVal) *value = minVal;
OnClick.Handle(this, &PopupMultiChoice::HandleClick);
UpdateText();
}
virtual void Draw(UIContext &dc);
UI::Event OnChoice;
private:
void UpdateText();
UI::EventReturn HandleClick(UI::EventParams &e);
void ChoiceCallback(int num);
int *value_;
const char **choices_;
int minVal_;
int numChoices_;
I18NCategory *category_;
ScreenManager *screenManager_;
std::string valueText_;
};
class PopupSliderChoice : public Choice {
public:
PopupSliderChoice(int *value, int minValue, int maxValue, const std::string &text, ScreenManager *screenManager, LayoutParams *layoutParams = 0)
: Choice(text, "", false, layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), screenManager_(screenManager) {
OnClick.Handle(this, &PopupSliderChoice::HandleClick);
}
void Draw(UIContext &dc);
private:
EventReturn HandleClick(EventParams &e);
int *value_;
int minValue_;
int maxValue_;
ScreenManager *screenManager_;
};
class PopupSliderChoiceFloat : public Choice {
public:
PopupSliderChoiceFloat(float *value, float minValue, float maxValue, const std::string &text, ScreenManager *screenManager, LayoutParams *layoutParams = 0)
: Choice(text, "", false, layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), screenManager_(screenManager) {
OnClick.Handle(this, &PopupSliderChoiceFloat::HandleClick);
}
void Draw(UIContext &dc);
private:
EventReturn HandleClick(EventParams &e);
float *value_;
float minValue_;
float maxValue_;
ScreenManager *screenManager_;
};
} // namespace UI