From c226e1933c4e3d6cc9d2d3e310fb416d37130004 Mon Sep 17 00:00:00 2001 From: Grzetan Date: Thu, 15 Jul 2021 12:23:21 +0200 Subject: [PATCH] Added following point --- README.md | 31 +++++++++++++++++++++++++++++++ example.py | 15 +++++++++------ setup.py | 2 +- src/pygameZoom.py | 47 ++++++++++++++++++++++++++++++++++------------- 4 files changed, 75 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 33a857d..23d1992 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/example.py b/example.py index b10b277..43c1adf 100644 --- a/example.py +++ b/example.py @@ -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): @@ -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() diff --git a/setup.py b/setup.py index cd6abf4..43d1c1f 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/src/pygameZoom.py b/src/pygameZoom.py index 89b196c..edff7d7 100644 --- a/src/pygameZoom.py +++ b/src/pygameZoom.py @@ -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 @@ -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 \ @@ -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 @@ -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