PagedMenu append but do not increment number

Please post any questions about developing your plugin here. Please use the search function before posting!
12jdlovins
Junior Member
Posts: 17
Joined: Mon Apr 16, 2018 5:11 am

PagedMenu append but do not increment number

Postby 12jdlovins » Wed May 02, 2018 11:15 pm

Hey is it possible to append text to a menu without incrementing the number required to press it? I want to have some text that is dynamic above the options but doing menu.append() causes it to increment the rest of the options. Setting selectable to false doesn't do that sadly.
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: PagedMenu append but do not increment number

Postby Ayuto » Thu May 03, 2018 7:54 am

There is the description attribute. It will display a text between the menu title and the first separator.
12jdlovins
Junior Member
Posts: 17
Joined: Mon Apr 16, 2018 5:11 am

Re: PagedMenu append but do not increment number

Postby 12jdlovins » Thu May 03, 2018 1:21 pm

I know I saw that but it's not what I need. I need to display information between the separators without messing up the ordering.

Title
-----------------
Some info
More info
1. Option 1
2. Option 2
------------------

Is what I'm ideally looking to do.


edit:

Looking at how the menus are coded this is not really easy to do it seems... which is a shame because i really need it
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: PagedMenu append but do not increment number

Postby Ayuto » Thu May 03, 2018 2:04 pm

You need to implement that on your own by subclassing PagedRadioMenu. This is how you would do that:

Syntax: Select all

from menus.radio import PagedRadioMenu
from menus.radio import PagedRadioOption as PagedOption
from commands.typed import TypedSayCommand

class PagedMenuHdr(PagedRadioMenu):
def _format_header(self, player_index, page, slots):
buffer = super()._format_header(player_index, page, slots)
buffer += 'Some info\nMoreInfo\n'
return buffer

menu = PagedMenuHdr()
menu.append(PagedOption('Option 1'))
menu.append(PagedOption('Option 2'))

@TypedSayCommand('!test')
def on_test(info, *args):
menu.send(info.index)
12jdlovins
Junior Member
Posts: 17
Joined: Mon Apr 16, 2018 5:11 am

Re: PagedMenu append but do not increment number

Postby 12jdlovins » Thu May 03, 2018 2:31 pm

Ayuto wrote:You need to implement that on your own by subclassing PagedRadioMenu. This is how you would do that:

Syntax: Select all

from menus.radio import PagedRadioMenu
from menus.radio import PagedRadioOption as PagedOption
from commands.typed import TypedSayCommand

class PagedMenuHdr(PagedRadioMenu):
def _format_header(self, player_index, page, slots):
buffer = super()._format_header(player_index, page, slots)
buffer += 'Some info\nMoreInfo\n'
return buffer

menu = PagedMenuHdr()
menu.append(PagedOption('Option 1'))
menu.append(PagedOption('Option 2'))

@TypedSayCommand('!test')
def on_test(info, *args):
menu.send(info.index)


Thanks for the reply!

That's pretty close but i only want it on the first page and it needs to be built each refresh on the build callback due to the information changing from user input. I'm messing around in radio.py and i see what the issue is but not sure how to fix it elegantly.
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: PagedMenu append but do not increment number

Postby Ayuto » Thu May 03, 2018 2:53 pm

Well, then you just need to add a single line:

Syntax: Select all

class PagedMenuHdr(PagedRadioMenu):
def _format_header(self, player_index, page, slots):
buffer = super()._format_header(player_index, page, slots)
if page.index == 0:
buffer += 'Some info\nMoreInfo\n'
return buffer
12jdlovins
Junior Member
Posts: 17
Joined: Mon Apr 16, 2018 5:11 am

Re: PagedMenu append but do not increment number

Postby 12jdlovins » Thu May 03, 2018 3:03 pm

Ayuto wrote:Well, then you just need to add a single line:

Syntax: Select all

class PagedMenuHdr(PagedRadioMenu):
def _format_header(self, player_index, page, slots):
buffer = super()._format_header(player_index, page, slots)
if page.index == 0:
buffer += 'Some info\nMoreInfo\n'
return buffer



Hmm well I guess that would work wouldn't it... I just finished hacking away at the base radio.py code and made this work with however many .append() calls... Though I will be forced to go with your solution if i want to keep updates :P

Syntax: Select all

def _format_body(self, player_index, page, slots):
"""Prepare the body for the menu.

:param int player_index: A player index.
:param _PlayerPage page: The player's current page.
:param slots: A set to which slots can be added.
:type slots: :class:`set`
"""
buffer = ''

# Get all options for the current page
options = self._get_options(page.index)
numbered_options = []

for option in options:
if isinstance(option, PagedRadioOption):
numbered_options.append(option)

page.options = dict(tuple(enumerate(numbered_options, 1)))
option_index = 1

# Loop through all options of the current page
for option in options:
if isinstance(option, PagedRadioOption):
buffer += option._render(player_index, option_index)
if option.selectable:
slots.add(option_index)
option_index += 1
elif isinstance(option, Text):
buffer += option._render(player_index, 0)
else:
buffer += Text(option)._render(player_index, 0)

# Fill the rest of the menu
if self.fill:
buffer += ' \n' * (self._get_max_item_count() - len(options))

return buffer


edit:

Yeah your solution is better since it allows to fit more stuff into the menu. I forgot you can extend things like that...

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 40 guests