Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Common/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ std::string LineNumberString(const std::string &str) {
return output.str();
}

std::string IndentString(const std::string &str, const std::string &sep, bool skipFirst) {
std::stringstream input(str);
std::stringstream output;
std::string line;

bool doIndent = !skipFirst;
while (std::getline(input, line)) {
if (doIndent) {
output << sep;
}
doIndent = true;
output << line << "\n";
}

return output.str();
}

void SkipSpace(const char **ptr) {
while (**ptr && isspace(**ptr)) {
(*ptr)++;
Expand Down
1 change: 1 addition & 0 deletions Common/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

// Useful for shaders with error messages..
std::string LineNumberString(const std::string &str);
std::string IndentString(const std::string &str, const std::string &sep, bool skipFirst = false);

// Other simple string utilities.

Expand Down
12 changes: 6 additions & 6 deletions Common/UI/UIScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ UIScreen::~UIScreen() {
}

void UIScreen::DoRecreateViews() {
std::lock_guard<std::recursive_mutex> guard(screenManager()->inputLock_);

if (recreateViews_) {
std::lock_guard<std::recursive_mutex> guard(screenManager()->inputLock_);

UI::PersistMap persisted;
bool persisting = root_ != nullptr;
if (persisting) {
Expand Down Expand Up @@ -144,7 +144,7 @@ bool UIScreen::touch(const TouchInput &touch) {
std::vector<UI::View *> views;
root_->Query(touch.x, touch.y, views);
for (auto view : views) {
INFO_LOG(SYSTEM, "%s", view->Describe().c_str());
INFO_LOG(SYSTEM, "%s", view->DescribeLog().c_str());
}
}

Expand Down Expand Up @@ -655,7 +655,7 @@ void SliderPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {

char temp[64];
sprintf(temp, "%d", sliderValue_);
edit_ = new TextEdit(temp, "", new LinearLayoutParams(10.0f));
edit_ = new TextEdit(temp, Title(), "", new LinearLayoutParams(10.0f));
edit_->SetMaxLen(16);
edit_->SetTextColor(dc.theme->popupStyle.fgColor);
edit_->SetTextAlign(FLAG_DYNAMIC_ASCII);
Expand Down Expand Up @@ -689,7 +689,7 @@ void SliderFloatPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {

char temp[64];
sprintf(temp, "%0.3f", sliderValue_);
edit_ = new TextEdit(temp, "", new LinearLayoutParams(10.0f));
edit_ = new TextEdit(temp, Title(), "", new LinearLayoutParams(10.0f));
edit_->SetMaxLen(16);
edit_->SetTextColor(dc.theme->popupStyle.fgColor);
edit_->SetTextAlign(FLAG_DYNAMIC_ASCII);
Expand Down Expand Up @@ -818,7 +818,7 @@ void TextEditPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {

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));
edit_ = new TextEdit(textEditValue_, Title(), placeholder_, new LinearLayoutParams(1.0f));
edit_->SetMaxLen(maxLen_);
edit_->SetTextColor(dc.theme->popupStyle.fgColor);
lin->Add(edit_);
Expand Down
1 change: 1 addition & 0 deletions Common/UI/UIScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class PopupScreen : public UIDialogScreen {
virtual bool ShowButtons() const { return true; }
virtual bool CanComplete(DialogResult result) { return true; }
virtual void OnCompleted(DialogResult result) {}
const std::string &Title() { return title_; }

virtual void update() override;

Expand Down
64 changes: 60 additions & 4 deletions Common/UI/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Common/Render/DrawBuffer.h"
#include "Common/Render/TextureAtlas.h"
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Data/Text/I18n.h"
#include "Common/UI/UI.h"
#include "Common/UI/View.h"
#include "Common/UI/Context.h"
Expand Down Expand Up @@ -135,7 +136,7 @@ void View::Query(float x, float y, std::vector<View *> &list) {
}
}

std::string View::Describe() const {
std::string View::DescribeLog() const {
return StringFromFormat("%0.1f,%0.1f %0.1fx%0.1f", bounds_.x, bounds_.y, bounds_.w, bounds_.h);
}

Expand Down Expand Up @@ -513,6 +514,11 @@ void Choice::Draw(UIContext &dc) {
}
}

std::string Choice::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
return ReplaceAll(u->T("%1 choice"), "%1", text_);
}

InfoItem::InfoItem(const std::string &text, const std::string &rightText, LayoutParams *layoutParams)
: Item(layoutParams), text_(text), rightText_(rightText) {
// We set the colors later once we have a UIContext.
Expand Down Expand Up @@ -547,6 +553,11 @@ void InfoItem::Draw(UIContext &dc) {
// dc.Draw()->DrawImageCenterTexel(dc.theme->whiteImage, bounds_.x, bounds_.y, bounds_.x2(), bounds_.y + 2, dc.theme->itemDownStyle.bgColor);
}

std::string InfoItem::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
return ReplaceAll(ReplaceAll(u->T("%1: %2"), "%1", text_), "%2", rightText_);
}

ItemHeader::ItemHeader(const std::string &text, LayoutParams *layoutParams)
: Item(layoutParams), text_(text) {
layoutParams_->width = FILL_PARENT;
Expand All @@ -572,6 +583,11 @@ void ItemHeader::GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec hor
dc.MeasureTextRect(dc.theme->uiFontSmall, 1.0f, 1.0f, text_.c_str(), (int)text_.length(), bounds, &w, &h, ALIGN_LEFT | ALIGN_VCENTER);
}

std::string ItemHeader::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
return ReplaceAll(u->T("%1 heading"), "%1", text_);
}

void PopupHeader::Draw(UIContext &dc) {
const float paddingHorizontal = 12;
const float availableWidth = bounds_.w - paddingHorizontal * 2;
Expand Down Expand Up @@ -600,6 +616,11 @@ void PopupHeader::Draw(UIContext &dc) {
}
}

std::string PopupHeader::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
return ReplaceAll(u->T("%1 heading"), "%1", text_);
}

void CheckBox::Toggle() {
if (toggle_) {
*toggle_ = !(*toggle_);
Expand Down Expand Up @@ -642,6 +663,15 @@ void CheckBox::Draw(UIContext &dc) {
dc.SetFontScale(1.0f, 1.0f);
}

std::string CheckBox::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
std::string text = ReplaceAll(u->T("%1 checkbox"), "%1", text_);
if (!smallText_.empty()) {
text += "\n" + smallText_;
}
return text;
}

float CheckBox::CalculateTextScale(const UIContext &dc, float availWidth) const {
float actualWidth, actualHeight;
Bounds availBounds(0, 0, availWidth, bounds_.h);
Expand Down Expand Up @@ -705,6 +735,11 @@ void Button::GetContentDimensions(const UIContext &dc, float &w, float &h) const
h *= scale_;
}

std::string Button::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
return ReplaceAll(u->T("%1 button"), "%1", GetText());
}

void Button::Click() {
Clickable::Click();
UI::PlayUISound(UI::UISound::CONFIRM);
Expand All @@ -729,7 +764,7 @@ void Button::Draw(UIContext &dc) {
}
dc.SetFontStyle(dc.theme->uiFont);
dc.SetFontScale(scale_, scale_);
if (imageID_.isValid() && text_.empty()) {
if (imageID_.isValid() && (ignoreText_ || text_.empty())) {
dc.Draw()->DrawImage(imageID_, bounds_.centerX(), bounds_.centerY(), scale_, 0xFFFFFFFF, ALIGN_CENTER);
} else if (!text_.empty()) {
dc.DrawText(text_.c_str(), bounds_.centerX(), bounds_.centerY(), style.fgColor, ALIGN_CENTER);
Expand Down Expand Up @@ -812,8 +847,8 @@ void TextView::Draw(UIContext &dc) {
}
}

TextEdit::TextEdit(const std::string &text, const std::string &placeholderText, LayoutParams *layoutParams)
: View(layoutParams), text_(text), undo_(text), placeholderText_(placeholderText),
TextEdit::TextEdit(const std::string &text, const std::string &title, const std::string &placeholderText, LayoutParams *layoutParams)
: View(layoutParams), text_(text), title_(title), undo_(text), placeholderText_(placeholderText),
textColor_(0xFFFFFFFF), maxLen_(255) {
caret_ = (int)text_.size();
}
Expand Down Expand Up @@ -861,6 +896,11 @@ void TextEdit::GetContentDimensions(const UIContext &dc, float &w, float &h) con
h += 2;
}

std::string TextEdit::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
return ReplaceAll(u->T("%1 text field"), "%1", GetText());
}

// Handles both windows and unix line endings.
static std::string FirstLine(const std::string &text) {
size_t pos = text.find("\r\n");
Expand Down Expand Up @@ -1037,6 +1077,12 @@ void ProgressBar::Draw(UIContext &dc) {
dc.DrawTextRect(temp, bounds_, 0xFFFFFFFF, ALIGN_CENTER);
}

std::string ProgressBar::DescribeText() const {
auto u = GetI18NCategory("UI Elements");
float percent = progress_ * 100.0f;
return ReplaceAll(u->T("Progress: %1%"), "%1", StringFromInt((int)percent));
}

void Spinner::GetContentDimensions(const UIContext &dc, float &w, float &h) const {
w = 48;
h = 48;
Expand Down Expand Up @@ -1181,6 +1227,12 @@ void Slider::Draw(UIContext &dc) {
dc.DrawText(temp, bounds_.x2() - 22, bounds_.centerY(), dc.theme->popupStyle.fgColor, ALIGN_CENTER | FLAG_DYNAMIC_ASCII);
}

std::string Slider::DescribeText() const {
if (showPercent_)
return StringFromFormat("%i%% / %i%%", *value_, maxValue_);
return StringFromFormat("%i / %i", *value_, maxValue_);
}

void Slider::Update() {
View::Update();
if (repeat_ >= 0) {
Expand Down Expand Up @@ -1291,6 +1343,10 @@ void SliderFloat::Draw(UIContext &dc) {
dc.DrawText(temp, bounds_.x2() - 22, bounds_.centerY(), dc.theme->popupStyle.fgColor, ALIGN_CENTER);
}

std::string SliderFloat::DescribeText() const {
return StringFromFormat("%0.2f / %0.2f", *value_, maxValue_);
}

void SliderFloat::Update() {
View::Update();
if (repeat_ >= 0) {
Expand Down
Loading