From bc93999cf37a65d48f25e30795c85a0aef97efac Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 14 Jul 2019 16:31:36 -0400 Subject: Always reset the cursor back to the character much nicer! --- src/display/viewport.rs | 19 +++++++++++++++---- src/game.rs | 1 + 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/display/viewport.rs b/src/display/viewport.rs index 780eb88714..b510b0504c 100644 --- a/src/display/viewport.rs +++ b/src/display/viewport.rs @@ -2,7 +2,7 @@ use super::BoxStyle; use super::Draw; use crate::display::draw_box::draw_box; use crate::display::utils::clone_times; -use crate::types::{BoundingBox, Position, Positioned}; +use crate::types::{pos, BoundingBox, Position, Positioned}; use std::fmt::{self, Debug}; use std::io::{self, Write}; @@ -23,6 +23,9 @@ pub struct Viewport { /// The actual screen that the viewport writes to pub out: W, + + /// Reset the cursor back to this position after every draw + pub cursor_position: Position, } impl Viewport { pub fn new(outer: BoundingBox, inner: BoundingBox, out: W) -> Self { @@ -31,6 +34,7 @@ impl Viewport { inner, out, game: outer.move_tr_corner(Position { x: 0, y: 1 }), + cursor_position: pos(0, 0), } } @@ -63,7 +67,12 @@ impl Viewport { return Ok(()); } self.cursor_goto(entity.position())?; - entity.do_draw(self) + entity.do_draw(self)?; + self.reset_cursor() + } + + fn reset_cursor(&mut self) -> io::Result<()> { + self.cursor_goto(self.cursor_position) } /// Move the cursor to the given inner-relative position @@ -74,7 +83,8 @@ impl Viewport { /// Clear whatever single character is drawn at the given inner-relative /// position, if visible pub fn clear(&mut self, pos: Position) -> io::Result<()> { - write!(self, "{} ", self.on_screen(pos).cursor_goto(),) + write!(self, "{} ", self.on_screen(pos).cursor_goto(),)?; + self.reset_cursor() } /// Initialize this viewport by drawing its outer box to the screen @@ -101,7 +111,8 @@ impl Viewport { " ".to_string(), self.outer.dimensions.w - msg.len() as u16 ), - ) + )?; + self.reset_cursor() } } diff --git a/src/game.rs b/src/game.rs index 57c04cfb88..48142b6376 100644 --- a/src/game.rs +++ b/src/game.rs @@ -242,6 +242,7 @@ impl<'a> Game<'a> { (old_pos - self.character().position).as_tiles(), )); self.viewport.clear(old_pos)?; + self.viewport.cursor_position = self.character().position; self.viewport.draw( // TODO this clone feels unnecessary. &self.character().clone(), -- cgit 1.4.1