def draw(self, canvas): Plato.draw(self, canvas) # draw a square #canvas.set_line_width(1.0) #canvas.set_source_rgba(0.7, 1.0, 0.7, 0.5) #canvas.rectangle(self.left, self.top, self.width, self.height) #canvas.fill() Primitives.rounded_rectangle(canvas, self.left, self.top, self.width, self.height) # some numbers l1 = str((int(self.x),int(self.y))) l2 = 'local:' + str((int(self.width),int(self.height))) l3 = 'world:' + str((int(self.world_width),int(self.world_height))) l4 = 'rot:' + str(self.rotation) canvas.set_source_rgb(0,0,0) canvas.select_font_face("Monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) canvas.set_font_size(16.0) canvas.move_to(self.left+10,self.top + 30.0) canvas.show_text(l1) canvas.move_to(self.left+10,self.top + 60.0) canvas.show_text(l2) canvas.move_to(self.left+10,self.top + 90.0) canvas.show_text(l3) canvas.move_to(self.left+10,self.top + 120.0) canvas.show_text(l4)
def __init__(self, x, y): Plato.__init__(self, x, y, 40, 40) #self.x = x #self.y = y #self.width = 40 #self.height = 40 #self.rotation = 0.0 self.padding = 0.0 self.can_drag = True self.datapoints = {} self.numdatapoints = 1000 self.object = None self.variable = None # ignore workspace scaling, translation and rotation self.lock_scale = False self.lock_position = False self.lock_rotation = False self.skip_size = 5 self.skip_count = self.skip_size self.min = 0 self.max = 100
def draw(self, context): Plato.draw(self, context) # init sprites if self.buf == None: self.buf = self._make_buffer(context) # border context.set_line_width(1.0) context.set_source_rgb(0, 0, 0) context.rectangle(self.left, self.top, self.width, self.height) context.stroke() # reset color #self.color = lambda context : context.set_source_rgb(1.0, 1.0, 1.0) # fast grid inc_x = (self.width / self.resolution_x) inc_y = (self.height / self.resolution_y) for x in range(self.resolution_x): for y in range(self.resolution_y): cell = self.grid[x*y] if cell != 0: pos_x = self.left + (x * inc_x) #+ (inc_x / 2.0) pos_y = self.top + (y * inc_y) #+ (inc_y / 2.0) self.colors[cell](context) context.rectangle(pos_x, pos_y, inc_x, inc_y) #context.set_source_surface(self.buf, pos_x, pos_y) #context.paint() self.grid[x*y] = self.fast_random() #self.grid[x*y] = random.randint(0, 2) context.fill()
def draw(self, canvas): Plato.draw(self, canvas) canvas.set_line_width(1.0) canvas.set_source_rgba(1.0, 0.7, 0.7, 0.5) canvas.arc(0.0, 0.0, self.width / 2.0, -math.pi, math.pi) canvas.fill()
def swallow(self, body): #print 'Reactor swallowing a body:', body.__class__ if body.__class__ is Rotor: # TODO ugly hack print "create_rotor" self.rotor = self.create_rotor(0, 0, 5, 150) else: Plato.swallow(self, body)
def draw(self, canvas): #Primitives.rounded_rectangle(canvas, self.left, self.top, self.width, self.height) Plato.draw(self, canvas) # draw a square canvas.set_line_width(1.0) canvas.set_source_rgba(0.7, 0.7, 1.0, 0.5) canvas.rectangle(self.left, self.top, self.width, self.height) canvas.stroke()
def __init__(self, x, y, width = 150.0, height = 150.0): Plato.__init__(self, x, y, width, height) self.buf = None self.color = lambda context : context.set_source_rgb(1.0, 1.0, 1.0) self.particle_radius = 10.0 r = 50 resolution_x = r resolution_y = r resolution = resolution_x * resolution_y num_particles = 500 self.field = glug.VectorField(resolution_x, resolution_y, num_particles) #self.field.diffusion = 0.01 #self.field.viscosity = 0.01 for particle in self.field.particles(): (particle.position_x, particle.position_y) = \ (random.randint(self.left, self.right-self.particle_radius), \ random.randint(self.top, self.bottom-self.particle_radius))
def __init__(self, x, y, width = 30.0, height = 30.0): Plato.__init__(self, x, y, width, height) self.color = lambda context : context.set_source_rgb(1.0, 1.0, 1.0) self.resolution_x = 20 self.resolution_y = 20 self.resolution = self.resolution_x * self.resolution_y #self.grid = [[0 for col in range(resolution)] for row in range(resolution)] self.grid = [0 for cell in range(self.resolution)] self.noise_size = self.resolution * 10 self.noise = [random.randint(0, 2) for i in range(self.noise_size)] self.noise_count = 0 self.colors = [ lambda context : context.set_source_rgb(0.0, 1.0, 0.0), lambda context : context.set_source_rgb(1.0, 0.0, 0.0), lambda context : context.set_source_rgb(0.0, 0.0, 1.0) ] self.buf = None
def draw(self, canvas): Plato.draw(self, canvas) canvas.set_line_width(1.0) canvas.set_source_rgb(0.0, 0.0, 0.0) canvas.rectangle(self.left, self.top, self.width, self.height) canvas.fill() #self.rotation += 1.0 #if self.rotation >= 360.0: # self.rotation = 0.0 #def set_newton(self, newton): # self._newton = newton # if self._newton is None: # print "body.set_newton() - invalid newton" # return #self.ode_init()
def __init__(self, x, y, w, h): Plato.__init__(self, x, y, w, h) # set up physics engine #print 'creating world' self._ode_world = ode.World() #self._ode.setGravity( (0,0.1,0) ) #self._ode_space = ode.Space(type=1) # hashspace self._ode_space = ode.QuadTreeSpace((0,0,0),(1,1,1),4) # create walls #self.ode_floor = ode.GeomPlane(self.ode_space, (0, -1, 0), -TheWorld.unit_range) #self.ode_wall_right = ode.GeomPlane(self.ode_space, (-1, 0, 0), -TheWorld.unit_range) #self.ode_wall_left = ode.GeomPlane(self.ode_space, (1, 0, 0), -TheWorld.unit_range) #self.ode_celing = ode.GeomPlane(self.ode_space, (0, 1, 0), -TheWorld.unit_range) # contact group for collision detection self._ode_contactgroup = ode.JointGroup() # jointgroup for constraining movement to 2d, all moving objects self._ode_2dgroup = ode.JointGroup() # motor group self._ode_motorgroup = ode.JointGroup() # jointgroup for fixed objects - e.g. walls, inputs outputs self._ode_anchorgroup = ode.JointGroup() self._ode_bodies = [] self._remove_list = [] # for operations on bodies not associated with in/outputs self._add_list = [] self.__particlecount_by_type = {} self.collision_lock = False
def __init__(self, text, x = 0.0, y = 0.0): Plato.__init__(self, x, y, 0.0, 0.0) # zoomin' self.lock_zoom = False # font properies - derived from css properties self.font_family = "Monospace" self.font_size = 10 # selection self.selection = (0, 0) # other properties self.margin = 5.0 self.border_width = 1.0 self.border_radius = 10.0 # in the case of rounded borders self.color = (0.0, 0.0, 0.0, 1.0) self.border_color = (1.0, 0.0, 0.0, 1.0) self.background_color = (0.7, 0.7, 1.0, 0.5) self.selection_background = 'background="gray"' self.selection_foreground = '' # 'color="white"' self.__text = text self.cursorindex = 0 self.pixel_width = 0.0 self.pixel_height = 0.0 # create text layout self.layout = pango.Layout(TheWorld.pango_context) fontDescription = pango.FontDescription(self.font_family + " " + str(self.font_size)) self.layout.set_font_description(fontDescription) self.layout.set_markup('<span>' + text + '</span>') # calc text metrics self.recalc_text_size()
def draw(self, context): Plato.draw(self, context) # render text border if self.lock_zoom: context.scale(self.world_width / (self.world_width * TheWorld.scale), self.world_height / (self.world_height * TheWorld.scale)) self.draw_background(context, self.left, self.top, self.width, self.height) # render the text & cursor context.set_source_rgba(self.color[0], self.color[1], self.color[2], self.color[3]) context.move_to(self.left, self.top) context.save() context.identity_matrix() if not self.lock_zoom: # todo - handle scaling when embedded too context.scale(TheWorld.scale, TheWorld.scale) if not self.lock_rotation: context.rotate(math.radians(self.rotation) + TheWorld.rotate) context.show_layout(self.layout) self.draw_cursor(context) context.restore()
def _get_y(self): # world if self._ode_geom is None: return Plato._get_y(self) return Body._get_y(self)
def draw(self, canvas): Plato.draw(self, canvas) canvas.set_line_width(1.0) canvas.set_source_rgba(0.7, 1.0, 0.7, 0.5) Primitives.circle(canvas, 0.0, 0.0, self.width / 2.0)
def swallow(self, body): print 'swallowing a body:', body.__class__ body.set_newton(self) Plato.swallow(self, body)
def __init__(self, x, y, w, h): Plato.__init__(self,x, y, w, h) self.is_toolbox = True
def __init__(self, x, y): Plato.__init__(self, x, y, SIZE, SIZE)
def __init__(self, x, y, w, h): Plato.__init__(self, x, y, w, h) Body.__init__(self, None)
def __init__(self, x, y): Plato.__init__(self, x, y, WIDTH, LENGTH) Body.__init__(self)
def _set_rotation(self, value): if self._ode_geom is None: return Plato._set_rotation(self, value) return Body._set_rotation(self, value)
def __init__(self, x, y, w, h): Plato.__init__(self, x, y, w, h) self.input_buffer = [] self.output_buffer = []
def __init__(self, x, y, w, h): Plato.__init__(self,x, y, w, h)
def draw(self, context): Plato.draw(self, context) now_time = time.time() if self.__reactor_output is not None and self.render_valves: x,y,z = self.__reactor_output.getPosition() w = self.__reactor_output.width h = self.__reactor_output.height context.set_line_width(0.5) #context.move_to(x+r,y) context.set_source_rgb(1.0, 0.0, 0.0) #context.arc(x, y, r, 0, math.pi * 2.0) context.rectangle(x-(w/2), y-(h/2), w, h) context.fill() # draw input r = 10.0 context.move_to(self.input_x + r, self.input_y) context.set_source_rgb(0.0, 1.0, 0.0) context.arc(self.input_x, self.input_y, r, 0, math.pi * 2.0) context.fill() # draw lightweight bodies count = 0 for body in self._ode_bodies: # check for out of bound particles fudge = 60.0 # for reactor outlets if self.bounds_check: (x,y) = body.position if x < self.left-fudge or x > self.right+fudge or y < self.top or y > self.bottom: o = body.particle_radius * 2.0 #print 'out of bounds:', body.position if x > 0.0: newx = min(self.right-o, x) else: newx = max(self.left+o, x) if y > 0.0: newy = min(self.bottom-o, y) else: newy = max(self.top+o, y) body.position = (newx, newy) # invert and brake motion vector (vx, vy) = body.velocity body.velocity = (-vx/4.0, -vy/4.0) # check particle age body_age = now_time - body.create_time # render body body.draw(context) # render rotor if self.__rotor is not None: self._ode_align_z_axis(self.__rotor) # restrict rotation to 2D rotor = self.__rotor x,y,z = rotor.getPosition() R = rotor.getRotation() # convert rotation matrix to euler angle attitude = math.asin(R[3]); r = math.acos((R[0] + R[4] + R[8] - 1.0) / 2.0) #r = MathLib.matrix_to_euler(R) if math.degrees(attitude) < 0: r = math.radians((180.0 - math.degrees(r)) + 180.0) context.save() context.translate(x,y) context.rotate(r) context.set_source_rgb(0.0,0.0,0.0) context.rectangle(-rotor.w/2.0, -rotor.h/2.0, rotor.w, rotor.h) context.fill() context.restore() # render walls if self.render_walls: for wall in self.__ode_walls: x,y,z = wall.getPosition() context.set_source_rgb(0.0, 1.0, 0.0) context.rectangle(x-(wall.w/2.0), y-(wall.h/2.0), wall.w, wall.h) context.fill() # tank #Primitives.rounded_rectangle_bling(context, self.left, self.top, self.width, self.height, r = 10, padding = 0.0) # decorations for properties if self.mouse_over: if self.mouse_over_property: context.set_source_rgb(1.0, 0.0, 0.0) else: context.set_source_rgb(0.0, 0.0, 0.0) context.move_to(0.0 - (self.width/6.0), self.top - self.decorator_size*2.0) context.show_text(str(self.num_particles) + ' particles') context.set_line_width(4.0) context.move_to(0.0 - self.decorator_size, self.top) context.arc(0.0, self.top, self.decorator_size, -math.pi, 0.0) context.stroke()
def _get_position(self): if self._ode_geom is None: return Plato._get_position(self) return Body._get_position(self)
def draw(self, context): Plato.draw(self, context) # set up fonts context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) context.set_font_size(6) context.set_source_rgb(0.0, 0.0, 0.0) # ticks num_ticks = 5 overlap = 3.0 context.set_line_width(0.5) context.set_source_rgb(0.5,0.5,0.5) for i in range(-num_ticks, num_ticks, 1): inc = (TheWorld.unit_range / num_ticks) * i # horizontal context.move_to(-TheWorld.unit_range - overlap, inc) context.line_to(TheWorld.unit_range, inc) context.stroke() context.move_to(-TheWorld.unit_range - (overlap * 5.0), inc + 3.0) value = abs(i - num_ticks) value = ((self.max/2) / num_ticks) * value context.show_text(str(value)) # vertical context.move_to((TheWorld.unit_range / num_ticks) + inc, -TheWorld.unit_range) context.line_to((TheWorld.unit_range / num_ticks) + inc, TheWorld.unit_range + overlap) context.stroke() context.move_to((TheWorld.unit_range / num_ticks) + inc - 5.0, TheWorld.unit_range + (overlap * 3.0)) value = (abs(i + num_ticks) + 1.0) / 2.0 # normalize value = (self.numdatapoints / num_ticks) * value # relative to number of samples value = value / TheSpaceTime.ticks_per_second # relative to time - TODO NOT ACCURATE!! Measures relative to target ticks not actual value = int(value) context.show_text(str(value) + "s") # background #x1 = -TheWorld.unit_range #y1 = -TheWorld.unit_range #x2 = TheWorld.unit_range #y2 = TheWorld.unit_range #cx1 = -TheWorld.unit_range / 4.0 #cy1 = -TheWorld.unit_range #cx2 = TheWorld.unit_range / 4.0 #cy2 = TheWorld.unit_range #context.set_source_rgba(0.9, 0.9, 0.9, 0.5) #context.move_to(x1, 0) #context.curve_to(cx1, y1, cx2, cy2, TheWorld.unit_range, 0); #context.line_to(x2, y1) #context.line_to(x1, y1) #context.close_path() #context.fill() #context.set_source_rgba(0.9, 0.9, 0.9, 0.7) #context.move_to(x1, 0) #context.curve_to(cx1, y1, cx2, cy2, TheWorld.unit_range, 0); #context.line_to(x2, y2) #context.line_to(x1, y2) #context.close_path() #context.fill() context.rectangle(-TheWorld.unit_range, -TheWorld.unit_range, TheWorld.unit_range*2, TheWorld.unit_range*2) context.set_source_rgb(0,0,0) context.stroke() # axis #context.set_source_rgba(0.0, 0.0, 0.0, 0.4) #context.move_to(-TheWorld.unit_range, 0.0) #context.line_to(TheWorld.unit_range, 0.0) #context.stroke() # plot the data for colour in self.datapoints.iterkeys(): x = -TheWorld.unit_range y = 0 data = self.datapoints[colour] if len(data) > 0: y = ((data[0] * TheWorld.unit_range) / (self.max/2)) + TheWorld.unit_range y = min(y, TheWorld.unit_range) context.move_to(x, y) for i in data: i *= -TheWorld.unit_range # invert and scale y axis i /= (self.max/2) i += TheWorld.unit_range i = max(i, -TheWorld.unit_range) context.line_to(x, i) x += ((TheWorld.unit_range*2.0) / (self.numdatapoints - 1)) context.set_line_width(0.5) context.set_source_rgb(colour[0],colour[1],colour[2]) context.stroke()
def _get_world_height(self): if self._ode_geom is None: return Plato._get_world_height(self) # world return Body._get_world_height(self)
def _set_world_height(self, value): if self._ode_geom is None: return Plato._set_world_height(self, value) return Body._set_world_height(self, value)
def _get_rotation(self): # local -> relative to top ? if self._ode_geom is None: return Plato._get_rotation(self) return Body._get_rotation(self)
def draw(self, context): t1 = time.time() # BEWARE: properties are dog slow! -> TODO check rest of system's inner loops for same # cache property lookups (width, height, left, right, top, bottom) = (self.width, self.height, self.left, self.right, self.top, self.bottom) # a constant flow # TODO - slow random numbers x = 1 s = 10 for y in range(10, 20, 4): #(self.resolution_y / 2.0), 12, 4): element = self.field.element(x, y) element.u = 1.0 element.v = 0.2 element.density = 1.0 self.field.update_element(element) #y = self.field.resolution_y - 3 #for x in range(10, self.field.resolution_x-3, 25): # e = self.field.element(x, y) # e.u = 0.1 # e.v = -2.5 # e.density = 5.0 # self.field.update_element(e) # update vector field & particles #self.field.update_velocity() #self.field.update_density() #self.field.update_particles(width, height); self.field.step(width, height); t2 = time.time() Plato.draw(self, context) # init sprites if self.buf == None: self.buf = self._make_buffer(context) # draw background context.set_line_width(1.0) context.set_source_rgb(0.90,0.90,1.0) context.rectangle(left, top, width, height) context.fill() context.set_source_rgb(0.0,0.0,1.0) context.rectangle(left, top, width, height) context.stroke() if True: # draw vector field inc_x = (width / self.field.resolution_x) inc_y = (height / self.field.resolution_y) count = 0 for element in self.field.render_elements(0.01): count = count + 1 if True: # density pos_x = left + (element.x * inc_x) pos_y = top + (element.y * inc_y) c = 0.9 - min(0.9, abs(element.density)) context.set_source_rgb(c, c, 1.0) context.rectangle(pos_x, pos_y, inc_x, inc_y) context.fill() if True: # flow velocity vectors pos_x = left + (element.x * inc_x) pos_y = top + (element.y * inc_y) (vx, vy) = (element.u * 50.0, element.v * 50.0) (x1, y1) = (pos_x + (inc_x/2), pos_y + (inc_y/2)) (x2, y2) = (x1 + (vx), y1 + (vy)) context.set_line_width(0.2) context.set_source_rgb(1.0,0.0,0.0) context.move_to(x1, y1) context.line_to(x2, y2) context.stroke() #print "rendered: %d elements" % count if True: # draw particles context.save() context.scale(2.0/self.particle_radius,2.0/self.particle_radius) # higher res particles please for particle in self.field.particles(): (x,y) = (particle.position_x, particle.position_y) context.set_source_surface(self.buf, x*(self.particle_radius/2.0), y*(self.particle_radius/2.0)) context.paint() #context.rectangle(x, y, self.particle_radius/2.0, self.particle_radius/2.0) #context.stroke() context.restore() print 'update:', t2 - t1, 'draw:', time.time() - t2, 'total:', time.time() - t1
def draw(self, canvas): Plato.draw(self, canvas)