-
Notifications
You must be signed in to change notification settings - Fork 3
14 zoom in/out and move the camera #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
938e36a
11b8ee0
6cdfcdd
b63db1d
dd71dba
b83ffc1
4f0c757
c7d1bb8
c5cf2ba
17dee82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import pygame | ||
| import sys | ||
|
|
||
| class Viewpoint: | ||
| """ | ||
| A class to manage the viewpoint for rendering a 2D surface with zoom and pan capabilities. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| size=None, | ||
| position=None, | ||
| zoom=1, | ||
| ): | ||
|
|
||
|
|
||
| if size is None: | ||
| size = (1410, 1000) | ||
| self.paper = pygame.Surface(size) | ||
|
|
||
|
|
||
| self.screen = pygame.display.set_mode((1280, 720)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made the screen an instance variable of the viewpoint. That way, a lot of things happen inside the viewpoint solely. |
||
|
|
||
| if position is None: | ||
| position = (0, 0) | ||
|
|
||
| self._view = pygame.Vector3(position[0], position[1], zoom) | ||
|
|
||
| def check_user_input(self): | ||
|
|
||
| if pygame.key.get_pressed()[pygame.K_PLUS]: | ||
| self._view.z += 0.1 | ||
|
|
||
| if pygame.key.get_pressed()[pygame.K_MINUS]: | ||
| self._view.z -=0.1 | ||
|
|
||
| if pygame.key.get_pressed()[pygame.K_UP]: | ||
| self._view.y += 10 | ||
|
|
||
| if pygame.key.get_pressed()[pygame.K_DOWN]: | ||
| self._view.y -= 10 | ||
|
|
||
| if pygame.key.get_pressed()[pygame.K_LEFT]: | ||
| self._view.x += 10 | ||
|
|
||
| if pygame.key.get_pressed()[pygame.K_RIGHT]: | ||
| self._view.x -= 10 | ||
|
|
||
| for event in pygame.event.get(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HSMarieK: I moved that piece to here as well - I think we could collect all sorts of user input here - what do you think? |
||
| if event.type == pygame.QUIT: | ||
| self.teardown() | ||
|
|
||
| self._view.z = max(self._view.z, 0.1) | ||
| self._view.z = min(self._view.z, 5) | ||
|
|
||
| def clear(self): | ||
| self.screen.fill('white') | ||
| self.paper.fill('white') | ||
|
|
||
| def _draw(self): | ||
| self.screen.blit( | ||
| pygame.transform.smoothscale_by(self.paper, self._view.z), | ||
| (self._view.x,self._view.y), | ||
| ) | ||
|
|
||
| def teardown(self): | ||
| pygame.quit() | ||
| sys.exit() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved more stuff into the
Viewpointclass. Maybe - after the refactoring - all pygame specific things can happen insideViewpoint?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't put everything in the Viewpoint class. But the visualization.py is supposed to contain every pygame specific thing in the future.