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/other/DeviceTree.h"
|
|
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
|
|
#include <imgui.h>
|
2023-09-17 20:00:16 -07:00
|
|
|
#include <wpi/StringExtras.h>
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
#include "glass/Context.h"
|
|
|
|
|
#include "glass/ContextInternal.h"
|
|
|
|
|
#include "glass/DataSource.h"
|
|
|
|
|
|
|
|
|
|
using namespace glass;
|
|
|
|
|
|
|
|
|
|
void DeviceTreeModel::Update() {
|
|
|
|
|
for (auto&& display : m_displays) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (display.first) {
|
|
|
|
|
display.first->Update();
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceTreeModel::Exists() {
|
|
|
|
|
for (auto&& display : m_displays) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (display.first && display.first->Exists()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DeviceTreeModel::Display() {
|
|
|
|
|
for (auto&& display : m_displays) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (display.second) {
|
|
|
|
|
display.second(display.first);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void glass::HideDevice(const char* id) {
|
|
|
|
|
gContext->deviceHidden[id] = true;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
bool glass::BeginDevice(const char* id, ImGuiTreeNodeFlags flags) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (gContext->deviceHidden[id]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
PushID(id);
|
|
|
|
|
|
|
|
|
|
// build label
|
[glass] Use JSON files for storage instead of imgui ini
Storage is now nested.
Separate "roots" can be configured which save to separate files.
In particular, this is used to save wpigui and ImGui window position
to a -window.json file.
ImGui's ini (for window position) is mapped to JSON.
You can optionally specify a directory to load from on the command line.
If one isn't provided, it uses the global system directory.
Any changes made are automatically saved here.
Workspace | Open: select directory, the current layout is replaced with that
workspace, and future auto-saves also switch to that location. The main
window size/location is not changed, only the contents.
Workspace | Save As: select directory, the current layout is saved there,
and future auto-saves also switch to that location.
Workspace | Reset: window locations are preserved, but all other settings
are reset to default (including e.g. removing plot windows). This will also
end up clearing the current save file. as with load, the main window
size/location is not changed.
Workspace | Save As Global: "save as" to the global system location
Notably, the main window size/location is only loaded at startup, but is
auto-saved as part of the current workspace.
2021-11-25 00:51:00 -08:00
|
|
|
std::string& name = GetStorage().GetString("name");
|
2020-09-12 10:55:46 -07:00
|
|
|
char label[128];
|
2023-09-17 20:00:16 -07:00
|
|
|
if (name.empty()) {
|
|
|
|
|
wpi::format_to_n_c_str(label, sizeof(label), "{}###header", id);
|
|
|
|
|
} else {
|
|
|
|
|
wpi::format_to_n_c_str(label, sizeof(label), "{}###header", name);
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
bool open = CollapsingHeader(label, flags);
|
2021-12-19 07:35:12 -08:00
|
|
|
PopupEditName("header", &name);
|
2020-09-12 10:55:46 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!open) {
|
|
|
|
|
PopID();
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
return open;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void glass::EndDevice() {
|
|
|
|
|
PopID();
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
static bool DeviceBooleanImpl(const char* name, bool readonly, bool* value) {
|
|
|
|
|
if (readonly) {
|
|
|
|
|
ImGui::LabelText(name, "%s", *value ? "true" : "false");
|
|
|
|
|
} else {
|
|
|
|
|
static const char* boolOptions[] = {"false", "true"};
|
|
|
|
|
int val = *value ? 1 : 0;
|
|
|
|
|
if (ImGui::Combo(name, &val, boolOptions, 2)) {
|
|
|
|
|
*value = val;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool DeviceDoubleImpl(const char* name, bool readonly, double* value) {
|
|
|
|
|
if (readonly) {
|
|
|
|
|
ImGui::LabelText(name, "%.6f", *value);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return ImGui::InputDouble(name, value, 0, 0, "%.6f",
|
|
|
|
|
ImGuiInputTextFlags_EnterReturnsTrue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool DeviceEnumImpl(const char* name, bool readonly, int* value,
|
|
|
|
|
const char** options, int32_t numOptions) {
|
|
|
|
|
if (readonly) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (*value < 0 || *value >= numOptions) {
|
2020-09-12 10:55:46 -07:00
|
|
|
ImGui::LabelText(name, "%d (unknown)", *value);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2020-09-12 10:55:46 -07:00
|
|
|
ImGui::LabelText(name, "%s", options[*value]);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return ImGui::Combo(name, value, options, numOptions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool DeviceIntImpl(const char* name, bool readonly, int32_t* value) {
|
|
|
|
|
if (readonly) {
|
|
|
|
|
ImGui::LabelText(name, "%" PRId32, *value);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return ImGui::InputScalar(name, ImGuiDataType_S32, value, nullptr, nullptr,
|
|
|
|
|
nullptr, ImGuiInputTextFlags_EnterReturnsTrue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool DeviceLongImpl(const char* name, bool readonly, int64_t* value) {
|
|
|
|
|
if (readonly) {
|
|
|
|
|
ImGui::LabelText(name, "%" PRId64, *value);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return ImGui::InputScalar(name, ImGuiDataType_S64, value, nullptr, nullptr,
|
|
|
|
|
nullptr, ImGuiInputTextFlags_EnterReturnsTrue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename F, typename... Args>
|
|
|
|
|
static inline bool DeviceValueImpl(const char* name, bool readonly,
|
|
|
|
|
const DataSource* source, F&& func,
|
|
|
|
|
Args... args) {
|
|
|
|
|
ImGui::SetNextItemWidth(ImGui::GetWindowWidth() * 0.5f);
|
|
|
|
|
if (!source) {
|
|
|
|
|
return func(name, readonly, args...);
|
|
|
|
|
} else {
|
|
|
|
|
ImGui::PushID(name);
|
|
|
|
|
bool rv = func("", readonly, args...);
|
|
|
|
|
ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
|
|
|
|
|
ImGui::Selectable(name);
|
|
|
|
|
source->EmitDrag();
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool glass::DeviceBoolean(const char* name, bool readonly, bool* value,
|
|
|
|
|
const DataSource* source) {
|
|
|
|
|
return DeviceValueImpl(name, readonly, source, DeviceBooleanImpl, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool glass::DeviceDouble(const char* name, bool readonly, double* value,
|
|
|
|
|
const DataSource* source) {
|
|
|
|
|
return DeviceValueImpl(name, readonly, source, DeviceDoubleImpl, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool glass::DeviceEnum(const char* name, bool readonly, int* value,
|
|
|
|
|
const char** options, int32_t numOptions,
|
|
|
|
|
const DataSource* source) {
|
|
|
|
|
return DeviceValueImpl(name, readonly, source, DeviceEnumImpl, value, options,
|
|
|
|
|
numOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool glass::DeviceInt(const char* name, bool readonly, int32_t* value,
|
|
|
|
|
const DataSource* source) {
|
|
|
|
|
return DeviceValueImpl(name, readonly, source, DeviceIntImpl, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool glass::DeviceLong(const char* name, bool readonly, int64_t* value,
|
|
|
|
|
const DataSource* source) {
|
|
|
|
|
return DeviceValueImpl(name, readonly, source, DeviceLongImpl, value);
|
|
|
|
|
}
|