예제 #1
0
 def test_lineinput_dialog(self):
     conf = ui.UiConfig.instance(game=self.game)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         ui.LineInputDialog("123", 123, config=conf)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         ui.LineInputDialog(123, "123", config=conf)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         ui.LineInputDialog(default=12, config=conf)
     ld = ui.LineInputDialog("title", "test line input", config=conf)
     self.assertEqual(ld.label.text, "test line input")
     self.assertEqual(ld.title, "title")
     ld.label = "test 2"
     self.assertEqual(ld.label.text, "test 2")
     ld.label = pgl_base.Text("test 3")
     self.assertEqual(ld.label.text, "test 3")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         ld.label = 3
     ld.title = "test"
     self.assertEqual(ld.title, "test")
     ld.title = pgl_base.Text("test")
     self.assertEqual(ld.title, "test")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         ld.title = 3
     self.game.screen.place(ld, 0, 0)
     self.game.screen.update()
     ld = ui.LineInputDialog(pgl_base.Text("title"),
                             "test line input",
                             config=conf)
     self.assertEqual(ld.title, "title")
예제 #2
0
    def test_multiline_input_dialog(self):
        conf = ui.UiConfig.instance(game=self.game)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            ui.MultiLineInputDialog(fields=1, title="test", config=conf)
        mld = ui.MultiLineInputDialog(title="test", config=conf)
        self.assertListEqual(
            mld.fields,
            [{
                "label": "Input a value:",
                "default": "",
                "filter": constants.PRINTABLE_FILTER,
                "user_input": "",
            }],
        )
        fields = [
            {
                "label": pgl_base.Text("Input a value:"),
                "default": "",
                "filter": constants.PRINTABLE_FILTER,
                "user_input": "",
            },
            {
                "label": "test",
                "default": "",
                "filter": constants.PRINTABLE_FILTER,
                "user_input": "",
            },
            {
                "label": "test longer very much longer. for sure.",
                "default": "",
                "filter": constants.PRINTABLE_FILTER,
                "user_input": "",
            },
        ]
        mld.fields = fields
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            mld.fields = 42

        self.game.screen.place(mld, 0, 0)
        self.game.screen.update()
        mld.title = "test change"
        self.assertEqual(mld.title, "test change")
        mld.title = pgl_base.Text("test change")
        self.assertEqual(mld.title, "test change")
        mld = ui.MultiLineInputDialog(fields, title="test", config=conf)
        self.assertEqual(mld.title, "test")
        mld = ui.MultiLineInputDialog(fields,
                                      title=pgl_base.Text("test"),
                                      config=conf)
        self.assertEqual(mld.title, "test")
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            mld = ui.MultiLineInputDialog(fields, title=42, config=conf)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            mld.title = 42
        mld = ui.MultiLineInputDialog(fields, title=None, config=conf)
        self.assertEqual(mld.title, "")
예제 #3
0
 def test_text(self):
     text = pgl_base.Text(
         "Test case",
         core.Color(0, 0, 0),
         core.Color(255, 255, 0),
         pgl_base.Style.BRIGHT,
     )
     t = pgl_base.Console.instance()
     self.assertEqual(
         text.__repr__(),
         "".join([
             t.on_color_rgb(255, 255, 0),
             t.color_rgb(0, 0, 0),
             pgl_base.Style.BRIGHT,
             "Test case",
             "\x1b[0m",
         ]),
     )
     self.assertIn("TEST", text.black("TEST"))
     self.assertIn("30m", text.black("TEST"))
     self.assertIn("TEST", text.red("TEST"))
     self.assertIn("31m", text.red("TEST"))
     self.assertIn("TEST", text.green("TEST"))
     self.assertIn("32m", text.green("TEST"))
     self.assertIn("TEST", text.yellow("TEST"))
     self.assertIn("33m", text.yellow("TEST"))
     self.assertIn("TEST", text.blue("TEST"))
     self.assertIn("34m", text.blue_bright("TEST"))
     self.assertIn("TEST", text.red_bright("TEST"))
     self.assertIn("TEST", text.green_bright("TEST"))
     self.assertIn("TEST", text.yellow_bright("TEST"))
     self.assertIn("TEST", text.magenta_bright("TEST"))
     self.assertIn("TEST", text.cyan_bright("TEST"))
     self.assertIn("TEST", text.white_bright("TEST"))
     self.assertIn("TEST", text.black_bright("TEST"))
     self.assertIn("TEST", text.magenta("TEST"))
     self.assertIn("TEST", text.cyan("TEST"))
     self.assertIn("TEST", text.white("TEST"))
     self.assertIn("TEST", text.red_dim("TEST"))
     self.assertIn("TEST", text.blue_dim("TEST"))
     self.assertIn("TEST", text.green_dim("TEST"))
     self.assertIn("TEST", text.yellow_dim("TEST"))
     self.assertIn("TEST", text.magenta_dim("TEST"))
     self.assertIn("TEST", text.cyan_dim("TEST"))
     self.assertIn("TEST", text.white_dim("TEST"))
     self.assertIn("TEST", text.black_dim("TEST"))
     self.assertIsNone(text.warn("test"))
     self.assertIsNone(text.fatal("test"))
     self.assertIsNone(text.info("test"))
     self.assertIsNone(text.debug("test"))
     self.assertIsNone(text.print_white_on_red("test"))
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pgl_base.Text("breaking", "pink")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pgl_base.Text("breaking", None, "pink")
예제 #4
0
 def setUp(self):
     super().setUp()
     self.vectors = []
     self.vectors.append(pgl_base.Vector2D())
     self.vectors.append(pgl_base.Vector2D(1.0, 1.0))
     self.vectors.append(pgl_base.Vector2D(2, 2))
     self.text = pgl_base.Text(
         "Test case",
         core.Color(0, 0, 0),
         core.Color(255, 255, 0),
         pgl_base.Style.BRIGHT,
     )
     self.math = pgl_base.Math()
예제 #5
0
 def setUp(self):
     super().setUp()
     self.vectors = []
     self.vectors.append(pgl_base.Vector2D())
     self.vectors.append(pgl_base.Vector2D(1.0, 1.0))
     self.vectors.append(pgl_base.Vector2D(2, 2))
     self.text = pgl_base.Text(
         "Test case",
         pgl_base.Fore.WHITE,
         pgl_base.Back.YELLOW,
         pgl_base.Style.BRIGHT,
     )
     self.math = pgl_base.Math()
예제 #6
0
 def test_message_dialog(self):
     conf = ui.UiConfig.instance(game=self.game)
     # Test when height is none and adpatative_height is False (it will be tuned on)
     md = ui.MessageDialog(
         [
             "Test",
             "Message dialog",
             FakeText("fake"),
             pgl_base.Text("Another test"),
             core.Sprixel("!"),
         ],
         adaptive_height=False,
         config=conf,
     )
     self.assertEqual(md.title, "")
     md.title = "test"
     self.assertEqual(md.title, "test")
     md.title = pgl_base.Text("test")
     self.assertEqual(md.title, "test")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         md.title = 42
     self.game.screen.place(md, 0, 0)
     self.game.screen.update()
     md.add_line("test 2", constants.ALIGN_RIGHT)
     md.add_line("test 3", constants.ALIGN_CENTER)
     md.add_line(pgl_base.Text("test 4"), constants.ALIGN_RIGHT)
     md.add_line(pgl_base.Text("test 5"), constants.ALIGN_CENTER)
     self.game.screen.force_update()
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         md.add_line(1, constants.ALIGN_CENTER)
     self.assertIs(type(md.height), int)
     # Now test with fixed height
     md = ui.MessageDialog(
         [
             "Test",
             "Message dialog",
             FakeText("fake"),
             pgl_base.Text("Another test"),
             core.Sprixel("!"),
         ],
         adaptive_height=False,
         height=10,
         config=conf,
     )
     self.assertEqual(md.height, 10)
     md.height = 6
     self.assertEqual(md.height, 6)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         md.height = "6"
     md = ui.MessageDialog(title=pgl_base.Text("test"), config=conf)
     self.assertEqual(md.title, "test")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         ui.MessageDialog(title=42, config=conf)
예제 #7
0
    def test_screen_buffer(self):
        sprites_panda = SpriteCollection.load_json_file("tests/panda.spr")
        b = engine.Board(size=[20, 20])
        s = engine.Screen()
        # This is a dirty hack for CircleCI as it returns a 0x0 screen size.
        if s.width <= 0 or s.height <= 0:
            s._display_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
            s._screen_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
        # Because CircleCI return a console with no size (most probably because we are
        # not attached to any terminal), we need to make sure that the partial display
        # tests work in that environment too
        screen_width = 0
        screen_height = 0
        if s.width <= 0:
            screen_width = 50
        else:
            screen_width = s.width
        if s.height <= 0:
            screen_height = 50
        else:
            screen_height = s.height
        self.assertEqual(s.vcenter, int(s.height / 2))
        self.assertEqual(s.hcenter, int(s.width / 2))
        b.place_item(board_items.Tile(sprite=sprites_panda["panda"]), 0, 0)
        self.assertIsInstance(b.render_cell(1, 1), Sprixel)
        b.item(19, 19).model = "@"
        b.item(19, 19).sprixel = None
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        self.assertEqual(b.render_cell(19, 19), Sprixel())
        with self.assertRaises(base.PglOutOfBoardBoundException):
            b.render_cell(50, 50)
        self.assertIsNone(s.clear_buffers())
        self.assertIsNone(s.clear_screen_buffer())
        # And again after clear buffers.
        if s.width <= 0 or s.height <= 0:
            s._display_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
            s._screen_buffer = np.array(
                [[Sprixel(" ") for i in range(0, 50, 1)] for j in range(0, 50, 1)]
            )
        self.assertTrue(s._is_dirty)
        self.assertTrue(functions.pgl_isinstance(s.buffer, "numpy.ndarray"))
        self.assertIsNone(s.update())
        b = engine.Board(size=[1, 1])
        b.place_item(board_items.Wall(model="##"), 0, 0)
        self.assertIsNone(s.place("test", 0, 0))
        self.assertIsNone(s.place(b, 1, 0))
        t = base.Text("test 2")
        self.assertIsNone(s.place(t, 2, 0))
        self.assertIsNone(s.place(sprites_panda["panda"], 0, 5))
        self.assertIsNone(s.place(TB(), 3, 0))
        self.assertIsNone(s.place(board_items.BoardItem(model="##"), 10, 0))
        self.assertIsNone(
            s.place(
                board_items.Tile(
                    sprixels=[
                        [Sprixel("##"), Sprixel("##")],
                        [Sprixel("##"), Sprixel("##")],
                    ]
                ),
                4,
                0,
            )
        )
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(None, 0, 0)
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(1, 0, 0)
        s.update()
        t.text = "update"
        self.assertIsNone(
            s.place(sprites_panda["panda"], screen_height - 2, screen_width - 2)
        )
        self.assertIsNone(s.place("test", 1, screen_width - 2))
        s.update()
        self.assertIsNone(s.render())  # Should not render
        self.assertFalse(s.need_rendering)
        s.trigger_rendering()
        self.assertTrue(s.need_rendering)
        # Now testing partial display
        camera = board_items.Camera()
        camera.row = 0
        camera.column = 0

        b = engine.Board(
            size=[screen_width * 2, screen_height * 2],
            enable_partial_display=True,
            partial_display_viewport=[
                int(screen_height / 2) - 1,
                int(screen_width / 2) - 1,
            ],
            partial_display_focus=camera,
            DISPLAY_SIZE_WARNINGS=False,
        )
        for row in range(0, b.height):
            for col in range(0, b.width):
                b.place_item(
                    board_items.Wall(
                        sprixel=Sprixel(" ", Color(row * 4, col, int((row + col) / 2))),
                    ),
                    row,
                    col,
                )
        self.assertIsNone(s.place(b, 0, 0, 2))
        s.trigger_rendering()
        self.assertIsNone(s.update())
        b.partial_display_viewport = [
            int(screen_height * 3) - 1,
            int(screen_width * 3) - 1,
        ]
        camera.row += 1
        with self.assertRaises(IndexError):
            s.force_render()
            s.update()
        b.partial_display_viewport = [
            int(screen_height / 2) - 1,
            int(screen_width / 2) - 1,
        ]
        camera.row = b.height - 1
        camera.column = b.width - 1
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.update())
        camera = board_items.Tile(
            sprite=Sprite(
                sprixels=[[Sprixel("+"), Sprixel("+")], [Sprixel("+"), Sprixel("+")]]
            )
        )
        b.partial_display_focus = camera
        # Please never do that in real life...
        camera.pos = [1, 1]
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.render())
        self.assertIsNone(s.update())
        # This will succeed but the str type cannot benefit from deferred rendering.
        self.assertIsNone(s.place("test delete", 0, 0, 2))
        self.assertIsNone(s.update())
        self.assertIsNone(s.delete(0, 0))
        self.assertIsNone(s.update())
예제 #8
0
    )

    g.screen.place(g.current_board(), 35, 55)
    draw_box(g, 35, 1, g.current_board().height, 40, "Benchmark")
    draw_box(
        g,
        2,
        polus_map.partial_display_viewport[1] * 2 + 2,
        polus_map.partial_display_viewport[0] * 2,
        g.screen.width - polus_map.partial_display_viewport[1] * 2 - 2,
        "More info",
    )
    g.screen.place(
        "Currently running benchmark:", 3, polus_map.partial_display_viewport[1] * 2 + 3
    )
    text_phase = base.Text("Phase 1", core.Color(255, 128, 0))
    g.screen.place(text_phase, 3, polus_map.partial_display_viewport[1] * 2 + 33)
    bench_status = base.Text(
        "One board, multiple sprites, NPC movement computed",
        core.Color(0, 128, 255),
        style=constants.BOLD,
    )
    g.screen.place(bench_status, 4, polus_map.partial_display_viewport[1] * 2 + 5)
    g.screen.place(
        f"Baseline FPS (calc. in splashscreen): {baseline_fps}",
        6,
        polus_map.partial_display_viewport[1] * 2 + 3,
    )
    g.screen.place(
        "Remaining frames to render:", 8, polus_map.partial_display_viewport[1] * 2 + 3
    )
예제 #9
0
 def test_progressbar(self):
     conf = ui.UiConfig.instance(game=self.game)
     conf.borderless_dialog = False
     pb = ui.ProgressDialog(
         pgl_base.Text("test"),
         0,
         100,
         20,
         core.Sprixel("="),
         core.Sprixel("-"),
         True,
         True,
         conf,
     )
     pb.label = "test"
     self.assertEqual(pb.label, "test")
     pb.label = pgl_base.Text("Test text")
     self.assertEqual(pb.label, "Test text")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.label = 2
     self.assertEqual(pb.value, 0)
     pb.value = 2
     self.assertEqual(pb.value, 2)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.value = "3"
     self.assertEqual(pb.maximum, 100)
     pb.maximum = 20
     self.assertEqual(pb.maximum, 20)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.maximum = "3"
     self.game.screen.place(pb, 0, 0)
     self.game.screen.update()
     pb.value = 20
     self.game.screen.force_update()
     self.game.screen.force_update()
     pb = ui.ProgressBar(
         0,
         100,
         20,
         core.Sprixel("="),
         core.Sprixel("-"),
         conf,
     )
     self.assertIsInstance(pb.config, ui.UiConfig)
     pb.config = ui.UiConfig.instance()
     self.assertIsInstance(pb.config, ui.UiConfig)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.config = 3
     self.assertIsInstance(pb.progress_marker, core.Sprixel)
     self.assertIsInstance(pb.empty_marker, core.Sprixel)
     pb.progress_marker = "#"
     self.assertEqual(pb.progress_marker, "#")
     pb.progress_marker = pgl_base.Text("#")
     self.assertEqual(pb.progress_marker, "#")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.progress_marker = 3
     pb.empty_marker = "#"
     self.assertEqual(pb.empty_marker, "#")
     pb.empty_marker = pgl_base.Text("#")
     self.assertEqual(pb.empty_marker, "#")
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.empty_marker = 3
     pb.progress_marker = core.Sprixel("=")
     pb.empty_marker = core.Sprixel("-")
     self.assertIsInstance(pb.progress_marker, core.Sprixel)
     self.assertIsInstance(pb.empty_marker, core.Sprixel)
     self.assertEqual(pb.value, 0)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.value = "10"
     self.assertEqual(pb.maximum, 100)
     pb.maximum = 10
     self.assertEqual(pb.maximum, 10)
     with self.assertRaises(pgl_base.PglInvalidTypeException):
         pb.maximum = "10"
예제 #10
0
    def test_menus(self):
        screen = self.game.screen
        conf = ui.UiConfig.instance(game=self.game)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            ui.MenuBar([42], config=conf)
        menubar = ui.MenuBar([ui.MenuAction("Default", fake_callback)],
                             config=conf)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            menubar.add_entry(42)
        menubar.spacing = 0
        self.assertEqual(menubar.spacing, 0)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            menubar.spacing = "42"
        menubar.config = conf
        self.assertIsInstance(menubar.config, ui.UiConfig)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            menubar.config = "42"
        menubar.current_index = 0
        self.assertEqual(menubar.current_index, 0)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            menubar.current_index = "42"
        file_menu = ui.Menu(
            "File",
            [
                ui.MenuAction("Open", fake_callback),
                ui.MenuAction(pgl_base.Text("Save"), fake_callback),
                ui.MenuAction("Save as", fake_callback),
                ui.MenuAction("Quit", fake_callback),
            ],
            config=conf,
        )
        edit_menu = ui.Menu(
            pgl_base.Text("Edit", core.Color(0, 255, 255)),
            [ui.MenuAction("Copy", fake_callback)],
        )
        menubar.add_entry(file_menu)
        menubar.add_entry(edit_menu)
        help_action = ui.MenuAction("Help", fake_callback)
        menubar.add_entry(help_action)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            ui.Menu(42)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            edit_menu.add_entry(42)
        edit_menu.add_entry(ui.MenuAction("Paste", fake_callback))
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            ui.Menu("42", ["this", "is", "not going to", "work"])
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            ui.MenuAction(42, fake_callback)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            ui.MenuAction("42", 42)

        edit_menu.title = "*E*dit"
        self.assertEqual(edit_menu.title.text, "*E*dit")
        edit_menu.title = pgl_base.Text("Edit")
        self.assertEqual(edit_menu.title.text, "Edit")
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            edit_menu.title = 42

        help_action.title = "Help (Shift+H)"
        self.assertEqual(help_action.title.text, "Help (Shift+H)")
        help_action.title = pgl_base.Text("Help")
        self.assertEqual(help_action.title.text, "Help")
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            help_action.title = 42

        help_action.action = fake_callback
        self.assertTrue(callable(help_action.action))
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            help_action.action = 42
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            help_action.config = 42
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            help_action.padding = "42"
        help_action.padding = 2
        self.assertEqual(help_action.padding, 2)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            help_action.selected = 42
        help_action.selected = True
        self.assertTrue(help_action.selected)

        edit_menu.padding = 0
        self.assertEqual(edit_menu.padding, 0)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            edit_menu.padding = "42"
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            edit_menu.config = "42"
        edit_menu.selected = True
        self.assertTrue(edit_menu.selected)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            edit_menu.selected = 42
        export_menu = ui.Menu(
            "Export",
            [
                ui.MenuAction("Sprite (.spr)", fake_callback),
                ui.MenuAction("PNG (.png)", fake_callback),
                ui.MenuAction("Animation as a GIF (.gif)", fake_callback),
            ],
        )
        edit_menu.add_entry(export_menu)
        export_menu.selected = True
        self.assertTrue(export_menu.selected)
        self.assertTrue(type(export_menu.entries) is list)
        export_menu.entries = [
            ui.MenuAction("Sprite (.spr)", fake_callback),
            ui.MenuAction("PNG (.png)", fake_callback),
            ui.MenuAction("Animation as a GIF (.gif)", fake_callback),
        ]
        self.assertEqual(len(export_menu.entries), 3)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            export_menu.entries = 42
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            export_menu.entries = [42]
        edit_menu.select_next()
        self.assertIsInstance(edit_menu.current_entry(), ui.MenuAction)
        edit_menu.select_previous()
        self.assertIsNone(edit_menu.current_entry())

        menubar.select_next()
        entry = menubar.current_entry()
        self.assertTrue(entry.selected)
        menubar.current_index += 1
        self.assertTrue(menubar.current_entry().selected)
        menubar.select_previous()
        self.assertTrue(menubar.current_entry().selected)
        self.assertTrue(type(menubar.length()) is int)
        menubar.close()
        self.assertEqual(menubar.current_index, -1)

        self.assertIsNone(edit_menu.expand())
        # Screen update
        screen.place(menubar, 0, 0)
        screen.update()
        self.assertIsNone(edit_menu.collapse())
        help_action.padding = 0
        screen.force_update()

        menubar.entries = [
            ui.MenuAction("Sprite (.spr)", fake_callback),
            ui.MenuAction("PNG (.png)", fake_callback),
            ui.MenuAction("Animation as a GIF (.gif)", fake_callback),
        ]
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            menubar.entries = 42
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            menubar.entries = [42]
예제 #11
0
    def test_gridselector(self):
        conf = ui.UiConfig.instance(game=self.game)
        gd = ui.GridSelectorDialog(["a", "b", "c", "##"],
                                   10,
                                   20,
                                   "test",
                                   config=conf)
        self.assertEqual(gd.grid_selector.current_page, 0)
        gd.grid_selector.current_page = 1
        self.assertEqual(gd.grid_selector.current_page, 0)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            ui.GridSelectorDialog(
                ["a", "b", pgl_base.Text("c"), "##"],
                10,
                20,
                "test",
                config=conf)

        self.assertEqual(gd.title, "test")
        gd.title = "test 2"
        self.assertEqual(gd.title, "test 2")
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.title = 42
        self.assertIsInstance(gd.grid_selector, ui.GridSelector)
        self.assertEqual(len(gd.grid_selector.choices), 4)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.grid_selector.choices = 3

        gd.grid_selector.choices = ["a", "b", "c"]
        self.assertEqual(len(gd.grid_selector.choices), 3)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.grid_selector.choices = "42"
        gd.grid_selector.max_height = 20
        self.assertEqual(gd.grid_selector.max_height, 20)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.grid_selector.max_height = "42"
        gd.grid_selector.max_width = 20
        self.assertEqual(gd.grid_selector.max_width, 20)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.grid_selector.max_width = "42"
        self.assertEqual(gd.grid_selector.current_choice, 0)
        gd.grid_selector.current_choice = 1
        self.assertEqual(gd.grid_selector.current_choice, 1)
        self.assertIsInstance(gd.grid_selector.current_sprixel(), core.Sprixel)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.grid_selector.current_choice = "42"

        gd.grid_selector.cursor_down()
        self.assertEqual(gd.grid_selector.current_choice, 1)
        gd.grid_selector.cursor_up()
        self.assertEqual(gd.grid_selector.current_choice, 1)
        gd.grid_selector.cursor_right()
        self.assertEqual(gd.grid_selector.current_choice, 2)
        gd.grid_selector.cursor_left()
        self.assertEqual(gd.grid_selector.current_choice, 1)
        gd.grid_selector.max_width = 3
        gd.grid_selector.max_height = 3
        gd.grid_selector.page_down()
        self.assertEqual(gd.grid_selector.current_page, 1)
        gd.grid_selector.page_up()
        self.assertEqual(gd.grid_selector.current_page, 0)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.grid_selector.current_page = "42"

        self.game.screen.place(gd, 0, 0)
        self.game.screen.update()

        conf.borderless_dialog = not conf.borderless_dialog
        self.assertIsInstance(
            ui.GridSelectorDialog(["a", "b", "c", "##"],
                                  10,
                                  20,
                                  "test",
                                  config=conf),
            ui.GridSelectorDialog,
        )
        conf.borderless_dialog = not conf.borderless_dialog
        gd.grid_selector = gd.grid_selector
        self.assertIsInstance(gd.grid_selector, ui.GridSelector)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            gd.grid_selector = 42
        gd.grid_selector.choices = ["a" for _ in range(30)]
        self.assertEqual(gd.grid_selector.nb_pages(), 15)
예제 #12
0
    def test_screen_buffer(self):
        sprites_panda = SpriteCollection.load_json_file("tests/panda.spr")
        b = engine.Board(size=[20, 20])
        s = engine.Screen(50, 50)
        # Because CircleCI return a console with no size (most probably because we are
        # not attached to any terminal), we need to make sure that the partial display
        # tests work in that environment too
        screen_width = 0
        screen_height = 0
        if s.width <= 0:
            screen_width = 50
        else:
            screen_width = s.width
        if s.height <= 0:
            screen_height = 50
        else:
            screen_height = s.height
        self.assertEqual(s.vcenter, int(s.height / 2))
        self.assertEqual(s.hcenter, int(s.width / 2))
        b.place_item(board_items.Tile(sprite=sprites_panda["panda"]), 0, 0)
        self.assertIsInstance(b.render_cell(1, 1), Sprixel)
        b.item(19, 19).model = "@"
        b.item(19, 19).sprixel = None
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        self.assertEqual(b.render_cell(19, 19), Sprixel())
        b.place_item(board_items.Door(), 19, 19)
        b.place_item(
            board_items.Door(sprixel=Sprixel(
                "*", Color(125, 125, 0), is_bg_transparent=False)),
            19,
            19,
        )
        b.place_item(
            board_items.Door(sprixel=Sprixel("#", is_bg_transparent=True)), 19,
            19)
        b.place_item(
            board_items.NPC(sprixel=Sprixel("$", is_bg_transparent=True)), 19,
            19)
        self.assertEqual(b.layers(19, 19), 4)
        b.place_item(
            board_items.BoardItemVoid(sprixel=Sprixel(is_bg_transparent=True)),
            19, 19)
        b.place_item(
            board_items.BoardItemVoid(sprixel=Sprixel(is_bg_transparent=True)),
            19, 19)
        self.assertIsInstance(b.render_cell(19, 19), Sprixel)
        b._clean_layers(19, 19)
        self.assertEqual(b.layers(19, 19), 3)
        b._clean_layers(18, 19)
        self.assertEqual(b.layers(18, 19), 1)
        with self.assertRaises(base.PglOutOfBoardBoundException):
            b.render_cell(50, 50)
        self.assertIsNone(s.clear_buffers())
        self.assertIsNone(s.clear_screen_buffer())
        self.assertTrue(s._is_dirty)
        self.assertTrue(functions.pgl_isinstance(s.buffer, "numpy.ndarray"))
        self.assertIsNone(s.update())
        b = engine.Board(size=[1, 1])
        b.place_item(board_items.Wall(model="##"), 0, 0)
        self.assertIsNone(s.place("test", 0, 0))
        self.assertIsNone(s.place(b, 1, 0))
        self.assertIsInstance(s.get(1, 0), engine.Board)
        t = base.Text("test 2")
        self.assertIsNone(s.place(t, 2, 0))
        self.assertIsNone(s.place(sprites_panda["panda"], 0, 5))
        self.assertIsNone(s.place(TB(), 3, 0))
        self.assertIsNone(s.place(board_items.BoardItem(model="##"), 10, 0))
        self.assertIsNone(
            s.place(
                board_items.Tile(sprite=Sprite(sprixels=[
                    [Sprixel("##"), Sprixel("##")],
                    [Sprixel("##"), Sprixel("##")],
                ])),
                4,
                0,
            ))
        self.assertIsNone(
            s.place(
                Sprite(sprixels=[
                    [Sprixel("##"), Sprixel("##")],
                    [Sprixel("###"), Sprixel("##")],
                ]),
                8,
                0,
            ))
        s.force_render()
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(None, 0, 0)
        with self.assertRaises(base.PglInvalidTypeException):
            s.place(1, 0, 0)
        with self.assertRaises(base.PglException):
            s.place(TB(), 400, 0)
        with self.assertRaises(base.PglException):
            s.place(TB(), 0, 400)
        s.force_update()
        t.text = "update"
        self.assertIsNone(
            s.place(sprites_panda["panda"], screen_height - 2,
                    screen_width - 2))
        self.assertIsNone(s.place("test", 1, screen_width - 2))
        s.update()
        self.assertIsNone(s.render())  # Should not render
        self.assertFalse(s.need_rendering)
        s.trigger_rendering()
        self.assertTrue(s.need_rendering)
        # Now testing partial display
        camera = board_items.Camera()
        camera.row = 0
        camera.column = 0

        b = engine.Board(
            size=[screen_width * 2, screen_height * 2],
            enable_partial_display=True,
            partial_display_viewport=[
                int(screen_height / 2) - 1,
                int(screen_width / 2) - 1,
            ],
            partial_display_focus=camera,
            DISPLAY_SIZE_WARNINGS=False,
        )
        for row in range(0, b.height):
            for col in range(0, b.width):
                b.place_item(
                    board_items.Wall(sprixel=Sprixel(
                        " ", Color(row * 4, col, int((row + col) / 2))), ),
                    row,
                    col,
                )
        self.assertIsNone(s.place(b, 0, 0, 2))
        s.trigger_rendering()
        self.assertIsNone(s.update())
        b.partial_display_viewport = [
            int(screen_height / 2) - 1,
            int(screen_width / 2) - 1,
        ]
        camera.row = b.height - 1
        camera.column = b.width - 1
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.update())
        camera = board_items.Tile(
            sprite=Sprite(sprixels=[[Sprixel("+"), Sprixel("+")],
                                    [Sprixel("+"), Sprixel("+")]]))
        b.partial_display_focus = camera
        # Please never do that in real life...
        camera.pos = [1, 1]
        self.assertIsNone(s.trigger_rendering())
        self.assertIsNone(s.render())
        self.assertIsNone(s.update())
        # This will succeed but the str type cannot benefit from deferred rendering.
        self.assertIsNone(s.place("test delete", 0, 0, 2))
        self.assertIsNone(s.update())
        self.assertIsNone(s.delete(0, 0))
        self.assertIsNone(s.update())
        self.assertIsNone(
            functions.render_string_to_buffer(
                "hello",
                s._display_buffer,
                0,
                0,
                s._display_buffer.shape[0],
                s._display_buffer.shape[1],
            ))
        # This test has nothing to do here... but I'm lazy.
        self.assertEqual(5, functions.clamp(5, 0, 10))
        self.assertEqual(0, functions.clamp(-5, 0, 10))
        self.assertEqual(10, functions.clamp(15, 0, 10))
        # This one either and it's even worst: it's going to disappear!
        t = base.Text("this is a text\non multiple lines")
        t.render_to_buffer(
            s._display_buffer,
            s._display_buffer.shape[0] - 1,
            s._display_buffer.shape[1] - 5,
            s._display_buffer.shape[0],
            s._display_buffer.shape[1],
        )
        s.update()
        t = base.Text(
            "this is a text",
            Color(0, 0, 0),
            Color(255, 255, 255),
            font=Font("8bits"),
        )
        t.render_to_buffer(
            s._display_buffer,
            s._display_buffer.shape[0] - 1,
            s._display_buffer.shape[1] - 5,
            s._display_buffer.shape[0],
            s._display_buffer.shape[1],
        )
        s.update()
예제 #13
0
    def test_text(self):
        text = pgl_base.Text(
            "Test case",
            core.Color(0, 0, 0),
            core.Color(255, 255, 0),
            pgl_base.Style.BRIGHT,
        )
        self.assertEqual(text.length, 9)
        t = pgl_base.Console.instance()
        self.assertEqual(
            text.__repr__(),
            "".join([
                t.on_color_rgb(255, 255, 0),
                t.color_rgb(0, 0, 0),
                pgl_base.Style.BRIGHT,
                "Test case",
                "\x1b[0m",
            ]),
        )
        self.assertEqual(text.__str__(), text.__repr__())
        self.assertIn("TEST", text.black("TEST"))
        self.assertIn("30m", text.black("TEST"))
        self.assertIn("TEST", text.red("TEST"))
        self.assertIn("31m", text.red("TEST"))
        self.assertIn("TEST", text.green("TEST"))
        self.assertIn("32m", text.green("TEST"))
        self.assertIn("TEST", text.yellow("TEST"))
        self.assertIn("33m", text.yellow("TEST"))
        self.assertIn("TEST", text.blue("TEST"))
        self.assertIn("34m", text.blue_bright("TEST"))
        self.assertIn("TEST", text.red_bright("TEST"))
        self.assertIn("TEST", text.green_bright("TEST"))
        self.assertIn("TEST", text.yellow_bright("TEST"))
        self.assertIn("TEST", text.magenta_bright("TEST"))
        self.assertIn("TEST", text.cyan_bright("TEST"))
        self.assertIn("TEST", text.white_bright("TEST"))
        self.assertIn("TEST", text.black_bright("TEST"))
        self.assertIn("TEST", text.magenta("TEST"))
        self.assertIn("TEST", text.cyan("TEST"))
        self.assertIn("TEST", text.white("TEST"))
        self.assertIn("TEST", text.red_dim("TEST"))
        self.assertIn("TEST", text.blue_dim("TEST"))
        self.assertIn("TEST", text.green_dim("TEST"))
        self.assertIn("TEST", text.yellow_dim("TEST"))
        self.assertIn("TEST", text.magenta_dim("TEST"))
        self.assertIn("TEST", text.cyan_dim("TEST"))
        self.assertIn("TEST", text.white_dim("TEST"))
        self.assertIn("TEST", text.black_dim("TEST"))
        self.assertIsNone(text.warn("test"))
        self.assertIsNone(text.fatal("test"))
        self.assertIsNone(text.info("test"))
        self.assertIsNone(text.debug("test"))
        self.assertIsNone(text.print_white_on_red("test"))
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            pgl_base.Text("breaking", "pink")
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            pgl_base.Text("breaking", None, "pink")
        text.text = pgl_base.Text("Another test")
        self.assertEqual(text.text, "Another test")
        text.text = 12
        self.assertEqual(text.text, "Another test")
        text.bg_color = core.Color(12, 34, 56)
        self.assertEqual(text.bg_color, core.Color(12, 34, 56))
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            text.bg_color = "pink"
        text.fg_color = core.Color(56, 78, 90)
        self.assertEqual(text.fg_color, core.Color(56, 78, 90))
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            text.fg_color = "definitely not pink! (because tests are not copy/pasted)"
        text.bg_color = None
        self.assertIsNone(text.bg_color)
        text.fg_color = None
        self.assertIsNone(text.fg_color)

        text = pgl_base.Text("This is a test", font=core.Font("8bits"))
        self.assertIsNone(text.be_notified(None))
        self.assertIsInstance(text, pgl_base.Text)
        with self.assertRaises(pgl_base.PglInvalidTypeException):
            pgl_base.Text("This is a test", font=42)
        self.assertEqual(text.length, 126)
        text = pgl_base.Text(
            "Serialization test",
            core.Color(255, 0, 0),
            core.Color(0, 200, 200),
            "",
            font=core.Font("8bits"),
        )
        text2 = pgl_base.Text.load(text.serialize())
        self.assertEqual(text.text, text2.text)
        self.assertEqual(text.bg_color, text2.bg_color)
        self.assertEqual(text.fg_color, text2.fg_color)
        self.assertEqual(text.style, text2.style)
        text = pgl_base.Text("Serialization test", style=None)
        text2 = pgl_base.Text.load(text.serialize())
        self.assertEqual(text.text, text2.text)
        self.assertEqual(text.bg_color, text2.bg_color)
        self.assertEqual(text.fg_color, text2.fg_color)
        self.assertEqual(text.style, None)