Added controls to open menus

Former-commit-id: cfa167aef56e12def40a083effe37d1b0216c00f
This commit is contained in:
Pfauenauge90 2019-04-02 03:05:18 +02:00
parent 87a41a30a7
commit 8378663cca
3 changed files with 130 additions and 27 deletions

View File

@ -368,6 +368,7 @@ pub struct Hud {
menu_open: bool,
open_windows: Windows,
map_open: bool,
show_ui: bool,
settings_tab: SettingsTab,
}
@ -409,6 +410,7 @@ impl Hud {
bag_open: false,
menu_open: false,
map_open: false,
show_ui: true,
open_windows: Windows::None,
font_metamorph,
font_opensans,
@ -418,7 +420,7 @@ impl Hud {
fn update_layout(&mut self) -> Vec<Event> {
let mut events = Vec::new();
let ref mut ui_widgets = self.ui.set_widgets();
if self.show_ui {
// Chat box
if let Some(msg) = self
.chat
@ -1210,6 +1212,7 @@ impl Hud {
events.push(Event::Quit);
};
}
}
// update whether keyboard is captured
self.typing =
if let Some(widget_id) = ui_widgets.global_input().current.widget_capturing_keyboard {
@ -1228,6 +1231,72 @@ impl Hud {
pub fn toggle_menu(&mut self) {
self.menu_open = !self.menu_open;
}
pub fn toggle_inventory(&mut self) {
self.bag_open = !self.bag_open
}
pub fn toggle_questlog(&mut self) {
self.open_windows = match self.open_windows {
Windows::Small(Small::Questlog) => Windows::None,
Windows::None | Windows::Small(_) => Windows::Small(Small::Questlog),
Windows::CharacterAnd(small) => match small {
Some(Small::Questlog) => Windows::CharacterAnd(None),
_ => Windows::CharacterAnd(Some(Small::Questlog)),
},
Windows::Settings => unreachable!(),
};
}
pub fn toggle_map(&mut self) {
self.map_open = !self.map_open;
self.bag_open = false;
}
pub fn toggle_charwindow(&mut self) {
self.open_windows = match self.open_windows {
Windows::CharacterAnd(small) => match small {
Some(small) => Windows::Small(small),
None => Windows::None,
},
Windows::Small(small) => Windows::CharacterAnd(Some(small)),
Windows::None => Windows::CharacterAnd(None),
Windows::Settings => unreachable!(),
}
}
pub fn toggle_social(&mut self) {
self.open_windows = match self.open_windows {
Windows::Small(Small::Social) => Windows::None,
Windows::None | Windows::Small(_) => Windows::Small(Small::Social),
Windows::CharacterAnd(small) => match small {
Some(Small::Social) => Windows::CharacterAnd(None),
_ => Windows::CharacterAnd(Some(Small::Social)),
},
Windows::Settings => unreachable!(),
};
}
pub fn toggle_spellbook(&mut self) {
self.open_windows = match self.open_windows {
Windows::Small(Small::Spellbook) => Windows::None,
Windows::None | Windows::Small(_) => Windows::Small(Small::Spellbook),
Windows::CharacterAnd(small) => match small {
Some(Small::Spellbook) => Windows::CharacterAnd(None),
_ => Windows::CharacterAnd(Some(Small::Spellbook)),
},
Windows::Settings => unreachable!(),
};
}
pub fn toggle_settings(&mut self) {
self.open_windows = match self.open_windows {
Windows::Settings => Windows::None,
_ => Windows::Settings,
};
self.bag_open = false;
}
pub fn toggle_help(&mut self) {
self.show_help = !self.show_help
}
pub fn toggle_ui(&mut self) {
self.show_ui = !self.show_ui;
}
pub fn update_grab(&mut self, cursor_grabbed: bool) {
self.cursor_grabbed = cursor_grabbed;
@ -1261,7 +1330,8 @@ impl Hud {
} else {
false
}
}
}
WinEvent::KeyDown(key) | WinEvent::KeyUp(key) => match key {
Key::ToggleCursor => false,
_ => self.typing,

View File

@ -5,23 +5,17 @@ use std::time::Duration;
use vek::*;
// Project
use client::{self, Client};
use common::clock::Clock;
use client::{
self,
Client,
};
// Crate
use crate::{
Error,
PlayState,
PlayStateResult,
GlobalState,
hud::{Event as HudEvent, Hud},
key_state::KeyState,
window::{Event, Key, Window},
render::Renderer,
scene::Scene,
hud::{Hud, Event as HudEvent},
window::{Event, Key, Window},
Error, GlobalState, PlayState, PlayStateResult,
};
const FPS: u64 = 60;
@ -49,7 +43,12 @@ impl SessionState {
}
// The background colour
const BG_COLOR: Rgba<f32> = Rgba { r: 0.0, g: 0.3, b: 1.0, a: 1.0 };
const BG_COLOR: Rgba<f32> = Rgba {
r: 0.0,
g: 0.3,
b: 1.0,
a: 1.0,
};
impl SessionState {
/// Tick the session (and the client attached to it)
@ -70,7 +69,7 @@ impl SessionState {
}
}
}
Ok(())
}
@ -117,24 +116,33 @@ impl PlayState for SessionState {
loop {
// Handle window events
for event in global_state.window.fetch_events() {
// Pass all events to the ui first
if self.hud.handle_event(event.clone()) {
continue;
}
let _handled = match event {
Event::Close => return PlayStateResult::Shutdown,
// When 'q' is pressed, exit the session
Event::Char('q') => return PlayStateResult::Pop,
// When 'm' is pressed, open/close the in-game test menu
Event::Char('m') => self.hud.toggle_menu(),
// When 'm' is pressed, open/close the map window
Event::Char('m') => self.hud.toggle_map(),
Event::Char('i') => self.hud.toggle_inventory(),
Event::Char('l') => self.hud.toggle_questlog(),
Event::Char('c') => self.hud.toggle_charwindow(),
Event::Char('o') => self.hud.toggle_social(),
Event::Char('p') => self.hud.toggle_spellbook(),
Event::Char('n') => self.hud.toggle_settings(),
Event::KeyDown(Key::Interface) => self.hud.toggle_help(),
Event::KeyDown(Key::Help) => self.hud.toggle_ui(),
// Close windows on esc
Event::KeyDown(Key::Escape) => self.hud.toggle_windows(),
// Toggle cursor grabbing
Event::KeyDown(Key::ToggleCursor) => {
global_state.window.grab_cursor(!global_state.window.is_cursor_grabbed());
self.hud.update_grab(global_state.window.is_cursor_grabbed());
},
global_state
.window
.grab_cursor(!global_state.window.is_cursor_grabbed());
self.hud
.update_grab(global_state.window.is_cursor_grabbed());
}
// Movement Key Pressed
Event::KeyDown(Key::MoveForward) => self.key_state.up = true,
Event::KeyDown(Key::MoveBack) => self.key_state.down = true,
@ -146,7 +154,9 @@ impl PlayState for SessionState {
Event::KeyUp(Key::MoveLeft) => self.key_state.left = false,
Event::KeyUp(Key::MoveRight) => self.key_state.right = false,
// Pass all other events to the scene
event => { self.scene.handle_input_event(event); },
event => {
self.scene.handle_input_event(event);
}
};
// TODO: Do something if the event wasn't handled?
}
@ -156,14 +166,15 @@ impl PlayState for SessionState {
.expect("Failed to tick the scene");
// Maintain the scene
self.scene.maintain(global_state.window.renderer_mut(), &self.client);
self.scene
.maintain(global_state.window.renderer_mut(), &self.client);
// Maintain the UI
for event in self.hud.maintain(global_state.window.renderer_mut()) {
match event {
HudEvent::SendMessage(msg) => {
// TODO: Handle result
self.client.send_chat(msg);
},
}
HudEvent::Logout => return PlayStateResult::Pop,
HudEvent::Quit => return PlayStateResult::Shutdown,
}
@ -173,7 +184,8 @@ impl PlayState for SessionState {
self.render(global_state.window.renderer_mut());
// Display the frame on the window
global_state.window
global_state
.window
.swap_buffers()
.expect("Failed to swap window buffers");
@ -185,5 +197,7 @@ impl PlayState for SessionState {
}
}
fn name(&self) -> &'static str { "Session" }
fn name(&self) -> &'static str {
"Session"
}
}

View File

@ -53,6 +53,16 @@ impl Window {
key_map.insert(glutin::VirtualKeyCode::A, Key::MoveLeft);
key_map.insert(glutin::VirtualKeyCode::S, Key::MoveBack);
key_map.insert(glutin::VirtualKeyCode::D, Key::MoveRight);
key_map.insert(glutin::VirtualKeyCode::M, Key::Map);
key_map.insert(glutin::VirtualKeyCode::I, Key::Inventory);
key_map.insert(glutin::VirtualKeyCode::L, Key::QuestLog);
key_map.insert(glutin::VirtualKeyCode::C, Key::CharacterWindow);
key_map.insert(glutin::VirtualKeyCode::O, Key::Social);
key_map.insert(glutin::VirtualKeyCode::P, Key::Spellbook);
key_map.insert(glutin::VirtualKeyCode::N, Key::Settings);
key_map.insert(glutin::VirtualKeyCode::F1, Key::Help);
key_map.insert(glutin::VirtualKeyCode::F2, Key::Interface);
let tmp = Ok(Self {
events_loop,
@ -171,6 +181,15 @@ pub enum Key {
MoveRight,
Enter,
Escape,
Map,
Inventory,
QuestLog,
CharacterWindow,
Social,
Spellbook,
Settings,
Interface,
Help,
}
/// Represents an incoming event from the window