-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.qml
586 lines (507 loc) · 18.9 KB
/
main.qml
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 6.3
import sdl2mappercomponents 1.0
Window {
id: rootWindow
visible: true
width: 540
height: 600
minimumWidth: 540
minimumHeight: 600
//maximumWidth: 540
//maximumHeight: 600
//flags: Qt.Dialog
title: qsTr("SDL2 Gamepad Mapper")
QtObject
{
id: msgDialogStrings
property string copyGUIDText: qsTr("Copying GUID of \"%1\" to clipboard");
property string copyGUIDTitle: qsTr("Copying GUID");
property string copyMappingText: qsTr("Copying SDL Mapping string of \"%1\" to clipboard");
property string copyMappingTitle: qsTr("Copying SDL Mapping string");
property string localGCMappingFileSaveText: qsTr("Saved mapping to file: %1");
}
/*Component
{
id: msgDialogComp
MessageDialog
{
id: msgDialog
text: msgDialogStrings.copyGUIDText
title: msgDialogStrings.copyGUIDTitle
//standardButtons: StandardButton.Close
//icon: StandardIcon.Information
}
}
*/
MessageDialog
{
id: msgDialog
text: msgDialogStrings.copyGUIDText
title: msgDialogStrings.copyGUIDTitle
//standardButtons: StandardButton.Close
//icon: StandardIcon.Information
}
Component
{
id: mappingConfirmDialogComp
Dialog
{
id: saveMapDialog
width: parent.width - 100
height: 180
anchors.centerIn: parent
title: qsTr("Save Mapping?")
contentItem: Item
{
ColumnLayout
{
anchors.fill: parent
anchors.margins:
{
leftMargin: 4
rightMargin: 4
topMargin: 8
bottomMargin: 8
}
Text
{
Layout.fillHeight: false
Layout.preferredHeight: 30
Layout.alignment: Qt.AlignCenter
text: qsTr("Save Mapping with provided name?")
}
ColumnLayout
{
Layout.fillHeight: false
Label
{
text: qsTr("Mapping Name:")
}
TextField
{
id: mappingNameTxt
Layout.fillHeight: false
Layout.fillWidth: true
selectByMouse: true
text: {
var result = "";
var index = deviceCombo.currentIndex;
var joypad = backend.reader.joypadContainer.getJoystick(index);
if (joypad !== null)
{
result = joypad.gameContDevName;
}
return result;
}
maximumLength: 200
}
}
DialogButtonBox
{
Layout.fillWidth: true
Layout.alignment: Qt.AlignRight
spacing: 20
standardButtons: MessageDialog.Yes | MessageDialog.Cancel
onAccepted: {
var index = deviceCombo.currentIndex;
var joypad = backend.reader.joypadContainer.getJoystick(index);
if (joypad !== null)
{
joypad.gameMappedName = mappingNameTxt.text
var tempString = mappingDisplayItem.getGCMapping();
var result = joypad.setMappingString(tempString);
joypad.refreshGameController();
var logMessage = qsTr("Mapping string: %1").arg(tempString);
var successWrite = viewBackend.writeMappingString(tempString);
if (successWrite)
{
logger.log(logMessage);
var localFileLoc = msgDialogStrings.localGCMappingFileSaveText.arg(viewBackend.localWrittenFileString);
logger.log(localFileLoc);
}
viewBackend.joyComboModel.refreshItem(index);
}
saveMapDialog.close();
displayMappedItem.enableDisplay();
}
onRejected: {
saveMapDialog.close();
displayMappedItem.enableDisplay();
}
}
}
}
}
}
MessageDialog
{
id: createMappingDialog
text: "Create Game Controller Mapping?"
title: "Create Game Controller Mapping?"
buttons: MessageDialog.Yes | MessageDialog.No
onAccepted: {
actionButtonLayout.currentIndex = mappingActionRow.elementIndex;
displayMappedItem.disableDisplay();
//highlightImgLayout.currentIndex = mappingDisplayItem.elementIndex;
mappingDisplayItem.resetHighlight();
mappingDisplayItem.startMapping();
}
}
SDL2MapperBackend
{
id: backend
}
AppLogger
{
id: logger
onLogMessage: function(message) {
//console.log("LOGGER MESSAGE: " + message);
//logTxtArea.text = logTxtArea.text.concat("\n" + message);
logTxtArea.append(message);
}
}
Item
{
id: privateItem
MainViewBackend
{
id: viewBackend
mapperBack: backend
}
}
ColumnLayout
{
id: mainColLayout
spacing: 20
anchors.fill: parent
anchors.margins:
{
leftMargin: 8;
rightMargin: 8;
topMargin: 14;
bottomMargin: 14;
}
ComboBox
{
id: deviceCombo
Layout.fillWidth: true;
textRole: "details"
currentIndex: 0
model: ListModel
{
ListElement { details: "No devices" }
}
function changeActiveDevice() {
displayMappedItem.disableDisplay();
var index = currentIndex;
if (index >= 0)
{
var joypad = backend.reader.joypadContainer.getJoystick(index);
if (joypad !== null)
{
mappingDisplayItem.setJoyDevice(joypad);
displayMappedItem.setJoyDevice(joypad);
if (joypad.isGameController)
{
displayMappedItem.enableDisplay();
}
}
}
}
}
StackLayout
{
id: highlightImgLayout
Layout.fillHeight: true
Layout.fillWidth: true
Layout.alignment: Qt.AlignHCenter
currentIndex: mappingDisplayItem.mappingActive ? mappingDisplayItem.elementIndex : displayMappedItem.elementIndex
DisplayMappedJoystickItem
{
id: displayMappedItem
property int elementIndex: 0
logger: logger
Layout.alignment: Qt.AlignHCenter
//color: "#FF00FF"
}
MappingGCItem
{
id: mappingDisplayItem
property int elementIndex: 1
logger: logger
//Layout.fillWidth: true;
//Layout.fillHeight: false;
//Layout.maximumHeight: 50;
//height: 200
//width: 300
//Layout.preferredWidth: 50
//Layout.preferredHeight: 200
Layout.alignment: Qt.AlignHCenter
//color: "#FF00FF"
//scale: 0.5
//transformOrigin: Item.TopLeft
/*transform: Scale {
xScale: 0.5; yScale: 0.5;
}
*/
//Layout.maximumHeight: 500;
onMappingFinished: {
actionButtonLayout.currentIndex = defaultActionRow.elementIndex;
mappingDisplayItem.endMapping();
var msgDialog = mappingConfirmDialogComp.createObject(rootWindow);
msgDialog.open();
}
}
}
/*AnimatedImage
{
Layout.fillWidth: true;
Layout.fillHeight: true;
Layout.preferredHeight: 100;
Layout.maximumHeight: 500;
source: "file:///C:/Users/ryoch/Pictures/1541109515196-1.gif"
fillMode: Image.PreserveAspectFit
}
*/
StackLayout
{
id: actionButtonLayout
Layout.fillHeight: false
//Layout.preferredHeight: 60
RowLayout
{
id: defaultActionRow
spacing: 20
property int elementIndex: 0
enabled: deviceCombo.currentIndex >= 0
Button
{
Layout.fillWidth: true
text: qsTr("Copy Gamepad GUID")
onClicked: {
var index = deviceCombo.currentIndex;
var joypad = backend.reader.joypadContainer.getJoystick(index);
if (joypad !== null)
{
var guid = backend.reader.joypadContainer.getJoystick(index).GUID;
//var msgDialog = msgDialogComp.createObject(rootWindow);
msgDialog.text = msgDialogStrings.copyGUIDText.arg(joypad.gameContDevName);
msgDialog.title = msgDialogStrings.copyGUIDTitle;
msgDialog.open();
viewBackend.copyGUIDToClipboard(guid);
//logger.log(msgDialogStrings.copyGUIDText.arg(joypad.gameContDevName));
var logString = qsTr("The GUID for \"%1\" is \"%2\"").arg(joypad.gameMappedName).arg(joypad.GUID);
logger.log(logString);
//logger.log(viewBackend.generateGUIDStringDetails(joypad));
}
}
}
Button
{
Layout.fillWidth: true
text: qsTr("Create A New Mapping")
onClicked: {
var index = deviceCombo.currentIndex;
var joypad = backend.reader.joypadContainer.getJoystick(index);
if (joypad !== null)
{
createMappingDialog.open();
}
}
}
Button
{
Layout.fillWidth: true
text: qsTr("Copy Mapping String")
onClicked: {
var index = deviceCombo.currentIndex;
var joypad = backend.reader.joypadContainer.getJoystick(index);
var temp = joypad !== null ? joypad.gameContMapping : ""
if (temp !== "")
{
//var msgDialog = msgDialogComp.createObject(rootWindow);
msgDialog.text = msgDialogStrings.copyMappingText.arg(joypad.gameContDevName);
msgDialog.title = msgDialogStrings.copyMappingTitle;
msgDialog.open();
viewBackend.copyMappingString(temp);
var logString = qsTr("Mapping string for \"%1\" is \"%2\"").arg(joypad.gameContDevName).arg(temp);
logger.log(logString);
//logger.log(msgDialogStrings.copyMappingText.arg(joypad.gameContDevName));
}
}
}
}
RowLayout
{
id: mappingActionRow
spacing: 20
property int elementIndex: 1
Button
{
Layout.fillWidth: true
text: qsTr("Skip")
//enabled: !mappingDisplayItem.atListEnd
onClicked: {
mappingDisplayItem.activeNextHighlightButton();
}
}
Button
{
Layout.fillWidth: true
text: qsTr("Previous")
enabled: !mappingDisplayItem.atListStart
onClicked: {
mappingDisplayItem.activatePrevHighlightButton();
}
}
Button
{
Layout.fillWidth: true
text: qsTr("Cancel")
onClicked: {
actionButtonLayout.currentIndex = defaultActionRow.elementIndex;
//highlightImgLayout.currentIndex = displayMappedItem.elementIndex;
mappingDisplayItem.endMapping();
displayMappedItem.enableDisplay();
}
}
}
}
RowLayout
{
id: envVarActionRow
Layout.fillHeight: false
//Layout.preferredHeight: 60
spacing: 20
enabled: Qt.platform.os === "windows" && actionButtonLayout.currentIndex == defaultActionRow.elementIndex
//Layout.topMargin: 10
//visible: Qt.platform.os === "windows"
Button
{
Layout.fillWidth: true
text: qsTr("Set Mapping As Environmental Variable")
onClicked: {
var index = deviceCombo.currentIndex;
var joypad = backend.reader.joypadContainer.getJoystick(index);
if (joypad !== null && joypad.isGameController)
{
viewBackend.createMappingEnvvar(joypad.gameContMapping);
var logMessage = qsTr("Setting environmental variable \"SDL_GAMECONTROLLERCONFIG\" to \"%1\"")
.arg(joypad.gameContMapping);
logger.log(logMessage);
}
}
}
Button
{
Layout.fillWidth: true
text: qsTr("Reset Environmental Variable")
enabled: viewBackend.hasSDLEnvVar;
onClicked:
{
if (viewBackend.hasSDLEnvVar)
{
logger.log(qsTr("Reseting environmental variable \"SDL_GAMECONTROLLERCONFIG\""));
viewBackend.removeMappingEnvvar();
}
}
}
}
ScrollView
{
Layout.fillWidth: true;
Layout.fillHeight: false;
Layout.maximumHeight: 100
Layout.preferredHeight: 100
ScrollBar.vertical.policy: ScrollBar.AlwaysOn
TextArea
{
id: logTxtArea
text: "SDL2 Gamepad Mapper\n"
selectByMouse: true
readOnly: true
wrapMode: TextEdit.WrapAnywhere
}
}
Text
{
text: qsTr("Found %0 gamepad(s)").arg(backend.reader.joypadContainer.numPads);
}
}
function prepareSession()
{
logger.log(qsTr("Loading any existing mappings into SDL"));
viewBackend.loadExistingMappingFiles();
logger.log(qsTr("Scan for controllers"));
backend.start();
viewBackend.createJoyComboModel();
var numPads = backend.reader.joypadContainer.numPads;
var logMessage = qsTr("Found %0 gamepad(s)").arg(numPads);
logger.log(logMessage);
if (numPads > 0)
{
deviceCombo.model = Qt.binding(function()
{
return viewBackend.joyComboModel;
});
deviceCombo.currentIndex = -1;
deviceCombo.currentIndexChanged.connect(deviceCombo.changeActiveDevice);
deviceCombo.currentIndex = 0;
for (var i = 0; i < numPads; i++)
{
var joypad = backend.reader.joypadContainer.getJoystick(i);
if (joypad !== null)
{
logMessage = String("\"%1\", %2 %3").arg(joypad.gameMappedName)
.arg(joypad.GUID).arg(joypad.isGameController ? qsTr("(mapping available)") : "");
logger.log(logMessage);
}
}
}
deviceCombo.enabled = Qt.binding(function()
{
return !mappingDisplayItem.mappingActive && backend.reader.joypadContainer.numPads > 0;
});
if (viewBackend.hasSDLEnvVar)
{
logger.log(qsTr("Environmental variable \"SDL_GAMECONTROLLERCONFIG\" was found"));
}
else
{
logger.log(qsTr("Environmental variable \"SDL_GAMECONTROLLERCONFIG\" was not found"));
}
//mappingDisplayItem.mappingActive = true;
//mappingDisplayItem.setHighlightButton(0);
viewBackend.upstreamMappingCheckFinished.disconnect(prepareSession);
mainColLayout.enabled = true;
}
Component.onCompleted:
{
logTxtArea.text = "";
var logMessage = String("SDL2 Gamepad Mapper %1 (%2)").arg(viewBackend.progVersion)
.arg(viewBackend.generateSDLVersionText());
logger.log(logMessage);
logger.log("");
var fetchRemote = viewBackend.checkLatestMappingFile();
if (fetchRemote)
{
logger.log("Checking for latest GameController mapping file");
mainColLayout.enabled = false;
viewBackend.upstreamMappingCheckFinished.connect(prepareSession);
viewBackend.requestLatestMappingInfo();
//viewBackend.requestLatestMappingFile();
}
else
{
prepareSession();
}
}
Component.onDestruction:
{
backend.stop();
}
}