Skip to content

Commit 3b2e41a

Browse files
authored
Merge pull request #36 from HarryPeach/PS_29
Change cursor when not in canvas
2 parents 57086b1 + 30534f0 commit 3b2e41a

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

sand_game/__main__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ def open_filepicker(self, new: bool = False) -> str:
164164
defaultextension=".json",
165165
title="Export canvas")
166166
else:
167-
result = askopenfilename(initialfile="import", title="Import canvas")
167+
result = askopenfilename(
168+
initialfile="import", title="Import canvas")
168169

169170
return result
170171

@@ -301,13 +302,15 @@ def draw(self) -> None:
301302
particle = self.canvas_controller.get(x, y)
302303
if particle is not None:
303304
pyxel.pset(
304-
x + self.canvas_start_loc[0], y + self.canvas_start_loc[1],
305+
x + self.canvas_start_loc[0], y +
306+
self.canvas_start_loc[1],
305307
particle.color
306308
)
307309

308310
self.gui.draw()
309311
self.gui.handle_hover(pyxel.mouse_x, pyxel.mouse_y)
310-
draw_cursor(self.pen_size - 1, 7)
312+
draw_cursor(self.pen_size - 1, 7, self.canvas_width,
313+
self.canvas_height, self.canvas_start_loc)
311314

312315

313316
if __name__ == "__main__":

sand_game/draw_utils.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
from sand_game import canvas
12
import pyxel
3+
from typing import Tuple
24

35

4-
def draw_cursor(width: int, color: int) -> None:
6+
def draw_cursor(width: int, color: int, canvas_width: int, canvas_height: int,
7+
canvas_start_loc: Tuple[int, int]) -> None:
58
"""Draws the cursor
69
710
Args:
811
width (int): The width of the cursor
912
color (int): The color to draw
13+
canvas_width (int): The width of the canvas
14+
canvas_height (int): The height of the canvas
15+
canvas_start_loc (tuple[int, int]): The starting location of the canvas
1016
"""
11-
pyxel.circb(pyxel.mouse_x, pyxel.mouse_y, width, color)
17+
if ((pyxel.mouse_x < canvas_start_loc[0] - 1 or
18+
pyxel.mouse_x > canvas_start_loc[0] + canvas_width) or
19+
(pyxel.mouse_y < canvas_start_loc[1] - 1 or
20+
pyxel.mouse_y > canvas_start_loc[1] + canvas_height)):
21+
pyxel.mouse(True)
22+
else:
23+
pyxel.mouse(False)
24+
pyxel.circb(pyxel.mouse_x, pyxel.mouse_y, width, color)

0 commit comments

Comments
 (0)