Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dot operator #55

Merged
merged 5 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl<'a> Program<'a> {
render_help: false,
actual_size: false,
fullscreen: args.fullscreen,
last_action: Action::Noop,
},
})
}
Expand Down
59 changes: 42 additions & 17 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sdl2::keyboard::Keycode;
use sdl2::mouse::MouseButton;

/// Action represents the possible actions that could result from an event
#[derive(Clone)]
pub enum Action {
/// Quit indicates the app should quit in response to this event
Quit,
Expand Down Expand Up @@ -54,6 +55,24 @@ pub struct State {
pub actual_size: bool,
/// Tracks fullscreen state of app.
pub fullscreen: bool,
/// last_action records the last action performed. Used for repeating that action
pub last_action: Action,
}

impl State {
/// update_last_action takes an action, sets the last_action to said action, and returns the Action
fn process_action(&mut self, a: Action) -> Action {
match a {
Action::Quit | Action::ToggleFullscreen | Action::ReRender => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume ToggleFullscreen is here because it needs to rerender the screen?

self.last_action = Action::Noop;
a
}
_ => {
self.last_action = a.clone();
a
}
}
}
}

/// event_action returns which action should be performed in response to this event
Expand All @@ -67,15 +86,15 @@ pub fn event_action(state: &mut State, event: &Event) -> Action {
| Event::KeyDown {
keycode: Some(Keycode::Q),
..
} => Action::Quit,
} => state.process_action(Action::Quit),
Event::KeyDown {
keycode: Some(Keycode::F),
..
}
| Event::KeyDown {
keycode: Some(Keycode::F11),
..
} => Action::ToggleFullscreen,
} => state.process_action(Action::ToggleFullscreen),
Event::Window {
win_event: WindowEvent::Resized(_, _),
..
Expand All @@ -92,94 +111,100 @@ pub fn event_action(state: &mut State, event: &Event) -> Action {
| Event::Window {
win_event: WindowEvent::Maximized,
..
} => Action::ReRender,
} => state.process_action(Action::ReRender),
Event::KeyDown {
keycode: Some(Keycode::Z),
..
}
| Event::MouseButtonUp {
mouse_btn: MouseButton::Left,
..
} => Action::ToggleFit,
} => state.process_action(Action::ToggleFit),
Event::KeyDown {
keycode: Some(Keycode::Right),
..
}
| Event::KeyDown {
keycode: Some(Keycode::J),
..
} => Action::Next,
} => state.process_action(Action::Next),
Event::KeyDown {
keycode: Some(Keycode::Left),
..
}
| Event::KeyDown {
keycode: Some(Keycode::K),
..
} => Action::Prev,
} => state.process_action(Action::Prev),
Event::KeyDown {
keycode: Some(Keycode::G),
..
} => {
if state.left_shift || state.right_shift {
Action::Last
state.process_action(Action::Last)
} else {
Action::First
state.process_action(Action::First)
}
}
Event::KeyDown {
keycode: Some(Keycode::End),
..
} => Action::Last,
} => state.process_action(Action::Last),
Event::KeyDown {
keycode: Some(Keycode::Home),
..
} => Action::First,
} => state.process_action(Action::First),
Event::KeyDown {
keycode: Some(Keycode::C),
..
} => Action::Copy,
} => state.process_action(Action::Copy),
Event::KeyDown {
keycode: Some(Keycode::M),
..
} => Action::Move,
} => state.process_action(Action::Move),
Event::KeyDown {
keycode: Some(Keycode::W),
..
}
| Event::KeyDown {
keycode: Some(Keycode::PageUp),
..
} => Action::SkipForward,
} => state.process_action(Action::SkipForward),
Event::KeyDown {
keycode: Some(Keycode::B),
..
}
| Event::KeyDown {
keycode: Some(Keycode::PageDown),
..
} => Action::SkipBack,
} => state.process_action(Action::SkipBack),
Event::KeyDown {
keycode: Some(Keycode::D),
..
}
| Event::KeyDown {
keycode: Some(Keycode::Delete),
..
} => Action::Delete,
} => state.process_action(Action::Delete),
Event::KeyDown {
keycode: Some(Keycode::T),
..
} => {
state.render_infobar = !state.render_infobar;
Action::ReRender
state.process_action(Action::ReRender)
}
Event::KeyDown {
keycode: Some(Keycode::H),
..
} => {
state.render_help = !state.render_help;
Action::ReRender
state.process_action(Action::ReRender)
}
Event::KeyDown {
keycode: Some(Keycode::Period),
..
} => {
state.last_action.clone()
}
Event::KeyDown {
keycode: Some(Keycode::LShift),
Expand Down