Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 914e220f76fde8f3a9bb06546c7f7549d8891746

Move package files to sugargame/ package directory.
activity.py
(0 / 75)
  
1import os
2import gobject
3import gtk
4
5import sugar.activity.activity
6import sugar.graphics.style
7
8import pygame
9import event
10
11ACTIVITY = None
12
13def get_activity():
14 return ACTIVITY
15
16class PygameActivity(sugar.activity.activity.Activity):
17 def __init__(self, handle):
18 super(PygameActivity, self).__init__(handle)
19
20 self._socket = None
21 self._screen = None
22
23 # Fudge the toolbar size.
24 TOOLBAR_HEIGHT = 75
25 TAB_HEIGHT = 45
26 self.width = gtk.gdk.screen_width()
27 self.height = gtk.gdk.screen_height() - TOOLBAR_HEIGHT - TAB_HEIGHT
28
29 global ACTIVITY
30 ACTIVITY = self
31
32 def build_canvas( self ):
33 # Build the widget in which to embed Pygame.
34 self._socket = gtk.Socket()
35
36 eventbox = gtk.EventBox()
37 eventbox.set_flags(gtk.CAN_FOCUS)
38 eventbox.set_size_request(self.width, self.height)
39 eventbox.add(self._socket)
40 eventbox.show_all()
41
42 self.set_canvas(eventbox)
43
44 # Preinitialize Pygame so we can hook its methods.
45 os.environ['SDL_WINDOWID'] = str(self._socket.get_id())
46 pygame.init()
47
48 # Hook certain Pygame functions with GTK equivalents.
49 translator = event.Translator(self, eventbox)
50 translator.hook_pygame()
51
52 # Initialize the Pygame window.
53 self._screen = pygame.display.set_mode((self.width,self.height))
54
55 # Restore the default cursor.
56 self._socket.get_window().set_cursor(None)
57
58 def run_pygame(self, main_fn):
59 # Run the main loop after a short delay. The reason for the delay is that the
60 # Sugar activity is not properly created until after its constructor returns.
61 # If the Pygame main loop is called from the activity constructor, the
62 # constructor never returns and the activity freezes.
63 gobject.idle_add(self._run_pygame_cb, main_fn)
64
65 def _run_pygame_cb(self, main_fn):
66 # Run the Pygame main loop.
67 main_fn()
68 return False
69
70 def get_pygame_widget(self):
71 return self._socket
72
73 def get_pygame_screen(self):
74 return self._screen
75
event.py
(0 / 241)
  
1import gtk
2import gobject
3import pygame
4import pygame.event
5import logging
6
7class _MockEvent(object):
8 def __init__(self, keyval):
9 self.keyval = keyval
10
11class Translator(object):
12 key_trans = {
13 'Alt_L': pygame.K_LALT,
14 'Alt_R': pygame.K_RALT,
15 'Control_L': pygame.K_LCTRL,
16 'Control_R': pygame.K_RCTRL,
17 'Shift_L': pygame.K_LSHIFT,
18 'Shift_R': pygame.K_RSHIFT,
19 'Super_L': pygame.K_LSUPER,
20 'Super_R': pygame.K_RSUPER,
21 'KP_Page_Up' : pygame.K_KP9,
22 'KP_Page_Down' : pygame.K_KP3,
23 'KP_End' : pygame.K_KP1,
24 'KP_Home' : pygame.K_KP7,
25 'KP_Up' : pygame.K_KP8,
26 'KP_Down' : pygame.K_KP2,
27 'KP_Left' : pygame.K_KP4,
28 'KP_Right' : pygame.K_KP6,
29
30 }
31
32 mod_map = {
33 pygame.K_LALT: pygame.KMOD_LALT,
34 pygame.K_RALT: pygame.KMOD_RALT,
35 pygame.K_LCTRL: pygame.KMOD_LCTRL,
36 pygame.K_RCTRL: pygame.KMOD_RCTRL,
37 pygame.K_LSHIFT: pygame.KMOD_LSHIFT,
38 pygame.K_RSHIFT: pygame.KMOD_RSHIFT,
39 }
40
41 def __init__(self, mainwindow, inner_evb):
42 """Initialise the Translator with the windows to which to listen"""
43 self._mainwindow = mainwindow
44 self._inner_evb = inner_evb
45
46 # Enable events
47 self._mainwindow.set_events(
48 gtk.gdk.KEY_PRESS_MASK | \
49 gtk.gdk.KEY_RELEASE_MASK \
50 )
51
52 self._inner_evb.set_events(
53 gtk.gdk.POINTER_MOTION_MASK | \
54 gtk.gdk.POINTER_MOTION_HINT_MASK | \
55 gtk.gdk.BUTTON_MOTION_MASK | \
56 gtk.gdk.BUTTON_PRESS_MASK | \
57 gtk.gdk.BUTTON_RELEASE_MASK
58 )
59
60 self._mainwindow.set_flags(gtk.CAN_FOCUS)
61 self._inner_evb.set_flags(gtk.CAN_FOCUS)
62
63 # Callback functions to link the event systems
64 self._mainwindow.connect('unrealize', self._quit_cb)
65 self._inner_evb.connect('key_press_event', self._keydown_cb)
66 self._inner_evb.connect('key_release_event', self._keyup_cb)
67 self._inner_evb.connect('button_press_event', self._mousedown_cb)
68 self._inner_evb.connect('button_release_event', self._mouseup_cb)
69 self._inner_evb.connect('motion-notify-event', self._mousemove_cb)
70 self._inner_evb.connect('expose-event', self._expose_cb)
71 self._inner_evb.connect('configure-event', self._resize_cb)
72
73 # Internal data
74 self.__stopped = False
75 self.__keystate = [0] * 323
76 self.__button_state = [0,0,0]
77 self.__mouse_pos = (0,0)
78 self.__repeat = (None, None)
79 self.__held = set()
80 self.__held_time_left = {}
81 self.__held_last_time = {}
82 self.__tick_id = None
83
84 def hook_pygame(self):
85 pygame.key.get_pressed = self._get_pressed
86 pygame.key.set_repeat = self._set_repeat
87 pygame.mouse.get_pressed = self._get_mouse_pressed
88 pygame.mouse.get_pos = self._get_mouse_pos
89
90 def _expose_cb(self, event, widget):
91 pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE))
92 return True
93
94 def _resize_cb( self, activity, event ):
95 #evt = pygame.event.Event(eventwrap.pygame.VIDEORESIZE,
96 # size=(event.width,event.height), width=event.width, height=event.height))
97 #pygame.event.post(evt)
98 return False # continue processing
99
100 def _quit_cb(self, data=None):
101 self.__stopped = True
102 pygame.event.post(pygame.event.Event(pygame.QUIT))
103
104 def _keydown_cb(self, widget, event):
105 key = event.keyval
106 if key in self.__held:
107 return True
108 else:
109 if self.__repeat[0] is not None:
110 self.__held_last_time[key] = pygame.time.get_ticks()
111 self.__held_time_left[key] = self.__repeat[0]
112 self.__held.add(key)
113
114 return self._keyevent(widget, event, pygame.KEYDOWN)
115
116 def _keyup_cb(self, widget, event):
117 key = event.keyval
118 if self.__repeat[0] is not None:
119 if key in self.__held:
120 # This is possibly false if set_repeat() is called with a key held
121 del self.__held_time_left[key]
122 del self.__held_last_time[key]
123 self.__held.discard(key)
124
125 return self._keyevent(widget, event, pygame.KEYUP)
126
127 def _keymods(self):
128 mod = 0
129 for key_val, mod_val in self.mod_map.iteritems():
130 mod |= self.__keystate[key_val] and mod_val
131 return mod
132
133 def _keyevent(self, widget, event, type):
134 key = gtk.gdk.keyval_name(event.keyval)
135 if key is None:
136 # No idea what this key is.
137 return False
138
139 keycode = None
140 if key in self.key_trans:
141 keycode = self.key_trans[key]
142 elif hasattr(pygame, 'K_'+key.upper()):
143 keycode = getattr(pygame, 'K_'+key.upper())
144 elif hasattr(pygame, 'K_'+key.lower()):
145 keycode = getattr(pygame, 'K_'+key.lower())
146 elif key == 'XF86Start':
147 # view source request, specially handled...
148 self._mainwindow.view_source()
149 else:
150 print 'Key %s unrecognized' % key
151
152 if keycode is not None:
153 if type == pygame.KEYDOWN:
154 mod = self._keymods()
155 self.__keystate[keycode] = type == pygame.KEYDOWN
156 if type == pygame.KEYUP:
157 mod = self._keymods()
158 ukey = unichr(gtk.gdk.keyval_to_unicode(event.keyval))
159 if ukey == '\000':
160 ukey = ''
161 evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod)
162 self._post(evt)
163
164 return True
165
166 def _get_pressed(self):
167 return self.__keystate
168
169 def _get_mouse_pressed(self):
170 return self.__button_state
171
172 def _mousedown_cb(self, widget, event):
173 self.__button_state[event.button-1] = 1
174 return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN)
175
176 def _mouseup_cb(self, widget, event):
177 self.__button_state[event.button-1] = 0
178 return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP)
179
180 def _mouseevent(self, widget, event, type):
181 evt = pygame.event.Event(type, button=event.button, pos=(event.x, event.y))
182 self._post(evt)
183 return True
184
185 def _mousemove_cb(self, widget, event):
186 # From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/
187 # if this is a hint, then let's get all the necessary
188 # information, if not it's all we need.
189 if event.is_hint:
190 x, y, state = event.window.get_pointer()
191 else:
192 x = event.x
193 y = event.y
194 state = event.state
195
196 rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1])
197 self.__mouse_pos = (x, y)
198
199 self.__button_state = [
200 state & gtk.gdk.BUTTON1_MASK and 1 or 0,
201 state & gtk.gdk.BUTTON2_MASK and 1 or 0,
202 state & gtk.gdk.BUTTON3_MASK and 1 or 0,
203 ]
204
205 evt = pygame.event.Event(pygame.MOUSEMOTION,
206 pos=self.__mouse_pos, rel=rel, buttons=self.__button_state)
207 self._post(evt)
208 return True
209
210 def _tick_cb(self):
211 cur_time = pygame.time.get_ticks()
212 for key in self.__held:
213 delta = cur_time - self.__held_last_time[key]
214 self.__held_last_time[key] = cur_time
215
216 self.__held_time_left[key] -= delta
217 if self.__held_time_left[key] <= 0:
218 self.__held_time_left[key] = self.__repeat[1]
219 self._keyevent(None, _MockEvent(key), pygame.KEYDOWN)
220
221 return True
222
223 def _set_repeat(self, delay=None, interval=None):
224 if delay is not None and self.__repeat[0] is None:
225 self.__tick_id = gobject.timeout_add(10, self._tick_cb)
226 elif delay is None and self.__repeat[0] is not None:
227 gobject.source_remove(self.__tick_id)
228 self.__repeat = (delay, interval)
229
230 def _get_mouse_pos(self):
231 return self.__mouse_pos
232
233 def _post(self, evt):
234 try:
235 pygame.event.post(evt)
236 except pygame.error, e:
237 if str(e) == 'Event queue full':
238 print "Event queue full!"
239 pass
240 else:
241 raise e
  
1import os
2import gobject
3import gtk
4
5import sugar.activity.activity
6import sugar.graphics.style
7
8import pygame
9import event
10
11ACTIVITY = None
12
13def get_activity():
14 return ACTIVITY
15
16class PygameActivity(sugar.activity.activity.Activity):
17 def __init__(self, handle):
18 super(PygameActivity, self).__init__(handle)
19
20 self._socket = None
21 self._screen = None
22
23 # Fudge the toolbar size.
24 TOOLBAR_HEIGHT = 75
25 TAB_HEIGHT = 45
26 self.width = gtk.gdk.screen_width()
27 self.height = gtk.gdk.screen_height() - TOOLBAR_HEIGHT - TAB_HEIGHT
28
29 global ACTIVITY
30 ACTIVITY = self
31
32 def build_canvas( self ):
33 # Build the widget in which to embed Pygame.
34 self._socket = gtk.Socket()
35
36 eventbox = gtk.EventBox()
37 eventbox.set_flags(gtk.CAN_FOCUS)
38 eventbox.set_size_request(self.width, self.height)
39 eventbox.add(self._socket)
40 eventbox.show_all()
41
42 self.set_canvas(eventbox)
43
44 # Preinitialize Pygame so we can hook its methods.
45 os.environ['SDL_WINDOWID'] = str(self._socket.get_id())
46 pygame.init()
47
48 # Hook certain Pygame functions with GTK equivalents.
49 translator = event.Translator(self, eventbox)
50 translator.hook_pygame()
51
52 # Initialize the Pygame window.
53 self._screen = pygame.display.set_mode((self.width,self.height))
54
55 # Restore the default cursor.
56 self._socket.get_window().set_cursor(None)
57
58 def run_pygame(self, main_fn):
59 # Run the main loop after a short delay. The reason for the delay is that the
60 # Sugar activity is not properly created until after its constructor returns.
61 # If the Pygame main loop is called from the activity constructor, the
62 # constructor never returns and the activity freezes.
63 gobject.idle_add(self._run_pygame_cb, main_fn)
64
65 def _run_pygame_cb(self, main_fn):
66 # Run the Pygame main loop.
67 main_fn()
68 return False
69
70 def get_pygame_widget(self):
71 return self._socket
72
73 def get_pygame_screen(self):
74 return self._screen
75
  
1import gtk
2import gobject
3import pygame
4import pygame.event
5import logging
6
7class _MockEvent(object):
8 def __init__(self, keyval):
9 self.keyval = keyval
10
11class Translator(object):
12 key_trans = {
13 'Alt_L': pygame.K_LALT,
14 'Alt_R': pygame.K_RALT,
15 'Control_L': pygame.K_LCTRL,
16 'Control_R': pygame.K_RCTRL,
17 'Shift_L': pygame.K_LSHIFT,
18 'Shift_R': pygame.K_RSHIFT,
19 'Super_L': pygame.K_LSUPER,
20 'Super_R': pygame.K_RSUPER,
21 'KP_Page_Up' : pygame.K_KP9,
22 'KP_Page_Down' : pygame.K_KP3,
23 'KP_End' : pygame.K_KP1,
24 'KP_Home' : pygame.K_KP7,
25 'KP_Up' : pygame.K_KP8,
26 'KP_Down' : pygame.K_KP2,
27 'KP_Left' : pygame.K_KP4,
28 'KP_Right' : pygame.K_KP6,
29
30 }
31
32 mod_map = {
33 pygame.K_LALT: pygame.KMOD_LALT,
34 pygame.K_RALT: pygame.KMOD_RALT,
35 pygame.K_LCTRL: pygame.KMOD_LCTRL,
36 pygame.K_RCTRL: pygame.KMOD_RCTRL,
37 pygame.K_LSHIFT: pygame.KMOD_LSHIFT,
38 pygame.K_RSHIFT: pygame.KMOD_RSHIFT,
39 }
40
41 def __init__(self, mainwindow, inner_evb):
42 """Initialise the Translator with the windows to which to listen"""
43 self._mainwindow = mainwindow
44 self._inner_evb = inner_evb
45
46 # Enable events
47 self._mainwindow.set_events(
48 gtk.gdk.KEY_PRESS_MASK | \
49 gtk.gdk.KEY_RELEASE_MASK \
50 )
51
52 self._inner_evb.set_events(
53 gtk.gdk.POINTER_MOTION_MASK | \
54 gtk.gdk.POINTER_MOTION_HINT_MASK | \
55 gtk.gdk.BUTTON_MOTION_MASK | \
56 gtk.gdk.BUTTON_PRESS_MASK | \
57 gtk.gdk.BUTTON_RELEASE_MASK
58 )
59
60 self._mainwindow.set_flags(gtk.CAN_FOCUS)
61 self._inner_evb.set_flags(gtk.CAN_FOCUS)
62
63 # Callback functions to link the event systems
64 self._mainwindow.connect('unrealize', self._quit_cb)
65 self._inner_evb.connect('key_press_event', self._keydown_cb)
66 self._inner_evb.connect('key_release_event', self._keyup_cb)
67 self._inner_evb.connect('button_press_event', self._mousedown_cb)
68 self._inner_evb.connect('button_release_event', self._mouseup_cb)
69 self._inner_evb.connect('motion-notify-event', self._mousemove_cb)
70 self._inner_evb.connect('expose-event', self._expose_cb)
71 self._inner_evb.connect('configure-event', self._resize_cb)
72
73 # Internal data
74 self.__stopped = False
75 self.__keystate = [0] * 323
76 self.__button_state = [0,0,0]
77 self.__mouse_pos = (0,0)
78 self.__repeat = (None, None)
79 self.__held = set()
80 self.__held_time_left = {}
81 self.__held_last_time = {}
82 self.__tick_id = None
83
84 def hook_pygame(self):
85 pygame.key.get_pressed = self._get_pressed
86 pygame.key.set_repeat = self._set_repeat
87 pygame.mouse.get_pressed = self._get_mouse_pressed
88 pygame.mouse.get_pos = self._get_mouse_pos
89
90 def _expose_cb(self, event, widget):
91 pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE))
92 return True
93
94 def _resize_cb( self, activity, event ):
95 #evt = pygame.event.Event(eventwrap.pygame.VIDEORESIZE,
96 # size=(event.width,event.height), width=event.width, height=event.height))
97 #pygame.event.post(evt)
98 return False # continue processing
99
100 def _quit_cb(self, data=None):
101 self.__stopped = True
102 pygame.event.post(pygame.event.Event(pygame.QUIT))
103
104 def _keydown_cb(self, widget, event):
105 key = event.keyval
106 if key in self.__held:
107 return True
108 else:
109 if self.__repeat[0] is not None:
110 self.__held_last_time[key] = pygame.time.get_ticks()
111 self.__held_time_left[key] = self.__repeat[0]
112 self.__held.add(key)
113
114 return self._keyevent(widget, event, pygame.KEYDOWN)
115
116 def _keyup_cb(self, widget, event):
117 key = event.keyval
118 if self.__repeat[0] is not None:
119 if key in self.__held:
120 # This is possibly false if set_repeat() is called with a key held
121 del self.__held_time_left[key]
122 del self.__held_last_time[key]
123 self.__held.discard(key)
124
125 return self._keyevent(widget, event, pygame.KEYUP)
126
127 def _keymods(self):
128 mod = 0
129 for key_val, mod_val in self.mod_map.iteritems():
130 mod |= self.__keystate[key_val] and mod_val
131 return mod
132
133 def _keyevent(self, widget, event, type):
134 key = gtk.gdk.keyval_name(event.keyval)
135 if key is None:
136 # No idea what this key is.
137 return False
138
139 keycode = None
140 if key in self.key_trans:
141 keycode = self.key_trans[key]
142 elif hasattr(pygame, 'K_'+key.upper()):
143 keycode = getattr(pygame, 'K_'+key.upper())
144 elif hasattr(pygame, 'K_'+key.lower()):
145 keycode = getattr(pygame, 'K_'+key.lower())
146 elif key == 'XF86Start':
147 # view source request, specially handled...
148 self._mainwindow.view_source()
149 else:
150 print 'Key %s unrecognized' % key
151
152 if keycode is not None:
153 if type == pygame.KEYDOWN:
154 mod = self._keymods()
155 self.__keystate[keycode] = type == pygame.KEYDOWN
156 if type == pygame.KEYUP:
157 mod = self._keymods()
158 ukey = unichr(gtk.gdk.keyval_to_unicode(event.keyval))
159 if ukey == '\000':
160 ukey = ''
161 evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod)
162 self._post(evt)
163
164 return True
165
166 def _get_pressed(self):
167 return self.__keystate
168
169 def _get_mouse_pressed(self):
170 return self.__button_state
171
172 def _mousedown_cb(self, widget, event):
173 self.__button_state[event.button-1] = 1
174 return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN)
175
176 def _mouseup_cb(self, widget, event):
177 self.__button_state[event.button-1] = 0
178 return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP)
179
180 def _mouseevent(self, widget, event, type):
181 evt = pygame.event.Event(type, button=event.button, pos=(event.x, event.y))
182 self._post(evt)
183 return True
184
185 def _mousemove_cb(self, widget, event):
186 # From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/
187 # if this is a hint, then let's get all the necessary
188 # information, if not it's all we need.
189 if event.is_hint:
190 x, y, state = event.window.get_pointer()
191 else:
192 x = event.x
193 y = event.y
194 state = event.state
195
196 rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1])
197 self.__mouse_pos = (x, y)
198
199 self.__button_state = [
200 state & gtk.gdk.BUTTON1_MASK and 1 or 0,
201 state & gtk.gdk.BUTTON2_MASK and 1 or 0,
202 state & gtk.gdk.BUTTON3_MASK and 1 or 0,
203 ]
204
205 evt = pygame.event.Event(pygame.MOUSEMOTION,
206 pos=self.__mouse_pos, rel=rel, buttons=self.__button_state)
207 self._post(evt)
208 return True
209
210 def _tick_cb(self):
211 cur_time = pygame.time.get_ticks()
212 for key in self.__held:
213 delta = cur_time - self.__held_last_time[key]
214 self.__held_last_time[key] = cur_time
215
216 self.__held_time_left[key] -= delta
217 if self.__held_time_left[key] <= 0:
218 self.__held_time_left[key] = self.__repeat[1]
219 self._keyevent(None, _MockEvent(key), pygame.KEYDOWN)
220
221 return True
222
223 def _set_repeat(self, delay=None, interval=None):
224 if delay is not None and self.__repeat[0] is None:
225 self.__tick_id = gobject.timeout_add(10, self._tick_cb)
226 elif delay is None and self.__repeat[0] is not None:
227 gobject.source_remove(self.__tick_id)
228 self.__repeat = (delay, interval)
229
230 def _get_mouse_pos(self):
231 return self.__mouse_pos
232
233 def _post(self, evt):
234 try:
235 pygame.event.post(evt)
236 except pygame.error, e:
237 if str(e) == 'Event queue full':
238 print "Event queue full!"
239 pass
240 else:
241 raise e