def clear_buttons(self): get_screen("BaseBuildScreen").ids["scatter"].unbind( on_transform_with_touch=self.redo_building_move_buttons, size=self.redo_building_move_buttons) self.move_buttons_holder.clear_widgets() self.custom_buttons_holder.clear_widgets() self.log_deep_debug("Cleared buttons")
def update_positions(self): # TODO: Do something building is moved scatter: BetterScatter = get_screen("BaseBuildScreen").ids["scatter"] for building_id, icon in self.resource_miner_finished_icons.items(): building = get_screen("BaseBuildScreen").ids["base_layout"].buildings[int(building_id)] (x1, y1), (x2, y2) = building.get_projected_tl_br() x = x1 + ((x2 - x1) / 2) y = y2 icon.pos = scatter.to_parent(x, y)
def button_touch_move(self, button: BetterButton, touch: MotionEvent): self.canvas.after.clear() if touch.grab_current == button: building: BuildingBase = button.button_storage if button.button_id == "move": # TODO: Make this a bit better dx = touch.dx / (15 * self.scatter.scale) dy = touch.dy / (15 * self.scatter.scale) building.x += dx building.y += dy base_layout: BaseLayout = get_screen( "BaseBuildScreen").ids["base_layout"] polygon = Polygon(building.get_projected_tl_tr_br_bl()) for other_building in base_layout.buildings: if other_building.id != building.id: other_polygon = Polygon( other_building.get_projected_tl_tr_br_bl()) if polygon.intersects( other_polygon) or polygon.contains( other_polygon): self.log_deep_debug( "Building collided with other building while being moved" ) building.x -= dx building.y -= dy elif button.button_id == "rotate": building_x, building_y = self.scatter.to_parent( *building.get_projected_origin()) mouse_x = touch.x mouse_y = touch.y mouse_dx = mouse_x - building_x # adjacent mouse_dy = mouse_y - building_y # opposite try: rot = math.degrees(math.atan2(mouse_dy, mouse_dx)) delta_rot = rot - self.last_rotation building.rotation += delta_rot self.last_rotation = rot except ZeroDivisionError: self.log_warning( "Got ZeroDivisionError from rotating building | mouse_dx, mouse_dy =", mouse_dx, mouse_dy) else: self.log_critical( "Wrong button type for building transform buttons -", button.button_id) self.redo_building_move_buttons(building)
def item_pressed(self, button: TextBetterButton): item = str(button.button_storage) touch: MotionEvent = button.last_touch item_large_move_amount = graphicsConfig.getint( "InventoryScreen", "item_large_move_amount") if self.mode == "merge": # Move back if touch.is_double_tap or touch.is_triple_tap: self.remove(item, item_large_move_amount) else: self.remove(item, 1) elif self.mode == "recipes": item = str(button.button_storage) if item in GameConfig.get("Items", "recipes"): inventory_screen = get_screen("InventoryScreen") recipe = GameConfig.get("Items", "recipes", item) self.log_deep_debug("Creating GUI for recipe of item", item, "| Recipe is", recipe) self.set_all(recipe) inventory_screen.current_recipe_button_id = item + "_item" inventory_screen.ids[ "merge_output_button"].button_id = item + "_item" else: self.log_deep_debug( "Item", item, "was clicked on but is doesnt have a merge recipe")
def move_to_placed_buildings(self, building_type: str): self.array["inventory"][str(building_type)] -= 1 if self.array["inventory"][str(building_type)] == 0: self.array["inventory"].pop(str(building_type)) from graphics.spaceBuilderMergeGameScreenManager import get_screen layout = get_screen("BaseBuildScreen").ids["base_layout"] layout.add_building_with_id(building_type) self.log_config("Moved", building_type, "from inventory to place buildings")
def store(self): self.log_debug("Storing self -", self) # noinspection PyProtectedMember self._obj._instructions.clear() # noinspection PyProtectedMember self.renderer._instructions.remove(self._obj.as_instructions()) self.scene.children.remove(self._obj) self.selected = False self.parent.buildings.remove(self) SaveManager.un_register_update_game_data_function(self.update_game_data) get_screen("BaseBuildScreen").ids["building_buttons_handler"].clear_buttons() self.parent = None get_screen("BaseBuildScreen").ids["scatter"].rotation += 0.001 Clock.schedule_once(un_turn, 0) gameData.move_to_inventory(self.id)
def scrap(self): self.log_debug("Storing self -", self) # noinspection PyProtectedMember self._obj._instructions.clear() # noinspection PyProtectedMember self.renderer._instructions.remove(self._obj.as_instructions()) self.scene.children.remove(self._obj) self.selected = False self.parent.buildings.remove(self) SaveManager.un_register_update_game_data_function(self.update_game_data) get_screen("BaseBuildScreen").ids["building_buttons_handler"].clear_buttons() self.parent = None get_screen("BaseBuildScreen").ids["scatter"].rotation += 0.001 Clock.schedule_once(un_turn, 0) for item, amount in dict(GameConfig.get("Buildings", "scrap_products", self.__type__)).items(): gameData.add_to_inventory(item, amount)
def redo_buttons(self, button_ids: list[str], building: BuildingBase): self.scatter = get_screen("BaseBuildScreen").ids["scatter"] # Hacky fix so its not got every time the scatter is transformed self.clear_buttons() self.custom_buttons_holder.add_widget(self.spacer1) button_id: str for button_id in button_ids: button = BetterButton(button_id=button_id, size_type="big", on_release=button_pressed, button_storage=building) self.custom_buttons_holder.add_widget(button) self.custom_buttons_holder.add_widget(self.spacer2) if building.movable: # TODO: Fix problem of buttons going everywhere because of fix of positioning self.transform_button_1 = BetterButton( button_id="move", size_type="small", on_touch_down=self.button_touch_down, on_touch_move=self.button_touch_move, on_touch_up=self.button_touch_up, button_storage=building) self.transform_button_2 = BetterButton( button_id="rotate", size_type="small", on_touch_down=self.button_touch_down, on_touch_move=self.button_touch_move, on_touch_up=self.button_touch_up, button_storage=building) GlobalEvents.bind(on_scatter_transformed=lambda: self. redo_building_move_buttons(building)) self.move_buttons_holder.add_widget(self.transform_button_1) self.move_buttons_holder.add_widget(self.transform_button_2) # TODO: Find better solution Clock.schedule_once( lambda _elapsed_time: self.redo_building_move_buttons(building ), 0) self.log_deep_debug("Added buttons to self, they are", self.custom_buttons_holder.children)
def exit(self): self.log_debug("Reloading screen manager") App.get_running_app().root.switch() get_sm().current = "BaseBuildScreen" get_screen("BaseBuildScreen").fade_in()
def on_selected(self, instance, value: bool): self.log_deep_debug("Selected switched to", value, "on building", instance) if value: get_screen("BaseBuildScreen").ids["building_buttons_handler"].redo_buttons(self.button_ids, self)
def un_turn(_elapsed_time: int): get_screen("BaseBuildScreen").ids["scatter"].rotation -= 0.001
def on_touch_up_from_scatter(self, tx: int, ty: int): """ Ran by the scatter when user is not dragging """ # noinspection PyTypeChecker to_select: BuildingBase = None point = Point(tx, ty) building: BuildingBase for building in self.buildings: # I want my long time and effort to be remembered, this to so long, AND THE ANSWER WAS SO SIMPLE OMG """bPos = building._obj.pos[0], building._obj.pos[1], building._obj.pos[2] bPos2 = building._obj.pos[0] + self.renderer.width, building._obj.pos[1] + self.renderer.height, building._obj.pos[2] cPos = self.camera.pos""" """bVPos = Vector3(bPos) bVPos2 = Vector3(bPos2) x, y, z = bVPos + Vector3(0, 0, 100) x2, y2, z2 = bVPos2 + Vector3(0, 0, 100) print(x, y, z, x2, y2, z2)""" """pitch = atan((bPos[0] - cPos.x) / (bPos[1] - cPos.y)) yaw = atan((bPos[2] - cPos.z) / (bPos[1] - cPos.y)) x = width() / 2 + (pitch * (width() / self.camera.fov)) y = height() / 2 + (yaw * (height() / self.camera.fov)) pitch2 = atan((bPos2[0] - cPos.x) / (bPos2[1] - cPos.y)) yaw2 = atan((bPos2[2] - cPos.z) / (bPos2[1] - cPos.y)) x2 = width() / 2 + (pitch2 * (width() / self.camera.fov)) y2 = height() / 2 + (yaw2 * (height() / self.camera.fov)) print(touch.x, touch.y) print(bPos, bPos2, cPos) print(x, y, x2, y2) print(self.camera.rotation)""" """print(building) print(building._obj.pos, building._obj.scale) print(building._obj._instructions, building._obj._instructions.children) print() print() print(building._obj._translate.matrix) print() print() print(building._obj._scale.matrix) print() print() print(building._obj._rotors["x"].matrix) print() print(building._obj._rotors["y"].matrix) print() print(building._obj._rotors["z"].matrix)""" """print(building._obj.pos, (building._obj.pos[0] - (width() / 2), building._obj.pos[1] - (height() / 2)), building._obj.scale.xyz) print(touch.pos) print(touch.pos[0] - (width() / 2), touch.pos[1] - (height() / 2)) print(self.renderer.size) print(self.scatter_widget.scale) print(Vector3.get_XY_from_camera(building._obj.pos, self.camera)) print() print() print() print() print(Matrix()) print(Matrix().project(building._obj.pos[0], building._obj.pos[1], building._obj.pos[2], Matrix(), Matrix(), self.camera.pos.x, self.camera.pos.y, width(), height()))""" polygon = Polygon(building.get_projected_tl_tr_br_bl()) if polygon.contains(point): to_select = building break if to_select is None: self.log_deep_debug("User touched but no building was clicked") get_screen("BaseBuildScreen").ids["building_buttons_handler"].clear_buttons() else: buildings = self.buildings.copy() buildings.remove(to_select) for building in buildings: building.selected = False to_select.selected = True self.log_debug("Building", to_select, "was clicked on, setting building to selected")