diff --git a/app/build.gradle b/app/build.gradle index 6ee6915..cd4f8f2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "com.nicobrailo.pianoli" minSdkVersion 21 targetSdkVersion 33 - versionCode 20 - versionName "1.20" + versionCode 21 + versionName "1.21" } diff --git a/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java b/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java index 79b9ef8..4341f74 100644 --- a/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java +++ b/app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java @@ -16,6 +16,7 @@ import android.view.SurfaceHolder; import android.view.SurfaceView; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.ColorUtils; @@ -32,29 +33,7 @@ class PianoCanvas extends SurfaceView implements SurfaceHolder.Callback { final AppConfigTrigger appConfigHandler; final int screen_size_y, screen_size_x; - final int[] KEY_COLORS = new int[]{ - Color.rgb(220, 0, 0), // Red - Color.rgb(255, 135, 0), // Orange - Color.rgb(255, 255, 0), // Yellow - Color.rgb(80, 220, 20), // Light Green - Color.rgb(0, 150, 150), // Dark Green - Color.rgb(95, 70, 165), // Purple - Color.rgb(213, 43, 149), // Pink - }; - - /** - * Note that light green, orange and yellow have higher lightness than other colours, so adding just a little white doesn't have - * the desired effect. That is why they have a larger proportion of white added in. - */ - final int[] PRESSED_KEY_COLORS = new int[]{ - ColorUtils.blendARGB(KEY_COLORS[0], Color.WHITE, 0.5f), // Red - ColorUtils.blendARGB(KEY_COLORS[1], Color.WHITE, 0.6f), // Orange - ColorUtils.blendARGB(KEY_COLORS[2], Color.WHITE, 0.75f), // Yellow - ColorUtils.blendARGB(KEY_COLORS[3], Color.WHITE, 0.6f), // Light Green - ColorUtils.blendARGB(KEY_COLORS[4], Color.WHITE, 0.5f), // Dark Green - ColorUtils.blendARGB(KEY_COLORS[5], Color.WHITE, 0.5f), // Purple - ColorUtils.blendARGB(KEY_COLORS[6], Color.WHITE, 0.5f), // Pink - }; + final Theme theme; Map touch_pointer_to_keys = new HashMap<>(); @@ -67,6 +46,8 @@ public PianoCanvas(Context context, AttributeSet as, int defStyle) { this.setFocusable(true); this.getHolder().addCallback(this); + theme = Theme.fromPreferences(context); + final Point screen_size = new Point(); final AppCompatActivity ctx; try { @@ -107,9 +88,8 @@ void draw_all_keys(final Canvas canvas) { for (int i = 0; i < piano.get_keys_count(); i += 2) { // Draw big key - final int col_idx = (i / 2) % KEY_COLORS.length; Paint big_key_paint = new Paint(); - big_key_paint.setColor(piano.is_key_pressed(i) ? PRESSED_KEY_COLORS[col_idx] : KEY_COLORS[col_idx]); + big_key_paint.setColor(theme.getColorForKey(i, piano.is_key_pressed(i))); draw_key(canvas, piano.get_area_for_key(i), big_key_paint); } @@ -219,7 +199,7 @@ void draw_icon_on_black_key(final Canvas canvas, final Drawable icon, Integer ke } @Override - public void surfaceCreated(SurfaceHolder surfaceHolder) { + public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) { redraw(surfaceHolder); } @@ -254,7 +234,7 @@ void resetPianoState() { // Something has gone wrong with the piano or canvas state, and our state is out of sync // with the real state of the world (eg somehow we missed a touch down or up event). // Try to reset the state and hope the app survives. - touch_pointer_to_keys.clear();; + touch_pointer_to_keys.clear(); piano.resetState(); } @@ -336,12 +316,12 @@ public boolean onTouchEvent(MotionEvent event) { @Override - public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { + public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override - public void surfaceDestroyed(SurfaceHolder surfaceHolder) { + public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) { } } diff --git a/app/src/main/java/com/nicobrailo/pianoli/Preferences.java b/app/src/main/java/com/nicobrailo/pianoli/Preferences.java index 8e83360..191f818 100644 --- a/app/src/main/java/com/nicobrailo/pianoli/Preferences.java +++ b/app/src/main/java/com/nicobrailo/pianoli/Preferences.java @@ -20,6 +20,8 @@ public class Preferences { private final static String PREF_SELECTED_SOUND_SET = "selectedSoundSet"; private final static String PREF_SELECTED_MELODIES = "selectedMelodies"; private final static String PREF_ENABLE_MELODIES = "enableMelodies"; + private static final String DEFAULT_THEME = "boomwhacker"; + private final static String PREF_THEME = "theme"; /** * If none are selected, then we play all melodies. @@ -43,6 +45,10 @@ public static List selectedMelodies(Context context) { return melodies; } + public static String selectedTheme(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context).getString(PREF_THEME, DEFAULT_THEME); + } + public static boolean areMelodiesEnabled(Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_ENABLE_MELODIES, false); } diff --git a/app/src/main/java/com/nicobrailo/pianoli/Theme.java b/app/src/main/java/com/nicobrailo/pianoli/Theme.java new file mode 100644 index 0000000..2832064 --- /dev/null +++ b/app/src/main/java/com/nicobrailo/pianoli/Theme.java @@ -0,0 +1,66 @@ +package com.nicobrailo.pianoli; + +import android.content.Context; +import android.graphics.Color; + +import androidx.core.graphics.ColorUtils; + +public class Theme { + + private final int[] colours; + private final int[] pressedColours; + + public static Theme fromPreferences(Context context) { + String selectedTheme = Preferences.selectedTheme(context); + switch (selectedTheme) { + case "black_and_white": + return BLACK_AND_WHITE; + + default: + return BOOMWHACKER; + } + } + + private static final int[] BOOMWHACKER_COLORS = new int[] { + Color.rgb(220, 0, 0), // Red + Color.rgb(255, 135, 0), // Orange + Color.rgb(255, 255, 0), // Yellow + Color.rgb(80, 220, 20), // Light Green + Color.rgb(0, 150, 150), // Dark Green + Color.rgb(95, 70, 165), // Purple + Color.rgb(213, 43, 149), // Pink + }; + + /** + * Note that light green, orange and yellow have higher lightness than other colours, + * so adding just a little white doesn't have the desired effect. + * That is why they have a larger proportion of white added in. + */ + private static final int[] BOOMWHACKER_PRESSED_COLORS = new int[] { + ColorUtils.blendARGB(BOOMWHACKER_COLORS[0], Color.WHITE, 0.5f), // Red + ColorUtils.blendARGB(BOOMWHACKER_COLORS[1], Color.WHITE, 0.6f), // Orange + ColorUtils.blendARGB(BOOMWHACKER_COLORS[2], Color.WHITE, 0.75f), // Yellow + ColorUtils.blendARGB(BOOMWHACKER_COLORS[3], Color.WHITE, 0.6f), // Light Green + ColorUtils.blendARGB(BOOMWHACKER_COLORS[4], Color.WHITE, 0.5f), // Dark Green + ColorUtils.blendARGB(BOOMWHACKER_COLORS[5], Color.WHITE, 0.5f), // Purple + ColorUtils.blendARGB(BOOMWHACKER_COLORS[6], Color.WHITE, 0.5f), // Pink + }; + + private static final Theme BOOMWHACKER = new Theme(BOOMWHACKER_COLORS, BOOMWHACKER_PRESSED_COLORS); + + private static final Theme BLACK_AND_WHITE = new Theme( + new int[] { Color.rgb(240, 240, 240) }, + new int[] { Color.rgb(200, 200, 200) } + ); + + private Theme(int[] colors, int[] pressedColors) { + this.colours = colors; + this.pressedColours = pressedColors; + } + + public int getColorForKey(int keyIndex, boolean isPressed) { + final int col_idx = (keyIndex / 2) % colours.length; + return isPressed ? pressedColours[col_idx] : colours[col_idx]; + } + +} diff --git a/app/src/main/res/values/donottranslate.xml b/app/src/main/res/values/donottranslate.xml index f709012..d8fa01b 100644 --- a/app/src/main/res/values/donottranslate.xml +++ b/app/src/main/res/values/donottranslate.xml @@ -15,4 +15,14 @@ and may not be as common as some of the other nursery rhymes. It is there to show what it sounds like to have longer melodies available. --> + + + boomwhacker + black_and_white + + + + @string/theme_boomwhacker + @string/theme_black_and_white + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b00748..3ae2ddf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,4 +20,7 @@ Auto play melodies Available melodies List of melodies to cycle through + Theme + Black and White + Boomwhacker diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 184285d..4f2be12 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -9,6 +9,14 @@ app:title="@string/sound_set" app:useSimpleSummaryProvider="true" app:iconSpaceReserved="false" /> +