~ubuntu-branches/ubuntu/vivid/python-gasp/vivid

« back to all changes in this revision

Viewing changes to gasp/api.py

  • Committer: Bazaar Package Importer
  • Author(s): Luke Faraone
  • Date: 2009-07-27 21:16:02 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090727211602-50iw8v0ufxe5gm6i
Tags: 0.3.0-1
* New upstream version
* Update Maintainer email address
* Fixed copyright file to use the © symbol
* Switch to pysupport
* Fix get-orig-source in debian/rules
* Fixed setup.py so that images are installed in the proper location.
* debian/control: Added dependency on python-gobject, python-gtk2, and
  python-multiprocessing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import backend as real_backend
 
1
import backend
2
2
import math
3
3
import random
 
4
import time
4
5
 
5
 
backend = None
 
6
# diffrent update_when values
 
7
NOTHING = "Nothing"
 
8
MOUSE_PRESSED = "Mouse_Pressed"
 
9
KEY_PRESSED = "Key_Pressed"
6
10
 
7
11
#============================ SCREEN FUNCTIONS ==============================
8
12
 
17
21
        self.width = width
18
22
        self.height = height
19
23
        self.title = title
20
 
        self.background = background
 
24
        self.background = backend.color_convert(background)
21
25
 
22
26
        # backend.check_screen_atts(self)  # checks the variables
23
27
        backend.create_screen(self)
24
28
 
25
 
    def __str__(self):
26
 
        return 'Screen: \n width = %d \n height = %d \n title = %s \n' % \
27
 
               (self.width, self.height, self.title)
28
29
 
29
30
 
30
31
def begin_graphics(width=640, height=480, title="Gasp",
31
 
                   background=(255,255,255), back_end=real_backend):
 
32
                   background=(255,255,255), back_end=backend):
32
33
    """
33
34
    Initialize a graphics window in GASP
34
35
    """
35
 
    Screen(width, height, title, background, back_end)
36
 
 
 
36
    global screen
 
37
    screen = Screen(width, height, title, background, back_end)
37
38
 
38
39
def end_graphics():
39
40
    """
41
42
    """
42
43
    backend.end()
43
44
 
44
 
 
45
45
def clear_screen():
46
46
    """
47
47
    Removes all objects from the graphics window
55
55
    """
56
56
    backend.remove(obj)
57
57
 
58
 
 
59
58
#================================ Point =====================================
60
59
 
61
60
class Point:
87
86
        return Point(point)
88
87
 
89
88
 
90
 
# =============================== Shapes ====================================
91
 
 
92
 
class Plot:
 
89
 
 
90
class Shape:
 
91
    def __init__(self, center, filled=False, color=(0,0,0), thickness=1):
 
92
        self.center = make_it_a_point(center)
 
93
        self.filled = filled
 
94
        self.color = backend.color_convert(color)
 
95
        self.thickness = thickness
 
96
        self.rot = 0
 
97
        self.key = time.time()*random.random()
 
98
 
 
99
    def move_by(self,x,y):
 
100
        if self.on_screen:
 
101
            self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
 
102
            self.center = Point(self.center.x+x,self.center.y+y)
 
103
        else: move_by(self,x,y)
 
104
 
 
105
    def move_to(self,pos):
 
106
        if self.on_screen:
 
107
            x,y = pos[0]-self.center.x,pos[1]-self.center.y
 
108
            self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
 
109
            self.center = Point(self.center.x+x,self.center.y+y)
 
110
        else: move_to(self,pos)
 
111
 
 
112
    def draw(self,context):
 
113
        context.save()
 
114
        x,y = backend.flip_coords(self.topleft)
 
115
        context.translate(x,y-self.surface.get_height())
 
116
        if self.rot != 0:
 
117
            context.translate(self.surface.get_width()/2,self.surface.get_height()/2)
 
118
            context.rotate(self.rot)
 
119
            context.translate(-1*self.surface.get_width()/2,-1*self.surface.get_height()/2)
 
120
        context.set_source_surface(self.surface)
 
121
        context.paint()
 
122
        context.restore()
 
123
 
 
124
class Plot(Shape):
93
125
    """
94
126
    Allows screen objects to be placed in the graphics window
95
127
    """
96
128
    def __init__(self, pos, color=(0,0,0), size=1):
97
 
        self.pos = make_it_a_point(pos)
 
129
        self.center = make_it_a_point(pos)
98
130
        self.color = color
99
131
        self.size = size
100
 
        backend.plot(self)
101
 
 
102
 
    
103
 
class Line:
 
132
        self.key = time.time()*random.random()
 
133
        self.on_screen = False
 
134
        screen.action_objects.put([["plot",self],"new object"])
 
135
 
 
136
 
 
137
class Line(Shape):
104
138
    """
105
139
    Allows the creation of lines in the graphics window
106
140
    """
107
 
    def __init__(self, start, end, color=(0,0,0)):
 
141
    def __init__(self, start, end, color=(0,0,0),thickness=1):
108
142
        self.start = make_it_a_point(start)
109
143
        self.end = make_it_a_point(end)
110
 
        self.color = color
111
 
 
112
 
        backend.create_line(self)
 
144
        self.center = make_it_a_point(((start[0]+end[0])/2,(start[1]+end[1])/2))
 
145
        self.color = backend.color_convert(color)
 
146
        self.thickness=thickness
 
147
        self.key = time.time()*random.random()
 
148
        self.on_screen = False
 
149
        screen.action_objects.put([["line",self],"new object"])
 
150
 
 
151
    def move_by(self,x,y):
 
152
        if self.on_screen:
 
153
            self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
 
154
            self.center = Point(self.center.x+x,self.center.y+y)
 
155
        else: move_by(self,x,y)
 
156
 
 
157
    def move_to(self,pos):
 
158
        if self.on_screen:
 
159
            x,y = pos[0]-self.center.x,pos[1]-self.center.y
 
160
            self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
 
161
            self.center = Point(self.center.x+x,self.center.y+y)
 
162
        else: move_to(self,pos)
113
163
 
114
164
    def __repr__(self):
115
165
        return "A Line instance from (%d, %d) to (%d, %d)" % \
116
166
                (self.start.x, self.start.y, self.end.x, self.end.y)
117
 
    
118
 
 
119
 
class Shape:
120
 
    """
121
 
    Allows the creation of shapes in the graphics window
122
 
    """
123
 
    def __init__(self, center, filled=False, color=(0,0,0), thickness=1):
124
 
        self.center = make_it_a_point(center)
125
 
        self.filled = filled
126
 
        self.color = color
127
 
        self.thickness = thickness
128
 
        self.angle = 0
129
 
 
130
 
    def move_to(self, center):
131
 
        """
132
 
        Moves a shape to a designated point
133
 
        """
134
 
        self.center = make_it_a_point(center)
135
 
        backend.move_to(self, self.center)
136
 
 
137
 
    def move_by(self, dx=0, dy=0):
138
 
        """
139
 
        Moves a shape by specified X and Y amounts
140
 
        """
141
 
        self.center.x += dx
142
 
        self.center.y += dy
143
 
        backend.move_to(self, self.center)
144
 
 
145
 
    def rotate_to(self, angle):
146
 
        """
147
 
        Rotates a shape to an angle
148
 
        """
149
 
        change = angle - self.angle
150
 
        self.angle = angle
151
 
        backend.rotate_by(self, change)
152
 
 
153
 
    def rotate_by(self, angle):
154
 
        """
155
 
        Rotates a shape by an angle
156
 
        """
157
 
        self.angle += angle
158
 
        backend.rotate_by(self, angle)
159
 
 
160
167
 
161
168
class Box(Shape):
162
169
    """
168
175
                             int(lower_left_corner[1] + height/2.0)))
169
176
        self.height = height
170
177
        self.width = width
 
178
        self.on_screen = False
171
179
        Shape.__init__(self, self.center, filled, color, thickness)
172
 
        
173
 
        backend.create_box(self)
 
180
        screen.action_objects.put([["box",self],"new object"])
174
181
 
175
 
    def move_to(self, lower_left_corner):
176
 
        new_center = Point((int(lower_left_corner[0] + self.width/2.0),
177
 
                            int(lower_left_corner[1] + self.height/2.0)))
178
 
        Shape.move_to(self, new_center)
 
182
    def move_to(self,pos):
 
183
        if self.on_screen:
 
184
            x,y = pos[0]-self.topleft[0],pos[1]-self.topleft[1]
 
185
            self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
 
186
            self.center = Point(self.center.x+x,self.center.y+y)
 
187
        else:
 
188
            move_to(self,pos)
179
189
 
180
190
    def __repr__(self):
181
191
        return "Box instance at (%d, %d) with width %d and height %d" % \
182
192
               (self.center.x, self.center.y, self.width, self.height)
183
193
 
184
 
 
185
194
class Polygon(Shape):
186
195
    """
187
196
    Allows the creation of polygons in the graphics window
188
197
    """
189
 
    def __init__(self, points, filled=False, color=(0,0,0)):
 
198
    def __init__(self, points, filled=False, color=(0,0,0),thickness=1):
 
199
        self.thickness = thickness
190
200
        self.points = points
191
201
        x_values = []
192
202
        y_values = []    
193
203
        for point in self.points:
194
204
            x_values.append(point[0])
195
205
            y_values.append(point[1])
196
 
        self.height = (max(y_values) - min(y_values))
197
 
        self.width = (max(x_values) - min(x_values))
 
206
        self.height = (max(y_values) - min(y_values))+self.thickness*2
 
207
        self.width = (max(x_values) - min(x_values))+self.thickness*2
 
208
        self.topleft = (min(x_values)-self.thickness, min(y_values)-self.thickness)
198
209
        self.center = Point(self.width/2 + min(x_values), self.height/2 +
199
210
                                                              min(y_values))
 
211
        self.on_screen = False
200
212
        Shape.__init__(self, self.center, filled, color)
201
 
        backend.create_polygon(self)
 
213
        screen.action_objects.put([["polygon",self],"new object"])
202
214
            
203
215
    def __repr__(self):
204
216
        return "Polygon instance at (%i, %i) with the points:\n %t" % \
205
217
                (self.center.x , self.center.y, self.points)
206
 
        
207
218
 
208
219
class Circle(Shape):
209
220
    """
213
224
                 filled=False, color=(0,0,0), thickness=1):
214
225
        Shape.__init__(self, center, filled, color, thickness)
215
226
        self.radius = radius
216
 
        
217
 
        backend.create_circle(self)
 
227
        self.on_screen = False
 
228
        screen.action_objects.put([["circle",self],"new object"])
 
229
        #backend.create_circle(self)
218
230
 
219
231
    def __repr__(self):
220
232
        return "Circle instance at (%d, %d) with radius %d" % \
221
233
               (self.center.x, self.center.y, self.radius)
222
234
 
223
 
 
224
235
class Arc(Shape):
225
236
    """
226
237
    Allows the creation of arcs in the graphics window
229
240
                 filled=False, color=(0,0,0), thickness=1):
230
241
        Shape.__init__(self, center, filled, color, thickness)
231
242
        self.radius = radius
232
 
        self.start_angle = start_angle
233
 
        self.end_angle = end_angle
234
 
        
235
 
        backend.create_arc(self)
 
243
        self.start_angle = 360-start_angle
 
244
        self.end_angle = 360-end_angle
 
245
        self.on_screen = False        
 
246
        screen.action_objects.put([["arc",self],"new object"])
236
247
 
237
248
    def __repr__(self):
238
249
        return "Arc instance at (%d, %d) with start angle %d and end angle %d" \
239
250
             % (self.center.x, self.center.y, self.start_angle, self.end_angle)
240
251
 
241
252
 
 
253
 
242
254
class Oval(Shape):
243
255
    """
244
256
    Allows the creation of circles in the graphics window
248
260
        Shape.__init__(self, center, filled, color, thickness)
249
261
        self.width = width
250
262
        self.height = height
251
 
 
252
 
        backend.create_oval(self)
 
263
        self.on_screen = False
 
264
        screen.action_objects.put([["oval",self],"new object"])
253
265
 
254
266
    def __repr__(self):
255
267
        return "Oval instance at (%d, %d) with width %d and height %d" % \
256
268
               (self.center.x, self.center.y, self.width, self.height)
257
269
 
258
 
        
259
270
class Image(Shape):
260
271
    """
261
272
    Allows the placing of images in the graphics window
262
273
    """
263
274
    def __init__(self, file_path, center, width=0, height=0):
264
 
        self.name = ''.join(file_path.split('.')[:-1]).split('/')[-1]
265
275
        self.path_name = file_path
266
 
        self.x = center[0]
267
 
        self.y = center[1] 
268
 
        self.center = Point(center)
 
276
        self.center = Point(backend.flip_coords(center))
269
277
        self.width = width
270
278
        self.height = height
271
 
        backend.create_image(self)
 
279
        self.key = time.time()*random.random()
 
280
        self.on_screen = False
 
281
        screen.action_objects.put([["image",self],"new object"])
 
282
 
 
283
    def draw(self,context):
 
284
        context.save()
 
285
        context.translate(*self.topleft)
 
286
        context.translate(self.width/2,self.height/2)
 
287
        context.rotate(self.rot)
 
288
        context.translate(-1*self.width/2,-1*self.height/2)
 
289
        context.save()
 
290
        context.scale(self.scale[0],self.scale[1])
 
291
        context.set_source_pixbuf(self.pixbuf,0,0)
 
292
        context.paint()
 
293
        context.restore()
 
294
        context.restore()
 
295
 
 
296
    def move_by(self,x,y):
 
297
        if self.on_screen:
 
298
            self.topleft = (self.topleft[0]+x,self.topleft[1]-y)
 
299
            self.center = Point(self.center.x+x,self.center.y-y)
 
300
        else:
 
301
            move_by(self,x,y)
 
302
 
 
303
    def move_to(self,pos):
 
304
        if self.on_screen:
 
305
            x,y = pos[0]-self.center.x,pos[1]-self.center.y
 
306
            self.topleft = (self.topleft[0]+x,self.topleft[1]-y)
 
307
            self.center = Point(self.center.x+x,self.center.y-y)
 
308
        else:
 
309
            move_to(self,pos)
272
310
 
273
311
    def __repr__(self):
274
312
        return "Image instance at (%i, %i) from the file %s" % \
279
317
    """
280
318
    Moves a screen object to a specified location
281
319
    """
282
 
    obj.move_to(pos)
 
320
    screen.action_objects.put([obj,"move_to",pos])
283
321
 
284
322
 
285
323
def move_by(obj, dx, dy):
286
324
    """
287
325
    Moves a screen object by specified X and Y amounts
288
326
    """
289
 
    obj.move_by(dx, dy)
 
327
    screen.action_objects.put([obj,"move_by",(dx,dy)])
 
328
 
 
329
def get_text_length(text):
 
330
    return backend.get_text_length(text)
290
331
 
291
332
 
292
333
def rotate_to(obj, angle):
293
334
    """
294
335
    Rotates a screen object to a specified angle
295
336
    """
296
 
    obj.rotate_to(angle)
 
337
    screen.action_objects.put([obj,"rotate_to",math.radians(angle)])
297
338
 
298
339
 
299
340
def rotate_by(obj, angle):
300
341
    """
301
342
    Rotates a screen object by a specified angle
302
343
    """
303
 
    obj.rotate_by(angle)
 
344
    screen.action_objects.put([obj,"rotate_by",math.radians(angle)])
304
345
 
305
346
 
306
347
# =============================== Text ======================================
307
348
 
308
 
class Text:
 
349
class Text(Shape):
309
350
    """
310
351
    Allows the creation of text in the graphics window
311
352
    """
312
353
    def __init__(self, text, pos, color=(0,0,0), size=12):
313
354
        self.text = text
314
 
        self.pos = make_it_a_point(pos)
315
 
        self.color = color
 
355
        self.color = backend.color_convert(color)
316
356
        self.size = size
317
 
 
318
 
        backend.create_text(self)
319
 
 
320
 
 
321
 
# =============================== Sound =====================================
 
357
        self.pos = backend.flip_coords(pos)
 
358
        self.key = time.time()*random.random()
 
359
        self.on_screen = False
 
360
        screen.action_objects.put([["text",self],"new object"])
 
361
        #screen.action_objects.put(obj)
 
362
        #screen.objects.append(obj)
 
363
 
 
364
    def draw(self, context):
 
365
        context.save()
 
366
        context.move_to(*self.pos)
 
367
        if self.rot != 0:
 
368
            context.rotate(self.rot)
 
369
        context.set_source_rgb(*self.color)
 
370
        context.set_font_size(self.size)
 
371
        context.show_text(self.text)
 
372
        context.restore()
 
373
 
 
374
 
 
375
    def move_to(self,pos):
 
376
        if self.on_screen:
 
377
            self.pos = backend.flip_coords(pos)
 
378
        else:
 
379
            move_to(self,pos)
 
380
 
 
381
    def move_by(self,dx,dy):
 
382
        if self.on_screen:
 
383
            self.pos = (self.pos[0]+dx,self.pos[1]-dy)
 
384
        else:
 
385
            move_by(self,dx,dy)
 
386
 
 
387
# ============================ Text Entry ==================================
 
388
 
 
389
def Text_Entry(message="Enter Text:", pos=(0,0), color=(0,0,0), size=12):
 
390
    entry = backend.Entry(message,pos,color,size)
 
391
    return entry.entry
 
392
 
 
393
 
 
394
"""# =============================== Sound =====================================
322
395
 
323
396
class Sound:
324
 
    """
 
397
    ""
325
398
    Allows the creation and playback of sounds
326
 
    """
 
399
    ""
327
400
    def __init__(self, file_path):
328
401
        self.path = file_path
329
 
        self.name = ''.join(file_path.split('.')[:-1]).split('/')[-1]
330
 
        backend.create_sound(self)
 
402
        self.filename = ''.join(file_path.split('.')[:-1]).split('/')[-1]
331
403
 
332
404
    def play(self):
333
405
        backend.play_sound(self)
337
409
        
338
410
 
339
411
def create_sound(file_path):
340
 
    """
 
412
    ""
341
413
    Creates a sound object from a file
342
 
    """
 
414
    ""
343
415
    return Sound(file_path)
344
416
        
345
417
 
346
418
def play_sound(sound):
347
 
    """
 
419
    ""
348
420
    Starts to play a specified sound object
349
 
    """
 
421
    ""
350
422
    sound.play()
351
423
 
352
424
 
353
425
def stop_sound(sound):
354
 
    """
 
426
    ""
355
427
    Stops the playback of a specified sound object
356
 
    """
357
 
    sound.stop()
358
 
 
 
428
    ""
 
429
    sound.stop()"""
359
430
 
360
431
# =========================== Event Functions ==============================
361
432
 
363
434
    """
364
435
    Returns a Point() at the mouse's current position
365
436
    """
366
 
    return Point(backend.mouse_pos())
 
437
    pos = backend.mouse_pos()
 
438
    return backend.flip_coords((int(pos[0]),int(pos[1])))
367
439
 
368
440
 
369
441
def mouse_buttons():  
370
442
    """
371
443
    Returns a dictionary of the current state of the mouse buttons
372
444
    """
373
 
    return backend.mouse_pressed()
 
445
    return backend.mouse_state()
374
446
 
375
447
 
376
448
def keys_pressed(): 
379
451
    """
380
452
    return backend.keys()
381
453
 
382
 
 
383
 
def key_pressed(key, wait_for_upstroke=False): 
384
 
    """
385
 
    Returns a boolean of whether a key has been pressed
386
 
    """
387
 
    if wait_for_upstroke:
388
 
        return key in backend.waiting_keys()
389
 
    else:
390
 
        return key in keys_pressed()
391
 
 
392
 
 
393
 
    
 
454
def key_pressed(key):
 
455
    return key in backend.keys()
 
456
 
 
457
def screen_size():
 
458
    return backend.get_screen_size()
 
459
 
394
460
# ============================= GASP TOOLS ==================================
395
461
 
396
 
def screen_shot(filename):
 
462
def screen_shot(filename="screen_shot"):
397
463
    """
398
464
    Saves a screenshot to a specified file name
399
465
    """
400
466
    backend.screen_picture(filename)
401
467
 
402
 
random_choice = random.choice
403
 
 
404
 
 
405
468
def random_between(num1, num2):
406
469
    if (num1 == num2):
407
470
        return num1
418
481
    Prompts the user to enter a number
419
482
    """
420
483
    while True:
421
 
        result = input(prompt)
422
 
        if isinstance(result, (float, int)):
423
 
            return result
424
 
        print "But that wasn't a number!"
 
484
        try:
 
485
            result = eval(raw_input(prompt))
 
486
            if isinstance(result, (float, int)):
 
487
                return result
 
488
            print "But that wasn't a number!"
 
489
        except:
 
490
            print "But that wasn't a number!"
425
491
 
426
492
 
427
493
def read_yesorno(prompt='Yes or no? '):
451
517
    'mouse_clicked'
452
518
    'next_tick'
453
519
    """
454
 
    backend.update_when(event_type)
455
 
 
 
520
    if event_type == "key_pressed":
 
521
        return backend.update_when(event_type)
 
522
    else: backend.update_when(event_type)
456
523
 
457
524
def sleep(duration):
458
525
    """