demo_nanovg_launcher.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Part of ImGui Bundle - MIT License - Copyright (c) 2022-2026 Pascal Thomet - https://github.com/pthom/imgui_bundle
  2. from imgui_bundle import imgui, hello_imgui, imgui_md, immapp, ImVec2
  3. from imgui_bundle.demos_python import demo_utils # this will set the assets folder
  4. def _do_spawn_demo(demo_name: str):
  5. import os.path
  6. import subprocess
  7. import sys
  8. this_dir = os.path.dirname(__file__)
  9. demo_file = this_dir + "/demos_nanovg/" + demo_name + ".py"
  10. subprocess.Popen([sys.executable, demo_file])
  11. def demo_gui():
  12. static = demo_gui
  13. if not hasattr(static, "is_full_demo_opened"):
  14. static.is_full_demo_opened = False
  15. if not hasattr(static, "is_simple_demo_opened"):
  16. static.is_simple_demo_opened = False
  17. if not hasattr(static, "show_full_demo_rendering_code"):
  18. static.show_full_demo_rendering_code = False
  19. imgui_md.render_unindented("""
  20. [NanoVG](https://github.com/memononen/nanovg) provides antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
  21. """)
  22. if not static.is_full_demo_opened and not static.is_simple_demo_opened:
  23. imgui.same_line(imgui.get_window_width() - hello_imgui.em_size(14.0))
  24. if hello_imgui.image_button_from_asset("images/nanovg_full_demo.jpg", ImVec2(hello_imgui.em_size(11.0), 0.0)):
  25. _do_spawn_demo("demo_nanovg_full")
  26. nb_code_lines = 35
  27. if imgui.collapsing_header("Full Demo"):
  28. static.is_full_demo_opened = True
  29. imgui.begin_group()
  30. imgui.text(
  31. "This is the original NanoVG demo, integrated to ImGui Bundle (and also ported to python).\n"
  32. "Click the button below to launch the demo"
  33. )
  34. imgui.new_line()
  35. if demo_utils.can_run_subprocess():
  36. if imgui.button("Run full demo"):
  37. _do_spawn_demo("demo_nanovg_full")
  38. imgui.end_group()
  39. imgui.same_line(imgui.get_window_width() - hello_imgui.em_size(14.0))
  40. if hello_imgui.image_button_from_asset("images/nanovg_full_demo.jpg", ImVec2(hello_imgui.em_size(11.0), 0.0)):
  41. _do_spawn_demo("demo_nanovg_full")
  42. if imgui.radio_button("Show launcher code", not static.show_full_demo_rendering_code):
  43. static.show_full_demo_rendering_code = False
  44. imgui.same_line()
  45. if imgui.radio_button("Show rendering code", static.show_full_demo_rendering_code):
  46. static.show_full_demo_rendering_code = True
  47. if not static.show_full_demo_rendering_code:
  48. demo_utils.show_python_vs_cpp_file("demos_nanovg/demo_nanovg_full", nb_code_lines)
  49. else:
  50. demo_utils.show_python_vs_cpp_file("demos_nanovg/demo_nanovg_full/demo_nanovg_full_impl", nb_code_lines)
  51. else:
  52. static.is_full_demo_opened = False
  53. if imgui.collapsing_header("Simple Demo"):
  54. static.is_simple_demo_opened = True
  55. imgui.begin_group()
  56. imgui.text(
  57. "This is a simpler demo, that shows how to display NanoVG as the background, or as a texture.\n"
  58. "(via a framebuffer object)\n"
  59. "Click the button below to launch the demo"
  60. )
  61. imgui.new_line()
  62. if demo_utils.can_run_subprocess():
  63. if imgui.button("Run simple demo"):
  64. _do_spawn_demo("demo_nanovg_heart")
  65. imgui.end_group()
  66. imgui.same_line(imgui.get_window_width() - hello_imgui.em_size(14.0))
  67. if hello_imgui.image_button_from_asset("images/nanovg_demo_heart.jpg", ImVec2(hello_imgui.em_size(11.0), 0.0)):
  68. _do_spawn_demo("demo_nanovg_heart")
  69. demo_utils.show_python_vs_cpp_file("demos_nanovg/demo_nanovg_heart", nb_code_lines)
  70. else:
  71. static.is_simple_demo_opened = False
  72. if __name__ == "__main__":
  73. immapp.run(demo_gui, window_size=(1000, 800), with_markdown=True, with_node_editor=True)