Skip to content

Commit

Permalink
More console progress: outputless commands
Browse files Browse the repository at this point in the history
+:vpoffset, :vpfactor, :q

Cf. #4
  • Loading branch information
ctrlcctrlv committed Sep 15, 2020
1 parent 319cf55 commit f08c3ea
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ derive_more = "0.99.10"
# File dialog
nfd = { git = "https://github.com/raphlinus/nfd-rs", rev = "5e09b79bf511e3a91ae8cefdb96e9734fa4a79c2" }

# Command parsing for console
regex = "1"

## Our crates
# parses .glif files and gives us a place to put arbitrary data
glifparser = { git = "https://github.com/mfeq/glifparser" }
Expand Down
45 changes: 45 additions & 0 deletions src/events/console/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#![feature(unboxed_closures)]
use crate::events;
use std::collections::HashMap;

type Callback = Box<(Fn(Vec<String>) -> () + 'static)>;

fn callback<F>(f: F) -> Callback
where
F: Fn(Vec<String>) -> () + 'static,
{
Box::new(f) as Callback
}

use crate::STATE;
thread_local! {
pub static MAP: HashMap<&'static str, Callback> = {
let mut h = HashMap::new();

h.insert("vpoffset", callback(|s| {
if s.len() != 2 { return; } // FIXME: Tell user about errors!
if let (Ok(ox), Ok(oy)) = (s[0].parse(), s[1].parse()) {
STATE.with(|v| {
v.borrow_mut().offset = (ox, oy);
events::update_viewport(Some((ox, oy)), None, &v);
});
}
}));

h.insert("vpfactor", callback(|s| {
if s.len() != 1 { return; } // FIXME: Tell user about errors!
if let Ok(factor) = s[0].parse() {
STATE.with(|v| {
v.borrow_mut().factor = factor;
events::update_viewport(None, Some(factor), &v);
});
}
}));

h.insert("q", callback(|_| {
STATE.with(|v| v.borrow_mut().quit_requested = true);
}));

h
};
}
22 changes: 20 additions & 2 deletions src/events/console.rs → src/events/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::CONSOLE;

use clipboard::{ClipboardContext, ClipboardProvider};

mod commands;

use winit::event::{ModifiersState, VirtualKeyCode};
// Only called if ElementState::Pressed
pub fn set_state(vk: VirtualKeyCode, m: ModifiersState) {
Expand All @@ -17,8 +19,10 @@ pub fn set_state(vk: VirtualKeyCode, m: ModifiersState) {
c.borrow_mut().active(false);
}
VirtualKeyCode::Return => {
if c.borrow().active {
run_command(&mut c.borrow_mut());
}
c.borrow_mut().active(false);
run_command(&mut c.borrow_mut());
}
_ => {}
});
Expand Down Expand Up @@ -51,7 +55,21 @@ impl RendererConsole {
}
}

use regex::{self, Regex};
pub fn run_command(c: &mut RendererConsole) {
debug!("Command requested to be run: {}", &c.stdin);
lazy_static! {
static ref COMMAND_RE: Regex = Regex::new(r"\s+").unwrap();
}

let cmdline: Vec<_> = COMMAND_RE.split(&c.stdin).collect();

let (command, args) = (&cmdline[0][1..], &cmdline[1..]);

commands::MAP.with(|m| {
m.get(command)
.map(|f| f(args.to_vec().iter().map(|s| s.to_string()).collect()))
});

debug!("Command requested to be run: {:?}", (command, args));
c.stdin.clear()
}
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ extern crate skulpin;
extern crate skulpin_plugin_imgui;

extern crate clipboard;
extern crate regex;

// Our crates
extern crate glifparser;
extern crate mfeq_ipc;
Expand All @@ -40,7 +42,7 @@ use winit::event_loop::{ControlFlow, EventLoop};

use skia_safe::{Contains, Point, Rect};

use enum_iterator::IntoEnumIterator;
use enum_iterator::IntoEnumIterator as _;

use std::time::Instant;

Expand Down Expand Up @@ -140,6 +142,9 @@ fn main() {
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
debug_event!("{:?}", event);
if STATE.with(|v|v.borrow().quit_requested) {
*control_flow = ControlFlow::Exit;
}

// Without this, the program will crash if it launches with the cursor over the window, as
// the mouse event occurs before the redraw, which means that it uses an uninitialized
Expand Down Expand Up @@ -195,16 +200,16 @@ fn main() {
}

// We write to the Console in ReceivedCharacter, not here.
CONSOLE.with(|c| {
if CONSOLE.with(|c| {
if c.borrow().active {
if let Some(VirtualKeyCode::V) = virtual_keycode {
if modifiers.ctrl() {
c.borrow_mut().handle_clipboard();
}
}
return;
}
});
c.borrow().active
}) { return };

STATE.with(|v| {
let mode = v.borrow().mode;
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ impl Console {
let font =
Font::from_typeface_with_params(&*CONSOLE_TYPEFACE, 14. * (1. / factor), 1., 0.0);
let winsize = v.borrow().winsize;
let mut topleft = (offset.0, (winsize.height as f32) * (1. / factor));
let mut size = ((winsize.width as f32) * (1. / factor), offset.1);
let mut topleft = (
-(offset.0 * (1. / factor)),
(winsize.height as f32) * (1. / factor),
);
topleft.1 -= offset.1 * (1. / factor);
let mut size = ((winsize.width as f32) * (1. / factor), 0.);

let (_, trect) = font.measure_str("Q", None);
topleft.1 -= (CONSOLE_PADDING_Y_TOP + CONSOLE_PADDING_Y_BOTTOM) * (1. / factor);
Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub struct State<T> {
pub offset: (f32, f32),
pub dpi: f64, // from glutin scale_factor()
pub ipc_info: Option<mfeq_ipc::IPCInfo>,
pub quit_requested: bool,
}

impl<T> State<T> {
Expand All @@ -113,6 +114,7 @@ impl<T> State<T> {
offset: (0., 0.),
dpi: 1.,
ipc_info: None,
quit_requested: false,
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ pub fn set_panic_hook() {
set_hook(Box::new(|info| {
let msg = info.payload().downcast_ref::<&str>();

match msg {
Some(info) => eprintln!("\n{}\n", info.bright_red()),
_ => {}
if let Some(info) = msg {
eprintln!("\n{}\n", info.bright_red());
}

if let Some(args) = info.message() {
eprintln!("\n{}\n", args.to_string().bright_red());
}

if env::var("RUST_BACKTRACE").is_ok() {
Expand Down

0 comments on commit f08c3ea

Please sign in to comment.