Mercurial > pygame
annotate examples/scroll.py @ 3194:9fcb7abf3185 default tip
Remove some gcc 4.7.2 compiler warnings
| author | Lenard Lindstrom <len-l@telus.net> |
|---|---|
| date | Tue, 23 Apr 2013 19:53:36 -0700 |
| parents | 5f2fa62e5aec |
| children |
| rev | line source |
|---|---|
|
1620
5f2fa62e5aec
remove svn:executable tags and add setmods script to set executable permissions
Lenard Lindstrom <len-l@telus.net>
parents:
1565
diff
changeset
|
1 #!/usr/bin/env python |
|
5f2fa62e5aec
remove svn:executable tags and add setmods script to set executable permissions
Lenard Lindstrom <len-l@telus.net>
parents:
1565
diff
changeset
|
2 |
| 1525 | 3 """An zoomed image viewer that demonstrates Surface.scroll |
| 4 | |
| 5 This example shows a scrollable image that has a zoom factor of eight. | |
| 6 It uses the Surface.scroll function to shift the image on the display | |
| 7 surface. A clip rectangle protects a margin area. If called as a function, | |
| 8 the example accepts an optional image file path. If run as a program | |
| 9 it takes an optional file path command line argument. If no file | |
| 10 is provided a default image file is used. | |
| 11 | |
| 12 When running click on a black triangle to move one pixel in the direction | |
| 13 the triangle points. Or use the arrow keys. Close the window or press ESC | |
| 14 to quit. | |
| 15 | |
| 16 """ | |
| 17 | |
| 18 import sys | |
| 19 import os | |
| 20 | |
| 21 import pygame | |
| 22 from pygame.transform import scale | |
| 23 from pygame.locals import * | |
| 24 | |
| 25 main_dir = os.path.dirname(os.path.abspath(__file__)) | |
| 26 | |
| 27 DIR_UP = 1 | |
| 28 DIR_DOWN = 2 | |
| 29 DIR_LEFT = 3 | |
| 30 DIR_RIGHT = 4 | |
| 31 | |
| 32 zoom_factor = 8 | |
| 33 | |
| 34 def draw_arrow(surf, color, posn, direction): | |
| 35 x, y = posn | |
| 36 if direction == DIR_UP: | |
| 37 pointlist = ((x - 29, y + 30), (x + 30, y + 30), | |
| 38 (x + 1, y - 29), (x, y - 29)) | |
| 39 elif direction == DIR_DOWN: | |
| 40 pointlist = ((x - 29, y - 29), (x + 30, y - 29), | |
| 41 (x + 1, y + 30), (x, y + 30)) | |
| 42 elif direction == DIR_LEFT: | |
| 43 pointlist = ((x + 30, y - 29), (x + 30, y + 30), | |
| 44 (x - 29, y + 1), (x - 29, y)) | |
| 45 else: | |
| 46 pointlist = ((x - 29, y - 29), (x - 29, y + 30), | |
| 47 (x + 30, y + 1), (x + 30, y)) | |
| 48 pygame.draw.polygon(surf, color, pointlist) | |
| 49 | |
| 50 def add_arrow_button(screen, regions, posn, direction): | |
| 51 draw_arrow(screen, Color('black'), posn, direction) | |
| 52 draw_arrow(regions, (direction, 0, 0), posn, direction) | |
| 53 | |
| 54 def scroll_view(screen, image, direction, view_rect): | |
| 55 dx = dy = 0 | |
| 56 src_rect = None | |
| 57 zoom_view_rect = screen.get_clip() | |
|
1531
6a9da59b2f44
fix bug that let the example scroll past the bottom
Lenard Lindstrom <len-l@telus.net>
parents:
1527
diff
changeset
|
58 image_w, image_h = image.get_size() |
| 1525 | 59 if direction == DIR_UP: |
| 60 if view_rect.top > 0: | |
| 61 screen.scroll(dy=zoom_factor) | |
| 62 view_rect.move_ip(0, -1) | |
| 63 src_rect = view_rect.copy() | |
| 64 src_rect.h = 1 | |
| 65 dst_rect = zoom_view_rect.copy() | |
| 66 dst_rect.h = zoom_factor | |
| 67 elif direction == DIR_DOWN: | |
| 68 if view_rect.bottom < image_h: | |
| 69 screen.scroll(dy=-zoom_factor) | |
| 70 view_rect.move_ip(0, 1) | |
| 71 src_rect = view_rect.copy() | |
| 72 src_rect.h = 1 | |
| 73 src_rect.bottom = view_rect.bottom | |
| 74 dst_rect = zoom_view_rect.copy() | |
| 75 dst_rect.h = zoom_factor | |
| 76 dst_rect.bottom = zoom_view_rect.bottom | |
| 77 elif direction == DIR_LEFT: | |
| 78 if view_rect.left > 0: | |
| 79 screen.scroll(dx=zoom_factor) | |
| 80 view_rect.move_ip(-1, 0) | |
| 81 src_rect = view_rect.copy() | |
| 82 src_rect.w = 1 | |
| 83 dst_rect = zoom_view_rect.copy() | |
| 84 dst_rect.w = zoom_factor | |
| 85 elif direction == DIR_RIGHT: | |
| 86 if view_rect.right < image_w: | |
| 87 screen.scroll(dx=-zoom_factor) | |
| 88 view_rect.move_ip(1, 0) | |
| 89 src_rect = view_rect.copy() | |
| 90 src_rect.w = 1 | |
| 91 src_rect.right = view_rect.right | |
| 92 dst_rect = zoom_view_rect.copy() | |
| 93 dst_rect.w = zoom_factor | |
| 94 dst_rect.right = zoom_view_rect.right | |
| 95 if src_rect is not None: | |
| 96 scale(image.subsurface(src_rect), | |
| 97 dst_rect.size, | |
| 98 screen.subsurface(dst_rect)) | |
| 99 pygame.display.update(zoom_view_rect) | |
| 100 | |
| 101 def main(image_file=None): | |
| 102 if image_file is None: | |
| 103 image_file = os.path.join(main_dir, 'data', 'arraydemo.bmp') | |
| 104 margin = 80 | |
| 105 view_size = (30, 20) | |
| 106 zoom_view_size = (view_size[0] * zoom_factor, | |
| 107 view_size[1] * zoom_factor) | |
| 108 win_size = (zoom_view_size[0] + 2 * margin, | |
| 109 zoom_view_size[1] + 2 * margin) | |
| 110 background_color = Color('beige') | |
| 111 | |
| 112 pygame.init() | |
|
1527
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
113 |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
114 # set up key repeating so we can hold down the key to scroll. |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
115 old_k_delay, old_k_interval = pygame.key.get_repeat () |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
116 pygame.key.set_repeat (500, 30) |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
117 |
| 1525 | 118 try: |
| 119 screen = pygame.display.set_mode(win_size) | |
| 120 screen.fill(background_color) | |
| 121 pygame.display.flip() | |
|
1527
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
122 |
| 1525 | 123 image = pygame.image.load(image_file).convert() |
| 124 image_w, image_h = image.get_size() | |
| 125 | |
| 126 if image_w < view_size[0] or image_h < view_size[1]: | |
|
1565
162f965b7dd2
add scroll.py example to Python 3.1 adapted programs
Lenard Lindstrom <len-l@telus.net>
parents:
1531
diff
changeset
|
127 print ("The source image is too small for this example.") |
|
162f965b7dd2
add scroll.py example to Python 3.1 adapted programs
Lenard Lindstrom <len-l@telus.net>
parents:
1531
diff
changeset
|
128 print ("A %i by %i or larger image is required." % zoom_view_size) |
| 1525 | 129 return |
| 130 | |
| 131 regions = pygame.Surface(win_size, 0, 24) | |
| 132 add_arrow_button(screen, regions, | |
| 133 (40, win_size[1] // 2), DIR_LEFT) | |
| 134 add_arrow_button(screen, regions, | |
| 135 (win_size[0] - 40, win_size[1] // 2), DIR_RIGHT) | |
| 136 add_arrow_button(screen, regions, | |
| 137 (win_size[0] // 2, 40), DIR_UP) | |
| 138 add_arrow_button(screen, regions, | |
| 139 (win_size[0] // 2, win_size[1] - 40), DIR_DOWN) | |
| 140 pygame.display.flip() | |
| 141 | |
| 142 screen.set_clip((margin, margin, zoom_view_size[0], zoom_view_size[1])) | |
| 143 | |
| 144 view_rect = Rect(0, 0, view_size[0], view_size[1]) | |
|
1527
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
145 |
| 1525 | 146 scale(image.subsurface(view_rect), zoom_view_size, |
| 147 screen.subsurface(screen.get_clip())) | |
| 148 pygame.display.flip() | |
| 149 | |
|
1527
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
150 |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
151 # the direction we will scroll in. |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
152 direction = None |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
153 |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
154 clock = pygame.time.Clock() |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
155 clock.tick() |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
156 |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
157 going = True |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
158 while going: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
159 # wait for events before doing anything. |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
160 #events = [pygame.event.wait()] + pygame.event.get() |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
161 events = pygame.event.get() |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
162 |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
163 for e in events: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
164 if e.type == KEYDOWN: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
165 if e.key == K_ESCAPE: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
166 going = False |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
167 elif e.key == K_DOWN: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
168 scroll_view(screen, image, DIR_DOWN, view_rect) |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
169 elif e.key == K_UP: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
170 scroll_view(screen, image, DIR_UP, view_rect) |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
171 elif e.key == K_LEFT: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
172 scroll_view(screen, image, DIR_LEFT, view_rect) |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
173 elif e.key == K_RIGHT: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
174 scroll_view(screen, image, DIR_RIGHT, view_rect) |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
175 elif e.type == QUIT: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
176 going = False |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
177 elif e.type == MOUSEBUTTONDOWN: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
178 direction = regions.get_at(e.pos)[0] |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
179 elif e.type == MOUSEBUTTONUP: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
180 direction = None |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
181 |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
182 if direction: |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
183 scroll_view(screen, image, direction, view_rect) |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
184 clock.tick(30) |
|
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
185 |
| 1525 | 186 finally: |
|
1527
8cd16c38baf0
Allowed holding down the mouse, or key to scroll.
Rene Dudfield <renesd@gmail.com>
parents:
1525
diff
changeset
|
187 pygame.key.set_repeat (old_k_delay, old_k_interval) |
| 1525 | 188 pygame.quit() |
| 189 | |
| 190 if __name__ == '__main__': | |
| 191 if len(sys.argv) > 1: | |
| 192 image_file = sys.argv[1] | |
| 193 else: | |
| 194 image_file = None | |
| 195 main(image_file) |
