def setup(self, *, rect: Rect = None, screen_num: int = None): screens = ui.screens() # each if block here might set the rect to None to indicate failure if rect is not None: try: screen = ui.screen_containing(*rect.center) except Exception: rect = None if rect is None and screen_num is not None: screen = screens[screen_num % len(screens)] rect = screen.rect if rect is None: screen = screens[0] rect = screen.rect self.rect = rect.copy() self.screen = screen self.img = None if self.mcanvas is not None: self.mcanvas.close() self.mcanvas = canvas.Canvas.from_screen(screen) if self.active: self.mcanvas.register("draw", self.draw) self.mcanvas.freeze() self.columns = int(self.rect.width // self.field_size) self.rows = int(self.rect.height // self.field_size)
def draw(self, canvas): paint = canvas.paint # for other-screen or individual-window grids canvas.translate(self.rect.x, self.rect.y) canvas.clip_rect( Rect(-self.field_size * 2, -self.field_size * 2, self.rect.width + self.field_size * 4, self.rect.height + self.field_size * 4)) crosswidth = 6 def draw_crosses(): for row in range(1, self.rows): for col in range(1, self.columns): cx = self.field_size * col cy = self.field_size * row canvas.save() canvas.translate(0.5, 0.5) canvas.draw_line(cx - crosswidth + 0.5, cy, cx + crosswidth - 0.5, cy) canvas.draw_line(cx, cy + 0.5, cx, cy + crosswidth - 0.5) canvas.draw_line(cx, cy - crosswidth + 0.5, cx, cy - 0.5) canvas.restore() superblock_size = len(string.ascii_lowercase) * self.field_size colors = ["000055", "665566", "554444", "888855", "aa55aa", "55cccc" ] * 100 num = 1 self.superblocks = [] skipped_superblock = self.default_superblock + 1 if int(self.rect.height) // superblock_size == 0 and int( self.rect.width) // superblock_size == 0: skipped_superblock = 1 for row in range(0, int(self.rect.height) // superblock_size + 1): for col in range(0, int(self.rect.width) // superblock_size + 1): canvas.paint.color = colors[(row + col) % len(colors)] + hx( self.bg_transparency) #canvas.paint.color = "ffffff" canvas.paint.style = Paint.Style.FILL blockrect = Rect(col * superblock_size, row * superblock_size, superblock_size, superblock_size) blockrect.right = min(blockrect.right, self.rect.width) blockrect.bot = min(blockrect.bot, self.rect.height) canvas.draw_rect(blockrect) if skipped_superblock != num: # attempt to change backround color on the superblock chosen #canvas.paint.color = colors[(row + col) % len(colors)] + hx(self.bg_transparency) canvas.paint.color = setting_superblock_background_color.get( ) + hx(self.bg_transparency) canvas.paint.style = Paint.Style.FILL blockrect = Rect(col * superblock_size, row * superblock_size, superblock_size, superblock_size) blockrect.right = min(blockrect.right, self.rect.width) blockrect.bot = min(blockrect.bot, self.rect.height) canvas.draw_rect(blockrect) canvas.paint.color = setting_superblock_stroke_color.get( ) + hx(self.bg_transparency) canvas.paint.style = Paint.Style.STROKE canvas.paint.stroke_width = 5 blockrect = Rect(col * superblock_size, row * superblock_size, superblock_size, superblock_size) blockrect.right = min(blockrect.right, self.rect.width) blockrect.bot = min(blockrect.bot, self.rect.height) canvas.draw_rect(blockrect) #drawing the big number in the background canvas.paint.style = Paint.Style.FILL canvas.paint.textsize = int(superblock_size * 4 / 5) text_rect = canvas.paint.measure_text(str(num))[1] #text_rect.center = blockrect.center text_rect.x = blockrect.x text_rect.y = blockrect.y canvas.paint.color = setting_large_number_color.get() + hx( self.bg_transparency) canvas.draw_text(str(num), text_rect.x, text_rect.y + text_rect.height) self.superblocks.append(blockrect.copy()) num += 1 def draw_text(): canvas.paint.text_align = canvas.paint.TextAlign.CENTER canvas.paint.textsize = 17 for row in range(0, self.rows + 1): skip_it = row % 2 == 0 for col in range(0, self.columns + 1): skip_it = not skip_it center = Point2d( col * self.field_size + self.field_size / 2, row * self.field_size + self.field_size / 2, ) if not skip_it or not self.checkers: text_string = f"{letters[row % len(letters)]}{letters[col % len(letters)]}" text_rect = canvas.paint.measure_text(text_string)[1] background_rect = text_rect.copy() background_rect.center = Point2d( col * self.field_size + self.field_size / 2, row * self.field_size + self.field_size / 2, ) background_rect = background_rect.inset(-4) if (self.input_so_far.startswith( letters[row % len(letters)]) or len(self.input_so_far) > 1 and self.input_so_far.endswith( letters[col % len(letters)])): canvas.paint.color = setting_row_highlighter.get( ) + hx(self.label_transparency) else: canvas.paint.color = setting_letters_background_color.get( ) + hx(self.label_transparency) canvas.paint.style = Paint.Style.FILL canvas.draw_rect(background_rect) canvas.paint.color = setting_small_letters_color.get( ) + hx(self.label_transparency) #paint.style = Paint.Style.STROKE canvas.draw_text( text_string, col * self.field_size + self.field_size / 2, row * self.field_size + self.field_size / 2 + text_rect.height / 2) def draw_rulers(): for (x_pos, align) in [(-3, canvas.paint.TextAlign.RIGHT), (self.rect.width + 3, canvas.paint.TextAlign.LEFT)]: canvas.paint.text_align = align canvas.paint.textsize = 17 canvas.paint.color = "ffffffff" for row in range(0, self.rows + 1): text_string = letters[row % len(letters)] + "_" text_rect = canvas.paint.measure_text(text_string)[1] background_rect = text_rect.copy() background_rect.x = x_pos background_rect.y = row * self.field_size + self.field_size / 2 + text_rect.height / 2 canvas.draw_text(text_string, background_rect.x, background_rect.y) for y_pos in [-3, self.rect.height + 3 + 17]: canvas.paint.text_align = canvas.paint.TextAlign.CENTER canvas.paint.textsize = 17 canvas.paint.color = "ffffffff" for col in range(0, self.columns + 1): text_string = "_" + letters[col % len(letters)] text_rect = canvas.paint.measure_text(text_string)[1] background_rect = text_rect.copy() background_rect.x = col * self.field_size + self.field_size / 2 background_rect.y = y_pos canvas.draw_text(text_string, background_rect.x, background_rect.y) #paint.color = "00ff004f" #draw_crosses() paint.color = "ffffffff" #paint.stroke_width = 1 #paint.color = "ff0000ff" draw_text() if self.rulers: draw_rulers()