|
|
|
|
@ -3,10 +3,8 @@ extern crate sdl2; |
|
|
|
|
use sdl2::event::Event; |
|
|
|
|
use sdl2::keyboard::Keycode; |
|
|
|
|
use sdl2::pixels::Color; |
|
|
|
|
//use sdl2::rect::Rect;
|
|
|
|
|
//use sdl2::render::TextureQuery;
|
|
|
|
|
|
|
|
|
|
mod font_build; |
|
|
|
|
mod editor_render; |
|
|
|
|
|
|
|
|
|
static SCREEN_WIDTH: u32 = 1680; |
|
|
|
|
static SCREEN_HEIGHT: u32 = 945; |
|
|
|
|
@ -17,23 +15,8 @@ struct ModifierKeys { |
|
|
|
|
shift: bool, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
// handle the annoying Rect i32
|
|
|
|
|
macro_rules! rect( |
|
|
|
|
($x:expr, $y:expr, $w:expr, $h:expr) => ( |
|
|
|
|
Rect::new($x as i32, $y as i32, $w as u32, $h as u32) |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
fn get_corner_rect(rect_width: u32, rect_height: u32) -> Rect { |
|
|
|
|
let (w,h) = (rect_width as i32, rect_height as i32); |
|
|
|
|
let cx = 15; |
|
|
|
|
let cy = 15; |
|
|
|
|
rect!(cx, cy, w, h) |
|
|
|
|
} |
|
|
|
|
*/ |
|
|
|
|
pub fn main() -> Result<(), String> { |
|
|
|
|
let glyph_atlas = font_build::generate_glyph_atlas(); |
|
|
|
|
let glyph_atlas = editor_render::generate_glyph_atlas(); |
|
|
|
|
|
|
|
|
|
let sdl_context = sdl2::init()?; |
|
|
|
|
let video_subsys = sdl_context.video()?; |
|
|
|
|
@ -49,6 +32,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
|
|
let mut buffer = String::new(); |
|
|
|
|
let mut cursor_position = 0; |
|
|
|
|
let mut selection_anchor: Option<usize> = None; |
|
|
|
|
|
|
|
|
|
let mut draw_text = |text: &str, pos: usize| -> Result<(), String> { |
|
|
|
|
// Draw background
|
|
|
|
|
@ -57,12 +41,12 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
|
|
// Draw text
|
|
|
|
|
canvas.set_draw_color(Color::RGB(240, 240, 240)); |
|
|
|
|
let fb_text = font_build::draw_text(text, glyph_atlas.clone()); |
|
|
|
|
let fb_text = editor_render::draw_text(text, glyph_atlas.clone()); |
|
|
|
|
canvas.draw_points(&fb_text[..])?; |
|
|
|
|
|
|
|
|
|
// Draw cursor
|
|
|
|
|
canvas.set_draw_color(Color::RGB(64, 240, 240)); |
|
|
|
|
let fb_cursor = font_build::draw_cursor(text, pos); |
|
|
|
|
let fb_cursor = editor_render::draw_cursor(text, pos); |
|
|
|
|
canvas.draw_line(fb_cursor.0, fb_cursor.1)?; |
|
|
|
|
|
|
|
|
|
canvas.present(); |
|
|
|
|
@ -87,19 +71,15 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
Some(Keycode::LAlt) | Some(Keycode::RAlt) => { |
|
|
|
|
modifier_keys.alt = false |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Some(Keycode::LCtrl) | Some(Keycode::RCtrl) => { |
|
|
|
|
modifier_keys.ctrl = false |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Some(Keycode::LShift) | Some(Keycode::RShift) => { |
|
|
|
|
modifier_keys.shift = false |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Some(Keycode::LGui) | Some(Keycode::RGui) => { |
|
|
|
|
break
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_ => (), |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
@ -109,67 +89,113 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
Some(Keycode::LAlt) | Some(Keycode::RAlt) => { |
|
|
|
|
modifier_keys.alt = true |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Some(Keycode::LCtrl) | Some(Keycode::RCtrl) => { |
|
|
|
|
modifier_keys.ctrl = true |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Some(Keycode::LShift) | Some(Keycode::RShift) => { |
|
|
|
|
modifier_keys.shift = true |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Some(Keycode::LGui) | Some(Keycode::RGui) => { |
|
|
|
|
break
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// BACKSPACE key
|
|
|
|
|
Some(Keycode::Backspace) => { |
|
|
|
|
if buffer.len() > 0 { |
|
|
|
|
buffer.remove(cursor_position - 1); |
|
|
|
|
cursor_position -= 1; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
_ => (), |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
match (modifier_keys.shift, modifier_keys.ctrl, modifier_keys.alt) { |
|
|
|
|
(false, false, false) => { |
|
|
|
|
match keycode { |
|
|
|
|
// DELETE key
|
|
|
|
|
Some(Keycode::Delete) => { |
|
|
|
|
if buffer.len() > 0 && cursor_position < buffer.len() { |
|
|
|
|
buffer.remove(cursor_position); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// ENTER key
|
|
|
|
|
Some(Keycode::Return) => { |
|
|
|
|
let key = '\n'; |
|
|
|
|
buffer.insert(cursor_position, key); |
|
|
|
|
cursor_position += 1; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// HOME key
|
|
|
|
|
Some(Keycode::Home) => { |
|
|
|
|
cursor_position = 0; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// END key
|
|
|
|
|
Some(Keycode::End) => { |
|
|
|
|
cursor_position = buffer.len(); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// Left/Back arrow
|
|
|
|
|
Some(Keycode::Left) => { |
|
|
|
|
cursor_position = usize::checked_sub(cursor_position, 1) |
|
|
|
|
.unwrap_or(0); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// Right/Forward arrow
|
|
|
|
|
Some(Keycode::Right) => { |
|
|
|
|
cursor_position = (cursor_position + 1).min(buffer.len()); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// BACKSPACE key
|
|
|
|
|
Some(Keycode::Backspace) => { |
|
|
|
|
if buffer.len() > 0 { |
|
|
|
|
// Character backspace; regular
|
|
|
|
|
buffer.remove(cursor_position - 1); |
|
|
|
|
cursor_position -= 1; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_ => (), |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// DELETE key
|
|
|
|
|
Some(Keycode::Delete) => { |
|
|
|
|
if buffer.len() > 0 && cursor_position < buffer.len() { |
|
|
|
|
buffer.remove(cursor_position); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
(false, true, false) => { |
|
|
|
|
match keycode { |
|
|
|
|
Some(Keycode::Z) => println!("Undo"), |
|
|
|
|
Some(Keycode::X) => println!("Cut"), |
|
|
|
|
Some(Keycode::C) => println!("Copy"), |
|
|
|
|
Some(Keycode::V) => println!("Paste"), |
|
|
|
|
|
|
|
|
|
// BACKSPACE key
|
|
|
|
|
// Word backspace
|
|
|
|
|
// TODO: Clean up this cursed expression
|
|
|
|
|
Some(Keycode::Backspace) => { |
|
|
|
|
if buffer.len() > 0 { |
|
|
|
|
let buffer_chars: Vec<char> = buffer.chars() |
|
|
|
|
.collect(); |
|
|
|
|
while !(buffer_chars[cursor_position - 1] == ' ' || |
|
|
|
|
buffer_chars[cursor_position - 1] == '\n') && |
|
|
|
|
cursor_position > 1 { |
|
|
|
|
buffer.remove(cursor_position - 1); |
|
|
|
|
cursor_position -= 1; |
|
|
|
|
} |
|
|
|
|
buffer.remove(cursor_position - 1); |
|
|
|
|
cursor_position -= 1; |
|
|
|
|
|
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
_ => (), |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// ENTER key
|
|
|
|
|
Some(Keycode::Return) => { |
|
|
|
|
let key = '\n'; |
|
|
|
|
buffer.insert(cursor_position, key); |
|
|
|
|
cursor_position += 1; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// HOME key
|
|
|
|
|
Some(Keycode::Home) => { |
|
|
|
|
cursor_position = 0; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// END key
|
|
|
|
|
Some(Keycode::End) => { |
|
|
|
|
cursor_position = buffer.len(); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// Left/Back arrow
|
|
|
|
|
Some(Keycode::Left) => { |
|
|
|
|
cursor_position = usize::checked_sub(cursor_position, 1) |
|
|
|
|
.unwrap_or(0); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// Right/Forward arrow
|
|
|
|
|
Some(Keycode::Right) => { |
|
|
|
|
cursor_position = (cursor_position + 1).min(buffer.len()); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
(true, true, false) => { |
|
|
|
|
match keycode { |
|
|
|
|
Some(Keycode::Z) => println!("Redo"), |
|
|
|
|
Some(Keycode::X) => println!("Cut line(s)"), |
|
|
|
|
Some(Keycode::C) => println!("Copy line(s)"), |
|
|
|
|
_ => (), |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_ => (), |
|
|
|
|
@ -177,12 +203,11 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
Event::TextInput { text, .. } => { |
|
|
|
|
if !modifier_keys.alt && !modifier_keys.ctrl { |
|
|
|
|
let input_char = text.chars().nth(0).expect("Empty"); |
|
|
|
|
buffer.insert(cursor_position, input_char); |
|
|
|
|
cursor_position += 1; |
|
|
|
|
draw_text(&buffer, cursor_position)?; |
|
|
|
|
} |
|
|
|
|
let input_char = text.chars().nth(0).expect("Empty"); |
|
|
|
|
|
|
|
|
|
buffer.insert(cursor_position, input_char); |
|
|
|
|
cursor_position += 1; |
|
|
|
|
draw_text(&buffer, cursor_position)?; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_ => {} |
|
|
|
|
|