Skip to content

Commit fec6e4f

Browse files
committed
Add updated menus tutorial from #447
1 parent 6d81bda commit fec6e4f

File tree

1 file changed

+142
-0
lines changed
  • addons/source-python/docs/source-python/source/developing/module_tutorials

1 file changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
menus
2+
======
3+
4+
This page contains tutorials about the :mod:`menus` package.
5+
6+
7+
Creating PagedMenu
8+
--------------------------
9+
10+
This is a simple example to create a :class:`menus.PagedMenu`, which shows 20 options.
11+
After selecting an option the menu will be shuffled and shown again.
12+
13+
You can use paged menus if you have many menu entries and want Source.Python
14+
to take care about the pagination. If you need more control about the menu
15+
take a look at :class:`menus.SimpleMenu`.
16+
17+
.. code-block:: python
18+
19+
import random
20+
21+
from commands.say import SayCommand
22+
23+
from menus import PagedMenu
24+
from menus import PagedOption
25+
26+
from players.entity import Player
27+
28+
29+
# Register menu command
30+
@SayCommand(['menu', '/menu', '!menu'])
31+
def say_command(command, index, teamonly):
32+
# Send the menu to the player who issued the say command.
33+
menu.send(index)
34+
return False
35+
36+
def my_select_callback(menu, index, option):
37+
"""Called whenever a selection was made."""
38+
39+
# Shuffle the menu : D
40+
random.shuffle(menu)
41+
42+
# Make it sticky. If you return a menu, it will be shown immediately.
43+
return menu
44+
45+
menu = PagedMenu(
46+
title='Welcome menu',
47+
description='Choose an option:',
48+
select_callback=my_select_callback
49+
)
50+
51+
# Add options from 1 to 20
52+
for i in range(1, 20):
53+
menu.append(PagedOption(f'Option {i}', i))
54+
55+
# Instead of passing the select, build and close callbacks to the constructor
56+
# of PagedMenu, you can also use a decorator to register the callbacks.
57+
@menu.register_close_callback
58+
def _on_close_menu(menu, index):
59+
"""Called when the player closes the menu."""
60+
61+
# Send the menu again
62+
menu.send(index)
63+
64+
65+
Creating SimpleMenu
66+
--------------------------
67+
68+
This is an example to create :class:`menus.SimpleMenu` which asks a player to accept the server rules.
69+
If he declines, he will be kicked from the server.
70+
71+
.. code-block:: python
72+
73+
import time
74+
75+
from commands.say import SayCommand
76+
77+
from menus import SimpleMenu
78+
from menus import SimpleOption
79+
80+
from messages import SayText2
81+
82+
from players.entity import Player
83+
84+
# Register menu command
85+
@SayCommand(['menus', '/menus', '!menus'])
86+
def say_menus_command(command, index, teamonly):
87+
# Send the menu
88+
menu.send(index)
89+
return False
90+
91+
def my_menu_select_callback(menu, index, option):
92+
"""Called whenever a selection was made."""
93+
94+
if option.value == 'yes':
95+
SayText2('Thank you for accepting the rules!').send(index)
96+
97+
# Player has selected no option
98+
else:
99+
# Kick player for selecting no option
100+
Player(index).kick('You have to accept the rules!')
101+
102+
menu = SimpleMenu()
103+
104+
# Tell the current time
105+
menu.append(f"Current Time: {time.strftime('%H:%M:%S')}")
106+
107+
# Add empty line
108+
menu.append(' ')
109+
menu.append('Do you accept the rules?')
110+
menu.append(' ')
111+
112+
# Add menu options
113+
menu.append(SimpleOption(1, 'Yes', 'yes'))
114+
menu.append(SimpleOption(2, 'No', 'no'))
115+
116+
# Another way to define a select callback.
117+
menu.select_callback = my_menu_select_callback
118+
119+
120+
Creating ListMenu
121+
--------------------------
122+
123+
This example shows how to create a :class:`menus.ListMenu`.
124+
The goal of a list menu is to show a lot of data, but the menu entries are not selectable.
125+
126+
.. code-block:: python
127+
128+
from commands.say import SayCommand
129+
130+
from menus import ListMenu
131+
132+
# Register menu command
133+
@SayCommand(['menus', '/menus', '!menus'])
134+
def say_menus_command(command, index, teamonly):
135+
# Send the menu
136+
menu.send(index)
137+
return False
138+
139+
menu = ListMenu()
140+
141+
# Add menu text
142+
menu.append('This is an example text')

0 commit comments

Comments
 (0)