Skip to content

Commit b4675ab

Browse files
committed
Little bit of cursor optimization
1 parent c5173f4 commit b4675ab

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

src/rendering/cursor.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@ use embedded_graphics::{fonts::Font, geometry::Point, primitives::Rectangle};
77
/// [`TextBox`]: ../../struct.TextBox.html
88
#[derive(Copy, Clone, Debug)]
99
pub struct Cursor<F: Font> {
10-
_marker: PhantomData<F>,
11-
1210
/// Current cursor position
1311
pub position: Point,
1412

15-
left: i32,
16-
right: i32,
17-
bottom: i32,
18-
top: i32,
13+
_marker: PhantomData<F>,
14+
bounds: Rectangle,
1915
}
2016

2117
impl<F: Font> Cursor<F> {
@@ -26,18 +22,18 @@ impl<F: Font> Cursor<F> {
2622
Self {
2723
_marker: PhantomData,
2824
position: bounds.top_left,
29-
bottom: bounds.bottom_right.y + 1,
30-
top: bounds.top_left.y,
31-
left: bounds.top_left.x,
32-
right: bounds.bottom_right.x + 1,
25+
bounds: Rectangle::new(
26+
bounds.top_left,
27+
bounds.bottom_right + Point::new(1, 1 - F::CHARACTER_SIZE.height as i32),
28+
),
3329
}
3430
}
3531

3632
/// Returns the width of the textbox
3733
#[inline]
3834
#[must_use]
3935
pub fn line_width(&self) -> u32 {
40-
(self.right - self.left) as u32
36+
(self.bounds.bottom_right.x - self.bounds.top_left.x) as u32
4137
}
4238

4339
/// Starts a new line.
@@ -49,17 +45,19 @@ impl<F: Font> Cursor<F> {
4945
/// Moves the cursor back to the start of the line.
5046
#[inline]
5147
pub fn carriage_return(&mut self) {
52-
self.position.x = self.left;
48+
self.position.x = self.bounds.top_left.x;
5349
}
5450

55-
/// Returns whether the cursor is in the bounding box.
51+
/// Returns whether the cursor is completely in the bounding box.
52+
///
53+
/// Completely means, that the line that is marked by the cursor can be drawn without any
54+
/// vertical clipping or drawing outside the bounds.
5655
///
5756
/// *Note:* Only vertical overrun is checked.
5857
#[inline]
5958
#[must_use]
6059
pub fn in_display_area(&self) -> bool {
61-
self.position.y >= self.top
62-
&& (self.position.y + F::CHARACTER_SIZE.height as i32) <= self.bottom
60+
self.bounds.top_left.y <= self.position.y && self.position.y <= self.bounds.bottom_right.y
6361
}
6462

6563
/// Returns whether the current line has enough space to also include an object of given width.
@@ -73,7 +71,7 @@ impl<F: Font> Cursor<F> {
7371
#[inline]
7472
#[must_use]
7573
pub fn space(&self) -> u32 {
76-
(self.right - self.position.x) as u32
74+
(self.bounds.bottom_right.x - self.position.x) as u32
7775
}
7876

7977
/// Advances the cursor by a given amount.

0 commit comments

Comments
 (0)