def render(self): "Render the vector as an arrow on a pygame.Surface" l = self.length() * self.canvas.unit if l < 2: return Image((1, 1), self.stroke).image shape = self.arrowShape if type(shape) is dict: if shape["fixed"]: shape = shape.copy() shape["width"] /= l shape["head"] /= l del shape["fixed"] a = Arrow(l, **shape).config(fill=self.fill, stroke=self.stroke, weight=self.weight) else: dx, dy = shape cv = Canvas((l, 2 * dy)) y = cv.center[1] cv += Line((0, y), (l, y)).config(stroke=self.stroke, weight=self.weight) cv += Line((l - dx, y - dy), (l, y)).config(stroke=self.stroke, weight=self.weight) cv += Line((l - dx, y + dy), (l, y)).config(stroke=self.stroke, weight=self.weight) a = cv.snapshot() return a.image
def circles(sk, n=50): "Draw random circles for arena floor" size = sk.size cv = Canvas(size, "white") for i in range(n): r = randint(10, size[0]/8) cv += Circle(r).config(weight=0, fill=rgba(True), pos=randPixel(size)) return cv.snapshot()
def field(self): "Draw red and yellow goal boxes on green turf" w, h = self.size y = self.center[1] sz = boxSize(w, h) cv = Canvas((w, h), "#20b050") cv += Image(sz, "red").config(pos=(0, y), anchor=LEFT) cv += Image(sz, "yellow").config(pos=(w-1, y), anchor=RIGHT) return cv.snapshot()
def render(self): y = self._xy[1] cv = self.canvas u = cv.units w = round(abs(u[0] * self._barWidth)) h = round(abs(u[1] * y)) wt = round(self.weight) img = Canvas((w, h + 1), self.fill).config(weight=wt, border=self.stroke) return img.snapshot().original
def quilt(sk): "Draw some colors on the floor in a quilt pattern" w, h = sk.size w = (w - 64) // 6 h = (h - 64) // 4 cv = Canvas(sk.size, "grey") c = [ "pink", "darkgreen", "mintcream", "gold", "white", "cyan", "yellow", "blue", "brown", "tan2", "royalblue", "steelblue", "violet", "orange", "skyblue", "black", "tomato", "seashell", "salmon", "turquoise", "red", "magenta", "purple", "green", ] shuffle(c) for i in range(4): for j in range(6): color = c[j + 6 * i] cv += Image( (w, h), color).config(pos=(32 + (j + 0.5) * w, 32 + (i + 0.5) * h)) return cv.snapshot()
def title(self, title, padding=4, **kwargs): "Add a title bar" txtConfig = dict(font=Font.sans(), fontSize=15, fontStyle=BOLD, color="white", padding=padding) txtConfig.update(kwargs) title = Text(title).config(**txtConfig) cv = Canvas((self.width, title.height + self.weight), self.border) cv += title.config(pos=(cv.center[0], self.weight), anchor=TOP) self.insertTop(cv, 0, "TitleBar") return self
def buttons(cfg): "Create a canvas containing two buttons" cv = Canvas((256, 48)) # Selectable button btn1 = Button((120, 48)).textIcon("Selectable\nButton", True) cv["Selectable"] = btn1.config(anchor=TOPLEFT).bind(onaction=buttonClick) # Non-selectable button btn2 = Button(btn1.size, 2).textIcon("Popup Menu").bind(onaction=buttonClick) cv["Popup"] = btn2.config(pos=(255, 0), anchor=TOPRIGHT) # Configure button text btn1[-1].config(color=BLUE, align=CENTER, **cfg) btn2[-1].config(**cfg) return cv
def setup(sk): # Create a Canvas as a GUI dialog cv = Canvas((384, 256)).config(bg="#f0f0ff", weight=1) # Vertical positioning 16 pixels below last item added down = lambda cv: 16 + cv[-1].height text = dict(color=BLUE, font=FONT, fontStyle=BOLD, padding=4) if TextInputCanvas: # v2.2.dev ti = TextInputCanvas(336, "", "Type Some Text...", **text) else: # v2.0-2.1 ti = TextInput("", "Type Some Text...").config(**text) x, y = cv.center[0] - 8, 16 cv["Input"] = ti.bind(onaction, onchange=onaction).config(anchor=TOP, pos=(x, y), bg="white", weight=1) # Add a Radio box y += down(cv) cfg = {"font": FONT, "fontSize": 14} text = "Option A", "Option B", "Option C" box = Radio(text, txtConfig=cfg).bind(onchange=radioChange) cv["ABC"] = box.config(pos=(x, y), anchor=TOP, selected=1) # Add an Options box y += down(cv) text = "Option X", "Option Y", "Option Z" box = Options(text, txtConfig=cfg).config(pos=(x, y), anchor=TOP) cv["XYZ"] = box.bind(onaction=optionsChange) # Add Buttons y += down(cv) cv["Button Box"] = buttons(cfg).config(anchor=TOP, pos=(x, y)) # Modify canvas and sketch size based on content cv.resize((cv.width, y + down(cv)), False) w, h = cv.size sk.size = w + 48, h + 48 # Add a Slider slider = Slider((16, cv.height), [BLUE], 100, 0, 100) slider.config(pos=(cv.width, 0), anchor=TOPRIGHT, bg=GREY, weight=1) cv["Slider"] = slider.bind(onchange=sliderChange) # Create a popup menu img = Image.fromBytes(sc8prData("alien")) items = [("Action", img, None), ("Back", None, R_TRIANGLE)] cv.menu = Menu(items, txtConfig=cfg).config(pos=cv.center) cv.menu.bind(onaction=menuAction) # Add the dialog to the sketch sk["Dialog"] = cv.bind(resize=nothing).config(pos=sk.center)
def setup(sk): # Create a Canvas as a GUI dialog cv = Canvas((384,256)).config(bg="#f0f0ff", weight=1) # Vertical positioning 12 pixels below last item added down = lambda cv: 16 + cv[-1].height # Add a TextInput x, y = cv.center[0], 16 cv["Input"] = TextInput("", "Type Some Text...").config(anchor=TOP, font=FONT, fontStyle=BOLD, pos=(x,y), color=BLUE, bg="white", padding=4).bind(onaction) # Add a Radio box y += down(cv) cfg = {"font":FONT, "fontSize":14} text = "Option A", "Option B", "Option C" radio = Radio(text, txtConfig=cfg).bind(onchange=radioChange) cv["ABC"] = radio.config(pos=(x,y), anchor=TOP, selected=1) # Add an Options box y += down(cv) text = "Option X", "Option Y", "Option Z" radio = Options(text, txtConfig=cfg).config(pos=(x,y), anchor=TOP) cv["XYZ"] = radio.bind(onaction=optionsChange) # Add Buttons y += down(cv) cv["Button Box"] = buttons(cfg).config(anchor=TOP, pos=(x,y)) # Modify canvas and sketch size based on content cv.resize((cv.width, y + down(cv)), False) w, h = cv.size sk.size = w + 48, h + 48 # Add a Slider slider = Slider((16, cv.height), BLUE, 100, 0, 100) slider.config(pos=(cv.width, 0), anchor=TOPRIGHT, bg=GREY, weight=1) cv += slider.bind(onchange=sliderChange) # Create a popup menu img = Image.fromBytes(sc8prData("alien")) items = [("Action", img, None), ("Back", None, R_TRIANGLE)] cv.menu = Menu(items, txtConfig=cfg).config(pos=cv.center) cv.menu.bind(resize=nothing, onaction=menuAction) # Add the dialog to the sketch cv.cover = sk.cover() sk["Dialog"] = cv.bind(resize=nothing).config(pos=sk.center)
def snapshot(self): self._bg = self._options[self._status] return Canvas.snapshot(self)
def draw(self, srf=None, mode=3): self._bg = self._options[self._status] return Canvas.draw(self, srf, mode)