Ui.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <tests/framework/Framework.h>
  6. #include <anki/core/ConfigSet.h>
  7. #include <anki/util/HighRezTimer.h>
  8. #include <anki/Ui.h>
  9. #include <anki/Input.h>
  10. #include <anki/core/StagingGpuMemoryManager.h>
  11. namespace anki
  12. {
  13. class Label : public UiImmediateModeBuilder
  14. {
  15. public:
  16. using UiImmediateModeBuilder::UiImmediateModeBuilder;
  17. Bool m_windowInitialized = false;
  18. U32 m_buttonClickCount = 0;
  19. void build(CanvasPtr canvas) final
  20. {
  21. Vec4 oldBackground = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
  22. ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w = 0.8f;
  23. ImGui::Begin("ImGui Demo", nullptr);
  24. if(!m_windowInitialized)
  25. {
  26. ImGui::SetWindowPos(Vec2(20, 10));
  27. ImGui::SetWindowSize(Vec2(200, 500));
  28. m_windowInitialized = true;
  29. }
  30. ImGui::Text("Label default size");
  31. canvas->pushFont(canvas->getDefaultFont(), 30);
  32. ImGui::Text("Label size 30");
  33. ImGui::PopFont();
  34. m_buttonClickCount += ImGui::Button("Toggle");
  35. if(m_buttonClickCount & 1)
  36. {
  37. ImGui::Button("Toggled");
  38. }
  39. ImGui::End();
  40. ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = oldBackground;
  41. }
  42. };
  43. ANKI_TEST(Ui, Ui)
  44. {
  45. ConfigSet cfg = DefaultConfigSet::get();
  46. initConfig(cfg);
  47. cfg.set("gr_vsync", 1);
  48. cfg.set("gr_debugContext", 0);
  49. cfg.set("width", 1024);
  50. cfg.set("height", 760);
  51. cfg.set("rsrc_dataPaths", "engine_data");
  52. NativeWindow* win = createWindow(cfg);
  53. Input* in = new Input();
  54. GrManager* gr = createGrManager(cfg, win);
  55. PhysicsWorld* physics;
  56. ResourceFilesystem* fs;
  57. ResourceManager* resource = createResourceManager(cfg, gr, physics, fs);
  58. UiManager* ui = new UiManager();
  59. ANKI_TEST_EXPECT_NO_ERR(in->init(win));
  60. StagingGpuMemoryManager* stagingMem = new StagingGpuMemoryManager();
  61. ANKI_TEST_EXPECT_NO_ERR(stagingMem->init(gr, cfg));
  62. HeapAllocator<U8> alloc(allocAligned, nullptr);
  63. ANKI_TEST_EXPECT_NO_ERR(ui->init(allocAligned, nullptr, resource, gr, stagingMem, in));
  64. {
  65. FontPtr font;
  66. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(font, "UbuntuRegular.ttf", std::initializer_list<U32>{10, 20, 30, 60}));
  67. CanvasPtr canvas;
  68. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(canvas, font, 20, win->getWidth(), win->getHeight()));
  69. IntrusivePtr<Label> label;
  70. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(label));
  71. Bool done = false;
  72. while(!done)
  73. {
  74. ANKI_TEST_EXPECT_NO_ERR(in->handleEvents());
  75. HighRezTimer timer;
  76. timer.start();
  77. canvas->handleInput();
  78. if(in->getKey(KeyCode::ESCAPE))
  79. {
  80. done = true;
  81. }
  82. canvas->beginBuilding();
  83. label->build(canvas);
  84. TexturePtr presentTex = gr->acquireNextPresentableTexture();
  85. FramebufferPtr fb;
  86. {
  87. TextureViewInitInfo init;
  88. init.m_texture = presentTex;
  89. TextureViewPtr view = gr->newTextureView(init);
  90. FramebufferInitInfo fbinit;
  91. fbinit.m_colorAttachmentCount = 1;
  92. fbinit.m_colorAttachments[0].m_clearValue.m_colorf = {{1.0, 0.0, 1.0, 1.0}};
  93. fbinit.m_colorAttachments[0].m_textureView = view;
  94. fb = gr->newFramebuffer(fbinit);
  95. }
  96. CommandBufferInitInfo cinit;
  97. cinit.m_flags = CommandBufferFlag::GRAPHICS_WORK | CommandBufferFlag::SMALL_BATCH;
  98. CommandBufferPtr cmdb = gr->newCommandBuffer(cinit);
  99. cmdb->setTextureBarrier(presentTex, TextureUsageBit::NONE, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
  100. TextureSubresourceInfo());
  101. cmdb->beginRenderPass(fb, {{TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE}}, {});
  102. canvas->appendToCommandBuffer(cmdb);
  103. cmdb->endRenderPass();
  104. cmdb->setTextureBarrier(presentTex, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, TextureUsageBit::PRESENT,
  105. TextureSubresourceInfo());
  106. cmdb->flush();
  107. gr->swapBuffers();
  108. stagingMem->endFrame();
  109. timer.stop();
  110. const F32 TICK = 1.0f / 30.0f;
  111. if(timer.getElapsedTime() < TICK)
  112. {
  113. HighRezTimer::sleep(TICK - timer.getElapsedTime());
  114. }
  115. }
  116. }
  117. delete ui;
  118. delete stagingMem;
  119. delete resource;
  120. delete physics;
  121. delete fs;
  122. GrManager::deleteInstance(gr);
  123. delete in;
  124. delete win;
  125. }
  126. } // end namespace anki