about summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2024-12-16 02:45:37 -0600
committergennyble <gen@nyble.dev>2024-12-16 02:45:37 -0600
commitc4d2c86ea3388db76fd5fb36152d14c34902c47b (patch)
tree5841501838f966ce51c4959bb443c2adc8e3c8dc /src/main.rs
parentde63314d720a89fbccb03445a207f01812882eeb (diff)
downloadreally-etches-main.tar.gz
really-etches-main.zip
Fix crashing when we go to low HEAD main
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 59d004f..dc3989a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -132,6 +132,20 @@ impl Etch {
 			gif.save(path).unwrap();
 		}
 	}
+
+	pub fn keep_stylus_inbounds(&mut self) {
+		if self.stylus.x < 0.0 {
+			self.stylus.x = 0.0;
+		} else if self.stylus.x > self.img.width() as f32 - 1.0 {
+			self.stylus.x = self.img.width() as f32 - 1.0;
+		}
+
+		if self.stylus.y < 0.0 {
+			self.stylus.y = 0.0
+		} else if self.stylus.y > self.img.height() as f32 - 1.0 {
+			self.stylus.y = self.img.height() as f32 - 1.0;
+		}
+	}
 }
 
 // Why are my consts HERE of all places
@@ -240,10 +254,8 @@ impl ApplicationHandler for Etch {
 
 					let movement_x = left_delta / DIAL_SENSITIVITY;
 					let movement_y = right_delta / DIAL_SENSITIVITY;
-					self.stylus.x =
-						(self.stylus.x + movement_x).clamp(0.0, self.img.width() as f32);
-					self.stylus.y =
-						(self.stylus.y - movement_y).clamp(0.0, self.img.height() as f32);
+					self.stylus.x = self.stylus.x + movement_x;
+					self.stylus.y = self.stylus.y - movement_y;
 
 					self.next_check = Instant::now();
 				}
@@ -280,6 +292,8 @@ impl ApplicationHandler for Etch {
 					}
 				}
 
+				self.keep_stylus_inbounds();
+
 				// If the stylus moved, we should draw
 				if stylus_prev != self.stylus {
 					self.img.line(