Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 2 additions & 68 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from arcade.clock import GLOBAL_CLOCK, GLOBAL_FIXED_CLOCK, _setup_clock, _setup_fixed_clock
from arcade.color import TRANSPARENT_BLACK
from arcade.context import ArcadeContext
from arcade.sections import SectionManager
from arcade.types import LBWH, Color, Rect, RGBANormalized, RGBOrA255
from arcade.utils import is_raspberry_pi
from arcade.window_commands import get_display_size, set_window
Expand Down Expand Up @@ -979,37 +978,21 @@ def show_view(self, new_view: View) -> None:
# remove previously shown view's handlers
if self._current_view is not None:
self._current_view.on_hide_view()
if self._current_view.has_sections:
self.remove_handlers(self._current_view.section_manager)
self.remove_handlers(self._current_view)

# push new view's handlers
self._current_view = new_view
if new_view.has_sections:
section_manager_managed_events = new_view.section_manager.managed_events
section_handlers = {
event_type: getattr(new_view.section_manager, event_type, None)
for event_type in section_manager_managed_events
}
if section_handlers:
self.push_handlers(**section_handlers)
else:
section_manager_managed_events = set()

# Note: Excluding on_show because this even can trigger multiple times.
# It should only be called once when the view is shown.
view_handlers = {
event_type: getattr(new_view, event_type, None)
for event_type in self.event_types
if event_type != "on_show"
and event_type not in section_manager_managed_events
and hasattr(new_view, event_type)
if event_type != "on_show" and hasattr(new_view, event_type)
}
if view_handlers:
self.push_handlers(**view_handlers)
self._current_view.on_show_view()
if self._current_view.has_sections:
self._current_view.section_manager.on_show_view()

# Note: After the View has been pushed onto pyglet's stack of event handlers
# (via push_handlers()), pyglet
Expand All @@ -1028,18 +1011,9 @@ def hide_view(self) -> None:
return

self._current_view.on_hide_view()
if self._current_view.has_sections:
self._current_view.section_manager.on_hide_view()
self.remove_handlers(self._current_view.section_manager)
self.remove_handlers(self._current_view)
self._current_view = None

# def _create(self) -> None:
# super()._create()

# def _recreate(self, changes) -> None:
# super()._recreate(changes)

def flip(self) -> None:
"""
Present the rendered content to the screen.
Expand Down Expand Up @@ -1249,7 +1223,7 @@ class View:
Subclassing the window is very inflexible since you can't easily switch
your update and draw logic.

A view is a way to encapsulate that logic so you can easily switch between
A view is a way to encapsulate that logic, so you can easily switch between
different parts of your game. Maybe you have a title screen, a game screen,
and a game over screen. Each of these could be a different view.

Expand All @@ -1261,46 +1235,6 @@ class View:

def __init__(self, window: Window | None = None) -> None:
self.window = arcade.get_window() if window is None else window
self.key: int | None = None
self._section_manager: SectionManager | None = None

@property
def section_manager(self) -> SectionManager:
"""The section manager for this view.

If the view has section manager one will be created.
"""
if self._section_manager is None:
self._section_manager = SectionManager(self)
return self._section_manager

@property
def has_sections(self) -> bool:
"""Returns ``True`` if this view has sections."""
if self._section_manager is None:
return False
else:
return self.section_manager.has_sections

def add_section(
self,
section: arcade.Section,
at_index: int | None = None,
at_draw_order: int | None = None,
) -> None:
"""
Adds a section to the view Section Manager.

Args:
section:
The section to add to this section manager
at_index (optional):
The index to insert the section for event capture and
update events. If ``None`` it will be added at the end.
at_draw_order (optional):
Inserts the section in a specific draw order. Overwrites section.draw_order
"""
self.section_manager.add_section(section, at_index, at_draw_order)

def clear(
self,
Expand Down
61 changes: 42 additions & 19 deletions arcade/examples/sections_demo_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.sections_demo_1
"""

from __future__ import annotations

import arcade
from arcade import SectionManager


class Box(arcade.SpriteSolidColor):
""" This is a Solid Sprite that represents a GREEN Box on the screen """
"""This is a Solid Sprite that represents a GREEN Box on the screen"""

def __init__(self, section):
super().__init__(100, 100, color=arcade.color.APPLE_GREEN)
Expand All @@ -47,8 +49,7 @@ class ScreenPart(arcade.Section):
boundaries (left, bottom, etc.)
"""

def __init__(self, left: int, bottom: int, width: int, height: int,
**kwargs):
def __init__(self, left: int, bottom: int, width: int, height: int, **kwargs):
super().__init__(left, bottom, width, height, **kwargs)

self.selected: bool = False # if this section is selected
Expand All @@ -66,19 +67,26 @@ def on_update(self, delta_time: float):
self.box.on_update(delta_time)

def on_draw(self):
""" Draw this section """
"""Draw this section"""
if self.selected:
# Section is selected when mouse is within its boundaries
arcade.draw_lrbt_rectangle_filled(self.left, self.right, self.bottom,
self.top, arcade.color.GRAY)
arcade.draw_text(f'You\'re are on the {self.name}', self.left + 30,
self.top - 50, arcade.color.BLACK, 16)
arcade.draw_lrbt_rectangle_filled(
self.left, self.right, self.bottom, self.top, arcade.color.GRAY
)
arcade.draw_text(
f"You're are on the {self.name}",
self.left + 30,
self.top - 50,
arcade.color.BLACK,
16,
)

# draw the box
arcade.draw_sprite(self.box)

def on_mouse_drag(self, x: float, y: float, dx: float, dy: float,
_buttons: int, _modifiers: int):
def on_mouse_drag(
self, x: float, y: float, dx: float, dy: float, _buttons: int, _modifiers: int
):
# if we hold a box, then whe move it at the same rate the mouse moves
if self.hold_box:
self.hold_box.position = x, y
Expand Down Expand Up @@ -110,28 +118,43 @@ def on_mouse_leave(self, x: float, y: float):


class GameView(arcade.View):

def __init__(self):
super().__init__()

# add sections to the view
self.section_manager = SectionManager(self)

# 1) First section holds half of the screen
self.add_section(ScreenPart(0, 0, self.window.width / 2,
self.window.height, name='Left'))
self.section_manager.add_section(
ScreenPart(0, 0, self.window.width / 2, self.window.height, name="Left")
)

# 2) Second section holds the other half of the screen
self.add_section(ScreenPart(self.window.width / 2, 0,
self.window.width / 2, self.window.height,
name='Right'))
self.section_manager.add_section(
ScreenPart(
self.window.width / 2, 0, self.window.width / 2, self.window.height, name="Right"
)
)

def on_show_view(self) -> None:
self.section_manager.enable()

def on_hide_view(self) -> None:
self.section_manager.disable()

def on_draw(self):
# clear the screen
self.clear(color=arcade.color.BEAU_BLUE)

# draw a line separating each Section
arcade.draw_line(self.window.width / 2, 0, self.window.width / 2,
self.window.height, arcade.color.BLACK, 1)
arcade.draw_line(
self.window.width / 2,
0,
self.window.width / 2,
self.window.height,
arcade.color.BLACK,
1,
)


def main():
Expand All @@ -148,5 +171,5 @@ def main():
window.run()


if __name__ == '__main__':
if __name__ == "__main__":
main()
59 changes: 38 additions & 21 deletions arcade/examples/sections_demo_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
If Python and Arcade are installed, this example can be run from the command line with:
python -m arcade.examples.sections_demo_2
"""

import random

import arcade
import arcade
from arcade.color import BLACK, BLUE, RED, BEAU_BLUE, GRAY
from arcade.key import W, S, UP, DOWN

Expand All @@ -35,9 +36,8 @@ class Player(arcade.Section):
"""

def __init__(
self, left: int, bottom: int, width: int, height: int,
key_up: int, key_down: int, **kwargs
):
self, left: int, bottom: int, width: int, height: int, key_up: int, key_down: int, **kwargs
):
super().__init__(
left,
bottom,
Expand Down Expand Up @@ -67,19 +67,24 @@ def on_update(self, delta_time: float):

def on_draw(self):
# draw sections info and score
if self.name == 'Left':
keys = 'W and S'
if self.name == "Left":
keys = "W and S"
start_x = self.left + 5
else:
keys = 'UP and DOWN'
keys = "UP and DOWN"
start_x = self.left - 290
arcade.draw_text(
f'Player {self.name} (move paddle with: {keys})',
start_x, self.top - 20, BLUE, 9,
f"Player {self.name} (move paddle with: {keys})",
start_x,
self.top - 20,
BLUE,
9,
)
arcade.draw_text(
f'Score: {self.score}', self.left + 20,
self.bottom + 20, BLUE,
f"Score: {self.score}",
self.left + 20,
self.bottom + 20,
BLUE,
)

# draw the paddle
Expand All @@ -98,7 +103,6 @@ def on_key_release(self, _symbol: int, _modifiers: int):


class Pong(arcade.View):

def __init__(self):
super().__init__()

Expand All @@ -108,15 +112,22 @@ def __init__(self):

# we store each Section
self.left_player: Player = Player(
0, 0, PLAYER_SECTION_WIDTH, self.window.height, key_up=W,
key_down=S, name='Left')
0, 0, PLAYER_SECTION_WIDTH, self.window.height, key_up=W, key_down=S, name="Left"
)
self.right_player: Player = Player(
self.window.width - PLAYER_SECTION_WIDTH, 0, PLAYER_SECTION_WIDTH,
self.window.height, key_up=UP, key_down=DOWN, name='Right')
self.window.width - PLAYER_SECTION_WIDTH,
0,
PLAYER_SECTION_WIDTH,
self.window.height,
key_up=UP,
key_down=DOWN,
name="Right",
)

# add the sections to the SectionManager so Sections start to work
self.add_section(self.left_player)
self.add_section(self.right_player)
self.section_manager = arcade.SectionManager(self)
self.section_manager.add_section(self.left_player)
self.section_manager.add_section(self.right_player)

# add each paddle to the sprite list
self.paddles.append(self.left_player.paddle)
Expand All @@ -139,6 +150,12 @@ def setup(self):
self.left_player.setup()
self.right_player.setup()

def on_show_view(self) -> None:
self.section_manager.enable()

def on_hide_view(self) -> None:
self.section_manager.disable()

def on_update(self, delta_time: float):
self.ball.update() # update the ball

Expand Down Expand Up @@ -168,7 +185,7 @@ def on_update(self, delta_time: float):
self.end_game(self.left_player)

def end_game(self, winner: Player):
""" Called when one player wins """
"""Called when one player wins"""
winner.score += 1 # increment the winner score
self.setup() # prepare a new game

Expand All @@ -185,7 +202,7 @@ def on_draw(self):

def main():
# create the window
window = arcade.Window(title='Two player simple Pong with Sections!')
window = arcade.Window(title="Two player simple Pong with Sections!")

# create the custom View
game = Pong()
Expand All @@ -200,5 +217,5 @@ def main():
window.run()


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading
Loading