Cleanup
parent
5059e69cbb
commit
193feb5b92
|
@ -443,6 +443,7 @@ impl GPUState {
|
|||
}
|
||||
}
|
||||
|
||||
// Render utilities
|
||||
impl GPUState {
|
||||
fn push_ships(
|
||||
&mut self,
|
||||
|
|
|
@ -182,8 +182,8 @@ impl UiManager {
|
|||
|
||||
/// Draw all ui elements
|
||||
pub fn draw(&mut self, input: Rc<RenderInput>, state: &mut RenderState) -> Result<()> {
|
||||
if self.current_scene.is_none() {
|
||||
// Initialize start scene if we haven't yet
|
||||
if self.current_scene.is_none() {
|
||||
self.set_scene(
|
||||
state,
|
||||
input.clone(),
|
||||
|
@ -191,13 +191,16 @@ impl UiManager {
|
|||
)?;
|
||||
}
|
||||
|
||||
// Update timings if they're being displayed
|
||||
if self.show_timings {
|
||||
self.fps_indicator.step(&input, state);
|
||||
}
|
||||
|
||||
// Check for player ship state changes
|
||||
let mut keep_doing_actions = true;
|
||||
|
||||
// Send player state change events
|
||||
if {
|
||||
let player = input.player.ship;
|
||||
let send_event = {
|
||||
if let Some(player) = player {
|
||||
let ship = input.phys_img.get_ship(&PhysSimShipHandle(player)).unwrap();
|
||||
if self.last_player_state == 0
|
||||
|
@ -213,10 +216,7 @@ impl UiManager {
|
|||
self.last_player_state = 0;
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
let mut actions = Vec::new();
|
||||
if send_event {
|
||||
} {
|
||||
let action: Dynamic = self
|
||||
.engine
|
||||
.call_fn(
|
||||
|
@ -236,101 +236,56 @@ impl UiManager {
|
|||
})?;
|
||||
|
||||
if let Some(action) = action.try_cast::<SceneAction>() {
|
||||
match action {
|
||||
SceneAction::None => {}
|
||||
_ => {
|
||||
actions.push(action);
|
||||
}
|
||||
}
|
||||
keep_doing_actions = self.handle_action(state, input.clone(), action)?;
|
||||
}
|
||||
}
|
||||
|
||||
for e in &self.elements {
|
||||
let action: Dynamic = {
|
||||
if let Some(e) = e.sprite() {
|
||||
let mut x = (*e).borrow_mut();
|
||||
let event = x.check_mouse(&input, state);
|
||||
x.step(&input, state);
|
||||
x.push_to_buffer(&input, state);
|
||||
drop(x);
|
||||
for i in 0..self.elements.len() {
|
||||
match &self.elements[i] {
|
||||
UiElement::Sprite(e) => {
|
||||
// Draw and update sprites
|
||||
let mut sprite = (*e).borrow_mut();
|
||||
sprite.step(&input, state);
|
||||
sprite.push_to_buffer(&input, state);
|
||||
|
||||
if !keep_doing_actions {
|
||||
continue;
|
||||
}
|
||||
|
||||
let event = sprite.check_events(&input, state);
|
||||
// we MUST drop here, since script calls mutate the sprite RefCell
|
||||
drop(sprite);
|
||||
|
||||
match event {
|
||||
Event::None => Dynamic::from(SceneAction::None),
|
||||
Event::None => continue,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Event::MouseClick => self
|
||||
.engine
|
||||
.call_fn(
|
||||
&mut self.scope,
|
||||
&self
|
||||
.ct
|
||||
.get_config()
|
||||
.ui_scenes
|
||||
.get(self.current_scene.as_ref().unwrap())
|
||||
.unwrap(),
|
||||
"event",
|
||||
(
|
||||
State::new(input.clone()),
|
||||
MouseClickEvent {
|
||||
let event_arg = match event {
|
||||
Event::None => unreachable!("this shouldn't happen"),
|
||||
|
||||
Event::MouseClick => Dynamic::from(MouseClickEvent {
|
||||
down: true,
|
||||
element: SpriteElement::new(self.ct.clone(), e.clone()),
|
||||
},
|
||||
),
|
||||
)
|
||||
.with_context(|| format!("while handling click event"))
|
||||
.with_context(|| {
|
||||
format!("in ui scene `{}`", self.current_scene.as_ref().unwrap())
|
||||
})?,
|
||||
}),
|
||||
|
||||
Event::MouseRelease => self
|
||||
.engine
|
||||
.call_fn(
|
||||
&mut self.scope,
|
||||
&self
|
||||
.ct
|
||||
.get_config()
|
||||
.ui_scenes
|
||||
.get(self.current_scene.as_ref().unwrap())
|
||||
.unwrap(),
|
||||
"event",
|
||||
(
|
||||
State::new(input.clone()),
|
||||
MouseClickEvent {
|
||||
Event::MouseRelease => Dynamic::from(MouseClickEvent {
|
||||
down: false,
|
||||
element: SpriteElement::new(self.ct.clone(), e.clone()),
|
||||
},
|
||||
),
|
||||
)
|
||||
.with_context(|| format!("while handling release event"))
|
||||
.with_context(|| {
|
||||
format!("in ui scene `{}`", self.current_scene.as_ref().unwrap())
|
||||
})?,
|
||||
}),
|
||||
|
||||
Event::MouseHover => self
|
||||
.engine
|
||||
.call_fn(
|
||||
&mut self.scope,
|
||||
&self
|
||||
.ct
|
||||
.get_config()
|
||||
.ui_scenes
|
||||
.get(self.current_scene.as_ref().unwrap())
|
||||
.unwrap(),
|
||||
"event",
|
||||
(
|
||||
State::new(input.clone()),
|
||||
MouseHoverEvent {
|
||||
Event::MouseHover => Dynamic::from(MouseHoverEvent {
|
||||
enter: true,
|
||||
element: SpriteElement::new(self.ct.clone(), e.clone()),
|
||||
},
|
||||
),
|
||||
)
|
||||
.with_context(|| format!("while handling hover event"))
|
||||
.with_context(|| {
|
||||
format!("in ui scene `{}`", self.current_scene.as_ref().unwrap())
|
||||
})?,
|
||||
}),
|
||||
|
||||
Event::MouseUnhover => self
|
||||
Event::MouseUnhover => Dynamic::from(MouseHoverEvent {
|
||||
enter: false,
|
||||
element: SpriteElement::new(self.ct.clone(), e.clone()),
|
||||
}),
|
||||
};
|
||||
|
||||
let action: Dynamic = self
|
||||
.engine
|
||||
.call_fn(
|
||||
&mut self.scope,
|
||||
|
@ -341,51 +296,48 @@ impl UiManager {
|
|||
.get(self.current_scene.as_ref().unwrap())
|
||||
.unwrap(),
|
||||
"event",
|
||||
(
|
||||
State::new(input.clone()),
|
||||
MouseHoverEvent {
|
||||
enter: false,
|
||||
element: SpriteElement::new(self.ct.clone(), e.clone()),
|
||||
},
|
||||
),
|
||||
(State::new(input.clone()), event_arg),
|
||||
)
|
||||
.with_context(|| format!("while handling unhover event"))
|
||||
.with_context(|| format!("while handling event `{:?}`", event))
|
||||
.with_context(|| {
|
||||
format!("in ui scene `{}`", self.current_scene.as_ref().unwrap())
|
||||
})?,
|
||||
})?;
|
||||
|
||||
if let Some(action) = action.try_cast::<SceneAction>() {
|
||||
keep_doing_actions = self.handle_action(state, input.clone(), action)?;
|
||||
}
|
||||
} else if let Some(e) = e.radialbar() {
|
||||
}
|
||||
|
||||
UiElement::RadialBar(e) => {
|
||||
// Draw and update radialbar
|
||||
let mut x = (*e).borrow_mut();
|
||||
x.step(&input, state);
|
||||
x.push_to_buffer(&input, state);
|
||||
Dynamic::from(SceneAction::None)
|
||||
} else {
|
||||
Dynamic::from(SceneAction::None)
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(action) = action.try_cast::<SceneAction>() {
|
||||
match action {
|
||||
SceneAction::None => {}
|
||||
_ => {
|
||||
actions.push(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for a in actions {
|
||||
match a {
|
||||
SceneAction::None => {}
|
||||
SceneAction::GoTo(s) => {
|
||||
self.set_scene(state, input.clone(), s.clone())?;
|
||||
break;
|
||||
}
|
||||
UiElement::Text(..) => {}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
/// Do a SceneAction.
|
||||
/// Returns true if we should execute the action that is next, and false otherwise.
|
||||
fn handle_action(
|
||||
&mut self,
|
||||
state: &mut RenderState,
|
||||
input: Rc<RenderInput>,
|
||||
action: SceneAction,
|
||||
) -> Result<bool> {
|
||||
Ok(match action {
|
||||
SceneAction::None => true,
|
||||
SceneAction::GoTo(s) => {
|
||||
self.set_scene(state, input.clone(), s.clone())?;
|
||||
false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> UiManager {
|
||||
|
|
|
@ -22,24 +22,10 @@ impl UiElement {
|
|||
Self::Text(text)
|
||||
}
|
||||
|
||||
pub fn sprite(&self) -> Option<Rc<RefCell<Sprite>>> {
|
||||
match self {
|
||||
Self::Sprite(s) => Some(s.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text(&self) -> Option<&TextBox> {
|
||||
match self {
|
||||
Self::Text(t) => Some(t),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn radialbar(&self) -> Option<Rc<RefCell<RadialBar>>> {
|
||||
match self {
|
||||
Self::RadialBar(r) => Some(r.clone()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ impl Sprite {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn check_mouse(&mut self, input: &RenderInput, state: &mut RenderState) -> Event {
|
||||
pub fn check_events(&mut self, input: &RenderInput, state: &mut RenderState) -> Event {
|
||||
let r = self.rect.to_centered(state, input.ct.get_config().ui_scale);
|
||||
|
||||
if self.waiting_for_release && self.has_mouse && !input.player.input.pressed_leftclick() {
|
||||
|
|
Loading…
Reference in New Issue