Skip to content

Commit

Permalink
Overhaul buttons, enabling rects without graphics and changing mouse …
Browse files Browse the repository at this point in the history
…cursor icon
  • Loading branch information
Dove6 committed Aug 25, 2024
1 parent faef884 commit 93d8db9
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 132 deletions.
6 changes: 4 additions & 2 deletions pixlib/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use bevy_kira_audio::AudioPlugin;
use filesystems::{InsertedDisk, InsertedDiskResource};
use pixlib_parser::{common::IssueManager, runner::ObjectBuilderError};
use plugins::{
events_plugin::EventsPlugin, graphics_plugin::GraphicsPlugin, inputs_plugin::InputsPlugin,
scripts_plugin::ScriptsPlugin, sounds_plugin::SoundsPlugin, ui_plugin::UiPlugin,
app_plugin::AppPlugin, events_plugin::EventsPlugin, graphics_plugin::GraphicsPlugin,
inputs_plugin::InputsPlugin, scripts_plugin::ScriptsPlugin, sounds_plugin::SoundsPlugin,
ui_plugin::UiPlugin,
};
use resources::{ChosenScene, ObjectBuilderIssueManager, RootEntityToDespawn, WindowConfiguration};
use util::IssuePrinter;
Expand Down Expand Up @@ -80,6 +81,7 @@ fn main() {
window_resolution: WINDOW_SIZE,
},
SoundsPlugin,
AppPlugin,
))
.add_plugins(UiPlugin)
.run();
Expand Down
1 change: 1 addition & 0 deletions pixlib/src/plugins.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod app_plugin;
pub mod events_plugin;
pub mod graphics_plugin;
pub mod inputs_plugin;
Expand Down
54 changes: 54 additions & 0 deletions pixlib/src/plugins/app_plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use bevy::{
app::{App, Plugin, Update},
prelude::{in_state, EventReader, IntoSystemConfigs, OnEnter, Query, With},
window::{CursorGrabMode, CursorIcon, PrimaryWindow, Window},
};

use super::events_plugin::PixlibApplicationEvent;
use crate::AppState;

#[derive(Debug, Default)]
pub struct AppPlugin;

impl Plugin for AppPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
update_cursor.run_if(in_state(AppState::SceneViewer)),
)
.add_systems(OnEnter(AppState::SceneChooser), reset_cursor);
}
}

fn update_cursor(
mut reader: EventReader<PixlibApplicationEvent>,
mut windows: Query<&mut Window, With<PrimaryWindow>>,
) {
let mut window = windows.single_mut();
for evt in reader.read() {
match &evt.0 {
pixlib_parser::runner::ApplicationEvent::CursorLocked => {
window.cursor.grab_mode = CursorGrabMode::Locked
}
pixlib_parser::runner::ApplicationEvent::CursorFreed => {
window.cursor.grab_mode = CursorGrabMode::None
}
pixlib_parser::runner::ApplicationEvent::CursorHidden => window.cursor.visible = false,
pixlib_parser::runner::ApplicationEvent::CursorShown => window.cursor.visible = true,
pixlib_parser::runner::ApplicationEvent::CursorSetToPointer => {
window.cursor.icon = CursorIcon::Pointer
}
pixlib_parser::runner::ApplicationEvent::CursorSetToDefault => {
window.cursor.icon = CursorIcon::Default
}
_ => {}
};
}
}

fn reset_cursor(mut windows: Query<&mut Window, With<PrimaryWindow>>) {
let mut window = windows.single_mut();
window.cursor.grab_mode = CursorGrabMode::None;
window.cursor.visible = true;
window.cursor.icon = CursorIcon::Default;
}
6 changes: 5 additions & 1 deletion pixlib/src/plugins/events_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::RwLock;

use bevy::{
app::{App, Plugin, Update},
app::{App, AppExit, Plugin, Update},
prelude::{Event, EventWriter, Events, NonSend, ResMut},
};

Expand Down Expand Up @@ -79,8 +79,12 @@ pub struct PixlibApplicationEvent(pub ApplicationEvent);
fn redistribute_application_events(
runner: NonSend<ScriptRunner>,
mut writer: EventWriter<PixlibApplicationEvent>,
mut exit_writer: EventWriter<AppExit>,
) {
for evt in runner.events_out.app.borrow_mut().drain(..) {
if matches!(&evt, ApplicationEvent::ApplicationExited) {
exit_writer.send(AppExit);
}
writer.send(PixlibApplicationEvent(evt));
}
}
Expand Down
23 changes: 23 additions & 0 deletions pixlib_parser/src/runner/classes/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ impl Animation {
self.state.borrow().get_frame_size(context)
}

pub fn get_frame_rect(&self) -> anyhow::Result<Rect> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
self.state
.borrow_mut()
.use_and_drop_mut(|s| s.load_if_needed(context.clone()))?;
self.state.borrow().get_frame_rect(context)
}

pub fn get_center_frame_position(&self) -> anyhow::Result<(isize, isize)> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
self.state
Expand Down Expand Up @@ -1548,6 +1556,21 @@ impl AnimationState {
Ok((sprite.0.size_px.0 as usize, sprite.0.size_px.1 as usize))
}

pub fn get_frame_rect(&self, context: RunnerContext) -> anyhow::Result<Rect> {
let (_, frame, sprite) = self.get_sprite_data(context)?;
let position = (
self.position.0 + frame.offset_px.0 as isize + sprite.0.offset_px.0 as isize,
self.position.1 + frame.offset_px.1 as isize + sprite.0.offset_px.1 as isize,
);
let size = (sprite.0.size_px.0 as isize, sprite.0.size_px.1 as isize);
Ok(Rect {
top_left_x: position.0,
top_left_y: position.1,
bottom_right_x: position.0 + size.0,
bottom_right_y: position.1 + size.1,
})
}

pub fn get_center_frame_position(
&self,
context: RunnerContext,
Expand Down
153 changes: 103 additions & 50 deletions pixlib_parser/src/runner/classes/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{any::Any, cell::RefCell};
use super::super::content::EventHandler;
use super::super::initable::Initable;
use super::super::parsers::{
discard_if_empty, parse_bool, parse_event_handler, parse_i32, parse_rect, Rect,
discard_if_empty, parse_bool, parse_event_handler, parse_i32, parse_rect, ReferenceRect,
};

use crate::{common::DroppableRefMut, parser::ast::ParsedScript, runner::InternalEvent};
Expand All @@ -23,7 +23,7 @@ pub struct ButtonProperties {
pub gfx_on_move: Option<ImageName>, // GFXONMOVE
pub gfx_standard: Option<ImageName>, // GFXSTANDARD
pub priority: Option<i32>, // PRIORITY
pub rect: Option<Rect>, // RECT
pub rect: Option<ReferenceRect>, // RECT
pub snd_on_click: Option<SoundName>, // SNDONCLICK
pub snd_on_move: Option<SoundName>, // SNDONMOVE
pub snd_standard: Option<SoundName>, // SNDSTANDARD
Expand Down Expand Up @@ -61,7 +61,7 @@ pub struct ButtonState {
pub graphics_on_hover: Option<String>,
pub graphics_on_click: Option<String>,
pub priority: isize,
pub rect: Option<Rect>,
pub rect: Option<ReferenceRect>,

pub current_interaction: Interaction,
}
Expand Down Expand Up @@ -156,8 +156,17 @@ impl Button {

// custom

pub fn is_displaying(&self, object_name: &str) -> anyhow::Result<bool> {
self.state.borrow().is_displaying(object_name)
pub fn is_enabled(&self) -> anyhow::Result<bool> {
Ok(self.state.borrow().is_enabled)
}

pub fn get_rect(&self) -> anyhow::Result<Option<Rect>> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
self.state.borrow().get_rect(context)
}

pub fn get_priority(&self) -> anyhow::Result<isize> {
self.state.borrow().get_priority()
}

pub fn set_normal(&self) -> anyhow::Result<()> {
Expand Down Expand Up @@ -244,35 +253,53 @@ impl CnvType for Button {
.borrow_mut()
.enable_dragging()
.map(|_| CnvValue::Null),
CallableIdentifier::Method("GETONCLICK") => self.state.borrow().get_on_click(),
CallableIdentifier::Method("GETONMOVE") => self.state.borrow().get_on_move(),
CallableIdentifier::Method("GETONCLICK") => self
.state
.borrow()
.get_on_click()
.map(|v| v.map(CnvValue::String).unwrap_or_default()),
CallableIdentifier::Method("GETONMOVE") => self
.state
.borrow()
.get_on_move()
.map(|v| v.map(CnvValue::String).unwrap_or_default()),
CallableIdentifier::Method("GETPRIORITY") => self
.state
.borrow()
.get_priority()
.map(|v| CnvValue::Integer(v as i32)),
CallableIdentifier::Method("GETSTD") => self.state.borrow().get_std(),
CallableIdentifier::Method("GETSTD") => self
.state
.borrow()
.get_std()
.map(|v| v.map(CnvValue::String).unwrap_or_default()),
CallableIdentifier::Method("SETONCLICK") => self
.state
.borrow_mut()
.set_on_click()
.set_on_click(&arguments[0].to_string())
.map(|_| CnvValue::Null),
CallableIdentifier::Method("SETONMOVE") => self
.state
.borrow_mut()
.set_on_move()
.set_on_move(&arguments[0].to_string())
.map(|_| CnvValue::Null),
CallableIdentifier::Method("SETPRIORITY") => self
.state
.borrow_mut()
.set_priority()
.set_priority(arguments[0].to_int() as isize)
.map(|_| CnvValue::Null),
CallableIdentifier::Method("SETRECT") => {
self.state.borrow_mut().set_rect().map(|_| CnvValue::Null)
}
CallableIdentifier::Method("SETSTD") => {
self.state.borrow_mut().set_std().map(|_| CnvValue::Null)
let rect = parse_rect(arguments[0].to_str())?;
self.state
.borrow_mut()
.set_rect(rect)
.map(|_| CnvValue::Null)
}
CallableIdentifier::Method("SETSTD") => self
.state
.borrow_mut()
.set_std(&arguments[0].to_string())
.map(|_| CnvValue::Null),
CallableIdentifier::Method("SYN") => {
self.state.borrow_mut().syn().map(|_| CnvValue::Null)
}
Expand Down Expand Up @@ -489,49 +516,54 @@ impl ButtonState {
todo!()
}

pub fn get_on_click(&self) -> anyhow::Result<CnvValue> {
pub fn get_on_click(&self) -> anyhow::Result<Option<String>> {
// GETONCLICK
todo!()
Ok(self.graphics_on_click.clone())
}

pub fn get_on_move(&self) -> anyhow::Result<CnvValue> {
pub fn get_on_move(&self) -> anyhow::Result<Option<String>> {
// GETONMOVE
todo!()
Ok(self.graphics_on_hover.clone())
}

pub fn get_priority(&self) -> anyhow::Result<isize> {
// GETPRIORITY
todo!()
Ok(self.priority)
}

pub fn get_std(&self) -> anyhow::Result<CnvValue> {
pub fn get_std(&self) -> anyhow::Result<Option<String>> {
// GETSTD
todo!()
Ok(self.graphics_normal.clone())
}

pub fn set_on_click(&mut self) -> anyhow::Result<()> {
pub fn set_on_click(&mut self, object_name: &str) -> anyhow::Result<()> {
// SETONCLICK
todo!()
self.graphics_on_click = Some(object_name.to_owned());
Ok(())
}

pub fn set_on_move(&mut self) -> anyhow::Result<()> {
pub fn set_on_move(&mut self, object_name: &str) -> anyhow::Result<()> {
// SETONMOVE
todo!()
self.graphics_on_hover = Some(object_name.to_owned());
Ok(())
}

pub fn set_priority(&mut self) -> anyhow::Result<()> {
pub fn set_priority(&mut self, priority: isize) -> anyhow::Result<()> {
// SETPRIORITY
todo!()
self.priority = priority;
Ok(())
}

pub fn set_rect(&mut self) -> anyhow::Result<()> {
pub fn set_rect(&mut self, rect: ReferenceRect) -> anyhow::Result<()> {
// SETRECT
todo!()
self.rect = Some(rect);
Ok(())
}

pub fn set_std(&mut self) -> anyhow::Result<()> {
pub fn set_std(&mut self, object_name: &str) -> anyhow::Result<()> {
// SETSTD
todo!()
self.graphics_normal = Some(object_name.to_owned());
Ok(())
}

pub fn syn(&mut self) -> anyhow::Result<()> {
Expand All @@ -541,6 +573,45 @@ impl ButtonState {

// custom

pub fn get_rect(&self, context: RunnerContext) -> anyhow::Result<Option<Rect>> {
if let Some(reference_rect) = &self.rect {
match reference_rect {
ReferenceRect::Literal(rect) => Ok(Some(*rect)),
ReferenceRect::Reference(reference) => {
let object = context.runner.get_object(reference).ok_or(
RunnerError::ObjectNotFound {
name: reference.clone(),
},
)?;
if let CnvContent::Animation(animation) = &object.content {
animation.get_frame_rect().map(Some)
} else if let CnvContent::Image(image) = &object.content {
image.get_rect().map(Some)
} else {
Err(RunnerError::ExpectedGraphicsObject.into())
}
}
}
} else if let Some(graphics_normal) = &self.graphics_normal {
let object =
context
.runner
.get_object(graphics_normal)
.ok_or(RunnerError::ObjectNotFound {
name: graphics_normal.clone(),
})?;
if let CnvContent::Animation(animation) = &object.content {
animation.get_frame_rect().map(Some)
} else if let CnvContent::Image(image) = &object.content {
image.get_rect().map(Some)
} else {
Err(RunnerError::ExpectedGraphicsObject.into())
}
} else {
Ok(None)
}
}

fn set_interaction(
&mut self,
context: RunnerContext,
Expand Down Expand Up @@ -769,22 +840,4 @@ impl ButtonState {
}
self.try_set_interaction(context, Interaction::Hovering)
}

pub fn is_displaying(&self, object_name: &str) -> anyhow::Result<bool> {
Ok(match self.current_interaction {
Interaction::Hidden => false,
Interaction::None => self
.graphics_normal
.as_ref()
.is_some_and(|n| n == object_name),
Interaction::Hovering => self
.graphics_on_hover
.as_ref()
.is_some_and(|n| n == object_name),
Interaction::Pressing => self
.graphics_on_click
.as_ref()
.is_some_and(|n| n == object_name),
})
}
}
Loading

0 comments on commit 93d8db9

Please sign in to comment.