def __init__(self, model: CPU, width: int, height: int): """Create a view width x height""" self.width = width self.height = height self.model = model model.register_listener(self) model.memory.register_listener(self) self.window = graphics.graphics.GraphWin("Duck Machine", width, height) # CPU in left 1/3 of window cpu_region = Rectangle(Point(5, 5), Point(0.3 * self.width - 5, self.height - 5)) cpu_region.setFill("#fafaff") cpu_region.draw(self.window) # 16 registers, whose display we'll keep in a list, # with display as 8 rows of 2 columns self.registers = [] self._draw_registers() # Current instruction word above registers, in top # 1/5 of window instr_word_display = Rectangle( Point(10, 10), Point(0.3 * self.width - 10, 0.2 * self.height - 5)) instr_word_display.draw(self.window) self._draw_instruction(instr_word_display) # Memory in right 2/3 of window self._draw_memory()
def _draw_instruction(self, in_rect): x_center = (in_rect.p1.x + in_rect.p2.x) / 2.0 height = in_rect.p2.y - in_rect.p1.y y_line_1 = in_rect.p1.y + 0.33 * height y_line_2 = in_rect.p1.y + 0.66 * height self.instr_raw = Text(Point(x_center, y_line_1), "_") self.instr_raw.setSize(18) self.instr_raw.draw(self.window) self.instr_decoded = Text(Point(x_center, y_line_2), "_") self.instr_decoded.setSize(16) self.instr_decoded.draw(self.window)
def what_was_clicked(self, point: graphics.Point): if 0 < point.getX() < 50 and 400 < point.getY() < 425: return 'attack', self.menu.attackButton if self.menu.recruitButton.rect.getP1().getX() < point.getX( ) < self.menu.recruitButton.rect.getP2( ).getX() and self.menu.recruitButton.rect.getP1().getY() < point.getY( ) < self.menu.recruitButton.rect.getP2().getY(): return 'recruit', self.menu.recruitButton for nation in self.nations: for region in nation.regions: if region.shape.polygon.contains( geometry.Point(point.x, point.y)): return 'nation', nation
def _draw_reg(self, row, col ): x_min, y_min, x_max, y_max = self.reg_region height = y_max - y_min width = x_max - x_min llx = x_min + col * 0.5 * width + 5 lly = y_min + row * (height / 8) urx = x_min + (col + 1) * 0.5 * width -10 ury = y_min + (row + 1) * (height / 8) reg_display = Rectangle(Point(llx,lly), Point(urx,ury)) reg_display.draw(self.window) reg_center = Point((llx+urx)/2, (lly+ury)/2) label = Text(reg_center,"_") label.setSize(24) label.draw(self.window) reg_display.label = label self.registers.append(reg_display) log.debug("Displayed register {} at {}".format(row*2 + col, reg_display))
def _draw_memory_cell(self, row, col): x_min, y_min, x_max, y_max = self.mem_region width = x_max - x_min height = y_max - y_min cell_height = height / 32 cell_width = width / 8 llx = x_min + col * cell_width + 1 lly = y_min + row * cell_height + 1 urx = llx + cell_width - 2 ury = lly + cell_height - 2 mem_cell = Rectangle(Point(llx, lly), Point(urx, ury)) mem_cell.setFill("#dddddd") mem_cell.draw(self.window) center = Point((llx + urx) / 2, (lly + ury) / 2) label = Text(center, ".") label.draw(self.window) mem_cell.label = label self.mem_cells.append(mem_cell)