NP-Complete, a sci-fi roguelike
I wrote up some back story and stuff, but that's pretty much all the game parts right now.
NP-Complete is currently not much more than a widget library that prints using libtcod; that said, it's been exercised enough to have grown some hair that just begs for a reorganization. So let's see what widgets there are and what they should do.
Widget: the base class. All widgets are a subclass of
Widget, so this is where we put things that all widgets must be able to do.
console: this is the
tcod.Consolethe widget will ultimately be drawn on.
parent: widgets form a tree, wherein a widget can have any number of children. The inverse relationship must also be available.
children: as stated above. Implementation detail: the children are actually stored in an
OrderedSet(as per this recipe).
handlers: events get posted to root-level widgets and can be passed down the tree of children; the first widget to return
Truefrom a handler is considered to have handled the event, and it will not be bubbled further. This dictionary defines the handlers this widget has for each event type it's interested in.
bounds: the widget's area, defined as a
Rect. The width and height are determined by the equivalent constructor parameters; the position is up to the widget's interpretation.
placement: the widget's on-screen position and size. May be smaller than its full bounds. Position is relative to the parent widget (see
screen_to_pointfor translations to root console coordinates).
ColorSetcontaining all applicable colors (at minimum, foreground and background) to draw this widget.
effect: The background effect to be used when rendering the widget.
widgetas a child of this one, adding it to the list of
childrenand setting the new widget's
parent. This is normally automatically invoked by the widget constructor when a parent is provided. If the widget already had a parent, that parent is asked to abandon the widget first.
widgetfrom the list of children and unsets its
to_screen(point): Translates widget-relative coordinates to screen-relative.
to_local(point): Translates the other way.
render(): Asks the widget to render itself and its children. Usually called on the top-level widget.
OrderedSetensures render order is from first-added to last-added.
dispatch(event): If an event handler exists for this widget, it gets called; if it returns
True, the event is handled. Otherwise, the event is passed on to the children, in the same order as for rendering.
center(horizontal=True, vertical=True): Centers the widget's
parentwidget. Optionally centers only horizontally, or only vertically. If the widget has no parent, centers in the
Dialog(Widget): A widget that renders a frame inside itself. Primarily intended as a root widget for (what else) dialog boxes.
Image(Widget): A widget that displays an image using libtcod's image handling.
image: The TCODImage that will be blitted into the widget's display area.
bounds: Non-zero coordinates may be provided to display a different region of the image. Use the image's
heightattributes) to determine the maximum size of the image.
Label(Widget): A widget that displays text and can be dynamically sized. A label's width can be constrained, in which case it will only grow in height as more text is piled on.
text: The text to be rendered. May have multiple lines.
align: Alignment to use when rendering the text.
max_width: The maximum width of the label. Note that, internally, this is stored as the width of the
boundsRect. Further note that the bounds' height is ignored.
Button(Label): A clickable, focusable, shortcut-key-responding label.
key: The name of the shortcut key assigned to this button. Should be compatible with
utils.key_check(). May be
None, in which case it is your responsibility to set
key_matchto a suitable value.
key_match: A method matching the prototype
key_match(event_data)to be called from the button's
KEYhandler. Automatically populated when the
keyproperty is set to a valid key name, it should return
Trueif the event data (usually a
tcod.Keyobject) should activate the button.
action: A method matching the prototype
action(widget)to be called from the button's
ACTIVATEhandler. It will be passed the button widget.
events.KEY: A handler which posts an
ACTIVATEevent when the
dataof this event matches the button's shortcut key. Automatically generated, and should not need to be changed in normal operation.
events.ACTIVATE: Another auto-generated handler, this simply calls the
List(Widget): Displays a list of
Labels or derivatives, can be restricted in width, height, or both, can be scrolled vertically, focused, and clicked.
selected_child: The child which is currently selected. It will be the only child to receive an
scroll_pos: A tuple (top, bottom) of the current scrolling position. This is also exposed as
bounds.top + placement.height.
sub_console: This is an inner tcod console that keeps the entire list rendered; scrolling is implemented as choosing what part of this sub-console to blit onto the widget's actual target console.
register_child(child): On top of Widget's default
register_child, this also changes the widget's console to be the List's
sub_console, and its
positionto reflect the location in
sub_consolewhere the child will be rendered. If the List is width-restricted, the child's
widthwill also be restricted.
scroll_by(amount): Scrolls the list by
amountlines, limited to the actual
boundsof the list.
scroll_to(top, bottom=None): Scrolls the list to bring whichever of top or bottom is furthest into view (i.e. scroll up to reveal top, down to reveal bottom).
DynamicLabel(Label): A special label that automatically fetches its text from a callback whenever its
activate handler returns
method()that returns a string to be used as the label text.
Menu(List): A special kind of list that supports using keyboard shortcuts to select specific list items.
keys: A dictionary of key codes to list items, determining which item will get selected by a given key. It is highly preferable to use
add_item(), below, for adding to this dictionary.
add_item(key, item): As
List.add_item(item)above, but also records the key which, when pressed, will select the item.