Skip to content

Commit

Permalink
refactor out portions of screen draw
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasg committed Aug 24, 2024
1 parent 60e625b commit aa92306
Showing 1 changed file with 56 additions and 45 deletions.
101 changes: 56 additions & 45 deletions src/gview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl GSkyView {
* self.real_q;
(*self.scoring).borrow_mut().add_move();
}
fn draw_portion(
fn draw_stars(
&self,
quat: UnitQuaternion<f32>,
x_min: f32,
Expand Down Expand Up @@ -160,34 +160,35 @@ impl GSkyView {

fn draw(&self, font: &Font) {
clear_background(BLACK);
self.draw_portion(self.real_q, 0.0, 1.0, 0.0, 1.0, Some(font), 16);
self.draw_stars(self.real_q, 0.0, 1.0, 0.0, 1.0, Some(font), 16);
self.draw_target_rectangle(font);
self.draw_help();
self.show_state(font);
}

let header_1 = format!(
"Stars: {}, catalog: {}. Step: {:.4}, zoom: {:.3}, moves: {}, games: {}, score: {:.6}",
self.options.nstars,
self.options
.catalog_filename
.clone()
.unwrap_or("random".to_string()),
self.step,
self.fov.zoom(),
(*self.scoring).borrow().moves,
(*self.scoring).borrow().total.len(),
(*self.scoring).borrow().get_score(),
);
draw_text(&header_1, 10.0, 20.0, 18.0, GRAY);
let state_text = format!("State : {}", quat_coords_str(self.real_q));
draw_text(&state_text, 10.0, 38.0, 18.0, GRAY);
if self.options.show_distance {
let dist_text = format!(
"Target: {}, t/s: {}, distance: {:.6}",
quat_coords_str(self.target_q),
quat_coords_str(self.target_q / self.real_q),
self.distance()
fn draw_help(&self) {
if self.options.show_help {
let (reltx, relty, reltw, relth, font_size) = (0.6, 0.1, 0.4, 0.8, 20);
draw_rectangle(
reltx * screen_width(),
relty * screen_height(),
reltw * screen_width(),
relth * screen_height(),
BLACK,
);
draw_text(&dist_text, 10.0, 56.0, 18.0, GRAY);
for (i, line) in get_help_lines().iter().enumerate() {
draw_text(
line,
reltx * screen_width(),
relty * screen_width() + (font_size * i) as f32 * 1.12,
font_size as f32,
WHITE,
);
}
}
}

fn draw_target_rectangle(&self, font: &Font) {
let (reltx, relty, reltw, relth, font_size) = if self.options.only_target {
(0.0, 0.0, 1.0, 1.0, 16)
} else {
Expand All @@ -201,9 +202,11 @@ impl GSkyView {
);

draw_rectangle(tx, ty, tw, th, BLACK);
draw_line(tx, ty, tx + tw, ty, 1.0, YELLOW);
draw_line(tx + tw, ty, tx + tw, ty + th, 1.0, YELLOW);
self.draw_portion(
if !self.options.only_target {
draw_line(tx, ty, tx + tw, ty, 1.0, YELLOW);
draw_line(tx + tw, ty, tx + tw, ty + th, 1.0, YELLOW);
}
self.draw_stars(
self.target_q,
reltx,
reltx + reltw,
Expand All @@ -212,25 +215,33 @@ impl GSkyView {
Some(font),
font_size,
);
}

if self.options.show_help {
let (reltx, relty, reltw, relth, font_size) = (0.6, 0.1, 0.4, 0.8, 20);
draw_rectangle(
reltx * screen_width(),
relty * screen_height(),
reltw * screen_width(),
relth * screen_height(),
BLACK,
fn show_state(&self, font: &Font) {
let header_1 = format!(
"Stars: {}, catalog: {}. Step: {:.4}, zoom: {:.3}, moves: {}, games: {}, score: {:.6}",
self.options.nstars,
self.options
.catalog_filename
.clone()
.unwrap_or("random".to_string()),
self.step,
self.fov.zoom(),
(*self.scoring).borrow().moves,
(*self.scoring).borrow().total.len(),
(*self.scoring).borrow().get_score(),
);
draw_text(&header_1, 10.0, 20.0, 18.0, GRAY);
let state_text = format!("State : {}", quat_coords_str(self.real_q));
draw_text(&state_text, 10.0, 38.0, 18.0, GRAY);
if self.options.show_distance {
let dist_text = format!(
"Target: {}, t/s: {}, distance: {:.6}",
quat_coords_str(self.target_q),
quat_coords_str(self.target_q / self.real_q),
self.distance()
);
for (i, line) in get_help_lines().iter().enumerate() {
draw_text(
line,
reltx * screen_width(),
relty * screen_width() + (font_size * i) as f32 * 1.12,
font_size as f32,
WHITE,
);
}
draw_text(&dist_text, 10.0, 56.0, 18.0, GRAY);
}
}
}
Expand Down

0 comments on commit aa92306

Please sign in to comment.