|
|
|
|
@ -1,10 +1,11 @@ |
|
|
|
|
extern crate sdl2; |
|
|
|
|
|
|
|
|
|
use clipboard::{ClipboardProvider, ClipboardContext}; |
|
|
|
|
use num_format::{Locale, ToFormattedString}; |
|
|
|
|
use sdl2::event::{Event, WindowEvent}; |
|
|
|
|
use sdl2::keyboard::Keycode; |
|
|
|
|
use sdl2::pixels::Color; |
|
|
|
|
use sdl2::rect::Point; |
|
|
|
|
use sdl2::rect::{Point, Rect}; |
|
|
|
|
|
|
|
|
|
mod editor_render; |
|
|
|
|
|
|
|
|
|
@ -34,13 +35,15 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
|
|
let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?; |
|
|
|
|
|
|
|
|
|
let mut window_size = canvas.output_size()?; |
|
|
|
|
|
|
|
|
|
let mut buffer = String::new(); |
|
|
|
|
let mut cursor_position = 0; |
|
|
|
|
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 = |window_size: (u32, u32), text: &str, pos: usize| -> Result<(), String> { |
|
|
|
|
// Draw background
|
|
|
|
|
canvas.set_draw_color(Color::RGB(32, 32, 32)); |
|
|
|
|
canvas.clear(); |
|
|
|
|
@ -55,6 +58,29 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
); |
|
|
|
|
canvas.draw_points(&fb_text[..])?; |
|
|
|
|
|
|
|
|
|
// Draw info
|
|
|
|
|
let status = text.len().to_formatted_string(&Locale::en); |
|
|
|
|
let status_position = Point::new( |
|
|
|
|
pad_offset.x, |
|
|
|
|
window_size.1 as i32 - glyph_metrics.height as i32 * 2 |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
canvas.set_draw_color(Color::RGB(16, 64, 64)); |
|
|
|
|
canvas.fill_rect(Rect::new(0, |
|
|
|
|
status_position.y - 5, |
|
|
|
|
window_size.0, |
|
|
|
|
glyph_metrics.height as u32 + 10 |
|
|
|
|
))?; |
|
|
|
|
|
|
|
|
|
canvas.set_draw_color(Color::RGB(127, 240, 240)); |
|
|
|
|
let status_bar = editor_render::draw_text( |
|
|
|
|
&glyph_atlas, |
|
|
|
|
&glyph_metrics, |
|
|
|
|
&status, |
|
|
|
|
status_position |
|
|
|
|
); |
|
|
|
|
canvas.draw_points(&status_bar[..])?; |
|
|
|
|
|
|
|
|
|
// Draw cursor
|
|
|
|
|
canvas.set_draw_color(Color::RGB(64, 240, 240)); |
|
|
|
|
let fb_cursor = editor_render::draw_cursor( |
|
|
|
|
@ -70,7 +96,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
Ok(()) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
draw_text("", cursor_position)?; |
|
|
|
|
draw_text(window_size, "", cursor_position)?; |
|
|
|
|
|
|
|
|
|
let mut modifier_keys = ModifierKeys {alt: false, ctrl: false, shift: false}; |
|
|
|
|
|
|
|
|
|
@ -79,8 +105,9 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
for event in sdl_context.event_pump()?.poll_iter() { |
|
|
|
|
match event { |
|
|
|
|
Event::Window { win_event, .. } => { |
|
|
|
|
if let WindowEvent::Resized(_w, _h) = win_event { |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
if let WindowEvent::Resized(w, h) = win_event { |
|
|
|
|
window_size = (w as u32, h as u32); |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// ESC or SIGKILL
|
|
|
|
|
@ -130,7 +157,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
Some(Keycode::Delete) => { |
|
|
|
|
if buffer.len() > 0 && cursor_position < buffer.len() { |
|
|
|
|
buffer.remove(cursor_position); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
@ -139,19 +166,19 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
let key = '\n'; |
|
|
|
|
buffer.insert(cursor_position, key); |
|
|
|
|
cursor_position += 1; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// HOME key
|
|
|
|
|
Some(Keycode::Home) => { |
|
|
|
|
cursor_position = 0; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// END key
|
|
|
|
|
Some(Keycode::End) => { |
|
|
|
|
cursor_position = buffer.len(); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// Left/Back arrow
|
|
|
|
|
@ -159,14 +186,14 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
selection_anchor = None; |
|
|
|
|
cursor_position = usize::checked_sub(cursor_position, 1) |
|
|
|
|
.unwrap_or(0); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// Right/Forward arrow
|
|
|
|
|
Some(Keycode::Right) => { |
|
|
|
|
selection_anchor = None; |
|
|
|
|
cursor_position = (cursor_position + 1).min(buffer.len()); |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// BACKSPACE key
|
|
|
|
|
@ -175,7 +202,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
// Character backspace; regular
|
|
|
|
|
buffer.remove(cursor_position - 1); |
|
|
|
|
cursor_position -= 1; |
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
@ -209,7 +236,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
buffer.remove(cursor_position - 1); |
|
|
|
|
cursor_position -= 1; |
|
|
|
|
|
|
|
|
|
draw_text(&buffer, cursor_position)? |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)? |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
_ => (), |
|
|
|
|
@ -234,7 +261,7 @@ pub fn main() -> Result<(), String> { |
|
|
|
|
|
|
|
|
|
buffer.insert(cursor_position, input_char); |
|
|
|
|
cursor_position += 1; |
|
|
|
|
draw_text(&buffer, cursor_position)?; |
|
|
|
|
draw_text(window_size, &buffer, cursor_position)?; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
_ => {} |
|
|
|
|
|