re-arranged keybinding expressions

type-render
korin 3 years ago
parent 4791b3cfb9
commit bebacf1dfe
  1. 0
      src/editor_render.rs
  2. 179
      src/main.rs

@ -3,10 +3,8 @@ extern crate sdl2;
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::Color; 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_WIDTH: u32 = 1680;
static SCREEN_HEIGHT: u32 = 945; static SCREEN_HEIGHT: u32 = 945;
@ -17,23 +15,8 @@ struct ModifierKeys {
shift: bool, 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> { 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 sdl_context = sdl2::init()?;
let video_subsys = sdl_context.video()?; let video_subsys = sdl_context.video()?;
@ -49,6 +32,7 @@ pub fn main() -> Result<(), String> {
let mut buffer = String::new(); let mut buffer = String::new();
let mut cursor_position = 0; let mut cursor_position = 0;
let mut selection_anchor: Option<usize> = None;
let mut draw_text = |text: &str, pos: usize| -> Result<(), String> { let mut draw_text = |text: &str, pos: usize| -> Result<(), String> {
// Draw background // Draw background
@ -57,12 +41,12 @@ pub fn main() -> Result<(), String> {
// Draw text // Draw text
canvas.set_draw_color(Color::RGB(240, 240, 240)); 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[..])?; canvas.draw_points(&fb_text[..])?;
// Draw cursor // Draw cursor
canvas.set_draw_color(Color::RGB(64, 240, 240)); 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.draw_line(fb_cursor.0, fb_cursor.1)?;
canvas.present(); canvas.present();
@ -87,19 +71,15 @@ pub fn main() -> Result<(), String> {
Some(Keycode::LAlt) | Some(Keycode::RAlt) => { Some(Keycode::LAlt) | Some(Keycode::RAlt) => {
modifier_keys.alt = false modifier_keys.alt = false
}, },
Some(Keycode::LCtrl) | Some(Keycode::RCtrl) => { Some(Keycode::LCtrl) | Some(Keycode::RCtrl) => {
modifier_keys.ctrl = false modifier_keys.ctrl = false
}, },
Some(Keycode::LShift) | Some(Keycode::RShift) => { Some(Keycode::LShift) | Some(Keycode::RShift) => {
modifier_keys.shift = false modifier_keys.shift = false
}, },
Some(Keycode::LGui) | Some(Keycode::RGui) => { Some(Keycode::LGui) | Some(Keycode::RGui) => {
break break
}, },
_ => (), _ => (),
} }
}, },
@ -109,67 +89,113 @@ pub fn main() -> Result<(), String> {
Some(Keycode::LAlt) | Some(Keycode::RAlt) => { Some(Keycode::LAlt) | Some(Keycode::RAlt) => {
modifier_keys.alt = true modifier_keys.alt = true
}, },
Some(Keycode::LCtrl) | Some(Keycode::RCtrl) => { Some(Keycode::LCtrl) | Some(Keycode::RCtrl) => {
modifier_keys.ctrl = true modifier_keys.ctrl = true
}, },
Some(Keycode::LShift) | Some(Keycode::RShift) => { Some(Keycode::LShift) | Some(Keycode::RShift) => {
modifier_keys.shift = true modifier_keys.shift = true
}, },
Some(Keycode::LGui) | Some(Keycode::RGui) => { Some(Keycode::LGui) | Some(Keycode::RGui) => {
break break
}, },
_ => (),
// BACKSPACE key };
Some(Keycode::Backspace) => {
if buffer.len() > 0 { match (modifier_keys.shift, modifier_keys.ctrl, modifier_keys.alt) {
buffer.remove(cursor_position - 1); (false, false, false) => {
cursor_position -= 1; match keycode {
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)?
}
},
// 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 (false, true, false) => {
Some(Keycode::Delete) => { match keycode {
if buffer.len() > 0 && cursor_position < buffer.len() { Some(Keycode::Z) => println!("Undo"),
buffer.remove(cursor_position); Some(Keycode::X) => println!("Cut"),
draw_text(&buffer, cursor_position)? 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 (true, true, false) => {
Some(Keycode::Return) => { match keycode {
let key = '\n'; Some(Keycode::Z) => println!("Redo"),
buffer.insert(cursor_position, key); Some(Keycode::X) => println!("Cut line(s)"),
cursor_position += 1; Some(Keycode::C) => println!("Copy line(s)"),
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)?
}, },
_ => (), _ => (),
@ -177,12 +203,11 @@ pub fn main() -> Result<(), String> {
}, },
Event::TextInput { text, .. } => { Event::TextInput { text, .. } => {
if !modifier_keys.alt && !modifier_keys.ctrl { let input_char = text.chars().nth(0).expect("Empty");
let input_char = text.chars().nth(0).expect("Empty");
buffer.insert(cursor_position, input_char); buffer.insert(cursor_position, input_char);
cursor_position += 1; cursor_position += 1;
draw_text(&buffer, cursor_position)?; draw_text(&buffer, cursor_position)?;
}
}, },
_ => {} _ => {}

Loading…
Cancel
Save