Skip to content

Commit

Permalink
Merge pull request #189 from stargazing-dino/dialogue_clone
Browse files Browse the repository at this point in the history
Make dialogue clone
  • Loading branch information
janhohenheim authored May 17, 2024
2 parents 3ba3cb8 + c16e8dd commit 2536241
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl TextProvider for SharedTextProvider {
}

impl UnderlyingTextProvider for SharedTextProvider {
fn clone_shallow(&self) -> Box<dyn UnderlyingTextProvider> {
Box::new(self.clone())
}

fn accept_line_hints(&mut self, line_ids: &[LineId]) {
self.0.write().unwrap().accept_line_hints(line_ids)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl Debug for StringsFileTextProvider {
}

impl UnderlyingTextProvider for StringsFileTextProvider {
fn clone_shallow(&self) -> Box<dyn UnderlyingTextProvider> {
Box::new(self.clone())
}

fn accept_line_hints(&mut self, _line_ids: &[LineId]) {
// no-op
}
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/src/dialogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use yarnspinner_core::prelude::*;
/// Co-ordinates the execution of Yarn programs.
///
/// The main functions of interest are [`Dialogue::continue_`] and [`Dialogue::set_selected_option`].
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Dialogue {
vm: VirtualMachine,
language_code: Option<Language>,
Expand Down
14 changes: 14 additions & 0 deletions crates/runtime/src/text_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use yarnspinner_core::prelude::*;
///
/// By injecting this, we don't need to expose `Dialogue.ExpandSubstitutions` and `Dialogue.ParseMarkup`, since we can apply them internally.
pub trait TextProvider: Debug + Send + Sync {
/// Creates a shallow clone of this text provider, i.e. a clone that
/// shares the same underlying provider and will thus be perfectly in sync
/// with the original instance.
fn clone_shallow(&self) -> Box<dyn TextProvider>;
/// Passes the [`LineId`]s that this [`TextProvider`] should soon provide text for. These are the [`LineId`]s that are contained in the current node and are not required to be actually reached.
fn accept_line_hints(&mut self, line_ids: &[LineId]);
/// Returns the text for the given [`LineId`]. Will only be called if [`TextProvider::are_lines_available`] returns `true`.
Expand All @@ -31,6 +35,12 @@ pub trait TextProvider: Debug + Send + Sync {
fn as_any_mut(&mut self) -> &mut dyn Any;
}

impl Clone for Box<dyn TextProvider> {
fn clone(&self) -> Self {
self.clone_shallow()
}
}

#[allow(missing_docs)]
pub type StringTable = HashMap<LineId, String>;

Expand Down Expand Up @@ -73,6 +83,10 @@ impl StringTableTextProvider {
}

impl TextProvider for StringTableTextProvider {
fn clone_shallow(&self) -> Box<dyn TextProvider> {
Box::new(self.clone())
}

fn accept_line_hints(&mut self, _line_ids: &[LineId]) {
// no-op
}
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/src/virtual_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use yarnspinner_core::prelude::*;
mod execution_state;
mod state;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct VirtualMachine {
pub(crate) library: Library,
pub(crate) program: Option<Program>,
Expand Down
4 changes: 4 additions & 0 deletions crates/yarnspinner/tests/test_base/text_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl SharedTextProvider {
}

impl TextProvider for SharedTextProvider {
fn clone_shallow(&self) -> Box<dyn TextProvider> {
Box::new(self.clone())
}

fn accept_line_hints(&mut self, line_ids: &[LineId]) {
self.0.write().unwrap().accept_line_hints(line_ids);
}
Expand Down

0 comments on commit 2536241

Please sign in to comment.