Skip to content

Commit

Permalink
wip2
Browse files Browse the repository at this point in the history
  • Loading branch information
adamws committed Dec 18, 2023
1 parent 2f62ca5 commit 7e60499
Showing 1 changed file with 41 additions and 60 deletions.
101 changes: 41 additions & 60 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,26 +502,15 @@ mod digits_display_module {

use super::CanvasEntity;

fn to_digits(digits: &mut Vec<u8>, value: i32) {
digits.clear();
let mut v = value.abs();
loop {
let d: u8 = (v % 10) as u8;
v = v / 10;
digits.push(d);
if v == 0 {
break;
};
}
}
// '/' character is not used but it is included to keep ASCII value matching
const GLYPHS: &str = "-./0123456789";

pub struct DigitsDisplay<'a> {
texture: Texture<'a>,
glyph_width: u32,
glyph_height: u32,
position: Point,
sign: bool,
digits: Vec<u8>,
glyphs: Vec<u8>,
}

impl<'a> DigitsDisplay<'a> {
Expand All @@ -530,7 +519,7 @@ mod digits_display_module {
texture_creator: &'a TextureCreator<WindowContext>,
) -> Result<DigitsDisplay<'a>, String> {
let font_surface = font
.render("0123456789-")
.render(GLYPHS)
.blended(Color::RGBA(0, 0, 0, 255))
.map_err(|e| e.to_string())?;

Expand All @@ -539,22 +528,30 @@ mod digits_display_module {
.map_err(|e| e.to_string())?;

let TextureQuery { width, height, .. } = texture.query();
let glyph_width = width / 11;
let glyph_width = width / GLYPHS.len() as u32;
let glyph_height = height;

Ok(DigitsDisplay {
texture,
glyph_width,
glyph_height,
position: Point::new(0, 0),
sign: false,
digits: Vec::with_capacity(64),
glyphs: Vec::with_capacity(64),
})
}

pub fn update(&mut self, value: i32) {
self.sign = value < 0;
to_digits(&mut self.digits, value);
pub fn with_i32(&mut self, value: i32) {
self.glyphs.clear();
for c in format!("{}", value).bytes() {
self.glyphs.push(c - '-' as u8);
}
}

pub fn with_f64(&mut self, value: f64) {
self.glyphs.clear();
for c in format!("{:?}", value).bytes() {
self.glyphs.push(c - '-' as u8);
}
}
}

Expand All @@ -578,12 +575,7 @@ mod digits_display_module {
)
};

if self.sign {
render(10, m)?;
m += 1;
}

for &digit in self.digits.iter().rev() {
for &digit in self.glyphs.iter() {
render(digit as u32, m)?;
m += 1;
}
Expand All @@ -592,35 +584,13 @@ mod digits_display_module {

fn size(&self) -> (u32, u32) {
let TextureQuery { height, .. } = self.texture.query();
(
self.glyph_width * (self.digits.len() as u32 + if self.sign { 1 } else { 0 }),
height,
)
(self.glyph_width * self.glyphs.len() as u32, height)
}

fn reposition(&mut self, position: Point) {
self.position = position;
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_to_digits() {
let mut vec = Vec::<u8>::with_capacity(64);
to_digits(&mut vec, 123);
assert_eq!(vec, vec![3, 2, 1]);
}

#[test]
fn test_to_digits_negative() {
let mut vec = Vec::<u8>::with_capacity(64);
to_digits(&mut vec, -123);
assert_eq!(vec, vec![3, 2, 1]);
}
}
}

fn new_static_text<'a>(
Expand Down Expand Up @@ -658,10 +628,6 @@ impl<'a> LabeledDigitsDisplay<'a> {
let digits = digits_display_module::DigitsDisplay::new(&font, &texture_creator)?;
Ok(LabeledDigitsDisplay { label, digits })
}

fn update(&mut self, value: i32) {
self.digits.update(value)
}
}

impl<'a> CanvasEntity for LabeledDigitsDisplay<'a> {
Expand All @@ -688,6 +654,8 @@ impl<'a> CanvasEntity for LabeledDigitsDisplay<'a> {
struct StatusBar<'a> {
mouse_x_display: LabeledDigitsDisplay<'a>,
mouse_y_display: LabeledDigitsDisplay<'a>,
split_display: LabeledDigitsDisplay<'a>,
scale_display: LabeledDigitsDisplay<'a>,
}

impl<'a> StatusBar<'a> {
Expand All @@ -698,19 +666,25 @@ impl<'a> StatusBar<'a> {
Ok(StatusBar {
mouse_x_display: LabeledDigitsDisplay::new("x:", &font, &texture_creator)?,
mouse_y_display: LabeledDigitsDisplay::new(" y:", &font, &texture_creator)?,
split_display: LabeledDigitsDisplay::new(" split:", &font, &texture_creator)?,
scale_display: LabeledDigitsDisplay::new(" scale:", &font, &texture_creator)?,
})
}

fn update(&mut self, x: i32, y: i32) {
self.mouse_x_display.update(x);
self.mouse_y_display.update(y);
fn update(&mut self, x: i32, y: i32, split: i32, scale: f64) {
self.mouse_x_display.digits.with_i32(x);
self.mouse_y_display.digits.with_i32(y);
self.split_display.digits.with_i32(split);
self.scale_display.digits.with_f64(scale);
}
}

impl<'a> CanvasEntity for StatusBar<'a> {
fn draw(&self, renderer: &mut sdl2::render::WindowCanvas) -> Result<(), String> {
self.mouse_x_display.draw(renderer)?;
self.mouse_y_display.draw(renderer)?;
self.split_display.draw(renderer)?;
self.scale_display.draw(renderer)?;
Ok(())
}

Expand All @@ -722,12 +696,17 @@ impl<'a> CanvasEntity for StatusBar<'a> {

let mut p = position;
p = reposition_internal(&mut self.mouse_x_display, p);
_ = reposition_internal(&mut self.mouse_y_display, p);
p = reposition_internal(&mut self.mouse_y_display, p);
p = reposition_internal(&mut self.split_display, p);
_ = reposition_internal(&mut self.scale_display, p);
}

fn size(&self) -> (u32, u32) {
(
self.mouse_x_display.size().0 + self.mouse_y_display.size().0,
self.mouse_x_display.size().0
+ self.mouse_y_display.size().0
+ self.split_display.size().0
+ self.scale_display.size().0,
self.mouse_x_display.size().1,
)
}
Expand Down Expand Up @@ -896,7 +875,7 @@ pub fn app<P: AsRef<Path>>(

// Load a font TODO: not sure if needed, perhaps will load with fontdb only
let font = include_bytes!("../resources/DejaVuSansMono.ttf");
let font = &ttf_context.load_font_from_rwops(RWops::from_bytes(font)?, 16)?;
let font = &ttf_context.load_font_from_rwops(RWops::from_bytes(font)?, 12)?;

let min_size: (u32, u32) = (800, 600);
let max_size: (u32, u32) = get_max_window_size(&video_subsystem)?;
Expand Down Expand Up @@ -1037,6 +1016,8 @@ pub fn app<P: AsRef<Path>>(
status_bar.update(
mouse_state.x() - workarea.position.x(),
mouse_state.y() - workarea.position.y(),
diff.split as i32,
scale,
);
status_bar.draw(&mut canvas)?;

Expand Down

0 comments on commit 7e60499

Please sign in to comment.