Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/library/pippy/gasp/api.py
diff options
context:
space:
mode:
Diffstat (limited to 'library/pippy/gasp/api.py')
-rw-r--r--library/pippy/gasp/api.py534
1 files changed, 534 insertions, 0 deletions
diff --git a/library/pippy/gasp/api.py b/library/pippy/gasp/api.py
new file mode 100644
index 0000000..9a88ce7
--- /dev/null
+++ b/library/pippy/gasp/api.py
@@ -0,0 +1,534 @@
+import backend
+import math
+import random
+import time
+
+# diffrent update_when values
+NOTHING = "Nothing"
+MOUSE_PRESSED = "Mouse_Pressed"
+KEY_PRESSED = "Key_Pressed"
+
+#============================ SCREEN FUNCTIONS ==============================
+
+class Screen:
+ """
+ Defines the graphics window in which objects can be created
+ """
+ def __init__(self, width, height,
+ title="Gasp", background=(255,255,255), back_end=None):
+ global backend
+ backend = back_end
+ self.width = width
+ self.height = height
+ self.title = title
+ self.background = backend.color_convert(background)
+
+ # backend.check_screen_atts(self) # checks the variables
+ backend.create_screen(self)
+
+
+
+def begin_graphics(width=-1, height=-1, title="Gasp",
+ background=(255,255,255), back_end=backend):
+ """
+ Initialize a graphics window in GASP
+ """
+ global screen
+ screen = Screen(width, height, title, background, back_end)
+
+def end_graphics():
+ """
+ Close the graphics window
+ """
+ backend.end()
+
+def clear_screen():
+ """
+ Removes all objects from the graphics window
+ """
+ backend.clear_screen()
+
+
+def remove_from_screen(obj):
+ """
+ Removes a specified object from the graphics window
+ """
+ backend.remove(obj)
+
+#================================ Point =====================================
+
+class Point:
+ """
+ Creates a point object with an x and y attribute
+ """
+ def __init__(self, *args):
+ if len(args) == 1:
+ self.x, self.y = args[0]
+ else:
+ self.x, self.y = args
+
+ def __str__(self):
+ return "A Point instance at (%i, %i) \n" %(self.x, self.y)
+
+ def distance(self, other):
+ ''' The distance between two points '''
+ return math.sqrt(float((other.x - self.x))**2 +
+ (float(other.y - self.y))**2)
+
+
+def make_it_a_point(point):
+ """
+ Takes something and makes it into a point if it isn't one already
+ """
+ if isinstance(point, Point):
+ return point
+ else:
+ return Point(point)
+
+
+
+class Shape:
+ def __init__(self, center, filled=False, color=(0,0,0), thickness=1):
+ self.center = make_it_a_point(center)
+ self.filled = filled
+ self.color = backend.color_convert(color)
+ self.thickness = thickness
+ self.rot = 0
+ self.key = time.time()*random.random()
+
+ def move_by(self,x,y):
+ if self.on_screen:
+ self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
+ self.center = Point(self.center.x+x,self.center.y+y)
+ else: move_by(self,x,y)
+
+ def move_to(self,pos):
+ if self.on_screen:
+ x,y = pos[0]-self.center.x,pos[1]-self.center.y
+ self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
+ self.center = Point(self.center.x+x,self.center.y+y)
+ else: move_to(self,pos)
+
+ def draw(self,context):
+ context.save()
+ x,y = backend.flip_coords(self.topleft)
+ context.translate(x,y-self.surface.get_height())
+ if self.rot != 0:
+ context.translate(self.surface.get_width()/2,self.surface.get_height()/2)
+ context.rotate(self.rot)
+ context.translate(-1*self.surface.get_width()/2,-1*self.surface.get_height()/2)
+ context.set_source_surface(self.surface)
+ context.paint()
+ context.restore()
+
+class Plot(Shape):
+ """
+ Allows screen objects to be placed in the graphics window
+ """
+ def __init__(self, pos, color=(0,0,0), size=1):
+ self.center = make_it_a_point(pos)
+ self.color = color
+ self.size = size
+ self.key = time.time()*random.random()
+ self.on_screen = False
+ screen.action_objects.put([["plot",self],"new object"])
+
+
+class Line(Shape):
+ """
+ Allows the creation of lines in the graphics window
+ """
+ def __init__(self, start, end, color=(0,0,0),thickness=1):
+ self.start = make_it_a_point(start)
+ self.end = make_it_a_point(end)
+ self.center = make_it_a_point(((start[0]+end[0])/2,(start[1]+end[1])/2))
+ self.color = backend.color_convert(color)
+ self.thickness=thickness
+ self.key = time.time()*random.random()
+ self.on_screen = False
+ screen.action_objects.put([["line",self],"new object"])
+
+ def move_by(self,x,y):
+ if self.on_screen:
+ self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
+ self.center = Point(self.center.x+x,self.center.y+y)
+ else: move_by(self,x,y)
+
+ def move_to(self,pos):
+ if self.on_screen:
+ x,y = pos[0]-self.center.x,pos[1]-self.center.y
+ self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
+ self.center = Point(self.center.x+x,self.center.y+y)
+ else: move_to(self,pos)
+
+ def __repr__(self):
+ return "A Line instance from (%d, %d) to (%d, %d)" % \
+ (self.start.x, self.start.y, self.end.x, self.end.y)
+
+class Box(Shape):
+ """
+ Allows the creation of squares and rectangles in the graphics window
+ """
+ def __init__(self, lower_left_corner, width, height,
+ filled=False, color=(0,0,0), thickness=1):
+ self.center = Point((int(lower_left_corner[0] + width/2.0),
+ int(lower_left_corner[1] + height/2.0)))
+ self.height = height
+ self.width = width
+ self.on_screen = False
+ Shape.__init__(self, self.center, filled, color, thickness)
+ screen.action_objects.put([["box",self],"new object"])
+
+ def move_to(self,pos):
+ if self.on_screen:
+ x,y = pos[0]-self.topleft[0],pos[1]-self.topleft[1]
+ self.topleft = (self.topleft[0]+x,self.topleft[1]+y)
+ self.center = Point(self.center.x+x,self.center.y+y)
+ else:
+ move_to(self,pos)
+
+ def __repr__(self):
+ return "Box instance at (%d, %d) with width %d and height %d" % \
+ (self.center.x, self.center.y, self.width, self.height)
+
+class Polygon(Shape):
+ """
+ Allows the creation of polygons in the graphics window
+ """
+ def __init__(self, points, filled=False, color=(0,0,0),thickness=1):
+ self.thickness = thickness
+ self.points = points
+ x_values = []
+ y_values = []
+ for point in self.points:
+ x_values.append(point[0])
+ y_values.append(point[1])
+ self.height = (max(y_values) - min(y_values))+self.thickness*2
+ self.width = (max(x_values) - min(x_values))+self.thickness*2
+ self.topleft = (min(x_values)-self.thickness, min(y_values)-self.thickness)
+ self.center = Point(self.width/2 + min(x_values), self.height/2 +
+ min(y_values))
+ self.on_screen = False
+ Shape.__init__(self, self.center, filled, color)
+ screen.action_objects.put([["polygon",self],"new object"])
+
+ def __repr__(self):
+ return "Polygon instance at (%i, %i) with the points:\n %t" % \
+ (self.center.x , self.center.y, self.points)
+
+class Circle(Shape):
+ """
+ Allows the creation of circles in the graphics window
+ """
+ def __init__(self, center, radius,
+ filled=False, color=(0,0,0), thickness=1):
+ Shape.__init__(self, center, filled, color, thickness)
+ self.radius = radius
+ self.on_screen = False
+ screen.action_objects.put([["circle",self],"new object"])
+ #backend.create_circle(self)
+
+ def __repr__(self):
+ return "Circle instance at (%d, %d) with radius %d" % \
+ (self.center.x, self.center.y, self.radius)
+
+class Arc(Shape):
+ """
+ Allows the creation of arcs in the graphics window
+ """
+ def __init__(self, center, radius, start_angle, end_angle,
+ filled=False, color=(0,0,0), thickness=1):
+ Shape.__init__(self, center, filled, color, thickness)
+ self.radius = radius
+ self.start_angle = 360-start_angle
+ self.end_angle = 360-end_angle
+ self.on_screen = False
+ screen.action_objects.put([["arc",self],"new object"])
+
+ def __repr__(self):
+ return "Arc instance at (%d, %d) with start angle %d and end angle %d" \
+ % (self.center.x, self.center.y, self.start_angle, self.end_angle)
+
+
+
+class Oval(Shape):
+ """
+ Allows the creation of circles in the graphics window
+ """
+ def __init__(self, center, width, height,
+ filled=False, color=(0,0,0), thickness=1):
+ Shape.__init__(self, center, filled, color, thickness)
+ self.width = width
+ self.height = height
+ self.on_screen = False
+ screen.action_objects.put([["oval",self],"new object"])
+
+ def __repr__(self):
+ return "Oval instance at (%d, %d) with width %d and height %d" % \
+ (self.center.x, self.center.y, self.width, self.height)
+
+class Image(Shape):
+ """
+ Allows the placing of images in the graphics window
+ """
+ def __init__(self, file_path, center, width=0, height=0):
+ self.path_name = file_path
+ self.center = Point(backend.flip_coords(center))
+ self.width = width
+ self.height = height
+ self.key = time.time()*random.random()
+ self.on_screen = False
+ screen.action_objects.put([["image",self],"new object"])
+
+ def draw(self,context):
+ context.save()
+ context.translate(*self.topleft)
+ context.translate(self.width/2,self.height/2)
+ context.rotate(self.rot)
+ context.translate(-1*self.width/2,-1*self.height/2)
+ context.save()
+ context.scale(self.scale[0],self.scale[1])
+ context.set_source_pixbuf(self.pixbuf,0,0)
+ context.paint()
+ context.restore()
+ context.restore()
+
+ def move_by(self,x,y):
+ if self.on_screen:
+ self.topleft = (self.topleft[0]+x,self.topleft[1]-y)
+ self.center = Point(self.center.x+x,self.center.y-y)
+ else:
+ move_by(self,x,y)
+
+ def move_to(self,pos):
+ if self.on_screen:
+ x,y = pos[0]-self.center.x,pos[1]-self.center.y
+ self.topleft = (self.topleft[0]+x,self.topleft[1]-y)
+ self.center = Point(self.center.x+x,self.center.y-y)
+ else:
+ move_to(self,pos)
+
+ def __repr__(self):
+ return "Image instance at (%i, %i) from the file %s" % \
+ (self.center.x, self.center.y, self.path_name)
+
+
+def move_to(obj, pos):
+ """
+ Moves a screen object to a specified location
+ """
+ screen.action_objects.put([obj,"move_to",pos])
+
+
+def move_by(obj, dx, dy):
+ """
+ Moves a screen object by specified X and Y amounts
+ """
+ screen.action_objects.put([obj,"move_by",(dx,dy)])
+
+def get_text_length(text):
+ return backend.get_text_length(text)
+
+
+def rotate_to(obj, angle):
+ """
+ Rotates a screen object to a specified angle
+ """
+ screen.action_objects.put([obj,"rotate_to",math.radians(angle)])
+
+
+def rotate_by(obj, angle):
+ """
+ Rotates a screen object by a specified angle
+ """
+ screen.action_objects.put([obj,"rotate_by",math.radians(angle)])
+
+
+# =============================== Text ======================================
+
+class Text(Shape):
+ """
+ Allows the creation of text in the graphics window
+ """
+ def __init__(self, text, pos, color=(0,0,0), size=12):
+ self.text = text
+ self.color = backend.color_convert(color)
+ self.size = size
+ self.pos = backend.flip_coords(pos)
+ self.key = time.time()*random.random()
+ self.on_screen = False
+ screen.action_objects.put([["text",self],"new object"])
+ #screen.action_objects.put(obj)
+ #screen.objects.append(obj)
+
+ def draw(self, context):
+ context.save()
+ context.move_to(*self.pos)
+ if self.rot != 0:
+ context.rotate(self.rot)
+ context.set_source_rgb(*self.color)
+ context.set_font_size(self.size)
+ context.show_text(self.text)
+ context.restore()
+
+
+ def move_to(self,pos):
+ self.pos = backend.flip_coords(pos)
+ move_to(self,pos)
+
+ def move_by(self,dx,dy):
+ self.pos = (self.pos[0]+dx,self.pos[1]-dy)
+ move_by(self,dx,dy)
+
+# ============================ Text Entry ==================================
+
+def Text_Entry(message="Enter Text:", pos=(0,0), color=(0,0,0), size=12):
+ entry = backend.Entry(message,pos,color,size)
+ return entry.entry
+
+
+"""# =============================== Sound =====================================
+
+class Sound:
+ ""
+ Allows the creation and playback of sounds
+ ""
+ def __init__(self, file_path):
+ self.path = file_path
+ self.filename = ''.join(file_path.split('.')[:-1]).split('/')[-1]
+
+ def play(self):
+ backend.play_sound(self)
+
+ def stop(self):
+ backend.stop_sound(self)
+
+
+def create_sound(file_path):
+ ""
+ Creates a sound object from a file
+ ""
+ return Sound(file_path)
+
+
+def play_sound(sound):
+ ""
+ Starts to play a specified sound object
+ ""
+ sound.play()
+
+
+def stop_sound(sound):
+ ""
+ Stops the playback of a specified sound object
+ ""
+ sound.stop()"""
+
+# =========================== Event Functions ==============================
+
+def mouse_position():
+ """
+ Returns a Point() at the mouse's current position
+ """
+ pos = backend.mouse_pos()
+ return backend.flip_coords((int(pos[0]),int(pos[1])))
+
+
+def mouse_buttons():
+ """
+ Returns a dictionary of the current state of the mouse buttons
+ """
+ return backend.mouse_state()
+
+
+def keys_pressed():
+ """
+ Returns a list of all of the keys being pressed
+ """
+ return backend.keys()
+
+def key_pressed(key):
+ return key in backend.keys()
+
+def screen_size():
+ return backend.get_screen_size()
+
+# ============================= GASP TOOLS ==================================
+
+def screen_shot(filename="screen_shot"):
+ """
+ Saves a screenshot to a specified file name
+ """
+ backend.screen_picture(filename)
+
+def random_between(num1, num2):
+ if (num1 == num2):
+ return num1
+ elif (num1 > num2):
+ return random.randint(num2, num1)
+ else:
+ return random.randint(num1, num2)
+
+read_string = raw_input
+
+
+def read_number(prompt='Please enter a number: '):
+ """
+ Prompts the user to enter a number
+ """
+ while True:
+ try:
+ result = eval(raw_input(prompt))
+ if isinstance(result, (float, int)):
+ return result
+ print "But that wasn't a number!"
+ except:
+ print "But that wasn't a number!"
+
+
+def read_yesorno(prompt='Yes or no? '):
+ """
+ Asks the user to enter yes or no
+ """
+ while True:
+ result = raw_input(prompt)
+ result = result.lower()
+ if result=='yes' or result=='y': return True
+ if result=='no' or result=='n': return False
+ print "Please answer yes or no."
+
+# =============================== Time =====================================
+
+def set_speed(speed):
+ """
+ Sets program frame rate in frames per second
+ """
+ backend.set_frame_rate(speed)
+
+
+def update_when(event_type):
+ """
+ Event Types include
+ 'key_pressed'
+ 'mouse_clicked'
+ 'next_tick'
+ """
+ if event_type == "key_pressed":
+ return backend.update_when(event_type)
+ else: backend.update_when(event_type)
+
+def sleep(duration):
+ """
+ Sleeps for a length of time
+ """
+ backend.sleep(duration)
+
+
+# ============================== main =====================================
+
+if __name__ == "__main__":
+ """
+ If api.py is run independently, this runs the test suite
+ """
+ import doctest
+ doctest.testmod()