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 | |
| 7 | #include <imgui_internal.h> |
| 8 | #include <wpi/StringExtras.h> |
| 9 | |
| 10 | #include "glass/Context.h" |
| 11 | |
| 12 | using namespace glass; |
| 13 | |
| 14 | void Window::SetVisibility(Visibility visibility) { |
| 15 | switch (visibility) { |
| 16 | case kHide: |
| 17 | m_visible = false; |
| 18 | m_enabled = true; |
| 19 | break; |
| 20 | case kShow: |
| 21 | m_visible = true; |
| 22 | m_enabled = true; |
| 23 | break; |
| 24 | case kDisabled: |
| 25 | m_enabled = false; |
| 26 | break; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | void Window::Display() { |
| 31 | if (!m_view) { |
| 32 | return; |
| 33 | } |
| 34 | if (!m_visible || !m_enabled) { |
| 35 | PushID(m_id); |
| 36 | m_view->Hidden(); |
| 37 | PopID(); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | if (m_posCond != 0) { |
| 42 | ImGui::SetNextWindowPos(m_pos, m_posCond); |
| 43 | } |
| 44 | if (m_sizeCond != 0) { |
| 45 | ImGui::SetNextWindowSize(m_size, m_sizeCond); |
| 46 | } |
| 47 | if (m_setPadding) { |
| 48 | ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, m_padding); |
| 49 | } |
| 50 | |
| 51 | char label[128]; |
| 52 | std::snprintf(label, sizeof(label), "%s###%s", |
| 53 | m_name.empty() ? m_defaultName.c_str() : m_name.c_str(), |
| 54 | m_id.c_str()); |
| 55 | |
| 56 | if (Begin(label, &m_visible, m_flags)) { |
| 57 | if (m_renamePopupEnabled) { |
| 58 | PopupEditName(nullptr, &m_name); |
| 59 | } |
| 60 | m_view->Display(); |
| 61 | } else { |
| 62 | m_view->Hidden(); |
| 63 | } |
| 64 | End(); |
| 65 | if (m_setPadding) { |
| 66 | ImGui::PopStyleVar(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | bool Window::DisplayMenuItem(const char* label) { |
| 71 | bool wasVisible = m_visible; |
| 72 | ImGui::MenuItem( |
| 73 | label ? label : (m_name.empty() ? m_id.c_str() : m_name.c_str()), nullptr, |
| 74 | &m_visible, m_enabled); |
| 75 | return !wasVisible && m_visible; |
| 76 | } |
| 77 | |
| 78 | void Window::ScaleDefault(float scale) { |
| 79 | if ((m_posCond & ImGuiCond_FirstUseEver) != 0) { |
| 80 | m_pos.x *= scale; |
| 81 | m_pos.y *= scale; |
| 82 | } |
| 83 | if ((m_sizeCond & ImGuiCond_FirstUseEver) != 0) { |
| 84 | m_size.x *= scale; |
| 85 | m_size.y *= scale; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void Window::IniReadLine(const char* line) { |
| 90 | auto [name, value] = wpi::split(line, '='); |
| 91 | name = wpi::trim(name); |
| 92 | value = wpi::trim(value); |
| 93 | |
| 94 | if (name == "name") { |
| 95 | m_name = value; |
| 96 | } else if (name == "visible") { |
| 97 | if (auto num = wpi::parse_integer<int>(value, 10)) { |
| 98 | m_visible = num.value(); |
| 99 | } |
| 100 | } else if (name == "enabled") { |
| 101 | if (auto num = wpi::parse_integer<int>(value, 10)) { |
| 102 | m_enabled = num.value(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void Window::IniWriteAll(const char* typeName, ImGuiTextBuffer* out_buf) { |
| 108 | out_buf->appendf("[%s][%s]\nname=%s\nvisible=%d\nenabled=%d\n\n", typeName, |
| 109 | m_id.c_str(), m_name.c_str(), m_visible ? 1 : 0, |
| 110 | m_enabled ? 1 : 0); |
| 111 | } |