PopUp Menus in a custom GUI API? - c++

I have made my own GUI API for games. One assumption that must be made is the user may want to use a derived version of a Widget I have made.
An example of how I dealt with this with ToolTips is, the user allocates a new ToolTip and sets a global one for the GUI. Ex: getGui().setToolTip(customToolTip);
The GUI then calls toolTip->show() when necessary. The problem with menus in general is that you can have many of them appearing at a given time.
I also would hate to have the user implement an interface:
PopUpFactory::createPopUp()
PopUpFactory::destroyPopup()
How is this usually dealt with? Who or how is the memory also managed for these?
There is always the option of limiting to something like 9 nested menus and have the user set an array of 9 PopUps but that seems messy.
Thanks

Related

wxWidgets widget IDs

I'm a little bit confused about how the IDs work in wxWidgets, do I have to ensure that all ids across all windows that I create are unique to each of their own functions? Like if I have two wxID_OK's for two different dialog boxes are they going to start firing off events in other windows just because they share the same id?
Currently I've been maintaining a huge enum to grab my IDs from, this seems a bit silly though, and was wondering if I just had a misunderstanding of what is actually going on.
I nerver use id's nowadays. I use wxID_ANY for all widgets and use the widget pointer for identification. This works just as well and so there is no reason for two id's for the same widget. There might be some corner cases where real id's are required but i have not found any.
It's a good idea to use unique IDs inside each top level window (i.e. a wxFrame or a wxDialog) because the controls include their ID in the wxCommandEvents they generate and as command events are propagated upwards the window hierarchy until they reach the first top level window, it could be confusing to have 2 controls with the same ID as any handler defined in their common parent window would need to be careful to distinguish between them.
There are no restrictions on the reuse of IDs in different dialog boxes however.
And an even better idea is to not use any non standard IDs at all but just let wxWidgets generate them for you by specifying wxID_ANY when creating controls and using Bind() to connect your event handlers instead of the IDs in the event tables.

cpp graphical menu

I am making a new graphical menu interface for a project I am making. I don't want to use the menu system provided by windows APIs and want to make one from scratch.
My question is, what is the best method for setting up the structure?
I'm thinking I will need a menu item object, each of which will have to have their own item array list, etc...
Is it considered sloppy to have recursive coding like that? (Ie an object which contains objects of itself, which contains objects of itself, etc...)
I'm thinking I can give the item object a draw interface which checks itself to see if it has an item array that is not null. If it does, it executes the draw command all the way down, thereby giving me a menu with (for my purposes) unlimited submenu level
In my opinion your approach is fine. In nearly all UI frameworks, views contain views as subviews after all.
But the thing is that writing drawing code is too much work for small projects I think. I would consider using a UI framework such as QT and use its view mechanism as a starting point. You can write your own Menu class which will be a subclass of generic View class in the framework.

wxSmith a good way to manage wxPanel

I have a project where I'd like to have many wxPanel which are displayed or hide, depending the selection of the user. All panel are on the same position, only one is displayed at a time.
On a code side, there is no problem at all. Where it gets tricky, is how to manage this with wxSmith and keep a clear view while having many wxpanel at the sample location?
One way which is really not proper is to user the wxNotebook, and then when you start the soft delete all tabs and then show the needed panel.
I have look around to try to have the panel on a "other" wxSmith window and then load it, like a class but haven't find anything good.
I'm sure, as wxSmith is really a great tool that it must have a way to do this.
Thanks for your help!
See ya
"One way which is really not proper is to user the wxNotebook, and then when you start the soft delete all tabs and then show the needed panel."
Why not? I use that technique for AtomWeaver, and it works fine. The plus side is that you can design each page normally on a RAD GUI builder.
I've created a class called GUI_NotebookPageData that holds a pointer to a single notebook page. Create an array of these, holding info about all notebook pages.
Then, by index, or by name, get the info of the page you want to show/hide, and use wxNotebook's RemovePage()/InsertPage() methods.
This method is specially good for having several pages shown at the same time.
Actually it's possible to use external ressources with wxSmith, then it's very simple to manage the frames.
It create a derived class from wxPanel (or other window) on a new wxSmith window, easy to manage then just required to include it on the project.

Unlimited number of checkboxes

I wonder how to create mechanism which create new checkbox below previous when you click on button. Number of checkboxes are unlimited.
I don't think that table of objects work well, so I think about implementation in list of objects.
Any suggestions?
Here is what I would do:
Create an event for clicking that button (let's call it OnBtnClick)
Use a vector/list to hold all the checkboxes
When OnBtnClick is called you do:
create a checkbox with the desired position and size and make sure it receives an unique id (this will help you differentiate between checkboxes when they are clicked/checked/etc).
add the checkbox to the list (to get its status: checked or not checked)
add the checkbox to the desired window, the parent window (though this may happen automatically when you create it)
if you want to add an event for the added checkbox you should check the manual of your GUI framework (you will probably use the same event handler for all checkboxes and treat them separately based on their id)
Depending on the GUI framework used the bottom details may vary but the idea remains the same. I did this with wxWidgets, QT and MFC but I don't know which framework you use. You should be able to find code samples for each framework.
What would you do with unlimited number of check boxes - confuse the user? So, that he/she wouldn't attempt to use it again? Bad idea, as you can guess now.
You may (should) limit the number of check boxes (or better, limit the number of controls on form/dialog). IMO, more than 10-12 CBs would be cumbersome for the end user. Therefore, better idea is to have all of them on dialog/dialog-resource, and make all of them invisible/disabled. When user does some action, make them visible/enabled - so that end user may do something with it.
Still demand N number of CBs, where N is not determined beforehand? Then you may have checkboxes under Combo box, or use check-boxes under List Control. List Control already hosts this feature, but for CBs under Combo, you may need to write your own class.See this article as an example.

Making a game in Qt regarding GUI windows

I've been wanting to program a simple game with a simple GUI using Qt (Its will be a VERY simple game, nothing fancy). What I've been wondering is, how can I create multiple windows and display them when needed? For an example, a battle screen and an inventory screen. The user should only see one of them, but should be able to access the other one when needed. I was using stacked widget but I'm not sure if that's the proper way. Also, is it better to design the windows in the designer or to code them?
A StackWidget certainly would accomplish what you want to do. The reason why it is not always used for this kind of thing, is that it all the screens are pre-created at the beginning and always exist. This means it takes a little longer to initialize, and you are using more resources than you need at any one time
But as you are saying, if this is a simple game, then I don't see a big problem with it. Just as easily, you could also, create an empty layout and swap the inventory and game panels as needed.
Add my vote to everyone else suggesting to use the designer. It is so much easier to manipulate layouts, actions, and such using the designer then through code.
You can see the Designer manual here
So this is what I would suggest:
Create your "battleScreen.ui" - which is the designer file for your battle screen and everything in it, and then create your "inventory.ui". Both of these could be QWidgets, or QFrames, or whatever makes sense.
Then create your "Game.ui" which will be your QMainWindow.
In your Game main window, you can then add your QStackWidget, and place your inventory, and battle screens in the stack widget.
If you don't know how to do that...
1) drag a QWidget into your form (into the stack widget)
2) select the new QWidget and right-click.
3) Select "Promote to..."
4) Fill out the information to promote the QWidget to your inventory class
Promoted Class Name: The name of your inventory class
Header File: The header file of your inventory class
5) Click add
6) Click Promote.
Hope that helps.
Since I'm not sure what your goals are I can't advise whether or not the stacked widget is appropriate but I think you can accomplish quite a lot using the designer and style sheets. If you need to code some parts of the GUI, you can always drop in a place holder widget and either replace it with coded items or make them children of the place holders.
A general answer for a general question:
Use the Designer to create your windows; hide and show the auxiliary windows as needed.
Use a flow manager class to manage the visibility of a related set of windows.
The stacked widget is useful for managing a button/icon whose appearance changes based on state; the different representations live in the stack.