def delta(self, rect: pg.Rect) -> pg.Rect: rect = rect.copy() rect.x = ceil(rect.x * self.dz + self.dx) rect.y = ceil(rect.y * self.dz + self.dy) rect.w = ceil(rect.w * self.dz) rect.h = ceil(rect.h * self.dz) return rect
def hscrollbar_rect(self): # Get the distance between the scroll buttons (d) slr, slt = self.scroll_left_rect().topright d = self.scroll_right_rect().left - slr # The predefined step value _s = self.cell_size[1] # Get the total step number n = float(self.virtual_width) / _s # Get the visible step number v = float(self.width) / _s s = float(d) / n w = s * v if isinstance(w, float): if w - int(w) > 0: w += 1 left = max( slr, slr + (d * (float(self.hscroll) / self.virtual_width)) + self.hscroll_rel) r = Rect(left, slt, w, self.scroll_button_size) r.right = min(r.right, d + slr) r.inflate_ip(-4, -4) if r.w < 1: r.w = int(w) return r
def redraw_home(win: pygame.Surface, code: str, color: tuple, input_rect: pygame.Rect, go: Button, lobby: Button): win.fill(WHITE) pygame.draw.rect(win, color, input_rect) code_surface = font.render(code, True, BLACK) win.blit(code_surface, (input_rect.x + 5, input_rect.y + 5)) go.update(win) lobby.update(win) input_rect.w = max(150, code_surface.get_width() + 10)
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Map Editor Load Map", self.gui, self.ui_elements['panel']) # buttons select_rect = Rect(0, 0, 400, get_param('element_height') * 3) select_rect.y = scene_label_rect.bottom + get_param('element_padding') select_rect.centerx = panel_rect.w // 2 self.ui_elements['map_select'] = UISelectionList( select_rect, [f"Map {n}" for n in range(20)], self.gui, container=self.ui_elements['panel']) preview_rect = Rect(0, 0, 256, get_param('element_height') * 8) preview_rect.y = select_rect.bottom + get_param('element_padding') preview_rect.centerx = panel_rect.w // 2 self.ui_elements['preview'] = UILabel(preview_rect, "PREVIEW AREA", self.gui, self.ui_elements['panel']) button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.y = preview_rect.bottom + get_param('element_padding') button_rect.centerx = (panel_rect.w // 2) - 100 self.ui_elements['btn_load'] = UIButton(button_rect, "Load Map", self.gui, self.ui_elements['panel']) self.ui_elements['btn_load'].disable() button_rect.centerx = (panel_rect.w // 2) + 100 self.ui_elements['btn_delete'] = UIButton(button_rect, "Delete Map", self.gui, self.ui_elements['panel']) self.ui_elements['btn_delete'].disable() button_rect.w = 400 button_rect.centerx = panel_rect.w // 2 button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel'])
def world_to_screen_rect(self, rect: pg.Rect, is_world=True, clip=True): rect = rect.copy() if is_world: rect.x -= self.position[0] rect.y -= self.position[1] rect.x *= self.scale rect.y *= self.scale rect.w *= self.scale rect.h *= self.scale if self.cam_alignment[0] == self.ALIGNMENT_RIGHT: rect.x += self.display_port.x elif self.cam_alignment[0] == self.ALIGNMENT_CENTER: rect.x += self.display_port.x + (self.display_port.w // 2) elif self.cam_alignment[0] == self.ALIGNMENT_LEFT: rect.x += self.display_port.x + self.display_port.w if self.cam_alignment[1] == self.ALIGNMENT_BOTTOM: rect.y += self.display_port.y elif self.cam_alignment[1] == self.ALIGNMENT_CENTER: rect.y += self.display_port.y + (self.display_port.h // 2) elif self.cam_alignment[1] == self.ALIGNMENT_TOP: rect.y += self.display_port.y + self.display_port.h if clip: if rect.x < self.display_port.x: difference = self.display_port.x - rect.x rect.x += difference rect.w -= difference if rect.x + rect.w > self.display_port.x + self.display_port.w: difference = (rect.x + rect.w) - (self.display_port.x + self.display_port.w) rect.w -= difference if rect.y < self.display_port.y: difference = self.display_port.y - rect.y rect.y += difference rect.h -= difference if rect.y + rect.h > self.display_port.y + self.display_port.h: difference = (rect.y + rect.h) - (self.display_port.y + self.display_port.h) rect.h -= difference rect.w = max(rect.w, 0) rect.h = max(rect.h, 0) return rect
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Load Saved Game", self.gui, self.ui_elements['panel']) # buttons select_rect = Rect(0, 0, 400, get_param('element_height') * 3) select_rect.y = scene_label_rect.bottom + get_param('element_padding') select_rect.centerx = panel_rect.w // 2 self.ui_elements['game_select'] = UISelectionList( select_rect, [f"Game {n}" for n in range(5)], self.gui, container=self.ui_elements['panel']) button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.y = select_rect.bottom + get_param('element_padding') button_rect.centerx = (panel_rect.w // 2) - 100 self.ui_elements['btn_load'] = UIButton(button_rect, "Load Save", self.gui, self.ui_elements['panel']) self.ui_elements['btn_load'].disable() button_rect.centerx = (panel_rect.w // 2 + 100) self.ui_elements['btn_delete'] = UIButton(button_rect, "Delete Save", self.gui, self.ui_elements['panel']) self.ui_elements['btn_delete'].disable() button_rect.w = 400 button_rect.centerx = panel_rect.w // 2 button_rect.y += get_param('element_height') + get_param( 'element_padding') self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel'])
def hscrollbar_rect(self): # Get the distance between the scroll buttons (d) slr, slt = self.scroll_left_rect().topright d = self.scroll_right_rect().left - slr # The predefined step value _s = self.cell_size[1] # Get the total step number n = float(self.virtual_width) / _s # Get the visible step number v = float(self.width) / _s s = float(d) / n w = s * v if isinstance(w, float): if w - int(w) > 0: w += 1 left = max(slr, slr + (d * (float(self.hscroll) / self.virtual_width)) + self.hscroll_rel) r = Rect(left, slt, w, self.scroll_button_size) r.right = min(r.right, d + slr) r.inflate_ip(-4, -4) if r.w < 1: r.w = int(w) return r
def scale_rect(rectangle: pygame.Rect, scale=2): rectangle.x = rectangle.x * scale rectangle.y = rectangle.y * scale rectangle.w = rectangle.w * scale rectangle.h = rectangle.h * scale return rectangle
def build_gui(self): super().build_gui() panel_rect = Rect( 0, 0, 500, self.display.get_rect().height - (get_param('panel_padding') * 2)) panel_rect.centerx = self.display.get_rect().centerx panel_rect.y = get_param('panel_padding') self.ui_elements['panel'] = UIPanel(panel_rect, 1, self.gui) # all other elements are relative scene_label_rect = Rect(0, 0, 400, get_param('element_height')) scene_label_rect.y = get_param('element_padding') scene_label_rect.centerx = panel_rect.w // 2 # midpoint of the panel self.ui_elements['scene_label'] = UILabel(scene_label_rect, "Generate Random Map", self.gui, self.ui_elements['panel']) # map size label_rect = Rect(0, 0, 150, get_param('element_height')) label_rect.y += scene_label_rect.bottom + get_param('element_padding') dd_rect = Rect(0, 0, 250, get_param('element_height')) dd_rect.y = label_rect.y label_rect.centerx = 125 dd_rect.centerx = 325 self.ui_elements['d_size_label'] = UILabel(label_rect, "Map Size", self.gui, self.ui_elements['panel']) self.ui_elements['dd_map_size'] = UIDropDownMenu( ['64', '128', '256'], '64', dd_rect, self.gui, self.ui_elements['panel']) # Seed label_rect.y += get_param('element_height') + get_param( 'element_padding') ip_rect = Rect(0, 0, 180, get_param('element_height')) ip_rect.centerx = (panel_rect.w // 2) + 30 ip_rect.y = label_rect.y s_btn_rect = Rect(0, 0, 60, get_param('element_height')) s_btn_rect.x = ip_rect.right + get_param('element_padding') s_btn_rect.y = ip_rect.y self.ui_elements['seed_label'] = UILabel(label_rect, 'Map Seed', self.gui, self.ui_elements['panel']) self.ui_elements['input_seed'] = UITextEntryLine( ip_rect, self.gui, self.ui_elements['panel']) self.ui_elements['btn_rnd_seed'] = UIButton(s_btn_rect, 'Random', self.gui, self.ui_elements['panel']) # I want to add two sliders, 1 for mountains and 1 for water, these would be used to control the upper # and lower limits of the height mapping. h_sl_ops = (0, 100) h_sl_sel = 50 label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect = Rect(0, 0, 200, get_param('element_height')) h_sl_rect.centerx = (panel_rect.w // 2) + get_param('element_padding') + 30 h_sl_rect.y = label_rect.y self.ui_elements['hsl_grass'] = UILabel(label_rect, 'Grass', self.gui, self.ui_elements['panel']) self.ui_elements['hs_grass'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect.y = label_rect.y self.ui_elements['hsl_water'] = UILabel(label_rect, 'Water', self.gui, self.ui_elements['panel']) self.ui_elements['hs_water'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) label_rect.y += get_param('element_height') + get_param( 'element_padding') h_sl_rect.y = label_rect.y self.ui_elements['hsl_mountain'] = UILabel(label_rect, 'Mountain', self.gui, self.ui_elements['panel']) self.ui_elements['hs_mountain'] = UIHorizontalSlider( h_sl_rect, h_sl_sel, h_sl_ops, self.gui, self.ui_elements['panel']) # buttons button_rect = Rect(0, 0, 200, get_param('element_height')) button_rect.centerx = panel_rect.w // 2 button_rect.y = label_rect.bottom + get_param('element_padding') self.ui_elements['btn_preview'] = UIButton(button_rect, "Generate Preview", self.gui, self.ui_elements['panel']) button_rect.w = 200 button_rect.y += get_param('element_height') + get_param( 'element_padding') button_rect.centerx = (panel_rect.w // 2) - 100 self.ui_elements['btn_back'] = UIButton(button_rect, "Back", self.gui, self.ui_elements['panel']) # the apply button always starts off disabled button_rect.centerx = (panel_rect.w // 2) + 100 self.ui_elements['btn_next'] = UIButton(button_rect, "Next", self.gui, self.ui_elements['panel']) pv_rect = Rect(0, 0, 300, 300) pv_rect.centerx = panel_rect.w // 2 pv_rect.y = button_rect.bottom + get_param('element_padding') self.ui_elements['pv_image'] = UIImage(pv_rect, self.pv_img, self.gui, self.ui_elements['panel'])