Skip to content

Commit

Permalink
refactor: move label drawing into a dedicated function
Browse files Browse the repository at this point in the history
We may want to label things that aren't pressed.
  • Loading branch information
elasticdog committed May 4, 2024
1 parent 8df7444 commit 3ab1c1b
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions src/ui/piano.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,45 +137,48 @@ const Key = struct {
}
rl.drawRectangleLines(self.pos_x, self.pos_y, self.width, self.height, border_color);

// Label the key with the note name if it's pressed.
if (self.state == .pressed) {
const note = Note.fromMidi(self.midi_number);
const note_name = note.pitch.asText();

// raylib's drawText() function requires a '0' sentinel.
const note_name_z: [:0]const u8 = @ptrCast(note_name);

const font_size = 17;
const text_width = rl.measureText(note_name_z, font_size);

const rect_width = text_width + 8; // add padding to sides
const rect_height = 22;
const rect_x = self.pos_x + @divFloor(self.width - rect_width, 2);
const rect_y = self.pos_y + self.height - rect_height - 5;

const rect = rl.Rectangle{
.x = @floatFromInt(rect_x),
.y = @floatFromInt(rect_y),
.width = @floatFromInt(rect_width),
.height = @floatFromInt(rect_height),
};

const roundness = 0.4;
const segments = 4;
rl.drawRectangleRounded(rect, roundness, segments, rl.Color.orange);

const text_x = rect_x + @divFloor(rect_width - text_width, 2);
const text_y = (rect_y + (rect_height - font_size) / 2) + 1;
rl.drawText(
note_name_z,
text_x,
text_y,
font_size,
rl.Color.dark_gray,
);
self.drawLabel();
}
}

fn drawLabel(self: Key) void {
const note = Note.fromMidi(self.midi_number);
const note_name = note.pitch.asText();

// raylib's drawText() function requires a '0' sentinel.
const note_name_z: [:0]const u8 = @ptrCast(note_name);

const font_size = 17;
const text_width = rl.measureText(note_name_z, font_size);

const rect_width = text_width + 8; // add padding to sides
const rect_height = 22;
const rect_x = self.pos_x + @divFloor(self.width - rect_width, 2);
const rect_y = self.pos_y + self.height - rect_height - 5;

const rect = rl.Rectangle{
.x = @floatFromInt(rect_x),
.y = @floatFromInt(rect_y),
.width = @floatFromInt(rect_width),
.height = @floatFromInt(rect_height),
};

const roundness = 0.4;
const segments = 4;
rl.drawRectangleRounded(rect, roundness, segments, rl.Color.orange);

const text_x = rect_x + @divFloor(rect_width - text_width, 2);
const text_y = (rect_y + (rect_height - font_size) / 2) + 1;
rl.drawText(
note_name_z,
text_x,
text_y,
font_size,
rl.Color.black,
);
}

fn isHovered(self: Key, mouse_x: i32, mouse_y: i32) bool {
return mouse_x >= self.pos_x and mouse_x <= self.pos_x + self.width and
mouse_y >= self.pos_y and mouse_y <= self.pos_y + self.height;
Expand Down

0 comments on commit 3ab1c1b

Please sign in to comment.