Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/GamepadEmu.h
5691 views
1
// Copyright (c) 2012- 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
#pragma once
19
20
#include "Common/Input/InputState.h"
21
#include "Common/Render/DrawBuffer.h"
22
23
#include "Common/UI/View.h"
24
#include "Common/UI/ViewGroup.h"
25
#include "Core/CoreParameter.h"
26
#include "Core/HLE/sceCtrl.h"
27
#include "UI/EmuScreen.h"
28
29
struct TouchControlConfig;
30
class ControlMapper;
31
32
class GamepadEmuView : public UI::AnchorLayout {
33
public:
34
GamepadEmuView(const TouchControlConfig &config, float xres, float yres, bool *pause, ControlMapper *controlMapper, UI::LayoutParams *layoutParams);
35
void Update() override;
36
};
37
38
class GamepadComponent : public UI::View {
39
public:
40
GamepadComponent(std::string_view key, UI::LayoutParams *layoutParams);
41
42
bool Key(const KeyInput &input) override {
43
return false;
44
}
45
std::string DescribeText() const override;
46
virtual bool IsDown() const = 0;
47
virtual bool IsDownForFadeoutCheck() const {
48
return IsDown();
49
}
50
51
protected:
52
std::string key_;
53
};
54
55
class MultiTouchButton : public GamepadComponent {
56
public:
57
MultiTouchButton(std::string_view key, ImageID bgImg, ImageID bgDownImg, ImageID img, float scale, UI::LayoutParams *layoutParams)
58
: GamepadComponent(key, layoutParams), scale_(scale), bgImg_(bgImg), bgDownImg_(bgDownImg), img_(img) {
59
}
60
61
bool Touch(const TouchInput &input) override;
62
void Draw(UIContext &dc) override;
63
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
64
bool IsDown() const override { return pointerDownMask_ != 0; }
65
66
// chainable
67
MultiTouchButton *FlipImageH(bool flip) { flipImageH_ = flip; return this; }
68
MultiTouchButton *SetAngle(float angle) { angle_ = angle; bgAngle_ = angle; return this; }
69
MultiTouchButton *SetAngle(float angle, float bgAngle) { angle_ = angle; bgAngle_ = bgAngle; return this; }
70
71
bool CanGlide() const;
72
void SetMinimumAlpha(float minAlpha) { minimumAlpha_ = minAlpha; }
73
74
protected:
75
uint32_t pointerDownMask_ = 0;
76
float scale_;
77
78
private:
79
ImageID bgImg_;
80
ImageID bgDownImg_;
81
ImageID img_;
82
float bgAngle_ = 0.0f;
83
float angle_ = 0.0f;
84
bool flipImageH_ = false;
85
float minimumAlpha_ = 0.0f;
86
};
87
88
class BoolButton : public MultiTouchButton {
89
public:
90
BoolButton(bool *value, std::string_view key, ImageID bgImg, ImageID bgDownImg, ImageID img, float scale, UI::LayoutParams *layoutParams)
91
: MultiTouchButton(key, bgImg, bgDownImg, img, scale, layoutParams), value_(value) {
92
}
93
bool Touch(const TouchInput &input) override;
94
bool IsDown() const override { return *value_; }
95
96
UI::Event OnChange;
97
98
private:
99
bool *value_;
100
};
101
102
class PSPButton : public MultiTouchButton {
103
public:
104
PSPButton(int pspButtonBit, std::string_view key, ImageID bgImg, ImageID bgDownImg, ImageID img, float scale, UI::LayoutParams *layoutParams)
105
: MultiTouchButton(key, bgImg, bgDownImg, img, scale, layoutParams), pspButtonBit_(pspButtonBit) {
106
}
107
bool Touch(const TouchInput &input) override;
108
bool IsDown() const override;
109
110
private:
111
int pspButtonBit_;
112
};
113
114
class PSPDpad : public GamepadComponent {
115
public:
116
PSPDpad(ImageID arrowIndex, std::string_view key, ImageID arrowDownIndex, ImageID overlayIndex, float scale, float spacing, UI::LayoutParams *layoutParams);
117
118
bool Touch(const TouchInput &input) override;
119
void Draw(UIContext &dc) override;
120
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
121
bool IsDown() const override { return down_ != 0; }
122
123
private:
124
void ProcessTouch(float x, float y, bool down, bool ignorePress);
125
ImageID arrowIndex_;
126
ImageID arrowDownIndex_;
127
ImageID overlayIndex_;
128
129
float scale_;
130
float spacing_;
131
132
int dragPointerId_;
133
int down_;
134
};
135
136
class PSPStick : public GamepadComponent {
137
public:
138
PSPStick(ImageID bgImg, std::string_view key, ImageID stickImg, ImageID stickDownImg, int stick, float scale, UI::LayoutParams *layoutParams);
139
140
bool Touch(const TouchInput &input) override;
141
void Draw(UIContext &dc) override;
142
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
143
bool IsDown() const override { return dragPointerId_ != -1; }
144
145
protected:
146
int dragPointerId_ = -1;
147
ImageID bgImg_;
148
ImageID stickImageIndex_;
149
ImageID stickDownImg_;
150
151
int stick_;
152
float stick_size_;
153
float scale_;
154
155
float centerX_ = -1.0f;
156
float centerY_ = -1.0f;
157
158
private:
159
void ProcessTouch(float x, float y, bool down);
160
};
161
162
class PSPCustomStick : public PSPStick {
163
public:
164
PSPCustomStick(ImageID bgImg, const char *key, ImageID stickImg, ImageID stickDownImg, int stick, float scale, UI::LayoutParams *layoutParams);
165
166
bool Touch(const TouchInput &input) override;
167
void Draw(UIContext &dc) override;
168
169
private:
170
void ProcessTouch(float x, float y, bool down);
171
172
float posX_ = 0.0f;
173
float posY_ = 0.0f;
174
};
175
176
struct TouchControlConfig;
177
178
// Initializes the layout from Config. if a default layout does not exist, it sets up default values
179
void InitPadLayout(TouchControlConfig *config, DeviceOrientation orientation, float xres, float yres, float globalScale = 1.15f);
180
UI::ViewGroup *CreatePadLayout(const TouchControlConfig &config, float xres, float yres, bool *pause, ControlMapper *controlMapper);
181
182
const int D_pad_Radius = 50;
183
const int baseActionButtonSpacing = 60;
184
185
// Customizable buttons, press a combination of buttons specified by pspButtonBit.
186
class CustomButton : public MultiTouchButton {
187
public:
188
CustomButton(uint64_t pspButtonBit, std::string_view key, bool toggle, bool repeat, ControlMapper* controllMapper, ImageID bgImg, ImageID bgDownImg, ImageID img, float scale, bool invertedContentDimension, UI::LayoutParams *layoutParams)
189
: MultiTouchButton(key, bgImg, bgDownImg, img, scale, layoutParams), pspButtonBit_(pspButtonBit), toggle_(toggle), repeat_(repeat), controlMapper_(controllMapper), on_(false), invertedContentDimension_(invertedContentDimension) {
190
}
191
bool Touch(const TouchInput &input) override;
192
void Update() override;
193
194
bool IsDown() const override; // For visual purpose
195
bool IsDownForFadeoutCheck() const override;
196
197
void GetContentDimensions(const UIContext &dc, float &w, float &h) const override;
198
private:
199
uint64_t pspButtonBit_;
200
bool toggle_;
201
bool repeat_;
202
int pressedFrames_ = 0;
203
ControlMapper* controlMapper_;
204
bool on_;
205
bool invertedContentDimension_; // Swap width and height
206
};
207
208
struct GestureControlConfig;
209
210
class GestureGamepad : public UI::View {
211
public:
212
explicit GestureGamepad(ControlMapper* controlMapper, int zoneIndex, UI::LayoutParams *layoutParams) : UI::View(layoutParams), controlMapper_(controlMapper), zoneIndex_(zoneIndex) {}
213
~GestureGamepad();
214
215
bool Touch(const TouchInput &input) override;
216
void Update() override;
217
void Draw(UIContext &dc) override;
218
219
protected:
220
virtual std::string DescribeText() const override { return zoneIndex_ == 0 ? "gesture-left" : "gesture-right"; }
221
222
private:
223
const GestureControlConfig &GetZone();
224
225
int zoneIndex_;
226
float lastX_ = 0.0f;
227
float lastY_ = 0.0f;
228
float deltaX_ = 0.0f;
229
float deltaY_ = 0.0f;
230
float downX_ = 0.0f;
231
float downY_ = 0.0f;
232
float lastTapRelease_ = 0.0f;
233
float lastTouchDown_ = 0.0f;
234
int dragPointerId_ = -1;
235
bool swipeLeftReleased_ = true;
236
bool swipeRightReleased_ = true;
237
bool swipeUpReleased_ = true;
238
bool swipeDownReleased_ = true;
239
bool haveDoubleTapped_ = false;
240
ControlMapper* controlMapper_;
241
};
242
243
// Just edit this to add new image, shape or button function
244
namespace CustomKeyData {
245
// Image list
246
struct keyImage {
247
ImageID i; // ImageID
248
float r; // Rotation angle in degree
249
};
250
static const keyImage customKeyImages[] = {
251
{ ImageID("I_1"), 0.0f },
252
{ ImageID("I_2"), 0.0f },
253
{ ImageID("I_3"), 0.0f },
254
{ ImageID("I_4"), 0.0f },
255
{ ImageID("I_5"), 0.0f },
256
{ ImageID("I_6"), 0.0f },
257
{ ImageID("I_A"), 0.0f },
258
{ ImageID("I_B"), 0.0f },
259
{ ImageID("I_C"), 0.0f },
260
{ ImageID("I_D"), 0.0f },
261
{ ImageID("I_E"), 0.0f },
262
{ ImageID("I_F"), 0.0f },
263
{ ImageID("I_CIRCLE"), 0.0f },
264
{ ImageID("I_CROSS"), 0.0f },
265
{ ImageID("I_SQUARE"), 0.0f },
266
{ ImageID("I_TRIANGLE"), 0.0f },
267
{ ImageID("I_L"), 0.0f },
268
{ ImageID("I_R"), 0.0f },
269
{ ImageID("I_START"), 0.0f },
270
{ ImageID("I_SELECT"), 0.0f },
271
{ ImageID("I_CROSS"), 45.0f },
272
{ ImageID("I_SQUARE"), 45.0f },
273
{ ImageID("I_TRIANGLE"), 180.0f },
274
{ ImageID("I_ARROW"), 90.0f},
275
{ ImageID("I_ARROW"), 270.0f},
276
{ ImageID("I_ARROW"), 0.0f},
277
{ ImageID("I_ARROW"), 180.0f},
278
{ ImageID("I_GEAR"), 0.0f},
279
{ ImageID("I_ROTATE_LEFT"), 0.0f},
280
{ ImageID("I_ROTATE_RIGHT"), 0.0f},
281
{ ImageID("I_ARROW_LEFT"), 0.0f},
282
{ ImageID("I_ARROW_RIGHT"), 0.0f},
283
{ ImageID("I_ARROW_UP"), 0.0f},
284
{ ImageID("I_ARROW_DOWN"), 0.0f},
285
{ ImageID("I_THREE_DOTS"), 0.0f},
286
{ ImageID("I_EMPTY"), 0.0f},
287
};
288
289
// Shape list
290
struct keyShape {
291
ImageID i; // ImageID
292
ImageID l; // ImageID line version
293
float r; // Rotation angle in dregree
294
bool f; // Flip Horizontally
295
bool d; // Invert height and width for context dimension (for example for 90 degree rot)
296
};
297
static const keyShape customKeyShapes[] = {
298
{ ImageID("I_ROUND"), ImageID("I_ROUND_LINE"), 0.0f, false, false },
299
{ ImageID("I_RECT"), ImageID("I_RECT_LINE"), 0.0f, false, false },
300
{ ImageID("I_RECT"), ImageID("I_RECT_LINE"), 90.0f, false, true },
301
{ ImageID("I_SHOULDER"), ImageID("I_SHOULDER_LINE"), 0.0f, false, false },
302
{ ImageID("I_SHOULDER"), ImageID("I_SHOULDER_LINE"), 0.0f, true, false },
303
{ ImageID("I_DIR"), ImageID("I_DIR_LINE"), 270.0f, false, true },
304
{ ImageID("I_DIR"), ImageID("I_DIR_LINE"), 90.0f, false, true },
305
{ ImageID("I_DIR"), ImageID("I_DIR_LINE"), 180.0f, false, false },
306
{ ImageID("I_DIR"), ImageID("I_DIR_LINE"), 0.0f, false, false },
307
{ ImageID("I_SQUARE_SHAPE"), ImageID("I_SQUARE_SHAPE_LINE"), 0.0f, false, false },
308
{ ImageID("I_EMPTY"), ImageID("I_EMPTY"), 0.0f, false, false },
309
};
310
311
// Button list
312
struct keyList {
313
ImageID i; // UI ImageID
314
uint32_t c; // Key code
315
};
316
// For CustomButton. NOTE: This list can NOT be freely reordered! We store a bitmask of the indices.
317
// NOTE 2: Unfortunately we messed up here, we should NOT have used ifdefs! This breaks the order.
318
static const keyList g_customKeyList[] = {
319
{ ImageID("I_SQUARE"), CTRL_SQUARE },
320
{ ImageID("I_TRIANGLE"), CTRL_TRIANGLE },
321
{ ImageID("I_CIRCLE"), CTRL_CIRCLE },
322
{ ImageID("I_CROSS"), CTRL_CROSS },
323
{ ImageID::invalid(), CTRL_UP },
324
{ ImageID::invalid(), CTRL_DOWN },
325
{ ImageID::invalid(), CTRL_LEFT },
326
{ ImageID::invalid(), CTRL_RIGHT },
327
{ ImageID("I_START"), CTRL_START },
328
{ ImageID("I_SELECT"), CTRL_SELECT },
329
{ ImageID("I_L"), CTRL_LTRIGGER },
330
{ ImageID("I_R"), CTRL_RTRIGGER },
331
{ ImageID::invalid(), VIRTKEY_RAPID_FIRE },
332
{ ImageID::invalid(), VIRTKEY_FASTFORWARD },
333
{ ImageID::invalid(), VIRTKEY_SPEED_TOGGLE },
334
{ ImageID::invalid(), VIRTKEY_REWIND },
335
{ ImageID::invalid(), VIRTKEY_SAVE_STATE },
336
{ ImageID::invalid(), VIRTKEY_LOAD_STATE },
337
{ ImageID::invalid(), VIRTKEY_NEXT_SLOT },
338
#if !defined(MOBILE_DEVICE) // BAD!!
339
{ ImageID::invalid(), VIRTKEY_TOGGLE_FULLSCREEN },
340
#endif
341
{ ImageID::invalid(), VIRTKEY_SPEED_CUSTOM1 },
342
{ ImageID::invalid(), VIRTKEY_SPEED_CUSTOM2 },
343
{ ImageID::invalid(), VIRTKEY_TEXTURE_DUMP },
344
{ ImageID::invalid(), VIRTKEY_TEXTURE_REPLACE },
345
{ ImageID::invalid(), VIRTKEY_SCREENSHOT },
346
{ ImageID::invalid(), VIRTKEY_MUTE_TOGGLE },
347
{ ImageID::invalid(), VIRTKEY_OPENCHAT },
348
{ ImageID::invalid(), VIRTKEY_ANALOG_ROTATE_CW },
349
{ ImageID::invalid(), VIRTKEY_ANALOG_ROTATE_CCW },
350
{ ImageID::invalid(), VIRTKEY_PAUSE },
351
{ ImageID::invalid(), VIRTKEY_RESET_EMULATION },
352
{ ImageID::invalid(), VIRTKEY_DEVMENU },
353
#ifndef MOBILE_DEVICE // BAD!!!
354
{ ImageID::invalid(), VIRTKEY_RECORD },
355
#endif
356
{ ImageID::invalid(), VIRTKEY_AXIS_X_MIN },
357
{ ImageID::invalid(), VIRTKEY_AXIS_Y_MIN },
358
{ ImageID::invalid(), VIRTKEY_AXIS_X_MAX },
359
{ ImageID::invalid(), VIRTKEY_AXIS_Y_MAX },
360
{ ImageID::invalid(), VIRTKEY_PREVIOUS_SLOT },
361
{ ImageID::invalid(), VIRTKEY_TOGGLE_TOUCH_CONTROLS }, // 38 if !MOBILE_DEVICE, 36 if MOBILE_DEVICE. See IsDownForFadeoutCheck
362
{ ImageID::invalid(), VIRTKEY_TOGGLE_DEBUGGER },
363
{ ImageID::invalid(), VIRTKEY_PAUSE_NO_MENU },
364
{ ImageID::invalid(), VIRTKEY_TOGGLE_TILT },
365
// IMPORTANT: Only add at the end!
366
};
367
static_assert(ARRAY_SIZE(g_customKeyList) <= 64, "Too many key for a uint64_t bit mask");
368
};
369
370
// Gesture key only have virtual button that can work without constant press
371
namespace GestureKey {
372
static const uint32_t keyList[] = {
373
CTRL_SQUARE,
374
CTRL_TRIANGLE,
375
CTRL_CIRCLE,
376
CTRL_CROSS,
377
CTRL_UP,
378
CTRL_DOWN,
379
CTRL_LEFT,
380
CTRL_RIGHT,
381
CTRL_START,
382
CTRL_SELECT,
383
CTRL_LTRIGGER,
384
CTRL_RTRIGGER,
385
VIRTKEY_AXIS_Y_MAX,
386
VIRTKEY_AXIS_Y_MIN,
387
VIRTKEY_AXIS_X_MIN,
388
VIRTKEY_AXIS_X_MAX,
389
VIRTKEY_SPEED_TOGGLE,
390
VIRTKEY_REWIND,
391
VIRTKEY_SAVE_STATE,
392
VIRTKEY_LOAD_STATE,
393
VIRTKEY_PREVIOUS_SLOT,
394
VIRTKEY_NEXT_SLOT,
395
VIRTKEY_TEXTURE_DUMP,
396
VIRTKEY_TEXTURE_REPLACE,
397
VIRTKEY_SCREENSHOT,
398
VIRTKEY_MUTE_TOGGLE,
399
VIRTKEY_OPENCHAT,
400
VIRTKEY_PAUSE,
401
VIRTKEY_DEVMENU,
402
#ifndef MOBILE_DEVICE
403
VIRTKEY_RECORD,
404
#endif
405
VIRTKEY_AXIS_RIGHT_X_MIN,
406
VIRTKEY_AXIS_RIGHT_Y_MIN,
407
VIRTKEY_AXIS_RIGHT_X_MAX,
408
VIRTKEY_AXIS_RIGHT_Y_MAX,
409
VIRTKEY_TOGGLE_DEBUGGER,
410
VIRTKEY_TOGGLE_TILT,
411
};
412
}
413
414
void GamepadTouch();
415
void GamepadResetTouch();
416
void GamepadUpdateOpacity(float force = -1.0f);
417
float GamepadGetOpacity();
418
419