Skip to content

Commit

Permalink
Move all area logic to Piano privates, to simplify drawing logic.
Browse files Browse the repository at this point in the history
Logic outside of Piano shouldn't have to care about the relationship
between key-index odd/even and big-vs-small-ness of the keys.
We're not completely there yet, due to the z-index draw ordering,
but it's definitely an improvement.

- Style: convert snake_case names to proper camelCase, where touched.
  • Loading branch information
Jules Kerssemakers committed Jan 18, 2024
1 parent 599f4c4 commit b0fd1fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
27 changes: 19 additions & 8 deletions app/src/main/java/com/nicobrailo/pianoli/Piano.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,43 @@ int pos_to_key_idx(float pos_x, float pos_y) {
if (pos_y > keys_flats_height) return big_key_idx;

// Check if press is inside rect of flat key
Key flat = get_area_for_flat_key(big_key_idx);
Key flat = getAreaForSmallKey(big_key_idx + 1);
if (flat.contains(pos_x, pos_y)) return big_key_idx + 1;

if (big_key_idx > 0) {
Key prev_flat = get_area_for_flat_key(big_key_idx - 2);
Key prev_flat = getAreaForSmallKey(big_key_idx - 1);
if (prev_flat.contains(pos_x, pos_y)) return big_key_idx - 1;
}

// If not in the current or previous flat, it must be a hit in the big key
return big_key_idx;
}

Key get_area_for_key(int key_idx) {
int x_i = key_idx / 2 * keys_width;
@NonNull
Key getAreaForKey(int keyIdx) {
if ((keyIdx & 1) == 0) { // even positions are the full, big keys
return getAreaForBigKey(keyIdx);
} else {
return getAreaForSmallKey(keyIdx); // odd positions are the small/black/flat keys.
}
}

@NonNull
private Key getAreaForBigKey(int keyIdx) {
int x_i = keyIdx / 2 * keys_width;
return new Key(x_i, x_i + keys_width, 0, keys_height);
}

Key get_area_for_flat_key(int key_idx) {
final int octave_idx = (key_idx / 2) % 7;
if (octave_idx == 2 || octave_idx == 6) {
@NonNull
private Key getAreaForSmallKey(int keyIdx) {
final int octaveIdx = (keyIdx / 2) % 7;
if (octaveIdx == 2 || octaveIdx == 6) {
// Keys without flat get a null-area
return Key.CANT_TOUCH_THIS;
}

final int offset = keys_width - (keys_flat_width / 2);
int x_i = (key_idx / 2) * keys_width + offset;
int x_i = (keyIdx / 2) * keys_width + offset;
return new Key(x_i, x_i + keys_flat_width, 0, keys_flats_height);
}
}
37 changes: 18 additions & 19 deletions app/src/main/java/com/nicobrailo/pianoli/PianoCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,6 @@ private static void resetCanvas(Canvas canvas) {
canvas.drawPaint(p);
}

private void drawBigKeys(Canvas canvas) {
for (int i = 0; i < piano.get_keys_count(); i += 2) {
draw_key(canvas, piano.get_area_for_key(i), i);
}
}

private void drawSmallKeys(Canvas canvas) {
for (int i = 1; i < piano.get_keys_count(); i += 2) {
if (piano.get_area_for_flat_key(i) != Key.CANT_TOUCH_THIS) {
draw_key(canvas, piano.get_area_for_flat_key(i), i);
}
}
}

/**
* Overlays gear icons onto the currently-held and next expected flat keys.
*/
Expand All @@ -173,7 +159,12 @@ void drawConfigGears(Canvas androidCanvas) {
draw_icon_on_black_key(androidCanvas, gearIcon, appConfigTrigger.getNextExpectedKey(), normalSize, normalSize);
}

void draw_key(final Canvas canvas, final Key rect, int i) {
void drawKey(final Canvas canvas, final int i) {
Key rect = piano.getAreaForKey(i);
if (rect == Key.CANT_TOUCH_THIS) {
return; // don't waste performance drawing the skipped black keys.
}

Paint p = new Paint();
p.setColor(theme.getColorForKey(i, piano.is_key_pressed(i)));

Expand Down Expand Up @@ -253,7 +244,7 @@ void draw_key(final Canvas canvas, final Key rect, int i) {
*/
void draw_icon_on_black_key(final Canvas canvas, final Drawable icon, int key_idx,
final int icon_width, final int icon_height) {
final Key key = piano.get_area_for_flat_key(key_idx);
final Key key = piano.getAreaForKey(key_idx);
int icon_x = ((key.x_f - key.x_i) / 2) + key.x_i;
int icon_y = icon_height;

Expand Down Expand Up @@ -285,9 +276,17 @@ public void redraw(SurfaceHolder surfaceHolder) {
if (canvas == null) return;

resetCanvas(canvas);
drawBigKeys(canvas);
// Small keys drawn after big keys to ensure z-index
drawSmallKeys(canvas);

// draw main, big keys (even key index)
for (int i = 0; i < piano.get_keys_count(); i += 2) {
drawKey(canvas, i);
}

// Small keys drawn after big keys to ensure z-index (odd key index)
for (int i = 1; i < piano.get_keys_count(); i += 2) {
drawKey(canvas, i);
}

// Gear icons drawn after small keys, since they go on top of those.
drawConfigGears(canvas);

Expand Down

0 comments on commit b0fd1fd

Please sign in to comment.