/
EmulationActivity.java
870 lines (756 loc) · 28.8 KB
/
EmulationActivity.java
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
package org.dolphinemu.dolphinemu.activities;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.annotation.IntDef;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.SparseIntArray;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile;
import org.dolphinemu.dolphinemu.fragments.EmulationFragment;
import org.dolphinemu.dolphinemu.fragments.MenuFragment;
import org.dolphinemu.dolphinemu.fragments.SaveLoadStateFragment;
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.ui.main.MainActivity;
import org.dolphinemu.dolphinemu.ui.main.MainPresenter;
import org.dolphinemu.dolphinemu.ui.platform.Platform;
import org.dolphinemu.dolphinemu.utils.ControllerMappingHelper;
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.Java_GCAdapter;
import org.dolphinemu.dolphinemu.utils.Java_WiimoteAdapter;
import org.dolphinemu.dolphinemu.utils.Rumble;
import org.dolphinemu.dolphinemu.utils.TvUtil;
import java.lang.annotation.Retention;
import java.util.List;
import static java.lang.annotation.RetentionPolicy.SOURCE;
public final class EmulationActivity extends AppCompatActivity
{
private static final String BACKSTACK_NAME_MENU = "menu";
private static final String BACKSTACK_NAME_SUBMENU = "submenu";
public static final int REQUEST_CHANGE_DISC = 1;
private View mDecorView;
private EmulationFragment mEmulationFragment;
private SharedPreferences mPreferences;
private ControllerMappingHelper mControllerMappingHelper;
private Settings mSettings;
private boolean mDeviceHasTouchScreen;
private boolean mMenuVisible;
private static boolean sIsGameCubeGame;
private boolean activityRecreated;
private String mSelectedTitle;
private int mPlatform;
private String mPath;
private boolean backPressedOnce = false;
public static final String EXTRA_SELECTED_GAME = "SelectedGame";
public static final String EXTRA_SELECTED_TITLE = "SelectedTitle";
public static final String EXTRA_PLATFORM = "Platform";
@Retention(SOURCE)
@IntDef({MENU_ACTION_EDIT_CONTROLS_PLACEMENT, MENU_ACTION_TOGGLE_CONTROLS, MENU_ACTION_ADJUST_SCALE,
MENU_ACTION_CHOOSE_CONTROLLER, MENU_ACTION_REFRESH_WIIMOTES, MENU_ACTION_TAKE_SCREENSHOT,
MENU_ACTION_QUICK_SAVE, MENU_ACTION_QUICK_LOAD, MENU_ACTION_SAVE_ROOT,
MENU_ACTION_LOAD_ROOT, MENU_ACTION_SAVE_SLOT1, MENU_ACTION_SAVE_SLOT2,
MENU_ACTION_SAVE_SLOT3, MENU_ACTION_SAVE_SLOT4, MENU_ACTION_SAVE_SLOT5,
MENU_ACTION_SAVE_SLOT6, MENU_ACTION_LOAD_SLOT1, MENU_ACTION_LOAD_SLOT2,
MENU_ACTION_LOAD_SLOT3, MENU_ACTION_LOAD_SLOT4, MENU_ACTION_LOAD_SLOT5,
MENU_ACTION_LOAD_SLOT6, MENU_ACTION_EXIT, MENU_ACTION_CHANGE_DISC,
MENU_ACTION_RESET_OVERLAY})
public @interface MenuAction
{
}
public static final int MENU_ACTION_EDIT_CONTROLS_PLACEMENT = 0;
public static final int MENU_ACTION_TOGGLE_CONTROLS = 1;
public static final int MENU_ACTION_ADJUST_SCALE = 2;
public static final int MENU_ACTION_CHOOSE_CONTROLLER = 3;
public static final int MENU_ACTION_REFRESH_WIIMOTES = 4;
public static final int MENU_ACTION_TAKE_SCREENSHOT = 5;
public static final int MENU_ACTION_QUICK_SAVE = 6;
public static final int MENU_ACTION_QUICK_LOAD = 7;
public static final int MENU_ACTION_SAVE_ROOT = 8;
public static final int MENU_ACTION_LOAD_ROOT = 9;
public static final int MENU_ACTION_SAVE_SLOT1 = 10;
public static final int MENU_ACTION_SAVE_SLOT2 = 11;
public static final int MENU_ACTION_SAVE_SLOT3 = 12;
public static final int MENU_ACTION_SAVE_SLOT4 = 13;
public static final int MENU_ACTION_SAVE_SLOT5 = 14;
public static final int MENU_ACTION_SAVE_SLOT6 = 15;
public static final int MENU_ACTION_LOAD_SLOT1 = 16;
public static final int MENU_ACTION_LOAD_SLOT2 = 17;
public static final int MENU_ACTION_LOAD_SLOT3 = 18;
public static final int MENU_ACTION_LOAD_SLOT4 = 19;
public static final int MENU_ACTION_LOAD_SLOT5 = 20;
public static final int MENU_ACTION_LOAD_SLOT6 = 21;
public static final int MENU_ACTION_EXIT = 22;
public static final int MENU_ACTION_CHANGE_DISC = 23;
public static final int MENU_ACTION_JOYSTICK_REL_CENTER = 24;
public static final int MENU_ACTION_RUMBLE = 25;
public static final int MENU_ACTION_RESET_OVERLAY = 26;
private static SparseIntArray buttonsActionsMap = new SparseIntArray();
static
{
buttonsActionsMap.append(R.id.menu_emulation_edit_layout,
EmulationActivity.MENU_ACTION_EDIT_CONTROLS_PLACEMENT);
buttonsActionsMap.append(R.id.menu_emulation_toggle_controls,
EmulationActivity.MENU_ACTION_TOGGLE_CONTROLS);
buttonsActionsMap
.append(R.id.menu_emulation_adjust_scale, EmulationActivity.MENU_ACTION_ADJUST_SCALE);
buttonsActionsMap.append(R.id.menu_emulation_choose_controller,
EmulationActivity.MENU_ACTION_CHOOSE_CONTROLLER);
buttonsActionsMap
.append(R.id.menu_refresh_wiimotes, EmulationActivity.MENU_ACTION_REFRESH_WIIMOTES);
buttonsActionsMap
.append(R.id.menu_emulation_screenshot, EmulationActivity.MENU_ACTION_TAKE_SCREENSHOT);
buttonsActionsMap.append(R.id.menu_quicksave, EmulationActivity.MENU_ACTION_QUICK_SAVE);
buttonsActionsMap.append(R.id.menu_quickload, EmulationActivity.MENU_ACTION_QUICK_LOAD);
buttonsActionsMap
.append(R.id.menu_emulation_save_root, EmulationActivity.MENU_ACTION_SAVE_ROOT);
buttonsActionsMap
.append(R.id.menu_emulation_load_root, EmulationActivity.MENU_ACTION_LOAD_ROOT);
buttonsActionsMap.append(R.id.menu_emulation_save_1, EmulationActivity.MENU_ACTION_SAVE_SLOT1);
buttonsActionsMap.append(R.id.menu_emulation_save_2, EmulationActivity.MENU_ACTION_SAVE_SLOT2);
buttonsActionsMap.append(R.id.menu_emulation_save_3, EmulationActivity.MENU_ACTION_SAVE_SLOT3);
buttonsActionsMap.append(R.id.menu_emulation_save_4, EmulationActivity.MENU_ACTION_SAVE_SLOT4);
buttonsActionsMap.append(R.id.menu_emulation_save_5, EmulationActivity.MENU_ACTION_SAVE_SLOT5);
buttonsActionsMap.append(R.id.menu_emulation_load_1, EmulationActivity.MENU_ACTION_LOAD_SLOT1);
buttonsActionsMap.append(R.id.menu_emulation_load_2, EmulationActivity.MENU_ACTION_LOAD_SLOT2);
buttonsActionsMap.append(R.id.menu_emulation_load_3, EmulationActivity.MENU_ACTION_LOAD_SLOT3);
buttonsActionsMap.append(R.id.menu_emulation_load_4, EmulationActivity.MENU_ACTION_LOAD_SLOT4);
buttonsActionsMap.append(R.id.menu_emulation_load_5, EmulationActivity.MENU_ACTION_LOAD_SLOT5);
buttonsActionsMap.append(R.id.menu_change_disc, EmulationActivity.MENU_ACTION_CHANGE_DISC);
buttonsActionsMap.append(R.id.menu_exit, EmulationActivity.MENU_ACTION_EXIT);
buttonsActionsMap.append(R.id.menu_emulation_joystick_rel_center,
EmulationActivity.MENU_ACTION_JOYSTICK_REL_CENTER);
buttonsActionsMap.append(R.id.menu_emulation_rumble, EmulationActivity.MENU_ACTION_RUMBLE);
buttonsActionsMap
.append(R.id.menu_emulation_reset_overlay, EmulationActivity.MENU_ACTION_RESET_OVERLAY);
}
public static void launch(FragmentActivity activity, GameFile gameFile)
{
Intent launcher = new Intent(activity, EmulationActivity.class);
launcher.putExtra(EXTRA_SELECTED_GAME, gameFile.getPath());
launcher.putExtra(EXTRA_SELECTED_TITLE, gameFile.getTitle());
launcher.putExtra(EXTRA_PLATFORM, gameFile.getPlatform());
Bundle options = new Bundle();
// I believe this warning is a bug. Activities are FragmentActivity from the support lib
//noinspection RestrictedApi
activity.startActivityForResult(launcher, MainPresenter.REQUEST_EMULATE_GAME, options);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
{
// Get params we were passed
Intent gameToEmulate = getIntent();
mPath = gameToEmulate.getStringExtra(EXTRA_SELECTED_GAME);
mSelectedTitle = gameToEmulate.getStringExtra(EXTRA_SELECTED_TITLE);
mPlatform = gameToEmulate.getIntExtra(EXTRA_PLATFORM, 0);
activityRecreated = false;
}
else
{
activityRecreated = true;
restoreState(savedInstanceState);
}
mSettings = new Settings();
mSettings.loadSettings(null);
// TODO: The accurate way to find out which console we're emulating is to
// first launch emulation and then ask the core which console we're emulating
sIsGameCubeGame = Platform.fromNativeInt(mPlatform) == Platform.GAMECUBE;
mDeviceHasTouchScreen = getPackageManager().hasSystemFeature("android.hardware.touchscreen");
mControllerMappingHelper = new ControllerMappingHelper();
int themeId;
if (mDeviceHasTouchScreen)
{
BooleanSetting lockLandscape =
(BooleanSetting) mSettings.getSection(Settings.SECTION_INI_CORE)
.getSetting(SettingsFile.KEY_LOCK_LANDSCAPE);
// Force landscape if set
if (lockLandscape == null || lockLandscape.getValue())
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
themeId = R.style.DolphinEmulationBase;
// Get a handle to the Window containing the UI.
mDecorView = getWindow().getDecorView();
mDecorView.setOnSystemUiVisibilityChangeListener(visibility ->
{
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0)
{
// Go back to immersive fullscreen mode in 3s
Handler handler = new Handler(getMainLooper());
handler.postDelayed(this::enableFullscreenImmersive, 3000 /* 3s */);
}
});
// Set these options now so that the SurfaceView the game renders into is the right size.
enableFullscreenImmersive();
Toast.makeText(this, getString(R.string.emulation_touch_button_help), Toast.LENGTH_LONG)
.show();
}
else
{
themeId = R.style.DolphinEmulationTvBase;
}
setTheme(themeId);
Java_GCAdapter.manager = (UsbManager) getSystemService(Context.USB_SERVICE);
Java_WiimoteAdapter.manager = (UsbManager) getSystemService(Context.USB_SERVICE);
Rumble.initRumble(this);
setContentView(R.layout.activity_emulation);
// Find or create the EmulationFragment
mEmulationFragment = (EmulationFragment) getSupportFragmentManager()
.findFragmentById(R.id.frame_emulation_fragment);
if (mEmulationFragment == null)
{
mEmulationFragment = EmulationFragment.newInstance(mPath);
getSupportFragmentManager().beginTransaction()
.add(R.id.frame_emulation_fragment, mEmulationFragment)
.commit();
}
if (mDeviceHasTouchScreen)
{
setTitle(mSelectedTitle);
}
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
if (!isChangingConfigurations())
{
mEmulationFragment.saveTemporaryState();
}
outState.putString(EXTRA_SELECTED_GAME, mPath);
outState.putString(EXTRA_SELECTED_TITLE, mSelectedTitle);
outState.putInt(EXTRA_PLATFORM, mPlatform);
super.onSaveInstanceState(outState);
}
protected void restoreState(Bundle savedInstanceState)
{
mPath = savedInstanceState.getString(EXTRA_SELECTED_GAME);
mSelectedTitle = savedInstanceState.getString(EXTRA_SELECTED_TITLE);
mPlatform = savedInstanceState.getInt(EXTRA_PLATFORM);
}
@Override
protected void onStop()
{
super.onStop();
Rumble.clear();
}
@Override
public void onBackPressed()
{
if (!mDeviceHasTouchScreen)
{
boolean popResult = getSupportFragmentManager().popBackStackImmediate(
BACKSTACK_NAME_SUBMENU, FragmentManager.POP_BACK_STACK_INCLUSIVE);
if (!popResult)
{
toggleMenu();
}
}
else
{
if (backPressedOnce)
{
mEmulationFragment.stopEmulation();
finish();
}
else
{
backPressedOnce = true;
Toast.makeText(this, "Press back again to exit", Toast.LENGTH_LONG).show();
new Handler().postDelayed(() -> backPressedOnce = false, 3000);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result)
{
switch (requestCode)
{
case REQUEST_CHANGE_DISC:
// If the user picked a file, as opposed to just backing out.
if (resultCode == MainActivity.RESULT_OK)
{
String newDiscPath = FileBrowserHelper.getSelectedDirectory(result);
if (!TextUtils.isEmpty(newDiscPath))
{
NativeLibrary.ChangeDisc(newDiscPath);
}
}
break;
}
}
private void enableFullscreenImmersive()
{
// It would be nice to use IMMERSIVE_STICKY, but that doesn't show the toolbar.
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
private void toggleMenu()
{
boolean result = getSupportFragmentManager().popBackStackImmediate(
BACKSTACK_NAME_MENU, FragmentManager.POP_BACK_STACK_INCLUSIVE);
mMenuVisible = false;
if (!result)
{
// Removing the menu failed, so that means it wasn't visible. Add it.
Fragment fragment = MenuFragment.newInstance(mSelectedTitle);
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(
R.animator.menu_slide_in_from_left,
R.animator.menu_slide_out_to_left,
R.animator.menu_slide_in_from_left,
R.animator.menu_slide_out_to_left)
.add(R.id.frame_menu, fragment)
.addToBackStack(BACKSTACK_NAME_MENU)
.commit();
mMenuVisible = true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
if (sIsGameCubeGame)
{
getMenuInflater().inflate(R.menu.menu_emulation, menu);
}
else
{
getMenuInflater().inflate(R.menu.menu_emulation_wii, menu);
}
BooleanSetting enableSaveStates =
(BooleanSetting) mSettings.getSection(Settings.SECTION_INI_CORE)
.getSetting(SettingsFile.KEY_ENABLE_SAVE_STATES);
if (enableSaveStates != null && enableSaveStates.getValue())
{
menu.findItem(R.id.menu_quicksave).setVisible(true);
menu.findItem(R.id.menu_quickload).setVisible(true);
menu.findItem(R.id.menu_emulation_save_root).setVisible(true);
menu.findItem(R.id.menu_emulation_load_root).setVisible(true);
}
// Populate the checkbox value for joystick center on touch
menu.findItem(R.id.menu_emulation_joystick_rel_center)
.setChecked(mPreferences.getBoolean("joystickRelCenter", true));
menu.findItem(R.id.menu_emulation_rumble)
.setChecked(mPreferences.getBoolean("phoneRumble", true));
return true;
}
@SuppressWarnings("WrongConstant")
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int action = buttonsActionsMap.get(item.getItemId(), -1);
if (action >= 0)
{
if (item.isCheckable())
{
// Need to pass a reference to the item to set the checkbox state, since it is not done automatically
handleCheckableMenuAction(action, item);
}
else
{
handleMenuAction(action);
}
}
return true;
}
public void handleCheckableMenuAction(@MenuAction int menuAction, MenuItem item)
{
switch (menuAction)
{
case MENU_ACTION_JOYSTICK_REL_CENTER:
item.setChecked(!item.isChecked());
toggleJoystickRelCenter(item.isChecked());
break;
case MENU_ACTION_RUMBLE:
item.setChecked(!item.isChecked());
toggleRumble(item.isChecked());
break;
}
}
public void handleMenuAction(@MenuAction int menuAction)
{
switch (menuAction)
{
// Edit the placement of the controls
case MENU_ACTION_EDIT_CONTROLS_PLACEMENT:
editControlsPlacement();
break;
// Reset overlay placement
case MENU_ACTION_RESET_OVERLAY:
resetOverlay();
break;
// Enable/Disable specific buttons or the entire input overlay.
case MENU_ACTION_TOGGLE_CONTROLS:
toggleControls();
return;
// Adjust the scale of the overlay controls.
case MENU_ACTION_ADJUST_SCALE:
adjustScale();
return;
// (Wii games only) Change the controller for the input overlay.
case MENU_ACTION_CHOOSE_CONTROLLER:
chooseController();
return;
case MENU_ACTION_REFRESH_WIIMOTES:
NativeLibrary.RefreshWiimotes();
return;
// Screenshot capturing
case MENU_ACTION_TAKE_SCREENSHOT:
NativeLibrary.SaveScreenShot();
return;
// Quick save / load
case MENU_ACTION_QUICK_SAVE:
NativeLibrary.SaveState(9, false);
return;
case MENU_ACTION_QUICK_LOAD:
NativeLibrary.LoadState(9);
return;
// TV Menu only
case MENU_ACTION_SAVE_ROOT:
if (!mDeviceHasTouchScreen)
{
showSubMenu(SaveLoadStateFragment.SaveOrLoad.SAVE);
}
return;
case MENU_ACTION_LOAD_ROOT:
if (!mDeviceHasTouchScreen)
{
showSubMenu(SaveLoadStateFragment.SaveOrLoad.LOAD);
}
return;
// Save state slots
case MENU_ACTION_SAVE_SLOT1:
NativeLibrary.SaveState(0, false);
return;
case MENU_ACTION_SAVE_SLOT2:
NativeLibrary.SaveState(1, false);
return;
case MENU_ACTION_SAVE_SLOT3:
NativeLibrary.SaveState(2, false);
return;
case MENU_ACTION_SAVE_SLOT4:
NativeLibrary.SaveState(3, false);
return;
case MENU_ACTION_SAVE_SLOT5:
NativeLibrary.SaveState(4, false);
return;
case MENU_ACTION_SAVE_SLOT6:
NativeLibrary.SaveState(5, false);
return;
// Load state slots
case MENU_ACTION_LOAD_SLOT1:
NativeLibrary.LoadState(0);
return;
case MENU_ACTION_LOAD_SLOT2:
NativeLibrary.LoadState(1);
return;
case MENU_ACTION_LOAD_SLOT3:
NativeLibrary.LoadState(2);
return;
case MENU_ACTION_LOAD_SLOT4:
NativeLibrary.LoadState(3);
return;
case MENU_ACTION_LOAD_SLOT5:
NativeLibrary.LoadState(4);
return;
case MENU_ACTION_LOAD_SLOT6:
NativeLibrary.LoadState(5);
return;
case MENU_ACTION_CHANGE_DISC:
FileBrowserHelper.openFilePicker(this, REQUEST_CHANGE_DISC);
return;
case MENU_ACTION_EXIT:
// ATV menu is built using a fragment, this will pop that fragment before emulation ends.
if (TvUtil.isLeanback(getApplicationContext()))
toggleMenu(); // Hide the menu (it will be showing since we just clicked it)
mEmulationFragment.stopEmulation();
finish();
return;
}
}
private void toggleJoystickRelCenter(boolean state)
{
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean("joystickRelCenter", state);
editor.commit();
}
private void toggleRumble(boolean state)
{
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean("phoneRumble", state);
editor.apply();
Rumble.setPhoneVibrator(state, this);
}
private void editControlsPlacement()
{
if (mEmulationFragment.isConfiguringControls())
{
mEmulationFragment.stopConfiguringControls();
}
else
{
mEmulationFragment.startConfiguringControls();
}
}
// Gets button presses
@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (mMenuVisible)
{
return super.dispatchKeyEvent(event);
}
int action;
switch (event.getAction())
{
case KeyEvent.ACTION_DOWN:
// Handling the case where the back button is pressed.
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{
onBackPressed();
return true;
}
// Normal key events.
action = NativeLibrary.ButtonState.PRESSED;
break;
case KeyEvent.ACTION_UP:
action = NativeLibrary.ButtonState.RELEASED;
break;
default:
return false;
}
InputDevice input = event.getDevice();
return NativeLibrary.onGamePadEvent(input.getDescriptor(), event.getKeyCode(), action);
}
private void toggleControls()
{
final SharedPreferences.Editor editor = mPreferences.edit();
boolean[] enabledButtons = new boolean[14];
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.emulation_toggle_controls);
if (sIsGameCubeGame || mPreferences.getInt("wiiController", 3) == 0)
{
for (int i = 0; i < enabledButtons.length; i++)
{
enabledButtons[i] = mPreferences.getBoolean("buttonToggleGc" + i, true);
}
builder.setMultiChoiceItems(R.array.gcpadButtons, enabledButtons,
(dialog, indexSelected, isChecked) -> editor
.putBoolean("buttonToggleGc" + indexSelected, isChecked));
}
else if (mPreferences.getInt("wiiController", 3) == 4)
{
for (int i = 0; i < enabledButtons.length; i++)
{
enabledButtons[i] = mPreferences.getBoolean("buttonToggleClassic" + i, true);
}
builder.setMultiChoiceItems(R.array.classicButtons, enabledButtons,
(dialog, indexSelected, isChecked) -> editor
.putBoolean("buttonToggleClassic" + indexSelected, isChecked));
}
else
{
for (int i = 0; i < enabledButtons.length; i++)
{
enabledButtons[i] = mPreferences.getBoolean("buttonToggleWii" + i, true);
}
if (mPreferences.getInt("wiiController", 3) == 3)
{
builder.setMultiChoiceItems(R.array.nunchukButtons, enabledButtons,
(dialog, indexSelected, isChecked) -> editor
.putBoolean("buttonToggleWii" + indexSelected, isChecked));
}
else
{
builder.setMultiChoiceItems(R.array.wiimoteButtons, enabledButtons,
(dialog, indexSelected, isChecked) -> editor
.putBoolean("buttonToggleWii" + indexSelected, isChecked));
}
}
builder.setNeutralButton(getString(R.string.emulation_toggle_all),
(dialogInterface, i) -> mEmulationFragment.toggleInputOverlayVisibility());
builder.setPositiveButton(getString(R.string.ok), (dialogInterface, i) ->
{
editor.apply();
mEmulationFragment.refreshInputOverlay();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void adjustScale()
{
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.dialog_seekbar, null);
final SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekbar);
final TextView value = (TextView) view.findViewById(R.id.text_value);
final TextView units = (TextView) view.findViewById(R.id.text_units);
seekbar.setMax(150);
seekbar.setProgress(mPreferences.getInt("controlScale", 50));
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
public void onStartTrackingTouch(SeekBar seekBar)
{
// Do nothing
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
value.setText(String.valueOf(progress + 50));
}
public void onStopTrackingTouch(SeekBar seekBar)
{
// Do nothing
}
});
value.setText(String.valueOf(seekbar.getProgress() + 50));
units.setText("%");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.emulation_control_scale);
builder.setView(view);
builder.setPositiveButton(getString(R.string.ok), (dialogInterface, i) ->
{
SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt("controlScale", seekbar.getProgress());
editor.apply();
mEmulationFragment.refreshInputOverlay();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void chooseController()
{
final SharedPreferences.Editor editor = mPreferences.edit();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.emulation_choose_controller);
builder.setSingleChoiceItems(R.array.controllersEntries,
mPreferences.getInt("wiiController", 3),
(dialog, indexSelected) ->
{
editor.putInt("wiiController", indexSelected);
NativeLibrary.SetConfig("WiimoteNew.ini", "Wiimote1", "Extension",
getResources().getStringArray(R.array.controllersValues)[indexSelected]);
});
builder.setPositiveButton(getString(R.string.ok), (dialogInterface, i) ->
{
editor.apply();
mEmulationFragment.refreshInputOverlay();
Toast.makeText(getApplication(), R.string.emulation_controller_changed, Toast.LENGTH_SHORT)
.show();
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
private void resetOverlay()
{
new AlertDialog.Builder(this)
.setTitle(getString(R.string.emulation_touch_overlay_reset))
.setPositiveButton(R.string.yes, (dialogInterface, i) ->
{
mEmulationFragment.resetInputOverlay();
})
.setNegativeButton(R.string.cancel, (dialogInterface, i) ->
{
})
.create()
.show();
}
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event)
{
if (mMenuVisible)
{
return false;
}
if (((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0))
{
return super.dispatchGenericMotionEvent(event);
}
// Don't attempt to do anything if we are disconnecting a device.
if (event.getActionMasked() == MotionEvent.ACTION_CANCEL)
return true;
InputDevice input = event.getDevice();
List<InputDevice.MotionRange> motions = input.getMotionRanges();
for (InputDevice.MotionRange range : motions)
{
int axis = range.getAxis();
float origValue = event.getAxisValue(axis);
float value = mControllerMappingHelper.scaleAxis(input, axis, origValue);
// If the input is still in the "flat" area, that means it's really zero.
// This is used to compensate for imprecision in joysticks.
if (Math.abs(value) > range.getFlat())
{
NativeLibrary.onGamePadMoveEvent(input.getDescriptor(), axis, value);
}
else
{
NativeLibrary.onGamePadMoveEvent(input.getDescriptor(), axis, 0.0f);
}
}
return true;
}
private void showSubMenu(SaveLoadStateFragment.SaveOrLoad saveOrLoad)
{
// Get rid of any visible submenu
getSupportFragmentManager().popBackStack(
BACKSTACK_NAME_SUBMENU, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Fragment fragment = SaveLoadStateFragment.newInstance(saveOrLoad);
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(
R.animator.menu_slide_in_from_right,
R.animator.menu_slide_out_to_right,
R.animator.menu_slide_in_from_right,
R.animator.menu_slide_out_to_right)
.replace(R.id.frame_submenu, fragment)
.addToBackStack(BACKSTACK_NAME_SUBMENU)
.commit();
}
public boolean deviceHasTouchScreen()
{
return mDeviceHasTouchScreen;
}
public String getSelectedTitle()
{
return mSelectedTitle;
}
public static boolean isGameCubeGame()
{
return sIsGameCubeGame;
}
public boolean isActivityRecreated()
{
return activityRecreated;
}
public Settings getSettings()
{
return mSettings;
}
}