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: 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 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 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
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])