Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include "glass/Window.h" |
| 6 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 7 | #include <imgui.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 8 | #include <imgui_internal.h> |
| 9 | #include <wpi/StringExtras.h> |
| 10 | |
| 11 | #include "glass/Context.h" |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 12 | #include "glass/Storage.h" |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 13 | #include "glass/support/ExtraGuiWidgets.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 14 | |
| 15 | using namespace glass; |
| 16 | |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 17 | Window::Window(Storage& storage, std::string_view id, |
| 18 | Visibility defaultVisibility) |
| 19 | : m_id{id}, |
| 20 | m_name{storage.GetString("name")}, |
| 21 | m_defaultName{id}, |
| 22 | m_visible{storage.GetBool("visible", defaultVisibility != kHide)}, |
| 23 | m_enabled{storage.GetBool("enabled", defaultVisibility != kDisabled)}, |
| 24 | m_defaultVisible{storage.GetValue("visible").boolDefault}, |
| 25 | m_defaultEnabled{storage.GetValue("enabled").boolDefault} {} |
| 26 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 27 | void Window::SetVisibility(Visibility visibility) { |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 28 | m_visible = visibility != kHide; |
| 29 | m_enabled = visibility != kDisabled; |
| 30 | } |
| 31 | |
| 32 | void Window::SetDefaultVisibility(Visibility visibility) { |
| 33 | m_defaultVisible = visibility != kHide; |
| 34 | m_defaultEnabled = visibility != kDisabled; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void Window::Display() { |
| 38 | if (!m_view) { |
| 39 | return; |
| 40 | } |
| 41 | if (!m_visible || !m_enabled) { |
| 42 | PushID(m_id); |
| 43 | m_view->Hidden(); |
| 44 | PopID(); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if (m_posCond != 0) { |
| 49 | ImGui::SetNextWindowPos(m_pos, m_posCond); |
| 50 | } |
| 51 | if (m_sizeCond != 0) { |
| 52 | ImGui::SetNextWindowSize(m_size, m_sizeCond); |
| 53 | } |
| 54 | if (m_setPadding) { |
| 55 | ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, m_padding); |
| 56 | } |
| 57 | |
| 58 | char label[128]; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 59 | if (m_name.empty()) { |
| 60 | wpi::format_to_n_c_str(label, sizeof(label), "{}###{}", m_defaultName, |
| 61 | m_id); |
| 62 | } else { |
| 63 | wpi::format_to_n_c_str(label, sizeof(label), "{}###{}", m_name, m_id); |
| 64 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | |
| 66 | if (Begin(label, &m_visible, m_flags)) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 67 | if (m_renamePopupEnabled || m_view->HasSettings()) { |
| 68 | bool isClicked = (ImGui::IsMouseReleased(ImGuiMouseButton_Right) && |
| 69 | ImGui::IsItemHovered()); |
| 70 | ImGuiWindow* window = ImGui::GetCurrentWindow(); |
| 71 | |
| 72 | bool settingsButtonClicked = false; |
| 73 | // Not docked, and window has just enough for the circles not to be |
| 74 | // touching |
| 75 | if (!ImGui::IsWindowDocked() && |
| 76 | ImGui::GetWindowWidth() > (ImGui::GetFontSize() + 2) * 3 + |
| 77 | ImGui::GetStyle().FramePadding.x * 2) { |
| 78 | const ImGuiItemFlags itemFlagsRestore = |
| 79 | ImGui::GetCurrentContext()->CurrentItemFlags; |
| 80 | |
| 81 | ImGui::GetCurrentContext()->CurrentItemFlags |= |
| 82 | ImGuiItemFlags_NoNavDefaultFocus; |
| 83 | window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; |
| 84 | |
| 85 | // Allow to draw outside of normal window |
| 86 | ImGui::PushClipRect(window->OuterRectClipped.Min, |
| 87 | window->OuterRectClipped.Max, false); |
| 88 | |
| 89 | const ImRect titleBarRect = ImGui::GetCurrentWindow()->TitleBarRect(); |
| 90 | const ImVec2 position = {titleBarRect.Max.x - |
| 91 | (ImGui::GetStyle().FramePadding.x * 3) - |
| 92 | (ImGui::GetFontSize() * 2), |
| 93 | titleBarRect.Min.y}; |
| 94 | settingsButtonClicked = |
| 95 | HamburgerButton(ImGui::GetID("#SETTINGS"), position); |
| 96 | |
| 97 | ImGui::PopClipRect(); |
| 98 | |
| 99 | ImGui::GetCurrentContext()->CurrentItemFlags = itemFlagsRestore; |
| 100 | } |
| 101 | if (settingsButtonClicked || isClicked) { |
| 102 | ImGui::OpenPopup(window->ID); |
| 103 | } |
| 104 | |
| 105 | if (ImGui::BeginPopupEx(window->ID, |
| 106 | ImGuiWindowFlags_AlwaysAutoResize | |
| 107 | ImGuiWindowFlags_NoTitleBar | |
| 108 | ImGuiWindowFlags_NoSavedSettings)) { |
| 109 | if (m_renamePopupEnabled) { |
| 110 | ItemEditName(&m_name); |
| 111 | } |
| 112 | m_view->Settings(); |
| 113 | |
| 114 | ImGui::EndPopup(); |
| 115 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 116 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 117 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 118 | m_view->Display(); |
| 119 | } else { |
| 120 | m_view->Hidden(); |
| 121 | } |
| 122 | End(); |
| 123 | if (m_setPadding) { |
| 124 | ImGui::PopStyleVar(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | bool Window::DisplayMenuItem(const char* label) { |
| 129 | bool wasVisible = m_visible; |
| 130 | ImGui::MenuItem( |
| 131 | label ? label : (m_name.empty() ? m_id.c_str() : m_name.c_str()), nullptr, |
| 132 | &m_visible, m_enabled); |
| 133 | return !wasVisible && m_visible; |
| 134 | } |
| 135 | |
| 136 | void Window::ScaleDefault(float scale) { |
| 137 | if ((m_posCond & ImGuiCond_FirstUseEver) != 0) { |
| 138 | m_pos.x *= scale; |
| 139 | m_pos.y *= scale; |
| 140 | } |
| 141 | if ((m_sizeCond & ImGuiCond_FirstUseEver) != 0) { |
| 142 | m_size.x *= scale; |
| 143 | m_size.y *= scale; |
| 144 | } |
| 145 | } |