diff options
author | Griffin Smith <root@gws.fyi> | 2019-07-14T20·31-0400 |
---|---|---|
committer | Griffin Smith <root@gws.fyi> | 2019-07-14T20·31-0400 |
commit | bc93999cf37a65d48f25e30795c85a0aef97efac (patch) | |
tree | 0333a4917d95058009fd272e3834f2b36f4ab14b /src | |
parent | 575a051e6efcd8fd3b0a146f49040e543ae8e5b0 (diff) |
Always reset the cursor back to the character
much nicer!
Diffstat (limited to 'src')
-rw-r--r-- | src/display/viewport.rs | 19 | ||||
-rw-r--r-- | src/game.rs | 1 |
2 files changed, 16 insertions, 4 deletions
diff --git a/src/display/viewport.rs b/src/display/viewport.rs index 780eb887143d..b510b0504c58 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<W> { /// 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<W> Viewport<W> { pub fn new(outer: BoundingBox, inner: BoundingBox, out: W) -> Self { @@ -31,6 +34,7 @@ impl<W> Viewport<W> { inner, out, game: outer.move_tr_corner(Position { x: 0, y: 1 }), + cursor_position: pos(0, 0), } } @@ -63,7 +67,12 @@ impl<W: Write> Viewport<W> { 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<W: Write> Viewport<W> { /// 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<W: Write> Viewport<W> { " ".to_string(), self.outer.dimensions.w - msg.len() as u16 ), - ) + )?; + self.reset_cursor() } } diff --git a/src/game.rs b/src/game.rs index 57c04cfb883f..48142b637645 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(), |