def calculatePoints(self): relative_scale = ScreenObjectManager.instance.relativeScreenScale() new_size = [int(self.image.get_size()[0] * self.scale * relative_scale[0]), int(self.image.get_size()[1] * self.scale * relative_scale[1])] scaled = pygame.transform.scale(self.image, new_size) self.rotated = pygame.transform.rotate(scaled, self.rotation * 180 / np.pi) self.rotated.fill(self.fill, special_flags=pygame.BLEND_ADD) self.screen_location = utils.worldspace_to_screenspace(self.position) self.screen_size = self.rotated.get_size() if self.hAlignment == 'l': pass elif self.hAlignment == 'm': self.screen_location -= np.array([self.screen_size[0] / 2, 0.0]) elif self.hAlignment == 'r': self.screen_location -= np.array([self.screen_size[0], 0.0]) else: raise ValueError(f'hAlignment is incorrect: {self.hAlignment}') if self.vAlignment == 't': pass elif self.vAlignment == 'm': self.screen_location -= np.array([0.0, self.screen_size[1] / 2]) elif self.vAlignment == 'b': self.screen_location -= np.array([0.0, self.screen_size[1]]) else: raise ValueError(f'vAlignment is incorrect: {self.vAlignment}') from ev3sim.visual.utils import screenspace_to_worldspace physics_size = screenspace_to_worldspace([ScreenObjectManager.instance.screen_width / 2 + self.screen_size[0], ScreenObjectManager.instance.screen_height / 2 + self.screen_size[1]]) self.verts = [ (physics_size[0]/2, physics_size[1]/2), (physics_size[0]/2, -physics_size[1]/2), (-physics_size[0]/2, -physics_size[1]/2), (-physics_size[0]/2, physics_size[1]/2), ]
def calculatePoints(self): if not hasattr(self, 'font'): return self.surface, self.rect = self.font.render(self.text, fgcolor=self.fill) relative_scale = ScreenObjectManager.instance.relativeScreenScale() self.surface = pygame.transform.scale(self.surface, (int(self.surface.get_width() * relative_scale[0]), int(self.surface.get_height() * relative_scale[1]))) self.screen_size = (self.surface.get_width(), self.surface.get_height()) baseline = np.array([self.rect.x * relative_scale[0], self.rect.y * relative_scale[1]]) self.rect.move_ip(-self.rect.x, -self.rect.y) width, height = self.font.get_rect(self.text).width * relative_scale[0], self.font.get_rect(self.text).height * relative_scale[1] self.anchor = utils.worldspace_to_screenspace(self.position) if self.hAlignment == 'l': pass elif self.hAlignment == 'm': self.anchor -= np.array([width / 2.0, 0.0]) elif self.hAlignment == 'r': self.anchor -= np.array([width, 0.0]) else: raise ValueError(f'hAlignment is incorrect: {self.hAlignment}') if self.vAlignment == 't': self.anchor -= np.array([0.0, 0.0]) elif self.vAlignment == 'm': self.anchor -= np.array([0.0, height / 2]) elif self.vAlignment == 'baseline': self.anchor -= np.array([0.0, baseline[1]]) elif self.vAlignment == 'b': self.anchor -= np.array([0.0, height]) else: raise ValueError(f'vAlignment is incorrect: {self.vAlignment}') self.rect.move_ip(*self.anchor)
def calculatePoints(self): try: tmp = self.rotation, self.position except: return for i, v in enumerate(self.verts): self.points[i] = utils.worldspace_to_screenspace(local_space_to_world_space(v, self.rotation, self.position))
def applyToScreen(self): if self.fill: pygame.draw.line( ScreenObjectManager.instance.screen, self.fill, utils.worldspace_to_screenspace(self.start), utils.worldspace_to_screenspace(self.end), 1, ) if self.stroke and self.stroke_width: pygame.draw.line( ScreenObjectManager.instance.screen, self.fill, utils.worldspace_to_screenspace(self.start), utils.worldspace_to_screenspace(self.end), max(1, int(self.stroke_width * ScreenObjectManager.instance.screen_width / ScreenObjectManager.instance.map_width)), )
def _applyToScreen(self): if self.stroke and self.stroke_width: pygame.draw.line( ScreenObjectManager.instance.screen, self.fill, utils.worldspace_to_screenspace(self.start), utils.worldspace_to_screenspace(self.end), self.scaledStrokeWidth, ) elif self.fill: pygame.draw.line( ScreenObjectManager.instance.screen, self.fill, utils.worldspace_to_screenspace(self.start), utils.worldspace_to_screenspace(self.end), 1, )
def _applyToScreen(self, screen): if self.stroke and self.stroke_width: pygame.draw.line( screen, self.fill, utils.worldspace_to_screenspace(self.start, self.customMap), utils.worldspace_to_screenspace(self.end, self.customMap), self.scaledStrokeWidth, ) elif self.fill: pygame.draw.line( screen, self.fill, utils.worldspace_to_screenspace(self.start, self.customMap), utils.worldspace_to_screenspace(self.end, self.customMap), 1, )
def calculatePoints(self): try: tmp = self.radius except: return self.point = utils.worldspace_to_screenspace(self.position) self.v_radius = int(ScreenObjectManager.instance.screen_height / ScreenObjectManager.instance.map_height * self.radius) self.h_radius = int(ScreenObjectManager.instance.screen_width / ScreenObjectManager.instance.map_width * self.radius) self.rect = pygame.Rect(self.point[0] - self.h_radius, self.point[1] - self.v_radius, self.h_radius * 2, self.v_radius * 2)
def _applyToScreenGfx(self): # Note: aaline() isn't actually from gfxdraw, # it just seemed nicer to keep all the AA options toggleable together if self.stroke and self.stroke_width: pygame.draw.aaline( ScreenObjectManager.instance.screen, self.fill, utils.worldspace_to_screenspace(self.start), utils.worldspace_to_screenspace(self.end), self.scaledStrokeWidth, ) elif self.fill: pygame.draw.aaline( ScreenObjectManager.instance.screen, self.fill, utils.worldspace_to_screenspace(self.start), utils.worldspace_to_screenspace(self.end), )
def _calc_raw(self): res = self._SenseValueAboutPosition( self.global_position, lambda pos: ScreenObjectManager.instance.colourAtPixel( worldspace_to_screenspace(pos))) # These are 0-255. RAW is meant to be 0-1020 but actually more like 0-300. self.saved_raw = [ int(res[0] * self.__r_bias), int(res[1] * self.__g_bias), int(res[2] * self.__b_bias), ]
def calculatePoints(self): if self.customMap is None: relative_scale = ScreenObjectManager.instance.relativeScreenScale() # In order to have a reasonably sized image at all resolutions, calculate the scale to use based on the starting screen scale as well. relative_scale = relative_scale * ScreenObjectManager.instance.original_SCREEN_WIDTH / 1280 else: relative_scale = self.customMap[ "SCREEN_WIDTH"] / 1280 * 293.3 / self.customMap["MAP_WIDTH"] new_font_size = int(self.font_size * relative_scale) # Scale the font size as much as possible relative_scale = self.font_size * relative_scale / new_font_size self.font = pygame.freetype.Font(self.font_path, new_font_size) self.surface, self.rect = self.font.render(self.text, fgcolor=self.fill) self.surface = pygame.transform.scale( self.surface, (int(self.surface.get_width() * relative_scale), int(self.surface.get_height() * relative_scale)), ) self.screen_size = (self.surface.get_width(), self.surface.get_height()) baseline = np.array( [self.rect.x * relative_scale, self.rect.y * relative_scale]) self.rect.move_ip(-self.rect.x, -self.rect.y) width, height = ( self.font.get_rect(self.text).width * relative_scale, self.font.get_rect(self.text).height * relative_scale, ) self.anchor = utils.worldspace_to_screenspace(self.position, self.customMap) if self.hAlignment == "l": pass elif self.hAlignment == "m": self.anchor -= np.array([width / 2.0, 0.0]) elif self.hAlignment == "r": self.anchor -= np.array([width, 0.0]) else: raise ValueError(f"hAlignment is incorrect: {self.hAlignment}") if self.vAlignment == "t": self.anchor -= np.array([0.0, 0.0]) elif self.vAlignment == "m": self.anchor -= np.array([0.0, height / 2]) elif self.vAlignment == "baseline": self.anchor -= np.array([0.0, baseline[1]]) elif self.vAlignment == "b": self.anchor -= np.array([0.0, height]) else: raise ValueError(f"vAlignment is incorrect: {self.vAlignment}") self.rect.move_ip(*self.anchor)
def calculatePoints(self): try: tmp = self.radius except: return self.point = utils.worldspace_to_screenspace(self.position) self.v_radius = int( ScreenObjectManager.instance.SCREEN_HEIGHT / ScreenObjectManager.instance.MAP_HEIGHT * self.radius ) self.h_radius = int( ScreenObjectManager.instance.SCREEN_WIDTH / ScreenObjectManager.instance.MAP_WIDTH * self.radius ) self.rect = pygame.Rect( self.point[0] - self.h_radius, self.point[1] - self.v_radius, self.h_radius * 2, self.v_radius * 2 )
def calculatePoints(self): relative_scale = ScreenObjectManager.instance.relativeScreenScale() # In order to have a reasonably sized image at all resolutions, calculate the scale to use based on the starting screen scale as well. relative_scale = relative_scale * ScreenObjectManager.instance.original_SCREEN_WIDTH / 1280 new_size = [ int(self.image.get_size()[0] * self.scale * relative_scale), int(self.image.get_size()[1] * self.scale * relative_scale), ] scaled = pygame.transform.scale(self.image, new_size) self.rotated = pygame.transform.rotate(scaled, self.rotation * 180 / np.pi) self.rotated.fill(self.fill, special_flags=pygame.BLEND_ADD) self.screen_location = utils.worldspace_to_screenspace(self.position) self.screen_size = self.rotated.get_size() if self.hAlignment == "l": pass elif self.hAlignment == "m": self.screen_location -= np.array([self.screen_size[0] / 2, 0.0]) elif self.hAlignment == "r": self.screen_location -= np.array([self.screen_size[0], 0.0]) else: raise ValueError(f"hAlignment is incorrect: {self.hAlignment}") if self.vAlignment == "t": pass elif self.vAlignment == "m": self.screen_location -= np.array([0.0, self.screen_size[1] / 2]) elif self.vAlignment == "b": self.screen_location -= np.array([0.0, self.screen_size[1]]) else: raise ValueError(f"vAlignment is incorrect: {self.vAlignment}") from ev3sim.visual.utils import screenspace_to_worldspace physics_size = screenspace_to_worldspace( [ ScreenObjectManager.instance._SCREEN_WIDTH_ACTUAL / 2 + self.screen_size[0], ScreenObjectManager.instance._SCREEN_HEIGHT_ACTUAL / 2 + self.screen_size[1], ] ) self.verts = [ (physics_size[0] / 2, physics_size[1] / 2), (physics_size[0] / 2, -physics_size[1] / 2), (-physics_size[0] / 2, -physics_size[1] / 2), (-physics_size[0] / 2, physics_size[1] / 2), ]
def createBotImage(self, index, bg=None): from ev3sim.visual.manager import ScreenObjectManager from ev3sim.visual.utils import worldspace_to_screenspace from ev3sim.visual.objects import Text width = 0 lengths = [] surfaces = [] for text_line in self.bot_keys[index].replace("\\n", "\n").split("\n"): text_object = Text() text_object.initFromKwargs( text=text_line, hAlignment="m", vAlignment="m", ) named_surface = ScreenObjectManager.instance.screen.copy() named_surface.fill(pygame.Color("#181A25") if bg is None else bg) text_object.applyToScreen(named_surface) surfaces.append(named_surface) lengths.append(text_object.rect.height) width = max(width, text_object.rect.width) line_spacing = 10 s = max(width, sum(lengths) + (len(lengths) - 1) * line_spacing) + 30 pos = worldspace_to_screenspace((0, 0)) cropped_surface = pygame.Surface((s, s)) cropped_surface.fill(pygame.Color("#181A25") if bg is None else bg) cur_y = (s - (sum(lengths) + (len(lengths) - 1) * line_spacing)) // 2 for y, surface in zip(lengths, surfaces): cropped_surface.blit(surface, (0, cur_y), (pos[0] - s // 2, pos[1] - (y + 1) // 2 - 1, s, y + 2)) cur_y += y + line_spacing return pygame_gui.elements.UIImage( relative_rect=pygame.Rect(0, 0, *self._size), image_surface=cropped_surface, manager=self, object_id=pygame_gui.core.ObjectID( f"bot-image-{self.bot_keys[index]}"), )