From 7bb58ce5a01bc2388b6fe22a1f92ff50e4af0577 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 29 Aug 2021 16:59:55 -0700 Subject: [PATCH] UI: Refocus better on control mapping change. This is less awkward, since it keeps you closer to where you were before. --- UI/ControlMappingScreen.cpp | 54 ++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/UI/ControlMappingScreen.cpp b/UI/ControlMappingScreen.cpp index a827d9d409..3645747dba 100644 --- a/UI/ControlMappingScreen.cpp +++ b/UI/ControlMappingScreen.cpp @@ -46,7 +46,7 @@ class SingleControlMapper : public UI::LinearLayout { public: - SingleControlMapper(ControlMappingScreen *ctrlScreen, int pspKey, std::string keyName, ScreenManager *scrm, UI::LinearLayoutParams *layoutParams = 0); + SingleControlMapper(int pspKey, std::string keyName, ScreenManager *scrm, UI::LinearLayoutParams *layoutParams = nullptr); void Update() override; int GetPspKey() const { return pspKey_; } @@ -68,17 +68,19 @@ private: ADD, }; - ControlMappingScreen *ctrlScreen_; - Action action_; + UI::Choice *addButton_ = nullptr; + UI::Choice *replaceAllButton_ = nullptr; + std::vector rows_; + Action action_ = NONE; int actionIndex_; int pspKey_; std::string keyName_; ScreenManager *scrm_; - bool refresh_; + bool refresh_ = false; }; -SingleControlMapper::SingleControlMapper(ControlMappingScreen *ctrlScreen, int pspKey, std::string keyName, ScreenManager *scrm, UI::LinearLayoutParams *layoutParams) - : UI::LinearLayout(UI::ORIENT_VERTICAL, layoutParams), ctrlScreen_(ctrlScreen), action_(NONE), pspKey_(pspKey), keyName_(keyName), scrm_(scrm), refresh_(false) { +SingleControlMapper::SingleControlMapper(int pspKey, std::string keyName, ScreenManager *scrm, UI::LinearLayoutParams *layoutParams) + : UI::LinearLayout(UI::ORIENT_VERTICAL, layoutParams), pspKey_(pspKey), keyName_(keyName), scrm_(scrm) { Refresh(); } @@ -117,17 +119,16 @@ void SingleControlMapper::Refresh() { auto iter = keyImages.find(keyName_); // First, look among images. if (iter != keyImages.end()) { - Choice *c = root->Add(new Choice(iter->second, new LinearLayoutParams(leftColumnWidth, itemH))); - c->OnClick.Handle(this, &SingleControlMapper::OnReplaceAll); + replaceAllButton_ = new Choice(iter->second, new LinearLayoutParams(leftColumnWidth, itemH)); } else { // No image? Let's translate. - Choice *c = new Choice(mc->T(keyName_.c_str()), new LinearLayoutParams(leftColumnWidth, itemH)); - c->SetCentered(true); - root->Add(c)->OnClick.Handle(this, &SingleControlMapper::OnReplaceAll); + replaceAllButton_ = new Choice(mc->T(keyName_.c_str()), new LinearLayoutParams(leftColumnWidth, itemH)); + replaceAllButton_->SetCentered(true); } + root->Add(replaceAllButton_)->OnClick.Handle(this, &SingleControlMapper::OnReplaceAll); - Choice *p = root->Add(new Choice(" + ", new LayoutParams(WRAP_CONTENT, itemH))); - p->OnClick.Handle(this, &SingleControlMapper::OnAdd); + addButton_ = root->Add(new Choice(" + ", new LayoutParams(WRAP_CONTENT, itemH))); + addButton_->OnClick.Handle(this, &SingleControlMapper::OnAdd); if (g_Config.bMouseControl) { Choice *p = root->Add(new Choice("M", new LayoutParams(WRAP_CONTENT, itemH))); p->OnClick.Handle(this, &SingleControlMapper::OnAddMouse); @@ -138,22 +139,21 @@ void SingleControlMapper::Refresh() { std::vector mappings; KeyMap::KeyFromPspButton(pspKey_, &mappings, false); + rows_.empty(); for (size_t i = 0; i < mappings.size(); i++) { std::string deviceName = GetDeviceName(mappings[i].deviceId); std::string keyName = KeyMap::GetKeyOrAxisName(mappings[i].keyCode); LinearLayout *row = rightColumn->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT))); row->SetSpacing(1.0f); - - char tagbuf[16]; - sprintf(tagbuf, "%d", (int)i); + rows_.push_back(row); Choice *c = row->Add(new Choice(deviceName + "." + keyName, new LinearLayoutParams(FILL_PARENT, itemH, 1.0f))); - c->SetTag(tagbuf); + c->SetTag(StringFromFormat("%d_Change%d", (int)i, pspKey_)); c->OnClick.Handle(this, &SingleControlMapper::OnReplace); Choice *d = row->Add(new Choice(" X ", new LayoutParams(WRAP_CONTENT, itemH))); - d->SetTag(tagbuf); + d->SetTag(StringFromFormat("%d_Del%d", (int)i, pspKey_)); d->OnClick.Handle(this, &SingleControlMapper::OnDelete); } @@ -168,20 +168,26 @@ void SingleControlMapper::MappedCallback(KeyDef kdf) { switch (action_) { case ADD: KeyMap::SetKeyMapping(pspKey_, kdf, false); + addButton_->SetFocus(); break; case REPLACEALL: KeyMap::SetKeyMapping(pspKey_, kdf, true); + replaceAllButton_->SetFocus(); break; case REPLACEONE: KeyMap::g_controllerMap[pspKey_][actionIndex_] = kdf; KeyMap::g_controllerMapGeneration++; + if (actionIndex_ < rows_.size()) + rows_[actionIndex_]->SetFocus(); + else + SetFocus(); break; default: - ; + SetFocus(); + break; } g_Config.bMapMouse = false; refresh_ = true; - SetFocus(); // After this, we do not exist any more. So the refresh_ = true is probably irrelevant. } @@ -219,7 +225,11 @@ UI::EventReturn SingleControlMapper::OnDelete(UI::EventParams ¶ms) { KeyMap::g_controllerMap[pspKey_].erase(KeyMap::g_controllerMap[pspKey_].begin() + index); KeyMap::g_controllerMapGeneration++; refresh_ = true; - SetFocus(); + + if (index + 1 < rows_.size()) + rows_[index]->SetFocus(); + else + SetFocus(); return UI::EVENT_DONE; } @@ -258,7 +268,7 @@ void ControlMappingScreen::CreateViews() { std::vector mappableKeys = KeyMap::GetMappableKeys(); for (size_t i = 0; i < mappableKeys.size(); i++) { SingleControlMapper *mapper = rightColumn->Add( - new SingleControlMapper(this, mappableKeys[i].key, mappableKeys[i].name, screenManager(), + new SingleControlMapper(mappableKeys[i].key, mappableKeys[i].name, screenManager(), new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT))); mapper->SetTag(StringFromFormat("KeyMap%s", mappableKeys[i].name)); mappers_.push_back(mapper);