void PopupMenu::_activate_submenu(int over) { Node* n = get_node(items[over].submenu); ERR_EXPLAIN("item subnode does not exist: "+items[over].submenu); ERR_FAIL_COND(!n); Popup *pm = n->cast_to<Popup>(); ERR_EXPLAIN("item subnode is not a Popup: "+items[over].submenu); ERR_FAIL_COND(!pm); if (pm->is_visible()) return; //already visible! Point2 p = get_global_pos(); Rect2 pr(p,get_size()); Ref<StyleBox> style = get_stylebox("panel"); pm->set_pos(p+Point2(get_size().width,items[over]._ofs_cache-style->get_offset().y)); pm->popup(); PopupMenu *pum = pm->cast_to<PopupMenu>(); if (pum) { pr.pos-=pum->get_global_pos(); pum->clear_autohide_areas(); pum->add_autohide_area(Rect2(pr.pos.x,pr.pos.y,pr.size.x,items[over]._ofs_cache)); if (over<items.size()-1) { int from = items[over+1]._ofs_cache; pum->add_autohide_area(Rect2(pr.pos.x,pr.pos.y+from,pr.size.x,pr.size.y-from)); } } }
void CustomerInfoPanel::displayFiche(const QString & fichecontent, bool qtui, const QString & id) { Popup * popup = NULL; for(int i = m_popups.size() - 1; i >= 0; i --) { if(id == m_popups[i]->id()) { qDebug() << Q_FUNC_INFO << "fiche id already there" << i << id; popup = m_popups[i]; break; } } QBuffer * inputstream = new QBuffer(this); inputstream->open(QIODevice::ReadWrite); inputstream->write(fichecontent.toUtf8()); inputstream->close(); // Get Data and Popup the profile if ok if (popup == NULL) { popup = new Popup(m_autourl_allowed); m_popups.append(popup); popup->setId(id); connect(popup, SIGNAL(destroyed(QObject *)), this, SLOT(popupDestroyed(QObject *))); connect(popup, SIGNAL(wantsToBeShown(Popup *)), this, SLOT(showNewProfile(Popup *))); connect(popup, SIGNAL(actionFromPopup(const QString &, const QVariant &)), this, SLOT(actionFromPopup(const QString &, const QVariant &))); connect(popup, SIGNAL(newRemarkSubmitted(const QString &, const QString &)), b_engine, SLOT(sendNewRemark(const QString &, const QString &))); }
void Manager::showPopup() { Popup *pp = getRandomProvider<Popup>(adProviders); if (pp && showNextAd()) { time(&lastAdShownAt); pp->showPopupAd(); } }
int main(int argc, char* argv[]) { QApplication app(argc, argv); app.setStyleSheet("QPushButton { font: 20pt }"); Popup *d = new Popup(); d->setChar('a'); //d->open(); return app.exec(); }
void Gui::mouseReleased( int x, int y, MouseButtons mb ) { cout << "Ui::Gui::mouseReleased( " << x << ", " << y << " )" << endl; if ( pChannelPopup != NULL ) { pChannelPopup->mouseReleased( x, y, mb ); if ( pChannelPopup != NULL ) { if ( !pChannelPopup->passEvents() ) return; } } if ( pMouseDragWidget != NULL ) { pMouseDragWidget->onDestroy.disconnect( this ); pMouseDragWidget = NULL; } if ( pMouseChannelWidget != NULL ) { if ( pMousePressedWidget == pMouseChannelWidget ) { if ( pLastClick <= pDblClickTicks ) { pMouseChannelWidget->mouseDblClick( x - pMouseChannelWidget->absoluteXPos(), y - pMouseChannelWidget->absoluteYPos(), mb ); pMouseChannelWidget->mouseClick( x - pMouseChannelWidget->absoluteXPos(), y - pMouseChannelWidget->absoluteYPos(), mb ); pLastClick = pDblClickTicks + 1; } else { pMouseChannelWidget->mouseClick( x - pMouseChannelWidget->absoluteXPos(), y - pMouseChannelWidget->absoluteYPos(), mb ); pLastClick = 0; } pMousePressedWidget->onDestroy.disconnect( this ); pMousePressedWidget = NULL; } pMouseChannelWidget->mouseReleased( x - pMouseChannelWidget->absoluteXPos(), y - pMouseChannelWidget->absoluteYPos(), mb ); } else { for( int i = 0; i < pPopups.count(); i++ ) { Popup* p = pPopups.get( i ); Rect r = p->getRect(); if ( r.pointInside( x, y ) ) { p->mouseReleased( x, y, mb ); if ( !p->passEvents() ) return; } } Widget* o = fgFrame().mouseReleased( x, y, mb ); if ( o != NULL ) { if ( pMousePressedWidget == o ) { pMousePressedWidget->onDestroy.disconnect( this ); pMousePressedWidget = NULL; o->mouseClick( x, y, mb ); } //o->mouseReleased( x, y, mb ); } } }
void TopWindow::processEvent( EvtMenu &rEvtMenu ) { Popup *pPopup = m_rWindowManager.getActivePopup(); // We should never receive a menu event when there is no active popup! if( pPopup == NULL ) { msg_Warn( getIntf(), "unexpected menu event, ignoring" ); return; } pPopup->handleEvent( rEvtMenu ); }
void ColorPicker::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: { btn_pick->set_icon(get_icon("screen_picker", "ColorPicker")); bt_add_preset->set_icon(get_icon("add_preset")); _update_controls(); } break; case NOTIFICATION_ENTER_TREE: { btn_pick->set_icon(get_icon("screen_picker", "ColorPicker")); bt_add_preset->set_icon(get_icon("add_preset")); _update_color(); #ifdef TOOLS_ENABLED if (Engine::get_singleton()->is_editor_hint()) { PoolColorArray saved_presets = EditorSettings::get_singleton()->get_project_metadata("color_picker", "presets", PoolColorArray()); for (int i = 0; i < saved_presets.size(); i++) { add_preset(saved_presets[i]); } } #endif } break; case NOTIFICATION_PARENTED: { for (int i = 0; i < 4; i++) set_margin((Margin)i, get_constant("margin")); } break; case NOTIFICATION_VISIBILITY_CHANGED: { Popup *p = Object::cast_to<Popup>(get_parent()); if (p) p->set_size(Size2(get_combined_minimum_size().width + get_constant("margin") * 2, get_combined_minimum_size().height + get_constant("margin") * 2)); } break; case MainLoop::NOTIFICATION_WM_QUIT_REQUEST: { if (screen != NULL) { if (screen->is_visible()) { screen->hide(); } } } break; } }
void Screen::moveWindowToFront(Window *window) { mChildren.erase(std::remove(mChildren.begin(), mChildren.end(), window), mChildren.end()); mChildren.push_back(window); /* Brute force topological sort (no problem for a few windows..) */ bool changed = false; do { size_t baseIndex = 0; for (size_t index = 0; index < mChildren.size(); ++index) if (mChildren[index] == window) baseIndex = index; changed = false; for (size_t index = 0; index < mChildren.size(); ++index) { Popup *pw = dynamic_cast<Popup *>(mChildren[index]); if (pw && pw->parentWindow() == window && index < baseIndex) { moveWindowToFront(pw); changed = true; break; } } } while (changed); }
Popup* PopupFactory::createGameOverPopup(std::shared_ptr<Delegate> resetDelegate, std::shared_ptr<Delegate> quitDelegate) { cocos2d::CCSize worldSize = cocos2d::CCDirector::sharedDirector()->getWinSize(); std::vector<std::string> buttonsImages = {"retry","quitbig"}; std::vector<std::shared_ptr<Delegate>> delegates = {resetDelegate,quitDelegate}; Popup *p = Popup::create(); p->initPopup(worldSize.width - 300, worldSize.height - 200 , buttonsImages , delegates, "Game Over!", 100, "" ); return p; }
Popup* PopupFactory::createPausePopup(std::shared_ptr<Delegate> continueDelegate, std::shared_ptr<Delegate> quitDelegate) { //std::cout << "Create popup pause"<< std::endl; cocos2d::CCSize worldSize = cocos2d::CCDirector::sharedDirector()->getWinSize(); std::vector<std::string> buttonsImages = {"play","quitbig"}; std::vector<std::shared_ptr<Delegate>> delegates = {continueDelegate,quitDelegate}; Popup *p = Popup::create(); p->initPopup(worldSize.width - 300, worldSize.height - 200 , buttonsImages , delegates, "PAUSE", 100, "" ); return p; }
void PopupMenu::_activate_submenu(int over) { Node *n = get_node(items[over].submenu); ERR_EXPLAIN("item subnode does not exist: " + items[over].submenu); ERR_FAIL_COND(!n); Popup *pm = Object::cast_to<Popup>(n); ERR_EXPLAIN("item subnode is not a Popup: " + items[over].submenu); ERR_FAIL_COND(!pm); if (pm->is_visible_in_tree()) return; //already visible! Point2 p = get_global_position(); Rect2 pr(p, get_size()); Ref<StyleBox> style = get_stylebox("panel"); Point2 pos = p + Point2(get_size().width, items[over]._ofs_cache - style->get_offset().y); Size2 size = pm->get_size(); // fix pos if (pos.x + size.width > get_viewport_rect().size.width) pos.x = p.x - size.width; pm->set_position(pos); pm->popup(); PopupMenu *pum = Object::cast_to<PopupMenu>(pm); if (pum) { pr.position -= pum->get_global_position(); pum->clear_autohide_areas(); pum->add_autohide_area(Rect2(pr.position.x, pr.position.y, pr.size.x, items[over]._ofs_cache)); if (over < items.size() - 1) { int from = items[over + 1]._ofs_cache; pum->add_autohide_area(Rect2(pr.position.x, pr.position.y + from, pr.size.x, pr.size.y - from)); } } }
void View::create (WindowRef & ciWindow, const fs::path & assetFolder) { try { initGraph (&fps, GRAPH_RENDER_FPS, "Frame Time"); initGraph (&cpuGraph, GRAPH_RENDER_MS, "CPU Time"); setSize (ciWindow->getSize()); nanogui::Window * window = new nanogui::Window (this, "Button demo"); window->setPosition (ivec2 (15, 15)); window->setLayout (new GroupLayout()); /* No need to store a pointer, the data structure will be automatically freed when the parent window is deleted */ new Label (window, "Push buttons", "sans-bold"); Button * b = new Button (window, "Plain button"); b->setCallback ([] { cout << "pushed!" << endl; }); b = new Button (window, "Styled", ENTYPO_ICON_ROCKET); b->setBackgroundColor (Colour (0, 0, 255, 25)); b->setCallback ([] { cout << "pushed!" << endl; }); new Label (window, "Toggle buttons", "sans-bold"); b = new Button (window, "Toggle me"); b->setFlags (Button::ToggleButton); b->setChangeCallback ([] (bool state) { cout << "Toggle button state: " << state << endl; }); new Label (window, "Radio buttons", "sans-bold"); b = new Button (window, "Radio button 1"); b->setFlags (Button::RadioButton); b = new Button (window, "Radio button 2"); b->setFlags (Button::RadioButton); new Label (window, "A tool palette", "sans-bold"); Widget * tools = new Widget (window); tools->setLayout (new BoxLayout (Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new ToolButton (tools, ENTYPO_ICON_CLOUD); b = new ToolButton (tools, ENTYPO_ICON_FF); b = new ToolButton (tools, ENTYPO_ICON_COMPASS); b = new ToolButton (tools, ENTYPO_ICON_INSTALL); new Label (window, "Popup buttons", "sans-bold"); PopupButton * popupBtn = new PopupButton (window, "Popup", ENTYPO_ICON_EXPORT); Popup * popup = popupBtn->popup(); popup->setLayout (new GroupLayout()); new Label (popup, "Arbitrary widgets can be placed here"); new CheckBox (popup, "A check box"); popupBtn = new PopupButton (popup, "Recursive popup", ENTYPO_ICON_FLASH); popup = popupBtn->popup(); popup->setLayout (new GroupLayout()); new CheckBox (popup, "Another check box"); window = new nanogui::Window (this, "Basic widgets"); window->setPosition (ivec2 (200, 15)); window->setLayout (new GroupLayout()); new Label (window, "Message dialog", "sans-bold"); tools = new Widget (window); tools->setLayout (new BoxLayout (Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new Button (tools, "Info"); b->setCallback ([&] { auto dlg = new MessageDialog (this, MessageDialog::Type::Information, "Title", "This is an information message"); dlg->setCallback ([] (int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button (tools, "Warn"); b->setCallback ([&] { auto dlg = new MessageDialog (this, MessageDialog::Type::Warning, "Title", "This is a warning message"); dlg->setCallback ([] (int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button (tools, "Ask"); b->setCallback ([&] { auto dlg = new MessageDialog (this, MessageDialog::Type::Warning, "Title", "This is a question message", "Yes", "No", true); dlg->setCallback ([] (int result) { cout << "Dialog result: " << result << endl; }); }); std::string iconPath = assetFolder.string(); iconPath += "/icons"; std::vector<std::pair<int, std::string>> icons = NanoUtil::loadImageDirectory (getContext(), iconPath); new Label (window, "Image panel & scroll panel", "sans-bold"); PopupButton * imagePanelBtn = new PopupButton (window, "Image Panel"); imagePanelBtn->setIcon (ENTYPO_ICON_FOLDER); popup = imagePanelBtn->popup(); VScrollPanel * vscroll = new VScrollPanel (popup); ImagePanel * imgPanel = new ImagePanel (vscroll); imgPanel->setImages (icons); popup->setFixedSize (ivec2 (245, 150)); new Label (window, "Selected image", "sans-bold"); auto img = new ImageView (window); img->setFixedSize (ivec2 (40, 40)); img->setImage (icons[0].first); imgPanel->setCallback ([ &, img, imgPanel, imagePanelBtn] (int i) { img->setImage (imgPanel->images()[i].first); cout << "Selected item " << i << endl; }); new Label (window, "Combo box", "sans-bold"); new ComboBox (window, { "Combo box item 1", "Combo box item 2", "Combo box item 3" }); new Label (window, "Check box", "sans-bold"); CheckBox * cb = new CheckBox (window, "Flag 1", [] (bool state) { cout << "Check box 1 state: " << state << endl; } ); cb->setChecked (true); cb = new CheckBox (window, "Flag 2", [] (bool state) { cout << "Check box 2 state: " << state << endl; } ); new Label (window, "Progress bar", "sans-bold"); mProgress = new ProgressBar (window); new Label (window, "Slider and text box", "sans-bold"); Widget * panel = new Widget (window); panel->setLayout (new BoxLayout (Orientation::Horizontal, Alignment::Middle, 0, 20)); Slider * slider = new Slider (panel); slider->setValue (0.5f); slider->setFixedWidth (80); TxtBox * textBox = new TxtBox (panel); textBox->setFixedSize (ivec2 (60, 25)); textBox->setValue ("50"); textBox->setUnits ("%"); slider->setCallback ([textBox] (float value) { textBox->setValue (std::to_string ((int) (value * 100))); }); slider->setFinalCallback ([&] (float value) { cout << "Final slider value: " << (int) (value * 100) << endl; }); textBox->setFixedSize (ivec2 (60, 25)); textBox->setFontSize (20); textBox->setAlignment (TxtBox::Alignment::Right); window = new nanogui::Window(this, "Misc. widgets"); window->setPosition(ivec2(425, 15)); window->setLayout(new GroupLayout()); new Label(window, "Color wheel", "sans-bold"); new ColorWheel(window); new Label(window, "Function graph", "sans-bold"); Graph *graph = new Graph(window, "Some function"); graph->setHeader("E = 2.35e-3"); graph->setFooter("Iteration 89"); std::vector<float> &func = graph->values(); func.resize(100); for (int i = 0; i < 100; ++i) func[i] = 0.5f * (0.5f * std::sin(i / 10.f) + 0.5f * std::cos(i / 23.f) + 1); window = new nanogui::Window(this, "Grid of small widgets"); window->setPosition(ivec2(425, 288)); GridLayout *layout = new GridLayout(Orientation::Horizontal, 2, Alignment::Middle, 15, 5); layout->setColAlignment( { Alignment::Maximum, Alignment::Fill }); layout->setSpacing(0, 10); window->setLayout(layout); { new Label(window, "Floating point :", "sans-bold"); textBox = new TxtBox(window); textBox->setEditable(true); textBox->setFixedSize(ivec2(100, 20)); textBox->setValue("50"); textBox->setUnits("GiB"); textBox->setDefaultValue("0.0"); textBox->setFontSize(16); textBox->setFormat("[-]?[0-9]*\\.?[0-9]+"); } { new Label(window, "Positive integer :", "sans-bold"); textBox = new TxtBox(window); textBox->setEditable(true); textBox->setFixedSize(ivec2(100, 20)); textBox->setValue("50"); textBox->setUnits("Mhz"); textBox->setDefaultValue("0.0"); textBox->setFontSize(16); textBox->setFormat("[1-9][0-9]*"); } { new Label(window, "Checkbox :", "sans-bold"); cb = new CheckBox(window, "Check me"); cb->setFontSize(16); cb->setChecked(true); } new Label(window, "Combo box :", "sans-bold"); ComboBox *cobo = new ComboBox(window, { "Item 1", "Item 2", "Item 3" }); cobo->setFontSize(16); cobo->setFixedSize(ivec2(100, 20)); new Label(window, "Color button :", "sans-bold"); popupBtn = new PopupButton(window, "", 0); popupBtn->setBackgroundColor(Colour(255, 120, 0, 255)); popupBtn->setFontSize(16); popupBtn->setFixedSize(ivec2(100, 20)); popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); ColorWheel *colorwheel = new ColorWheel(popup); colorwheel->setColor(popupBtn->backgroundColor()); Button *colorBtn = new Button(popup, "Pick"); colorBtn->setFixedSize(ivec2(100, 25)); Colour c = colorwheel->color(); colorBtn->setBackgroundColor(c); colorwheel->setCallback([colorBtn](const Colour &value) { colorBtn->setBackgroundColor(value); }); colorBtn->setChangeCallback([colorBtn, popupBtn](bool pushed) { if (pushed) { popupBtn->setBackgroundColor(colorBtn->backgroundColor()); popupBtn->setPushed(false); } }); performLayout (mNVGContext); } catch (const std::exception & e) { std::cout << e.what() << std::endl; } }
ExampleApplication() : nanogui::Screen(Eigen::Vector2i(800, 600), "NanoGUI Test") { using namespace nanogui; Window *window = new Window(this, "Button demo"); window->setPosition(Vector2i(15, 15)); window->setLayout(new GroupLayout()); /* No need to store a pointer, the data structure will be automatically freed when the parent window is deleted */ new Label(window, "Push buttons", "sans-bold"); Button *b = new Button(window, "Plain button"); b->setCallback([] { cout << "pushed!" << endl; }); b = new Button(window, "Styled", ENTYPO_ICON_ROCKET); b->setBackgroundColor(Color(0, 0, 255, 255)); b->setCallback([] { cout << "pushed!" << endl; }); new Label(window, "Toggle buttons", "sans-bold"); b = new Button(window, "Toggle me"); b->setButtonFlags(Button::ToggleButton); b->setChangeCallback([](bool state) { cout << "Toggle button state: " << state << endl; }); new Label(window, "Radio buttons", "sans-bold"); b = new Button(window, "Radio button 1"); b->setButtonFlags(Button::RadioButton); b = new Button(window, "Radio button 2"); b->setButtonFlags(Button::RadioButton); new Label(window, "A tool palette", "sans-bold"); Widget *tools = new Widget(window); tools->setLayout(new BoxLayout(BoxLayout::Horizontal, BoxLayout::Middle, 0, 6)); b = new ToolButton(tools, ENTYPO_ICON_CLOUD); b = new ToolButton(tools, ENTYPO_ICON_FF); b = new ToolButton(tools, ENTYPO_ICON_COMPASS); b = new ToolButton(tools, ENTYPO_ICON_INSTALL); new Label(window, "Popup buttons", "sans-bold"); PopupButton *popupBtn = new PopupButton(window, "Popup", ENTYPO_ICON_EXPORT); Popup *popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); new Label(popup, "Arbitrary widgets can be placed here"); new CheckBox(popup, "A check box"); popupBtn = new PopupButton(popup, "Recursive popup", ENTYPO_ICON_FLASH); popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); new CheckBox(popup, "Another check box"); window = new Window(this, "Basic widgets"); window->setPosition(Vector2i(200, 15)); window->setLayout(new GroupLayout()); new Label(window, "Message dialog", "sans-bold"); tools = new Widget(window); tools->setLayout(new BoxLayout(BoxLayout::Horizontal, BoxLayout::Middle, 0, 6)); b = new Button(tools, "Info"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Information, "Title", "This is an information message"); dlg->setCallback([](int result) { }); }); b = new Button(tools, "Warn"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Warning, "Title", "This is a warning message"); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button(tools, "Ask"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Warning, "Title", "This is a question message", "Yes", "No", true); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); std::vector<std::pair<int, std::string>> icons = loadImageDirectory(mNVGContext, "icons"); new Label(window, "Image panel & scroll panel", "sans-bold"); PopupButton *imagePanelBtn = new PopupButton(window, "Image Panel"); imagePanelBtn->setIcon(ENTYPO_ICON_FOLDER); popup = imagePanelBtn->popup(); VScrollPanel *vscroll = new VScrollPanel(popup); ImagePanel *imgPanel = new ImagePanel(vscroll); imgPanel->setImageData(icons); popup->setFixedSize(Vector2i(245, 150)); new Label(window, "Selected image", "sans-bold"); auto img = new ImageView(window); img->setFixedSize(Vector2i(40, 40)); new Label(window, "File dialog", "sans-bold"); tools = new Widget(window); tools->setLayout(new BoxLayout(BoxLayout::Horizontal, BoxLayout::Middle, 0, 6)); b = new Button(tools, "Open"); b->setCallback([&] { cout << "File dialog result: " << file_dialog( { {"png", "Portable Network Graphics"}, {"txt", "Text file"} }, false) << endl; }); b = new Button(tools, "Save"); b->setCallback([&] { cout << "File dialog result: " << file_dialog( { {"png", "Portable Network Graphics"}, {"txt", "Text file"} }, true) << endl; }); img->setImage(icons[0].first); imgPanel->setCallback([&, img, imgPanel, imagePanelBtn](int i) { img->setImage(imgPanel->images()[i].first); cout << "Selected item " << i << endl; }); new Label(window, "Combo box", "sans-bold"); new ComboBox(window, { "Combo box item 1", "Combo box item 2", "Combo box item 3"}); new Label(window, "Check box", "sans-bold"); CheckBox *cb = new CheckBox(window, "Flag 1", [](bool state) { cout << "Check box 1 state: " << state << endl; } ); cb->setChecked(true); new CheckBox(window, "Flag 2", [](bool state) { cout << "Check box 2 state: " << state << endl; } ); new Label(window, "Progress bar", "sans-bold"); mProgress = new ProgressBar(window); new Label(window, "Slider and text box", "sans-bold"); Widget *panel = new Widget(window); panel->setLayout( new BoxLayout(BoxLayout::Horizontal, BoxLayout::Middle, 0, 20)); Slider *slider = new Slider(panel); slider->setValue(0.5f); slider->setFixedWidth(80); TextBox *textBox = new TextBox(panel); textBox->setFixedSize(Vector2i(60, 25)); textBox->setValue("50"); textBox->setUnits("%"); slider->setCallback([textBox](float value) { textBox->setValue(std::to_string((int) (value * 100))); }); slider->setFinalCallback([&](float value) { cout << "Final slider value: " << (int) (value * 100) << endl; }); window = new Window(this, "Misc. widgets"); window->setPosition(Vector2i(425, 15)); window->setLayout(new GroupLayout()); new Label(window, "Color wheel", "sans-bold"); new ColorWheel(window); new Label(window, "Function graph", "sans-bold"); Graph *graph = new Graph(window, "Some function"); graph->setHeader("E = 2.35e-3"); graph->setFooter("Iteration 89"); Eigen::VectorXf &func = graph->values(); func.resize(100); for (int i=0; i<100; ++i) func[i] = 0.5f * (0.5f * std::sin(i/10.f)+0.5f*std::cos(i/23.f)+1); performLayout(mNVGContext); /* All NanoGUI widgets are initialized at this point. Now create an OpenGL shader to draw the main window contents. NanoGUI comes with a simple Eigen-based wrapper around OpenGL 3, which eliminates most of the tedious and error-prone shader and buffer object management. */ mShader.init( /* An identifying name */ "a_simple_shader", /* Vertex shader */ "#version 330\n" "uniform mat4 modelViewProj;\n" "in vec3 position;\n" "void main() {\n" " gl_Position = modelViewProj * vec4(position, 1.0);\n" "}", /* Fragment shader */ "#version 330\n" "out vec4 color;\n" "uniform float intensity;\n" "void main() {\n" " color = vec4(vec3(intensity), 1.0);\n" "}" ); MatrixXu indices(3, 2); /* Draw 2 triangles */ indices.col(0) << 0, 1, 2; indices.col(1) << 2, 3, 0; MatrixXf positions(3, 4); positions.col(0) << -1, -1, 0; positions.col(1) << 1, -1, 0; positions.col(2) << 1, 1, 0; positions.col(3) << -1, 1, 0; mShader.bind(); mShader.uploadIndices(indices); mShader.uploadAttrib("position", positions); mShader.setUniform("intensity", 0.5f); }
void View::create (WindowRef & ciWindow) { try { initGraph (&fps, GRAPH_RENDER_FPS, "Frame Time"); initGraph (&cpuGraph, GRAPH_RENDER_MS, "CPU Time"); setSize (ciWindow->getSize()); nanogui::Window * window = new nanogui::Window (this, "Button demo"); window->setPosition (ivec2 (15, 15)); window->setLayout (new GroupLayout()); /* No need to store a pointer, the data structure will be automatically freed when the parent window is deleted */ new Label (window, "Push buttons", "sans-bold"); Button * b = new Button (window, "Plain button"); b->setCallback ([] { cout << "pushed!" << endl; }); b = new Button (window, "Styled", ENTYPO_ICON_ROCKET); b->setBackgroundColor (Colour (0, 0, 255, 25)); b->setCallback ([] { cout << "pushed!" << endl; }); new Label (window, "Toggle buttons", "sans-bold"); b = new Button (window, "Toggle me"); b->setFlags (Button::ToggleButton); b->setChangeCallback ([] (bool state) { cout << "Toggle button state: " << state << endl; }); new Label (window, "Radio buttons", "sans-bold"); b = new Button (window, "Radio button 1"); b->setFlags (Button::RadioButton); b = new Button (window, "Radio button 2"); b->setFlags (Button::RadioButton); new Label (window, "A tool palette", "sans-bold"); Widget * tools = new Widget (window); tools->setLayout (new BoxLayout (Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new ToolButton (tools, ENTYPO_ICON_CLOUD); b = new ToolButton (tools, ENTYPO_ICON_FF); b = new ToolButton (tools, ENTYPO_ICON_COMPASS); b = new ToolButton (tools, ENTYPO_ICON_INSTALL); new Label (window, "Popup buttons", "sans-bold"); PopupButton * popupBtn = new PopupButton (window, "Popup", ENTYPO_ICON_EXPORT); Popup * popup = popupBtn->popup(); popup->setLayout (new GroupLayout()); new Label (popup, "Arbitrary widgets can be placed here"); new CheckBox (popup, "A check box"); popupBtn = new PopupButton (popup, "Recursive popup", ENTYPO_ICON_FLASH); popup = popupBtn->popup(); popup->setLayout (new GroupLayout()); new CheckBox (popup, "Another check box"); window = new nanogui::Window (this, "Basic widgets"); window->setPosition (ivec2 (200, 15)); window->setLayout (new GroupLayout()); new Label (window, "Message dialog", "sans-bold"); tools = new Widget (window); tools->setLayout (new BoxLayout (Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new Button (tools, "Info"); b->setCallback ([&] { auto dlg = new MessageDialog (this, MessageDialog::Type::Information, "Title", "This is an information message"); dlg->setCallback ([] (int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button (tools, "Warn"); b->setCallback ([&] { auto dlg = new MessageDialog (this, MessageDialog::Type::Warning, "Title", "This is a warning message"); dlg->setCallback ([] (int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button (tools, "Ask"); b->setCallback ([&] { auto dlg = new MessageDialog (this, MessageDialog::Type::Warning, "Title", "This is a question message", "Yes", "No", true); dlg->setCallback ([] (int result) { cout << "Dialog result: " << result << endl; }); }); std::string iconPath ("E:/Code4/nanofish/projects/qdemos/cinder/ciNanogui/assets/icons"); std::vector<std::pair<int, std::string>> icons = NanoUtil::loadImageDirectory (getContext(), iconPath); new Label (window, "Image panel & scroll panel", "sans-bold"); PopupButton * imagePanelBtn = new PopupButton (window, "Image Panel"); imagePanelBtn->setIcon (ENTYPO_ICON_FOLDER); popup = imagePanelBtn->popup(); VScrollPanel * vscroll = new VScrollPanel (popup); ImagePanel * imgPanel = new ImagePanel (vscroll); imgPanel->setImages (icons); popup->setFixedSize (ivec2 (245, 150)); new Label (window, "Selected image", "sans-bold"); auto img = new ImageView (window); img->setFixedSize (ivec2 (40, 40)); img->setImage (icons[0].first); imgPanel->setCallback ([ &, img, imgPanel, imagePanelBtn] (int i) { img->setImage (imgPanel->images()[i].first); cout << "Selected item " << i << endl; }); new Label (window, "Combo box", "sans-bold"); new ComboBox (window, { "Combo box item 1", "Combo box item 2", "Combo box item 3" }); new Label (window, "Check box", "sans-bold"); CheckBox * cb = new CheckBox (window, "Flag 1", [] (bool state) { cout << "Check box 1 state: " << state << endl; } ); cb->setChecked (true); cb = new CheckBox (window, "Flag 2", [] (bool state) { cout << "Check box 2 state: " << state << endl; } ); new Label (window, "Progress bar", "sans-bold"); mProgress = new ProgressBar (window); performLayout (mNVGContext); } catch (const std::exception & e) { std::cout << e.what() << std::endl; } }
void Gui::mousePressed( int x, int y, MouseButtons mb ) { cout << "Ui::Gui::mousePressed( " << x << ", " << y << " )" << endl; if ( pChannelPopup != NULL ) { pChannelPopup->mousePressed( x, y, mb ); if ( pChannelPopup != NULL ) { if ( !pChannelPopup->passEvents() ) return; } } if ( pMouseChannelWidget != NULL ) { if ( pMousePressedWidget != NULL ) { pMousePressedWidget->onDestroy.disconnect( this ); } pMousePressedWidget = pMouseChannelWidget; pMousePressedWidget->onDestroy.connect( this, &Gui::objectDestroyed ); pMouseChannelWidget->mousePressed( x - pMouseChannelWidget->absoluteXPos(), y - pMouseChannelWidget->absoluteYPos(), mb ); } else { for ( int i = 0; i < pPopups.count(); i++ ) { Popup* p = pPopups.get( i ); Rect r = p->getRect(); if ( r.pointInside( x, y ) ) { p->mousePressed( x, y, mb ); if ( !p->passEvents() ) return; } } Widget* o = fgFrame().mousePressed( x, y, mb); setFocusedWidget( o ); if ( ( o != NULL ) ) { if ( pMousePressedWidget != NULL ) { pMousePressedWidget->onDestroy.disconnect( this ); } if ( pMouseDragWidget != NULL ) { pMouseDragWidget->onDestroy.disconnect( this ); } pMousePressedWidget = o; pMousePressedWidget->onDestroy.connect( this, &Gui::objectDestroyed ); pMouseDragWidget = o; pMouseDragWidget->onDestroy.connect( this, &Gui::objectDestroyed ); } else { if ( pMouseDragWidget != NULL ) { pMouseDragWidget->onDestroy.disconnect( this ); pMouseDragWidget = NULL; } } } pPressedX = x; pPressedY = y; }
void Gui::mouseMove( int x, int y, MouseButtons mb ) { // cout << "Ui::Gui::mouseMove( )" << endl; if ( pChannelPopup != NULL ) { pChannelPopup->mouseMove( x, y, mb ); if ( pChannelPopup != NULL ) { if ( !pChannelPopup->passEvents() ) return; } } if ( pMouseChannelWidget != NULL ) { Widget* o = pMouseChannelWidget->mouseMove( x - pMouseChannelWidget->absoluteXPos(), y - pMouseChannelWidget->absoluteYPos(), mb ); if ( pLastMouseOver != o ) { if ( (pLastMouseOver == pMouseChannelWidget) || (o == pMouseChannelWidget) ) { if ( o == pMouseChannelWidget ) { pMouseChannelWidget->mouseIn( mb ); } else { pMouseChannelWidget->mouseOut( mb ); pMouseChannelWidget->onDestroy.disconnect( this ); } } else { pLastMouseOver->onDestroy.disconnect( this ); } pLastMouseOver = o; pLastMouseOver->onDestroy.connect( this, &Gui::objectDestroyed ); } } else { for( int i = 0; i < pPopups.count(); i++ ) { Popup* p = pPopups.get( i ); Rect r = p->getRect(); if ( r.pointInside( x, y ) ) { p->mouseMove( x, y, mb ); if ( !p->passEvents() ) return; } } Widget* o = fgFrame().mouseMove( x, y, mb ); if ( o != NULL ) { if ( o != pLastMouseOver ) { if ( pLastMouseOver != NULL ) { pLastMouseOver->onDestroy.disconnect( this ); pLastMouseOver->mouseOut( mb ); pLastMouseOver = NULL; } // if ( o != &frame ) o->mouseIn( mb ); pLastMouseOver = o; pLastMouseOver->onDestroy.connect( this, &Gui::objectDestroyed ); } //o->mouseMove( x, y, mb ); } else { pLastMouseOver->onDestroy.disconnect( this ); pLastMouseOver = NULL; } } if ( pMouseDragWidget != NULL ) { if ( ( Utils::max(x, pPressedX) - Utils::min(x, pPressedX) > 5 ) || ( Utils::max(y, pPressedY) - Utils::min(y, pPressedY) > 5 ) ) { DragObject* d = NULL; pMouseDragWidget->onDrag( pMouseDragWidget, x - pMouseDragWidget->absoluteXPos(), y - pMouseDragWidget->absoluteYPos(), &d ); if ( d != NULL ) { pMouseDragWidget->mouseReleased( x - pMouseDragWidget->absoluteXPos(), y - pMouseDragWidget->absoluteYPos(), mb ); d->popup( x - (d->width() / 2), y - (d->height() / 2), *this ); if ( mouseChannelWidget() != NULL ) unsetMouseChannelWidget( *mouseChannelWidget() ); d->mousePressed( x, y, mb ); } pMouseDragWidget->onDestroy.disconnect( this ); pMouseDragWidget = NULL; } } }
void Gui::render( ) { List<UpdateWidget*>& l = Widget::updatedWidgets(); if ( l.count() <= 0 ) return; int i = l.count() - 1; Widget* o = NULL; List<Widget*> tmpL; l.sort( &objectsListSortCallback ); while( i >= 0 ){ o = l.get( i )->o; Rect r = l.get( i )->r; if ( pFgFrame != NULL ) fgFrame().getWidgetsInRect( tmpL, r ); if ( pTopFrame != NULL ) topFrame().getWidgetsInRect( tmpL, r ); tmpL.sort( &objectListSortCallback ); int i2 = 0; bool skip = false; // int before = tmpL.count(); while ( (i2 < tmpL.count()) && ( o != NULL ) ) { Widget* o2 = tmpL.get( i2 ); Rect r2( o2->absoluteXPos(), o2->absoluteYPos(), o2->width(), o2->height() ); if ( o2->border() != NULL ) { if ( o2->border()->drawmode() != DM_OPAQUE ) r2.applyBorder( o2->border() ); } if ( (r2.encloses( r )) && ( o2->drawmode() == DM_OPAQUE ) && ( o2->visible() ) ) { if ( o2->zIndex() > o->zIndex() ) { skip = true; break; } else { while ( i2+1 < tmpL.count() ) { tmpL.remove( i2+1 ); } break; } } i2++; } if ( !skip ) { for( int i2 = tmpL.count() - 1; i2 >= 0; i2-- ){ Rect r2 = Rect( tmpL.get( i2 )->absoluteXPos(), tmpL.get( i2 )->absoluteYPos(), tmpL.get( i2 )->width(), tmpL.get( i2 )->height() ); r2.crop( r ); screen().pushClipRect( r2 ); screen().pushClipRect( tmpL.get( i2 )->getClipRect() ); screen().setRelativePoint( tmpL.get( i2 )->absoluteXPos(), tmpL.get( i2 )->absoluteYPos() ); tmpL.get( i2 )->renderBorder( screen() ); screen().setRelativePoint( tmpL.get( i2 )->absoluteXPos() + tmpL.get( i2 )->borderLeft(), tmpL.get( i2 )->absoluteYPos() + tmpL.get( i2 )->borderTop() ); Rect r3( r2.left - tmpL.get( i2 )->absoluteXPos() - tmpL.get( i2 )->borderLeft(), r2.top - tmpL.get( i2 )->absoluteYPos() - tmpL.get( i2 )->borderTop(), r2.width, r2.height ); tmpL.get( i2 )->render( screen(), r3 ); screen().popClipRect(); screen().popClipRect(); screen().setRelativePoint( 0, 0 ); } for( int i = 0; i < pPopups.count(); i++ ) { Popup* p = pPopups.get( i ); r.crop( p->getRect() ); if ( r.area() > 0 ) { Rect r = p->getRect(); r.crop( r ); p->render( r ); } } } else { } tmpL.clear(); i--; } Widget::clearUpdatedWidgets(); }
void Setup_Video::apply() { const std::string mode = mModeListModel->getElementAt(mModeList->getSelected()); if (mode.find("x") != std::string::npos) { const int width = atoi(mode.substr(0, mode.find("x")).c_str()); const int height = atoi(mode.substr(mode.find("x") + 1).c_str()); gui->resize(width, height); } // Full screen changes bool fullscreen = mFsCheckBox->isSelected(); if (fullscreen != (config.getValue("screen", false) == 1)) { /* The OpenGL test is only necessary on Windows, since switching * to/from full screen works fine on Linux. On Windows we'd have to * reinitialize the OpenGL state and reload all textures. * * See http://libsdl.org/cgi/docwiki.cgi/SDL_SetVideoMode */ #ifdef WIN32 // checks for opengl usage if (!(config.getValue("opengl", false) == 1)) { #endif if (!graphics->setFullscreen(fullscreen)) { fullscreen = !fullscreen; if (!graphics->setFullscreen(fullscreen)) { std::stringstream error; error << _("Failed to switch to ") << (fullscreen ? _("windowed") : _("fullscreen")) << _("mode and restoration of old mode also failed!") << std::endl; logger->error(error.str()); } } #ifdef WIN32 } else { new OkDialog(_("Switching to full screen"), _("Restart needed for changes to take effect.")); } #endif config.setValue("screen", fullscreen ? true : false); } // OpenGL change if (mOpenGLCheckBox->isSelected() != mOpenGLEnabled) { config.setValue("opengl", mOpenGLCheckBox->isSelected() ? true : false); // OpenGL can currently only be changed by restarting, notify user. new OkDialog(_("Changing OpenGL"), _("Applying change to OpenGL requires restart.")); } if ((int) mFontSizeSlider->getValue() != mFontSize) { const int val = (int) mFontSizeSlider->getValue(); gui->changeFontSize(val); Widgets widgets = windowContainer->getWidgetList(); WidgetIterator iter; for (iter = widgets.begin(); iter != widgets.end(); ++iter) { Popup* popup = dynamic_cast<Popup*>(*iter); if (popup) { popup->adaptToNewSize(); continue; } } if (state != GAME_STATE && desktop) desktop->resize(); config.setValue("fontSize", val); } mFps = (mFpsCheckBox->isSelected()) ? (int) mFpsSlider->getValue() : 0; mFpsSlider->setEnabled(mFps > 0); // FPS change config.setValue("fpslimit", mFps); // We sync old and new values at apply time mFullScreenEnabled = config.getValue("screen", false); mOpenGLEnabled = config.getValue("opengl", false); mCustomCursorEnabled = config.getValue("customcursor", true); mNameEnabled = config.getValue("showownname", false); mPickupChatEnabled = config.getValue("showpickupchat", true); mPickupParticleEnabled = config.getValue("showpickupparticle", false); mSpeechMode = (int) config.getValue("speech", 3); mOpacity = config.getValue("guialpha", 0.8); mMouseOpacity = config.getValue("mousealpha", 0.7); mScreenWidth = (int) config.getValue("screenwidth", 800); mScreenHeight = (int) config.getValue("screenheight", 600); mFontSize = (int) config.getValue("fontSize", 11); mOverlayDetail = (int) config.getValue("OverlayDetail", 2); mParticleDetail = 3 - (int) config.getValue("particleEmitterSkip", 1); config.setValue("particleeffects", mParticleDetail != -1); }
ExampleApplication() : nanogui::Screen(Eigen::Vector2i(1024, 768), "NanoGUI Test") { using namespace nanogui; Window *window = new Window(this, "Button demo"); window->setPosition(Vector2i(15, 15)); window->setLayout(new GroupLayout()); /* No need to store a pointer, the data structure will be automatically freed when the parent window is deleted */ new Label(window, "Push buttons", "sans-bold"); Button *b = new Button(window, "Plain button"); b->setCallback([] { cout << "pushed!" << endl; }); /* Alternative construction notation using variadic template */ b = window->add<Button>("Styled", ENTYPO_ICON_ROCKET); b->setBackgroundColor(Color(0, 0, 255, 25)); b->setCallback([] { cout << "pushed!" << endl; }); new Label(window, "Toggle buttons", "sans-bold"); b = new Button(window, "Toggle me"); b->setFlags(Button::ToggleButton); b->setChangeCallback([](bool state) { cout << "Toggle button state: " << state << endl; }); new Label(window, "Radio buttons", "sans-bold"); b = new Button(window, "Radio button 1"); b->setFlags(Button::RadioButton); b = new Button(window, "Radio button 2"); b->setFlags(Button::RadioButton); new Label(window, "A tool palette", "sans-bold"); Widget *tools = new Widget(window); tools->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new ToolButton(tools, ENTYPO_ICON_CLOUD); b = new ToolButton(tools, ENTYPO_ICON_FF); b = new ToolButton(tools, ENTYPO_ICON_COMPASS); b = new ToolButton(tools, ENTYPO_ICON_INSTALL); new Label(window, "Popup buttons", "sans-bold"); PopupButton *popupBtn = new PopupButton(window, "Popup", ENTYPO_ICON_EXPORT); Popup *popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); new Label(popup, "Arbitrary widgets can be placed here"); new CheckBox(popup, "A check box"); popupBtn = new PopupButton(popup, "Recursive popup", ENTYPO_ICON_FLASH); popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); new CheckBox(popup, "Another check box"); window = new Window(this, "Basic widgets"); window->setPosition(Vector2i(200, 15)); window->setLayout(new GroupLayout()); new Label(window, "Message dialog", "sans-bold"); tools = new Widget(window); tools->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new Button(tools, "Info"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Type::Information, "Title", "This is an information message"); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button(tools, "Warn"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Type::Warning, "Title", "This is a warning message"); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button(tools, "Ask"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Type::Warning, "Title", "This is a question message", "Yes", "No", true); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); std::vector<std::pair<int, std::string>> icons = loadImageDirectory(mNVGContext, "icons"); new Label(window, "Image panel & scroll panel", "sans-bold"); PopupButton *imagePanelBtn = new PopupButton(window, "Image Panel"); imagePanelBtn->setIcon(ENTYPO_ICON_FOLDER); popup = imagePanelBtn->popup(); VScrollPanel *vscroll = new VScrollPanel(popup); ImagePanel *imgPanel = new ImagePanel(vscroll); imgPanel->setImages(icons); popup->setFixedSize(Vector2i(245, 150)); auto img_window = new Window(this, "Selected image"); img_window->setPosition(Vector2i(710, 15)); img_window->setLayout(new GroupLayout()); auto img = new ImageView(img_window); img->setPolicy(ImageView::SizePolicy::Expand); img->setFixedSize(Vector2i(275, 275)); img->setImage(icons[0].first); imgPanel->setCallback([&, img, imgPanel, imagePanelBtn](int i) { img->setImage(imgPanel->images()[i].first); cout << "Selected item " << i << endl; }); auto img_cb = new CheckBox(img_window, "Expand", [img](bool state) { if (state) img->setPolicy(ImageView::SizePolicy::Expand); else img->setPolicy(ImageView::SizePolicy::Fixed); }); img_cb->setChecked(true); new Label(window, "File dialog", "sans-bold"); tools = new Widget(window); tools->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new Button(tools, "Open"); b->setCallback([&] { cout << "File dialog result: " << file_dialog( { {"png", "Portable Network Graphics"}, {"txt", "Text file"} }, false) << endl; }); b = new Button(tools, "Save"); b->setCallback([&] { cout << "File dialog result: " << file_dialog( { {"png", "Portable Network Graphics"}, {"txt", "Text file"} }, true) << endl; }); new Label(window, "Combo box", "sans-bold"); new ComboBox(window, { "Combo box item 1", "Combo box item 2", "Combo box item 3"}); new Label(window, "Check box", "sans-bold"); CheckBox *cb = new CheckBox(window, "Flag 1", [](bool state) { cout << "Check box 1 state: " << state << endl; } ); cb->setChecked(true); cb = new CheckBox(window, "Flag 2", [](bool state) { cout << "Check box 2 state: " << state << endl; } ); new Label(window, "Progress bar", "sans-bold"); mProgress = new ProgressBar(window); new Label(window, "Slider and text box", "sans-bold"); Widget *panel = new Widget(window); panel->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 20)); Slider *slider = new Slider(panel); slider->setValue(0.5f); slider->setFixedWidth(80); TextBox *textBox = new TextBox(panel); textBox->setFixedSize(Vector2i(60, 25)); textBox->setValue("50"); textBox->setUnits("%"); slider->setCallback([textBox](float value) { textBox->setValue(std::to_string((int) (value * 100))); }); slider->setFinalCallback([&](float value) { cout << "Final slider value: " << (int) (value * 100) << endl; }); textBox->setFixedSize(Vector2i(60,25)); textBox->setFontSize(20); textBox->setAlignment(TextBox::Alignment::Right); window = new Window(this, "Misc. widgets"); window->setPosition(Vector2i(425,15)); window->setLayout(new GroupLayout()); TabWidget* tabWidget = window->add<TabWidget>(); Widget* layer = tabWidget->createTab("Color Wheel"); layer->setLayout(new GroupLayout()); // Use overloaded variadic add to fill the tab widget with Different tabs. layer->add<Label>("Color wheel widget", "sans-bold"); layer->add<ColorWheel>(); layer = tabWidget->createTab("Function Graph"); layer->setLayout(new GroupLayout()); layer->add<Label>("Function graph widget", "sans-bold"); Graph *graph = layer->add<Graph>("Some Function"); graph->setHeader("E = 2.35e-3"); graph->setFooter("Iteration 89"); VectorXf &func = graph->values(); func.resize(100); for (int i = 0; i < 100; ++i) func[i] = 0.5f * (0.5f * std::sin(i / 10.f) + 0.5f * std::cos(i / 23.f) + 1); // Dummy tab used to represent the last tab button. tabWidget->createTab("+"); // A simple counter. int counter = 1; tabWidget->setCallback([tabWidget, this, counter] (int index) mutable { if (index == (tabWidget->tabCount()-1)) { // When the "+" tab has been clicked, simply add a new tab. string tabName = "Dynamic " + to_string(counter); Widget* layerDyn = tabWidget->createTab(index, tabName); layerDyn->setLayout(new GroupLayout()); layerDyn->add<Label>("Function graph widget", "sans-bold"); Graph *graphDyn = layerDyn->add<Graph>("Dynamic function"); graphDyn->setHeader("E = 2.35e-3"); graphDyn->setFooter("Iteration " + to_string(index*counter)); VectorXf &funcDyn = graphDyn->values(); funcDyn.resize(100); for (int i = 0; i < 100; ++i) funcDyn[i] = 0.5f * std::abs((0.5f * std::sin(i / 10.f + counter) + 0.5f * std::cos(i / 23.f + 1 + counter))); ++counter; // We must invoke perform layout from the screen instance to keep everything in order. // This is essential when creating tabs dynamically. performLayout(); // Ensure that the newly added header is visible on screen tabWidget->ensureTabVisible(index); } }); tabWidget->setActiveTab(0); // A button to go back to the first tab and scroll the window. panel = window->add<Widget>(); panel->add<Label>("Jump to tab: "); panel->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 6)); auto ib = panel->add<IntBox<int>>(); ib->setEditable(true); b = panel->add<Button>("", ENTYPO_ICON_FORWARD); b->setFixedSize(Vector2i(22, 22)); ib->setFixedHeight(22); b->setCallback([tabWidget, ib] { int value = ib->value(); if (value >= 0 && value < tabWidget->tabCount()) { tabWidget->setActiveTab(value); tabWidget->ensureTabVisible(value); } }); window = new Window(this, "Grid of small widgets"); window->setPosition(Vector2i(425, 300)); GridLayout *layout = new GridLayout(Orientation::Horizontal, 2, Alignment::Middle, 15, 5); layout->setColAlignment( { Alignment::Maximum, Alignment::Fill }); layout->setSpacing(0, 10); window->setLayout(layout); { new Label(window, "Floating point :", "sans-bold"); textBox = new TextBox(window); textBox->setEditable(true); textBox->setFixedSize(Vector2i(100, 20)); textBox->setValue("50"); textBox->setUnits("GiB"); textBox->setDefaultValue("0.0"); textBox->setFontSize(16); textBox->setFormat("[-]?[0-9]*\\.?[0-9]+"); } { new Label(window, "Positive integer :", "sans-bold"); auto intBox = new IntBox<int>(window); intBox->setEditable(true); intBox->setFixedSize(Vector2i(100, 20)); intBox->setValue(50); intBox->setUnits("Mhz"); intBox->setDefaultValue("0"); intBox->setFontSize(16); intBox->setFormat("[1-9][0-9]*"); intBox->setSpinnable(true); intBox->setMinValue(1); intBox->setValueIncrement(2); } { new Label(window, "Checkbox :", "sans-bold"); cb = new CheckBox(window, "Check me"); cb->setFontSize(16); cb->setChecked(true); } new Label(window, "Combo box :", "sans-bold"); ComboBox *cobo = new ComboBox(window, { "Item 1", "Item 2", "Item 3" }); cobo->setFontSize(16); cobo->setFixedSize(Vector2i(100,20)); new Label(window, "Color button :", "sans-bold"); popupBtn = new PopupButton(window, "", 0); popupBtn->setBackgroundColor(Color(255, 120, 0, 255)); popupBtn->setFontSize(16); popupBtn->setFixedSize(Vector2i(100, 20)); popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); ColorWheel *colorwheel = new ColorWheel(popup); colorwheel->setColor(popupBtn->backgroundColor()); Button *colorBtn = new Button(popup, "Pick"); colorBtn->setFixedSize(Vector2i(100, 25)); Color c = colorwheel->color(); colorBtn->setBackgroundColor(c); colorwheel->setCallback([colorBtn](const Color &value) { colorBtn->setBackgroundColor(value); }); colorBtn->setChangeCallback([colorBtn, popupBtn](bool pushed) { if (pushed) { popupBtn->setBackgroundColor(colorBtn->backgroundColor()); popupBtn->setPushed(false); } }); performLayout(); /* All NanoGUI widgets are initialized at this point. Now create an OpenGL shader to draw the main window contents. NanoGUI comes with a simple Eigen-based wrapper around OpenGL 3, which eliminates most of the tedious and error-prone shader and buffer object management. */ mShader.init( /* An identifying name */ "a_simple_shader", /* Vertex shader */ "#version 330\n" "uniform mat4 modelViewProj;\n" "in vec3 position;\n" "void main() {\n" " gl_Position = modelViewProj * vec4(position, 1.0);\n" "}", /* Fragment shader */ "#version 330\n" "out vec4 color;\n" "uniform float intensity;\n" "void main() {\n" " color = vec4(vec3(intensity), 1.0);\n" "}" ); MatrixXu indices(3, 2); /* Draw 2 triangles */ indices.col(0) << 0, 1, 2; indices.col(1) << 2, 3, 0; MatrixXf positions(3, 4); positions.col(0) << -1, -1, 0; positions.col(1) << 1, -1, 0; positions.col(2) << 1, 1, 0; positions.col(3) << -1, 1, 0; mShader.bind(); mShader.uploadIndices(indices); mShader.uploadAttrib("position", positions); mShader.setUniform("intensity", 0.5f); }
ExampleApplication() : nanogui::Screen(Eigen::Vector2i(1024, 768), "NanoGUI Test") { using namespace nanogui; Window *window = new Window(this, "Button demo"); window->setPosition(Vector2i(15, 15)); window->setLayout(new GroupLayout()); /* No need to store a pointer, the data structure will be automatically freed when the parent window is deleted */ new Label(window, "Push buttons", "sans-bold"); Button *b = new Button(window, "Plain button"); b->setCallback([] { cout << "pushed!" << endl; }); b = new Button(window, "Styled", ENTYPO_ICON_ROCKET); b->setBackgroundColor(Color(0, 0, 255, 25)); b->setCallback([] { cout << "pushed!" << endl; }); new Label(window, "Toggle buttons", "sans-bold"); b = new Button(window, "Toggle me"); b->setFlags(Button::ToggleButton); b->setChangeCallback([](bool state) { cout << "Toggle button state: " << state << endl; }); new Label(window, "Radio buttons", "sans-bold"); b = new Button(window, "Radio button 1"); b->setFlags(Button::RadioButton); b = new Button(window, "Radio button 2"); b->setFlags(Button::RadioButton); new Label(window, "A tool palette", "sans-bold"); Widget *tools = new Widget(window); tools->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new ToolButton(tools, ENTYPO_ICON_CLOUD); b = new ToolButton(tools, ENTYPO_ICON_FF); b = new ToolButton(tools, ENTYPO_ICON_COMPASS); b = new ToolButton(tools, ENTYPO_ICON_INSTALL); new Label(window, "Popup buttons", "sans-bold"); PopupButton *popupBtn = new PopupButton(window, "Popup", ENTYPO_ICON_EXPORT); Popup *popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); new Label(popup, "Arbitrary widgets can be placed here"); new CheckBox(popup, "A check box"); popupBtn = new PopupButton(popup, "Recursive popup", ENTYPO_ICON_FLASH); popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); new CheckBox(popup, "Another check box"); window = new Window(this, "Basic widgets"); window->setPosition(Vector2i(200, 15)); window->setLayout(new GroupLayout()); new Label(window, "Message dialog", "sans-bold"); tools = new Widget(window); tools->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new Button(tools, "Info"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Type::Information, "Title", "This is an information message"); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button(tools, "Warn"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Type::Warning, "Title", "This is a warning message"); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); b = new Button(tools, "Ask"); b->setCallback([&] { auto dlg = new MessageDialog(this, MessageDialog::Type::Warning, "Title", "This is a question message", "Yes", "No", true); dlg->setCallback([](int result) { cout << "Dialog result: " << result << endl; }); }); std::vector<std::pair<int, std::string>> icons = loadImageDirectory(mNVGContext, "icons"); new Label(window, "Image panel & scroll panel", "sans-bold"); PopupButton *imagePanelBtn = new PopupButton(window, "Image Panel"); imagePanelBtn->setIcon(ENTYPO_ICON_FOLDER); popup = imagePanelBtn->popup(); VScrollPanel *vscroll = new VScrollPanel(popup); ImagePanel *imgPanel = new ImagePanel(vscroll); imgPanel->setImages(icons); popup->setFixedSize(Vector2i(245, 150)); auto img_window = new Window(this, "Selected image"); img_window->setPosition(Vector2i(675, 15)); img_window->setLayout(new GroupLayout()); auto img = new ImageView(img_window); img->setPolicy(ImageView::SizePolicy::Expand); img->setFixedSize(Vector2i(300, 300)); img->setImage(icons[0].first); imgPanel->setCallback([&, img, imgPanel, imagePanelBtn](int i) { img->setImage(imgPanel->images()[i].first); cout << "Selected item " << i << endl; }); auto img_cb = new CheckBox(img_window, "Expand", [img](bool state) { if (state) img->setPolicy(ImageView::SizePolicy::Expand); else img->setPolicy(ImageView::SizePolicy::Fixed); }); img_cb->setChecked(true); new Label(window, "File dialog", "sans-bold"); tools = new Widget(window); tools->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 6)); b = new Button(tools, "Open"); b->setCallback([&] { cout << "File dialog result: " << file_dialog( { {"png", "Portable Network Graphics"}, {"txt", "Text file"} }, false) << endl; }); b = new Button(tools, "Save"); b->setCallback([&] { cout << "File dialog result: " << file_dialog( { {"png", "Portable Network Graphics"}, {"txt", "Text file"} }, true) << endl; }); new Label(window, "Combo box", "sans-bold"); new ComboBox(window, { "Combo box item 1", "Combo box item 2", "Combo box item 3"}); new Label(window, "Check box", "sans-bold"); CheckBox *cb = new CheckBox(window, "Flag 1", [](bool state) { cout << "Check box 1 state: " << state << endl; } ); cb->setChecked(true); cb = new CheckBox(window, "Flag 2", [](bool state) { cout << "Check box 2 state: " << state << endl; } ); new Label(window, "Progress bar", "sans-bold"); mProgress = new ProgressBar(window); new Label(window, "Slider and text box", "sans-bold"); Widget *panel = new Widget(window); panel->setLayout(new BoxLayout(Orientation::Horizontal, Alignment::Middle, 0, 20)); Slider *slider = new Slider(panel); slider->setValue(0.5f); slider->setFixedWidth(80); TextBox *textBox = new TextBox(panel); textBox->setFixedSize(Vector2i(60, 25)); textBox->setValue("50"); textBox->setUnits("%"); slider->setCallback([textBox](float value) { textBox->setValue(std::to_string((int) (value * 100))); }); slider->setFinalCallback([&](float value) { cout << "Final slider value: " << (int) (value * 100) << endl; }); textBox->setFixedSize(Vector2i(60,25)); textBox->setFontSize(20); textBox->setAlignment(TextBox::Alignment::Right); window = new Window(this,"Misc. widgets"); window->setPosition(Vector2i(425,15)); window->setLayout(new GroupLayout()); new Label(window,"Color wheel","sans-bold"); new ColorWheel(window); new Label(window, "Function graph", "sans-bold"); Graph *graph = new Graph(window, "Some function"); graph->setHeader("E = 2.35e-3"); graph->setFooter("Iteration 89"); VectorXf &func = graph->values(); func.resize(100); for (int i = 0; i < 100; ++i) func[i] = 0.5f * (0.5f * std::sin(i / 10.f) + 0.5f * std::cos(i / 23.f) + 1); window = new Window(this, "Grid of small widgets"); window->setPosition(Vector2i(425, 288)); GridLayout *layout = new GridLayout(Orientation::Horizontal, 2, Alignment::Middle, 15, 5); layout->setColAlignment( { Alignment::Maximum, Alignment::Fill }); layout->setSpacing(0, 10); window->setLayout(layout); { new Label(window, "Floating point :", "sans-bold"); textBox = new TextBox(window); textBox->setEditable(true); textBox->setFixedSize(Vector2i(100, 20)); textBox->setValue("50"); textBox->setUnits("GiB"); textBox->setDefaultValue("0.0"); textBox->setFontSize(16); textBox->setFormat("[-]?[0-9]*\\.?[0-9]+"); } { new Label(window, "Positive integer :", "sans-bold"); textBox = new TextBox(window); textBox->setEditable(true); textBox->setFixedSize(Vector2i(100, 20)); textBox->setValue("50"); textBox->setUnits("Mhz"); textBox->setDefaultValue("0.0"); textBox->setFontSize(16); textBox->setFormat("[1-9][0-9]*"); } { new Label(window, "Checkbox :", "sans-bold"); cb = new CheckBox(window, "Check me"); cb->setFontSize(16); cb->setChecked(true); } new Label(window, "Combo box :", "sans-bold"); ComboBox *cobo = new ComboBox(window, { "Item 1", "Item 2", "Item 3" }); cobo->setFontSize(16); cobo->setFixedSize(Vector2i(100,20)); new Label(window, "Color button :", "sans-bold"); popupBtn = new PopupButton(window, "", 0); popupBtn->setBackgroundColor(Color(255, 120, 0, 255)); popupBtn->setFontSize(16); popupBtn->setFixedSize(Vector2i(100, 20)); popup = popupBtn->popup(); popup->setLayout(new GroupLayout()); ColorWheel *colorwheel = new ColorWheel(popup); colorwheel->setColor(popupBtn->backgroundColor()); Button *colorBtn = new Button(popup, "Pick"); colorBtn->setFixedSize(Vector2i(100, 25)); Color c = colorwheel->color(); colorBtn->setBackgroundColor(c); colorwheel->setCallback([colorBtn](const Color &value) { colorBtn->setBackgroundColor(value); }); colorBtn->setChangeCallback([colorBtn, popupBtn](bool pushed) { if (pushed) { popupBtn->setBackgroundColor(colorBtn->backgroundColor()); popupBtn->setPushed(false); } }); performLayout(mNVGContext); /* All NanoGUI widgets are initialized at this point. Now create an OpenGL shader to draw the main window contents. NanoGUI comes with a simple Eigen-based wrapper around OpenGL 3, which eliminates most of the tedious and error-prone shader and buffer object management. */ mShader.init( /* An identifying name */ "a_simple_shader", /* Vertex shader */ "#version 330\n" "uniform mat4 modelViewProj;\n" "in vec4 vertex;\n" "in vec3 color;\n" "out vec3 varying_color;\n" "void main() {\n" " gl_Position = modelViewProj * vertex;\n" " varying_color = color;\n" "}", /* Fragment shader */ "#version 330\n" "in vec3 varying_color;\n" "out vec4 frag_color;\n" "uniform float intensity;\n" "void main() {\n" " frag_color = vec4(intensity * varying_color, 1.0);\n" "}" ); mShader.registerAttribs({ {"color", "vec3"} }); mShader.registerUniforms({ {"modelViewProj", "mat4"}, {"intensity", "float"} }); }