gamemenu 0.2 2002/06/05

gamemenu: baseclass and implementation for various game "menus."

Menu will allow you to add a graphical context menu to your games with
relative ease. Examples of support types include:

- Rotating Icons (like Secret of Mana) in class RotatingIconMenu
- Circling Text (like The Sims) in class CircleTextMenu

Running this program instead of using it as a library will let you examine
several examples.

If you want more menu styles, implement a derived class (or tree) from
GameMenu or RadialMenu, and see the RotatingIconMenu and CircleTextMenu for
example code.

If you like the style of menu but want to change how icons/text/desc are
rendered, derive a class from the Icon, Text, or Desc with your own
render_str(). Then to use these, instead of
menu.add_item(..., icon='file.png', ...)
use menu.add_item(..., icon=MyIcon('file.png'), ...)
and similarly for text= and desc=
Example render_str() functions are available in Icon, Text, and Desc
classes.
Written by Michael Urman (mu on irc.openprojects.net)
Uses Modules
Numeric copy pygame
Classes
GameMenu Item MenuAttribute
class CircleTextMenu(RadialMenu)
CircleTextMenu implements a menu system like The Sims uses (minus the floating head)
Methods
__init__(self, copyMenu=None)
_drawdoublebuf(self, screen, back, items, pos, sel, osel, clear=0)
_drawdoublebuf(self, screen, back, items, pos, sel, osel, clear=0) -> None

Draws items to a double-buffer screen with corresponding positions or erase if clear=1.
_drawsinglebuf(self, screen, back, items, pos, sel, osel, clear=0)
_drawsinglebuf(self, screen, back, items, pos, sel, osel, clear=0) -> None

Draws items to a single-buffer screen with corresponding positions or erase if clear=1.
add_item(self, id, **kwargs) from GameMenu
del_item(self, *ids) from GameMenu
get_event(self) from GameMenu
get_hover_coords(self) from GameMenu
item(self, id) from GameMenu
run(self, center, screen)
run(self, center, screen) -> Selection

Display the Circle Text Menu centered around center and handle events until the menu is cancelled or an item is selected. Return None or the Item correspondingly
set_events(self, menuevent, *events) from GameMenu
set_item_desc(self, id, desc) from GameMenu
set_item_icon(self, id, icon) from GameMenu
set_items_enable(self, enable, *ids) from GameMenu
set_items_visible(self, visible, *ids) from GameMenu
set_radius(self, radius) from RadialMenu
set_rotatecount(self, count) from RadialMenu
set_skew(self, axis) from RadialMenu
set_style(self, style, params=None) from GameMenu
class Desc(MenuAttribute)
Desc abstracts the description information
Variables
COLOR = (255, 255, 255)
FONT = None
SIZE = 24
rendered = {}
Methods
__getattr__(self, attr) from MenuAttribute
__init__(self, *args) from MenuAttribute
desaturate(self, surf) from MenuAttribute
get_height(self) from MenuAttribute
get_image(self, image=0) from MenuAttribute
get_rect(self) from MenuAttribute
get_size(self) from MenuAttribute
get_width(self) from MenuAttribute
hilight(self, surf, color=(255, 255, 255), percent=0.33) from MenuAttribute
render_str(self, str)
render_str(self, str) -> None

Render a set of images for the description, treating str as a title
class GameMenu
GameMenu is the base which holds the common data for all menus
Methods
__init__(self, copyMenu=None)
add_item(self, id, **kwargs)
add_item(self, id, **kwargs) -> None

Add a menu item. Suggested keywords include: icon, text, desc, submenu, visible, enabled
del_item(self, *ids)
del_item(self, *ids) -> None

Remove a menu item(s)
get_event(self)
get_event(self) -> menu event

Return a menu event based on handlers set in set_events. This will be one of 'select', 'cancel', 'next', 'prev', or None if a QUIT event is received.
get_hover_coords(self)
get_hover_coords(self) -> (x,y)

return the current hover position, valid only after a get_event() returns 'hover'
item(self, id)
item(self, id) -> menu item

Return a reference to menu item matching id
run(self)
set_events(self, menuevent, *events)
set_events(self, menuevent, *events) -> None

sets the events that generate a given menu event. if *events is None, the menuevent is disabled. otherwise each event should be a tuple of the form: (<EventType>, <keyword>, <val>). an example tuple (MOUSEBUTTONUP, 'button', 1) describes a menuevent generation on the release of the left mouse button.

valid menuevent values are the strings: select, cancel, next, prev
set_item_desc(self, id, desc)
set_item_desc(self, id, desc) -> None

sets item's title for the given id. desc should be a Desc or a string
set_item_icon(self, id, icon)
set_item_icon(self, id, icon) -> None

sets item's icon for the given id. icon should be an Icon or a string holding the path to an icon file.
set_items_enable(self, enable, *ids)
set_items_enable(self, enable, *ids) -> None

sets 'enabled' to enable for all ids. enabled items (enable=1) should be selectable; disabled items (enable=0) should be visible but cannot be selected
set_items_visible(self, visible, *ids)
set_items_visible(self, visible, *ids) -> None

sets 'visible' to visible for all ids. visible items (visible=1) should be drawn; invisible items (visible=0) should not be drawn and hence skipped over in the selection process as well
set_style(self, style, params=None)
set_style(self, style, params=None) -> None

sets the select style for the menu to one of: hover, event. the default is a mouse hover with a left-click release over the drawn image. params is an optional dictionary containing elements described in set_select_params()
class Icon(MenuAttribute)
Icon abstracts the icons used in the RotatingIconMenu
Variables
rendered = {}
Methods
__getattr__(self, attr) from MenuAttribute
__init__(self, *args) from MenuAttribute
desaturate(self, surf) from MenuAttribute
get_height(self) from MenuAttribute
get_image(self, image=0) from MenuAttribute
get_rect(self) from MenuAttribute
get_size(self) from MenuAttribute
get_width(self) from MenuAttribute
hilight(self, surf, color=(255, 255, 255), percent=0.33) from MenuAttribute
render_str(self, str)
render_str(self, str) -> None

Render a set of images for the icon, treating str as an icon file
class Item
Item abstracts the menu items with some convenience 'functions.' In particular, accessing icon, title, and desc are merely item.icon, etc.
Methods
__getattr__(self, attr)
__init__(self, kwargs)
__setattr__(self, attr, val)
class MenuAttribute
MenuAttribute is the base of drawn menu attributes
Methods
__getattr__(self, attr)
Quick access functionality to width, height, size, and rect
__init__(self, *args)
MenuAttribute(*args) -> MenuAttribute

Instantiates a MenuAttribute (or derived class) according to arguments. Currently the only accepted format is:

- MenuAttribute(string) (meaning depends on the subclass)
desaturate(self, surf)
desaturate(self, surf) -> surf

Creates a new surface and fills it with the greyscale according to the Gimp's Y = 0.3R + 0.59G + 0.11B
get_height(self)
get_height(self) -> height

Return the height of image
get_image(self, image=0)
get_image(self, image=0) -> surf

Return the image indexed by argument image
get_rect(self)
get_rect(self) -> rect

Return the image's rect
get_size(self)
get_size(self) -> width, height

Return tuple of width and height of image
get_width(self)
get_width(self) -> width

Return the width of image
hilight(self, surf, color=(255, 255, 255), percent=0.33)
hilight(self, surf) -> surf

Creates a new surface and fills it with a highlighted version by blitting a 33% white on top.
render_str(self, str)
class RadialMenu(GameMenu)
Base class for radial menus; not a functional menu itself
Methods
__init__(self, copyMenu=None)
add_item(self, id, **kwargs) from GameMenu
del_item(self, *ids) from GameMenu
get_event(self) from GameMenu
get_hover_coords(self) from GameMenu
item(self, id) from GameMenu
run(self) from GameMenu
set_events(self, menuevent, *events) from GameMenu
set_item_desc(self, id, desc) from GameMenu
set_item_icon(self, id, icon) from GameMenu
set_items_enable(self, enable, *ids) from GameMenu
set_items_visible(self, visible, *ids) from GameMenu
set_radius(self, radius)
set_radius(self, radius) -> none

Choose the radius in pixels for the rotating menu. radius can either be a single number, or a tuple of (x,y) radii for an axially aligned ellipse.
set_rotatecount(self, count)
set_rotatecount(self, count) -> None

Set the selected item location in counter-clockwise spots from the top center
set_skew(self, axis)
set_skew(self, axis) -> None

Choose the radius in pixels for the rotating menu. radius is a tuple of (x,y) skew factors.
set_style(self, style, params=None) from GameMenu
class RotatingIconMenu(RadialMenu)
RotatingIconMenu implements a menu system like Secret of Mana's
Methods
__init__(self, copyMenu=None)
_drawdoublebuf(self, screen, back, items, pos, opos, sel, osel)
_drawdoublebuf(sel, screen, back, items, pos, opos, sel, osel) -> None

Draws items to a double-buffer screen with corresponding positions (if passed), erasing old positions (if passed).
_drawsinglebuf(self, screen, back, items, pos, opos, sel, osel)
_drawsinglebuf(self, screen, back, items, pos, opos, sel, osel) -> None

Draws items to a single-buffer screen with corresponding positions (if passed), erasing old positions (if passed).
add_item(self, id, **kwargs) from GameMenu
del_item(self, *ids) from GameMenu
get_event(self) from GameMenu
get_hover_coords(self) from GameMenu
item(self, id) from GameMenu
run(self, center, screen)
run(self, center, screen) -> Selection

Display the Rotating Icon Menu centered around center and handle events until the menu is cancelled or an item is selected. Return None or the Item correspondingly
set_events(self, menuevent, *events) from GameMenu
set_item_desc(self, id, desc) from GameMenu
set_item_icon(self, id, icon) from GameMenu
set_items_enable(self, enable, *ids) from GameMenu
set_items_visible(self, visible, *ids) from GameMenu
set_radius(self, radius) from RadialMenu
set_rotatecount(self, count) from RadialMenu
set_skew(self, axis) from RadialMenu
set_style(self, style, params=None) from GameMenu
class Text(MenuAttribute)
Text abstracts the text used in the CircleTextMenu
Variables
BGCOLOR = (40, 40, 120)
COLOR = (255, 255, 255)
FONT = None
SIZE = 18
rendered = {}
Methods
__getattr__(self, attr) from MenuAttribute
__init__(self, *args) from MenuAttribute
desaturate(self, surf) from MenuAttribute
get_height(self) from MenuAttribute
get_image(self, image=0) from MenuAttribute
get_rect(self) from MenuAttribute
get_size(self) from MenuAttribute
get_width(self) from MenuAttribute
hilight(self, surf, color=(255, 255, 255), percent=0.33) from MenuAttribute
render_str(self, str)
render_str(self, str) -> None

Render a set of images for the text, treating str as a label
Functions
_main
_main()
Test the functionality of the menus if run as a program