2013-08-12 23:07:27 +02:00
|
|
|
#include "input/input_state.h"
|
|
|
|
#include "input/keycodes.h"
|
2013-06-03 19:57:40 +02:00
|
|
|
#include "ui/ui_screen.h"
|
|
|
|
#include "ui/ui_context.h"
|
|
|
|
#include "ui/screen.h"
|
2013-08-31 05:33:23 +08:00
|
|
|
#include "i18n/i18n.h"
|
2013-09-07 13:38:12 +02:00
|
|
|
#include "gfx_es2/draw_buffer.h"
|
2013-06-03 19:57:40 +02:00
|
|
|
|
2013-06-08 22:41:17 +02:00
|
|
|
UIScreen::UIScreen()
|
2013-08-12 23:07:27 +02:00
|
|
|
: Screen(), root_(0), recreateViews_(true), hatDown_(0) {
|
2013-06-08 22:41:17 +02:00
|
|
|
}
|
|
|
|
|
2013-11-26 13:56:56 +01:00
|
|
|
UIScreen::~UIScreen() {
|
|
|
|
delete root_;
|
|
|
|
}
|
|
|
|
|
2013-07-21 13:30:47 +02:00
|
|
|
void UIScreen::DoRecreateViews() {
|
2013-06-08 22:41:17 +02:00
|
|
|
if (recreateViews_) {
|
2013-06-03 19:57:40 +02:00
|
|
|
delete root_;
|
|
|
|
root_ = 0;
|
|
|
|
CreateViews();
|
2014-03-03 11:39:06 +01:00
|
|
|
if (root_ && root_->GetDefaultFocusView()) {
|
|
|
|
root_->GetDefaultFocusView()->SetFocus();
|
|
|
|
}
|
2013-06-09 01:21:08 +02:00
|
|
|
recreateViews_ = false;
|
2013-06-03 19:57:40 +02:00
|
|
|
}
|
2013-07-21 13:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScreen::update(InputState &input) {
|
|
|
|
DoRecreateViews();
|
2013-06-03 19:57:40 +02:00
|
|
|
|
2013-07-20 12:04:24 +02:00
|
|
|
if (root_) {
|
|
|
|
UpdateViewHierarchy(input, root_);
|
|
|
|
}
|
2013-06-03 19:57:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIScreen::render() {
|
2013-07-21 13:30:47 +02:00
|
|
|
DoRecreateViews();
|
|
|
|
|
2013-06-08 22:41:17 +02:00
|
|
|
if (root_) {
|
|
|
|
UI::LayoutViewHierarchy(*screenManager()->getUIContext(), root_);
|
|
|
|
|
|
|
|
screenManager()->getUIContext()->Begin();
|
2013-06-09 11:18:57 +02:00
|
|
|
DrawBackground(*screenManager()->getUIContext());
|
2013-06-08 22:41:17 +02:00
|
|
|
root_->Draw(*screenManager()->getUIContext());
|
|
|
|
screenManager()->getUIContext()->End();
|
|
|
|
screenManager()->getUIContext()->Flush();
|
|
|
|
}
|
2013-06-03 19:57:40 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 13:04:10 +02:00
|
|
|
bool UIScreen::touch(const TouchInput &touch) {
|
2013-06-08 22:41:17 +02:00
|
|
|
if (root_) {
|
2013-08-12 23:07:27 +02:00
|
|
|
UI::TouchEvent(touch, root_);
|
2014-06-15 13:04:10 +02:00
|
|
|
return true;
|
2013-06-08 22:41:17 +02:00
|
|
|
}
|
2014-06-15 13:04:10 +02:00
|
|
|
return false;
|
2013-06-03 19:57:40 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 13:04:10 +02:00
|
|
|
bool UIScreen::key(const KeyInput &key) {
|
2013-07-08 12:34:39 +02:00
|
|
|
if (root_) {
|
2014-06-15 13:04:10 +02:00
|
|
|
return UI::KeyEvent(key, root_);
|
2013-07-08 12:34:39 +02:00
|
|
|
}
|
2014-06-15 13:04:10 +02:00
|
|
|
return false;
|
2013-07-08 12:34:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 13:04:10 +02:00
|
|
|
bool UIDialogScreen::key(const KeyInput &key) {
|
2015-02-03 00:10:38 +01:00
|
|
|
bool retval = UIScreen::key(key);
|
2015-08-28 17:05:11 +02:00
|
|
|
if (!retval && (key.flags & KEY_DOWN) && UI::IsEscapeKey(key)) {
|
2013-10-28 16:05:21 +01:00
|
|
|
if (finished_) {
|
|
|
|
ELOG("Screen already finished");
|
|
|
|
} else {
|
|
|
|
finished_ = true;
|
|
|
|
screenManager()->finishDialog(this, DR_BACK);
|
|
|
|
}
|
2014-06-15 13:04:10 +02:00
|
|
|
return true;
|
2013-08-13 00:06:23 +02:00
|
|
|
}
|
2015-02-03 00:10:38 +01:00
|
|
|
return retval;
|
2013-08-13 00:06:23 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 13:04:10 +02:00
|
|
|
bool UIScreen::axis(const AxisInput &axis) {
|
2013-08-12 23:07:27 +02:00
|
|
|
// Simple translation of hat to keys for Shield and other modern pads.
|
|
|
|
// TODO: Use some variant of keymap?
|
|
|
|
int flags = 0;
|
|
|
|
if (axis.axisId == JOYSTICK_AXIS_HAT_X) {
|
|
|
|
if (axis.value < -0.7f)
|
|
|
|
flags |= PAD_BUTTON_LEFT;
|
|
|
|
if (axis.value > 0.7f)
|
|
|
|
flags |= PAD_BUTTON_RIGHT;
|
|
|
|
}
|
|
|
|
if (axis.axisId == JOYSTICK_AXIS_HAT_Y) {
|
|
|
|
if (axis.value < -0.7f)
|
|
|
|
flags |= PAD_BUTTON_UP;
|
|
|
|
if (axis.value > 0.7f)
|
|
|
|
flags |= PAD_BUTTON_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yeah yeah, this should be table driven..
|
|
|
|
int pressed = flags & ~hatDown_;
|
|
|
|
int released = ~flags & hatDown_;
|
|
|
|
if (pressed & PAD_BUTTON_LEFT) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_LEFT, KEY_DOWN));
|
|
|
|
if (pressed & PAD_BUTTON_RIGHT) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_RIGHT, KEY_DOWN));
|
|
|
|
if (pressed & PAD_BUTTON_UP) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_UP, KEY_DOWN));
|
|
|
|
if (pressed & PAD_BUTTON_DOWN) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_DOWN, KEY_DOWN));
|
|
|
|
if (released & PAD_BUTTON_LEFT) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_LEFT, KEY_UP));
|
|
|
|
if (released & PAD_BUTTON_RIGHT) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_RIGHT, KEY_UP));
|
|
|
|
if (released & PAD_BUTTON_UP) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_UP, KEY_UP));
|
|
|
|
if (released & PAD_BUTTON_DOWN) key(KeyInput(DEVICE_ID_KEYBOARD, NKCODE_DPAD_DOWN, KEY_UP));
|
|
|
|
hatDown_ = flags;
|
2013-08-15 22:13:57 +02:00
|
|
|
if (root_) {
|
|
|
|
UI::AxisEvent(axis, root_);
|
2014-06-15 13:04:10 +02:00
|
|
|
return true;
|
2013-08-15 22:13:57 +02:00
|
|
|
}
|
2014-06-15 13:04:10 +02:00
|
|
|
return (pressed & (PAD_BUTTON_LEFT | PAD_BUTTON_RIGHT | PAD_BUTTON_UP | PAD_BUTTON_DOWN)) != 0;
|
2013-08-12 23:07:27 +02:00
|
|
|
}
|
|
|
|
|
2013-06-03 19:57:40 +02:00
|
|
|
UI::EventReturn UIScreen::OnBack(UI::EventParams &e) {
|
2013-10-25 13:07:13 +02:00
|
|
|
screenManager()->finishDialog(this, DR_BACK);
|
2013-06-03 19:57:40 +02:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
2013-07-16 00:25:08 +02:00
|
|
|
|
2013-12-05 14:15:18 +01:00
|
|
|
UI::EventReturn UIScreen::OnOK(UI::EventParams &e) {
|
|
|
|
screenManager()->finishDialog(this, DR_OK);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn UIScreen::OnCancel(UI::EventParams &e) {
|
|
|
|
screenManager()->finishDialog(this, DR_CANCEL);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-08-16 12:51:57 +02:00
|
|
|
PopupScreen::PopupScreen(std::string title, std::string button1, std::string button2)
|
2014-07-18 12:22:33 +02:00
|
|
|
: box_(0), title_(title), defaultButton_(NULL) {
|
2015-07-02 14:20:04 +02:00
|
|
|
I18NCategory *di = GetI18NCategory("Dialog");
|
2013-11-29 11:33:51 +01:00
|
|
|
if (!button1.empty())
|
2015-07-02 14:20:04 +02:00
|
|
|
button1_ = di->T(button1.c_str());
|
2013-11-29 11:33:51 +01:00
|
|
|
if (!button2.empty())
|
2015-07-02 14:20:04 +02:00
|
|
|
button2_ = di->T(button2.c_str());
|
2013-08-31 05:33:23 +08:00
|
|
|
}
|
2013-08-20 13:03:42 +02:00
|
|
|
|
2014-06-15 13:04:10 +02:00
|
|
|
bool PopupScreen::touch(const TouchInput &touch) {
|
2013-08-20 13:03:42 +02:00
|
|
|
if (!box_ || (touch.flags & TOUCH_DOWN) == 0 || touch.id != 0) {
|
2014-06-15 13:04:10 +02:00
|
|
|
return UIDialogScreen::touch(touch);
|
2013-08-20 13:03:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!box_->GetBounds().Contains(touch.x, touch.y))
|
2013-10-25 13:07:13 +02:00
|
|
|
screenManager()->finishDialog(this, DR_BACK);
|
2013-08-20 13:03:42 +02:00
|
|
|
|
2014-06-15 13:04:10 +02:00
|
|
|
return UIDialogScreen::touch(touch);
|
2013-08-20 13:03:42 +02:00
|
|
|
}
|
2013-07-16 00:25:08 +02:00
|
|
|
|
2014-07-18 12:22:33 +02:00
|
|
|
bool PopupScreen::key(const KeyInput &key) {
|
|
|
|
if (key.flags & KEY_DOWN) {
|
2014-07-19 22:57:09 -07:00
|
|
|
if (key.keyCode == NKCODE_ENTER && defaultButton_) {
|
2014-07-18 12:22:33 +02:00
|
|
|
UI::EventParams e;
|
|
|
|
defaultButton_->OnClick.Trigger(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UIDialogScreen::key(key);
|
|
|
|
}
|
|
|
|
|
2013-07-16 00:25:08 +02:00
|
|
|
void PopupScreen::CreateViews() {
|
|
|
|
using namespace UI;
|
|
|
|
|
2014-02-10 12:35:36 +01:00
|
|
|
UIContext &dc = *screenManager()->getUIContext();
|
|
|
|
|
2013-07-16 00:25:08 +02:00
|
|
|
root_ = new AnchorLayout(new LayoutParams(FILL_PARENT, FILL_PARENT));
|
2013-08-10 23:03:12 +02:00
|
|
|
|
2014-02-10 12:35:36 +01:00
|
|
|
float yres = screenManager()->getUIContext()->GetBounds().h;
|
|
|
|
|
2013-10-28 16:05:21 +01:00
|
|
|
box_ = new LinearLayout(ORIENT_VERTICAL,
|
2014-02-10 12:35:36 +01:00
|
|
|
new AnchorLayoutParams(550, FillVertical() ? yres - 30 : WRAP_CONTENT, dc.GetBounds().centerX(), dc.GetBounds().centerY(), NONE, NONE, true));
|
2013-07-17 01:03:29 +02:00
|
|
|
|
2013-08-20 13:03:42 +02:00
|
|
|
root_->Add(box_);
|
|
|
|
box_->SetBG(UI::Drawable(0xFF303030));
|
|
|
|
box_->SetHasDropShadow(true);
|
2013-07-16 00:25:08 +02:00
|
|
|
|
2013-08-14 23:29:27 +02:00
|
|
|
View *title = new PopupHeader(title_);
|
2013-08-20 13:03:42 +02:00
|
|
|
box_->Add(title);
|
2013-07-16 00:25:08 +02:00
|
|
|
|
2013-08-20 13:03:42 +02:00
|
|
|
CreatePopupContents(box_);
|
2014-03-03 12:55:29 +01:00
|
|
|
root_->SetDefaultFocusView(box_);
|
2013-07-16 00:25:08 +02:00
|
|
|
|
2013-11-29 11:33:51 +01:00
|
|
|
if (ShowButtons() && !button1_.empty()) {
|
2013-08-15 22:13:57 +02:00
|
|
|
// And the two buttons at the bottom.
|
2013-08-16 16:47:25 +02:00
|
|
|
LinearLayout *buttonRow = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(200, WRAP_CONTENT));
|
|
|
|
buttonRow->SetSpacing(0);
|
|
|
|
Margins buttonMargins(5, 5);
|
2013-08-16 12:51:57 +02:00
|
|
|
|
|
|
|
// Adjust button order to the platform default.
|
|
|
|
#if defined(_WIN32)
|
2014-07-18 12:22:33 +02:00
|
|
|
defaultButton_ = buttonRow->Add(new Button(button1_, new LinearLayoutParams(1.0f, buttonMargins)));
|
|
|
|
defaultButton_->OnClick.Handle(this, &PopupScreen::OnOK);
|
2013-08-16 16:47:25 +02:00
|
|
|
if (!button2_.empty())
|
|
|
|
buttonRow->Add(new Button(button2_, new LinearLayoutParams(1.0f, buttonMargins)))->OnClick.Handle(this, &PopupScreen::OnCancel);
|
2013-08-16 12:51:57 +02:00
|
|
|
#else
|
2013-08-16 16:47:25 +02:00
|
|
|
if (!button2_.empty())
|
|
|
|
buttonRow->Add(new Button(button2_, new LinearLayoutParams(1.0f)))->OnClick.Handle(this, &PopupScreen::OnCancel);
|
2014-07-18 12:22:33 +02:00
|
|
|
defaultButton_ = buttonRow->Add(new Button(button1_, new LinearLayoutParams(1.0f)));
|
|
|
|
defaultButton_->OnClick.Handle(this, &PopupScreen::OnOK);
|
2013-08-16 12:51:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-08-20 13:03:42 +02:00
|
|
|
box_->Add(buttonRow);
|
2013-08-15 22:13:57 +02:00
|
|
|
}
|
2013-07-16 00:25:08 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 12:51:57 +02:00
|
|
|
void MessagePopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
|
2014-09-14 19:51:13 -04:00
|
|
|
std::vector<std::string> messageLines;
|
|
|
|
SplitString(message_, '\n', messageLines);
|
|
|
|
for (const auto& lineOfText : messageLines)
|
|
|
|
parent->Add(new UI::TextView(lineOfText, ALIGN_LEFT | ALIGN_VCENTER, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessagePopupScreen::OnCompleted(DialogResult result) {
|
|
|
|
if (result == DR_OK) {
|
|
|
|
if (callback_)
|
|
|
|
callback_(true);
|
|
|
|
} else {
|
|
|
|
if (callback_)
|
|
|
|
callback_(false);
|
|
|
|
}
|
2013-08-16 12:51:57 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 00:25:08 +02:00
|
|
|
UI::EventReturn PopupScreen::OnOK(UI::EventParams &e) {
|
2013-08-16 12:51:57 +02:00
|
|
|
OnCompleted(DR_OK);
|
2013-07-16 00:25:08 +02:00
|
|
|
screenManager()->finishDialog(this, DR_OK);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn PopupScreen::OnCancel(UI::EventParams &e) {
|
2013-08-16 12:51:57 +02:00
|
|
|
OnCompleted(DR_CANCEL);
|
2013-07-16 00:25:08 +02:00
|
|
|
screenManager()->finishDialog(this, DR_CANCEL);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
|
|
|
|
using namespace UI;
|
|
|
|
|
2013-12-11 09:32:14 +01:00
|
|
|
listView_ = parent->Add(new ListView(&adaptor_)); //, new LinearLayoutParams(1.0)));
|
2014-02-10 12:35:36 +01:00
|
|
|
listView_->SetMaxHeight(screenManager()->getUIContext()->GetBounds().h - 140);
|
2013-07-16 00:25:08 +02:00
|
|
|
listView_->OnChoice.Handle(this, &ListPopupScreen::OnListChoice);
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn ListPopupScreen::OnListChoice(UI::EventParams &e) {
|
|
|
|
adaptor_.SetSelected(e.a);
|
2013-08-15 22:43:42 +02:00
|
|
|
if (callback_)
|
2013-10-25 13:07:13 +02:00
|
|
|
callback_(adaptor_.GetSelected());
|
2013-08-15 22:13:57 +02:00
|
|
|
screenManager()->finishDialog(this, DR_OK);
|
2013-08-16 12:51:57 +02:00
|
|
|
OnCompleted(DR_OK);
|
2013-07-16 00:25:08 +02:00
|
|
|
OnChoice.Dispatch(e);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:38:12 +02:00
|
|
|
namespace UI {
|
|
|
|
|
|
|
|
UI::EventReturn PopupMultiChoice::HandleClick(UI::EventParams &e) {
|
2015-06-27 09:41:53 -07:00
|
|
|
restoreFocus_ = HasFocus();
|
|
|
|
|
2015-09-23 12:43:45 +02:00
|
|
|
I18NCategory *category = category_ ? GetI18NCategory(category_) : nullptr;
|
|
|
|
|
2013-09-07 13:38:12 +02:00
|
|
|
std::vector<std::string> choices;
|
|
|
|
for (int i = 0; i < numChoices_; i++) {
|
2015-09-23 12:43:45 +02:00
|
|
|
choices.push_back(category ? category->T(choices_[i]) : choices_[i]);
|
2013-09-07 13:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Screen *popupScreen = new ListPopupScreen(text_, choices, *value_ - minVal_,
|
|
|
|
std::bind(&PopupMultiChoice::ChoiceCallback, this, placeholder::_1));
|
|
|
|
screenManager_->push(popupScreen);
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2014-07-22 09:54:24 +02:00
|
|
|
void PopupMultiChoice::Update(const InputState &input_state) {
|
|
|
|
UpdateText();
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:38:12 +02:00
|
|
|
void PopupMultiChoice::UpdateText() {
|
2015-09-23 12:43:45 +02:00
|
|
|
I18NCategory *category = GetI18NCategory(category_);
|
2013-09-11 22:20:30 +02:00
|
|
|
// Clamp the value to be safe.
|
|
|
|
if (*value_ < minVal_ || *value_ > minVal_ + numChoices_ - 1) {
|
|
|
|
valueText_ = "(invalid choice)"; // Shouldn't happen. Should be no need to translate this.
|
|
|
|
} else {
|
2015-09-23 12:43:45 +02:00
|
|
|
valueText_ = category ? category->T(choices_[*value_ - minVal_]) : choices_[*value_ - minVal_];
|
2013-09-11 22:20:30 +02:00
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PopupMultiChoice::ChoiceCallback(int num) {
|
|
|
|
if (num != -1) {
|
|
|
|
*value_ = num + minVal_;
|
|
|
|
UpdateText();
|
|
|
|
|
|
|
|
UI::EventParams e;
|
|
|
|
e.v = this;
|
|
|
|
e.a = num;
|
|
|
|
OnChoice.Trigger(e);
|
2015-06-27 09:41:53 -07:00
|
|
|
|
|
|
|
if (restoreFocus_) {
|
|
|
|
SetFocusedView(this);
|
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupMultiChoice::Draw(UIContext &dc) {
|
2013-12-11 20:01:00 +01:00
|
|
|
Style style = dc.theme->itemStyle;
|
|
|
|
if (!IsEnabled()) {
|
|
|
|
style = dc.theme->itemDisabledStyle;
|
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
Choice::Draw(dc);
|
|
|
|
int paddingX = 12;
|
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
2013-12-11 20:01:00 +01:00
|
|
|
dc.DrawText(valueText_.c_str(), bounds_.x2() - paddingX, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
|
2013-09-07 13:38:12 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 16:50:03 +01:00
|
|
|
PopupSliderChoice::PopupSliderChoice(int *value, int minValue, int maxValue, const std::string &text, ScreenManager *screenManager, LayoutParams *layoutParams)
|
2014-07-17 00:48:47 +10:00
|
|
|
: Choice(text, "", false, layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), step_(1), screenManager_(screenManager) {
|
2013-12-02 16:50:03 +01:00
|
|
|
OnClick.Handle(this, &PopupSliderChoice::HandleClick);
|
|
|
|
}
|
|
|
|
|
2014-05-05 23:39:57 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-12-02 16:50:03 +01:00
|
|
|
PopupSliderChoiceFloat::PopupSliderChoiceFloat(float *value, float minValue, float maxValue, const std::string &text, ScreenManager *screenManager, LayoutParams *layoutParams)
|
2014-08-16 15:14:38 -04:00
|
|
|
: Choice(text, "", false, layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), step_(1.0f), screenManager_(screenManager) {
|
2013-12-02 16:50:03 +01:00
|
|
|
OnClick.Handle(this, &PopupSliderChoiceFloat::HandleClick);
|
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
|
2014-08-16 15:07:06 -04:00
|
|
|
PopupSliderChoiceFloat::PopupSliderChoiceFloat(float *value, float minValue, float maxValue, const std::string &text, float step, ScreenManager *screenManager, LayoutParams *layoutParams)
|
|
|
|
: Choice(text, "", false, layoutParams), value_(value), minValue_(minValue), maxValue_(maxValue), step_(step), screenManager_(screenManager) {
|
|
|
|
OnClick.Handle(this, &PopupSliderChoiceFloat::HandleClick);
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:38:12 +02:00
|
|
|
EventReturn PopupSliderChoice::HandleClick(EventParams &e) {
|
2015-06-27 09:48:24 -07:00
|
|
|
restoreFocus_ = HasFocus();
|
|
|
|
|
2014-05-05 23:39:57 -04:00
|
|
|
SliderPopupScreen *popupScreen = new SliderPopupScreen(value_, minValue_, maxValue_, text_, step_);
|
2013-12-02 16:50:03 +01:00
|
|
|
popupScreen->OnChange.Handle(this, &PopupSliderChoice::HandleChange);
|
2013-09-07 13:38:12 +02:00
|
|
|
screenManager_->push(popupScreen);
|
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-12-02 16:50:03 +01:00
|
|
|
EventReturn PopupSliderChoice::HandleChange(EventParams &e) {
|
|
|
|
e.v = this;
|
|
|
|
OnChange.Trigger(e);
|
2015-06-27 09:48:24 -07:00
|
|
|
|
|
|
|
if (restoreFocus_) {
|
|
|
|
SetFocusedView(this);
|
|
|
|
}
|
2013-12-02 16:50:03 +01:00
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
|
|
|
|
void PopupSliderChoice::Draw(UIContext &dc) {
|
2013-12-11 20:01:00 +01:00
|
|
|
Style style = dc.theme->itemStyle;
|
|
|
|
if (!IsEnabled()) {
|
|
|
|
style = dc.theme->itemDisabledStyle;
|
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
Choice::Draw(dc);
|
|
|
|
char temp[32];
|
|
|
|
sprintf(temp, "%i", *value_);
|
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
2013-12-11 20:01:00 +01:00
|
|
|
dc.DrawText(temp, bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
|
2013-09-07 13:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EventReturn PopupSliderChoiceFloat::HandleClick(EventParams &e) {
|
2015-06-27 09:48:24 -07:00
|
|
|
restoreFocus_ = HasFocus();
|
|
|
|
|
2014-08-16 15:07:06 -04:00
|
|
|
SliderFloatPopupScreen *popupScreen = new SliderFloatPopupScreen(value_, minValue_, maxValue_, text_, step_);
|
2013-12-02 16:50:03 +01:00
|
|
|
popupScreen->OnChange.Handle(this, &PopupSliderChoiceFloat::HandleChange);
|
2013-09-07 13:38:12 +02:00
|
|
|
screenManager_->push(popupScreen);
|
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-12-02 16:50:03 +01:00
|
|
|
EventReturn PopupSliderChoiceFloat::HandleChange(EventParams &e) {
|
|
|
|
e.v = this;
|
|
|
|
OnChange.Trigger(e);
|
2015-06-27 09:48:24 -07:00
|
|
|
|
|
|
|
if (restoreFocus_) {
|
|
|
|
SetFocusedView(this);
|
|
|
|
}
|
2013-12-02 16:50:03 +01:00
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:38:12 +02:00
|
|
|
void PopupSliderChoiceFloat::Draw(UIContext &dc) {
|
2013-12-11 20:01:00 +01:00
|
|
|
Style style = dc.theme->itemStyle;
|
|
|
|
if (!IsEnabled()) {
|
|
|
|
style = dc.theme->itemDisabledStyle;
|
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
Choice::Draw(dc);
|
|
|
|
char temp[32];
|
|
|
|
sprintf(temp, "%2.2f", *value_);
|
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
2013-12-11 20:01:00 +01:00
|
|
|
dc.DrawText(temp, bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
|
2013-09-07 13:38:12 +02:00
|
|
|
}
|
|
|
|
|
2013-12-06 15:01:34 +01:00
|
|
|
EventReturn SliderPopupScreen::OnDecrease(EventParams ¶ms) {
|
2014-05-05 23:39:57 -04:00
|
|
|
sliderValue_ -= step_;
|
2013-12-06 15:01:34 +01:00
|
|
|
slider_->Clamp();
|
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventReturn SliderPopupScreen::OnIncrease(EventParams ¶ms) {
|
2014-05-05 23:39:57 -04:00
|
|
|
sliderValue_ += step_;
|
2013-12-06 15:01:34 +01:00
|
|
|
slider_->Clamp();
|
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-07-18 10:25:30 +02:00
|
|
|
void SliderPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
|
|
|
|
using namespace UI;
|
2013-08-14 23:29:27 +02:00
|
|
|
sliderValue_ = *value_;
|
2013-12-06 15:01:34 +01:00
|
|
|
LinearLayout *lin = parent->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(UI::Margins(10, 5))));
|
2014-05-05 23:39:57 -04:00
|
|
|
slider_ = new Slider(&sliderValue_, minValue_, maxValue_, step_, new LinearLayoutParams(1.0f));
|
2013-12-06 15:01:34 +01:00
|
|
|
lin->Add(slider_);
|
|
|
|
lin->Add(new Button(" - "))->OnClick.Handle(this, &SliderPopupScreen::OnDecrease);
|
|
|
|
lin->Add(new Button(" + "))->OnClick.Handle(this, &SliderPopupScreen::OnIncrease);
|
|
|
|
|
2013-08-20 15:25:03 +02:00
|
|
|
UI::SetFocusedView(slider_);
|
2013-07-18 10:25:30 +02:00
|
|
|
}
|
2013-07-20 13:54:09 +02:00
|
|
|
|
|
|
|
void SliderFloatPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
|
|
|
|
using namespace UI;
|
2013-08-14 23:29:27 +02:00
|
|
|
sliderValue_ = *value_;
|
2014-08-16 15:07:06 -04:00
|
|
|
LinearLayout *lin = parent->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(UI::Margins(10, 5))));
|
|
|
|
slider_ = new SliderFloat(&sliderValue_, minValue_, maxValue_, new LinearLayoutParams(1.0f));
|
|
|
|
lin->Add(slider_);
|
|
|
|
lin->Add(new Button(" - "))->OnClick.Handle(this, &SliderFloatPopupScreen::OnDecrease);
|
|
|
|
lin->Add(new Button(" + "))->OnClick.Handle(this, &SliderFloatPopupScreen::OnIncrease);
|
|
|
|
// slider_ = parent->Add(new SliderFloat(&sliderValue_, minValue_, maxValue_, new LinearLayoutParams(UI::Margins(10, 5))));
|
2013-08-20 15:25:03 +02:00
|
|
|
UI::SetFocusedView(slider_);
|
2013-08-14 23:29:27 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 15:07:06 -04:00
|
|
|
EventReturn SliderFloatPopupScreen::OnDecrease(EventParams ¶ms) {
|
|
|
|
sliderValue_ -= step_;
|
|
|
|
slider_->Clamp();
|
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventReturn SliderFloatPopupScreen::OnIncrease(EventParams ¶ms) {
|
|
|
|
sliderValue_ += step_;
|
|
|
|
slider_->Clamp();
|
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
2013-08-16 12:51:57 +02:00
|
|
|
void SliderPopupScreen::OnCompleted(DialogResult result) {
|
2013-12-02 16:50:03 +01:00
|
|
|
if (result == DR_OK) {
|
2013-08-16 12:51:57 +02:00
|
|
|
*value_ = sliderValue_;
|
2013-12-02 16:50:03 +01:00
|
|
|
EventParams e;
|
|
|
|
e.v = 0;
|
|
|
|
e.a = *value_;
|
|
|
|
OnChange.Trigger(e);
|
|
|
|
}
|
2013-08-14 23:29:27 +02:00
|
|
|
}
|
|
|
|
|
2013-08-16 12:51:57 +02:00
|
|
|
void SliderFloatPopupScreen::OnCompleted(DialogResult result) {
|
2013-12-02 16:50:03 +01:00
|
|
|
if (result == DR_OK) {
|
2013-08-16 12:51:57 +02:00
|
|
|
*value_ = sliderValue_;
|
2013-12-02 16:50:03 +01:00
|
|
|
EventParams e;
|
|
|
|
e.v = 0;
|
|
|
|
e.a = (int)*value_;
|
|
|
|
e.f = *value_;
|
|
|
|
OnChange.Trigger(e);
|
|
|
|
}
|
2013-07-20 13:54:09 +02:00
|
|
|
}
|
2013-09-07 13:38:12 +02:00
|
|
|
|
2014-07-21 11:59:05 +02:00
|
|
|
PopupTextInputChoice::PopupTextInputChoice(std::string *value, const std::string &title, const std::string &placeholder, int maxLen, ScreenManager *screenManager, LayoutParams *layoutParams)
|
|
|
|
: Choice(title, "", false, layoutParams), value_(value), placeHolder_(placeholder), screenManager_(screenManager), maxLen_(maxLen) {
|
2014-07-18 11:03:18 +02:00
|
|
|
OnClick.Handle(this, &PopupTextInputChoice::HandleClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventReturn PopupTextInputChoice::HandleClick(EventParams &e) {
|
2015-06-27 09:48:24 -07:00
|
|
|
restoreFocus_ = HasFocus();
|
|
|
|
|
2014-07-21 11:59:05 +02:00
|
|
|
TextEditPopupScreen *popupScreen = new TextEditPopupScreen(value_, placeHolder_, text_, maxLen_);
|
2014-07-18 11:03:18 +02:00
|
|
|
popupScreen->OnChange.Handle(this, &PopupTextInputChoice::HandleChange);
|
|
|
|
screenManager_->push(popupScreen);
|
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PopupTextInputChoice::Draw(UIContext &dc) {
|
|
|
|
Style style = dc.theme->itemStyle;
|
|
|
|
if (!IsEnabled()) {
|
|
|
|
style = dc.theme->itemDisabledStyle;
|
|
|
|
}
|
|
|
|
Choice::Draw(dc);
|
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
|
|
|
dc.DrawText(value_->c_str(), bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventReturn PopupTextInputChoice::HandleChange(EventParams &e) {
|
|
|
|
e.v = this;
|
|
|
|
OnChange.Trigger(e);
|
2015-06-27 09:48:24 -07:00
|
|
|
|
|
|
|
if (restoreFocus_) {
|
|
|
|
SetFocusedView(this);
|
|
|
|
}
|
2014-07-18 11:03:18 +02:00
|
|
|
return EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
|
|
|
|
using namespace UI;
|
|
|
|
|
|
|
|
textEditValue_ = *value_;
|
|
|
|
LinearLayout *lin = parent->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams((UI::Size)300, WRAP_CONTENT)));
|
|
|
|
edit_ = new TextEdit(textEditValue_, placeholder_, new LinearLayoutParams(1.0f));
|
2014-07-21 11:59:05 +02:00
|
|
|
edit_->SetMaxLen(maxLen_);
|
2014-07-18 11:03:18 +02:00
|
|
|
lin->Add(edit_);
|
|
|
|
|
|
|
|
UI::SetFocusedView(edit_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEditPopupScreen::OnCompleted(DialogResult result) {
|
|
|
|
if (result == DR_OK) {
|
|
|
|
*value_ = edit_->GetText();
|
|
|
|
EventParams e;
|
|
|
|
e.v = edit_;
|
|
|
|
OnChange.Trigger(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:42:41 -04:00
|
|
|
void ChoiceWithValueDisplay::Draw(UIContext &dc) {
|
|
|
|
Style style = dc.theme->itemStyle;
|
|
|
|
std::ostringstream valueText;
|
|
|
|
Choice::Draw(dc);
|
|
|
|
dc.SetFontStyle(dc.theme->uiFont);
|
|
|
|
|
2015-09-23 12:43:45 +02:00
|
|
|
I18NCategory *category = GetI18NCategory(category_);
|
|
|
|
|
2014-07-18 18:42:41 -04:00
|
|
|
if (sValue_ != nullptr) {
|
2015-09-23 12:43:45 +02:00
|
|
|
if (category)
|
|
|
|
valueText << category->T(*sValue_);
|
2014-07-21 18:42:25 +02:00
|
|
|
else
|
|
|
|
valueText << *sValue_;
|
2014-07-18 18:42:41 -04:00
|
|
|
} else if (iValue_ != nullptr) {
|
|
|
|
valueText << *iValue_;
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.DrawText(valueText.str().c_str(), bounds_.x2() - 12, bounds_.centerY(), style.fgColor, ALIGN_RIGHT | ALIGN_VCENTER);
|
|
|
|
}
|
|
|
|
|
2013-10-19 14:24:46 -07:00
|
|
|
} // namespace UI
|