0

I'm creating a menu system for an openGL application I'm working on in C++. I'm shooting for a structure to something I've learned before (TObjects in Delphi). The structure I am shooting for is something along the lines of

Object->Component->Control(if visual/interacts)->Specific Item

One thing I am thinking about is how I control the users 'focus'. Focus would be a member of control, and would be useful for instance what buttons would do, where typing occurs, etc... It would seem that focus should be a pointer to a specific GUI item, and thus a derived class of control. The object type that I would need to point to would not always be the same. How can I declare a pointer in the control base class to point to any of derived classes? I've tried searching google but I don't think I'm using the correct terms because I just get a lot of explanations of inheritance.. I don't think the answer is there.

One work around I thought about using was a protected value in the Component Base class, let's call it ID. ID would be unique each time any new object is created and I could declare the focus pointer as int* focus and then try to back track from the IDs what needs to happen. It seems like it would be a bit more of a hassle.

Thanks in advance for any advice

Chemistpp
  • 2,006
  • 2
  • 28
  • 48
  • I am not sure that any user would want to have focus controlled. Typically focus is for input in a particular window or control. People tend to want to decide for themselves as to which control or window should have focus. About the only contrary to this is a modal dialog box and that is typically for a specific action see http://stackoverflow.com/questions/361493/why-are-modal-dialog-boxes-evil for a perspective on this. – Richard Chambers Jun 18 '13 at 00:24
  • I mean it's so that I know what the user has focused on. When I handle click events, I can see what the user as clicked and set the focus to that object so particular events can occur (like entering a password into an edit box). – Chemistpp Jun 18 '13 at 00:29
  • for a windowing system normally each window will have an identifier assigned to it for instance a window handle which is used with the operating system calls to access, control, and manage the windows. Since the operating system knows which window has focus in order to know where to send mouse and keyboard events, there is normally a function that you would use to ask the operating system which window has focus. Here is a tutorial on opengl windows. http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/ – Richard Chambers Jun 18 '13 at 00:36
  • Also here is a wiki that has an example of a single window showing keyboard input and how it is done. http://content.gpwiki.org/index.php/GLFW:Tutorials:Basics – Richard Chambers Jun 18 '13 at 00:41
  • 1
    Well, after giving that tutorial a quick look, I book marked it because it will be really useful for other things down the road I knew I was going to have questions with. The more I think about it, the more I think I don't need this pointer since I've declared virtual functions, such as event handlers and rendering, and all of these are derived classes of each other. I would already at one point have had to figure out what the user has 'focused to on a click' etc to change such a pointer like I described, but at that time, I can just call that objects specific event handler function. – Chemistpp Jun 18 '13 at 00:51
  • That sounds like a reasonable path to take. I would expect the openGL library to handle at least some of what you are wanting to do and in a more reliable and robust way. It really looks to me like you will know what window has focus because that is the window that is getting keyboard or mouse events. You may need to have some kind of a call back or something if you need to impose some kind of structure on your windows however openGL may already have the facilities for that as well. Good luck on your project. – Richard Chambers Jun 18 '13 at 01:02
  • "I just get a lot of explanations of inheritance.. I don't think the answer is there." It's entirely there. If you want a pointer to a collection of different objects, you either need to learn templates or you need a base class which all those objects inherit. `class Focusable { ... }; class ThingThatICanRender : public Focusable { ... };` with that you can now do `ThingThatICanRender foo; Focusable* focus = &foo;` – kfsone Jun 18 '13 at 06:44
  • kfsone If you read my post directly two posts up from yours, I came to that realization that if I set up my inheritance correctly, then I don't need this pointer. I, not being a programmer by trade, was tackling the structure from the bottom up, making a label and an edit box , etc... As opposed to starting from the root of the tree, setting up the virtual functions, and realizing how to use them properly. However, I started thinking also that I should be able to point to a base class but since I haven't gotten the entire thing retooled, I can't test that stuff yet. – Chemistpp Jun 19 '13 at 00:42

0 Answers0