2013-08-16 14:48:43 +00:00
|
|
|
// Copyright (c) 2013- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "i18n/i18n.h"
|
|
|
|
#include "input/keycodes.h"
|
|
|
|
#include "input/input_state.h"
|
|
|
|
#include "ui/ui.h"
|
|
|
|
#include "ui/ui_context.h"
|
2013-08-17 08:34:38 +00:00
|
|
|
#include "ui/view.h"
|
|
|
|
#include "ui/viewgroup.h"
|
2013-08-16 14:48:43 +00:00
|
|
|
|
|
|
|
#include "Core/HLE/sceCtrl.h"
|
|
|
|
#include "Common/KeyMap.h"
|
|
|
|
#include "Core/Config.h"
|
|
|
|
#include "UI/ui_atlas.h"
|
|
|
|
#include "UI/ControlMappingScreen.h"
|
|
|
|
#include "UI/UIShader.h"
|
|
|
|
|
|
|
|
extern void DrawBackground(float alpha);
|
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
class ControlMapper : public UI::LinearLayout {
|
|
|
|
public:
|
|
|
|
ControlMapper(int pspKey, std::string keyName, ScreenManager *scrm, UI::LinearLayoutParams *layoutParams = 0);
|
|
|
|
|
|
|
|
virtual void Update(const InputState &input);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Refresh();
|
|
|
|
|
|
|
|
UI::EventReturn OnAdd(UI::EventParams ¶ms);
|
|
|
|
UI::EventReturn OnDelete(UI::EventParams ¶ms);
|
|
|
|
UI::EventReturn OnReplace(UI::EventParams ¶ms);
|
|
|
|
UI::EventReturn OnReplaceAll(UI::EventParams ¶ms);
|
|
|
|
|
|
|
|
void MappedCallback(KeyDef key);
|
|
|
|
|
|
|
|
enum Action {
|
|
|
|
NONE,
|
|
|
|
REPLACEONE,
|
|
|
|
REPLACEALL,
|
|
|
|
ADD,
|
|
|
|
};
|
|
|
|
|
|
|
|
Action action_;
|
|
|
|
int actionIndex_;
|
|
|
|
int pspKey_;
|
|
|
|
std::string keyName_;
|
|
|
|
ScreenManager *scrm_;
|
|
|
|
bool refresh_;
|
|
|
|
};
|
|
|
|
|
|
|
|
ControlMapper::ControlMapper(int pspKey, std::string keyName, ScreenManager *scrm, UI::LinearLayoutParams *layoutParams)
|
|
|
|
: UI::LinearLayout(UI::ORIENT_VERTICAL, layoutParams), action_(NONE), pspKey_(pspKey), keyName_(keyName), scrm_(scrm), refresh_(false) {
|
|
|
|
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlMapper::Update(const InputState &input) {
|
|
|
|
if (refresh_) {
|
|
|
|
refresh_ = false;
|
|
|
|
Refresh();
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
void ControlMapper::Refresh() {
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
using namespace UI;
|
2013-08-16 14:48:43 +00:00
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
LinearLayout *root = Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
|
|
|
|
|
|
|
|
root->Add(new Choice(keyName_, new LinearLayoutParams(200, WRAP_CONTENT)))->OnClick.Handle(this, &ControlMapper::OnReplaceAll);
|
|
|
|
LinearLayout *rightColumn = root->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0f)));
|
2013-08-20 13:40:19 +00:00
|
|
|
rightColumn->SetSpacing(2.0f);
|
2013-08-17 08:34:38 +00:00
|
|
|
std::vector<KeyDef> mappings;
|
|
|
|
KeyMap::KeyFromPspButton(pspKey_, &mappings);
|
2013-08-16 14:48:43 +00:00
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
for (size_t i = 0; i < mappings.size(); i++) {
|
|
|
|
std::string deviceName = GetDeviceName(mappings[i].deviceId);
|
2013-08-17 09:18:45 +00:00
|
|
|
std::string keyName = KeyMap::GetKeyOrAxisName(mappings[i].keyCode);
|
2013-08-17 08:34:38 +00:00
|
|
|
int image = -1;
|
|
|
|
|
|
|
|
LinearLayout *row = rightColumn->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
|
|
|
|
|
|
|
|
Choice *c = row->Add(new Choice(deviceName + "." + keyName, new LinearLayoutParams(1.0f)));
|
2013-08-17 11:26:19 +00:00
|
|
|
char tagbuf[16];
|
2013-08-20 17:20:03 +00:00
|
|
|
sprintf(tagbuf, "%i", (int)i);
|
2013-08-17 11:26:19 +00:00
|
|
|
c->SetTag(tagbuf);
|
2013-08-17 08:34:38 +00:00
|
|
|
c->OnClick.Handle(this, &ControlMapper::OnReplace);
|
2013-08-20 17:20:03 +00:00
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
Choice *d = row->Add(new Choice("X"));
|
2013-08-17 11:26:19 +00:00
|
|
|
d->SetTag(tagbuf);
|
2013-08-17 08:34:38 +00:00
|
|
|
d->OnClick.Handle(this, &ControlMapper::OnDelete);
|
2013-08-20 17:20:03 +00:00
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
row->Add(new Choice("+"))->OnClick.Handle(this, &ControlMapper::OnAdd);
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
if (mappings.size() == 0) {
|
|
|
|
// look like an empty line
|
|
|
|
rightColumn->Add(new Choice("", new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT)))->OnClick.Handle(this, &ControlMapper::OnAdd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlMapper::MappedCallback(KeyDef kdf) {
|
|
|
|
switch (action_) {
|
2013-08-17 09:18:45 +00:00
|
|
|
case ADD:
|
|
|
|
KeyMap::SetKeyMapping(pspKey_, kdf, false);
|
|
|
|
break;
|
2013-08-17 08:34:38 +00:00
|
|
|
case REPLACEALL:
|
|
|
|
KeyMap::SetKeyMapping(pspKey_, kdf, true);
|
|
|
|
break;
|
|
|
|
case REPLACEONE:
|
|
|
|
KeyMap::g_controllerMap[pspKey_][actionIndex_] = kdf;
|
|
|
|
break;
|
2013-08-17 11:26:19 +00:00
|
|
|
default:
|
|
|
|
;
|
2013-08-17 08:34:38 +00:00
|
|
|
}
|
|
|
|
refresh_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn ControlMapper::OnReplace(UI::EventParams ¶ms) {
|
|
|
|
actionIndex_ = atoi(params.v->Tag().c_str());
|
|
|
|
action_ = REPLACEONE;
|
|
|
|
scrm_->push(new KeyMappingNewKeyDialog(pspKey_, true, std::bind(&ControlMapper::MappedCallback, this, placeholder::_1)));
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn ControlMapper::OnReplaceAll(UI::EventParams ¶ms) {
|
|
|
|
action_ = REPLACEALL;
|
|
|
|
scrm_->push(new KeyMappingNewKeyDialog(pspKey_, true, std::bind(&ControlMapper::MappedCallback, this, placeholder::_1)));
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn ControlMapper::OnAdd(UI::EventParams ¶ms) {
|
2013-08-17 09:18:45 +00:00
|
|
|
action_ = ADD;
|
|
|
|
scrm_->push(new KeyMappingNewKeyDialog(pspKey_, true, std::bind(&ControlMapper::MappedCallback, this, placeholder::_1)));
|
2013-08-17 08:34:38 +00:00
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn ControlMapper::OnDelete(UI::EventParams ¶ms) {
|
2013-08-17 09:18:45 +00:00
|
|
|
int index = atoi(params.v->Tag().c_str());
|
|
|
|
KeyMap::g_controllerMap[pspKey_].erase(KeyMap::g_controllerMap[pspKey_].begin() + index);
|
2013-08-17 08:34:38 +00:00
|
|
|
refresh_ = true;
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlMappingScreen::CreateViews() {
|
|
|
|
using namespace UI;
|
|
|
|
|
|
|
|
I18NCategory *k = GetI18NCategory("KeyMapping");
|
|
|
|
I18NCategory *g = GetI18NCategory("General");
|
|
|
|
|
|
|
|
root_ = new LinearLayout(ORIENT_HORIZONTAL);
|
|
|
|
|
|
|
|
LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(200, FILL_PARENT));
|
|
|
|
leftColumn->Add(new Choice(k->T("Clear All")))->OnClick.Handle(this, &ControlMappingScreen::OnClearMapping);
|
2013-08-17 22:41:19 +00:00
|
|
|
leftColumn->Add(new Choice(k->T("Default All")))->OnClick.Handle(this, &ControlMappingScreen::OnDefaultMapping);
|
2013-08-17 08:34:38 +00:00
|
|
|
leftColumn->Add(new Spacer(new LinearLayoutParams(1.0f)));
|
|
|
|
leftColumn->Add(new Choice(g->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);
|
|
|
|
|
|
|
|
/*
|
|
|
|
ChoiceStrip *mode = leftColumn->Add(new ChoiceStrip(ORIENT_VERTICAL));
|
|
|
|
mode->AddChoice("Replace");
|
|
|
|
mode->AddChoice("Add");
|
|
|
|
*/
|
|
|
|
ScrollView *rightScroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0f));
|
2013-08-19 20:05:55 +00:00
|
|
|
rightScroll->SetScrollToTop(false);
|
2013-08-17 08:34:38 +00:00
|
|
|
LinearLayout *rightColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0f));
|
|
|
|
rightScroll->Add(rightColumn);
|
|
|
|
|
|
|
|
root_->Add(leftColumn);
|
|
|
|
root_->Add(rightScroll);
|
|
|
|
|
|
|
|
std::vector<KeyMap::KeyMap_IntStrPair> mappableKeys = KeyMap::GetMappableKeys();
|
|
|
|
for (size_t i = 0; i < mappableKeys.size(); i++) {
|
|
|
|
rightColumn->Add(new ControlMapper(mappableKeys[i].key, mappableKeys[i].name, screenManager(), new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UI::EventReturn ControlMappingScreen::OnClearMapping(UI::EventParams ¶ms) {
|
|
|
|
KeyMap::g_controllerMap.clear();
|
2013-08-17 22:41:19 +00:00
|
|
|
RecreateViews();
|
|
|
|
return UI::EVENT_DONE;
|
|
|
|
}
|
2013-08-17 08:34:38 +00:00
|
|
|
|
2013-08-17 22:41:19 +00:00
|
|
|
UI::EventReturn ControlMappingScreen::OnDefaultMapping(UI::EventParams ¶ms) {
|
|
|
|
KeyMap::RestoreDefault();
|
2013-08-17 08:34:38 +00:00
|
|
|
RecreateViews();
|
|
|
|
return UI::EVENT_DONE;
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 22:41:19 +00:00
|
|
|
|
2013-08-16 15:16:11 +00:00
|
|
|
void KeyMappingNewKeyDialog::CreatePopupContents(UI::ViewGroup *parent) {
|
|
|
|
using namespace UI;
|
2013-08-16 14:48:43 +00:00
|
|
|
|
|
|
|
I18NCategory *keyI18N = GetI18NCategory("KeyMapping");
|
|
|
|
I18NCategory *generalI18N = GetI18NCategory("General");
|
|
|
|
|
2013-08-16 15:16:11 +00:00
|
|
|
std::string pspButtonName = KeyMap::GetPspButtonName(this->pspBtn_);
|
2013-08-16 14:48:43 +00:00
|
|
|
|
2013-08-24 07:35:50 +00:00
|
|
|
parent->Add(new TextView(std::string(keyI18N->T("Map a new key for")) + " " + pspButtonName, new LinearLayoutParams(Margins(10,0))));
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyMappingNewKeyDialog::key(const KeyInput &key) {
|
|
|
|
if (key.flags & KEY_DOWN) {
|
2013-08-16 15:16:11 +00:00
|
|
|
if (key.keyCode == NKCODE_EXT_MOUSEBUTTON_1) {
|
|
|
|
return;
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
2013-08-19 22:49:25 +00:00
|
|
|
|
2013-08-17 08:34:38 +00:00
|
|
|
KeyDef kdf(key.deviceId, key.keyCode);
|
2013-08-16 15:16:11 +00:00
|
|
|
screenManager()->finishDialog(this, DR_OK);
|
2013-08-17 08:34:38 +00:00
|
|
|
if (callback_)
|
|
|
|
callback_(kdf);
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyMappingNewKeyDialog::axis(const AxisInput &axis) {
|
|
|
|
if (axis.value > AXIS_BIND_THRESHOLD) {
|
2013-08-17 08:34:38 +00:00
|
|
|
KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, 1));
|
2013-08-16 15:16:11 +00:00
|
|
|
screenManager()->finishDialog(this, DR_OK);
|
2013-08-17 08:34:38 +00:00
|
|
|
if (callback_)
|
|
|
|
callback_(kdf);
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
2013-08-16 15:16:11 +00:00
|
|
|
|
2013-08-16 14:48:43 +00:00
|
|
|
if (axis.value < -AXIS_BIND_THRESHOLD) {
|
2013-08-17 08:34:38 +00:00
|
|
|
KeyDef kdf(axis.deviceId, KeyMap::TranslateKeyCodeFromAxis(axis.axisId, -1));
|
2013-08-16 14:48:43 +00:00
|
|
|
screenManager()->finishDialog(this, DR_OK);
|
2013-08-17 08:34:38 +00:00
|
|
|
if (callback_)
|
|
|
|
callback_(kdf);
|
2013-08-16 14:48:43 +00:00
|
|
|
}
|
|
|
|
}
|