cocos.menu module — cocos v0.6.10 documentation

# cocos.menu module

A Layer that implements a simple menu

## Menu

This module provides a Menu class. Menus can contain regular items (which trigger a function when selected), toggle items (which toggle a flag when selected), or entry items (which lets you enter alphanumeric data).

To use a menu in your code, just subclass Menu and add the menu to an Scene or another Layer.

class `ColorMenuItem`(label, callback_func, items, default_item=0)

  Bases: `cocos.menu.MenuItem`

  A menu item for selecting a color.

  Example:

      colors = [(255, 255, 255), (100, 200, 100), (200, 50, 50)]

      items.append( ColorMenuItem(
                      'Jacket:',
                      self.on_jacket_color,
                      colors ))

  `draw`(*args, **kwargs)

    This is the function you will have to override if you want your subclassed `CocosNode` to draw something on screen.

    You must respect the position, scale, rotation and anchor attributes. If you want OpenGL to do the scaling for you, you can:

        def draw(self):
            glPushMatrix()
            self.transform()
            # ... draw ..
            glPopMatrix()

  `generateWidgets`(pos_x, pos_y, font_item, font_item_selected)

    Generate a normal and a selected widget. This method should be implemented by descendents.

  `on_key_press`(symbol, modifiers)

class `EntryMenuItem`(label, callback_func, value, max_length=0)

  Bases: `cocos.menu.MenuItem`

  A menu item for entering a value.

  When selected, `self.value` is toggled, the callback function is called with `self.value` as argument.

  `on_key_press`(symbol, modifiers)
  `on_text`(text)
  property `value`

class `ImageMenuItem`(image, callback_func, *args, **kwargs)

  Bases: `cocos.menu.BaseMenuItem`

  A menu item that shows a selectable Image

  `draw`()

    This is the function you will have to override if you want your subclassed `CocosNode` to draw something on screen.

    You must respect the position, scale, rotation and anchor attributes. If you want OpenGL to do the scaling for you, you can:

        def draw(self):
            glPushMatrix()
            self.transform()
            # ... draw ..
            glPopMatrix()

  `generateWidgets`(pos_x, pos_y, font_item, font_item_selected)

    Generate a normal and a selected widget. This method should be implemented by descendents.

class `Menu`(title='')

  Bases: `cocos.layer.base_layers.Layer`

  Abstract base class for menu layers.

  Normal usage is:

  > - create a subclass

  > - override __init__ to set all style attributes, and then call create_menu()

  > - Finally you shall add the menu to an Scene or another Layer

  `create_menu`(items, selected_effect=None, unselected_effect=None, activated_effect=None, layout_strategy=<function verticalMenuLayout>)

    Creates the menu

    The order of the list important since the first one will be shown first.

    Example:

        l = []
        l.append( MenuItem('Options', self.on_new_game ) )
        l.append( MenuItem('Quit', self.on_quit ) )
        self.create_menu( l, zoom_in(), zoom_out() )

    Parameters:
      itemslist

        list of BaseMenuItem that will be part of the Menu

      selected_effectfunction

        This action will be executed when the BaseMenuItem is selected

      unselected_effectfunction

        This action will be executed when the BaseMenuItem is unselected

      activated_effectfunction

        this action will executed when the BaseMenuItem is activated (pressing Enter or by clicking on it)

  `draw`()

    This is the function you will have to override if you want your subclassed `CocosNode` to draw something on screen.

    You must respect the position, scale, rotation and anchor attributes. If you want OpenGL to do the scaling for you, you can:

        def draw(self):
            glPushMatrix()
            self.transform()
            # ... draw ..
            glPopMatrix()

  `on_key_press`(symbol, modifiers)
  `on_mouse_motion`(x, y, dx, dy)
  `on_mouse_release`(x, y, buttons, modifiers)
  `on_text`(text)
  `activate_sound` = None
  `is_event_handler` = True

    Receives pyglet events

  `select_sound` = None

class `MenuItem`(label, callback_func, *args, **kwargs)

  Bases: `cocos.menu.BaseMenuItem`

  A menu item that shows a label.

  `draw`()

    This is the function you will have to override if you want your subclassed `CocosNode` to draw something on screen.

    You must respect the position, scale, rotation and anchor attributes. If you want OpenGL to do the scaling for you, you can:

        def draw(self):
            glPushMatrix()
            self.transform()
            # ... draw ..
            glPopMatrix()

  `generateWidgets`(pos_x, pos_y, font_item, font_item_selected)

    Generate a normal and a selected widget. This method should be implemented by descendents.

  `get_item_height`()

    Returns the width of the item. This method should be implemented by descendents.

    Return type:

      int

  `get_item_width`()

    Returns the width of the item. This method should be implemented by descendents.

    Return type:

      int

class `MultipleMenuItem`(label, callback_func, items, default_item=0)

  Bases: `cocos.menu.MenuItem`

  A menu item for switching between multiple values.

  Example:

      self.volumes = ['Mute','10','20','30','40','50','60','70','80','90','100']

      items.append( MultipleMenuItem(
                      'SFX volume: ',
                      self.on_sfx_volume,
                      self.volumes,
                      8 ) )

  `on_key_press`(symbol, modifiers)

class `ToggleMenuItem`(label, callback_func, value=False)

  Bases: `cocos.menu.MultipleMenuItem`

  A menu item for a boolean toggle option.

  Example:

      items.append( ToggleMenuItem('Show FPS:', self.on_show_fps, director.show_FPS) )

  `on_key_press`(symbol, mod)

`fixedPositionMenuLayout`(positions)

`shake`()

  Predefined action that performs a slight rotation and then goes back to the original rotation position.

`shake_back`()

  Predefined action that rotates to 0 degrees in 0.1 seconds

`verticalMenuLayout`(menu)

`zoom_in`()

  Predefined action that scales to 1.5 factor in 0.2 seconds

`zoom_out`()

  Predefined action that scales to 1.0 factor in 0.2 seconds