pub struct DialogEngine<P: TextProvider> {
pub provider: P,
pub stream: Option<TextStream<P::Char>>,
pub state: DialogState,
}Expand description
A general-purpose dialog engine that drives text display one character per frame.
P is the TextProvider implementation that supplies the game’s
character encoding and rendering logic.
§Usage
let provider = MyProvider::new();
let mut engine = DialogEngine::new(provider);
let mut buffer = TileBuffer::new(20, 18);
engine.open_dialog(&[0x48, 0x45, 0x4C, 0x4C, 0x4F]); // "HELLO"
while engine.is_active() {
engine.update(&mut buffer); // one char per frame
}
engine.advance(); // close dialogFields§
§provider: PThe text provider (charmap, rendering, control codes).
stream: Option<TextStream<P::Char>>Active character stream, or None if no dialog is open.
state: DialogStateCurrent dialog state.
Implementations§
Source§impl<P: TextProvider> DialogEngine<P>
impl<P: TextProvider> DialogEngine<P>
Sourcepub fn new(provider: P) -> Self
pub fn new(provider: P) -> Self
Creates a new dialog engine with the given text provider.
Examples found in repository?
460fn demo_dialog() {
461 println!("\n╔══ NPC DIALOG ═══════════════════════════╗");
462 let provider = HelloConfig::new();
463 let mut engine = DialogEngine::new(provider);
464 let mut buffer = TileBuffer::new(20, 18);
465
466 // "Greetings, young hero!" + DONE
467 let text: &[u8] = b"Greetings, young hero!";
468 let mut full = text.to_vec();
469 full.push(0xFF); // DONE
470
471 engine.open_dialog(&full);
472 while engine.is_active() { engine.update(&mut buffer); }
473
474 print!("║ Merlin: \"");
475 for i in 0..20usize {
476 let t = buffer.tiles[i].tile_id;
477 if (0x20..=0x7E).contains(&t) { print!("{}", t as u8 as char); }
478 }
479 println!("\"");
480 println!("╚══════════════════════════════════════════╝");
481}Sourcepub fn open_dialog(&mut self, text: &[u8])
pub fn open_dialog(&mut self, text: &[u8])
Opens a dialog with the given raw byte text.
The bytes are decoded via the provider’s TextProvider::decode_stream
and the state is reset to the initial typing mode.
Examples found in repository?
460fn demo_dialog() {
461 println!("\n╔══ NPC DIALOG ═══════════════════════════╗");
462 let provider = HelloConfig::new();
463 let mut engine = DialogEngine::new(provider);
464 let mut buffer = TileBuffer::new(20, 18);
465
466 // "Greetings, young hero!" + DONE
467 let text: &[u8] = b"Greetings, young hero!";
468 let mut full = text.to_vec();
469 full.push(0xFF); // DONE
470
471 engine.open_dialog(&full);
472 while engine.is_active() { engine.update(&mut buffer); }
473
474 print!("║ Merlin: \"");
475 for i in 0..20usize {
476 let t = buffer.tiles[i].tile_id;
477 if (0x20..=0x7E).contains(&t) { print!("{}", t as u8 as char); }
478 }
479 println!("\"");
480 println!("╚══════════════════════════════════════════╝");
481}Sourcepub fn update(&mut self, buffer: &mut TileBuffer)
pub fn update(&mut self, buffer: &mut TileBuffer)
Processes one character from the current dialog stream.
Call this once per frame to achieve the classic typewriter effect.
Control codes are dispatched to the provider’s
TextProvider::process_control; printable characters are drawn
via TextProvider::render_char.
If the engine is not in DialogMode::Typing, this call is a no-op.
If the stream is exhausted or a Done control action is returned,
the dialog state transitions to DialogMode::Done.
Examples found in repository?
460fn demo_dialog() {
461 println!("\n╔══ NPC DIALOG ═══════════════════════════╗");
462 let provider = HelloConfig::new();
463 let mut engine = DialogEngine::new(provider);
464 let mut buffer = TileBuffer::new(20, 18);
465
466 // "Greetings, young hero!" + DONE
467 let text: &[u8] = b"Greetings, young hero!";
468 let mut full = text.to_vec();
469 full.push(0xFF); // DONE
470
471 engine.open_dialog(&full);
472 while engine.is_active() { engine.update(&mut buffer); }
473
474 print!("║ Merlin: \"");
475 for i in 0..20usize {
476 let t = buffer.tiles[i].tile_id;
477 if (0x20..=0x7E).contains(&t) { print!("{}", t as u8 as char); }
478 }
479 println!("\"");
480 println!("╚══════════════════════════════════════════╝");
481}Sourcepub fn advance(&mut self)
pub fn advance(&mut self)
Advances the dialog past the current page or closes it.
- If paused, resumes typing.
- If waiting for input or scrolling, resumes typing.
- If the dialog is done, clears the stream and resets state
(so [
is_active] returnsfalse).
Call this in response to the player pressing the A button.
Sourcepub fn is_active(&self) -> bool
pub fn is_active(&self) -> bool
Returns true if a dialog is currently active.
A dialog is active when a stream is loaded and the mode is not
DialogMode::Done.
Examples found in repository?
460fn demo_dialog() {
461 println!("\n╔══ NPC DIALOG ═══════════════════════════╗");
462 let provider = HelloConfig::new();
463 let mut engine = DialogEngine::new(provider);
464 let mut buffer = TileBuffer::new(20, 18);
465
466 // "Greetings, young hero!" + DONE
467 let text: &[u8] = b"Greetings, young hero!";
468 let mut full = text.to_vec();
469 full.push(0xFF); // DONE
470
471 engine.open_dialog(&full);
472 while engine.is_active() { engine.update(&mut buffer); }
473
474 print!("║ Merlin: \"");
475 for i in 0..20usize {
476 let t = buffer.tiles[i].tile_id;
477 if (0x20..=0x7E).contains(&t) { print!("{}", t as u8 as char); }
478 }
479 println!("\"");
480 println!("╚══════════════════════════════════════════╝");
481}