-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput_glyphs_steamworks.cpp
308 lines (271 loc) · 14.8 KB
/
input_glyphs_steamworks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**************************************************************************/
/* input_glyphs_steamworks.cpp */
/**************************************************************************/
/* This file is part of: */
/* EIRTeam.Steamworks */
/* https://ph.eirteam.moe */
/**************************************************************************/
/* Copyright (c) 2023-present Álex Román (EIRTeam) & contributors. */
/* */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "input_glyphs_steamworks.h"
#include "core/error/error_macros.h"
#include "core/io/dir_access.h"
#include "core/io/image.h"
#include "core/io/json.h"
#include "scene/resources/image_texture.h"
#include "scene/resources/placeholder_textures.h"
#include "steamworks.h"
#include "steam/isteaminput.h"
#include <iterator>
#ifdef MODULE_INPUT_GLYPHS_ENABLED
#include "modules/input_glyphs/input_glyph_svg_decode.h"
SWC::SteamInputType __input_type_lut[] = {
SWC::SteamInputType::STEAM_INPUT_TYPE_UNKNOWN,
SWC::SteamInputType::STEAM_INPUT_TYPE_STEAM_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_X_BOX360_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_X_BOX_ONE_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_GENERIC_GAMEPAD,
SWC::SteamInputType::STEAM_INPUT_TYPE_PS3_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_PS4_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_PS5_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_SWITCH_PRO_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_STEAM_DECK_CONTROLLER,
SWC::SteamInputType::STEAM_INPUT_TYPE_UNKNOWN, // Keyboard
};
static_assert(std::size(__input_type_lut) == InputGlyphsConstants::INPUT_TYPE_MAX);
SWC::InputActionOrigin __origin_to_teamworks_xbox_origin_lut[]{
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_A,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_B,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_X,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_Y,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_VIEW,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_MENU,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_BUMPER,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_BUMPER,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_TRIGGER_PULL,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_TRIGGER_CLICK,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_TRIGGER_PULL,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_TRIGGER_CLICK,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_STICK_MOVE,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_STICK_CLICK,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_STICK_D_PAD_NORTH,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_STICK_D_PAD_SOUTH,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_STICK_D_PAD_WEST,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_STICK_D_PAD_EAST,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_STICK_MOVE,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_STICK_CLICK,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_STICK_D_PAD_NORTH,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_STICK_D_PAD_SOUTH,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_STICK_D_PAD_WEST,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_STICK_D_PAD_EAST,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_D_PAD_NORTH,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_D_PAD_SOUTH,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_D_PAD_WEST,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_D_PAD_EAST,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_D_PAD_MOVE,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_SHARE,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_GRIP_UPPER,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_LEFT_GRIP_LOWER,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_GRIP_UPPER,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_X_BOX_ONE_RIGHT_GRIP_LOWER,
SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_PS5_CENTER_PAD_CLICK,
};
static_assert(std::size(__origin_to_teamworks_xbox_origin_lut) == InputGlyphsConstants::INPUT_ORIGIN_COUNT);
SWC::InputActionOrigin HBSteamworksInputGlyphsSource::origin_to_steamworks_xbox_origin(const InputGlyphsConstants::InputOrigin &p_input_origin) {
if (p_input_origin == InputGlyphsConstants::INPUT_ORIGIN_INVALID) {
return SWC::InputActionOrigin::INPUT_ACTION_ORIGIN_NONE;
}
return __origin_to_teamworks_xbox_origin_lut[p_input_origin];
}
SWC::SteamInputType HBSteamworksInputGlyphsSource::input_type_to_steamworks_input_type(const InputGlyphsConstants::InputType &p_input_type) {
// InputType matches ESteamInputType int eh steamworks SDK, so we just do a straight conversion
ERR_FAIL_COND_V(p_input_type == InputGlyphsConstants::INPUT_TYPE_MAX, SWC::SteamInputType::STEAM_INPUT_TYPE_X_BOX_ONE_CONTROLLER);
return __input_type_lut[p_input_type];
}
InputGlyphsConstants::InputType HBSteamworksInputGlyphsSource::steamworks_input_type_to_input_type(const SWC::SteamInputType &p_steam_input_type) {
for (int i = 0; i < InputGlyphsConstants::INPUT_TYPE_MAX; i++) {
if (__input_type_lut[i] == p_steam_input_type) {
return (InputGlyphsConstants::InputType)i;
}
}
return InputGlyphsConstants::UNKNOWN;
}
ESteamInputGlyphSize input_glph_size_to_steamworks(const InputGlyphSize &p_glyph_size) {
return (ESteamInputGlyphSize)p_glyph_size;
}
Ref<Texture2D> HBSteamworksInputGlyphsSource::get_input_glyph(const InputGlyphsConstants::InputType &p_input_type, const InputGlyphsConstants::InputOrigin &p_input_origin, const BitField<InputGlyphStyle> &p_glyphs_style, const InputGlyphSize &p_size) {
HBSteamInput *input = Steamworks::get_singleton()->get_input();
// Convert from xbox 360 reference origin to the destination input type
SWC::InputActionOrigin steamworks_origin = (SWC::InputActionOrigin)origin_to_steamworks_xbox_origin(p_input_origin);
SWC::SteamInputType steamworks_input_type = (SWC::SteamInputType)input_type_to_steamworks_input_type(p_input_type);
SWC::InputActionOrigin translated_origin = input->translate_action_origin(steamworks_input_type, steamworks_origin);
String glyph_path = input->get_glyph_svg_for_action_origin(translated_origin, p_glyphs_style);
Vector2i size = Vector2i(32, 32);
switch (p_size) {
case GLYPH_SIZE_LARGE: {
size = Vector2i(256, 256);
} break;
case GLYPH_SIZE_MEDIUM: {
size = Vector2i(128, 128);
} break;
default:
case GLYPH_SIZE_SMALL: {
size = Vector2i(32, 32);
} break;
}
if (glyph_path.is_empty()) {
Ref<PlaceholderTexture2D> placeholder = memnew(PlaceholderTexture2D);
placeholder->set_size(size);
return placeholder;
}
Error err;
Ref<FileAccess> file = FileAccess::open(glyph_path, FileAccess::ModeFlags::READ, &err);
if (glyph_path.is_empty() || err != OK) {
Ref<PlaceholderTexture2D> placeholder = memnew(PlaceholderTexture2D);
placeholder->set_size(size);
return placeholder;
}
Ref<Image> out_image;
out_image.instantiate();
String svg_str = file->get_as_utf8_string();
PackedByteArray pba = svg_str.to_utf8_buffer();
if (InputGlyphSVGDecode::render_svg(out_image, pba, size) != OK) {
Ref<PlaceholderTexture2D> placeholder = memnew(PlaceholderTexture2D);
placeholder->set_size(size);
return placeholder;
}
Ref<Texture2D> tex = ImageTexture::create_from_image(out_image);
tex->set_meta("glyph_path", glyph_path);
return tex;
}
InputGlyphsConstants::InputType HBSteamworksInputGlyphsSource::identify_joy(int p_device) const {
InputHandle_t input_handle = Steamworks::get_singleton()->get_input()->get_joy_steam_input_handle(p_device);
if (input_handle == 0) {
return InputGlyphsConstants::UNKNOWN;
}
SWC::SteamInputType steam_input_type = Steamworks::get_singleton()->get_input()->get_input_type_for_handle(input_handle);
return steamworks_input_type_to_input_type(steam_input_type);
}
void HBSteamworksInputGlyphDumpTool::_dump_input_type(InputDumpInfo &p_dump_info, InputGlyphsConstants::InputType p_input_type, Ref<HBSteamworksInputGlyphsSource> p_source) {
HBSteamInput *input = Steamworks::get_singleton()->get_input();
SWC::SteamInputType steam_input_type = p_source->input_type_to_steamworks_input_type(p_input_type);
for (int size_i = 0; size_i < InputGlyphSize::GLYPH_SIZE_MAX; size_i++) {
InputGlyphSize size = (InputGlyphSize)size_i;
for (int theme_i = 0; theme_i < InputGlyphStyle::GLYPH_STYLE_THEME_COUNT; theme_i++) {
const char *THEME_NAMES[] = {
"knockout",
"light",
"dark"
};
String theme_name = THEME_NAMES[theme_i];
String theme_path = p_dump_info.module_path.path_join(theme_name);
Ref<DirAccess> dir_fs = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
if (!dir_fs->dir_exists(theme_path)) {
dir_fs->make_dir_recursive(theme_path);
}
int base_style = theme_i;
const int ABXY_STYLES[] = {
base_style | InputGlyphStyle::GLYPH_STYLE_NEUTRAL_COLOR_ABXY,
base_style | InputGlyphStyle::GLYPH_STYLE_SOLID_ABXY,
base_style | InputGlyphStyle::GLYPH_STYLE_SOLID_ABXY | InputGlyphStyle::GLYPH_STYLE_NEUTRAL_COLOR_ABXY,
};
static_assert(std::size(ABXY_STYLES) - 1 < InputGlyphStyle::GLYPH_STYLE_THEME_COUNT);
// First get the different ABXY variations
for (int i = 0; i < 4 * 3; i++) {
int style_modifiers = ABXY_STYLES[i / 4];
int style = style_modifiers | base_style;
InputGlyphsConstants::InputOrigin origin = (InputGlyphsConstants::InputOrigin)(i % 4);
SWC::InputActionOrigin input_origin = HBSteamworksInputGlyphsSource::origin_to_steamworks_xbox_origin(origin);
input_origin = input->translate_action_origin(steam_input_type, input_origin);
Ref<Texture2D> tex = p_source->get_input_glyph(p_input_type, origin, style, size);
String svg_path = input->get_glyph_svg_for_action_origin(input_origin, style);
String new_file_path = theme_name + "/" + svg_path.get_file();
if (!svg_path.is_empty()) {
new_file_path = theme_name + "/" + svg_path.get_file();
dir_fs->copy(svg_path, theme_path.path_join(svg_path.get_file()));
}
p_dump_info.themes[theme_i].file_names.push_back(new_file_path);
p_dump_info.themes[theme_i].filename_to_idx_map.insert(new_file_path, p_dump_info.themes[theme_i].file_names.size() - 1);
p_dump_info.themes[theme_i].texture_map_abxy_overrides[i] = p_dump_info.themes[theme_i].file_names.size() - 1;
}
// Now do the rest
for (int i = 0; i < InputGlyphsConstants::INPUT_ORIGIN_COUNT; i++) {
InputGlyphsConstants::InputOrigin origin = (InputGlyphsConstants::InputOrigin)i;
InputGlyphStyle glyph_style = (InputGlyphStyle)base_style;
SWC::InputActionOrigin input_origin = HBSteamworksInputGlyphsSource::origin_to_steamworks_xbox_origin(origin);
input_origin = input->translate_action_origin(steam_input_type, input_origin);
Ref<Texture2D> tex = p_source->get_input_glyph(p_input_type, origin, glyph_style, size);
String svg_path = input->get_glyph_svg_for_action_origin(input_origin, base_style);
String new_file_path;
if (!svg_path.is_empty()) {
new_file_path = theme_name + "/" + svg_path.get_file();
dir_fs->copy(svg_path, theme_path.path_join(svg_path.get_file()));
}
p_dump_info.themes[theme_i].file_names.push_back(new_file_path);
p_dump_info.themes[theme_i].filename_to_idx_map.insert(new_file_path, p_dump_info.themes[theme_i].file_names.size() - 1);
p_dump_info.themes[theme_i].texture_map[i] = p_dump_info.themes[theme_i].file_names.size() - 1;
}
}
}
}
void HBSteamworksInputGlyphDumpTool::dump(const String &p_module_dir_path) {
const String glyphs_path = p_module_dir_path.path_join("resources");
if (!DirAccess::dir_exists_absolute(glyphs_path)) {
DirAccess::make_dir_recursive_absolute(glyphs_path);
}
Ref<HBSteamworksInputGlyphsSource> source = HBSteamworksInputGlyphsSource::_create_current();
InputDumpInfo info[InputGlyphsConstants::INPUT_TYPE_MAX];
Dictionary out_dict;
Array input_types_out;
for (int i = 0; i < InputGlyphsConstants::INPUT_TYPE_MAX; i++) {
InputGlyphsConstants::InputType input_type = (InputGlyphsConstants::InputType)(i);
info[i].module_path = glyphs_path;
_dump_input_type(info[i], input_type, source);
Dictionary input_type_dict;
input_type_dict["input_type"] = input_type;
Array themes;
for (int j = 0; j < GLYPH_STYLE_THEME_COUNT; j++) {
Array graphics_abxy;
Array graphics;
for (int k = 0; k < 4 * 3; k++) {
int str_idx = info[i].themes[j].texture_map_abxy_overrides[k];
graphics_abxy.push_back(info[i].themes[j].file_names[str_idx]);
}
for (int k = 0; k < InputGlyphsConstants::INPUT_ORIGIN_COUNT; k++) {
int str_idx = info[i].themes[j].texture_map[k];
graphics.push_back(info[i].themes[j].file_names[str_idx]);
}
Dictionary theme_dict;
theme_dict["graphics"] = graphics;
theme_dict["graphics_abxy_overrides"] = graphics_abxy;
themes.append(theme_dict);
}
input_type_dict["themes"] = themes;
input_types_out.push_back(input_type_dict);
}
out_dict["input_types"] = input_types_out;
Ref<FileAccess> fa = FileAccess::open(glyphs_path.path_join("info.json"), FileAccess::WRITE);
fa->store_string(JSON::stringify(out_dict, " "));
SceneTree::get_singleton()->quit();
}
#endif // MODULE_INPUT_GLYPHS_ENABLED