blob: 6296fab046a0854dcad150ca15aa6d85a8dee857 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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"
Austin Schuh75263e32022-02-22 18:05:32 -080011#include "glass/Storage.h"
Austin Schuh812d0d12021-11-04 20:16:48 -070012
13using namespace glass;
14
Austin Schuh75263e32022-02-22 18:05:32 -080015Window::Window(Storage& storage, std::string_view id,
16 Visibility defaultVisibility)
17 : m_id{id},
18 m_name{storage.GetString("name")},
19 m_defaultName{id},
20 m_visible{storage.GetBool("visible", defaultVisibility != kHide)},
21 m_enabled{storage.GetBool("enabled", defaultVisibility != kDisabled)},
22 m_defaultVisible{storage.GetValue("visible").boolDefault},
23 m_defaultEnabled{storage.GetValue("enabled").boolDefault} {}
24
Austin Schuh812d0d12021-11-04 20:16:48 -070025void Window::SetVisibility(Visibility visibility) {
Austin Schuh75263e32022-02-22 18:05:32 -080026 m_visible = visibility != kHide;
27 m_enabled = visibility != kDisabled;
28}
29
30void Window::SetDefaultVisibility(Visibility visibility) {
31 m_defaultVisible = visibility != kHide;
32 m_defaultEnabled = visibility != kDisabled;
Austin Schuh812d0d12021-11-04 20:16:48 -070033}
34
35void Window::Display() {
36 if (!m_view) {
37 return;
38 }
39 if (!m_visible || !m_enabled) {
40 PushID(m_id);
41 m_view->Hidden();
42 PopID();
43 return;
44 }
45
46 if (m_posCond != 0) {
47 ImGui::SetNextWindowPos(m_pos, m_posCond);
48 }
49 if (m_sizeCond != 0) {
50 ImGui::SetNextWindowSize(m_size, m_sizeCond);
51 }
52 if (m_setPadding) {
53 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, m_padding);
54 }
55
56 char label[128];
57 std::snprintf(label, sizeof(label), "%s###%s",
58 m_name.empty() ? m_defaultName.c_str() : m_name.c_str(),
59 m_id.c_str());
60
61 if (Begin(label, &m_visible, m_flags)) {
62 if (m_renamePopupEnabled) {
63 PopupEditName(nullptr, &m_name);
64 }
65 m_view->Display();
66 } else {
67 m_view->Hidden();
68 }
69 End();
70 if (m_setPadding) {
71 ImGui::PopStyleVar();
72 }
73}
74
75bool Window::DisplayMenuItem(const char* label) {
76 bool wasVisible = m_visible;
77 ImGui::MenuItem(
78 label ? label : (m_name.empty() ? m_id.c_str() : m_name.c_str()), nullptr,
79 &m_visible, m_enabled);
80 return !wasVisible && m_visible;
81}
82
83void Window::ScaleDefault(float scale) {
84 if ((m_posCond & ImGuiCond_FirstUseEver) != 0) {
85 m_pos.x *= scale;
86 m_pos.y *= scale;
87 }
88 if ((m_sizeCond & ImGuiCond_FirstUseEver) != 0) {
89 m_size.x *= scale;
90 m_size.y *= scale;
91 }
92}