add turtle.reset

This commit is contained in:
2025-10-18 08:28:17 +02:00
parent 7e23dc9d9c
commit 16430f3958
6 changed files with 223 additions and 124 deletions
+35
View File
@@ -635,6 +635,41 @@ impl TurtlePlan {
self
}
/// Resets the turtle to its default state.
///
/// This clears all drawings, clears the animation queue, and resets all turtle parameters:
/// - Position: (0, 0)
/// - Heading: 0° (facing right)
/// - Pen: down
/// - Pen width: 2.0
/// - Pen color: black
/// - Fill color: none
/// - Visibility: visible
/// - Shape: arrow
/// - Speed: default
///
/// # Examples
///
/// ```no_run
/// # use turtle_lib::*;
/// #
/// #[turtle_main("Reset Example")]
/// fn draw(turtle: &mut TurtlePlan) {
/// // Draw something
/// turtle.forward(100.0);
///
/// // Reset everything back to default
/// turtle.reset();
///
/// // Start fresh
/// turtle.forward(50.0);
/// }
/// ```
pub fn reset(&mut self) -> &mut Self {
self.queue.push(TurtleCommand::Reset);
self
}
/// Consumes the `TurtlePlan` and returns the command queue.
///
/// Use this to finalize the turtle commands and pass them to `TurtleApp`.
+3
View File
@@ -42,6 +42,9 @@ pub enum TurtleCommand {
// Fill operations
BeginFill,
EndFill,
// Reset
Reset,
}
/// Queue of turtle commands with execution state
+22 -4
View File
@@ -30,7 +30,6 @@ pub fn execute_command_side_effects(command: &TurtleCommand, state: &mut Turtle)
state.begin_fill(fill_color);
true
}
TurtleCommand::EndFill => {
if let Some(mut fill_state) = state.filling.take() {
if !fill_state.current_contour.is_empty() {
@@ -79,7 +78,6 @@ pub fn execute_command_side_effects(command: &TurtleCommand, state: &mut Turtle)
}
true
}
TurtleCommand::PenUp => {
state.params.pen_down = false;
if state.filling.is_some() {
@@ -91,7 +89,6 @@ pub fn execute_command_side_effects(command: &TurtleCommand, state: &mut Turtle)
state.close_fill_contour();
true
}
TurtleCommand::PenDown => {
state.params.pen_down = true;
if state.filling.is_some() {
@@ -106,7 +103,23 @@ pub fn execute_command_side_effects(command: &TurtleCommand, state: &mut Turtle)
true
}
_ => false, // Not a side-effect-only command
TurtleCommand::Reset => {
state.reset();
true
}
TurtleCommand::Move(_)
| TurtleCommand::Turn(_)
| TurtleCommand::Circle { .. }
| TurtleCommand::Goto(_)
| TurtleCommand::SetColor(_)
| TurtleCommand::SetFillColor(_)
| TurtleCommand::SetPenWidth(_)
| TurtleCommand::SetSpeed(_)
| TurtleCommand::SetShape(_)
| TurtleCommand::SetHeading(_)
| TurtleCommand::ShowTurtle
| TurtleCommand::HideTurtle => false,
}
}
@@ -248,6 +261,11 @@ pub fn execute_command(command: &TurtleCommand, state: &mut Turtle) {
TurtleCommand::ShowTurtle => state.params.visible = true,
TurtleCommand::HideTurtle => state.params.visible = false,
// Reset
TurtleCommand::Reset => {
state.reset();
}
_ => {} // Already handled by execute_command_side_effects
}
+11 -4
View File
@@ -92,11 +92,18 @@ impl Turtle {
Angle::radians(self.params.heading)
}
/// Reset turtle to default state (preserves turtle_id)
/// Reset turtle to default state (preserves turtle_id and queued commands)
pub fn reset(&mut self) {
let id = self.turtle_id;
*self = Self::default();
self.turtle_id = id;
// Clear all drawings
self.commands.clear();
// Clear fill state
self.filling = None;
// Reset parameters to defaults
self.params = TurtleParams::default();
// Keep turtle_id and tween_controller (preserves queued commands)
}
/// Start recording fill vertices
+4
View File
@@ -436,6 +436,10 @@ impl TweenController {
// Fill commands don't change turtle state for tweening purposes
// They're handled directly in execution
}
TurtleCommand::Reset => {
// Reset returns to default state
target = TurtleParams::default();
}
}
target