ppizarror / pygame-menu

A menu for pygame. Simple, and easy to use
https://pygame-menu.readthedocs.io/
Other
544 stars 141 forks source link

Can the width of dropselect's options be set to auto? #386

Closed larryw3i closed 2 years ago

larryw3i commented 2 years ago

Can the width of dropselect's options be set to auto?
Em....., it depends on the width of the font rendering when the width be set to auto, because the width of dropselect is not easy to adjust when my game supports multiple languages.

ppizarror commented 2 years ago

Hi, actually dropselect width cannot be set to auto, but it can be easily implemented If you see https://github.com/ppizarror/pygame-menu/blob/820a506989db8c4e925d225e495d949d70ed2c1c/pygame_menu/widgets/widget/dropselect.py#L728-L868

Render first create the rect elements, then draws the options. For example, in https://github.com/ppizarror/pygame-menu/blob/820a506989db8c4e925d225e495d949d70ed2c1c/pygame_menu/widgets/widget/dropselect.py#L751-L758 the current_rect_bg width is set to widget' _selection_box_width property defined at the constructor level (which is created if you use menu.add.dropselect by DropSelectManager class.

To create an auto feature you must first retrieve the options width (render, then check for the font' rect size). And then update the _selection_box_width. If you look in https://github.com/ppizarror/pygame-menu/blob/820a506989db8c4e925d225e495d949d70ed2c1c/pygame_menu/widgets/widget/dropselect.py#L326 the apply_font method, the selection_box_width property updates if the value is zero to fit the width of the placeholder, as commented in the docs: "Selection box width in px. If 0 compute automatically to fit placeholder". This can be further generalized.

If you can contribute to this new feature it would be super cool. In the mid-term I do not have much free time to try this feature. However, I can try in the next few weeks.

Greetings

larryw3i commented 2 years ago

Thank you. I'll try, although it seems a little complicated to me.

larryw3i commented 2 years ago

I guess I've found the stupidest but effective way.


_max_items_len = max([len(i[0]) for i in self._items])
self._placeholder = f'{placeholder:^{_max_items_len}}' \
    if selection_box_width == 0 else placeholder