NanoGUI is a a minimalistic cross-platform widget library for OpenGL 3.x. It supports automatic layout generation, stateful C++11 lambdas callbacks, a variety of useful widget types and Retina-capable rendering on Apple devices thanks to NanoVG by Mikko Mononen. Python bindings of all functionality are provided using pybind11.
NanoGUI builds on GLFW for cross-platform OpenGL context creation and event handling, GLEW to use OpenGL 3.x Windows, Eigen for basic vector types, and NanoVG to draw 2D primitives.
Note that the depencency library NanoVG already includes some basic example code to draw good-looking static widgets; what NanoGUI does is to flesh it out into a complete GUI toolkit with event handling, layout generation, etc.
NanoGUI currently works on Mac OS X (Clang) Linux (GCC or Clang) and Windows (Visual Studio ≥ 2015); it requires a recent C++11 capable compiler. All dependencies are jointly built using a CMake-based build system.
NanoGUI makes it easy to instantiate widgets, set layout constraints, and
register event callbacks using high-level C++11 code. For instance, the
following two lines from the included example application add a new button to
an existing window window
and register an event callback.
Button *b = new Button(window, "Plain button");
b->setCallback([] { cout << "pushed!" << endl; });
The following lines from the example application create the coupled slider and text box on the bottom of the second window (see the screenshot).
/* Create an empty panel with a horizontal layout */
Widget *panel = new Widget(window);
panel->setLayout(new BoxLayout(BoxLayout::Horizontal, BoxLayout::Middle, 0, 20));
/* Add a slider and set defaults */
Slider *slider = new Slider(panel);
slider->setValue(0.5f);
slider->setFixedWidth(80);
/* Add a textbox and set defaults */
TextBox *textBox = new TextBox(panel);
textBox->setFixedSize(Vector2i(60, 25));
textBox->setValue("50");
textBox->setUnits("%");
/* Propagate slider changes to the text box */
slider->setCallback([textBox](float value) {
textBox->setValue(std::to_string((int) (value * 100)));
});
The Python version of this same piece of code looks like this:
# Create an empty panel with a horizontal layout
panel = Widget(window)
panel.setLayout(BoxLayout(BoxLayout.Horizontal, BoxLayout.Middle, 0, 20))
# Add a slider and set defaults
slider = Slider(panel)
slider.setValue(0.5f)
slider.setFixedWidth(80)
# Add a textbox and set defaults
textBox = TextBox(panel)
textBox.setFixedSize(Vector2i(60, 25))
textBox.setValue("50")
textBox.setUnits("%")
# Propagate slider changes to the text box
def cb(value):
textBox.setValue("%i" % int(value * 100))
slider.setCallback(cb)
Clone the repository and all dependencies (with git clone --recursive
),
run CMake to generate Makefiles or CMake/Visual Studio project files, and
the rest should just work automatically.