Skip to content

Commit

Permalink
Extract general traits for conditional-like objects and graphics-like…
Browse files Browse the repository at this point in the history
… objects

Fix AnimationState::is_playing() and ButtonState::enable
  • Loading branch information
Dove6 committed Aug 25, 2024
1 parent 92a19ea commit ce43bc2
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 86 deletions.
20 changes: 11 additions & 9 deletions pixlib_parser/src/runner/classes/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,6 @@ impl Animation {
Ok(Some((frame.clone(), sprite.0.clone(), sprite.1.clone())))
}

pub fn hide(&self) -> anyhow::Result<()> {
self.state.borrow_mut().hide()
}

pub fn show(&self) -> anyhow::Result<()> {
self.state.borrow_mut().show()
}

pub fn play(&self, sequence_name: &str) -> anyhow::Result<()> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
self.state.borrow_mut().play(context, sequence_name)
Expand Down Expand Up @@ -343,6 +335,16 @@ impl Animation {
}
}

impl GeneralGraphics for Animation {
fn hide(&self) -> anyhow::Result<()> {
self.state.borrow_mut().hide()
}

fn show(&self) -> anyhow::Result<()> {
self.state.borrow_mut().show()
}
}

impl CnvType for Animation {
fn as_any(&self) -> &dyn Any {
self
Expand Down Expand Up @@ -1172,7 +1174,7 @@ impl AnimationState {

pub fn is_playing(&self) -> anyhow::Result<bool> {
// ISPLAYING BOOL
Ok(self.is_playing)
Ok(self.is_playing && !self.is_paused)
}

pub fn is_visible(&self) -> anyhow::Result<bool> {
Expand Down
17 changes: 7 additions & 10 deletions pixlib_parser/src/runner/classes/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,13 @@ impl BehaviorState {
// RUNC
if let Some(condition) = condition_name {
let condition_object = context.runner.get_object(condition).unwrap();
if let CnvContent::Condition(ref condition) = &condition_object.content {
if !condition.check()? {
return Ok(CnvValue::Null);
}
} else if let CnvContent::ComplexCondition(ref condition) = &condition_object.content {
if !condition.check()? {
return Ok(CnvValue::Null);
}
} else {
return Err(RunnerError::ExpectedConditionObject.into());
let condition: &dyn GeneralCondition = match &condition_object.content {
CnvContent::Condition(c) => c,
CnvContent::ComplexCondition(c) => c,
_ => return Err(RunnerError::ExpectedConditionObject.into()),
};
if !condition.check()? {
return Ok(CnvValue::Null);
}
}
self.run(context, code, arguments)
Expand Down
72 changes: 27 additions & 45 deletions pixlib_parser/src/runner/classes/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ impl ButtonState {

pub fn enable(&mut self, context: RunnerContext) -> anyhow::Result<()> {
// ENABLE
if self.is_enabled {
return Ok(());
}
self.is_enabled = true;
self.set_interaction(context, Interaction::None)
}
Expand Down Expand Up @@ -640,22 +643,15 @@ impl ButtonState {
.as_ref()
.and_then(|name| context.runner.get_object(name))
{
if let CnvContent::Image(ref normal_image) = &normal_obj.content {
if interaction == Interaction::None {
normal_image.show()
} else {
normal_image.hide()
}
} else if let CnvContent::Animation(ref normal_animation) = &normal_obj.content {
if interaction == Interaction::None {
normal_animation.show()?;
normal_animation.resume()
} else {
normal_animation.pause()?;
normal_animation.hide()
}
let normal_graphics: &dyn GeneralGraphics = match &normal_obj.content {
CnvContent::Animation(a) => a,
CnvContent::Image(i) => i,
_ => return Err(RunnerError::ExpectedGraphicsObject.into()),
};
if interaction == Interaction::None {
normal_graphics.show()
} else {
Err(RunnerError::ExpectedGraphicsObject.into())
normal_graphics.hide()
}?
} /*else {
println!(
Expand All @@ -682,22 +678,15 @@ impl ButtonState {
.as_ref()
.and_then(|name| context.runner.get_object(name))
{
if let CnvContent::Image(ref on_hover_image) = &on_hover_obj.content {
if interaction == Interaction::Hovering {
on_hover_image.show()
} else {
on_hover_image.hide()
}
} else if let CnvContent::Animation(ref on_hover_animation) = &on_hover_obj.content {
if interaction == Interaction::Hovering {
on_hover_animation.show()?;
on_hover_animation.resume()
} else {
on_hover_animation.pause()?;
on_hover_animation.hide()
}
let on_hover_graphics: &dyn GeneralGraphics = match &on_hover_obj.content {
CnvContent::Animation(a) => a,
CnvContent::Image(i) => i,
_ => return Err(RunnerError::ExpectedGraphicsObject.into()),
};
if interaction == Interaction::Hovering {
on_hover_graphics.show()
} else {
Err(RunnerError::ExpectedGraphicsObject.into())
on_hover_graphics.hide()
}?
} /*else {
println!(
Expand All @@ -724,22 +713,15 @@ impl ButtonState {
.as_ref()
.and_then(|name| context.runner.get_object(name))
{
if let CnvContent::Image(ref on_click_image) = &on_click_obj.content {
if interaction == Interaction::Pressing {
on_click_image.show()
} else {
on_click_image.hide()
}
} else if let CnvContent::Animation(ref on_click_animation) = &on_click_obj.content {
if interaction == Interaction::Pressing {
on_click_animation.show()?;
on_click_animation.resume()
} else {
on_click_animation.pause()?;
on_click_animation.hide()
}
let on_click_graphics: &dyn GeneralGraphics = match &on_click_obj.content {
CnvContent::Animation(a) => a,
CnvContent::Image(i) => i,
_ => return Err(RunnerError::ExpectedGraphicsObject.into()),
};
if interaction == Interaction::Pressing {
on_click_graphics.show()
} else {
Err(RunnerError::ExpectedGraphicsObject.into())
on_click_graphics.hide()
}?
} /*else {
println!(
Expand Down
17 changes: 11 additions & 6 deletions pixlib_parser/src/runner/classes/complexcondition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ impl ComplexCondition {
right: props.operand2,
}
}
}

pub fn check(&self) -> anyhow::Result<bool> {
impl GeneralCondition for ComplexCondition {
fn check(&self) -> anyhow::Result<bool> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
self.state.borrow().check(context)
}
Expand Down Expand Up @@ -171,18 +173,21 @@ impl ComplexConditionState {
}

pub fn check(&self, context: RunnerContext) -> anyhow::Result<bool> {
// TODO: allow for complexconditions built on other complexconditions
let CnvContent::ComplexCondition(ref complex_condition) = &context.current_object.content
else {
panic!();
};
let left_object = context.runner.get_object(&complex_condition.left).unwrap();
let CnvContent::Condition(ref left) = &left_object.content else {
panic!();
let left: &dyn GeneralCondition = match &left_object.content {
CnvContent::Condition(c) => c,
CnvContent::ComplexCondition(c) => c,
_ => return Err(RunnerError::ExpectedConditionObject.into()),
};
let right_object = context.runner.get_object(&complex_condition.right).unwrap();
let CnvContent::Condition(ref right) = &right_object.content else {
panic!();
let right: &dyn GeneralCondition = match &right_object.content {
CnvContent::Condition(c) => c,
CnvContent::ComplexCondition(c) => c,
_ => return Err(RunnerError::ExpectedConditionObject.into()),
};
let result = match complex_condition.operator {
ComplexConditionOperator::And => {
Expand Down
4 changes: 3 additions & 1 deletion pixlib_parser/src/runner/classes/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ impl Condition {
right: props.operand2,
}
}
}

pub fn check(&self) -> anyhow::Result<bool> {
impl GeneralCondition for Condition {
fn check(&self) -> anyhow::Result<bool> {
let context = RunnerContext::new_minimal(&self.parent.parent.runner, &self.parent);
self.state.borrow().check(context)
}
Expand Down
6 changes: 4 additions & 2 deletions pixlib_parser/src/runner/classes/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ impl Image {
let image = &loaded_data.image;
Ok(Some((image.0.clone(), image.1.clone())))
}
}

pub fn hide(&self) -> anyhow::Result<()> {
impl GeneralGraphics for Image {
fn hide(&self) -> anyhow::Result<()> {
self.state.borrow_mut().hide()
}

pub fn show(&self) -> anyhow::Result<()> {
fn show(&self) -> anyhow::Result<()> {
self.state.borrow_mut().show()
}
}
Expand Down
9 changes: 9 additions & 0 deletions pixlib_parser/src/runner/classes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ impl CnvTypeFactory {
}
}

pub trait GeneralCondition {
fn check(&self) -> anyhow::Result<bool>;
}

pub trait GeneralGraphics {
fn show(&self) -> anyhow::Result<()>;
fn hide(&self) -> anyhow::Result<()>;
}

pub type EpisodeName = String;
pub type SceneName = String;
pub type ConditionName = String;
Expand Down
13 changes: 8 additions & 5 deletions pixlib_parser/src/runner/classes/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ struct SequenceState {

// deduced from methods
pub is_paused: bool,
pub is_visible: bool,
pub music_frequency: usize,
pub music_volume: f32,
pub music_pan: f32,
Expand Down Expand Up @@ -96,7 +95,6 @@ impl Sequence {
let sequence = Self {
parent,
state: RefCell::new(SequenceState {
is_visible: true,
music_volume: 1f32,
..Default::default()
}),
Expand Down Expand Up @@ -356,7 +354,13 @@ impl SequenceState {
pub fn hide(&mut self) -> anyhow::Result<()> {
// HIDE
todo!()
for animation_obj in self.animation_mapping.values() {
let CnvContent::Animation(animation) = &animation_obj.content else {
unreachable!()
};
animation.hide()?;
}
Ok(())
}
pub fn is_playing(&self) -> anyhow::Result<bool> {
Expand Down Expand Up @@ -412,8 +416,7 @@ impl SequenceState {
pub fn show(&mut self) -> anyhow::Result<()> {
// SHOW
self.is_visible = true;
todo!() // make visible
todo!()
}
pub fn stop(&mut self, context: RunnerContext, emit_on_finished: bool) -> anyhow::Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions pixlib_parser/src/runner/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl Display for CallableIdentifierOwned {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("CallableIdentifierOwned::")?;
match self {
CallableIdentifierOwned::Method(name) => f.write_fmt(format_args!("::Method({name})")),
CallableIdentifierOwned::Event(name) => f.write_fmt(format_args!("::Event({name})")),
CallableIdentifierOwned::Method(name) => f.write_fmt(format_args!("Method({name})")),
CallableIdentifierOwned::Event(name) => f.write_fmt(format_args!("Event({name})")),
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions pixlib_parser/src/runner/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ impl CnvObject {
} else {
arguments.to_owned()
};
// println!(
// "Calling method: {:?} of: {:?} with arguments: {:?}",
// identifier, self.name, arguments
// );
self.content.call_method(identifier, &arguments, context)
// println!("Result is {:?}", result);

self.content
.call_method(identifier.clone(), &arguments, context)
// .inspect(|v| {
// println!(
// "Called method: {:?} of: {:?} with arguments: {:?} and result: {:?}",
// identifier, self.name, arguments, v
// )
// })
}

pub fn init(self: &Arc<Self>, context: Option<RunnerContext>) -> anyhow::Result<()> {
Expand Down

0 comments on commit ce43bc2

Please sign in to comment.