Skip to content

Commit

Permalink
minor optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
subalterngames committed Jan 6, 2024
1 parent fd7ecf9 commit 62d417f
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 57 deletions.
6 changes: 1 addition & 5 deletions render/src/export_settings_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,7 @@ impl Drawable for ExportSettingsPanel {
ColorKey::NoFocus
};
let background = &self.backgrounds[&conn.exporter.export_type.get()];
renderer.rectangle_pixel(
background.background.position,
background.background.size,
&color,
);
renderer.rectangle_pixel(&background.background, &color);
renderer.rectangle_lines(&background.border, &color);
renderer.rectangle(&self.title_rect, &ColorKey::Background);
renderer.text(&self.title, &color);
Expand Down
8 changes: 2 additions & 6 deletions render/src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl MainMenu {
let position = [x, rect[0][1]];
let size = [w, rect[1][1]];
// Draw the mask.
renderer.rectangle_pixel(position, size, &ColorKey::Background);
renderer.rectangle_note(position, size, &ColorKey::Background);
}

/// Set the lerp target of power bar.
Expand All @@ -306,11 +306,7 @@ impl Drawable for MainMenu {
fn update(&self, renderer: &Renderer, state: &State, _: &Conn, _: &Text, _: &PathsState) {
self.panel.update_ex(&COLOR, renderer);
if state.unsaved_changes {
renderer.rectangle_pixel(
self.title_changes.rect.position,
self.title_changes.rect.size,
&ColorKey::Background,
);
renderer.rectangle_pixel(&self.title_changes.rect, &ColorKey::Background);
renderer.text(&self.title_changes.label, &COLOR);
}
for label in self.labels.iter() {
Expand Down
12 changes: 2 additions & 10 deletions render/src/music_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ impl Drawable for MusicPanel {
renderer.corners(&self.name_rect, focus);
// Draw a rectangle for input.
if state.input.alphanumeric_input {
renderer.rectangle_pixel(
self.name_input_rect.position,
self.name_input_rect.size,
&ColorKey::TextFieldBG,
);
renderer.rectangle_pixel(&self.name_input_rect, &ColorKey::TextFieldBG);
}
}
// Draw the name.
Expand All @@ -103,11 +99,7 @@ impl Drawable for MusicPanel {
renderer.corners(&self.bpm.corners_rect, focus);
// Draw a rectangle for input.
if state.input.alphanumeric_input {
renderer.rectangle_pixel(
self.bpm.input_rect.position,
self.bpm.input_rect.size,
&ColorKey::TextFieldBG,
);
renderer.rectangle_pixel(&self.bpm.input_rect, &ColorKey::TextFieldBG);
}
}
// Draw the BPM.
Expand Down
14 changes: 3 additions & 11 deletions render/src/open_file_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,11 @@ impl Drawable for OpenFilePanel {
ColorKey::NoFocus
};
// Draw the panel background.
renderer.rectangle_pixel(
self.panel.background.background.position,
self.panel.background.background.size,
&ColorKey::Background,
);
renderer.rectangle_pixel(&self.panel.background.background, &ColorKey::Background);
renderer.rectangle_lines(&self.panel.background.border, &focus_color);
// Draw the title.
let title = &self.titles[&paths_state.open_file_type];
renderer.rectangle_pixel(title.rect.position, title.rect.size, &ColorKey::Background);
renderer.rectangle_pixel(&title.rect, &ColorKey::Background);
renderer.text(&title.label, &focus_color);
// Draw the working directory.
let mut x = self.panel.background.grid_rect.position[0] + 1;
Expand Down Expand Up @@ -256,11 +252,7 @@ impl Drawable for OpenFilePanel {
// Possibly show the input dialogue.
if let Some(filename) = &paths_state.get_filename() {
// Draw the background of the prompt.
renderer.rectangle_pixel(
self.prompt.background.position,
self.prompt.background.size,
&ColorKey::Background,
);
renderer.rectangle_pixel(&self.prompt.background, &ColorKey::Background);
renderer.rectangle_lines(
&self.prompt.border,
&if focus {
Expand Down
12 changes: 2 additions & 10 deletions render/src/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,9 @@ impl Panel {

/// Draw an empty panel. The border and title text will be an explicitly defined color.
pub fn update_ex(&self, color: &ColorKey, renderer: &Renderer) {
renderer.rectangle_pixel(
self.background.background.position,
self.background.background.size,
&ColorKey::Background,
);
renderer.rectangle_pixel(&self.background.background, &ColorKey::Background);
renderer.rectangle_lines(&self.background.border, color);
renderer.rectangle_pixel(
self.title.rect.position,
self.title.rect.size,
&ColorKey::Background,
);
renderer.rectangle_pixel(&self.title.rect, &ColorKey::Background);
renderer.text(&self.title.label, color);
}

Expand Down
4 changes: 2 additions & 2 deletions render/src/piano_roll_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Drawable for PianoRollPanel {
self.piano_roll_rows_rect[0],
&dt,
);
renderer.rectangle_pixel(
renderer.rectangle_note(
[select_0.x, self.piano_roll_rows_rect[1]],
[x1 - select_0.x, self.piano_roll_rows_rect[3]],
&color,
Expand All @@ -278,7 +278,7 @@ impl Drawable for PianoRollPanel {
// Get the y value from the pitch.
let y = self.piano_roll_rows_rect[1]
+ ((state.view.dn[0] - note.note.note) as f32) * self.cell_size[1];
renderer.rectangle_pixel([note.x, y], [w, self.cell_size[1]], &note.color)
renderer.rectangle_note([note.x, y], [w, self.cell_size[1]], &note.color)
}
// Volume.
self.volume.update(&notes, renderer, state);
Expand Down
4 changes: 2 additions & 2 deletions render/src/piano_roll_panel/multi_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl MultiTrack {
&dt,
)
.clamp(self.rect_f[0], x1);
renderer.rectangle_pixel([select_0.x, note_y], [x1 - select_0.x, h], &color)
renderer.rectangle_note([select_0.x, note_y], [x1 - select_0.x, h], &color)
}
}
// Draw some notes.
Expand All @@ -166,7 +166,7 @@ impl MultiTrack {
if note_x1 > x1 {
note_w = x1 - note_x0;
}
renderer.rectangle_pixel(
renderer.rectangle_note(
[note_x0, note_y],
[note_w, self.note_height],
&ColorKey::Background,
Expand Down
6 changes: 1 addition & 5 deletions render/src/piano_roll_panel/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ impl Volume {
};
renderer.rectangle(&self.rect, &ColorKey::Background);
renderer.border(&self.rect, &bg_color);
renderer.rectangle_pixel(
self.title_rect.position,
self.title_rect.size,
&ColorKey::Background,
);
renderer.rectangle_pixel(&self.title_rect, &ColorKey::Background);
renderer.text(&self.title, &bg_color);
// Render the lines in layers.
// This forces selected notes and playing notes to render on top.
Expand Down
22 changes: 16 additions & 6 deletions render/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,27 @@ impl Renderer {
draw_rectangle(xy[0], xy[1], wh[0], wh[1], color);
}

/// Draw a rectangle using pixel coordinates instead of grid coordinates.
///
/// - `rect` The rectangle.
/// - `color` A `ColorKey` for the rectangle.
pub(crate) fn rectangle_pixel(&self, rect: &RectanglePixel, color: &ColorKey) {
draw_rectangle(
rect.position[0],
rect.position[1],
rect.size[0],
rect.size[1],
self.colors[color],
)
}

/// Draw a rectangle using pixel coordinates instead of grid coordinates.
/// This is used to draw notes.
///
/// - `position` The top-left position in pixel coordinates.
/// - `size` The width-height in pixel coordinates.
/// - `color` A `ColorKey` for the rectangle.
pub(crate) fn rectangle_pixel(&self, position: [f32; 2], size: [f32; 2], color: &ColorKey) {
pub(crate) fn rectangle_note(&self, position: [f32; 2], size: [f32; 2], color: &ColorKey) {
draw_rectangle(
position[0],
position[1],
Expand Down Expand Up @@ -523,11 +537,7 @@ impl Renderer {
self.corners(&ki.corners_rect, focus[0]);
// Draw a rectangle for input.
if alphanumeric_input {
self.rectangle_pixel(
ki.input_rect.position,
ki.input_rect.size,
&ColorKey::TextFieldBG,
);
self.rectangle_pixel(&ki.input_rect, &ColorKey::TextFieldBG);
}
}
let key_color = &Self::get_key_color(focus[0]);
Expand Down

0 comments on commit 62d417f

Please sign in to comment.