diff --git a/src/main.rs b/src/main.rs index 579b253..f8f2c13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,12 +25,11 @@ struct ModifierKeys { shift: bool, } - pub fn main() -> Result<(), String> { - let mut modifier_keys = ModifierKeys {alt: false, ctrl: false, shift: false}; - + // Initialize clipboard let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap(); + // Initialize SDL2, window, and canvas let sdl_context = sdl2::init()?; let video_subsys = sdl_context.video()?; @@ -46,18 +45,24 @@ pub fn main() -> Result<(), String> { let mut window_size = canvas.output_size()?; + // Initalize buffer let mut buffer = String::new(); - let mut cursor_position = 0; - let mut selection_anchor: Option = None; + // Initialize undo data & values let mut undo_history: Vec<(String, usize)> = vec![]; let mut undo_position: usize = 0; let mut undo_timer: u32 = UNDO_TIME_COUNT; - let pad_offset = Point::new(10, 10); + // Initialize input values + let mut modifier_keys = ModifierKeys {alt: false, ctrl: false, shift: false}; + let mut cursor_position = 0; + let mut selection_anchor: Option = None; + // Initialize graphics data and values let (glyph_atlas, glyph_metrics) = editor_render::generate_glyph_data(); + let pad_offset = Point::new(10, 10); + // The drawing routine let mut draw = |window_size: (u32, u32), text: &str, pos: usize| -> Result<(), String> { // Draw background canvas.set_draw_color(Color::RGB(32, 32, 32)); @@ -110,6 +115,7 @@ pub fn main() -> Result<(), String> { Ok(()) }; + // Easier way to please the borrow checker macro_rules! draw { () => { @@ -118,6 +124,7 @@ pub fn main() -> Result<(), String> { } draw!(); + 'mainloop: loop { // TODO: Make this completely user-configurable instead of hardcoded for event in sdl_context.event_pump()?.poll_iter() { @@ -125,6 +132,7 @@ pub fn main() -> Result<(), String> { Event::Window { win_event, .. } => { if let WindowEvent::Resized(w, h) = win_event { window_size = (w as u32, h as u32); + draw!(); } } @@ -178,6 +186,7 @@ pub fn main() -> Result<(), String> { selection_anchor = None; buffer.remove(cursor_position); + draw!(); } }, @@ -190,6 +199,7 @@ pub fn main() -> Result<(), String> { let key = '\n'; buffer.insert(cursor_position, key); cursor_position += 1; + draw!(); }, @@ -198,6 +208,7 @@ pub fn main() -> Result<(), String> { selection_anchor = None; cursor_position = 0; + draw!(); }, @@ -206,6 +217,7 @@ pub fn main() -> Result<(), String> { selection_anchor = None; cursor_position = buffer.len(); + draw!(); }, @@ -215,6 +227,7 @@ pub fn main() -> Result<(), String> { cursor_position = usize::checked_sub(cursor_position, 1) .unwrap_or(0); + draw!(); }, @@ -223,6 +236,7 @@ pub fn main() -> Result<(), String> { selection_anchor = None; cursor_position = (cursor_position + 1).min(buffer.len()); + draw!(); }, @@ -235,6 +249,7 @@ pub fn main() -> Result<(), String> { buffer.remove(cursor_position - 1); cursor_position -= 1; + draw!(); } }, @@ -246,6 +261,15 @@ pub fn main() -> Result<(), String> { // CTRL down (false, true, false) => { match keycode { + // Select all + Some(Keycode::A) => { + selection_anchor = Some(buffer.len()); + cursor_position = 0; + + draw!() + }, + + // Undo Some(Keycode::Z) => { if undo_position > 1 { undo_position -= 1; @@ -253,15 +277,20 @@ pub fn main() -> Result<(), String> { buffer = last_undo.0; cursor_position = last_undo.1; - draw!(); + draw!() } }, + // TODO: Cut Some(Keycode::X) => println!("Cut"), + // Copy + // TODO: Use selection Some(Keycode::C) => { clipboard_context.set_contents(buffer.clone()).unwrap() }, + + // Paste Some(Keycode::V) => { let paste = clipboard_context.get_contents().unwrap(); for character in paste.chars() { @@ -270,7 +299,7 @@ pub fn main() -> Result<(), String> { undo_timer = UNDO_TIME_COUNT; } - draw!(); + draw!() }, // BACKSPACE key @@ -292,13 +321,14 @@ pub fn main() -> Result<(), String> { buffer.remove(cursor_position - 1); cursor_position -= 1; - draw!(); + draw!() } }, _ => (), } }, + // SHIFT + CTRL down (true, true, false) => { match keycode { Some(Keycode::Z) => { @@ -323,14 +353,15 @@ pub fn main() -> Result<(), String> { } }, + // Process user input Event::TextInput { text, .. } => { undo_timer = 0; selection_anchor = None; let input_char = text.chars().nth(0).expect("Empty"); - buffer.insert(cursor_position, input_char); cursor_position += 1; + draw!(); }, @@ -342,20 +373,18 @@ pub fn main() -> Result<(), String> { if undo_timer < UNDO_TIME_COUNT { undo_timer += 1; } else if undo_timer == UNDO_TIME_COUNT { - // println!("Saving undo step."); + // Upon editing after an undo, this clears every action after such undo if undo_position < undo_history.len() { undo_history.truncate(undo_position); } undo_history.push((buffer.clone(), cursor_position)); + undo_timer += 1; undo_position += 1; - - //println!("Undo data: {undo_history:?}"); } ::std::thread::sleep(std::time::Duration::new(0, 1_000_000_000 / REFRESH_RATE)); } - format!("{selection_anchor:?}"); println!("{buffer}"); Ok(()) }