1/*
2 src/example2.cpp -- C++ version of an example application that shows
3 how to use the form helper class. For a Python implementation, see
4 '../python/example2.py'.
5
6 NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>.
7 The widget drawing code is based on the NanoVG demo application
8 by Mikko Mononen.
9
10 All rights reserved. Use of this source code is governed by a
11 BSD-style license that can be found in the LICENSE.txt file.
12*/
13
14#include <nanogui/nanogui.h>
15#include <iostream>
16
17using namespace nanogui;
18
19enum test_enum {
20 Item1 = 0,
21 Item2,
22 Item3
23};
24
25bool bvar = true;
26int ivar = 12345678;
27double dvar = 3.1415926;
28float fvar = (float)dvar;
29std::string strval = "A string";
30test_enum enumval = Item2;
31Color colval(0.5f, 0.5f, 0.7f, 1.f);
32
33int main(int /* argc */, char ** /* argv */) {
34 nanogui::init();
35
36 /* scoped variables */ {
37 bool use_gl_4_1 = false;// Set to true to create an OpenGL 4.1 context.
38 Screen *screen = nullptr;
39
40 if (use_gl_4_1) {
41 // NanoGUI presents many options for you to utilize at your discretion.
42 // See include/nanogui/screen.h for what all of these represent.
43 screen = new Screen(Vector2i(500, 700), "NanoGUI test [GL 4.1]",
44 /*resizable*/true, /*fullscreen*/false, /*colorBits*/8,
45 /*alphaBits*/8, /*depthBits*/24, /*stencilBits*/8,
46 /*nSamples*/0, /*glMajor*/4, /*glMinor*/1);
47 } else {
48 screen = new Screen(Vector2i(500, 700), "NanoGUI test");
49 }
50
51 bool enabled = true;
52 FormHelper *gui = new FormHelper(screen);
53 ref<Window> window = gui->addWindow(Eigen::Vector2i(10, 10), "Form helper example");
54 gui->addGroup("Basic types");
55 gui->addVariable("bool", bvar);
56 gui->addVariable("string", strval);
57
58 gui->addGroup("Validating fields");
59 gui->addVariable("int", ivar)->setSpinnable(true);
60 gui->addVariable("float", fvar);
61 gui->addVariable("double", dvar)->setSpinnable(true);
62
63 gui->addGroup("Complex types");
64 gui->addVariable("Enumeration", enumval, enabled)
65 ->setItems({"Item 1", "Item 2", "Item 3"});
66 gui->addVariable("Color", colval)
67 ->setFinalCallback([](const Color &c) {
68 std::cout << "ColorPicker Final Callback: ["
69 << c.r() << ", "
70 << c.g() << ", "
71 << c.b() << ", "
72 << c.w() << "]" << std::endl;
73 });
74
75 gui->addGroup("Other widgets");
76 gui->addButton("A button", []() { std::cout << "Button pressed." << std::endl; });
77
78 screen->setVisible(true);
79 screen->performLayout();
80 window->center();
81
82 nanogui::mainloop();
83 }
84
85 nanogui::shutdown();
86 return 0;
87}
88