~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Source/Android/src/org/dolphinemu/dolphinemu/emulation/EmulationActivity.java

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2013 Dolphin Emulator Project
 
3
 * Licensed under GPLv2
 
4
 * Refer to the license.txt file included.
 
5
 */
 
6
 
 
7
package org.dolphinemu.dolphinemu.emulation;
 
8
 
 
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;
 
21
 
 
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;
 
26
 
 
27
import java.util.List;
 
28
 
 
29
/**
 
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.
 
32
 */
 
33
public final class EmulationActivity extends Activity
 
34
{
 
35
        private boolean Running;
 
36
        private boolean IsActionBarHidden = false;
 
37
        private float screenWidth;
 
38
        private float screenHeight;
 
39
 
 
40
        @Override
 
41
        public void onCreate(Bundle savedInstanceState)
 
42
        {
 
43
                super.onCreate(savedInstanceState);
 
44
 
 
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;
 
51
 
 
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);
 
56
 
 
57
                // Set the transparency for the action bar.
 
58
                ColorDrawable actionBarBackground = new ColorDrawable(Color.parseColor("#303030"));
 
59
                actionBarBackground.setAlpha(175);
 
60
                getActionBar().setBackgroundDrawable(actionBarBackground);
 
61
 
 
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
 
66
                // load the game.
 
67
                Intent gameToEmulate = getIntent();
 
68
 
 
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);
 
78
                else
 
79
                        NativeLibrary.SetDimensions((int)screenWidth, (int)screenHeight);
 
80
                NativeLibrary.SetFilename(gameToEmulate.getStringExtra("SelectedGame"));
 
81
                Running = true;
 
82
 
 
83
                // Set the emulation window.
 
84
                setContentView(R.layout.emulation_view);
 
85
 
 
86
                // Hide the action bar by default so it doesn't get in the way.
 
87
                getActionBar().hide();
 
88
                IsActionBarHidden = true;
 
89
        }
 
90
 
 
91
        @Override
 
92
        public void onStop()
 
93
        {
 
94
                super.onStop();
 
95
 
 
96
                if (Running)
 
97
                        NativeLibrary.StopEmulation();
 
98
        }
 
99
 
 
100
        @Override
 
101
        public void onPause()
 
102
        {
 
103
                super.onPause();
 
104
 
 
105
                if (Running)
 
106
                        NativeLibrary.PauseEmulation();
 
107
        }
 
108
 
 
109
        @Override
 
110
        public void onResume()
 
111
        {
 
112
                super.onResume();
 
113
 
 
114
                if (Running)
 
115
                        NativeLibrary.UnPauseEmulation();
 
116
        }
 
117
        
 
118
        @Override
 
119
        public void onDestroy()
 
120
        {
 
121
                super.onDestroy();
 
122
 
 
123
                if (Running)
 
124
                {
 
125
                        NativeLibrary.StopEmulation();
 
126
                        Running = false;
 
127
                }
 
128
        }
 
129
        
 
130
        @Override
 
131
        public boolean onTouchEvent(MotionEvent event)
 
132
        {
 
133
                float X = event.getX();
 
134
                float Y = event.getY();
 
135
                int Action = event.getActionMasked();
 
136
 
 
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;
 
140
 
 
141
                NativeLibrary.onTouchEvent(Action, ScreenX, ScreenY);
 
142
                
 
143
                return false;
 
144
        }
 
145
        
 
146
        @Override
 
147
        public void onBackPressed()
 
148
        {
 
149
                // The back button in the emulation
 
150
                // window is the toggle for the action bar.
 
151
                if (IsActionBarHidden)
 
152
                {
 
153
                        IsActionBarHidden = false;
 
154
                        getActionBar().show();
 
155
                }
 
156
                else
 
157
                {
 
158
                        IsActionBarHidden = true;
 
159
                        getActionBar().hide();
 
160
                }
 
161
        }
 
162
 
 
163
        @Override
 
164
        public boolean onCreateOptionsMenu(Menu menu)
 
165
        {
 
166
                MenuInflater inflater = getMenuInflater();
 
167
                inflater.inflate(R.menu.emuwindow_overlay, menu);
 
168
                return true;
 
169
        }
 
170
 
 
171
        @Override
 
172
        public boolean onMenuItemSelected(int itemId, MenuItem item)
 
173
        {
 
174
                switch(item.getItemId())
 
175
                {
 
176
                        // Save state slots
 
177
                        case R.id.saveSlot1:
 
178
                                NativeLibrary.SaveState(0);
 
179
                                return true;
 
180
 
 
181
                        case R.id.saveSlot2:
 
182
                                NativeLibrary.SaveState(1);
 
183
                                return true;
 
184
 
 
185
                        case R.id.saveSlot3:
 
186
                                NativeLibrary.SaveState(2);
 
187
                                return true;
 
188
 
 
189
                        case R.id.saveSlot4:
 
190
                                NativeLibrary.SaveState(3);
 
191
                                return true;
 
192
 
 
193
                        case R.id.saveSlot5:
 
194
                                NativeLibrary.SaveState(4);
 
195
                                return true;
 
196
 
 
197
                        // Load state slot
 
198
                        case R.id.loadSlot1:
 
199
                                NativeLibrary.LoadState(0);
 
200
                                return true;
 
201
 
 
202
                        case R.id.loadSlot2:
 
203
                                NativeLibrary.LoadState(1);
 
204
                                return true;
 
205
 
 
206
                        case R.id.loadSlot3:
 
207
                                NativeLibrary.LoadState(2);
 
208
                                return true;
 
209
 
 
210
                        case R.id.loadSlot4:
 
211
                                NativeLibrary.LoadState(3);
 
212
                                return true;
 
213
 
 
214
                        case R.id.loadSlot5:
 
215
                                NativeLibrary.LoadState(4);
 
216
                                return true;
 
217
 
 
218
                        case R.id.exitEmulation:
 
219
                        {
 
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)
 
226
                                        {
 
227
                                                finish();
 
228
                                        }
 
229
                                });
 
230
                                builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
 
231
                                        public void onClick(DialogInterface dialog, int which)
 
232
                                        {
 
233
                                                // Do nothing. Just makes the No button appear.
 
234
                                        }
 
235
                                });
 
236
                                builder.show();
 
237
                                return true;
 
238
                        }
 
239
 
 
240
                        default:
 
241
                                return super.onMenuItemSelected(itemId, item);
 
242
                }
 
243
        }
 
244
 
 
245
        // Gets button presses
 
246
        @Override
 
247
        public boolean dispatchKeyEvent(KeyEvent event)
 
248
        {
 
249
                int action = 0;
 
250
 
 
251
                if (Running)
 
252
                {
 
253
                        switch (event.getAction())
 
254
                        {
 
255
                                case KeyEvent.ACTION_DOWN:
 
256
                                        // Handling the case where the back button is pressed.
 
257
                                        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
 
258
                                        {
 
259
                                                onBackPressed();
 
260
                                                return true;
 
261
                                        }
 
262
 
 
263
                                        // Normal key events.
 
264
                                        action = 0;
 
265
                                        break;
 
266
                                case KeyEvent.ACTION_UP:
 
267
                                        action = 1;
 
268
                                        break;
 
269
                                default:
 
270
                                        return false;
 
271
                        }
 
272
                        InputDevice input = event.getDevice();
 
273
                        NativeLibrary.onGamePadEvent(InputConfigFragment.getInputDesc(input), event.getKeyCode(), action);
 
274
                        return true;
 
275
                }
 
276
                return false;
 
277
        }
 
278
 
 
279
        @Override
 
280
        public boolean dispatchGenericMotionEvent(MotionEvent event)
 
281
        {
 
282
                if (((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == 0) || !Running)
 
283
                {
 
284
                        return super.dispatchGenericMotionEvent(event);
 
285
                }
 
286
 
 
287
                InputDevice input = event.getDevice();
 
288
                List<InputDevice.MotionRange> motions = input.getMotionRanges();
 
289
 
 
290
                for (InputDevice.MotionRange range : motions)
 
291
                {
 
292
                        NativeLibrary.onGamePadMoveEvent(InputConfigFragment.getInputDesc(input), range.getAxis(), event.getAxisValue(range.getAxis()));
 
293
                }
 
294
 
 
295
                return true;
 
296
        }
 
297
}