CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/UI/CustomButtonMappingScreen.cpp
Views: 1401
// Copyright (c) 2013- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "Common/System/Display.h"18#include "Common/Render/DrawBuffer.h"19#include "Common/Render/TextureAtlas.h"20#include "Common/UI/Context.h"21#include "Common/UI/View.h"22#include "Common/UI/ViewGroup.h"2324#include "Common/Data/Text/I18n.h"25#include "Common/Data/Color/RGBAUtil.h"26#include "Common/Math/curves.h"27#include "Common/TimeUtil.h"28#include "Common/StringUtils.h"29#include "Core/Config.h"3031#include "UI/CustomButtonMappingScreen.h"3233class ButtonShapeScreen : public PopupScreen {34public:35ButtonShapeScreen(std::string_view title, int *setting) : PopupScreen(title), setting_(setting) {}3637void CreatePopupContents(UI::ViewGroup *parent) override {38using namespace UI;39using namespace CustomKeyData;4041ScrollView *scroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f));42LinearLayout *items = new LinearLayoutList(ORIENT_VERTICAL);4344for (int i = 0; i < ARRAY_SIZE(customKeyShapes); ++i) {45Choice *c = items->Add(new Choice(ImageID(customKeyShapes[i].l), 0.6f, customKeyShapes[i].r*PI/180, customKeyShapes[i].f));46c->OnClick.Add([=](UI::EventParams &e) {47*setting_ = i;48TriggerFinish(DR_OK);49return UI::EVENT_DONE;50});51}5253scroll->Add(items);54parent->Add(scroll);55}5657const char *tag() const override { return "ButtonShape"; }5859private:60int *setting_;61};6263class ButtonIconScreen : public PopupScreen {64public:65ButtonIconScreen(std::string_view title, int *setting) : PopupScreen(title), setting_(setting) {}6667void CreatePopupContents(UI::ViewGroup *parent) override {68using namespace UI;69using namespace CustomKeyData;7071ScrollView *scroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f));72LinearLayout *items = new LinearLayoutList(ORIENT_VERTICAL);7374for (int i = 0; i < ARRAY_SIZE(customKeyImages); ++i) {75Choice *c = items->Add(new Choice(ImageID(customKeyImages[i].i), 1.0f, customKeyImages[i].r*PI/180));76c->OnClick.Add([=](UI::EventParams &e) {77*setting_ = i;78TriggerFinish(DR_OK);79return UI::EVENT_DONE;80});81}8283scroll->Add(items);84parent->Add(scroll);85}8687const char *tag() const override { return "ButtonIcon"; }8889private:90int *setting_;91};9293class ButtonPreview : public UI::View {94public:95ButtonPreview(ImageID bgImg, ImageID img, float rotationIcon, bool flipShape, float rotationShape, int x, int y)96: View(new UI::AnchorLayoutParams(x, y, UI::NONE, UI::NONE, true)), bgImg_(bgImg), img_(img), rotI_(rotationIcon),97flipS_(flipShape), rotS_(rotationShape), x_(x), y_(y) {}9899void Draw(UIContext &dc) override {100float opacity = g_Config.iTouchButtonOpacity / 100.0f;101102uint32_t colorBg = colorAlpha(g_Config.iTouchButtonStyle != 0 ? 0xFFFFFF : 0xc0b080, opacity);103uint32_t color = colorAlpha(0xFFFFFF, opacity);104105dc.Draw()->DrawImageRotated(bgImg_, x_, y_, 1.0f, rotS_*PI/180, colorBg, flipS_);106dc.Draw()->DrawImageRotated(img_, x_, y_, 1.0f, rotI_*PI/180, color, false);107}108private:109int x_;110int y_;111float rotI_;112float rotS_;113bool flipS_;114ImageID bgImg_;115ImageID img_;116};117118void CustomButtonMappingScreen::CreateViews() {119using namespace UI;120using namespace CustomKeyData;121auto co = GetI18NCategory(I18NCat::CONTROLS);122auto mc = GetI18NCategory(I18NCat::MAPPABLECONTROLS);123root_ = new LinearLayout(ORIENT_VERTICAL);124root_->Add(new ItemHeader(co->T("Custom Key Setting")));125LinearLayout *root__ = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(1.0));126root_->Add(root__);127LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(120, FILL_PARENT));128auto di = GetI18NCategory(I18NCat::DIALOG);129130ConfigCustomButton* cfg = nullptr;131bool* show = nullptr;132memset(array, 0, sizeof(array));133cfg = &g_Config.CustomButton[id_];134show = &g_Config.touchCustom[id_].show;135for (int i = 0; i < ARRAY_SIZE(customKeyList); i++)136array[i] = (0x01 == ((g_Config.CustomButton[id_].key >> i) & 0x01));137138leftColumn->Add(new ButtonPreview(g_Config.iTouchButtonStyle == 0 ? customKeyShapes[cfg->shape].i : customKeyShapes[cfg->shape].l,139customKeyImages[cfg->image].i, customKeyImages[cfg->image].r, customKeyShapes[cfg->shape].f, customKeyShapes[cfg->shape].r, 62, 82));140141root__->Add(leftColumn);142rightScroll_ = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT, 1.0f));143leftColumn->Add(new Spacer(new LinearLayoutParams(1.0f)));144leftColumn->Add(new Choice(di->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);145root__->Add(rightScroll_);146147LinearLayout *vertLayout = new LinearLayout(ORIENT_VERTICAL);148rightScroll_->Add(vertLayout);149150vertLayout->Add(new ItemHeader(co->T("Button style")));151vertLayout->Add(new CheckBox(show, co->T("Visible")));152153Choice *icon = vertLayout->Add(new Choice(co->T("Icon")));154icon->SetIcon(ImageID(customKeyImages[cfg->image].i), 1.0f, customKeyImages[cfg->image].r*PI/180, false, false); // Set right icon on the choice155icon->OnClick.Add([=](UI::EventParams &e) {156auto iconScreen = new ButtonIconScreen(co->T("Icon"), &(cfg->image));157if (e.v)158iconScreen->SetPopupOrigin(e.v);159160screenManager()->push(iconScreen);161return UI::EVENT_DONE;162});163164Choice *shape = vertLayout->Add(new Choice(co->T("Shape")));165shape->SetIcon(ImageID(customKeyShapes[cfg->shape].l), 0.6f, customKeyShapes[cfg->shape].r*PI/180, customKeyShapes[cfg->shape].f, false); // Set right icon on the choice166shape->OnClick.Add([=](UI::EventParams &e) {167auto shape = new ButtonShapeScreen(co->T("Shape"), &(cfg->shape));168if (e.v)169shape->SetPopupOrigin(e.v);170171screenManager()->push(shape);172return UI::EVENT_DONE;173});174175vertLayout->Add(new ItemHeader(co->T("Button Binding")));176vertLayout->Add(new CheckBox(&(cfg->toggle), co->T("Toggle mode")));177vertLayout->Add(new CheckBox(&(cfg->repeat), co->T("Repeat mode")));178179const int cellSize = 400;180UI::GridLayoutSettings gridsettings(cellSize, 64, 5);181gridsettings.fillCells = true;182GridLayout *grid = vertLayout->Add(new GridLayout(gridsettings, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));183184// Button image and action are defined in GamepadEmu.h185for (int i = 0; i < ARRAY_SIZE(customKeyList); ++i) {186LinearLayout *row = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT));187row->SetSpacing(0);188189CheckBox *checkbox = new CheckBox(&array[i], "", "", new LinearLayoutParams(50, WRAP_CONTENT));190row->Add(checkbox);191192Choice *choice;193if (customKeyList[i].i.isValid()) {194choice = new Choice(customKeyList[i].i, new LinearLayoutParams(1.0f));195} else {196choice = new Choice(mc->T(KeyMap::GetPspButtonNameCharPointer(customKeyList[i].c)), new LinearLayoutParams(1.0f));197}198199ChoiceEventHandler *choiceEventHandler = new ChoiceEventHandler(checkbox);200choice->OnClick.Handle(choiceEventHandler, &ChoiceEventHandler::onChoiceClick);201202choice->SetCentered(true);203204row->Add(choice);205grid->Add(row);206}207}208209static uint64_t arrayToInt(const bool ary[ARRAY_SIZE(CustomKeyData::customKeyList)]) {210uint64_t value = 0;211for (int i = ARRAY_SIZE(CustomKeyData::customKeyList)-1; i >= 0; i--) {212value |= ary[i] ? 1 : 0;213if (i > 0) {214value = value << 1;215}216}217return value;218}219220void CustomButtonMappingScreen::saveArray() {221if (id_ >= 0 && id_ < Config::CUSTOM_BUTTON_COUNT) {222g_Config.CustomButton[id_].key = arrayToInt(array);223}224}225226void CustomButtonMappingScreen::dialogFinished(const Screen *dialog, DialogResult result) {227saveArray();228RecreateViews();229}230231void CustomButtonMappingScreen::onFinish(DialogResult result) {232saveArray();233g_Config.Save("CustomButtonMappingScreen::onFinish");234}235236UI::EventReturn CustomButtonMappingScreen::ChoiceEventHandler::onChoiceClick(UI::EventParams &e){237checkbox_->Toggle();238return UI::EVENT_DONE;239};240241242