diff --git a/include/gui_handler.hpp b/include/gui_handler.hpp index 7db1449384d039ee953792b830840fe1b0a1f47b..94235ec152c3e287de4d6995a7790d556e9d4549 100644 --- a/include/gui_handler.hpp +++ b/include/gui_handler.hpp @@ -39,12 +39,12 @@ private: GLuint btex_immediate_off_ = 0, btex_immediate_on_ = 0; GLuint btex_draw_off_ = 0, btex_draw_on_ = 0; GLuint btex_erase_off_ = 0, btex_erase_on_ = 0; - GLuint btex_singleslit_off_ = 0, btex_singleslit_on_ = 0; - GLuint btex_doubleslit_off_ = 0, btex_doubleslit_on_ = 0; - GLuint btex_lattice_off_ = 0, btex_lattice_on_ = 0; - GLuint btex_waveguide_off_ = 0, btex_waveguide_on_ = 0; - GLuint btex_ssh_off_ = 0, btex_ssh_on_ = 0; - GLuint btex_fresnel_off_ = 0, btex_fresnel_on_ = 0; + GLuint btex_singleslit_off_ = 0; + GLuint btex_doubleslit_off_ = 0; + GLuint btex_lattice_off_ = 0; + GLuint btex_waveguide_off_ = 0; + GLuint btex_ssh_off_ = 0; + GLuint btex_fresnel_off_ = 0; /*Font*/ ImFont* font_ = NULL; diff --git a/src/gui_handler.cpp b/src/gui_handler.cpp index 3eed801ec80202f567755c85b169e66a4628c572..cb8a08e7f43ad12db066c2f4cb6b212e8d65efa3 100644 --- a/src/gui_handler.cpp +++ b/src/gui_handler.cpp @@ -26,12 +26,16 @@ void GuiHandler::init(Toolbox& tb, const std::string path_img, const std::string ImGui_ImplOpenGL3_Init(glsl_version); load_button_textures_(path_img); + std::cout << "Images loaded" << std::endl; /*Load font*/ std::string fullpath_font = path_ttf + "Cousine-Regular.ttf"; - font_ = io_.Fonts->AddFontFromFileTTF(fullpath_font.c_str(), 20.0f); + font_ = io_.Fonts->AddFontFromFileTTF(fullpath_font.c_str(), 30.0f); if (font_ != NULL) { - std::cout << "Font loaded" << std::endl; + std::cout << "Fonts loaded" << std::endl; + } + else { + std::cout << "Failed to load fonts" << std::endl; } io_.Fonts->Build(); } @@ -153,14 +157,13 @@ void GuiHandler::update(Toolbox& tb) { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplSDL2_NewFrame(tb.infra.window()); ImGui::NewFrame(); - ImGui::SetNextWindowPos(ImVec2(tb.screen_w * (1.f - gui_pos_), 0.f), ImGuiCond_Always); - ImGui::SetNextWindowSize(ImVec2(tb.screen_w * gui_pos_, tb.screen_h), ImGuiCond_Always); //ImGui::SetMouseCursor(ImGuiMouseCursor_None); - draw_gui_(tb); + draw_gui_ft_(tb); /*Debugging: Developer window*/ + ImGui::Begin("Developer", nullptr, ImGuiWindowFlags_AlwaysAutoResize); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0 / double(ImGui::GetIO().Framerate), double(ImGui::GetIO().Framerate)); GLint total_mem_kb = 0; @@ -195,6 +198,7 @@ void GuiHandler::update(Toolbox& tb) { ImGui::Text("Timeout Threshold: %d s", tb.timeout_threshold); ImGui::Text("Timeout Timer: %d s", tb.timeout_timer); ImGui::End(); + /*Debugging: Event Console*/ /* ImGui::SetNextWindowPos(ImVec2(tb.screen_w * (0.5), 0.f), ImGuiCond_Always); @@ -224,7 +228,6 @@ void GuiHandler::update(Toolbox& tb) { ImGui::Text(str.c_str()); } */ - ImGui::End(); } void GuiHandler::render(Toolbox &tb) { @@ -244,7 +247,7 @@ void GuiHandler::draw_gui_(Toolbox& tb) { /*Set Window Position and Size and start the Window*/ ImGui::SetNextWindowPos(ImVec2(tb.screen_w * (1.f - gui_pos_), 0.f), ImGuiCond_Always); ImGui::SetNextWindowSize(ImVec2(tb.screen_w * gui_pos_, tb.screen_h), ImGuiCond_Always); - ImGui::Begin("Controller", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse); + ImGui::Begin("Controller", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar); /*Modes*/ if (tb.m_state == static_cast<int>(MSTATE::IMMEDIATE)) { @@ -457,7 +460,240 @@ void GuiHandler::draw_gui_(Toolbox& tb) { } void GuiHandler::draw_gui_ft_(Toolbox& tb) { + /*Set Window Position and Size and start the Window*/ + ImGui::SetNextWindowPos(ImVec2(tb.screen_w * (1.f - gui_pos_), 0.f), ImGuiCond_Always); + ImGui::SetNextWindowSize(ImVec2(tb.screen_w * gui_pos_, tb.screen_h), ImGuiCond_Always); + ImGui::Begin("Controller", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar); + + /*Top Space*/ + ImGui::Dummy(ImVec2(0, 50)); + /*Modes*/ + int space_left_normal = 35; + int image_button_dim = 210; + int space_left_to_center = image_button_dim / 2 + space_left_normal + 7; + ImGui::Dummy(ImVec2(space_left_to_center, 0)); + ImGui::SameLine(); + if (tb.m_state == static_cast<int>(MSTATE::IMMEDIATE)) { + if (ImGui::ImageButton((void*)(btex_immediate_on_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.m_state = static_cast<int>(MSTATE::IMMEDIATE); + } + } + else { + if (ImGui::ImageButton((void*)(btex_immediate_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.m_state = static_cast<int>(MSTATE::IMMEDIATE); + } + } + ImGui::Dummy(ImVec2(space_left_to_center + image_button_dim / 5 + 7, 0)); + ImGui::SameLine(); + ImGui::Text("Spielen"); + ImGui::Dummy(ImVec2(space_left_normal, 0)); + ImGui::SameLine(); + if (tb.m_state == static_cast<int>(MSTATE::DRAW)) { + if (ImGui::ImageButton((void*)(btex_draw_on_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.m_state = static_cast<int>(MSTATE::DRAW); + } + } + else { + if (ImGui::ImageButton((void*)(btex_draw_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.m_state = static_cast<int>(MSTATE::DRAW); + } + } + ImGui::SameLine(); + if (tb.m_state == static_cast<int>(MSTATE::ERASE)) { + if (ImGui::ImageButton((void*)(btex_erase_on_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.m_state = static_cast<int>(MSTATE::ERASE); + } + } + else { + if (ImGui::ImageButton((void*)(btex_erase_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.m_state = static_cast<int>(MSTATE::ERASE); + } + } + ImGui::Dummy(ImVec2(space_left_normal + image_button_dim / 5, 0)); + ImGui::SameLine(); + ImGui::Text("Zeichnen"); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(100 - image_button_dim / 6, 0)); + ImGui::SameLine(); + ImGui::Text("Radieren"); + + ImGui::NewLine(); + ImGui::Separator(); + ImGui::NewLine(); + + /*Patterns*/ + + //SSH + ImGui::Dummy(ImVec2(space_left_to_center, 0)); + ImGui::SameLine(); + if (ImGui::ImageButton((void*)(btex_ssh_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.mailbox.push_back({ WAVEMESSAGE::RESET_DAMPING }); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_WAVE }); + tb.mailbox.push_back({ DRAWERMESSAGE::CLEAR }); + tb.mailbox.push_back({ BLOCKCHAINMESSAGE::CLEAR }); + tb.mailbox.push_back({ PATTERNMESSAGE::PATTERN_SSH }); + tb.source_frequency = 1.424f; + } + ImGui::Dummy(ImVec2(space_left_to_center + 7, 0)); + ImGui::SameLine(); + ImGui::Text("Metamaterial"); + + //Lattice and Waveguide + ImGui::Dummy(ImVec2(space_left_normal, 0)); + ImGui::SameLine(); + if (ImGui::ImageButton((void*)(btex_lattice_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.mailbox.push_back({ WAVEMESSAGE::RESET_DAMPING }); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_WAVE }); + tb.mailbox.push_back({ DRAWERMESSAGE::CLEAR }); + tb.mailbox.push_back({ BLOCKCHAINMESSAGE::CLEAR }); + tb.mailbox.push_back({ PATTERNMESSAGE::PATTERN_LATTICE }); + tb.source_frequency = 5.f; + } + ImGui::SameLine(); + if (ImGui::ImageButton((void*)(btex_waveguide_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.mailbox.push_back({ WAVEMESSAGE::RESET_DAMPING }); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_WAVE }); + tb.mailbox.push_back({ DRAWERMESSAGE::CLEAR }); + tb.mailbox.push_back({ BLOCKCHAINMESSAGE::CLEAR }); + tb.mailbox.push_back({ PATTERNMESSAGE::PATTERN_WAVEGUIDE }); + tb.source_frequency = 5.f; + } + ImGui::Dummy(ImVec2(space_left_normal + image_button_dim / 4 + 7, 0)); + ImGui::SameLine(); + ImGui::Text("Gitter"); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(49, 0)); + ImGui::SameLine(); + ImGui::Text("Wellenleiter"); + + //Doubleslit, Fresnel + ImGui::Dummy(ImVec2(space_left_normal, 0)); + ImGui::SameLine(); + if (ImGui::ImageButton((void*)(btex_doubleslit_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.mailbox.push_back({ WAVEMESSAGE::RESET_DAMPING }); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_WAVE }); + tb.mailbox.push_back({ DRAWERMESSAGE::CLEAR }); + tb.mailbox.push_back({ BLOCKCHAINMESSAGE::CLEAR }); + tb.mailbox.push_back({ PATTERNMESSAGE::PATTERN_DOUBLESLIT }); + tb.source_frequency = 5.f; + } + ImGui::SameLine(); + if (ImGui::ImageButton((void*)(btex_fresnel_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.mailbox.push_back({ WAVEMESSAGE::RESET_DAMPING }); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_WAVE }); + tb.mailbox.push_back({ DRAWERMESSAGE::CLEAR }); + tb.mailbox.push_back({ BLOCKCHAINMESSAGE::CLEAR }); + tb.mailbox.push_back({ PATTERNMESSAGE::PATTERN_FRESNEL }); + tb.source_frequency = 5.f; + } + ImGui::Dummy(ImVec2(space_left_normal + image_button_dim / 12, 0)); + ImGui::SameLine(); + ImGui::Text("Doppelspalt"); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(image_button_dim / 12 + image_button_dim / 18 - 14, 0)); + ImGui::SameLine(); + ImGui::Text("Fresnellinse"); + + //Singleslit + ImGui::Dummy(ImVec2(space_left_to_center, 0)); + ImGui::SameLine(); + if (ImGui::ImageButton((void*)(btex_singleslit_off_), ImVec2(image_button_dim, image_button_dim), ImVec2(0, 0), ImVec2(1, 1), 0, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + tb.mailbox.push_back({ WAVEMESSAGE::RESET_DAMPING }); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_WAVE }); + tb.mailbox.push_back({ DRAWERMESSAGE::CLEAR }); + tb.mailbox.push_back({ BLOCKCHAINMESSAGE::CLEAR }); + tb.mailbox.push_back({ PATTERNMESSAGE::PATTERN_SINGLESLIT }); + tb.source_frequency = 5.f; + } + ImGui::Dummy(ImVec2(space_left_to_center + image_button_dim / 12, 0)); + ImGui::SameLine(); + ImGui::Text("Einzelspalt"); + + ImGui::NewLine(); + ImGui::Separator(); + ImGui::NewLine(); + + + /*Game Control*/ + int button_width = image_button_dim; + //int button_height = 30; + int button_height = 40; + + //RESET and START / STOPP + ImGui::Dummy(ImVec2(space_left_normal, 0)); + ImGui::SameLine(); + if (ImGui::Button("RESET", ImVec2(button_width, button_height))) { + /*If any of this changes, then also change TimeoutHandler::update!*/ + tb.source_frequency = 5.f; /*TODO: If initial frequency changes, this must be adapted*/ + tb.g_state = static_cast<int>(GSTATE::RUN); + tb.m_state = static_cast<int>(MSTATE::IMMEDIATE); + tb.s_state = static_cast<int>(SSTATE::RUN); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_WAVE }); + tb.mailbox.push_back({ WAVEMESSAGE::RESET_DAMPING }); + tb.mailbox.push_back({ BLOCKCHAINMESSAGE::CLEAR }); + tb.mailbox.push_back({ DRAWERMESSAGE::CLEAR }); + } + ImGui::SameLine(); + if (tb.g_state != static_cast<int>(GSTATE::FREEZE)) { + if (ImGui::Button("STOPP", ImVec2(button_width, button_height))) { + tb.g_state = static_cast<int>(GSTATE::FREEZE); + } + } + else { + ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor(247, 248, 68)); + ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)ImColor(0, 0, 0)); + if (ImGui::Button("START", ImVec2(button_width, button_height))) { + tb.g_state = static_cast<int>(GSTATE::RUN); + } + ImGui::PopStyleColor(2); + } + + ImGui::NewLine(); + ImGui::Separator(); + ImGui::NewLine(); + + + /*Frequency Selection Slider*/ + ImGui::Dummy(ImVec2(space_left_normal - 18, 0)); + ImGui::SameLine(); + ImGui::Text("1.4 Hz"); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(95, 0)); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(150, 0)); + ImGui::SameLine(); + ImGui::Text("10 Hz"); + ImGui::Dummy(ImVec2(space_left_normal, 0)); + ImGui::SameLine(); + ImGui::SetNextItemWidth(2 * image_button_dim + 15); + ImGui::SliderFloat("", &tb.source_frequency, 1.424f, 10.f); + std::vector<float> wave_1; + for (float t = 0; t < 1.f; t += .05f) { + wave_1.push_back(std::sin(2. * 3.1415 * t)); + } + ImGui::Dummy(ImVec2(space_left_normal - 18, 0)); + ImGui::SameLine(); + ImGui::SetNextItemWidth(50); + ImGui::PushStyleColor(ImGuiCol_FrameBg, { 0,0,0,0 }); + ImGui::PushStyleColor(ImGuiCol_PlotLines, { 169. / 256.,218. / 256.,224. / 256.,1. }); + ImGui::PlotLines("", wave_1.data(), wave_1.size()); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(90, 0)); + ImGui::SameLine(); + ImGui::Text("Frequenz"); + ImGui::SameLine(); + ImGui::Dummy(ImVec2(93, 0)); + ImGui::SameLine(); + wave_1.clear(); + for (float t = 0; t < 1.f; t += .05f) { + wave_1.push_back(std::sin(8. * 3.1415 * t)); + } + ImGui::SetNextItemWidth(50); + ImGui::PlotLines("", wave_1.data(), wave_1.size()); + ImGui::PopStyleColor(2); + + ImGui::End(); } void GuiHandler::draw_old_gui_(Toolbox& tb){ @@ -540,63 +776,51 @@ void GuiHandler::draw_old_gui_(Toolbox& tb){ void GuiHandler::load_button_textures_(const std::string path) { /*Immediate Mode Textures*/ - std::string file = path + "immediate_off.jpg"; + std::string file = path + "immediate_off.png"; load_image_to_texture_(file, btex_immediate_off_); - file = path + "immediate_on.jpg"; + file = path + "immediate_on.png"; load_image_to_texture_(file, btex_immediate_on_); /*Draw Mode Textures*/ - file = path + "draw_off.jpg"; + file = path + "draw_off.png"; load_image_to_texture_(file, btex_draw_off_); - file = path + "draw_on.jpg"; + file = path + "draw_on.png"; load_image_to_texture_(file, btex_draw_on_); /*Erase Mode Textures*/ - file = path + "erase_off.jpg"; + file = path + "erase_off.png"; load_image_to_texture_(file, btex_erase_off_); - file = path + "erase_on.jpg"; + file = path + "erase_on.png"; load_image_to_texture_(file, btex_erase_on_); - /*Singleslit Textures*/ + /*Singleslit Texture*/ file = path + "singleslit_off.jpg"; load_image_to_texture_(file, btex_singleslit_off_); - file = path + "singleslit_on.jpg"; - load_image_to_texture_(file, btex_singleslit_on_); - /*Doubleslit Textures*/ + /*Doubleslit Texture*/ file = path + "doubleslit_off.jpg"; load_image_to_texture_(file, btex_doubleslit_off_); - file = path + "doubleslit_on.jpg"; - load_image_to_texture_(file, btex_doubleslit_on_); - /*Lattice Textures*/ + /*Lattice Texture*/ file = path + "lattice_off.jpg"; load_image_to_texture_(file, btex_lattice_off_); - file = path + "lattice_on.jpg"; - load_image_to_texture_(file, btex_lattice_on_); - /*Waveguide Textures*/ + /*Waveguide Texture*/ file = path + "waveguide_off.jpg"; load_image_to_texture_(file, btex_waveguide_off_); - file = path + "waveguide_on.jpg"; - load_image_to_texture_(file, btex_waveguide_on_); - /*SSH Textures*/ + /*SSH Texture*/ file = path + "ssh_off.jpg"; load_image_to_texture_(file, btex_ssh_off_); - file = path + "ssh_on.jpg"; - load_image_to_texture_(file, btex_ssh_on_); - /*Fresnel Textures*/ + /*Fresnel Texture*/ file = path + "fresnel_off.jpg"; load_image_to_texture_(file, btex_fresnel_off_); - file = path + "fresnel_on.jpg"; - load_image_to_texture_(file, btex_fresnel_on_); } void GuiHandler::load_image_to_texture_(const std::string file, GLuint& texture) { int width, height, nrChannels; unsigned char* data; - data = stbi_load(file.c_str(), &width, &height, &nrChannels, 0); + data = stbi_load(file.c_str(), &width, &height, &nrChannels, STBI_rgb_alpha); if (!data) { std::cout << "Failed to load file " << file << std::endl; return; } glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);