Submodal

By Chris Proctor

class quest.contrib.submodal.SubmodalMixin[source]

Extends quest.modal.Modal so that it can support submodals.

Sometimes a modal needs to go into a deeper level of detail. For example an inventory list might need to present more details about a specific item. This could all be implemented within one modal, but it would get complicated quickly. Instead, SubmodalMixin allows a Modal to open its own submodal using open_submodal(modal).

The top-level modal remains in control and is the only modal which actually gets rendered; any others are just used to access data and to figure out how to respond to choices. A modal which mixes in SubmodalMixin will check to see whether it has the property submodal assigned before getting its text labels, option labels, or choosing an option. When a submodal is present, that modal’s behavior is called instead of the modal. (That submodal could have its own submodals!) When a submodal is ready to be closed, it should call self.close() from choose_option as usual. Instead of closing all the modals, close will signal the parent modal that it should resume control. In case you want to close more than one layer of modals, you can call self.parent.close().

Submodal uses two elegant and complex computer science ideas. The first is recursion, or having a function call itself. Recursion is useful when you can break a task into a simpler version of itself, or when you can accomplish a task by asking someone else to do it. (Asking yourself to do a task by asking yourself to do it leads to infinite recursion and a headache.) Check out get_active_modal to see an example of recursion.

The second elegant idea is virtualization, the idea that you never know if you’re at the top level. A submodal acts just like the main modal, attached to a game. Either way, the modal calls close() when it is finished. If it was a submodal, control passes back to the parent, but the modal calling close() doesn’t know or care. A lot of stories have been written about virtualization–the idea of being in a dream and not knowing whether you’re in a dream (or whether this reality is itself a dream, or maybe even a dream within a dream!

parent

This submodal’s parent modal (if it has one).

Type

Modal

submodal

The current submodal (if any).

Type

Modal

open_submodal(modal)[source]

Attaches a submodal and sets the submodal’s parent to self.

close(close_all=False)[source]

Closes this submodal, handing control back to the parent if there is one.

Parameters

close_all (bool) – If True, closes all modals, not just this one.

get_active_modal()[source]

Finds the modal who should control rendering and handling input.

If a modal has a submodal, the submodal should be in charge. (But that submodal might have a sub-sub-modal.) So Submodal.get_active_modal uses recursion to find a modal that doesn’t have a submodal. Instead of doing the whole task, it just asks its submodal to find the active submodal–using exactly the same method.

set_text_labels()[source]

Creates text labels, using the active submodal if there is one.

set_option_labels()[source]

Creates option labels, using the active submodal if there is one.

handle_change_option(change)[source]

Handles a change option, using the active submodal if there is one.

handle_choice()[source]

Lets submodal choose option, if set