Skip to content

Commit

Permalink
Added following point
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzetan committed Jul 15, 2021
1 parent cefcd3d commit c226e19
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ But this way is not recommended because it only works when we are blitting gener

So I recommend you to always use first method, that way pygameZoom knows where it's canvas is located, and it allows you to blit generated image at any coordinates on the screen.

# Disabling zooming and dragging

You can disable zooming and dragging at any given time.

```python
#Zooming
self.pygameZoom.allow_zooming(bool)
#Dragging
self.pygameZoom.allow_dragging(bool)
```

Where bool is either True or False.

When False user can't use corresponding feature

## Let's take a look on how to draw shapes with pygameZoom.

To draw any of shapes listed bellow ad corresponding code to draw shapes section in refresh_window.
Expand Down Expand Up @@ -249,6 +264,22 @@ Zooming without quality loss work only with shapes.

--------------------------

# Following a point

pygameZoom has a feature to follow any given point on canvas, it's useful for example when you want to zoom into
moving object in your game.

```python
self.pygameZoom.follow_point(x,y,zoom)
```
Where (x,y) are coordinates of followed point.

zoom - zoom while following

I recommend you to disable zooming and dragging while using this feature, because it gets buggy when user tries to zoom or drag while this feature is on

--------------------------

Great se we finished writing our class. Don't forget to make an instance of Window class.

```python
Expand Down
15 changes: 9 additions & 6 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def __init__(self):
self.run = True
self.pygameZoom = PygameZoom(500, 400)
self.pygameZoom.set_background((255, 0, 0))
self.x = 0
self.y = 0
self.loop()

def drawTree(self, a, b, pos, deepness):
Expand All @@ -27,13 +29,14 @@ def drawTree(self, a, b, pos, deepness):
def refresh_window(self):
self.WIN.fill(0)
# self.drawTree(500, 800, -90, 12)
# self.pygameZoom.draw_ellipse((255, 255, 0), (200, 200, 100, 100))
# self.pygameZoom.draw_circle((255, 255, 0), 200, 200, 20)
# self.pygameZoom.draw_rect((0, 0, 0), 100, 100, 100, 100)
# self.pygameZoom.draw_line((255, 255, 255), 0, 0, 200, 200)
self.pygameZoom.draw_ellipse((255, 255, 0), (200, 200, 100, 100))
self.pygameZoom.draw_circle((255, 255, 0), 200, 200, 20)
self.pygameZoom.draw_rect((0, 0, 0), 100, 100, 100, 100)
self.pygameZoom.draw_line((255, 255, 255), 0, 0, 200, 200)
# self.pygameZoom.draw_polygon((0,0,255), [(200,400),(300,2),(400,400)])
surface = pygame.image.load("download.jpeg")
self.pygameZoom.blit(surface, (100, 100))
# surface = pygame.image.load("download.jpeg")
# self.pygameZoom.blit(surface, (100, 100))

self.pygameZoom.render(self.WIN, (100, 100))
pygame.display.update()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='pygameZoom',
version='0.0.3',
version='0.0.4',
description='Zoom into pygame figures without quality loss',
long_description=description,
long_description_content_type="text/markdown",
Expand Down
47 changes: 34 additions & 13 deletions src/pygameZoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class PygameZoom(object):
last_point = None
position_in_window = (0, 0)
background = 0
zoomingDisabled = False
draggingDisabled = False

def __init__(self, W, H) -> None:
self.WIDTH = W
Expand Down Expand Up @@ -142,6 +144,24 @@ def render(self, window, pos):
self.position_in_window = pos
window.blit(self.generate_surface(), pos)

def follow_point(self, x, y, zoom):
self.zoom = zoom
scale_x = self.WIDTH / zoom
scale_y = self.HEIGHT / zoom

self.boundaries[0] = x - scale_x / 2
self.boundaries[1] = x + scale_x / 2
self.boundaries[2] = y - scale_y / 2
self.boundaries[3] = y + scale_y / 2

self.correct_boundaries()

def allow_zooming(self, bool):
self.zoomingDisabled = not bool

def allow_dragging(self, bool):
self.draggingDisabled = not bool

def get_mouse_pos(self):
mouse_pos = pygame.mouse.get_pos()
if self.position_in_window[0] <= mouse_pos[0] <= self.position_in_window[0] + self.WIDTH and \
Expand Down Expand Up @@ -198,7 +218,7 @@ def update_boundaries(self, operation):

def update_zoom(self):
# If mouse is not on image, exit
if self.get_mouse_pos() is False:
if self.get_mouse_pos() is False or self.zoomingDisabled:
self.last_point = None
return

Expand All @@ -222,18 +242,19 @@ def update_zoom(self):
self.last_point = mouse_pos
continue

offset_x = ((self.last_point[0] - mouse_pos[0]) / self.WIDTH) * (
self.boundaries[1] - self.boundaries[0])
offset_y = ((self.last_point[1] - mouse_pos[1]) / self.HEIGHT) * (
self.boundaries[3] - self.boundaries[2])
if not self.draggingDisabled:
offset_x = ((self.last_point[0] - mouse_pos[0]) / self.WIDTH) * (
self.boundaries[1] - self.boundaries[0])
offset_y = ((self.last_point[1] - mouse_pos[1]) / self.HEIGHT) * (
self.boundaries[3] - self.boundaries[2])

if self.boundaries[0] >= 0 and self.boundaries[1] <= self.WIDTH:
self.boundaries[0] += offset_x
self.boundaries[1] += offset_x
if self.boundaries[0] >= 0 and self.boundaries[1] <= self.WIDTH:
self.boundaries[0] += offset_x
self.boundaries[1] += offset_x

if self.boundaries[2] >= 0 and self.boundaries[3] <= self.HEIGHT:
self.boundaries[2] += offset_y
self.boundaries[3] += offset_y
if self.boundaries[2] >= 0 and self.boundaries[3] <= self.HEIGHT:
self.boundaries[2] += offset_y
self.boundaries[3] += offset_y

self.correct_boundaries()
self.last_point = mouse_pos
self.correct_boundaries()
self.last_point = mouse_pos

0 comments on commit c226e19

Please sign in to comment.