Ejemplo n.º 1
0
    def __init__(self, rect, font, scrollback=1000,
                 bg=(255, 165, 0)):
        image = pygame.Surface(rect.size)
        image.set_colorkey(bg)
        image.set_alpha(127)
        image.fill(bg)
        Sprite.__init__(self, rect, image)
        self.scrollback = scrollback
        self.buffer = [''] * scrollback
        self.font = font
        self.bg = bg
        self.linesize = linesize(font)
	self.numlines = int(self.rect.height / self.linesize) + 1
	self.currentcolor = (255, 255, 255)
	self.offset = 0
Ejemplo n.º 2
0
    def _get_image(self, **kwargs) -> Surface:
        # Parse keyword arguments.
        alpha = kwargs['alpha']
        color_edge = kwargs['color_edge']
        color_inside = kwargs['color_inside']
        radius = kwargs['radius']
        width = kwargs['width']

        # Create the image.
        image = Surface((radius * 2, radius * 2), pygame.SRCALPHA)
        pygame.gfxdraw.aacircle(image, radius, radius, radius, color_edge)
        pygame.gfxdraw.filled_circle(image, radius, radius, radius, color_edge)
        pygame.gfxdraw.aacircle(image, radius, radius, radius - width,
                                color_inside)
        pygame.gfxdraw.filled_circle(image, radius, radius, radius - width,
                                     color_inside)
        image.fill((255, 255, 255, alpha), None, pygame.BLEND_RGBA_MULT)

        return image
Ejemplo n.º 3
0
    def mouseon(self):
	# we want the ORT to behave like an exit
	if self.nick == 'ORT_Number_1':
	    self.server.whichmouseover('ov_tram_exit')
        if self.label is not None:
	    print self.nick, 'on'
	    self.label.die()
	
	# Strip color codes from the nick for display
	nicklist = list(self.nick)
	nick = ''
	avoid = list(string.digits)
	avoid.extend([',', '\x03'])
	found = 0
	for n in nicklist:
	    if n == '\x03':
		found = 1
	    if found == 1:
		try:
		    test = avoid.index(n)
		except:
		    found = 0
	    if found == 0:
		nick = nick + n
	
	sx, sy = _font.size(nick)
	s = _font.render(nick, 0, (255, 255, 255))
	image = pygame.Surface((sx+2, sy+2))
	image.set_colorkey((255,165,0))
	image.fill((255,165,0))
	for x in range(3):
	    for y in range(3):
	        image.blit(s, (x, y))
	image.blit(_font.render(nick, 1, (0,0,0)), (1, 1))
	image.set_alpha(127)
	x, y = self.rect.center
	nx, ny = self.noffset
	self.label = ClampingSprite((x+nx, y+ny), image, Rect(0, 0, 640, 480))
	self.group.add(self.label)
	return [self.label.rect]
Ejemplo n.º 4
0
def prepareUIIcons(color):
    for image in ui_icons.values():
        image.fill((0, 0, 0, 255), None, pygame.BLEND_RGBA_MULT)
        image.fill(color[0:3] + (0,), None, pygame.BLEND_RGBA_ADD)
Ejemplo n.º 5
0
    def render(self):
        """Render the text, returning the affected rect."""

	pad = 3
        maxdist = 200
        screen_rect = Rect(0, 0, 640, 480 - (linesize(_font)+2))
        size = linesize(_font)
        
        # Try not overlapping anything
        balloonrects = map(lambda s: s.rect, self.group.list)
        try: balloonrects.remove(self.rect)
        except ValueError: pass

        # First, try to avoid everything
        rects1 = invertrect(screen_rect, balloonrects +
                            map(lambda s: s.rect, self.avgroup.list))
        rects1.sort(self.nearer)

        # Next, try to avoid just balloons and my av
        rects2 = invertrect(screen_rect, balloonrects +
                            [self.avatar.rect])
        rects2.sort(self.nearer)

        # Finally, just avoid my own av
        rects3 = invertrect(screen_rect, [self.avatar.rect])
        rects3.sort(self.nearer)

        rects = filter(lambda r,p=self.pos,m=maxdist: rectdist(r,p) < m,
                       rects1 + rects2 + rects3) + [screen_rect]

        notdone = 1
        while notdone:
            # Loop until we can fit the balloon into *some* rect
            for r in rects:
                try: lines = wrap_lines(self.text, r.width-pad*2, _font)
                except ValueError: continue
                if len(lines) * size + pad*2 < r.height:
                    notdone = 0
                    break
            else:
                # Couldn't render the balloon, delete some lines
                del self.text[0]
                del self.timeouts[0]

        # Count the number of lines in each sequence of text
        surfaces = map(lambda t,f=_font,c=self.fgcolor: f.render(t, 1, c),
                       lines)
        width = max(map(lambda l: _font.size(l)[0], lines)) + (pad * 2)
        height = len(surfaces) * size + (pad * 2)

        x, y = self.pos
        rect = Rect(x-width/2, y-height/2, width, height).clamp(r)

        bigrect = rect.union((self.pos, (1, 1)))
        image = pygame.Surface(bigrect.size)
        image.set_colorkey((255,165,0))
        image.fill((255,165,0))

        cx, cy = closest(rect, self.pos)
        cx = cx - bigrect.left
        cy = cy - bigrect.top
        x = x - bigrect.left
        y = y - bigrect.top
        left = rect.left - bigrect.left
        right = rect.right - bigrect.left - 1
        top = rect.top - bigrect.top
        bottom = rect.bottom - bigrect.top - 1
        # Calculate arcs for corners
        nwarc = arc(pad, (left+pad, top+pad), pi*1.5, pi*2, 5)
        nearc = arc(pad, (right-pad, top+pad), 0, pi*0.5, 5)
        searc = arc(pad, (right-pad, bottom-pad), pi*0.5, pi, 5)
        swarc = arc(pad, (left+pad, bottom-pad), pi, pi*1.5, 5)
        
        # Go in a clockwise direction
        if x > right-pad*2:
            # Drawing to the east
            if y > bottom-pad*2:
                # Draw the arrow to the southeast
                points = ((right, bottom-pad), (x, y),
                          (right-pad, bottom)) + swarc + nwarc + nearc
            elif y < top+pad*2:
                # Draw the arrow to the northeast
                points = ((right-pad, top), (x, y),
                          (right, top+pad)) + searc + swarc + nwarc
            elif x > right:
                # Due east
                points = ((right, cy-pad), (x, y),
                          (right, cy+pad)) + \
                          searc + swarc + nwarc + nearc
            else:
                # Arrow is inside balloon
                points = searc + swarc + nwarc + nearc
        elif x < left+pad*2:
            # Drawing to the west
            if y > bottom-pad*2:
                # Southwest
                points = ((left+pad, bottom), (x, y),
                          (left, bottom-pad)) + \
                          nwarc + nearc + searc
            elif y < top+pad*2:
                # Northwest
                points = ((left, top+pad), (x, y),
                          (left+pad, top)) + \
                          nearc + searc + swarc
            elif x < left:
                # Due west
                points = ((left, cy+pad), (x, y),
                          (left, cy-pad)) + \
                          nwarc + nearc + searc + swarc
            else:
                # Arrow is inside balloon
                points = nwarc + nearc + searc + swarc
        elif y < top:
            # Due north
            points = ((cx-pad, top), (x, y), (cx+pad, top)) + \
                     nearc + searc + swarc + nwarc
        elif y > bottom:
            # Due south
            points = ((cx+pad, bottom), (x, y),
                      (cx-pad, bottom)) + \
                      swarc + nwarc + nearc + searc
        else:
            print >> sys.stderr, 'Oops, arrow point is inside balloon!'
            points = swarc + nwarc + nearc + searc

        pygame.draw.polygon(image, (255,255,255), points, 0)
        pygame.draw.polygon(image, (0,0,0), points, 1)
        y = rect.top - bigrect.top + pad
        x = rect.left - bigrect.left
        for s in surfaces:
            image.blit(s, (((width - s.get_width())/2+x), y))
            y = y + size

        self.image = image

        #rect = rect.clamp((0, 0, 640, 480))
        self.rect = bigrect

        return bigrect
Ejemplo n.º 6
0
    def run(self):
        while not self.terminated:
            try:
                framenb, motion = self.q.popleft()
                fdata = None
                with self.lock:
                    fdata, nbytes = self.decoder.decode_frame(
                        self.frame2decode)

                if fdata:
                    (frame, w, h, ls) = fdata
                    #print("=== w:%d h:%d ls:%d" % (w,h,ls))
                    if frame:
                        image = np.fromstring(frame, dtype=np.uint8)
                    else:
                        image = self.image
                        image.fill(220)
                        w = self.resx
                        h = self.resy

                    image = image.reshape((h, w, 3))
                    re = motion[0]
                    vv = motion[1]
                    mm = motion[2]

                    x0 = re[0]
                    y0 = re[1]
                    w = re[2]
                    h = re[3]
                    xmin = mm[0] * 16
                    ymin = mm[1] * 16
                    xmax = mm[2] * 16
                    ymax = mm[3] * 16
                    x1 = x0 + w
                    y1 = y0 + h
                    x0 *= 16
                    x1 *= 16
                    y0 *= 16
                    y1 *= 16

                    # center line
                    if self.ycross > 0:
                        cv2.line(image, (0, int(self.ycross)),
                                 (self.resx, int(self.ycross)), (0, 0, 0), 1)

                    if self.xcross > 0:
                        cv2.line(image, (int(self.xcross), 0),
                                 (int(self.xcross), self.resy), (0, 0, 0), 1)

                    if framenb < 0:
                        framenb = -framenb
                        #cv2.rectangle(image,(x0,y0),(x1,y1),(20,20,220),1)
                        cv2.rectangle(image, (xmin, ymin), (xmax, ymax),
                                      (20, 20, 220), 1)
                    else:
                        cv2.rectangle(image, (xmin, ymin), (xmax, ymax),
                                      (200, 200, 200), 1)
                        cv2.rectangle(image, (x0, y0), (x1, y1), (20, 220, 20),
                                      1)

                    txt = "%d" % (framenb / 2)
                    cv2.putText(image, txt, (int(x0), int(y0)),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (20, 220, 20),
                                1)
                    xm = int((x1 + x0) / 2)
                    ym = int((y1 + y0) / 2)
                    xe = int(xm - 4 * vv[0])
                    ye = int(ym - 4 * vv[1])
                    cv2.arrowedLine(image, (xm, ym), (xe, ye), (20, 220, 20),
                                    1)
                    #image = cv2.resize(image,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_LINEAR)
                    image = np.rot90(image, self.k)
                    imagepath = self.imgtemplate % self.nbimage
                    if self.nbimage > self.nimages:
                        self.nbimage = 0
                    else:
                        self.nbimage += 1
                    cv2.imwrite(imagepath, image,
                                [cv2.IMWRITE_JPEG_QUALITY, 90])
                    try:
                        fs = open(self.imgctrl_file, "w")
                        fs.write(imagepath)
                        fs.close()
                    except:
                        print("cannot write %s" % self.imgctrl_file)
                        pass
                    #pg.image.save(surface, self.imgpath)

                #- do garbage collection here!
                c0, c1, c2 = gc.get_count()
                t0, t1, t2 = gc.get_threshold()
                if c0 > t0 or c1 > t1 or c2 > t2:
                    gc.collect()

            except IndexError:
                self.event.clear()
                self.event.wait(1)
Ejemplo n.º 7
0
def prepareUIIcons(color):
    for image in ui_icons.values():
        image.fill((0, 0, 0, 255), None, pygame.BLEND_RGBA_MULT)
        image.fill(color[0:3] + (0, ), None, pygame.BLEND_RGBA_ADD)