Skip to content

Commit

Permalink
[ui] Add support for modal windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Lysai authored and pingw33n committed Jun 18, 2020
1 parent 1ed09b5 commit 5ef6726
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/game/skilldex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,34 @@ pub enum Command {

pub struct Skilldex {
msgs: Messages,
ui: Option<SkilldexUi>,
window: Option<Handle>,
}

impl Skilldex {
pub fn new(fs: &FileSystem, language: &str) -> Self {
let msgs = Messages::read_file(fs, language, "game/skilldex.msg").unwrap();
Self {
msgs,
ui: None,
window: None,
}
}

pub fn is_visible(&self) -> bool {
self.ui.is_some()
self.window.is_some()
}

pub fn show(&mut self,
ui: &mut Ui,
levels: EnumMap<Skill, i32>,
target: Option<object::Handle>,
) {
assert!(self.ui.is_none());

let underlay = ui.new_window(Rect::with_size(0, 0, 640, 480), None);
assert!(self.window.is_none());

let win_size = ui.frm_db().get(FrameId::SKILLDEX_WINDOW).unwrap().first().size();
let window = ui.new_window(Rect::with_size(
640 - win_size.x - 4, 379 - win_size.y - 6, win_size.x, win_size.y),
Some(Sprite::new(FrameId::SKILLDEX_WINDOW)));
ui.set_modal_window(Some(window));

let mut header = Panel::new();
header.set_text(Some(panel::Text {
Expand Down Expand Up @@ -132,20 +131,11 @@ impl Skilldex {
cancel.set_text(Some(text));
ui.new_widget(window, Rect::with_size(48, 338, 90, btn_size.y), None, None, cancel);

self.ui = Some(SkilldexUi {
underlay,
window,
});
self.window = Some(window);
}

pub fn hide(&mut self, ui: &mut Ui) {
let the_ui = self.ui.take().unwrap();
ui.remove(the_ui.underlay);
ui.remove(the_ui.window);
let window = self.window.take().unwrap();
ui.remove(window);
}
}

struct SkilldexUi {
underlay: Handle,
window: Handle,
}
19 changes: 19 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub struct Ui {
simulate_mouse_move: bool,
mouse_focus: Option<Handle>,
keyboard_focus: Option<Handle>,
modal_window: Option<Handle>,
}

impl Ui {
Expand All @@ -161,6 +162,7 @@ impl Ui {
simulate_mouse_move: false,
mouse_focus: None,
keyboard_focus: None,
modal_window: None,
}
}

Expand Down Expand Up @@ -202,6 +204,9 @@ impl Ui {
if self.keyboard_focus == Some(handle) {
self.keyboard_focus = None;
}
if self.modal_window == Some(handle) {
self.modal_window = None;
}
self.widget_bases.remove(handle);

let widg = widg.borrow();
Expand Down Expand Up @@ -335,6 +340,17 @@ impl Ui {
self.cursor_constraints.truncate(1);
}

pub fn is_window(&self, handle: Handle) -> bool {
self.widget(handle).borrow().downcast_ref::<Window>().is_some()
}

pub fn set_modal_window(&mut self, win: Option<Handle>) {
if let Some(win) = win {
assert!(self.is_window(win));
}
self.modal_window = win;
}

fn widget_handle_event(&mut self,
now: Instant,
target: Handle,
Expand Down Expand Up @@ -499,6 +515,9 @@ impl Ui {
}
return Some(winh);
}
if Some(winh) == self.modal_window {
break;
}
}
None
}
Expand Down

0 comments on commit 5ef6726

Please sign in to comment.