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

Option for menu items to directly call a method rather than using events #256

Open
ConquerProgramming1 opened this issue Feb 4, 2022 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@ConquerProgramming1
Copy link
Contributor

pygame_gui understandably follows the event model of pygame, but that can be inefficient and unnecessary when rather than dozens to thousands of objects processing the same event that doesn't apply to them, a single call can be made to achieve the desired result.

To increase efficiency, and make code easier to debug, I end up creating my own approach to this on top of the existing menu system (UIDropDownMenu in my case), where I keep a dictionary of menu items and which method to call when the menu item is selected.

		commands = {
			'Village': None,
			'Recruit character (100 coins)': self.create_character,
			'Build...': self.build_menu_item_chosen,
		}

I propose that UIDropDownMenu, at minimum, allow for a dictionary of menu items names and the associated method pointer to call. If a dictionary is provided instead of a list, the associated method would be called directly and the event process would be skipped. The existing API doesn't need to change, except for specifying that the options_list can be either a list or dictionary.

I'm going to take a crack at implementing this, so this request is really for any discussion needed...

@MyreMylar
Copy link
Owner

Sounds cool, I know tkinter uses this callback function model.

Perhaps we might also want to exclude elements that have a callback function for specific events from generating events and like-wise stop certain elements from even listening for any/certain events if they don't need to be. Could perhaps have a couple of broad categories of events (input, UI) and be able to exclude certain elements from listening to them by default...

So far all the UIs I've built have been fairly small scale in the range of tens of simultaneous active elements, but if you have use cases with thousands and thousands of elements each generating lots of events then I can see forcing each of those elements to in turn look at events from each of the the other thousand would get wasteful fairly quickly.

@ConquerProgramming1
Copy link
Contributor Author

Ah yeah in the PR I did suppress event processing if the callback was provided.

@ConquerProgramming1
Copy link
Contributor Author

Regarding the categories of events, allowing for registering for certain types of events is another way to handle that, though perhaps gets too much away from the pygame model...

I looked at UISelectionList and realized if we do the same there, it'll be a bit more complex for the developer: We'd probably want a callback that accepted a list of selected strings, so their method declaration would need to be correct. Though I guess that's not too big an issue with typing to help out, and example code.

@MyreMylar MyreMylar added this to the Version 0.7.0 milestone Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants