Jump to content
  • Advertisement
Sign in to follow this  
Jan Wassenberg

Wonderfully simple OpenGL UI

This topic is 1499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's a brief writeup of a simple (9 KLOC, 340 KB binary) but capable and portable OpenGL GUI:

http://wassenberg.dreamhosters.com/blog/2014-05-17/wonderfully-simple-gui/

 

Obligatory screenshot: [attachment=21585:ui.png]

 

Header files are attached: [attachment=21584:ui_headers.zip]

 

I'd love to get a discussion going on any perceived shortcomings of this approach, missing features,

alternative libraries that didn't show up in my search, and your thoughts in general.

Share this post


Link to post
Share on other sites
Advertisement

Well where are the cpp files that implement all of the features with just the interface there isn't much to discuss to be honest. Also this will only work for 2d UI, what about 3d UI like this:

 

What about controller input?

 

How is the UI constructed, from code, other filed, can I override how a button is drawn easily or which shader is being used to render with?

Edited by NightCreature83

Share this post


Link to post
Share on other sites

Let's see how far we can get talking about the interface :)

Ooh, that's a pretty one. Looks like three different modes - main menu as extruded 3d text, then the oblique view, then the white 2d one for actually entering text. It's easy enough to get something like the second one by rendering the UI output to texture and incorporating that into some other scene. I'm more interested in `serious' apps that don't need fancy presentation, though.

Controller button input is easily supported by calling Interaction::OnKeyDown with a real or fake corresponding SDL key code.

"other filed"? You mean whether any data files are necessary? The font renderer has a binary blob that's bundled with the executable (on Linux, it'd be a standalone file), same for the shaders.

If you want ALL buttons to look different, that's an easy change in Painter::DrawButton and Layout::ButtonSize (if you're also changing the default size). This is probably debatable, but I prefer separating size, presentation and control logic. What's the use case for switching shaders?

Here's the code that drives the demo UI. It's simple enough that auto-generation of code is not necessary :D
Note the standard IMGUI if (Button()) { it_was_clicked; } pattern.

static void DoUI(Interaction& p_Interaction, Canvas& p_Canvas, TextPainter& p_TextPainter)
{
    Layout layout(p_Interaction.windowSize);
    Painter painter(p_Canvas, p_TextPainter);
    UI ui(p_Interaction, layout, painter, p_TextPainter);

    layout.BeginGroup(GROUP_LEFT_DOWNWARDS);

    static bool showOptions = true;
    ui.CheckBox(L"Show options", showOptions);
    if (showOptions)
    {
        layout.BeginGroup(GROUP_TOP_RIGHTWARDS | GROUP_NO_MARGIN);

        static bool enableBlending = true;
        ui.CheckBox(L"Enable blending", enableBlending);
        if (enableBlending)
        {
            static float value = 4.0f;
            ui.Slider(1.0f, 16.0f, value);
        }

        layout.EndGroup();

        std::vector<UString> labels;
        labels.push_back(L"YCoCg-DXT5");
        labels.push_back(L"DXT1");
        labels.push_back(L"RGBA");

        static int listSelected = 0;
        ui.ListBox(&labels[0], labels.size(), listSelected);

        layout.BeginGroup(GROUP_TOP_RIGHTWARDS);

        ui.Label(L"Format");
        static int formatIdx;
        ui.ComboBox(&labels[0], labels.size(), formatIdx);

        layout.EndGroup();
    }

    layout.EndGroup();

    if (showOptions)
    {
        layout.BeginGroup(GROUP_RIGHT_DOWNWARDS);

        static bool panelUnfold = true;
        if (ui.BeginPanel(L"Panel", panelUnfold, GROUP_DEFAULT))
        {
            static int selected;
            ui.RadioButton(L"Radio1", 0, selected);
            ui.RadioButton(L"Radio2", 1, selected);
            static bool checked1, checked2; 
            ui.CheckBox(L"Check1", checked1);
            ui.CheckBox(L"Check2", checked2);

            ui.EndPanel();
        }

        if (ui.Button(L"Do things"))
        {
            SDL_Log("clicked\n");
        }

        static UString buf(L"edit box");
        ui.LineEdit(buf);

        layout.EndGroup();
    }

    p_Canvas.Draw();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!