Skip to content

Commit

Permalink
Merge pull request #58 from pserwylo/themes
Browse files Browse the repository at this point in the history
Themes, and a bump for release v1.21
  • Loading branch information
pserwylo authored Sep 1, 2023
2 parents 85405b8 + 51595ce commit 0d3bbfd
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.nicobrailo.pianoli"
minSdkVersion 21
targetSdkVersion 33
versionCode 20
versionName "1.20"
versionCode 21
versionName "1.21"
}


Expand Down
38 changes: 9 additions & 29 deletions app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Integer, Integer> touch_pointer_to_keys = new HashMap<>();

Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {

}
}
6 changes: 6 additions & 0 deletions app/src/main/java/com/nicobrailo/pianoli/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -43,6 +45,10 @@ public static List<Melody> 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);
}
Expand Down
66 changes: 66 additions & 0 deletions app/src/main/java/com/nicobrailo/pianoli/Theme.java
Original file line number Diff line number Diff line change
@@ -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];
}

}
10 changes: 10 additions & 0 deletions app/src/main/res/values/donottranslate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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. -->
</string-array>

<string-array name="theme_entryValues">
<item>boomwhacker</item>
<item>black_and_white</item>
</string-array>

<string-array name="theme_entries">
<item>@string/theme_boomwhacker</item>
<item>@string/theme_black_and_white</item>
</string-array>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
<string name="pref_enable_melodies">Auto play melodies</string>
<string name="pref_selected_melodies">Available melodies</string>
<string name="pref_selected_melodies_summary">List of melodies to cycle through</string>
<string name="theme">Theme</string>
<string name="theme_black_and_white">Black and White</string>
<string name="theme_boomwhacker">Boomwhacker</string>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
app:title="@string/sound_set"
app:useSimpleSummaryProvider="true"
app:iconSpaceReserved="false" />
<ListPreference
app:defaultValue="boomwhacker"
app:key="theme"
app:title="@string/theme"
app:entries="@array/theme_entries"
app:entryValues="@array/theme_entryValues"
app:useSimpleSummaryProvider="true"
app:iconSpaceReserved="false" />
<SwitchPreference
app:defaultValue="false"
app:key="enableMelodies"
Expand Down
3 changes: 3 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/21.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You can now choose different themes for your piano keys. Initial themes are the existing rainbow theme inspired by the Boomwhacker toys, and a Black and White theme. In the future, we will add more themes.

Thanks to @juleskers for the input and encouragement to implement this.

0 comments on commit 0d3bbfd

Please sign in to comment.