Ui.cpp 4.1 KB

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