技术笔记:NanoGUI使用

简介: 技术笔记:NanoGUI使用

为什么用


NanoGUI是一个支持OpenGL绘制的gui 库,也提供python的绑定(由pybind11实现)


优点


支持Mac、Windows和Linux,能够在windows平台上提供OpenGL3.x支持。


体积小


支持高清屏


使用lambda回调函数,逻辑清晰


缺点


维护不及时


功能少


缺少移动版本


总体来说,对于一般应用已经足够。


编译


从github下载:


git clone --recursive


Mac/Linux上使用CMake构建,在Windows上生成VC solution后编译。注意windows上仅支持win64 build。vsiual studio 2015需要升级update 2 或update3(见NanoGUI::issures 201)。


C++实例


使用nanogui界面时刻直接从nanogui::screen 继承:


class App : public nanogui::Screen {


public:


App() : nanogui::Screen(Eigen::Vector2i(1024, 768), "NanoGUI Test") {


//初始化界面中第一个或多个菜单窗口


}


virtual void draw(NVGcontext ctx) {


//更新界面


//...


Screen::draw(ctx);


}


virtual void drawContents() {


//使用OpenGL绘制窗口内容


}


virtual void Screen::drawAll() {


//代码效果参考:http://www.lyjsj.net.cn/wx/art_23328.html

glClearColor(mBackground【0】, mBackground【1】, mBackground【2】, 1.0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);


drawContents();


drawWidgets();


glfwSwapBuffers(mGLFWWindow);


}


virtual bool keyboardEvent(int key, int scancode, int action, int modifiers);


...


}


int main(int / argc /, char ** / argv /) {


try {


nanogui::init();


/ scoped variables / {


nanogui::ref app = new App();


app->drawAll();


app->setVisible(true);


nanogui::mainloop();


}


nanogui::shutdown();


} catch (const std::runtime_error &e) {


//...


return -1;


}


也可将其变为实例实现在view类中。


class myViewer{


public:


nanogui::Screen screen;


void draw(){


ngui->refresh();


screen->drawWidgets();


...


}


}


初始化菜单和窗口上的控件时,需要为其清晰的指定父窗口指针和布局(Layout)。也定义lambda函数作为button等对象的回调函数。


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->setTooltip("short tooltip");


也可以为对象自定义回调函数,然后将其加入到系统循环的refresh()中。


std::vectorvoid()] mRefreshCallbacks;


void refresh() {


for (auto const &callback : mRefreshCallbacks)


callback();


}


加入UI对象


template detail::FormWidget


addVariable(const std::string &label, const std::function[span class="hljs-title function invoke__">void(Type)> &setter, const std::function[span class="hljs-title function invoke__">Type()> &getter, bool editable = true) {


Label labelW = new Label(mWindow, label, mLabelFontName, mLabelFontSize);


//使用专用模板将数值类型转换为对应的UI对象


auto widget = new detail::FormWidget(mWindow);


//getter,setter函数加入callbacks集合


auto refresh = 【widget, getter】 {


Type value = getter(), current = widget->value();


if (value != current)


widget->setValue(value);


};


refresh();


widget->setCallback(setter);


widget->setEditable(editable);


widget->setFontSize(mWidgetFontSize);


Vector2i fs = widget->fixedSize();


widget->setFixedSize(Vector2i(fs.x() != 0 ? fs.x() : mFixedSize.x(),


fs.y() != 0 ? fs.y() : mFixedSize.y()));


mRefreshCallbacks.push_back(refresh);


}


Python实例


python中直接将生成的lib文件和pyd文件放到.py文件的同一目录(或Python搜索路径),实现参考实例的中的Python代码。

相关文章
|
3天前
|
监控 安全 网络协议
技术笔记:TOMOYOLinux(undone)
技术笔记:TOMOYOLinux(undone)
|
3天前
|
Linux 开发工具 C++
技术笔记:RustGUI编程
技术笔记:RustGUI编程
|
3天前
|
Linux API Apache
技术好文:saltstackpillar
技术好文:saltstackpillar
|
3天前
|
自然语言处理 编译器 API
技术笔记:Roslyn介绍
技术笔记:Roslyn介绍
|
3天前
|
XML 数据可视化 API
技术笔记:Layouts
技术笔记:Layouts
|
3天前
|
存储 数据库 数据安全/隐私保护
技术笔记:MouStudio技术储备之
技术笔记:MouStudio技术储备之
|
4天前
|
前端开发 关系型数据库 MySQL
技术好文:R基础学习(三)
技术好文:R基础学习(三)
|
3天前
|
NoSQL Linux Shell
技术笔记:linux系统开发基础
技术笔记:linux系统开发基础
10 0
|
3天前
|
存储 编解码 索引
技术好文:StudingDay3
技术好文:StudingDay3
|
4天前
|
前端开发
技术好文:wobble
技术好文:wobble