From 4ddaf75ce0af6a5e11634658522405f39c28d410 Mon Sep 17 00:00:00 2001 From: dgis Date: Wed, 27 Mar 2019 23:39:45 +0100 Subject: [PATCH] Begin to add a custom background color. --- app/src/main/cpp/core/engine.c | 4 +- .../eight/AppCompatPreferenceActivity.java | 110 ------------------ .../emulator/forty/eight/MainActivity.java | 19 ++- .../emulator/forty/eight/MainScreenView.java | 41 ++++++- .../forty/eight/SettingsActivity.java | 44 +++++++ app/src/main/res/values/arrays.xml | 15 +++ app/src/main/res/values/strings.xml | 8 +- app/src/main/res/xml/pref_general.xml | 23 ++++ 8 files changed, 145 insertions(+), 119 deletions(-) delete mode 100644 app/src/main/java/org/emulator/forty/eight/AppCompatPreferenceActivity.java create mode 100644 app/src/main/res/values/arrays.xml diff --git a/app/src/main/cpp/core/engine.c b/app/src/main/cpp/core/engine.c index 3bff90b..bf93f4e 100644 --- a/app/src/main/cpp/core/engine.c +++ b/app/src/main/cpp/core/engine.c @@ -292,7 +292,9 @@ static __inline VOID AdjustSpeed(VOID) // adjust emulation speed { DWORD dwCycles,dwTicks; - EnterCriticalSection(&csSlowLock); + LOGD("bEnableSlow: %d, bCpuSlow: %d, bKeySlow: %d, bSoundSlow: %d, nOpcSlow: %d", bEnableSlow, bCpuSlow, bKeySlow, bSoundSlow, nOpcSlow); + + EnterCriticalSection(&csSlowLock); { // cycles elapsed for next check if ((dwCycles = (DWORD) (Chipset.cycles & 0xFFFFFFFF)-dwOldCyc) >= dwT2Cycles) diff --git a/app/src/main/java/org/emulator/forty/eight/AppCompatPreferenceActivity.java b/app/src/main/java/org/emulator/forty/eight/AppCompatPreferenceActivity.java deleted file mode 100644 index ee7eb4c..0000000 --- a/app/src/main/java/org/emulator/forty/eight/AppCompatPreferenceActivity.java +++ /dev/null @@ -1,110 +0,0 @@ -package org.emulator.forty.eight; - -import android.content.res.Configuration; -import android.os.Bundle; -import android.preference.PreferenceActivity; -import android.view.MenuInflater; -import android.view.View; -import android.view.ViewGroup; - -import androidx.annotation.LayoutRes; -import androidx.annotation.Nullable; -import androidx.appcompat.app.ActionBar; -import androidx.appcompat.app.AppCompatDelegate; -import androidx.appcompat.widget.Toolbar; - -/** - * A {@link PreferenceActivity} which implements and proxies the necessary calls - * to be used with AppCompat. - */ -public abstract class AppCompatPreferenceActivity extends PreferenceActivity { - - private AppCompatDelegate mDelegate; - - @Override - protected void onCreate(Bundle savedInstanceState) { - getDelegate().installViewFactory(); - getDelegate().onCreate(savedInstanceState); - super.onCreate(savedInstanceState); - } - - @Override - protected void onPostCreate(Bundle savedInstanceState) { - super.onPostCreate(savedInstanceState); - getDelegate().onPostCreate(savedInstanceState); - } - - public ActionBar getSupportActionBar() { - return getDelegate().getSupportActionBar(); - } - - public void setSupportActionBar(@Nullable Toolbar toolbar) { - getDelegate().setSupportActionBar(toolbar); - } - - @Override - public MenuInflater getMenuInflater() { - return getDelegate().getMenuInflater(); - } - - @Override - public void setContentView(@LayoutRes int layoutResID) { - getDelegate().setContentView(layoutResID); - } - - @Override - public void setContentView(View view) { - getDelegate().setContentView(view); - } - - @Override - public void setContentView(View view, ViewGroup.LayoutParams params) { - getDelegate().setContentView(view, params); - } - - @Override - public void addContentView(View view, ViewGroup.LayoutParams params) { - getDelegate().addContentView(view, params); - } - - @Override - protected void onPostResume() { - super.onPostResume(); - getDelegate().onPostResume(); - } - - @Override - protected void onTitleChanged(CharSequence title, int color) { - super.onTitleChanged(title, color); - getDelegate().setTitle(title); - } - - @Override - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); - getDelegate().onConfigurationChanged(newConfig); - } - - @Override - protected void onStop() { - super.onStop(); - getDelegate().onStop(); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - getDelegate().onDestroy(); - } - - public void invalidateOptionsMenu() { - getDelegate().invalidateOptionsMenu(); - } - - private AppCompatDelegate getDelegate() { - if (mDelegate == null) { - mDelegate = AppCompatDelegate.create(this, null); - } - return mDelegate; - } -} diff --git a/app/src/main/java/org/emulator/forty/eight/MainActivity.java b/app/src/main/java/org/emulator/forty/eight/MainActivity.java index 0301d94..800c5e1 100644 --- a/app/src/main/java/org/emulator/forty/eight/MainActivity.java +++ b/app/src/main/java/org/emulator/forty/eight/MainActivity.java @@ -23,6 +23,7 @@ import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.view.ViewGroup; +import android.view.Window; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; @@ -112,14 +113,15 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On - - - sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); mainActivity = this; ViewGroup mainScreenContainer = findViewById(R.id.main_screen_container); mainScreenView = new MainScreenView(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + mainScreenView.setStatusBarColor(getWindow().getStatusBarColor()); + } toolbar.setVisibility(View.GONE); mainScreenContainer.addView(mainScreenView, 0); @@ -1201,6 +1203,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On String[] settingKeys = { "settings_realspeed", "settings_grayscale", "settings_allow_rotation", "settings_fill_screen", "settings_scale", "settings_allow_sound", "settings_haptic_feedback", + "settings_background_kml_color", "settings_background_fallback_color", "settings_kml", "settings_port1", "settings_port2" }; for (String settingKey : settingKeys) { updateFromPreferences(settingKey, false); @@ -1236,6 +1239,16 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On // Nothing to do break; + case "settings_background_kml_color": + mainScreenView.setBackgroundKmlColor(sharedPreferences.getBoolean("settings_background_kml_color", false)); + break; + case "settings_background_fallback_color": + String fallbackColor = sharedPreferences.getString("settings_background_fallback_color", "0"); + try { + mainScreenView.setBackgroundFallbackColor(Integer.parseInt(fallbackColor)); + } catch (NumberFormatException ex) {} + break; + case "settings_kml": case "settings_kml_default": case "settings_kml_folder": diff --git a/app/src/main/java/org/emulator/forty/eight/MainScreenView.java b/app/src/main/java/org/emulator/forty/eight/MainScreenView.java index 76ca65f..0af96de 100644 --- a/app/src/main/java/org/emulator/forty/eight/MainScreenView.java +++ b/app/src/main/java/org/emulator/forty/eight/MainScreenView.java @@ -8,6 +8,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.os.Build; import android.util.DisplayMetrics; import android.util.Log; import android.view.KeyEvent; @@ -28,7 +29,10 @@ public class MainScreenView extends SurfaceView { private float screenOffsetY= 0.0f; private boolean fillScreen = false; private float fixScale = 0.0f; - private int backgroundColor = Color.BLACK; + private int kmlBackgroundColor = Color.BLACK; + private boolean useKmlBackgroundColor = false; + private int fallbackBackgroundColorType = 0; + private int statusBarColor = 0; public MainScreenView(Context context) { super(context); @@ -223,7 +227,7 @@ public class MainScreenView extends SurfaceView { protected void onDraw(Canvas canvas) { //Log.d(TAG, "Emu48-PAINT onDraw() mIsScaling: " + mIsScaling + ", mIsPanning: " + mIsPanning + ", mIsFlinging: " + mIsFlinging); - canvas.drawColor(backgroundColor); + canvas.drawColor(getBackgroundColor()); canvas.save(); canvas.translate(screenOffsetX, screenOffsetY); @@ -248,8 +252,9 @@ public class MainScreenView extends SurfaceView { Bitmap oldBitmapMainScreen = bitmapMainScreen; bitmapMainScreen = Bitmap.createBitmap(Math.max(1, param1), Math.max(1, param2), Bitmap.Config.ARGB_8888); int globalColor = NativeLib.getGlobalColor(); - backgroundColor = Color.argb(255, (globalColor & 0x00FF0000) >> 16, (globalColor & 0x0000FF00) >> 8, globalColor & 0x000000FF); - bitmapMainScreen.eraseColor(backgroundColor); + kmlBackgroundColor = Color.argb(255, (globalColor & 0x00FF0000) >> 16, (globalColor & 0x0000FF00) >> 8, globalColor & 0x000000FF); + + bitmapMainScreen.eraseColor(getBackgroundColor()); NativeLib.changeBitmap(bitmapMainScreen); if(oldBitmapMainScreen != null) { @@ -277,4 +282,32 @@ public class MainScreenView extends SurfaceView { calcTranslateAndScale(getWidth(), getHeight()); postInvalidate(); } + + public void setBackgroundKmlColor(boolean useKmlBackgroundColor) { + this.useKmlBackgroundColor = useKmlBackgroundColor; + postInvalidate(); + } + + public void setBackgroundFallbackColor(int fallbackBackgroundColorType) { + this.fallbackBackgroundColorType = fallbackBackgroundColorType; + postInvalidate(); + } + + public void setStatusBarColor(int statusBarColor) { + this.statusBarColor = statusBarColor; + } + + + + private int getBackgroundColor() { + if(useKmlBackgroundColor) { + return kmlBackgroundColor; + } else switch(fallbackBackgroundColorType) { + case 0: + return 0; + case 1: + return statusBarColor; + } + return 0; + } } diff --git a/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java b/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java index 86af7cc..d2e2692 100644 --- a/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java +++ b/app/src/main/java/org/emulator/forty/eight/SettingsActivity.java @@ -115,12 +115,56 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere addPreferencesFromResource(R.xml.pref_general); setHasOptionsMenu(true); + // Sound settings + Preference preferenceAllowSound = findPreference("settings_allow_sound"); if(preferenceAllowSound != null && !NativeLib.getSoundEnabled()) { preferenceAllowSound.setSummary("Cannot initialize the sound engine."); preferenceAllowSound.setEnabled(false); } + // Background color settings + + Preference preferenceBackgroundFallbackColor = findPreference("settings_background_fallback_color"); +// final ColorPickerPreferenceCompat preferenceBackgroundCustomColor = (ColorPickerPreferenceCompat)findPreference("settings_background_custom_color"); + if(preferenceBackgroundFallbackColor != null /*&& preferenceBackgroundCustomColor != null*/) { + final String[] stringArrayBackgroundFallbackColor = getResources().getStringArray(R.array.settings_background_fallback_color_item); + Preference.OnPreferenceChangeListener onPreferenceChangeListenerBackgroundFallbackColor = new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + if(value != null) { + String stringValue = value.toString(); + int backgroundFallbackColor = -1; + try { + backgroundFallbackColor = Integer.parseInt(stringValue); + } catch (NumberFormatException ex) {} + if(backgroundFallbackColor >= 0 && backgroundFallbackColor < stringArrayBackgroundFallbackColor.length) + preference.setSummary(stringArrayBackgroundFallbackColor[backgroundFallbackColor]); +// preferenceBackgroundCustomColor.setEnabled(backgroundFallbackColor == 2); + } + return true; + } + }; + preferenceBackgroundFallbackColor.setOnPreferenceChangeListener(onPreferenceChangeListenerBackgroundFallbackColor); + onPreferenceChangeListenerBackgroundFallbackColor.onPreferenceChange(preferenceBackgroundFallbackColor, + sharedPreferences.getString(preferenceBackgroundFallbackColor.getKey(), "0")); + + + //preferenceBackgroundCustomColor.setColorValue(customColor); + +// Preference.OnPreferenceChangeListener onPreferenceChangeListenerBackgroundCustomColor = new Preference.OnPreferenceChangeListener() { +// @Override +// public boolean onPreferenceChange(Preference preference, Object value) { +// if(value != null) { +// int customColor = (Integer)value; +// } +// return true; +// } +// }; +// preferenceBackgroundCustomColor.setOnPreferenceChangeListener(onPreferenceChangeListenerBackgroundCustomColor); +// onPreferenceChangeListenerBackgroundCustomColor.onPreferenceChange(preferenceBackgroundCustomColor, sharedPreferences.getBoolean(preferenceBackgroundCustomColor.getKey(), false)); + } + // Ports 1 & 2 settings final Preference preferencePort1en = findPreference("settings_port1en"); diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..dff18c7 --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,15 @@ + + + + + Black (default) + Status bar color + + + + 0 + 1 + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6af57b6..0b9ad55 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -66,6 +66,13 @@ Fill screen Allow sounds Allow haptic feedback + Background Color + Use KML color + Use default KML transparency color of the background bitmap (if exist) + Other possible colors + Else if the KML color is not used + Custom color + Used when selecting the \'Custom color\' in the \'Other possible colors\' Memory Cards Port 1 is Plugged Port 1 is Writable @@ -73,5 +80,4 @@ Port 2 is Writable Port 2 File - diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml index 209a8d3..5ba6d7d 100644 --- a/app/src/main/res/xml/pref_general.xml +++ b/app/src/main/res/xml/pref_general.xml @@ -42,6 +42,29 @@ android:defaultValue="true" /> + + + + + + + + + + +