Skip to content

Commit b32c82b

Browse files
committed
Added feature requested in issue #113
Added missing documentation. Fixed one occurence not using _get_max_item_count().
1 parent ed25bce commit b32c82b

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed

addons/source-python/packages/source-python/menus/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,26 @@
1616

1717
from menus.esc import SimpleESCMenu as SimpleMenu
1818
from menus.esc import SimpleESCOption as SimpleOption
19+
20+
from menus.esc import ListESCMenu as ListMenu
21+
from menus.esc import ListESCOption as ListOption
1922
else:
2023
from menus.radio import PagedRadioMenu as PagedMenu
2124
from menus.radio import PagedRadioOption as PagedOption
2225

2326
from menus.radio import SimpleRadioMenu as SimpleMenu
2427
from menus.radio import SimpleRadioOption as SimpleOption
2528

29+
from menus.radio import ListRadioMenu as ListMenu
30+
from menus.radio import ListRadioOption as ListOption
31+
2632

2733
# =============================================================================
2834
# >> ALL DECLARATION
2935
# =============================================================================
30-
__all__ = ('PagedMenu',
36+
__all__ = ('ListMenu',
37+
'ListOption',
38+
'PagedMenu',
3139
'PagedOption',
3240
'SimpleMenu',
3341
'SimpleOption',

addons/source-python/packages/source-python/menus/esc.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def __init__(
178178
:param Color title_color: See :meth:`SimpleESCMenu.__init__`.
179179
:param bool fill: If True the menu will always have the same size by
180180
filling unused options.
181+
:param _BaseMenu parent_menu: A menu that will be displayed when
182+
hitting 'Back' on the first page.
181183
"""
182184
super().__init__(
183185
data, select_callback, build_callback,
@@ -227,7 +229,7 @@ def _format_body(self, player_index, page, data):
227229
# Fill the rest of the menu with empty options
228230
if self.fill:
229231
option_num = len(page.options)
230-
for index in range(5 - option_num):
232+
for index in range(self._get_max_item_count() - option_num):
231233
index += option_num + 1
232234
button = data.find_key(str(index), True)
233235
button.set_string('msg', '')
@@ -301,6 +303,38 @@ def _select(self, player_index, choice_index):
301303
return super()._select(player_index, choice_index)
302304

303305

306+
class ListESCMenu(PagedESCMenu):
307+
"""Creates a list-like ESC menu.
308+
309+
Navigation options are added automatically."""
310+
311+
def __init__(
312+
self, data=None, select_callback=None, build_callback=None,
313+
description=None, title=None, title_color=WHITE, fill=True,
314+
parent_menu=None, items_per_page=5):
315+
"""Initialize the object.
316+
317+
:param iterable|None data: See :meth:`menus.base._BaseMenu.__init__`.
318+
:param callable|None select_callback: See
319+
:meth:`menus.base._BaseMenu.__init__`.
320+
:param callable|None build_callback: See
321+
:meth:`menus.base._BaseMenu.__init__`.
322+
:param str|None description: See :meth:`SimpleESCMenu.__init__`.
323+
:param str|None title: See :meth:`SimpleESCMenu.__init__`.
324+
:param Color title_color: See :meth:`SimpleESCMenu.__init__`.
325+
:param bool fill: See :meth:`PagedESCMenu.__init__`.
326+
:param _BaseMenu parent_menu: See :meth:`PagedESCMenu.__init__`.
327+
:param int items_per_page: Number of options that should be displayed
328+
on a single page (5 is the maximum).
329+
"""
330+
super().__init__(data, select_callback, build_callback, description,
331+
title, title_color, fill, parent_menu)
332+
self.items_per_page = items_per_page
333+
334+
def _get_max_item_count(self):
335+
return self.items_per_page
336+
337+
304338
class SimpleESCOption(_BaseOption):
305339
"""Provides options for :class:`SimpleESCMenu` objects."""
306340

@@ -332,3 +366,25 @@ def _render(self, player_index, choice_index=None):
332366
"""See :meth:`menus.base._MenuData._render`."""
333367
return '{0}. {1}'.format(
334368
choice_index, _translate_text(self.text, player_index))
369+
370+
371+
class ListESCOption(PagedESCOption):
372+
"""Provides options for :class:`ListESCMenu` objects."""
373+
374+
def __init__(self, text, highlight=True, enumerated=True):
375+
"""Initialize the option.
376+
377+
:param str text: See :meth:`menus.base._BaseOption.__init__`.
378+
:param bool hightlight: See :meth:`SimpleESCMenu.__init__`.
379+
:param bool enumerated: If True the number of the option will be added
380+
in front of the text.
381+
"""
382+
super().__init__(text, None, highlight, False)
383+
self.enumerated = enumerated
384+
385+
def _render(self, player_index, choice_index=None):
386+
"""See :meth:`menus.base._MenuData._render`."""
387+
if self.enumerated:
388+
return super()._render(player_index, choice_index)
389+
390+
return _translate_text(self.text, player_index)

addons/source-python/packages/source-python/menus/radio.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def __init__(
157157
the body.
158158
:param bool fill: If True the menu will be filled so that it will
159159
always have the same size.
160+
:param _BaseMenu parent_menu: A menu that will be displayed when
161+
hitting 'Back' on the first page.
160162
"""
161163
super().__init__(data, select_callback, build_callback)
162164

@@ -311,6 +313,40 @@ def _select(self, player_index, choice_index):
311313
return super()._select(player_index, choice_index)
312314

313315

316+
class ListRadioMenu(PagedRadioMenu):
317+
"""Creates a list-like radio menu.
318+
319+
Navigation options are added automatically."""
320+
321+
def __init__(
322+
self, data=None, select_callback=None, build_callback=None,
323+
description=None, title=None, top_separator='-' * 30,
324+
bottom_separator='-' * 30, fill=True, parent_menu=None,
325+
items_per_page=10):
326+
"""Initialize the object.
327+
328+
:param iterable|None data: See :meth:`menus.base._BaseMenu.__init__`.
329+
:param callable|None select_callback: See
330+
:meth:`menus.base._BaseMenu.__init__`.
331+
:param callable|None build_callback: See
332+
:meth:`menus.base._BaseMenu.__init__`.
333+
:param str|None description: See :meth:`PagedRadioMenu.__init__`.
334+
:param str|None title: See :meth:`PagedRadioMenu.__init__`.
335+
:param str top_separator: See :meth:`PagedRadioMenu.__init__`.
336+
:param str bottom_separator: See :meth:`PagedRadioMenu.__init__`.
337+
:param bool fill: See :meth:`PagedRadioMenu.__init__`.
338+
:param _BaseMenu parent_menu: See :meth:`PagedRadioMenu.__init__`.
339+
:param int items_per_page: Number of options that should be displayed
340+
on a single page.
341+
"""
342+
super().__init__(data, select_callback, build_callback, description,
343+
title, top_separator, bottom_separator, fill, parent_menu)
344+
self.items_per_page = items_per_page
345+
346+
def _get_max_item_count(self):
347+
return self.items_per_page
348+
349+
314350
class _BaseRadioOption(_BaseOption):
315351
"""Base class for radio options."""
316352

@@ -359,3 +395,28 @@ def _render(self, player_index, choice_index):
359395
choice_index,
360396
_translate_text(self.text, player_index)
361397
)
398+
399+
400+
class ListRadioOption(PagedRadioOption):
401+
"""Provides options for :class:`ListRadioMenu` objects."""
402+
403+
def __init__(self, text, highlight=True, enumerated=True):
404+
"""Initialize the option.
405+
406+
:param str text: See :meth:`menus.base._BaseOption.__init__`.
407+
:param bool hightlight: If True the text will be highlighted.
408+
:param bool enumerated: If True the number of the option will be added
409+
in front of the text.
410+
411+
.. note:: ``highlight`` only works if ``enumerated`` is set to True.
412+
"""
413+
super().__init__(text, None, highlight, False)
414+
self.enumerated = enumerated
415+
416+
def _render(self, player_index, choice_index):
417+
"""See :meth:`menus.base._MenuData._render`."""
418+
if self.enumerated:
419+
return super()._render(player_index, choice_index)
420+
421+
return '{}{}\n'.format(self._get_highlight_prefix(),
422+
_translate_text(self.text, player_index))

0 commit comments

Comments
 (0)