|
|
|
@ -1,8 +1,10 @@ |
|
|
|
extern crate sdl2; |
|
|
|
extern crate sdl2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use clipboard::{ClipboardProvider, ClipboardContext}; |
|
|
|
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::Point; |
|
|
|
|
|
|
|
|
|
|
|
mod editor_render; |
|
|
|
mod editor_render; |
|
|
|
|
|
|
|
|
|
|
|
@ -16,6 +18,7 @@ struct ModifierKeys { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn main() -> Result<(), String> { |
|
|
|
pub fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap(); |
|
|
|
let glyph_atlas = editor_render::generate_glyph_atlas(); |
|
|
|
let glyph_atlas = editor_render::generate_glyph_atlas(); |
|
|
|
|
|
|
|
|
|
|
|
let sdl_context = sdl2::init()?; |
|
|
|
let sdl_context = sdl2::init()?; |
|
|
|
@ -34,6 +37,8 @@ pub fn main() -> Result<(), String> { |
|
|
|
let mut cursor_position = 0; |
|
|
|
let mut cursor_position = 0; |
|
|
|
let mut selection_anchor: Option<usize> = None; |
|
|
|
let mut selection_anchor: Option<usize> = None; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let pad_offset = Point::new(10, 10); |
|
|
|
|
|
|
|
|
|
|
|
let mut draw_text = |text: &str, pos: usize| -> Result<(), String> { |
|
|
|
let mut draw_text = |text: &str, pos: usize| -> Result<(), String> { |
|
|
|
// Draw background
|
|
|
|
// Draw background
|
|
|
|
canvas.set_draw_color(Color::RGB(32, 32, 32)); |
|
|
|
canvas.set_draw_color(Color::RGB(32, 32, 32)); |
|
|
|
@ -41,12 +46,20 @@ 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 = editor_render::draw_text(text, glyph_atlas.clone()); |
|
|
|
let fb_text = editor_render::draw_text( |
|
|
|
|
|
|
|
&glyph_atlas, |
|
|
|
|
|
|
|
text, |
|
|
|
|
|
|
|
pad_offset |
|
|
|
|
|
|
|
); |
|
|
|
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 = editor_render::draw_cursor(text, pos); |
|
|
|
let fb_cursor = editor_render::draw_cursor( |
|
|
|
|
|
|
|
pos, |
|
|
|
|
|
|
|
text, |
|
|
|
|
|
|
|
pad_offset |
|
|
|
|
|
|
|
); |
|
|
|
canvas.draw_line(fb_cursor.0, fb_cursor.1)?; |
|
|
|
canvas.draw_line(fb_cursor.0, fb_cursor.1)?; |
|
|
|
|
|
|
|
|
|
|
|
canvas.present(); |
|
|
|
canvas.present(); |
|
|
|
@ -102,6 +115,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
match (modifier_keys.shift, modifier_keys.ctrl, modifier_keys.alt) { |
|
|
|
match (modifier_keys.shift, modifier_keys.ctrl, modifier_keys.alt) { |
|
|
|
|
|
|
|
// All modifiers up
|
|
|
|
(false, false, false) => { |
|
|
|
(false, false, false) => { |
|
|
|
match keycode { |
|
|
|
match keycode { |
|
|
|
// DELETE key
|
|
|
|
// DELETE key
|
|
|
|
@ -134,6 +148,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
|
|
|
|
// Left/Back arrow
|
|
|
|
// Left/Back arrow
|
|
|
|
Some(Keycode::Left) => { |
|
|
|
Some(Keycode::Left) => { |
|
|
|
|
|
|
|
selection_anchor = None; |
|
|
|
cursor_position = usize::checked_sub(cursor_position, 1) |
|
|
|
cursor_position = usize::checked_sub(cursor_position, 1) |
|
|
|
.unwrap_or(0); |
|
|
|
.unwrap_or(0); |
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
@ -141,6 +156,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
|
|
|
|
// Right/Forward arrow
|
|
|
|
// Right/Forward arrow
|
|
|
|
Some(Keycode::Right) => { |
|
|
|
Some(Keycode::Right) => { |
|
|
|
|
|
|
|
selection_anchor = None; |
|
|
|
cursor_position = (cursor_position + 1).min(buffer.len()); |
|
|
|
cursor_position = (cursor_position + 1).min(buffer.len()); |
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
}, |
|
|
|
}, |
|
|
|
@ -159,11 +175,14 @@ pub fn main() -> Result<(), String> { |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CTRL down
|
|
|
|
(false, true, false) => { |
|
|
|
(false, true, false) => { |
|
|
|
match keycode { |
|
|
|
match keycode { |
|
|
|
Some(Keycode::Z) => println!("Undo"), |
|
|
|
Some(Keycode::Z) => println!("Undo"), |
|
|
|
Some(Keycode::X) => println!("Cut"), |
|
|
|
Some(Keycode::X) => println!("Cut"), |
|
|
|
Some(Keycode::C) => println!("Copy"), |
|
|
|
Some(Keycode::C) => { |
|
|
|
|
|
|
|
clipboard_context.set_contents(buffer.clone()).unwrap() |
|
|
|
|
|
|
|
}, |
|
|
|
Some(Keycode::V) => println!("Paste"), |
|
|
|
Some(Keycode::V) => println!("Paste"), |
|
|
|
|
|
|
|
|
|
|
|
// BACKSPACE key
|
|
|
|
// BACKSPACE key
|
|
|
|
|