Skip to content

Commit 116c096

Browse files
committed
Merge branch 'master' of https://github.com/srpg/Source.Python
2 parents 11eb03e + 715d219 commit 116c096

File tree

1 file changed

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

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 paged menu
11+
12+
.. code-block:: python
13+
14+
import random
15+
16+
from commands.say import SayCommand
17+
18+
from menus import PagedMenu
19+
from menus import PagedOption
20+
21+
from players.entity import Player
22+
23+
24+
# Register menu command
25+
@SayCommand(['menu', '/menu', '!menu'])
26+
def say_command(command, index, teamonly):
27+
# Send the menu
28+
menu.send(index)
29+
return False
30+
31+
def my_select_callback(menu, index, option):
32+
'''
33+
Called whenever a selection was made.
34+
'''
35+
36+
# Shuffle the menu : D
37+
random.shuffle(menu)
38+
39+
# Make it sticky
40+
return menu
41+
42+
menu = PagedMenu(
43+
title='Welcome menu',
44+
description='Choose an option:',
45+
select_callback=my_select_callback
46+
)
47+
48+
# Add options from 1 to 20
49+
for i in range(1, 20):
50+
menu.append(PagedOption(f'{i}', i))
51+
52+
# Register close button to send back the menu
53+
@menu.register_close_callback
54+
def _on_close_menu(menu, index):
55+
menu.send(index)
56+
57+
58+
Creating SimpleMenu
59+
--------------------------
60+
61+
This is a simple example to create simple menu
62+
63+
.. code-block:: python
64+
65+
import time
66+
67+
from commands.say import SayCommand
68+
69+
from menus import SimpleMenu
70+
from menus import SimpleOption
71+
from menus import Text
72+
73+
from messages import SayText2
74+
75+
from players.entity import Player
76+
77+
# Register menu command
78+
@SayCommand(['menus', '/menus', '!menus'])
79+
def say_menus_command(command, index, teamonly):
80+
# Send the menu
81+
menu.send(index)
82+
return False
83+
84+
def my_menu_select_callback(menu, index, option):
85+
'''
86+
Called whenever a selection was made.
87+
'''
88+
89+
if option.value == 'yes':
90+
SayText2('Thank you for accepting the rules!').send(index)
91+
92+
# player have selected no option
93+
else:
94+
# Kick player for selecting no option
95+
Player(index).kick('You have to accept the rules!')
96+
97+
menu = SimpleMenu()
98+
99+
# Tell the current time
100+
menu.append(Text(f"Current Time: {time.strftime('%H:%M:%S')}"))
101+
102+
# Add empty line
103+
menu.append(Text(' '))
104+
menu.append(Text('Do you accept the rules?'))
105+
menu.append(Text(' '))
106+
107+
# Add in menu options
108+
menu.append(SimpleOption(1, 'Yes', 'yes'))
109+
menu.append(SimpleOption(2, 'No', 'no'))
110+
111+
menu.select_callback=my_menu_select_callback
112+
113+
114+
Creating ListMenu
115+
--------------------------
116+
117+
This is a simple example to create list menu
118+
119+
.. code-block:: python
120+
121+
from commands.say import SayCommand
122+
123+
from menus import ListMenu
124+
from menus import Text
125+
126+
# Register menu command
127+
@SayCommand(['menus', '/menus', '!menus'])
128+
def say_menus_command(command, index, teamonly):
129+
# Send the menu
130+
menu.send(index)
131+
return False
132+
133+
menu = ListMenu()
134+
# Add in menu text
135+
menu.append(Text('This is a example text'))

0 commit comments

Comments
 (0)