|
| 1 | +.. _sections: |
| 2 | + |
| 3 | +Sections |
| 4 | +======== |
| 5 | + |
| 6 | +In a simple game, the whole viewport is used to display the game "map". |
| 7 | +In more advanced games it's fairly normal to have this viewport divided into |
| 8 | +different "sections" with different usages. Areas where different information is |
| 9 | +displayed and processed. For example you can have a menu at the top, some info |
| 10 | +panel at the right and the game main "screen" (the "map") covering the rest of |
| 11 | +the viewport. |
| 12 | + |
| 13 | +To achieve this separation of game logic you have Sections. |
| 14 | +A :class:`Section` is a way to divide a :class:`View` space into smaller parts, |
| 15 | +each one will then receive events redirected depending on configuration and the |
| 16 | +space of the view occupied. |
| 17 | +Sections can isolate code that otherwise goes packed together in a :class:`View` |
| 18 | +. This way the code remains exactly where it belongs and not mixed together with |
| 19 | +code from other parts of the program. |
| 20 | + |
| 21 | +By configuring a :class:`Section` you can capture some events or for example only |
| 22 | +capture certain keys from keyboard events. Also you can configure which |
| 23 | +events are propagated to other underlying sections or even to the view itself. |
| 24 | + |
| 25 | +Sections can also be "modal" meaning that they will capture all the events |
| 26 | +first but draw last and also will prevent other views from receiving the |
| 27 | +``on_update`` event. |
| 28 | + |
| 29 | +Also note that if you don't use sections in your code, nothing changes. Even |
| 30 | +the :class:`SectionManager` is not created if you don't add sections. |
| 31 | + |
| 32 | +**Key features of Sections:** |
| 33 | + |
| 34 | + - Divide the screen into logical components (Sections). |
| 35 | + - Event dispatching: a :class:`Section` will capture mouse events based on the space occupied from the view. Also keyboard events will be captured based on configuration. |
| 36 | + - Prevent dispatching: a :class:`Section` can be configured to prevent dispatching events captured or let events flow to other sections underneath. |
| 37 | + - Event capturing order: based on a :class:`Section` insertion order you can configure the order in which sections will capture events. |
| 38 | + - Draw order: you can configure the order in which sections are drawn (sections can overlap!). |
| 39 | + - :class:`Section` "enable" property to show or hide sections. You can toogle that. |
| 40 | + - Modal Sections: sections that draw last but capture all events and also stop |
| 41 | + other sections from updating. |
| 42 | + - Automated camera swich: Sections will try to activate and deactivate cameras when changing between sections. |
| 43 | + |
| 44 | + |
| 45 | +**Important: You don't need to cover 100% of the View with sections. Sections** |
| 46 | +**can work with the View as well. Also, Sections can overlap.** |
| 47 | + |
| 48 | + |
| 49 | +A simple example |
| 50 | +---------------- |
| 51 | + |
| 52 | +A small program without the use of sections needs to perform some checks |
| 53 | +inside a ``on_mouse_release`` event to know what to do depending on the mouse |
| 54 | +position. |
| 55 | + |
| 56 | +For example maybe if the mouse is on top of the map you want to do something, |
| 57 | +but if the mouse is somewhere else you may need to do other things. |
| 58 | + |
| 59 | +This is what this somehow looks without sections: |
| 60 | + |
| 61 | +.. code:: py |
| 62 | +
|
| 63 | + class MyView(arcade.View): |
| 64 | + # ... |
| 65 | +
|
| 66 | + def on_mouse_release(x: int, y: int, *args, **kwargs): |
| 67 | + if x > 700: |
| 68 | + # click in the side |
| 69 | + do_some_logic_when_side_clicking() |
| 70 | + else: |
| 71 | + # click on the game map |
| 72 | + do_something_in_the_game_map() |
| 73 | +
|
| 74 | +
|
| 75 | +This code can and often become long and with a lot of checks to know what to do. |
| 76 | + |
| 77 | +By using Sections this is what looks like: |
| 78 | + |
| 79 | + |
| 80 | +.. code:: py |
| 81 | +
|
| 82 | + class Map(arcade.Section): |
| 83 | +
|
| 84 | + # ... |
| 85 | +
|
| 86 | + def on_mouse_release(x: int, y: int, *args, **kwargs): |
| 87 | + # clicks on the map are handled here |
| 88 | + pass |
| 89 | +
|
| 90 | +
|
| 91 | + class Side(arcade.Section): |
| 92 | +
|
| 93 | + # ... |
| 94 | +
|
| 95 | + def on_mouse_release(x: int, y: int, *args, **kwargs): |
| 96 | + # clicks on the side of the screen are handled here |
| 97 | + pass |
| 98 | +
|
| 99 | +
|
| 100 | + class MyView(arcade.View): |
| 101 | +
|
| 102 | + def __init__(self, *args, **kwargs): |
| 103 | + self.map_section = Map(0, 0, 700, self.window.height) |
| 104 | + self.side_section = SideSpace(700, 0, 100, self.window.height) |
| 105 | +
|
| 106 | + self.add_section(self.map_section) |
| 107 | + self.add_section(self.side_section) |
| 108 | +
|
| 109 | + # ... |
| 110 | +
|
| 111 | +
|
| 112 | +How to work with Sections |
| 113 | +------------------------- |
| 114 | + |
| 115 | +To work with sections you first need to have a :class:`View`. Sections depend on |
| 116 | +Views and are handled by a special :class:`SectionManager` inside the |
| 117 | +:class:`View`. Don't worry, 99% of the time you won't need to interact with the |
| 118 | +:class:`SectionManager`. |
| 119 | + |
| 120 | +To create a :class:`Section` start by inheriting from ``arcade.Section``. |
| 121 | + |
| 122 | +Based on the :class:`Section` configuration your section will start receiving |
| 123 | +events from the View :class:`SectionManager`. A :class:`Section` has all the |
| 124 | +events a :class:`View` has like ``on_draw``, ``on_update``, ``on_mouse_press``, |
| 125 | +etc. |
| 126 | + |
| 127 | +On instantiation define the positional arguments (left, bottom, width, height) |
| 128 | +of the section. These are very important properties of a :class:`Section`: as they |
| 129 | +define the event capture rectangular area. |
| 130 | + |
| 131 | + |
| 132 | +Properties of a :class:`Section`: |
| 133 | + |
| 134 | +**position: (left, bottom, width, height)**: |
| 135 | + This are mandatory arguments that you need to provide when instantiating a |
| 136 | + :class:`Section`. This is very important as this rectangular positioning |
| 137 | + will determine the event capture space for mouse related events. |
| 138 | + This also will help you determine inside a class the space that is |
| 139 | + holding for example when you want to draw something or calculate coordinates. |
| 140 | + |
| 141 | +**name:** |
| 142 | + A :class:`Section` can optionally get a name so it will be easier to |
| 143 | + debug and indetify what Section is doing what. When logging for example |
| 144 | + is very nice to log the :class:`Section` name at the beginnig so you |
| 145 | + have a reference from where the log was generated. |
| 146 | + |
| 147 | +**accept_keyboard_keys:** |
| 148 | + This allows to tell if a :class:`Section` can receive keyboard events |
| 149 | + (accept_keyboard_keys=False) or to tell which keyboard keys are captured |
| 150 | + in this :class:`Section` (accept_keyboard_keys={arade.key.UP, arcade.key.DOWN}) |
| 151 | + |
| 152 | +**accept_mouse_events:** |
| 153 | + This allows to tell if a :class:`Section` can receive mouse events or which |
| 154 | + mouse events are accepted. |
| 155 | + For example: accept_mouse_events={'on_mouse_move'} means only mouse move events |
| 156 | + will be captured. |
| 157 | + |
| 158 | +**prevent_dispatch:** |
| 159 | + This tells a :class:`Section` if it should prevent the dispatching of certain |
| 160 | + events to other sections down event capture stream. By default a :class:`Section` |
| 161 | + will prevent dispatching all handled events. |
| 162 | + By passing ``prevent_dispatch={'on_mouse_press'}`` all events will propagate |
| 163 | + down the event capture stream except the ``on_mouse_press`` event. |
| 164 | + |
| 165 | +**prevent_dispatch_view:** |
| 166 | + This allows to tell if a :class:`Section` if events (and what events) should |
| 167 | + not be dispatched to the underlying :class:`View`. |
| 168 | + This is handy if you want to do some action in the :class:`View` code whether |
| 169 | + or not the event was handled by another :class:`Section`. By default a |
| 170 | + :class:`Section` will prevent dispatching all handled events to the :class:`View`. |
| 171 | + |
| 172 | +**local_mouse_coordinates:** |
| 173 | + If True the section mouse events will receive x, y coordinates section |
| 174 | + related to the section dimensions and position (not related to the screen). |
| 175 | + |
| 176 | +**enabled:** |
| 177 | + By default all sections are enabled. This allows to tell if this particuar |
| 178 | + :class:`Section` should be enabled or not. If a :class:`Section` is not |
| 179 | + enabled, it will not capture any event, draw, update, etc. It will be |
| 180 | + as it didn't exist. |
| 181 | + You can enable and disable sections at any time allowing some cool efects. |
| 182 | + |
| 183 | +**modal:** |
| 184 | + This tells the :class:`SectionManager` that this :class:`Section` is modal. |
| 185 | + This means that the :class:`Section` will capture all events first and not |
| 186 | + deliver any events to the underlying sections or view. Also, It will draw |
| 187 | + last (on top of other ``on_draw`` calls). When enabled a modal :class:`Section` |
| 188 | + will prevent all other sections from receive ``on_update`` events. |
| 189 | + |
| 190 | +**draw_order:** |
| 191 | + This allows to define the draw order this :class:`Section` will have. |
| 192 | + This is handy when you have overlaping sections and you want some |
| 193 | + :class:`Section` to be drawn ontop of another. |
| 194 | + By default sections will be draw in the order they are added (except modal |
| 195 | + sections). |
| 196 | + |
| 197 | +Other handy :class:`Section` properties: |
| 198 | + |
| 199 | +- block_updates: if True this section will not have the ``on_update`` method called. |
| 200 | +- camera: this is meant to hold a ``arcade.Camera`` but it is None by default. The SectionManager will trigger the use of the camera when is needed automatically. |
| 201 | + |
| 202 | +Handy :class:`Section`: methods: |
| 203 | + |
| 204 | +- overlaps_with: this will tell if another :class:`Section` overlaps with this one. |
| 205 | +- mouse_is_on_top: this will tell if given a x, y coodinate, the mouse is on top of the section. |
| 206 | +- get_xy_screen_relative: get screen x, y coordinates from x, y section coordinates. |
| 207 | +- get_xy_section_relative: get section x, y coordinates from x, y screen coordinates. |
| 208 | + |
| 209 | + |
| 210 | +Sections configuration and logic with an example |
| 211 | +------------------------------------------------ |
| 212 | + |
| 213 | +Imagine a game where you have this basic components: |
| 214 | + |
| 215 | +- A 800x600 screen viewport |
| 216 | +- A game map |
| 217 | +- A menu bar at the top of the screen |
| 218 | +- A side right panel with data from the game |
| 219 | +- Popup messages (dialogs) |
| 220 | + |
| 221 | +With this configuration you can divide this logic into sections with a some |
| 222 | +configuration. |
| 223 | + |
| 224 | +Lets look what this configuration may look: |
| 225 | + |
| 226 | +.. code:: py |
| 227 | +
|
| 228 | + import arcade |
| 229 | +
|
| 230 | +
|
| 231 | + class Map(arcade.Section): |
| 232 | + #... define all the section logic |
| 233 | +
|
| 234 | +
|
| 235 | + class Menu(arcade.Section): |
| 236 | + #... define all the section logic |
| 237 | +
|
| 238 | +
|
| 239 | + class Panel(arcade.Section): |
| 240 | + #... define all the section logic |
| 241 | +
|
| 242 | +
|
| 243 | + class PopUp(arcade.Section): |
| 244 | + def __init__(message, *args, **kwargs): |
| 245 | + super().__init(*args, **kwargs) |
| 246 | + self.message = message |
| 247 | +
|
| 248 | + # define draw logic, etc... |
| 249 | +
|
| 250 | +
|
| 251 | + class MyView(arcade.View): |
| 252 | +
|
| 253 | + def __init__(self, *args, **kwargs): |
| 254 | + self.map = Map(left=0, bottom=0, width=600, height=550, |
| 255 | + name='Map', draw_order=2) |
| 256 | + self.menu = Menu(left=0, bottom=550, width=800, height=50, |
| 257 | + name='Menu', accept_keyboard_keys=False, |
| 258 | + accept_mouse_events={'on_mouse_press'}) |
| 259 | + self.panel = Panel(left=600, bottom=0, width=200, height=550, |
| 260 | + name='Panel', accept_keyboard_keys=False, |
| 261 | + accept_mouse_events=False) |
| 262 | +
|
| 263 | + popup_left = (self.view.window.width // 2) - 200 |
| 264 | + popup_bottom = (self.view.window.height // 2) - 100 |
| 265 | + popup_width = 400 |
| 266 | + popup_height = 200 |
| 267 | + self.popup = PopUp(message='', popup_left, popup_bottom, popup_width, |
| 268 | + popup_height, enabled=False, modal=True) |
| 269 | +
|
| 270 | + def close(): |
| 271 | + self.popup.message = 'Are you sure you want to close the view?' |
| 272 | + self.popup.enabled = True |
| 273 | +
|
| 274 | +
|
| 275 | +Lets go step by step. |
| 276 | +First we configure a Map section that will hold the map. This Section will start at |
| 277 | +left, bottom = 0,0 and will not occupy the whole screen. |
| 278 | +Mouse events that occur outside of this coordinates will not be handled by the Map |
| 279 | +event handlers. So Map will only needs to take care of what happens inside the map. |
| 280 | + |
| 281 | +Second we configure a Menu section that will hold some buttons. This menu takes the top |
| 282 | +space of the screen that the Map has left. The Map + the Menu will occupy 100% of the height |
| 283 | +of the screen. The menu section is configured to not receive any keyboard events and |
| 284 | +to only receive on_mouse_press events, ignoring all other type of mouse events. |
| 285 | + |
| 286 | +Third, the Panel also doesn't receive keyboard events. So the Map is the only handling |
| 287 | +keyboard events at the moment. Also no mouse events are allowed in the panel. |
| 288 | +This panel is just to show data. |
| 289 | + |
| 290 | +For the last part notice that we define a section that it will be disabled at first |
| 291 | +and that is modal. This section will render something with a message. |
| 292 | +The section is used when the close method of the view is called. Because PopUp is a |
| 293 | +modal section, when enabled it's rendered on top of everything. Also, all other |
| 294 | +section stoped updating and all events are captured by the modal section. |
| 295 | +So in brief we are "stopping" the world outside the popup section. |
| 296 | + |
| 297 | + |
| 298 | +Section Unique Events |
| 299 | +--------------------- |
| 300 | + |
| 301 | +There a few unique events that belong to sections and are somehow special in the way they are triggered: |
| 302 | + |
| 303 | +**``on_mouse_enter`` and ``on_mouse_leave``:** |
| 304 | + These events are triggered on two ocasions: when the mouse enters/leaves the |
| 305 | + view and when the :class:`SectionManager` detects by mouse motion (or dragging) |
| 306 | + that the mouse has enter / leaved the section dimensions. |
| 307 | + |
| 308 | +**``on_show_section`` and ``on_hide_section``:** |
| 309 | + There events are triggered only when the section is enabled and under |
| 310 | + certain circumstances that must be known: |
| 311 | + - When the section is added or removed from the :class:`SectionManager` and the :class:`View` is currently being shown |
| 312 | + - When the section is enabled or disabled |
| 313 | + - When Window calls ``on_show_view`` or ``on_hide_view`` |
| 314 | + |
| 315 | + |
| 316 | +The Section Manager |
| 317 | +------------------- |
| 318 | + |
| 319 | +Behind the scenes, when sections are added to the :class:`View` the |
| 320 | +:class:`SectionManager` is what will handle all events instead of |
| 321 | +the :class:`View` itself. |
| 322 | + |
| 323 | +You can access the :class:`SectionManager` by accessing the ``View.section_manager``. |
| 324 | + |
| 325 | +Usually you won't need to work with the :class:`SectionManager`, but there are |
| 326 | +some cases where you will need to work with it. |
| 327 | + |
| 328 | +You add sections usually with ``View.add_section`` but the same method exists on |
| 329 | +the :class:`SectionManager`. Also you have a ``remove_section`` and a |
| 330 | +``clear_sections`` method. |
| 331 | + |
| 332 | +You can ``enable`` or ``disable`` the :class:`SectionManager` to completely |
| 333 | +enable or disable all sections at once. |
| 334 | + |
| 335 | +There are some other functionality exposed from the :class:`SectionManager` like |
| 336 | +``get_section_by_name`` that can also be useful. Check the api to know about those. |
| 337 | + |
| 338 | +Also there are three attributes that can be configured in the :class:`SectionManager` |
| 339 | +that are useful and important sometimes. |
| 340 | + |
| 341 | +By default, ``on_draw``, ``on_update`` and ``on_resize`` are events that will always |
| 342 | +be triggered in the :class:`View` before the any section has triggered them. |
| 343 | +This is the default but you can configure this with the following attributes: |
| 344 | + |
| 345 | +-``view_draw_first`` |
| 346 | +-``view_update_first`` |
| 347 | +-``view_resize_first`` |
| 348 | + |
| 349 | +Both three work the same way: |
| 350 | + |
| 351 | +- True (default) to trigger that event in the :class:`View` before the sections. |
| 352 | +- False so it's triggered in the :class:`View` after sections corresponding methods. |
| 353 | +- None to not trigger that event in the :class:`View` at all. |
0 commit comments