Pangolin: 3 - Use UI
# Targets
- Add button
- Print some texts when click Button
# Modules
## Add UI panel
```cpp
const int UI_WIDTH = 180;
// Add named OpenGL viewport to window and provide 3D Handler
pangolin::View& d_cam = pangolin::CreateDisplay()
.SetBounds(0.0, 1.0, pangolin::Attach::Pix(UI_WIDTH), 1.0, -float(w) / float(h))
.SetHandler(new pangolin::Handler3D(s_cam));
// Add named Panel and bind to variables beginning 'ui'
// A Panel is just a View with a default layout and input handling
pangolin::CreatePanel("ui") .SetBounds(0.0, 1.0, 0.0, pangolin::Attach::Pix(UI_WIDTH));
```
## Add button
```cpp
pangolin::CreatePanel("ui").SetBounds(0.0, 1.0, 0.0, pangolin::Attach::Pix(UI_WIDTH));
pangolin::Var a_button("ui.Button", false, false);
if (pangolin::Pushed(a_button))
std::cout << "You Pushed a button!" << std::endl;
```
Result:
![image-20200607131244515](https://raw.githubusercontent.com/yubaoliu/assets/image/image-20200607131244515.png)
Clic the button:
```sh
You Pushed a button!
```
## Add callback function to deal with keyboard event and mouse click event
```cpp
pangolin::Var> reset("ui.Reset", SampleMethod);
pangolin::RegisterKeyPressCallback(pangolin::PANGO_CTRL + 'r', SampleMethod);
void SampleMethod()
{
std::cout << "You typed ctrl-r or pushed reset" << std::endl;
}
```
Result:
Click "Reset" or press "C-r" to reset sth.
![image-20200607133438299](https://raw.githubusercontent.com/yubaoliu/assets/image/image-20200607133438299.png)
## Save window and cube
```cpp
pangolin::Var save_window("ui.Save_Window", false, false);
pangolin::Var save_cube("ui.Save_Cube", false, false);
if (pangolin::Pushed(save_window))
{
std::cout<<"Save window"< record_cube("ui.Record_Cube",false,false);
if (pangolin::Pushed(record_cube))
pangolin::DisplayBase().RecordOnRender("ffmpeg:[fps=50,bps=8388608,unique_filename]//screencap.avi");
```
No comments