From 1d9e955bfafae26218542b242294fb04f05efdca Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Sun, 26 Apr 2020 14:38:59 +0200 Subject: [PATCH] New GUI buttons and clean media player --- ImGuiToolkit.cpp | 111 ++++++++++++++++++++++++++++++++++++--- ImGuiToolkit.h | 16 ++++-- ImGuiVisitor.cpp | 4 -- UserInterfaceManager.cpp | 28 +++++----- main.cpp | 51 ++++++------------ 5 files changed, 146 insertions(+), 64 deletions(-) diff --git a/ImGuiToolkit.cpp b/ImGuiToolkit.cpp index 40c33ff..0d915eb 100644 --- a/ImGuiToolkit.cpp +++ b/ImGuiToolkit.cpp @@ -1,5 +1,6 @@ #include +#include #ifdef _WIN32 # include @@ -24,21 +25,66 @@ unsigned int textureicons = 0; std::map fontmap; -void ImGuiToolkit::OpenWebpage( const char* url ) +void ImGuiToolkit::ButtonOpenWebpage( const char* url ) { + char label[512]; + sprintf( label, "%s %s", ICON_FA_EXTERNAL_LINK_ALT, url ); + + if ( ImGui::Button(label) ) + { + #ifdef _WIN32 - ShellExecuteA( nullptr, nullptr, url, nullptr, nullptr, 0 ); + ShellExecuteA( nullptr, nullptr, url, nullptr, nullptr, 0 ); #elif defined __APPLE__ - char buf[1024]; - sprintf( buf, "open %s", url ); - system( buf ); + char buf[1024]; + sprintf( buf, "open %s", url ); + system( buf ); #else - char buf[1024]; - sprintf( buf, "xdg-open %s", url ); - system( buf ); + char buf[1024]; + sprintf( buf, "xdg-open %s", url ); + system( buf ); #endif + + } } +void ImGuiToolkit::ButtonToggle(const char* str_id, bool* toggle) +{ + ImVec4* colors = ImGui::GetStyle().Colors; + ImVec2 p = ImGui::GetCursorScreenPos(); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + + float height = ImGui::GetFrameHeight() * 0.75f; + float width = height * 1.6f; + float radius = height * 0.50f; + + ImGui::InvisibleButton(str_id, ImVec2(width, height)); + if (ImGui::IsItemClicked()) + *toggle = !*toggle; + + float t = *toggle ? 1.0f : 0.0f; + + ImGuiContext& g = *GImGui; + float ANIM_SPEED = 0.1f; + if (g.LastActiveId == g.CurrentWindow->GetID(str_id))// && g.LastActiveIdTimer < ANIM_SPEED) + { + float t_anim = ImSaturate(g.LastActiveIdTimer / ANIM_SPEED); + t = *toggle ? (t_anim) : (1.0f - t_anim); + } + + ImU32 col_bg; + if (ImGui::IsItemHovered()) + col_bg = ImGui::GetColorU32(ImLerp(colors[ImGuiCol_FrameBgHovered], colors[ImGuiCol_TabHovered], t)); + else + col_bg = ImGui::GetColorU32(ImLerp(colors[ImGuiCol_FrameBg], colors[ImGuiCol_TabActive], t)); + + draw_list->AddRectFilled(p, ImVec2(p.x + width, p.y + height), col_bg, height * 0.5f); + draw_list->AddCircleFilled(ImVec2(p.x + radius + t * (width - radius * 2.0f), p.y + radius), radius - 1.5f, IM_COL32(255, 255, 255, 250)); + ImGui::SameLine(0,10); + ImGui::Text(str_id); +} + + void ImGuiToolkit::Icon(int i, int j) { // icons.dds is a 20 x 20 grid of icons @@ -66,6 +112,55 @@ bool ImGuiToolkit::ButtonIcon(int i, int j) return ret; } +bool ImGuiToolkit::ButtonIconToggle(int i, int j, int i_toggle, int j_toggle, bool* toggle) +{ + bool ret = false; + ImGui::PushID( i * 20 + j + i_toggle * 20 + j_toggle); + + if (*toggle) { + if ( ButtonIcon(i_toggle, j_toggle)) { + *toggle = false; + ret = true; + } + } + else { + if ( ButtonIcon(i, j)) { + *toggle = true; + ret = true; + } + } + + ImGui::PopID(); + return ret; +} + +struct Sum +{ + void operator()(std::pair n) { sum += n.first * 20 + n.second; } + int sum{0}; +}; + +bool ImGuiToolkit::ButtonIconMultistate(std::vector > icons, int* state) +{ + bool ret = false; + Sum id = std::for_each(icons.begin(), icons.end(), Sum()); + ImGui::PushID( id.sum ); + + int s = CLAMP(*state, 0, icons.size() - 1); + if ( ButtonIcon( icons[s].first, icons[s].second ) ){ + ++s; + if (s > icons.size() -1) + *state = 0; + else + *state = s; + ret = true; + } + + ImGui::PopID(); + return ret; +} + + void ImGuiToolkit::ShowIconsWindow(bool* p_open) { if (textureicons == 0) diff --git a/ImGuiToolkit.h b/ImGuiToolkit.h index caf70fd..f063793 100644 --- a/ImGuiToolkit.h +++ b/ImGuiToolkit.h @@ -4,18 +4,25 @@ #include #include #include +#include #include #include "rsc/fonts/IconsFontAwesome5.h" namespace ImGuiToolkit { - // Icons from resource icon.dds - void Icon(int i, int j); - bool ButtonIcon(int i, int j); + void Icon(int i, int j); void ShowIconsWindow(bool* p_open); - void ToggleButton( const char* label, bool& toggle ); + + // utility buttons + bool ButtonIcon(int i, int j); + bool ButtonIconToggle(int i, int j, int i_toggle, int j_toggle, bool* toggle); + bool ButtonIconMultistate(std::vector > icons, int* state); + void ButtonToggle( const char* label, bool* toggle ); + void ButtonOpenWebpage( const char* url ); + + // utility sliders void Bar(float value, float in, float out, float min, float max, const char* title, bool expand); bool TimelineSlider(const char* label, guint64 *time, guint64 duration, guint64 step); bool TimelineSliderEdit(const char* label, guint64 *time, guint64 duration, guint64 step, @@ -39,7 +46,6 @@ namespace ImGuiToolkit } accent_color; void SetAccentColor(accent_color color); - void OpenWebpage( const char* url ); } #endif // __IMGUI_TOOLKIT_H_ diff --git a/ImGuiVisitor.cpp b/ImGuiVisitor.cpp index 3564ef0..4d7ead7 100644 --- a/ImGuiVisitor.cpp +++ b/ImGuiVisitor.cpp @@ -116,10 +116,6 @@ void ImGuiVisitor::visit(Shader &n) ImGui::PushID(n.id()); -// if (ImGuiToolkit::ButtonIcon(14, 8)) n.color = glm::vec4(1.f, 1.f, 1.f, 1.f); -// ImGui::SameLine(0, 10); -// ImGui::ColorEdit3("Color", glm::value_ptr(n.color) ) ; - if (ImGuiToolkit::ButtonIcon(10, 2)) { n.blending = Shader::BLEND_OPACITY; n.color = glm::vec4(1.f, 1.f, 1.f, 1.f); diff --git a/UserInterfaceManager.cpp b/UserInterfaceManager.cpp index fcab85e..bf9d1be 100644 --- a/UserInterfaceManager.cpp +++ b/UserInterfaceManager.cpp @@ -536,15 +536,14 @@ static void ShowAboutOpengl(bool* p_open) ImGui::PopFont(); ImGui::Separator(); ImGui::Text("OpenGL is the premier environment for developing portable, \ninteractive 2D and 3D graphics applications."); - - if ( ImGui::Button( ICON_FA_EXTERNAL_LINK_ALT " https://www.opengl.org") ) - ImGuiToolkit::OpenWebpage("https://www.opengl.org"); + ImGuiToolkit::ButtonOpenWebpage("https://www.opengl.org"); ImGui::SameLine(); // static std::string allextensions( glGetString(GL_EXTENSIONS) ); static bool show_opengl_info = false; - ImGui::Checkbox( "Show details " ICON_FA_CHEVRON_DOWN, &show_opengl_info); + ImGuiToolkit::ButtonIconToggle(10,0,13,14,&show_opengl_info); + ImGui::SameLine(); ImGui::Text("Details"); if (show_opengl_info) { ImGui::Separator(); @@ -604,13 +603,12 @@ static void ShowAboutGStreamer(bool* p_open) ImGui::Separator(); ImGui::Text("A flexible, fast and multiplatform multimedia framework."); ImGui::Text("GStreamer is licensed under the LGPL License."); - - if ( ImGui::Button( ICON_FA_EXTERNAL_LINK_ALT " https://gstreamer.freedesktop.org") ) - ImGuiToolkit::OpenWebpage("https://gstreamer.freedesktop.org/"); + ImGuiToolkit::ButtonOpenWebpage("https://gstreamer.freedesktop.org/"); ImGui::SameLine(); static bool show_config_info = false; - ImGui::Checkbox("Show details " ICON_FA_CHEVRON_DOWN , &show_config_info); + ImGuiToolkit::ButtonIconToggle(10,0,13,14,&show_config_info); + ImGui::SameLine(); ImGui::Text("Details"); if (show_config_info) { ImGui::Separator(); @@ -691,11 +689,14 @@ void MainWindow::Render() { if (ImGui::BeginMenu("File")) { - if (ImGui::MenuItem( ICON_FA_FILE " New")) { - - } if (ImGui::MenuItem( ICON_FA_FILE_UPLOAD " Open", "Ctrl+O")) { UserInterface::manager().OpenFileMedia(); + } + if (ImGui::MenuItem( ICON_FA_FILE_DOWNLOAD " Save", "Ctrl+S")) { + UserInterface::manager().OpenFileMedia(); + } + if (ImGui::MenuItem( ICON_FA_FILE " Close", "Ctrl+W")) { + } if (ImGui::MenuItem( ICON_FA_POWER_OFF " Quit", "Ctrl+Q")) { Rendering::manager().Close(); @@ -736,7 +737,10 @@ void MainWindow::Render() } // content -// ImGuiToolkit::Icon(7, 1); +// static bool on; +// ImGuiToolkit::ButtonToggle("Active", &on); +// ImGui::SameLine(); +// ImGuiToolkit::ButtonIconToggle(12,11,14,1,&on); // ImGui::SameLine(0, 10); // ImGuiToolkit::PushFont(ImGuiToolkit::FONT_ITALIC); // ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) diff --git a/main.cpp b/main.cpp index d6f5340..f741f35 100644 --- a/main.cpp +++ b/main.cpp @@ -43,20 +43,6 @@ #define PI 3.14159265358979323846 -// test scene - -//// ("file:///home/bhbn/Videos/MOV001.MOD"); -//// ("file:///home/bhbn/Videos/TestFormats/Commodore64GameReviewMoondust.flv"); -//// ("file:///home/bhbn/Videos/fish.mp4"); -//// ("file:///home/bhbn/Videos/jean/Solitude1080p.mov"); -//// ("file:///home/bhbn/Videos/TearsOfSteel_720p_h265.mkv"); -//// ("file:///home/bhbn/Videos/TestFormats/_h264GoldenLamps.mkv"); -//// ("file:///home/bhbn/Videos/TestEncoding/vpxvp9high.webm"); -//// ("file:///home/bhbn/Videos/iss.mov"); -//// ("file:///home/bhbn/Videos//iss.mov"); -//// ("file:///Users/Herbelin/Movies/mp2test.mpg"); - - void drawMediaPlayer() { @@ -95,7 +81,7 @@ void drawMediaPlayer() ImGui::SameLine(0, spacing); // remember playing mode of the GUI - static bool media_playing_mode = mp->isPlaying(); + bool media_playing_mode = mp->isPlaying(); // display buttons Play/Stop depending on current playing mode if (media_playing_mode) { @@ -121,31 +107,26 @@ void drawMediaPlayer() ImGui::PopButtonRepeat(); } - ImGui::SameLine(0, spacing * 2.f); - ImGui::Dummy(ImVec2(width - 700.0, 0)); // right align + ImGui::SameLine(0, spacing * 4.f); +// ImGui::Dummy(ImVec2(width - 700.0, 0)); // right align - static int current_loop = 1; - static const char* loop_names[3] = { "Stop", "Rewind", "Bounce" }; - const char* current_loop_name = loop_names[current_loop]; - ImGui::SameLine(0, spacing); - if (current_loop == 0) ImGuiToolkit::Icon(0, 15); - else if (current_loop == 1) ImGuiToolkit::Icon(1, 15); - else ImGuiToolkit::Icon(19, 14); - ImGui::SameLine(0, spacing); - ImGui::SetNextItemWidth(90); - if ( ImGui::SliderInt("", ¤t_loop, 0, 2, current_loop_name) ) + static int current_loop = 0; + static std::vector< std::pair > iconsloop = { {0,15}, {1,15}, {19,14} }; + current_loop = (int) mp->loop(); + if ( ImGuiToolkit::ButtonIconMultistate(iconsloop, ¤t_loop) ) mp->setLoop( (MediaPlayer::LoopMode) current_loop ); float speed = static_cast(mp->playSpeed()); ImGui::SameLine(0, spacing); - ImGui::SetNextItemWidth(270); + ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - 40.0); // ImGui::SetNextItemWidth(width - 90.0); - if (ImGui::SliderFloat( "Speed", &speed, -10.f, 10.f, "x %.1f", 2.f)) + if (ImGui::DragFloat( "##Speed", &speed, 0.01f, -10.f, 10.f, "Speed x %.1f", 2.f)) mp->setPlaySpeed( static_cast(speed) ); ImGui::SameLine(0, spacing); - if (ImGuiToolkit::ButtonIcon(19, 15)) { + if (ImGuiToolkit::ButtonIcon(12, 14)) { speed = 1.f; mp->setPlaySpeed( static_cast(speed) ); + mp->setLoop( MediaPlayer::LOOP_REWIND ); } guint64 current_t = mp->position(); @@ -174,8 +155,8 @@ void drawMediaPlayer() } // display info - ImGui::Text("%s %d x %d", mp->codec().c_str(), mp->width(), mp->height()); - ImGui::Text("Framerate %.2f / %.2f", mp->updateFrameRate() , mp->frameRate() ); +// ImGui::Text("%s %d x %d", mp->codec().c_str(), mp->width(), mp->height()); +// ImGui::Text("Framerate %.2f / %.2f", mp->updateFrameRate() , mp->frameRate() ); ImGui::End(); } @@ -183,7 +164,7 @@ void drawMediaPlayer() void drawScene() { - Mixer::manager().update(); +// Mixer::manager().update(); Mixer::manager().draw(); @@ -256,8 +237,6 @@ int main(int, char**) Rendering::manager().PushFrontDrawCallback(drawScene); // init elements to the scene - //testnode3.getShader()->blending = Shader::BLEND_OPACITY; - Mixer::manager().createSourceMedia("file:///home/bhbn/Videos/iss.mov"); Mixer::manager().createSourceMedia("file:///home/bhbn/Videos/fish.mp4"); @@ -282,6 +261,8 @@ int main(int, char**) /// while ( Rendering::manager().isActive() ) { + Mixer::manager().update(); + Rendering::manager().Draw(); }