Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/CustomButtonMappingScreen.cpp
5654 views
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/System/Display.h"
19
#include "Common/Render/DrawBuffer.h"
20
#include "Common/Render/TextureAtlas.h"
21
#include "Common/UI/Context.h"
22
#include "Common/UI/View.h"
23
#include "Common/UI/ViewGroup.h"
24
#include "Common/UI/ScrollView.h"
25
#include "Common/UI/PopupScreens.h"
26
27
#include "Common/Data/Text/I18n.h"
28
#include "Common/Data/Color/RGBAUtil.h"
29
#include "Common/Math/curves.h"
30
#include "Common/TimeUtil.h"
31
#include "Common/StringUtils.h"
32
#include "Core/Config.h"
33
34
#include "UI/CustomButtonMappingScreen.h"
35
36
class ButtonShapeScreen : public UI::PopupScreen {
37
public:
38
ButtonShapeScreen(std::string_view title, int *setting) : PopupScreen(title), setting_(setting) {}
39
40
void CreatePopupContents(UI::ViewGroup *parent) override {
41
using namespace UI;
42
using namespace CustomKeyData;
43
44
ScrollView *scroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f));
45
LinearLayout *items = new LinearLayoutList(ORIENT_VERTICAL);
46
47
for (int i = 0; i < ARRAY_SIZE(customKeyShapes); ++i) {
48
Choice *c = items->Add(new Choice(ImageID(customKeyShapes[i].l), 0.6f, customKeyShapes[i].r*PI/180, customKeyShapes[i].f));
49
c->OnClick.Add([=](UI::EventParams &e) {
50
*setting_ = i;
51
TriggerFinish(DR_OK);
52
});
53
}
54
55
scroll->Add(items);
56
parent->Add(scroll);
57
}
58
59
const char *tag() const override { return "ButtonShape"; }
60
61
private:
62
int *setting_;
63
};
64
65
class ButtonIconScreen : public UI::PopupScreen {
66
public:
67
ButtonIconScreen(std::string_view title, int *setting) : PopupScreen(title), setting_(setting) {}
68
69
void CreatePopupContents(UI::ViewGroup *parent) override {
70
using namespace UI;
71
using namespace CustomKeyData;
72
73
ScrollView *scroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f));
74
LinearLayout *items = new LinearLayoutList(ORIENT_VERTICAL);
75
76
for (int i = 0; i < ARRAY_SIZE(customKeyImages); ++i) {
77
Choice *c = items->Add(new Choice(ImageID(customKeyImages[i].i), 1.0f, customKeyImages[i].r*PI/180));
78
c->OnClick.Add([=](UI::EventParams &e) {
79
*setting_ = i;
80
TriggerFinish(DR_OK);
81
});
82
}
83
84
scroll->Add(items);
85
parent->Add(scroll);
86
}
87
88
const char *tag() const override { return "ButtonIcon"; }
89
90
private:
91
int *setting_;
92
};
93
94
class ButtonPreview : public UI::View {
95
public:
96
ButtonPreview(ImageID bgImg, ImageID img, float rotationIcon, bool flipShape, float rotationShape, int x, int y)
97
: View(new UI::AnchorLayoutParams(x, y, UI::NONE, UI::NONE, UI::Centering::Both)), bgImg_(bgImg), img_(img), rotI_(rotationIcon),
98
flipS_(flipShape), rotS_(rotationShape), x_(x), y_(y) {}
99
100
void Draw(UIContext &dc) override {
101
float opacity = g_Config.iTouchButtonOpacity / 100.0f;
102
103
uint32_t colorBg = colorAlpha(g_Config.iTouchButtonStyle != 0 ? 0xFFFFFF : 0xc0b080, opacity);
104
uint32_t color = colorAlpha(0xFFFFFF, opacity);
105
106
dc.Draw()->DrawImageRotated(bgImg_, x_, y_, 1.0f, rotS_*PI/180, colorBg, flipS_);
107
dc.Draw()->DrawImageRotated(img_, x_, y_, 1.0f, rotI_*PI/180, color, false);
108
}
109
private:
110
int x_;
111
int y_;
112
float rotI_;
113
float rotS_;
114
bool flipS_;
115
ImageID bgImg_;
116
ImageID img_;
117
};
118
119
std::string_view CustomButtonMappingScreen::GetTitle() const {
120
auto co = GetI18NCategory(I18NCat::CONTROLS);
121
return co->T("Custom touch button setup");
122
}
123
124
void CustomButtonMappingScreen::CreateDialogViews(UI::ViewGroup *parent) {
125
using namespace UI;
126
using namespace CustomKeyData;
127
auto co = GetI18NCategory(I18NCat::CONTROLS);
128
auto mc = GetI18NCategory(I18NCat::MAPPABLECONTROLS);
129
LinearLayout *root__ = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, FILL_PARENT, 1.0));
130
parent->Add(root__);
131
LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(120, FILL_PARENT));
132
auto di = GetI18NCategory(I18NCat::DIALOG);
133
134
TouchControlConfig &touch = g_Config.GetTouchControlsConfig(deviceOrientation_);
135
136
ConfigCustomButton *cfg = nullptr;
137
bool *show = nullptr;
138
memset(array, 0, sizeof(array));
139
cfg = &g_Config.CustomButton[id_];
140
show = &touch.touchCustom[id_].show;
141
for (int i = 0; i < ARRAY_SIZE(g_customKeyList); i++)
142
array[i] = (0x01 == ((g_Config.CustomButton[id_].key >> i) & 0x01));
143
144
// TODO: Less hacky layout work
145
const Bounds layoutBounds = screenManager()->getUIContext()->GetLayoutBounds();
146
leftColumn->Add(new ButtonPreview(g_Config.iTouchButtonStyle == 0 ? customKeyShapes[cfg->shape].i : customKeyShapes[cfg->shape].l,
147
customKeyImages[cfg->image].i, customKeyImages[cfg->image].r, customKeyShapes[cfg->shape].f, customKeyShapes[cfg->shape].r, layoutBounds.x + 62, layoutBounds.y + 102));
148
149
root__->Add(leftColumn);
150
151
ScrollView *rightScroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT, 1.0f));
152
root__->Add(rightScroll);
153
154
LinearLayout *vertLayout = new LinearLayout(ORIENT_VERTICAL);
155
rightScroll->Add(vertLayout);
156
157
vertLayout->Add(new ItemHeader(co->T("Button style")));
158
vertLayout->Add(new CheckBox(show, co->T("Visible")));
159
160
Choice *icon = vertLayout->Add(new Choice(co->T("Icon")));
161
icon->SetIconRight(ImageID(customKeyImages[cfg->image].i), 1.0f, customKeyImages[cfg->image].r*PI/180, false, false); // Set right icon on the choice
162
icon->OnClick.Add([=](UI::EventParams &e) {
163
auto iconScreen = new ButtonIconScreen(co->T("Icon"), &(cfg->image));
164
if (e.v)
165
iconScreen->SetPopupOrigin(e.v);
166
167
screenManager()->push(iconScreen);
168
});
169
170
Choice *shape = vertLayout->Add(new Choice(co->T("Shape")));
171
shape->SetIconRight(ImageID(customKeyShapes[cfg->shape].l), 0.6f, customKeyShapes[cfg->shape].r*PI/180, customKeyShapes[cfg->shape].f, false); // Set right icon on the choice
172
shape->OnClick.Add([=](UI::EventParams &e) {
173
auto shape = new ButtonShapeScreen(co->T("Shape"), &(cfg->shape));
174
if (e.v)
175
shape->SetPopupOrigin(e.v);
176
177
screenManager()->push(shape);
178
});
179
180
vertLayout->Add(new ItemHeader(co->T("Button Binding")));
181
vertLayout->Add(new CheckBox(&(cfg->toggle), co->T("Toggle mode")));
182
vertLayout->Add(new CheckBox(&(cfg->repeat), co->T("Repeat mode")));
183
184
const int cellSize = 400;
185
UI::GridLayoutSettings gridsettings(cellSize, 64, 5);
186
gridsettings.fillCells = true;
187
GridLayout *grid = vertLayout->Add(new GridLayout(gridsettings, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
188
189
// Button image and action are defined in GamepadEmu.h
190
for (int i = 0; i < ARRAY_SIZE(g_customKeyList); ++i) {
191
LinearLayout *row = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT));
192
row->SetSpacing(0);
193
194
CheckBox *checkbox = new CheckBox(&array[i], "", "", new LinearLayoutParams(50, WRAP_CONTENT));
195
row->Add(checkbox);
196
197
Choice *choice;
198
if (g_customKeyList[i].i.isValid()) {
199
choice = new Choice(g_customKeyList[i].i, new LinearLayoutParams(1.0f));
200
} else {
201
choice = new Choice(mc->T(KeyMap::GetPspButtonNameCharPointer(g_customKeyList[i].c)), new LinearLayoutParams(1.0f));
202
}
203
204
ChoiceEventHandler *choiceEventHandler = new ChoiceEventHandler(checkbox);
205
choice->OnClick.Handle(choiceEventHandler, &ChoiceEventHandler::onChoiceClick);
206
207
choice->SetCentered(true);
208
209
row->Add(choice);
210
grid->Add(row);
211
}
212
}
213
214
static uint64_t arrayToInt(const bool ary[ARRAY_SIZE(CustomKeyData::g_customKeyList)]) {
215
uint64_t value = 0;
216
for (int i = ARRAY_SIZE(CustomKeyData::g_customKeyList)-1; i >= 0; i--) {
217
value |= ary[i] ? 1 : 0;
218
if (i > 0) {
219
value = value << 1;
220
}
221
}
222
return value;
223
}
224
225
void CustomButtonMappingScreen::saveArray() {
226
if (id_ >= 0 && id_ < TouchControlConfig::CUSTOM_BUTTON_COUNT) {
227
g_Config.CustomButton[id_].key = arrayToInt(array);
228
}
229
}
230
231
void CustomButtonMappingScreen::dialogFinished(const Screen *dialog, DialogResult result) {
232
saveArray();
233
RecreateViews();
234
}
235
236
void CustomButtonMappingScreen::onFinish(DialogResult result) {
237
saveArray();
238
g_Config.Save("CustomButtonMappingScreen::onFinish");
239
}
240
241
void CustomButtonMappingScreen::ChoiceEventHandler::onChoiceClick(UI::EventParams &e){
242
checkbox_->Toggle();
243
};
244
245