Skip to content
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

Allow to hide information about pages in the menu #471

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions addons/source-python/packages/source-python/menus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,21 @@ def _translate_text(text, player_index):
return text


def _format_paged_title(menu, player_index, page):
buffer = ''

if menu.title:
buffer += _translate_text(menu.title, player_index)

if menu.show_pages:
if buffer:
buffer += ' '

buffer += f'[{page.index + 1}/{menu.page_count}]'

return buffer


# =============================================================================
# >> LISTENERS
# =============================================================================
Expand Down
20 changes: 7 additions & 13 deletions addons/source-python/packages/source-python/menus/esc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from menus.base import _BaseMenu
from menus.base import _PagedMenuBase
from menus.base import _BaseOption
from menus.base import _format_paged_title
from menus.base import _translate_text
from menus.queue import ESC_SELECTION_CMD
from menus.queue import _esc_queues
Expand Down Expand Up @@ -169,7 +170,7 @@ class PagedESCMenu(SimpleESCMenu, _PagedMenuBase):
def __init__(
self, data=None, select_callback=None, build_callback=None, close_callback=None,
description=None, title=None, title_color=WHITE, fill=True,
parent_menu=None):
parent_menu=None, show_pages=True):
"""Initialize the object.

:param iterable|None data: See :meth:`menus.base._BaseMenu.__init__`.
Expand All @@ -192,6 +193,7 @@ def __init__(
description, title, title_color)
self.fill = fill
self.parent_menu = parent_menu
self.show_pages = show_pages

@staticmethod
def _get_max_item_count():
Expand All @@ -205,16 +207,8 @@ def _format_header(self, player_index, page, data):
:param _PlayerPage page: The player's current page.
:param KeyValues data: The current menu data.
"""
# Create the page info string
info = '[{0}/{1}]'.format(page.index + 1, self.page_count)

if self.title is not None:
data.set_string('title', '{0} {1}'.format(
_translate_text(self.title, player_index), info))
else:
data.set_string('title', info)

data.set_color('color', self.title_color)
buffer = _format_paged_title(self, player_index, page)
data.set_string('title', buffer)

def _format_body(self, player_index, page, data):
"""Prepare the body for the menu.
Expand Down Expand Up @@ -316,7 +310,7 @@ class ListESCMenu(PagedESCMenu):
def __init__(
self, data=None, select_callback=None, build_callback=None, close_callback=None,
description=None, title=None, title_color=WHITE, fill=True,
parent_menu=None, items_per_page=5):
parent_menu=None, items_per_page=5, show_pages=True):
"""Initialize the object.

:param iterable|None data: See :meth:`menus.base._BaseMenu.__init__`.
Expand All @@ -335,7 +329,7 @@ def __init__(
on a single page (5 is the maximum).
"""
super().__init__(data, select_callback, build_callback, close_callback, description,
title, title_color, fill, parent_menu)
title, title_color, fill, parent_menu, show_pages)
self.items_per_page = items_per_page

def _get_max_item_count(self):
Expand Down
15 changes: 8 additions & 7 deletions addons/source-python/packages/source-python/menus/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from menus.base import _BaseMenu
from menus.base import _PagedMenuBase
from menus.base import _BaseOption
from menus.base import _format_paged_title
from menus.base import _translate_text
from menus.queue import _radio_queues
# Messages
Expand Down Expand Up @@ -141,7 +142,7 @@ def __init__(
self, data=None, select_callback=None,
build_callback=None, close_callback=None, description=None,
title=None, top_separator='-' * 30, bottom_separator='-' * 30,
fill=True, parent_menu=None):
fill=True, parent_menu=None, show_pages=True):
"""Initialize the object.

:param iterable|None data: See :meth:`menus.base._BaseMenu.__init__`.
Expand Down Expand Up @@ -172,6 +173,7 @@ def __init__(
self.bottom_separator = bottom_separator
self.fill = fill
self.parent_menu = parent_menu
self.show_pages = show_pages

@staticmethod
def _get_max_item_count():
Expand All @@ -186,11 +188,10 @@ def _format_header(self, player_index, page, slots):
:param slots: A set to which slots can be added.
:type slots: :class:`set`
"""
# Create the page info string
info = '[{0}/{1}]\n'.format(page.index + 1, self.page_count)
buffer = _format_paged_title(self, player_index, page)

buffer = '{0} {1}'.format(_translate_text(
self.title, player_index), info) if self.title else info
if buffer:
buffer += '\n'

# Set description if present
if self.description is not None:
Expand Down Expand Up @@ -325,7 +326,7 @@ def __init__(
self, data=None, select_callback=None, build_callback=None, close_callback=None,
description=None, title=None, top_separator='-' * 30,
bottom_separator='-' * 30, fill=True, parent_menu=None,
items_per_page=10):
items_per_page=10, show_pages=True):
"""Initialize the object.

:param iterable|None data: See :meth:`menus.base._BaseMenu.__init__`.
Expand All @@ -345,7 +346,7 @@ def __init__(
on a single page.
"""
super().__init__(data, select_callback, build_callback, close_callback, description,
title, top_separator, bottom_separator, fill, parent_menu)
title, top_separator, bottom_separator, fill, parent_menu, show_pages)
self.items_per_page = items_per_page

def _get_max_item_count(self):
Expand Down