2
* Copyright 2013 Dolphin Emulator Project
4
* Refer to the license.txt file included.
7
package org.dolphinemu.dolphinemu.emulation;
9
import android.app.Activity;
10
import android.app.AlertDialog;
11
import android.content.DialogInterface;
12
import android.content.Intent;
13
import android.content.SharedPreferences;
14
import android.graphics.Color;
15
import android.graphics.drawable.ColorDrawable;
16
import android.os.Bundle;
17
import android.preference.PreferenceManager;
18
import android.util.DisplayMetrics;
19
import android.view.*;
20
import android.view.WindowManager.LayoutParams;
22
import org.dolphinemu.dolphinemu.NativeLibrary;
23
import org.dolphinemu.dolphinemu.R;
24
import org.dolphinemu.dolphinemu.settings.InputConfigFragment;
25
import org.dolphinemu.dolphinemu.settings.VideoSettingsFragment;
27
import java.util.List;
30
* This is the activity where all of the emulation handling happens.
31
* This activity is responsible for displaying the SurfaceView that we render to.
33
public final class EmulationActivity extends Activity
35
private boolean Running;
36
private boolean IsActionBarHidden = false;
37
private float screenWidth;
38
private float screenHeight;
41
public void onCreate(Bundle savedInstanceState)
43
super.onCreate(savedInstanceState);
45
// Retrieve screen dimensions.
46
DisplayMetrics displayMetrics = new DisplayMetrics();
47
WindowManager wm = getWindowManager();
48
wm.getDefaultDisplay().getMetrics(displayMetrics);
49
this.screenHeight = displayMetrics.heightPixels;
50
this.screenWidth = displayMetrics.widthPixels;
52
// Request window features for the emulation view.
53
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
54
getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);
55
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
57
// Set the transparency for the action bar.
58
ColorDrawable actionBarBackground = new ColorDrawable(Color.parseColor("#303030"));
59
actionBarBackground.setAlpha(175);
60
getActionBar().setBackgroundDrawable(actionBarBackground);
62
// Set the native rendering screen width/height.
63
// Also get the intent passed from the GameList when the game
64
// was selected. This is so the path of the game can be retrieved
65
// and set on the native side of the code so the emulator can actually
67
Intent gameToEmulate = getIntent();
69
// Due to a bug in Adreno, it renders the screen rotated 90 degrees when using OpenGL
70
// Flip the width and height when on Adreno to work around this.
71
// Mali isn't affected by this bug.
72
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
73
if (prefs.getString("gpuPref", "Software Rendering").equals("OGL")
74
&& VideoSettingsFragment.SupportsGLES3()
75
&& VideoSettingsFragment.m_GLVendor != null
76
&& VideoSettingsFragment.m_GLVendor.equals("Qualcomm"))
77
NativeLibrary.SetDimensions((int)screenHeight, (int)screenWidth);
79
NativeLibrary.SetDimensions((int)screenWidth, (int)screenHeight);
80
NativeLibrary.SetFilename(gameToEmulate.getStringExtra("SelectedGame"));
83
// Set the emulation window.
84
setContentView(R.layout.emulation_view);
86
// Hide the action bar by default so it doesn't get in the way.
87
getActionBar().hide();
88
IsActionBarHidden = true;
97
NativeLibrary.StopEmulation();
101
public void onPause()
106
NativeLibrary.PauseEmulation();
110
public void onResume()
115
NativeLibrary.UnPauseEmulation();
119
public void onDestroy()
125
NativeLibrary.StopEmulation();
131
public boolean onTouchEvent(MotionEvent event)
133
float X = event.getX();
134
float Y = event.getY();
135
int Action = event.getActionMasked();
137
// Converts button locations 0 - 1 to OGL screen coords -1.0 - 1.0
138
float ScreenX = ((X / screenWidth) * 2.0f) - 1.0f;
139
float ScreenY = ((Y / screenHeight) * -2.0f) + 1.0f;
141
NativeLibrary.onTouchEvent(Action, ScreenX, ScreenY);
147
public void onBackPressed()
149
// The back button in the emulation
150
// window is the toggle for the action bar.
151
if (IsActionBarHidden)
153
IsActionBarHidden = false;
154
getActionBar().show();
158
IsActionBarHidden = true;
159
getActionBar().hide();
164
public boolean onCreateOptionsMenu(Menu menu)
166
MenuInflater inflater = getMenuInflater();
167
inflater.inflate(R.menu.emuwindow_overlay, menu);
172
public boolean onMenuItemSelected(int itemId, MenuItem item)
174
switch(item.getItemId())
178
NativeLibrary.SaveState(0);
182
NativeLibrary.SaveState(1);
186
NativeLibrary.SaveState(2);
190
NativeLibrary.SaveState(3);
194
NativeLibrary.SaveState(4);
199
NativeLibrary.LoadState(0);
203
NativeLibrary.LoadState(1);
207
NativeLibrary.LoadState(2);
211
NativeLibrary.LoadState(3);
215
NativeLibrary.LoadState(4);
218
case R.id.exitEmulation:
220
// Create a confirmation method for quitting the current emulation instance.
221
AlertDialog.Builder builder = new AlertDialog.Builder(this);
222
builder.setTitle(getString(R.string.overlay_exit_emulation));
223
builder.setMessage(R.string.overlay_exit_emulation_confirm);
224
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
225
public void onClick(DialogInterface dialog, int which)
230
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
231
public void onClick(DialogInterface dialog, int which)
233
// Do nothing. Just makes the No button appear.
241
return super.onMenuItemSelected(itemId, item);
245
// Gets button presses
247
public boolean dispatchKeyEvent(KeyEvent event)
253
switch (event.getAction())
255
case KeyEvent.ACTION_DOWN:
256
// Handling the case where the back button is pressed.
257
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
263
// Normal key events.
266
case KeyEvent.ACTION_UP:
272
InputDevice input = event.getDevice();
273
NativeLibrary.onGamePadEvent(InputConfigFragment.getInputDesc(input), event.getKeyCode(), action);
280
public boolean dispatchGenericMotionEvent(MotionEvent event)
282
if (((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0) || !Running)
284
return super.dispatchGenericMotionEvent(event);
287
InputDevice input = event.getDevice();
288
List<InputDevice.MotionRange> motions = input.getMotionRanges();
290
for (InputDevice.MotionRange range : motions)
292
NativeLibrary.onGamePadMoveEvent(InputConfigFragment.getInputDesc(input), range.getAxis(), event.getAxisValue(range.getAxis()));