Search code examples
user-interfaceopenglopentk

Programming custom GUI OPenGL


I am creating my own GUI in OpenTK.

I want to fire a mouse event when the cursor is, for example, in one of the GUI controls. How can I do that? Because now I'm just iterating through a list of items in the main class, and in the Opentk´s window´s MouseMove event I'm just checking if the mouse coordinates are within the "region" of the component I'm drawing.

This works for now, but I think it could be done in a better way. This way my code is unordered and in the main class, and I would rather have it in the specific component class.

What I would like is to have an event attached to each component of my GUI, so that I can define many events for one component.

I mean, I would like to have for example a button component where I can override or just use a method that fires when an event occurs. Same as OpenGL´s window where you can override events.


Solution

  • This is not a complete answer, because your question is quite broad, but I hope it helps.

    In order to implement such a system, here are the core components for a potential design:

    UI Components: Some kind of standard interface where different component types can define logic for interactions. Depending on the language, the most common approach is probably something like a parent class Component, with methods to be overridden. These would probably include things like:

    • Mouse Hover
    • Mouse Click (press / release)
    • Click drag

    It will also likely need some additional associated information:

    • Some way to determine the component's location. Could be providing a bounding box, or perhaps a method that tests if a given point is within this component or not.
    • Information or functionality for drawing the component.
    • Display and layering settings (is it visible or hidden, should it draw on top of other components or behind).

    UI Context: The context is a structure that defines the set of components that are existent in the UI. This could be something like a list structure of Components. In order to build your UI, you would add components to this context. The context will define some behaviour:

    • Managing components (add / remove / modify).
    • How to draw the entire context (for example, looping over each component and executing the draw functionality for each).
    • Handling of events (see next section)

    Event Dispatch: To make your UI usable, you can insert an "adapter" layer that handles events from your windowing library (OpenTK) then translates them into usable events for your components and dispatches them. Here is an example of how this might work for a "click" event (pseudo-code):

    function TK_Event_ClickPressed(point) {
        for component in context {
            if component.ContainsPoint(point) {
                component.EventClickPressed()
            }
        }
    }
    

    This is actually the more tricky part of the design, in my opinion, because there are some tricky conventions around how component based UI works. You don't necessarily have to follow them, but they're important to be aware of at least because it is probably how people expect your UI to work:

    • After click press, click drag continues to occur until click release, even if the cursor leaves the component area.
    • "Actions" occur on click release.
    • Click release only takes action if the corresponding click press occurred on the same component.
    • The click release doesn't take any action if the cursor is no longer inside the component (leaving and re-entering the component before release still does the action, though).
    • You can only be actively clicking one component at a time (the one shown on top), even if multiple components overlap at that spot.

    Assuming that you follow these conventions, this means that dispatching events is actually a bit more complicated than just checking if the event point was in a given component or not. You need to maintain some kind of state to keep track of whether the context is currently in a click or not, and which component, if any, is "consuming" the current click. That is, which component should be given the click release and drag events if they occur.


    With these systems in place, you just need to create a window, create a UI context, register the adapter layer to the window to act on that context, set up the window to draw the context on frame, then use the context to add / remove / modify components in your program.