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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
3 """An zoomed image viewer that demonstrates Surface.scroll
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
4
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
5 This example shows a scrollable image that has a zoom factor of eight.
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
6 It uses the Surface.scroll function to shift the image on the display
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
7 surface. A clip rectangle protects a margin area. If called as a function,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
8 the example accepts an optional image file path. If run as a program
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
9 it takes an optional file path command line argument. If no file
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
10 is provided a default image file is used.
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
11
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
12 When running click on a black triangle to move one pixel in the direction
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
13 the triangle points. Or use the arrow keys. Close the window or press ESC
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
14 to quit.
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
15
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
16 """
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
17
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
18 import sys
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
19 import os
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
20
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
21 import pygame
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
22 from pygame.transform import scale
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
23 from pygame.locals import *
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
24
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
25 main_dir = os.path.dirname(os.path.abspath(__file__))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
26
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
27 DIR_UP = 1
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
28 DIR_DOWN = 2
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
29 DIR_LEFT = 3
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
30 DIR_RIGHT = 4
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
31
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
32 zoom_factor = 8
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
33
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
34 def draw_arrow(surf, color, posn, direction):
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
35 x, y = posn
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
36 if direction == DIR_UP:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
37 pointlist = ((x - 29, y + 30), (x + 30, y + 30),
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
38 (x + 1, y - 29), (x, y - 29))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
39 elif direction == DIR_DOWN:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
40 pointlist = ((x - 29, y - 29), (x + 30, y - 29),
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
41 (x + 1, y + 30), (x, y + 30))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
42 elif direction == DIR_LEFT:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
43 pointlist = ((x + 30, y - 29), (x + 30, y + 30),
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
44 (x - 29, y + 1), (x - 29, y))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
45 else:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
46 pointlist = ((x - 29, y - 29), (x - 29, y + 30),
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
47 (x + 30, y + 1), (x + 30, y))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
48 pygame.draw.polygon(surf, color, pointlist)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
49
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
50 def add_arrow_button(screen, regions, posn, direction):
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
51 draw_arrow(screen, Color('black'), posn, direction)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
52 draw_arrow(regions, (direction, 0, 0), posn, direction)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
53
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
54 def scroll_view(screen, image, direction, view_rect):
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
55 dx = dy = 0
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
56 src_rect = None
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
59 if direction == DIR_UP:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
60 if view_rect.top > 0:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
61 screen.scroll(dy=zoom_factor)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
62 view_rect.move_ip(0, -1)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
63 src_rect = view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
64 src_rect.h = 1
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
65 dst_rect = zoom_view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
66 dst_rect.h = zoom_factor
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
67 elif direction == DIR_DOWN:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
68 if view_rect.bottom < image_h:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
69 screen.scroll(dy=-zoom_factor)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
70 view_rect.move_ip(0, 1)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
71 src_rect = view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
72 src_rect.h = 1
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
73 src_rect.bottom = view_rect.bottom
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
74 dst_rect = zoom_view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
75 dst_rect.h = zoom_factor
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
76 dst_rect.bottom = zoom_view_rect.bottom
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
77 elif direction == DIR_LEFT:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
78 if view_rect.left > 0:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
79 screen.scroll(dx=zoom_factor)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
80 view_rect.move_ip(-1, 0)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
81 src_rect = view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
82 src_rect.w = 1
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
83 dst_rect = zoom_view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
84 dst_rect.w = zoom_factor
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
85 elif direction == DIR_RIGHT:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
86 if view_rect.right < image_w:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
87 screen.scroll(dx=-zoom_factor)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
88 view_rect.move_ip(1, 0)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
89 src_rect = view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
90 src_rect.w = 1
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
91 src_rect.right = view_rect.right
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
92 dst_rect = zoom_view_rect.copy()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
93 dst_rect.w = zoom_factor
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
94 dst_rect.right = zoom_view_rect.right
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
95 if src_rect is not None:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
96 scale(image.subsurface(src_rect),
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
97 dst_rect.size,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
98 screen.subsurface(dst_rect))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
99 pygame.display.update(zoom_view_rect)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
100
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
101 def main(image_file=None):
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
102 if image_file is None:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
103 image_file = os.path.join(main_dir, 'data', 'arraydemo.bmp')
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
104 margin = 80
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
105 view_size = (30, 20)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
106 zoom_view_size = (view_size[0] * zoom_factor,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
107 view_size[1] * zoom_factor)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
108 win_size = (zoom_view_size[0] + 2 * margin,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
109 zoom_view_size[1] + 2 * margin)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
110 background_color = Color('beige')
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
111
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
118 try:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
119 screen = pygame.display.set_mode(win_size)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
120 screen.fill(background_color)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
123 image = pygame.image.load(image_file).convert()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
124 image_w, image_h = image.get_size()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
125
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
129 return
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
130
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
131 regions = pygame.Surface(win_size, 0, 24)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
132 add_arrow_button(screen, regions,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
133 (40, win_size[1] // 2), DIR_LEFT)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
134 add_arrow_button(screen, regions,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
135 (win_size[0] - 40, win_size[1] // 2), DIR_RIGHT)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
136 add_arrow_button(screen, regions,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
137 (win_size[0] // 2, 40), DIR_UP)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
138 add_arrow_button(screen, regions,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
139 (win_size[0] // 2, win_size[1] - 40), DIR_DOWN)
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
140 pygame.display.flip()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
141
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
142 screen.set_clip((margin, margin, zoom_view_size[0], zoom_view_size[1]))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
143
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
146 scale(image.subsurface(view_rect), zoom_view_size,
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
147 screen.subsurface(screen.get_clip()))
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
148 pygame.display.flip()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
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
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
188 pygame.quit()
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
189
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
190 if __name__ == '__main__':
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
191 if len(sys.argv) > 1:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
192 image_file = sys.argv[1]
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
193 else:
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
194 image_file = None
48ab3db38416 add Surface.scroll method
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
195 main(image_file)