2020-12-26 14:31:24 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "glass/WindowManager.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <cstdio>
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
#include <wpigui.h>
|
|
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
WindowManager::WindowManager(std::string_view iniName)
|
2020-09-12 10:55:46 -07:00
|
|
|
: m_iniSaver{iniName, this} {}
|
|
|
|
|
|
|
|
|
|
// read/write open state to ini file
|
|
|
|
|
void* WindowManager::IniSaver::IniReadOpen(const char* name) {
|
|
|
|
|
return m_manager->GetOrAddWindow(name, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowManager::IniSaver::IniReadLine(void* entry, const char* lineStr) {
|
|
|
|
|
static_cast<Window*>(entry)->IniReadLine(lineStr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowManager::IniSaver::IniWriteAll(ImGuiTextBuffer* out_buf) {
|
|
|
|
|
const char* typeName = GetTypeName();
|
|
|
|
|
for (auto&& window : m_manager->m_windows) {
|
|
|
|
|
window->IniWriteAll(typeName, out_buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
Window* WindowManager::AddWindow(std::string_view id,
|
2020-09-12 10:55:46 -07:00
|
|
|
wpi::unique_function<void()> display) {
|
|
|
|
|
auto win = GetOrAddWindow(id, false);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!win) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
if (win->HasView()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print(stderr, "GUI: ignoring duplicate window '{}'\n", id);
|
2020-09-12 10:55:46 -07:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
win->SetView(MakeFunctionView(std::move(display)));
|
|
|
|
|
return win;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
Window* WindowManager::AddWindow(std::string_view id,
|
2020-09-12 10:55:46 -07:00
|
|
|
std::unique_ptr<View> view) {
|
|
|
|
|
auto win = GetOrAddWindow(id, false);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!win) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
if (win->HasView()) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print(stderr, "GUI: ignoring duplicate window '{}'\n", id);
|
2020-09-12 10:55:46 -07:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
win->SetView(std::move(view));
|
|
|
|
|
return win;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
Window* WindowManager::GetOrAddWindow(std::string_view id, bool duplicateOk) {
|
2020-09-12 10:55:46 -07:00
|
|
|
// binary search
|
|
|
|
|
auto it = std::lower_bound(
|
|
|
|
|
m_windows.begin(), m_windows.end(), id,
|
2021-06-06 16:13:58 -07:00
|
|
|
[](const auto& elem, std::string_view s) { return elem->GetId() < s; });
|
2020-09-12 10:55:46 -07:00
|
|
|
if (it != m_windows.end() && (*it)->GetId() == id) {
|
|
|
|
|
if (!duplicateOk) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::print(stderr, "GUI: ignoring duplicate window '{}'\n", id);
|
2020-09-12 10:55:46 -07:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return it->get();
|
|
|
|
|
}
|
|
|
|
|
// insert before (keeps sort)
|
|
|
|
|
return m_windows.emplace(it, std::make_unique<Window>(id))->get();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
Window* WindowManager::GetWindow(std::string_view id) {
|
2020-09-12 10:55:46 -07:00
|
|
|
// binary search
|
|
|
|
|
auto it = std::lower_bound(
|
|
|
|
|
m_windows.begin(), m_windows.end(), id,
|
2021-06-06 16:13:58 -07:00
|
|
|
[](const auto& elem, std::string_view s) { return elem->GetId() < s; });
|
2020-12-28 12:58:06 -08:00
|
|
|
if (it == m_windows.end() || (*it)->GetId() != id) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
return it->get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowManager::GlobalInit() {
|
|
|
|
|
wpi::gui::AddInit([this] { m_iniSaver.Initialize(); });
|
|
|
|
|
wpi::gui::AddWindowScaler([this](float scale) {
|
|
|
|
|
// scale default window positions
|
|
|
|
|
for (auto&& window : m_windows) {
|
|
|
|
|
window->ScaleDefault(scale);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
wpi::gui::AddLateExecute([this] { DisplayWindows(); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowManager::DisplayMenu() {
|
|
|
|
|
for (auto&& window : m_windows) {
|
|
|
|
|
window->DisplayMenuItem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowManager::DisplayWindows() {
|
|
|
|
|
for (auto&& window : m_windows) {
|
|
|
|
|
window->Display();
|
|
|
|
|
}
|
|
|
|
|
}
|