Beispiel #1
0
 def blit(self, img, posn, anchor=NW):
     "Blit an image to the plot"
     if not isinstance(img, Image): img = Image(img)
     img.blitTo(self, self.coords(posn), anchor)
Beispiel #2
0
	def render(self):

		# Calculate metrics
		w, h = self.size
		px, hw = self.metrics
		if self._val <= self.min or self.min >= self.max: x = 0
		elif self._val >= self.max: x = self.pixels - hw
		else: x = round(self.pad + (self._val - self.min) / (self.max - self.min) * px)
		if self.vertical: x = self.pixels - 1 - x - hw

		# Background
		img = Image(self.size, self.bgColor)
		pygame.draw.rect(img.surface, self.bgColor, (0,0) + self.size)

		# Tick marks
		if self.ticks and self.step != None:
			n = round((self.max - self.min) / self.step)
			tick0 = self.pad + hw / 2
			dTick = px / n
			y = (w if self.vertical else h) // 2 - 1
			for i in range(n+1):
				tick = round(tick0 + i * dTick)
				p1 = (y, tick) if self.vertical else (tick, y)
				p2 = (y+1, tick) if self.vertical else (tick, y+1)
				pygame.draw.line(img.surface, self.borderColor, p1, p2, 1)
				tick += dTick

		# Handle
		if self.handle:
			r = pygame.Rect(((0, x) + (w, hw)) if self.vertical else (x, 0) + (hw, h))
			pygame.draw.rect(img.surface, self.fgColor, r)
			r1, r2 = r.bottomleft, r.topright
			if self.vertical:
				r1, r2 = r2, r1
			pygame.draw.line(img.surface, self.borderColor, r.topleft, r1, 1)
			pygame.draw.line(img.surface, self.borderColor, r2, r.bottomright, 1)
		elif self.vertical:
			pygame.draw.rect(img.surface, self.fgColor, (0, x) + (w, h - x))
		else:
			pygame.draw.rect(img.surface, self.fgColor, (0, 0) + (x, h))

		# Handle centre
		x += hw // 2
		self.handleDecor(x, img.surface, 1 if hw < 25 else 3)

		# Text
		txt = self.getValueText()
		if txt != None:
			txt = Image.text(txt, self.font, self.color)
			dx = hw // 2 + 8
			tw = txt.width
			x = x - tw - dx  if x + dx + tw + 4 > (h if self.vertical else w) else x + dx
			if self.vertical:
				txt = Image(pygame.transform.rotate(txt.surface, -90))
				x = (0, x)
			else: x = (x, 0)
			txt.blitTo(img.surface, x)

		# Finish up
		if self.border: img.borderInPlace(self.border, self.borderColor)
		self.imgs = {0: img}