def __init__(self): self.__surface = pygame.Surface(config.help_window_size) self.size = config.help_window_size self.width = config.help_window_size[0] self.height = config.help_window_size[1] self.font = pygame.font.Font(None, config.help_window_font_size) self.__scrollbar = ScrollBar(self.height, (0, 1), '', _locals.ORIENTATION_VERTICAL, 2, config.field_color, config.scrollbar_size[1], (253, 246, 220)) self.__scrollbar.set_shadow(color=(0, 0, 0), position=_locals.POSITION_NORTHWEST, offset=2) self.__scrollbar.set_controls(False) self.__scrollbar.set_position(self.width - config.scrollbar_size[0], 0) self.text = '' self.default_text_coordinates = (10, 10) self.text_rects = [] self.text_lines_surf = [] self.add_log(config.battleship_rules_text, False) self.counter = 0
def main(test: bool = False) -> None: """ Main function. :param test: Indicate function is being tested :return: None """ scr_size = (480, 480) screen = create_example_window('Example - Scrollbar', scr_size) world = make_world(int(scr_size[0] * 4), scr_size[1] * 3) screen.fill((120, 90, 130)) thick_h = 20 thick_v = 40 # Horizontal ScrollBar sb_h = ScrollBar(length=scr_size[0] - thick_v, values_range=(50, world.get_width() - scr_size[0] + thick_v), slider_pad=2, page_ctrl_thick=thick_h, onchange=h_changed) sb_h.set_shadow(color=(0, 0, 0), position=pygame_menu.locals.POSITION_SOUTHEAST) sb_h.set_controls(False) sb_h.set_position(0, scr_size[1] - thick_h) sb_h.set_page_step(scr_size[0] - thick_v) # Vertical ScrollBar sb_v = ScrollBar(length=scr_size[1] - thick_h, values_range=(0, world.get_height() - scr_size[1] + thick_h), orientation=pygame_menu.locals.ORIENTATION_VERTICAL, slider_pad=6, slider_color=(135, 193, 180), slider_hover_color=(180, 180, 180), page_ctrl_thick=thick_v, page_ctrl_color=(253, 246, 220), onchange=v_changed) sb_v.set_shadow(color=(52, 54, 56), position=pygame_menu.locals.POSITION_NORTHWEST, offset=4) sb_v.set_controls(False) sb_v.set_position(scr_size[0] - thick_v, 0) sb_v.set_page_step(scr_size[1] - thick_h) clock = pygame.time.Clock() # ------------------------------------------------------------------------- # Main loop # ------------------------------------------------------------------------- while True: # Clock tick clock.tick(60) # Application events events = pygame.event.get() for event in events: if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN and event.key == pygame.K_h: sb_h.set_value(100) if event.type == pygame.KEYDOWN and event.key == pygame.K_v: sb_v.set_value(200) sb_h.update([event]) sb_h.draw(screen) sb_v.update([event]) sb_v.draw(screen) trunc_world_orig = (sb_h.get_value(), sb_v.get_value()) trunc_world = (scr_size[0] - thick_v, scr_size[1] - thick_h) # noinspection PyTypeChecker screen.blit(world, (0, 0), (trunc_world_orig, trunc_world)) pygame.display.update() # At first loop returns if test: break
def __init__(self, area_width, area_height, area_color=None, extend_x=0, extend_y=0, scrollbar_color=(235, 235, 235), scrollbar_slider_color=(200, 200, 200), scrollbar_slider_pad=0, scrollbar_thick=20, scrollbars=(_locals.POSITION_SOUTH, _locals.POSITION_EAST), shadow=False, shadow_color=(0, 0, 0), shadow_offset=2, shadow_position=_locals.POSITION_SOUTHEAST, world=None, ): assert isinstance(area_width, (int, float)) assert isinstance(area_height, (int, float)) assert isinstance(scrollbar_slider_pad, (int, float)) assert isinstance(scrollbar_thick, (int, float)) assert isinstance(shadow, bool) assert isinstance(shadow_offset, (int, float)) assert_color(scrollbar_color) assert_color(scrollbar_slider_color) assert_color(shadow_color) assert_position(shadow_position) assert area_width > 0 and area_height > 0, \ 'area size must be greater than zero' self._rect = pygame.Rect(0.0, 0.0, area_width, area_height) self._world = world # type: pygame.Surface self._scrollbars = [] self._scrollbar_positions = tuple(set(scrollbars)) # Ensure unique self._scrollbar_thick = scrollbar_thick self._bg_surface = None self._extend_x = extend_x self._extend_y = extend_y if area_color: self._bg_surface = make_surface(width=area_width + extend_x, height=area_height + self._extend_y) if isinstance(area_color, _baseimage.BaseImage): area_color.draw(surface=self._bg_surface, area=self._bg_surface.get_rect()) else: self._bg_surface.fill(area_color) self._view_rect = self.get_view_rect() for pos in self._scrollbar_positions: # type:str assert_position(pos) if pos == _locals.POSITION_EAST or pos == _locals.POSITION_WEST: sbar = ScrollBar(self._view_rect.height, (0, max(1, self.get_hidden_height())), orientation=_locals.ORIENTATION_VERTICAL, slider_pad=scrollbar_slider_pad, slider_color=scrollbar_slider_color, page_ctrl_thick=scrollbar_thick, page_ctrl_color=scrollbar_color, onchange=self._on_vertical_scroll) else: sbar = ScrollBar(self._view_rect.width, (0, max(1, self.get_hidden_width())), slider_pad=scrollbar_slider_pad, slider_color=scrollbar_slider_color, page_ctrl_thick=scrollbar_thick, page_ctrl_color=scrollbar_color, onchange=self._on_horizontal_scroll) sbar.set_shadow(enabled=shadow, color=shadow_color, position=shadow_position, offset=shadow_offset) sbar.set_controls(joystick=False) self._scrollbars.append(sbar) self._apply_size_changes()
def test_scrollbar(self): """ Test ScrollBar widget. """ screen_size = surface.get_size() world = MenuUtils.get_large_surface() # Vertical right scrollbar thick = 80 length = screen_size[1] world_range = (50, world.get_height()) orientation = _locals.ORIENTATION_VERTICAL x, y = screen_size[0] - thick, 0 sb = ScrollBar(length, world_range, '', orientation, slider_pad=2, slider_color=(210, 120, 200), page_ctrl_thick=thick, page_ctrl_color=(235, 235, 230)) sb.set_shadow(color=(245, 245, 245), position=_locals.POSITION_SOUTHEAST, offset=2) sb.set_position(x, y) self.assertEqual(sb.get_orientation(), _locals.ORIENTATION_VERTICAL) self.assertEqual(sb.get_minimum(), world_range[0]) self.assertEqual(sb.get_maximum(), world_range[1]) sb.set_value(80) self.assertAlmostEqual(sb.get_value(), 80, delta=2) # Scaling delta sb.update( PygameUtils.mouse_click(x + thick / 2, y + 2, evtype=pygame.MOUSEBUTTONDOWN)) self.assertEqual(sb.get_value(), 50) sb.set_page_step(length) self.assertAlmostEqual(sb.get_page_step(), length, delta=2) # Scaling delta sb.draw(surface)
def main(test=False): """ Main function. :param test: Indicate function is being tested :type test: bool :return: None """ os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.init() scr_size = (400, 600) screen = pygame.display.set_mode(scr_size) world = make_world(int(scr_size[0] * 4), scr_size[1] * 3) screen.fill((120, 90, 130)) pygame.display.set_caption('ScrollBar') thick_h = 20 thick_v = 40 # Horizontal ScrollBar sb_h = ScrollBar(scr_size[0] - thick_v, (50, world.get_width() - scr_size[0] + thick_v), slider_pad=2, page_ctrl_thick=thick_h, onchange=h_changed) sb_h.set_shadow(color=(0, 0, 0), position=_locals.POSITION_SOUTHEAST) sb_h.set_controls(False) sb_h.set_position(0, scr_size[1] - thick_h) sb_h.set_page_step(scr_size[0] - thick_v) # Vertical ScrollBar # noinspection PyArgumentEqualDefault sb_v = ScrollBar(scr_size[1] - thick_h, (0, world.get_height() - scr_size[1] + thick_h), '', _locals.ORIENTATION_VERTICAL, 6, (135, 193, 180), thick_v, (253, 246, 220), onchange=v_changed) sb_v.set_shadow(color=(52, 54, 56), position=_locals.POSITION_NORTHWEST, offset=4) sb_v.set_controls(False) sb_v.set_position(scr_size[0] - thick_v, 0) sb_v.set_page_step(scr_size[1] - thick_h) clock = pygame.time.Clock() # ------------------------------------------------------------------------- # Main loop # ------------------------------------------------------------------------- while True: # Clock tick clock.tick(60) # Application events events = pygame.event.get() for event in events: if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN and event.key == pygame.K_h: sb_h.set_value(100) if event.type == pygame.KEYDOWN and event.key == pygame.K_v: sb_v.set_value(200) sb_h.update([event]) sb_h.draw(screen) sb_v.update([event]) sb_v.draw(screen) trunc_world_orig = (sb_h.get_value(), sb_v.get_value()) trunc_world = (scr_size[0] - thick_v, scr_size[1] - thick_h) screen.blit(world, (0, 0), (trunc_world_orig, trunc_world)) pygame.display.update() # At first loop returns if test: break
def __init__( self, area_width: int, area_height: int, area_color: Optional[Union[ColorInputType, 'pygame_menu.BaseImage']] = None, extend_x: int = 0, extend_y: int = 0, menubar: Optional['pygame_menu.widgets.MenuBar'] = None, parent_scrollarea: Optional['ScrollArea'] = None, scrollarea_id: str = '', scrollbar_color: ColorInputType = (235, 235, 235), scrollbar_cursor: CursorInputType = None, scrollbar_slider_color: ColorInputType = (200, 200, 200), scrollbar_slider_hover_color: ColorInputType = (180, 180, 180), scrollbar_slider_pad: NumberType = 0, scrollbar_thick: int = 20, scrollbars: StringVector = DEFAULT_SCROLLBARS, shadow: bool = False, shadow_color: ColorInputType = (0, 0, 0), shadow_offset: int = 2, shadow_position: str = POSITION_SOUTHEAST, world: Optional['pygame.Surface'] = None ) -> None: super(ScrollArea, self).__init__(object_id=scrollarea_id) assert isinstance(area_height, int) assert isinstance(area_width, int) assert isinstance(extend_x, int) assert isinstance(extend_y, int) assert isinstance(scrollbar_slider_pad, NumberInstance) assert isinstance(scrollbar_thick, int) assert isinstance(shadow, bool) assert isinstance(shadow_offset, int) assert isinstance(world, (pygame.Surface, type(None))) if area_color is not None and not isinstance(area_color, pygame_menu.BaseImage): area_color = assert_color(area_color) scrollbar_color = assert_color(scrollbar_color) scrollbar_slider_color = assert_color(scrollbar_slider_color) shadow_color = assert_color(shadow_color) assert_position(shadow_position) assert area_width > 0 and area_height > 0, \ 'area size must be greater than zero' assert isinstance(scrollbars, (str, VectorInstance)) unique_scrolls = [] if isinstance(scrollbars, str): unique_scrolls.append(scrollbars) else: for s in scrollbars: if s not in unique_scrolls: unique_scrolls.append(s) self._area_color = area_color self._bg_surface = None self._bg_surface = None self._decorator = Decorator(self) self._rect = pygame.Rect(0, 0, int(area_width), int(area_height)) self._scrollbar_positions = tuple(unique_scrolls) # Ensure unique self._scrollbar_thick = scrollbar_thick self._scrollbars = [] self._translate = (0, 0) self._world = world self._extend_x = extend_x self._extend_y = extend_y self._menubar = menubar self.set_parent_scrollarea(parent_scrollarea) self._view_rect = self.get_view_rect() for pos in self._scrollbar_positions: assert_position(pos) if pos == POSITION_EAST or pos == POSITION_WEST: sbar = ScrollBar( length=self._view_rect.height, onchange=self._on_vertical_scroll, orientation=ORIENTATION_VERTICAL, page_ctrl_color=scrollbar_color, page_ctrl_thick=scrollbar_thick, slider_color=scrollbar_slider_color, slider_hover_color=scrollbar_slider_hover_color, slider_pad=scrollbar_slider_pad, values_range=(0, max(1, self.get_hidden_height())) ) else: sbar = ScrollBar( length=self._view_rect.width, onchange=self._on_horizontal_scroll, page_ctrl_color=scrollbar_color, page_ctrl_thick=scrollbar_thick, slider_color=scrollbar_slider_color, slider_hover_color=scrollbar_slider_hover_color, slider_pad=scrollbar_slider_pad, values_range=(0, max(1, self.get_hidden_width())) ) sbar.set_shadow( enabled=shadow, color=shadow_color, position=shadow_position, offset=shadow_offset ) sbar.set_controls(joystick=False) sbar.set_cursor(cursor=scrollbar_cursor) sbar.set_scrollarea(self) sbar.configured = True sbar.hide() self._scrollbars.append(sbar) self._apply_size_changes() # Menu reference self._menu = None
def test_scrollbar(self) -> None: """ Test ScrollBar widget. """ screen_size = surface.get_size() world = pygame.Surface((WINDOW_SIZE[0] * 2, WINDOW_SIZE[1] * 3)) world.fill((200, 200, 200)) for x in range(100, world.get_width(), 200): for y in range(100, world.get_height(), 200): pygame.draw.circle(world, (225, 34, 43), (x, y), 100, 10) # Vertical right scrollbar thick = 80 length = screen_size[1] world_range = (50, world.get_height()) x, y = screen_size[0] - thick, 0 sb = ScrollBar( length, world_range, 'sb2', ORIENTATION_VERTICAL, slider_pad=2, slider_color=(210, 120, 200), page_ctrl_thick=thick, page_ctrl_color=(235, 235, 230) ) self.assertEqual(sb.get_thickness(), 80) self.assertIsNone(sb.get_scrollarea()) sb.set_shadow(color=(245, 245, 245), position=POSITION_SOUTHEAST) self.assertFalse(sb._font_shadow) sb.set_position(x, y) self.assertEqual(sb._orientation, 1) self.assertEqual(sb.get_orientation(), ORIENTATION_VERTICAL) self.assertEqual(sb.get_minimum(), world_range[0]) self.assertEqual(sb.get_maximum(), world_range[1]) sb.set_value(80) self.assertAlmostEqual(sb.get_value(), 80, delta=2) # Scaling delta sb.update(PygameEventUtils.mouse_click(x + thick / 2, y + 2, evtype=pygame.MOUSEBUTTONDOWN)) self.assertEqual(sb.get_value(), 50) self.assertEqual(sb.get_value_percentage(), 0) sb.set_page_step(length) self.assertAlmostEqual(sb.get_page_step(), length, delta=2) # Scaling delta sb.draw(surface) # Test events sb.update(PygameEventUtils.key(pygame.K_PAGEDOWN, keydown=True)) self.assertEqual(sb.get_value(), 964) sb.update(PygameEventUtils.key(pygame.K_PAGEUP, keydown=True)) self.assertEqual(sb.get_value(), 50) self.assertEqual(sb._last_mouse_pos, (-1, -1)) sb.update(PygameEventUtils.enter_window()) self.assertEqual(sb._last_mouse_pos, (-1, -1)) sb.update(PygameEventUtils.leave_window()) self.assertEqual(sb._last_mouse_pos, pygame.mouse.get_pos()) self.assertFalse(sb.scrolling) sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), evtype=pygame.MOUSEBUTTONDOWN)) self.assertTrue(sb.scrolling) sb.update(PygameEventUtils.mouse_click(1, 1)) self.assertFalse(sb.scrolling) self.assertEqual(sb.get_value(), 50) sb.update(PygameEventUtils.middle_rect_click(sb.get_rect(to_absolute_position=True), evtype=pygame.MOUSEBUTTONDOWN)) self.assertEqual(sb.get_value(), 964) sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), evtype=pygame.MOUSEBUTTONDOWN)) self.assertTrue(sb.scrolling) sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), button=4, evtype=pygame.MOUSEBUTTONDOWN)) self.assertEqual(sb.get_value(), 875) sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), button=5, evtype=pygame.MOUSEBUTTONDOWN)) self.assertEqual(sb.get_value(), 964) self.assertEqual(sb.get_value_percentage(), 0.522) # Test mouse motion while scrolling sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), button=5, delta=(0, 50), rel=(0, 10), evtype=pygame.MOUSEMOTION)) self.assertEqual(sb.get_value_percentage(), 0.547) sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), button=5, delta=(0, 50), rel=(0, -10), evtype=pygame.MOUSEMOTION)) self.assertEqual(sb.get_value_percentage(), 0.522) sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), button=5, delta=(0, 50), rel=(0, 999), evtype=pygame.MOUSEMOTION)) self.assertEqual(sb.get_value_percentage(), 1) sb.readonly = True self.assertFalse(sb.update([])) # Ignore events if mouse outside the region sb.update(PygameEventUtils.middle_rect_click(sb.get_slider_rect(), button=5, delta=(0, 999), rel=(0, -10), evtype=pygame.MOUSEMOTION)) self.assertIn(sb.get_value_percentage(), (0.976, 1)) # Test remove onreturn sb = ScrollBar(length, world_range, 'sb', ORIENTATION_VERTICAL, onreturn=-1) self.assertIsNone(sb._onreturn) self.assertTrue(sb._kwargs.get('onreturn', 0)) # Scrollbar ignores scaling self.assertRaises(WidgetTransformationNotImplemented, lambda: sb.scale(2, 2)) self.assertFalse(sb._scale[0]) self.assertRaises(WidgetTransformationNotImplemented, lambda: sb.resize(2, 2)) self.assertFalse(sb._scale[0]) self.assertRaises(WidgetTransformationNotImplemented, lambda: sb.set_max_width(10)) self.assertIsNone(sb._max_width[0]) self.assertRaises(WidgetTransformationNotImplemented, lambda: sb.set_max_height(10)) self.assertIsNone(sb._max_height[0]) sb._apply_font() sb.set_padding(10) self.assertEqual(sb._padding[0], 0) self.assertRaises(WidgetTransformationNotImplemented, lambda: sb.rotate(10)) self.assertEqual(sb._angle, 0) self.assertRaises(WidgetTransformationNotImplemented, lambda: sb.flip(True, True)) self.assertFalse(sb._flip[0]) self.assertFalse(sb._flip[1]) # Set minimum sb.set_minimum(0.5 * sb._values_range[1]) # Test hide sb._mouseover = True sb.hide()
def __init__(self, area_width: int, area_height: int, area_color: Optional[Union[ColorType, '_baseimage.BaseImage']] = None, extend_x: int = 0, extend_y: int = 0, menubar: Optional['MenuBar'] = None, scrollbar_color: ColorType = (235, 235, 235), scrollbar_slider_color: ColorType = (200, 200, 200), scrollbar_slider_pad: NumberType = 0, scrollbar_thick: NumberType = 20, scrollbars: Union[str, Tuple[str, ...]] = get_scrollbars_from_position(_locals.POSITION_SOUTHEAST), shadow: bool = False, shadow_color: ColorType = (0, 0, 0), shadow_offset: NumberType = 2, shadow_position: str = _locals.POSITION_SOUTHEAST, world: Optional['pygame.Surface'] = None ) -> None: assert isinstance(area_width, int) assert isinstance(area_height, int) assert isinstance(scrollbar_slider_pad, (int, float)) assert isinstance(scrollbar_thick, (int, float)) assert isinstance(shadow, bool) assert isinstance(shadow_offset, (int, float)) assert isinstance(world, (pygame.Surface, type(None))) assert_color(scrollbar_color) assert_color(scrollbar_slider_color) assert_color(shadow_color) assert_position(shadow_position) assert area_width > 0 and area_height > 0, \ 'area size must be greater than zero' self._bg_surface = None self._decorator = Decorator(self) self._rect = pygame.Rect(0, 0, int(area_width), int(area_height)) self._scrollbar_positions = tuple(set(scrollbars)) # Ensure unique self._scrollbar_thick = scrollbar_thick self._scrollbars = [] self._world = world self._extend_x = extend_x self._extend_y = extend_y self._menubar = menubar if area_color: self._bg_surface = make_surface(width=area_width + extend_x, height=area_height + self._extend_y) if isinstance(area_color, _baseimage.BaseImage): area_color.draw(surface=self._bg_surface, area=self._bg_surface.get_rect()) else: self._bg_surface.fill(area_color) self._view_rect = self.get_view_rect() for pos in self._scrollbar_positions: assert_position(pos) if pos == _locals.POSITION_EAST or pos == _locals.POSITION_WEST: sbar = ScrollBar( length=self._view_rect.height, values_range=(0, max(1, self.get_hidden_height())), orientation=_locals.ORIENTATION_VERTICAL, slider_pad=scrollbar_slider_pad, slider_color=scrollbar_slider_color, page_ctrl_thick=scrollbar_thick, page_ctrl_color=scrollbar_color, onchange=self._on_vertical_scroll ) else: sbar = ScrollBar( length=self._view_rect.width, values_range=(0, max(1, self.get_hidden_width())), slider_pad=scrollbar_slider_pad, slider_color=scrollbar_slider_color, page_ctrl_thick=scrollbar_thick, page_ctrl_color=scrollbar_color, onchange=self._on_horizontal_scroll ) sbar.set_shadow( enabled=shadow, color=shadow_color, position=shadow_position, offset=shadow_offset ) sbar.set_controls(joystick=False) self._scrollbars.append(sbar) self._apply_size_changes() # Menu reference self._menu = None
def create_rect(self, width: int, height: int) -> None: """ Create rect object. :param width: Area width :param height: Area height """ assert isinstance(width, int) assert isinstance(height, int) self._rect = pygame.Rect(0, 0, int(width), int(height)) self._scrollbars = [] self._view_rect = self.get_view_rect() # Unpack properties (scrollbar_color, scrollbar_thick, scrollbar_slider_color, scrollbar_slider_hover_color, scrollbar_slider_pad, scrollbar_cursor, shadow, shadow_color, shadow_position, shadow_offset, controls_joystick, controls_mouse, controls_touchscreen, controls_keyboard) = self._scrollbars_props for pos in self._scrollbar_positions: assert_position(pos) if pos == POSITION_EAST or pos == POSITION_WEST: sbar = ScrollBar( length=self._view_rect.height, onchange=self._on_vertical_scroll, orientation=ORIENTATION_VERTICAL, page_ctrl_color=scrollbar_color, page_ctrl_thick=scrollbar_thick, slider_color=scrollbar_slider_color, slider_hover_color=scrollbar_slider_hover_color, slider_pad=scrollbar_slider_pad, values_range=(0, max(1, self.get_hidden_height()))) else: sbar = ScrollBar( length=self._view_rect.width, onchange=self._on_horizontal_scroll, page_ctrl_color=scrollbar_color, page_ctrl_thick=scrollbar_thick, slider_color=scrollbar_slider_color, slider_hover_color=scrollbar_slider_hover_color, slider_pad=scrollbar_slider_pad, values_range=(0, max(1, self.get_hidden_width()))) sbar.set_shadow(enabled=shadow, color=shadow_color, position=shadow_position, offset=shadow_offset) sbar.set_controls(joystick=controls_joystick, mouse=controls_mouse, touchscreen=controls_touchscreen, keyboard=controls_keyboard) sbar.set_cursor(cursor=scrollbar_cursor) sbar.set_scrollarea(self) sbar.configured = True sbar.hide() self._scrollbars.append(sbar) self._apply_size_changes()
class HelpWindow: def __init__(self): self.__surface = pygame.Surface(config.help_window_size) self.size = config.help_window_size self.width = config.help_window_size[0] self.height = config.help_window_size[1] self.font = pygame.font.Font(None, config.help_window_font_size) self.__scrollbar = ScrollBar(self.height, (0, 1), '', _locals.ORIENTATION_VERTICAL, 2, config.field_color, config.scrollbar_size[1], (253, 246, 220)) self.__scrollbar.set_shadow(color=(0, 0, 0), position=_locals.POSITION_NORTHWEST, offset=2) self.__scrollbar.set_controls(False) self.__scrollbar.set_position(self.width - config.scrollbar_size[0], 0) self.text = '' self.default_text_coordinates = (10, 10) self.text_rects = [] self.text_lines_surf = [] self.add_log(config.battleship_rules_text, False) self.counter = 0 def update(self, screen): self.__surface.fill(config.help_window_color) # self.update_scrollbar(screen, event) self.__scrollbar.draw(self.__surface) self.draw_text() trunc_world_orig = (0, self.__scrollbar.get_value()) trunc_world = self.size screen.blit(self.__surface, config.help_window_coordinates, (trunc_world_orig, trunc_world)) def update_scrollbar(self, event): if event.type in (pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP, pygame.MOUSEMOTION): event.pos = event.pos[0] - config.help_window_coordinates[0], \ event.pos[1] - config.help_window_coordinates[1] self.__scrollbar.update([event]) self.__scrollbar.draw(self.__surface) def change_height(self, new_height): new_scrollbar_max = self.__scrollbar.get_maximum() + (new_height - self.height) self.height = new_height self.__surface = pygame.transform.scale(self.__surface, (self.width, self.height)) self.__scrollbar.set_length(self.height) self.__scrollbar.set_maximum(new_scrollbar_max) def add_log(self, text, game_log=True, turn_log=False): if turn_log: self.counter += 1 text = str(self.counter) + '. ' + text text = self.process_text(text) self.add_text(text, game_log) def add_text(self, text, game_log): x, y = self.default_text_coordinates lines = text.splitlines() for line in lines: line_sprite = self.font.render(line, True, pygame.Color("black")) self.text_lines_surf.append(line_sprite) self.text_rects.append(line_sprite.get_rect()) self.text_rects[0].x, self.text_rects[0].y = 0, 0 for rect in self.text_rects: rect.y = y rect.x = x y += config.help_window_font_size # self.text_rects.y = self.default_text_coordinates[1] if y >= self.height: self.change_height(self.height + config.help_window_font_size) if game_log: self.__scrollbar.set_value(self.__scrollbar.get_maximum()) def draw_text(self): for i in range(0, len(self.text_lines_surf)): self.__surface.blit(self.text_lines_surf[i], self.text_rects[i]) def process_text(self, text): max_in_row = int(self.width / config.help_window_font_size * 2) counter = 0 last_space = 0 for i in range(0, len(text)): if text[i] == '\n': counter = 0 if counter >= max_in_row: text = text[0:last_space] + '\n' + text[last_space + 1:] counter = 0 i = last_space + 1 if text[i] == ' ': last_space = i counter += 1 return text def return_to_default(self): self.text_rects.clear() self.text_lines_surf.clear() self.add_log(config.battleship_rules_text, False) self.counter = 0 self.change_height(config.help_window_size[1])