def __init__(self, width, height, rows, columns, selection_color, grid_color, grid_width): super(HexCanvas, self).__init__() self.grid = hexgrid.Grid(hexgrid.OrientationFlat, hexgrid.Point(0, 0), hexgrid.Point(width, height), morton.Morton(2, 32)) self.hexes = [] for x in range(0, rows): for y in range(0, columns): self.hexes.append(hex_at(x, y)) lines_dict = {} # dictionary of Point() to Point()[] for h in range(0, len(self.hexes)): corners = self.grid.hex_corners(self.hexes[h]) for i in range(0, 6): if corners[i] not in lines_dict.keys(): lines_dict[corners[i]] = [] if not corners[(i + 1) % 6] in lines_dict[corners[i]]: if corners[(i + 1) % 6] not in lines_dict.keys() \ or corners[i] not in lines_dict[corners[(i + 1) % 6]]: lines_dict[corners[i]].append(corners[(i + 1) % 6]) with self.canvas: Color(selection_color[0], selection_color[1], selection_color[2], selection_color[3]) self.selection_mesh = Mesh(vertices=hex_vertices( self.grid, self.hexes[0]), indices=[0, 1, 2, 3, 4, 5], mode='triangle_fan') Color(grid_color[0], grid_color[1], grid_color[2], grid_color[3]) for key in lines_dict.keys(): lines = [] for point in lines_dict[key]: lines.append([key.x, key.y, point.x, point.y]) Line(points=lines, width=grid_width, cap='square')
def show_precincts(self): precinct_graphics = self.precinct_graphics = {} with self.canvas: PushMatrix() Translate(self.focus_region_width, 0) for precinct in self.voronoi_mapping.precincts: assert len(precinct.boundary) >= 6 tess = Tesselator() tess.add_contour(precinct.boundary) tess.tesselate(WINDING_ODD, TYPE_POLYGONS) graphics = [ Color(rgba=(0, 0, 0, 1))] for vertices, indices in tess.meshes: graphics.append( Mesh( vertices=vertices, indices=indices, mode="triangle_fan")) graphics.append( Color(rgba=(0, 1, 0, 1))) graphics.append( Line(points=precinct.boundary, width=1)) precinct_graphics[precinct] = graphics PopMatrix()
def render(self, matrix, color_transform, rendering_index, rendering_count, visible): if not visible: return bitmap = Mesh(vertices=self._vertices, indices=(1, 0, 2, 1, 2, 3), mode='triangle_fan', texture=self._texture) self._factory.render(bitmap, matrix, color_transform)
def _create_segment_mesh_object(self, segment, x_offset, y_offset): """Creates a Mesh vertex instruction for the specified segment number""" points = self._segment_points[segment] vertices = [] indices = [] for index in range(0, len(points), 2): vertices.extend( [points[index] + x_offset, points[index + 1] + y_offset, 0, 0]) indices.append(int(index / 2)) return Mesh(vertices=vertices, indices=indices, mode="triangle_fan")
def create_rect(pos, size, color=(1, 1, 1, 1), radius=0, format_c='rgba', correct_radius=True): min_size = min(*size) if correct_radius and min_size <= 2 * radius: radius = min_size / 2 texture = create_brush(color, format_c=format_c) if radius <= 0 or min_size < 2 * radius: return Rectangle(pos=pos, size=size, texture=texture) else: left_down_coner_center = pos[0] + radius, pos[1] + radius left_upper_coner_center = pos[0] + radius, pos[1] + size[1] - radius right_down_coner_center = pos[0] - radius + size[0], pos[1] + radius right_upper_coner_center = pos[0] - radius + size[0], pos[1] + size[ 1] - radius points = [] points += create_round_line(left_down_coner_center, radius, start_angle=270, end_angle=180, point_count=int(radius)) points += create_round_line(left_upper_coner_center, radius, start_angle=180, end_angle=90, point_count=int(radius)) points += create_round_line(right_upper_coner_center, radius, start_angle=90, end_angle=0, point_count=int(radius)) points += create_round_line(right_down_coner_center, radius, start_angle=360, end_angle=270, point_count=int(radius)) vertex = convert_points_tuples(points) return Mesh(vertices=vertex, mode='triangle_fan', texture=texture, indices=range(int(radius) * 4))
def _draw_widget(self, *args) -> None: """Establish the drawing instructions for the widget.""" del args if self.canvas is None: return self.canvas.clear() self._segment_colors = [] self._segment_mesh_objects = [] # Get the list of encoded characters (apply flash mask if applicable) encoded_characters = [ self._encoded_characters[index] & self._flash_character_mask[index] if self._apply_flash_mask else self._encoded_characters[index] for index in range(len(self._encoded_characters)) ] with self.canvas: Color(*self.background_color) Scale(self.scale, origin=self.center) Rotate(angle=self.rotation, origin=self.center) # Fill background Rectangle(pos=self.pos, size=self.size) x_offset = self.x + self.padding y_offset = self.y + self.padding for index, encoded_char in enumerate(encoded_characters): colors = [None] * (self._segment_count + 2) mesh_objects = [None] * self._segment_count for segment in range(self._segment_count): colors[segment] = self._create_segment_color( index, segment, encoded_char) mesh_objects[segment] = self._create_segment_mesh_object( segment, x_offset, y_offset) self._segment_colors.append(colors) self._segment_mesh_objects.append(mesh_objects) if self.dot_enabled: colors[ self._dot_segment_index] = self._create_segment_color( index, self._dot_segment_index, encoded_char) Ellipse(pos=(self._dot_points[0] + x_offset, self._dot_points[1] + y_offset), size=(self._dot_points[2], self._dot_points[2])) if self.comma_enabled: colors[self. _comma_segment_index] = self._create_segment_color( index, self._comma_segment_index, encoded_char) Mesh(vertices=[ self._comma_points[0] + x_offset, self._comma_points[1] + y_offset, 0, 0, self._comma_points[2] + x_offset, self._comma_points[3] + y_offset, 0, 0, self._comma_points[4] + x_offset, self._comma_points[5] + y_offset, 0, 0, self._comma_points[6] + x_offset, self._comma_points[7] + y_offset, 0, 0, self._comma_points[8] + x_offset, self._comma_points[9] + y_offset, 0, 0, self._comma_points[10] + x_offset, self._comma_points[11] + y_offset, 0, 0, self._comma_points[12] + x_offset, self._comma_points[13] + y_offset, 0, 0, self._comma_points[14] + x_offset, self._comma_points[15] + y_offset, 0, 0 ], indices=[0, 1, 2, 3, 4, 5, 6, 7], mode="triangle_fan") x_offset += self.char_width + self.character_spacing