diff options
author | Daniel Schadt <kingdread@gmx.de> | 2019-12-14 01:35:02 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2019-12-14 01:37:49 +0100 |
commit | a1722af9a32d10826c142664050d30307defcb89 (patch) | |
tree | d46bc579466b6f38d0dd391169cedececc54461c /src/render.rs | |
parent | 4e9068acbf79244993fab4c33b29a207c219e4a8 (diff) | |
download | kondou-a1722af9a32d10826c142664050d30307defcb89.tar.gz kondou-a1722af9a32d10826c142664050d30307defcb89.tar.bz2 kondou-a1722af9a32d10826c142664050d30307defcb89.zip |
replace magic constants in renderer
Diffstat (limited to 'src/render.rs')
-rw-r--r-- | src/render.rs | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/src/render.rs b/src/render.rs index 9b71e18..e3f2d31 100644 --- a/src/render.rs +++ b/src/render.rs @@ -5,7 +5,7 @@ use image::{ Primitive, Rgba, RgbaImage, }; use imageproc::{drawing, rect::Rect}; -use num_traits::NumCast; +use num_traits::{Num, NumCast}; use rusttype::{Font, Scale, SharedBytes}; use std::{error::Error, fmt}; @@ -105,6 +105,16 @@ impl Default for RenderOptions { const BG_CROP_HEIGHT: u32 = 136; const BG_CROP_WIDTH: u32 = 647; +const TRAITS_PER_TIER: u32 = 3; +const TIER_COUNT: u32 = 3; +const TOTAL_COLUMN_COUNT: u32 = 2 * TIER_COUNT; +const MINOR_LINE_SPLIT: u32 = 4; + +fn half<T: Num>(input: T) -> T { + let two = T::one() + T::one(); + input / two +} + pub struct Renderer<'r> { api: &'r mut Api, options: RenderOptions, @@ -153,10 +163,10 @@ impl<'r> Renderer<'r> { CatmullRom, ); let minor_img = with_mask(&minor_img, &self.minor_mask); - let y_pos = (buffer.height() - minor_img.height()) / 2; - let x_slice = (buffer.width() - self.options.traitline_x_offset) / 6; + let y_pos = half(buffer.height() - minor_img.height()); + let x_slice = (buffer.width() - self.options.traitline_x_offset) / TOTAL_COLUMN_COUNT; let x_pos = 2 * (minor.tier - 1) * x_slice - + (x_slice - minor_img.width()) / 2 + + half(x_slice - minor_img.width()) + self.options.traitline_x_offset; imageops::overlay(buffer, &minor_img, x_pos, y_pos); Ok(()) @@ -179,12 +189,12 @@ impl<'r> Renderer<'r> { } else { major_img.to_rgba() }; - let y_slice = buffer.height() / 3; - let y_pos = vertical_pos as u32 * y_slice + (y_slice - major_img.height()) / 2; - let x_slice = (buffer.width() - self.options.traitline_x_offset) / 6; + let y_slice = buffer.height() / TRAITS_PER_TIER; + let y_pos = vertical_pos as u32 * y_slice + half(y_slice - major_img.height()); + let x_slice = (buffer.width() - self.options.traitline_x_offset) / TOTAL_COLUMN_COUNT; let x_pos = 2 * (major.tier - 1) * x_slice + x_slice - + (x_slice - major_img.width()) / 2 + + half(x_slice - major_img.width()) + self.options.traitline_x_offset; imageops::overlay(buffer, &major_img, x_pos, y_pos); Ok(()) @@ -200,15 +210,15 @@ impl<'r> Renderer<'r> { return Ok(()); } - let x_slice = (buffer.width() - self.options.traitline_x_offset) / 6; + let x_slice = (buffer.width() - self.options.traitline_x_offset) / TOTAL_COLUMN_COUNT; let start_x = 2 * tier as u32 * x_slice - + (x_slice + self.options.trait_size) / 2 + + half(x_slice + self.options.trait_size) + self.options.traitline_x_offset; let end_x = start_x + x_slice - self.options.trait_size; - let start_y = (buffer.height() - self.options.trait_size) / 2 - + self.options.trait_size / 4 * (choice as u32); - let y_slice = buffer.height() / 3; - let end_y = y_slice * (choice as u32 - 1) + y_slice / 2; + let start_y = half(buffer.height() - self.options.trait_size) + + self.options.trait_size / MINOR_LINE_SPLIT * (choice as u32); + let y_slice = buffer.height() / TRAITS_PER_TIER; + let end_y = y_slice * (choice as u32 - 1) + half(y_slice); draw_thick_line( buffer, @@ -217,7 +227,7 @@ impl<'r> Renderer<'r> { self.options.line_height, self.options.line_color, ); - if tier == 2 { + if tier as u32 == TIER_COUNT - 1 { return Ok(()); } @@ -268,7 +278,7 @@ impl<'r> Renderer<'r> { let major_traits = self.api.get_traits(&spec.major_traits)?; for (i, major) in major_traits.iter().enumerate() { let choice = choices[major.tier as usize - 1]; - let vert_pos = (i % 3) as u8; + let vert_pos = (i as u32 % TRAITS_PER_TIER) as u8; let chosen = choice as u8 == vert_pos + 1; self.render_major_trait(&mut buffer, &major, vert_pos, chosen)?; } @@ -354,7 +364,7 @@ impl<'r> Renderer<'r> { for (alignment, image) in images { let pos_x = match alignment { Alignment::Left => 0, - Alignment::Center => (width - image.width()) / 2, + Alignment::Center => half(width - image.width()), Alignment::Right => width - image.width(), }; imageops::overlay(&mut buffer, image, pos_x, pos_y); @@ -441,7 +451,7 @@ fn draw_thick_line<I>( .round() as u32; let mut line_buffer: RgbaImage = ImageBuffer::new(line_length, line_length); - let halfway = (line_length - thickness) / 2; + let halfway = half(line_length - thickness); let rect = Rect::at(0, halfway as i32).of_size(line_length, thickness); drawing::draw_filled_rect_mut(&mut line_buffer, rect, color); @@ -453,13 +463,13 @@ fn draw_thick_line<I>( Rgba([0, 0, 0, 0]), ); - let half_x = (start.0 as i32 + delta_x / 2) as u32; - let half_y = (start.1 as i32 + delta_y / 2) as u32; + let half_x = (start.0 as i32 + half(delta_x)) as u32; + let half_y = (start.1 as i32 + half(delta_y)) as u32; imageops::overlay( image, &line_buffer, - half_x - line_length / 2, - half_y - line_length / 2, + half_x - half(line_length), + half_y - half(line_length), ); } |