FTXUI基础笔记(botton按钮组件基础)

举报
zhangrelay 发表于 2022/07/17 23:00:41 2022/07/17
【摘要】   先看原版示例程序吧: #include <memory> // for shared_ptr, __shared_ptr_access#include <string> // for operator+, to_string #include "ftxui/component/captu...

 

先看原版示例程序吧:


  
  1. #include <memory> // for shared_ptr, __shared_ptr_access
  2. #include <string> // for operator+, to_string
  3. #include "ftxui/component/captured_mouse.hpp" // for ftxui
  4. #include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
  5. #include "ftxui/component/component_base.hpp" // for ComponentBase
  6. #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
  7. #include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
  8. using namespace ftxui;
  9. int main(int argc, const char* argv[]) {
  10. int value = 50;
  11. // The tree of components. This defines how to navigate using the keyboard.
  12. auto buttons = Container::Horizontal({
  13. Button("计数加1", [&] { value--; }),
  14. Button("计数减1", [&] { value++; }),
  15. });
  16. // Modify the way to render them on screen:
  17. auto component = Renderer(buttons, [&] {
  18. return vbox({
  19. text(" 数值 = " + std::to_string(value)),
  20. separator(),
  21. gauge(value * 0.01f),
  22. separator(),
  23. buttons->Render(),
  24. }) |
  25. border;
  26. });
  27. auto screen = ScreenInteractive::FitComponent();
  28. screen.Loop(component);
  29. return 0;
  30. }
  31. // Copyright 2020 Arthur Sonzogni. All rights reserved.
  32. // Use of this source code is governed by the MIT license that can be found in
  33. // the LICENSE file.

默认数值为50。点击按键实现加减1功能。


简要解释一下:

头文件部分

#include <memory>  // for shared_ptr, __shared_ptr_access
#include <string>  // for operator+, to_string

#include "ftxui/component/captured_mouse.hpp"  // for ftxui
#include "ftxui/component/component.hpp"  // for Button, Horizontal, Renderer
#include "ftxui/component/component_base.hpp"      // for ComponentBase
#include "ftxui/component/screen_interactive.hpp"  // for ScreenInteractive
#include "ftxui/dom/elements.hpp"  // for separator, gauge, text, Element, operator|, vbox, border

按钮属性,包括点击后功能

  // The tree of components. This defines how to navigate using the keyboard.
  auto buttons = Container::Horizontal({
      Button("计数加1", [&] { value--; }),
      Button("计数减1", [&] { value++; }),
  });

整体在终端的显示:

  // Modify the way to render them on screen:
  auto component = Renderer(buttons, [&] {
    return vbox({
               text(" 数值 = " + std::to_string(value)),
               separator(),
               gauge(value * 0.01f),
               separator(),
               buttons->Render(),
           }) |
           border;
  });


程序bug,加减写反了:

自行修订即可。 


这个功能太简陋了,原版复杂一些的示例如下:

 


  
  1. #include <memory> // for shared_ptr, __shared_ptr_access
  2. #include <string> // for operator+, to_string
  3. #include "ftxui/component/captured_mouse.hpp" // for ftxui
  4. #include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
  5. #include "ftxui/component/component_base.hpp" // for ComponentBase
  6. #include "ftxui/component/component_options.hpp" // for ButtonOption
  7. #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
  8. #include "ftxui/dom/elements.hpp" // for gauge, separator, text, vbox, operator|, Element, border
  9. #include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Green, Color::Red
  10. using namespace ftxui;
  11. int main(int argc, const char* argv[]) {
  12. int value = 50;
  13. // The tree of components. This defines how to navigate using the keyboard.
  14. auto buttons = Container::Horizontal({
  15. Button(
  16. "计数减一", [&] { value--; }, ButtonOption::Animated(Color::Red)),
  17. Button(
  18. "复位(默认50)", [&] { value = 50; }, ButtonOption::Animated(Color::Green)),
  19. Button(
  20. "计数加一", [&] { value++; }, ButtonOption::Animated(Color::Blue)),
  21. });
  22. // Modify the way to render them on screen:
  23. auto component = Renderer(buttons, [&] {
  24. return vbox({
  25. vbox({
  26. text(" 数值 = " + std::to_string(value)),
  27. separator(),
  28. gauge(value * 0.01f),
  29. }) | border,
  30. buttons->Render(),
  31. });
  32. });
  33. auto screen = ScreenInteractive::FitComponent();
  34. screen.Loop(component);
  35. return 0;
  36. }
  37. // Copyright 2020 Arthur Sonzogni. All rights reserved.
  38. // Use of this source code is governed by the MIT license that can be found in
  39. // the LICENSE file.

复位

 

看起来效果好一些。

改变之处:

  // The tree of components. This defines how to navigate using the keyboard.
  auto buttons = Container::Horizontal({
      Button(
          "计数减一", [&] { value--; }, ButtonOption::Animated(Color::Red)),
      Button(
          "复位(默认50)", [&] { value = 50; }, ButtonOption::Animated(Color::Green)),
      Button(
          "计数加一", [&] { value++; }, ButtonOption::Animated(Color::Blue)),
  });


-End-


文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/125826268

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。