Skip to content

Adds menus tutorial to wiki #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
Menus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be all lowercase, so that the presentation is consistent with the other tutorials on the index:

Index

======

This page contains tutorials about the :mod:`menus` package.


Creating PagedMenu
--------------------------

This is a simple example to create paged menu

.. code-block:: python

import random

from commands.say import SayCommand

from menus import PagedMenu
from menus import PagedOption

from players.entity import Player


# Register menu command
@SayCommand(['menu', '/menu', '!menu'])
def say_command(command, index, teamonly):
# Send the menu
menu.send(index)
return False

menu = PagedMenu(title='Welcome menu', description='Choose an option:', select_callback=my_select_callback)

# Add options from 1 to 20
for i in range(1, 20):
menu.append(PagedOption(f'{i}', i))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be declared after my_select_callback because it currently won't load and produce a NameError. Moreover, the first line is too long and does not conform with PEP 8, which we try to follow and promote for this project.


def my_select_callback(menu, index, option):
'''
Called whenever a selection was made.
'''

# Shuffle the menu : D
random.shuffle(menu)

# Make it sticky
return menu


Creating SimpleMenu
--------------------------

This is a simple example to create simple menu

.. code-block:: python

import time

from commands.say import SayCommand

from menus import SimpleMenu
from menus import SimpleOption
from menus import Text

from messages import SayText2

from players.entity import Player

# Register menu command
@SayCommand(['menus', '/menus', '!menus'])
def say_menus_command(command, index, teamonly):
# Send the menu
menu.send(index)
return False


menu = SimpleMenu()
# Tell the current time
menu.append(Text(f"Current Time: {time.strftime('%H:%M:%S')}"))
# Add empty line
menu.append(Text(' '))
menu.append(Text('Do you accept the rules?'))
menu.append(Text(' '))
# Add in menu options
menu.append(SimpleOption(1, 'Yes', 'yes'))
menu.append(SimpleOption(2, 'No', 'no'))
menu.select_callback=my_menu_select_callback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above; this should be declared after my_menu_select_callback. And it should also have spaces surrounding the operator:

variable = value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have now more updated the wiki based of your comments.
I also added when selects close button, it registers it instead closing the menu



def my_menu_select_callback(menu, index, option):
if option.value == 'yes':
SayText2('Thank you for accepting the rules!').send(index)
# Player selected no option
else:
Player(index).kick('You have to accept the rules!')

Creating ListMenu
--------------------------

This is a simple example to create list menu

.. code-block:: python

from commands.say import SayCommand

from menus import ListMenu
from menus import Text

# Register menu command
@SayCommand(['menus', '/menus', '!menus'])
def say_menus_command(command, index, teamonly):
# Send the menu
menu.send(index)
return False

menu = ListMenu()
# Add in menu text
menu.append(Text('This is a example text'))