|
|
|
|
@ -2,14 +2,12 @@ extern crate sdl2; |
|
|
|
|
|
|
|
|
|
use std::path::Path; |
|
|
|
|
|
|
|
|
|
use font_build::draw_text; |
|
|
|
|
use sdl2::event::Event; |
|
|
|
|
use sdl2::keyboard::Keycode; |
|
|
|
|
use sdl2::pixels::Color; |
|
|
|
|
use sdl2::rect::Rect; |
|
|
|
|
use sdl2::render::TextureQuery; |
|
|
|
|
|
|
|
|
|
//mod _catlas;
|
|
|
|
|
//use _catlas::*;
|
|
|
|
|
//use sdl2::rect::Rect;
|
|
|
|
|
//use sdl2::render::TextureQuery;
|
|
|
|
|
|
|
|
|
|
mod font_build; |
|
|
|
|
|
|
|
|
|
@ -22,6 +20,7 @@ struct ModifierKeys { |
|
|
|
|
shift: bool, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
// handle the annoying Rect i32
|
|
|
|
|
macro_rules! rect( |
|
|
|
|
($x:expr, $y:expr, $w:expr, $h:expr) => ( |
|
|
|
|
@ -35,10 +34,9 @@ fn get_corner_rect(rect_width: u32, rect_height: u32) -> Rect { |
|
|
|
|
let cy = 15; |
|
|
|
|
rect!(cx, cy, w, h) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
pub fn main() -> Result<(), String> { |
|
|
|
|
font_build::generate_glyph_atlas(); |
|
|
|
|
let glyph_atlas = font_build::generate_glyph_atlas(); |
|
|
|
|
|
|
|
|
|
let font_path: &Path = Path::new("./fonts/Monoid-Regular.ttf"); |
|
|
|
|
|
|
|
|
|
@ -54,7 +52,6 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
.map_err(|e| e.to_string())?; |
|
|
|
|
|
|
|
|
|
let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?; |
|
|
|
|
let texture_creator = canvas.texture_creator(); |
|
|
|
|
|
|
|
|
|
// Load a font
|
|
|
|
|
let mut font = ttf_context.load_font(font_path, 12)?; |
|
|
|
|
@ -64,28 +61,13 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
let mut cursor_position = 0; |
|
|
|
|
|
|
|
|
|
let mut draw_text = |text: &str| -> Result<(), String> { |
|
|
|
|
// render a surface, and convert it to a texture bound to the canvas
|
|
|
|
|
let surface = font |
|
|
|
|
.render(text) |
|
|
|
|
.blended(Color::RGBA(255, 255, 255, 255)) |
|
|
|
|
.map_err(|e| e.to_string())?; |
|
|
|
|
let texture = texture_creator |
|
|
|
|
.create_texture_from_surface(&surface) |
|
|
|
|
.map_err(|e| e.to_string())?; |
|
|
|
|
|
|
|
|
|
canvas.set_draw_color(Color::RGB(32, 32, 32)); |
|
|
|
|
canvas.clear(); |
|
|
|
|
|
|
|
|
|
let TextureQuery { width, height, .. } = texture.query(); |
|
|
|
|
canvas.set_draw_color(Color::RGB(240, 240, 240)); |
|
|
|
|
let fb_text = draw_text(text, glyph_atlas.clone()); |
|
|
|
|
canvas.draw_points(&fb_text[..])?; |
|
|
|
|
|
|
|
|
|
// If the example text is too big for the screen, downscale it (and center regardless)
|
|
|
|
|
// let padding = 64;
|
|
|
|
|
let target = get_corner_rect( |
|
|
|
|
width, |
|
|
|
|
height, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
canvas.copy(&texture, None, Some(target))?; |
|
|
|
|
canvas.present(); |
|
|
|
|
|
|
|
|
|
Ok(()) |
|
|
|
|
@ -185,11 +167,15 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
cursor_position = (cursor_position + 1).min(buffer.len()) |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Event::TextInput { text, .. } => { |
|
|
|
|
println!("{text}") |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Event::KeyDown { keycode, .. } => { |
|
|
|
|
let key = keycode.unwrap().to_string(); |
|
|
|
|
|
|
|
|
|
// Ignore multi-char keycodes
|
|
|
|
|
if key.len() > 1 { break } |
|
|
|
|
if key.len() != 1 { break } |
|
|
|
|
|
|
|
|
|
let key_case = match modifier_keys.shift { |
|
|
|
|
true => key.to_uppercase(), |
|
|
|
|
@ -199,9 +185,10 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
buffer.insert(cursor_position, key_case); |
|
|
|
|
cursor_position += 1; |
|
|
|
|
draw_text(&buffer)?; |
|
|
|
|
println!("{key}"); |
|
|
|
|
//println!("{key}");
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ => {} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|