CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/CustomButtonMappingScreen.cpp
Views: 1401
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
25
#include "Common/Data/Text/I18n.h"
26
#include "Common/Data/Color/RGBAUtil.h"
27
#include "Common/Math/curves.h"
28
#include "Common/TimeUtil.h"
29
#include "Common/StringUtils.h"
30
#include "Core/Config.h"
31
32
#include "UI/CustomButtonMappingScreen.h"
33
34
class ButtonShapeScreen : public PopupScreen {
35
public:
36
ButtonShapeScreen(std::string_view title, int *setting) : PopupScreen(title), setting_(setting) {}
37
38
void CreatePopupContents(UI::ViewGroup *parent) override {
39
using namespace UI;
40
using namespace CustomKeyData;
41
42
ScrollView *scroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f));
43
LinearLayout *items = new LinearLayoutList(ORIENT_VERTICAL);
44
45
for (int i = 0; i < ARRAY_SIZE(customKeyShapes); ++i) {
46
Choice *c = items->Add(new Choice(ImageID(customKeyShapes[i].l), 0.6f, customKeyShapes[i].r*PI/180, customKeyShapes[i].f));
47
c->OnClick.Add([=](UI::EventParams &e) {
48
*setting_ = i;
49
TriggerFinish(DR_OK);
50
return UI::EVENT_DONE;
51
});
52
}
53
54
scroll->Add(items);
55
parent->Add(scroll);
56
}
57
58
const char *tag() const override { return "ButtonShape"; }
59
60
private:
61
int *setting_;
62
};
63
64
class ButtonIconScreen : public PopupScreen {
65
public:
66
ButtonIconScreen(std::string_view title, int *setting) : PopupScreen(title), setting_(setting) {}
67
68
void CreatePopupContents(UI::ViewGroup *parent) override {
69
using namespace UI;
70
using namespace CustomKeyData;
71
72
ScrollView *scroll = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, 1.0f));
73
LinearLayout *items = new LinearLayoutList(ORIENT_VERTICAL);
74
75
for (int i = 0; i < ARRAY_SIZE(customKeyImages); ++i) {
76
Choice *c = items->Add(new Choice(ImageID(customKeyImages[i].i), 1.0f, customKeyImages[i].r*PI/180));
77
c->OnClick.Add([=](UI::EventParams &e) {
78
*setting_ = i;
79
TriggerFinish(DR_OK);
80
return UI::EVENT_DONE;
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, true)), 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
void CustomButtonMappingScreen::CreateViews() {
120
using namespace UI;
121
using namespace CustomKeyData;
122
auto co = GetI18NCategory(I18NCat::CONTROLS);
123
auto mc = GetI18NCategory(I18NCat::MAPPABLECONTROLS);
124
root_ = new LinearLayout(ORIENT_VERTICAL);
125
root_->Add(new ItemHeader(co->T("Custom Key Setting")));
126
LinearLayout *root__ = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(1.0));
127
root_->Add(root__);
128
LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(120, FILL_PARENT));
129
auto di = GetI18NCategory(I18NCat::DIALOG);
130
131
ConfigCustomButton* cfg = nullptr;
132
bool* show = nullptr;
133
memset(array, 0, sizeof(array));
134
cfg = &g_Config.CustomButton[id_];
135
show = &g_Config.touchCustom[id_].show;
136
for (int i = 0; i < ARRAY_SIZE(customKeyList); i++)
137
array[i] = (0x01 == ((g_Config.CustomButton[id_].key >> i) & 0x01));
138
139
leftColumn->Add(new ButtonPreview(g_Config.iTouchButtonStyle == 0 ? customKeyShapes[cfg->shape].i : customKeyShapes[cfg->shape].l,
140
customKeyImages[cfg->image].i, customKeyImages[cfg->image].r, customKeyShapes[cfg->shape].f, customKeyShapes[cfg->shape].r, 62, 82));
141
142
root__->Add(leftColumn);
143
rightScroll_ = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT, 1.0f));
144
leftColumn->Add(new Spacer(new LinearLayoutParams(1.0f)));
145
leftColumn->Add(new Choice(di->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);
146
root__->Add(rightScroll_);
147
148
LinearLayout *vertLayout = new LinearLayout(ORIENT_VERTICAL);
149
rightScroll_->Add(vertLayout);
150
151
vertLayout->Add(new ItemHeader(co->T("Button style")));
152
vertLayout->Add(new CheckBox(show, co->T("Visible")));
153
154
Choice *icon = vertLayout->Add(new Choice(co->T("Icon")));
155
icon->SetIcon(ImageID(customKeyImages[cfg->image].i), 1.0f, customKeyImages[cfg->image].r*PI/180, false, false); // Set right icon on the choice
156
icon->OnClick.Add([=](UI::EventParams &e) {
157
auto iconScreen = new ButtonIconScreen(co->T("Icon"), &(cfg->image));
158
if (e.v)
159
iconScreen->SetPopupOrigin(e.v);
160
161
screenManager()->push(iconScreen);
162
return UI::EVENT_DONE;
163
});
164
165
Choice *shape = vertLayout->Add(new Choice(co->T("Shape")));
166
shape->SetIcon(ImageID(customKeyShapes[cfg->shape].l), 0.6f, customKeyShapes[cfg->shape].r*PI/180, customKeyShapes[cfg->shape].f, false); // Set right icon on the choice
167
shape->OnClick.Add([=](UI::EventParams &e) {
168
auto shape = new ButtonShapeScreen(co->T("Shape"), &(cfg->shape));
169
if (e.v)
170
shape->SetPopupOrigin(e.v);
171
172
screenManager()->push(shape);
173
return UI::EVENT_DONE;
174
});
175
176
vertLayout->Add(new ItemHeader(co->T("Button Binding")));
177
vertLayout->Add(new CheckBox(&(cfg->toggle), co->T("Toggle mode")));
178
vertLayout->Add(new CheckBox(&(cfg->repeat), co->T("Repeat mode")));
179
180
const int cellSize = 400;
181
UI::GridLayoutSettings gridsettings(cellSize, 64, 5);
182
gridsettings.fillCells = true;
183
GridLayout *grid = vertLayout->Add(new GridLayout(gridsettings, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
184
185
// Button image and action are defined in GamepadEmu.h
186
for (int i = 0; i < ARRAY_SIZE(customKeyList); ++i) {
187
LinearLayout *row = new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT));
188
row->SetSpacing(0);
189
190
CheckBox *checkbox = new CheckBox(&array[i], "", "", new LinearLayoutParams(50, WRAP_CONTENT));
191
row->Add(checkbox);
192
193
Choice *choice;
194
if (customKeyList[i].i.isValid()) {
195
choice = new Choice(customKeyList[i].i, new LinearLayoutParams(1.0f));
196
} else {
197
choice = new Choice(mc->T(KeyMap::GetPspButtonNameCharPointer(customKeyList[i].c)), new LinearLayoutParams(1.0f));
198
}
199
200
ChoiceEventHandler *choiceEventHandler = new ChoiceEventHandler(checkbox);
201
choice->OnClick.Handle(choiceEventHandler, &ChoiceEventHandler::onChoiceClick);
202
203
choice->SetCentered(true);
204
205
row->Add(choice);
206
grid->Add(row);
207
}
208
}
209
210
static uint64_t arrayToInt(const bool ary[ARRAY_SIZE(CustomKeyData::customKeyList)]) {
211
uint64_t value = 0;
212
for (int i = ARRAY_SIZE(CustomKeyData::customKeyList)-1; i >= 0; i--) {
213
value |= ary[i] ? 1 : 0;
214
if (i > 0) {
215
value = value << 1;
216
}
217
}
218
return value;
219
}
220
221
void CustomButtonMappingScreen::saveArray() {
222
if (id_ >= 0 && id_ < Config::CUSTOM_BUTTON_COUNT) {
223
g_Config.CustomButton[id_].key = arrayToInt(array);
224
}
225
}
226
227
void CustomButtonMappingScreen::dialogFinished(const Screen *dialog, DialogResult result) {
228
saveArray();
229
RecreateViews();
230
}
231
232
void CustomButtonMappingScreen::onFinish(DialogResult result) {
233
saveArray();
234
g_Config.Save("CustomButtonMappingScreen::onFinish");
235
}
236
237
UI::EventReturn CustomButtonMappingScreen::ChoiceEventHandler::onChoiceClick(UI::EventParams &e){
238
checkbox_->Toggle();
239
return UI::EVENT_DONE;
240
};
241
242