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.
|
2019-09-23 00:24:10 -07:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
#include "glass/support/ExtraGuiWidgets.h"
|
2019-09-23 00:24:10 -07:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
|
|
|
|
#include <imgui_internal.h>
|
2020-08-14 20:02:35 -07:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
#include "glass/DataSource.h"
|
2019-09-23 00:24:10 -07:00
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
namespace glass {
|
|
|
|
|
|
|
|
|
|
void DrawLEDSources(const int* values, DataSource** sources, int numValues,
|
2020-08-14 20:02:35 -07:00
|
|
|
int cols, const ImU32* colors, float size, float spacing,
|
|
|
|
|
const LEDConfig& config) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (numValues == 0 || cols < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (size == 0) {
|
|
|
|
|
size = ImGui::GetFontSize() / 2.0;
|
|
|
|
|
}
|
|
|
|
|
if (spacing == 0) {
|
|
|
|
|
spacing = ImGui::GetFontSize() / 3.0;
|
|
|
|
|
}
|
2019-09-23 00:24:10 -07:00
|
|
|
|
2019-12-01 21:28:02 -08:00
|
|
|
int rows = (numValues + cols - 1) / cols;
|
|
|
|
|
float inc = size + spacing;
|
|
|
|
|
|
2019-09-23 00:24:10 -07:00
|
|
|
ImDrawList* drawList = ImGui::GetWindowDrawList();
|
|
|
|
|
const ImVec2 p = ImGui::GetCursorScreenPos();
|
2019-12-01 21:28:02 -08:00
|
|
|
|
2020-08-14 20:02:35 -07:00
|
|
|
float sized2 = size / 2;
|
2019-12-01 21:28:02 -08:00
|
|
|
float ystart, yinc;
|
|
|
|
|
if (config.start & 1) {
|
|
|
|
|
// lower
|
2020-08-14 20:02:35 -07:00
|
|
|
ystart = p.y + sized2 + inc * (rows - 1);
|
2019-12-01 21:28:02 -08:00
|
|
|
yinc = -inc;
|
|
|
|
|
} else {
|
|
|
|
|
// upper
|
2020-08-14 20:02:35 -07:00
|
|
|
ystart = p.y + sized2;
|
2019-12-01 21:28:02 -08:00
|
|
|
yinc = inc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float xstart, xinc;
|
|
|
|
|
if (config.start & 2) {
|
|
|
|
|
// right
|
2020-08-14 20:02:35 -07:00
|
|
|
xstart = p.x + sized2 + inc * (cols - 1);
|
2019-12-01 21:28:02 -08:00
|
|
|
xinc = -inc;
|
|
|
|
|
} else {
|
|
|
|
|
// left
|
2020-08-14 20:02:35 -07:00
|
|
|
xstart = p.x + sized2;
|
2019-12-01 21:28:02 -08:00
|
|
|
xinc = inc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float x = xstart, y = ystart;
|
2020-08-14 20:02:35 -07:00
|
|
|
int rowcol = 1; // row for row-major, column for column-major
|
|
|
|
|
for (int i = 0; i < numValues; ++i) {
|
|
|
|
|
if (config.order == LEDConfig::RowMajor) {
|
|
|
|
|
if (i >= (rowcol * cols)) {
|
|
|
|
|
++rowcol;
|
2019-12-01 21:28:02 -08:00
|
|
|
if (config.serpentine) {
|
|
|
|
|
x -= xinc;
|
|
|
|
|
xinc = -xinc;
|
|
|
|
|
} else {
|
|
|
|
|
x = xstart;
|
|
|
|
|
}
|
|
|
|
|
y += yinc;
|
|
|
|
|
}
|
2020-08-14 20:02:35 -07:00
|
|
|
} else {
|
|
|
|
|
if (i >= (rowcol * rows)) {
|
|
|
|
|
++rowcol;
|
2019-12-01 21:28:02 -08:00
|
|
|
if (config.serpentine) {
|
|
|
|
|
y -= yinc;
|
|
|
|
|
yinc = -yinc;
|
|
|
|
|
} else {
|
|
|
|
|
y = ystart;
|
|
|
|
|
}
|
|
|
|
|
x += xinc;
|
|
|
|
|
}
|
2020-08-14 20:02:35 -07:00
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
if (values[i] > 0) {
|
2020-08-14 20:02:35 -07:00
|
|
|
drawList->AddRectFilled(ImVec2(x, y), ImVec2(x + size, y + size),
|
|
|
|
|
colors[values[i] - 1]);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else if (values[i] < 0) {
|
2020-08-14 20:02:35 -07:00
|
|
|
drawList->AddRect(ImVec2(x, y), ImVec2(x + size, y + size),
|
|
|
|
|
colors[-values[i] - 1], 0.0f, 0, 1.0);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-08-14 20:02:35 -07:00
|
|
|
if (sources) {
|
|
|
|
|
ImGui::SetCursorScreenPos(ImVec2(x - sized2, y - sized2));
|
|
|
|
|
if (sources[i]) {
|
|
|
|
|
ImGui::PushID(i);
|
|
|
|
|
ImGui::Selectable("", false, 0, ImVec2(inc, inc));
|
|
|
|
|
sources[i]->EmitDrag();
|
|
|
|
|
ImGui::PopID();
|
|
|
|
|
} else {
|
|
|
|
|
ImGui::Dummy(ImVec2(inc, inc));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (config.order == LEDConfig::RowMajor) {
|
|
|
|
|
x += xinc;
|
|
|
|
|
} else {
|
2019-12-01 21:28:02 -08:00
|
|
|
y += yinc;
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-12-01 21:28:02 -08:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!sources) {
|
|
|
|
|
ImGui::Dummy(ImVec2(inc * cols, inc * rows));
|
|
|
|
|
}
|
2020-08-14 20:02:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DrawLEDs(const int* values, int numValues, int cols, const ImU32* colors,
|
|
|
|
|
float size, float spacing, const LEDConfig& config) {
|
|
|
|
|
DrawLEDSources(values, nullptr, numValues, cols, colors, size, spacing,
|
|
|
|
|
config);
|
2019-09-23 00:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-12 10:55:46 -07:00
|
|
|
bool DeleteButton(ImGuiID id, const ImVec2& pos) {
|
|
|
|
|
ImGuiContext& g = *GImGui;
|
|
|
|
|
ImGuiWindow* window = g.CurrentWindow;
|
|
|
|
|
|
|
|
|
|
// We intentionally allow interaction when clipped so that a mechanical
|
|
|
|
|
// Alt,Right,Validate sequence close a window. (this isn't the regular
|
|
|
|
|
// behavior of buttons, but it doesn't affect the user much because navigation
|
|
|
|
|
// tends to keep items visible).
|
|
|
|
|
const ImRect bb(
|
|
|
|
|
pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f);
|
|
|
|
|
bool is_clipped = !ImGui::ItemAdd(bb, id);
|
|
|
|
|
|
|
|
|
|
bool hovered, held;
|
|
|
|
|
bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (is_clipped) {
|
|
|
|
|
return pressed;
|
|
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
// Render
|
|
|
|
|
ImU32 col =
|
|
|
|
|
ImGui::GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered);
|
|
|
|
|
ImVec2 center = bb.GetCenter();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (hovered) {
|
2020-09-12 10:55:46 -07:00
|
|
|
window->DrawList->AddCircleFilled(
|
|
|
|
|
center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f), col, 12);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-09-12 10:55:46 -07:00
|
|
|
|
|
|
|
|
ImU32 cross_col = ImGui::GetColorU32(ImGuiCol_Text);
|
|
|
|
|
window->DrawList->AddCircle(center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f),
|
|
|
|
|
cross_col, 12);
|
|
|
|
|
float cross_extent = g.FontSize * 0.5f * 0.5f - 1.0f;
|
|
|
|
|
center -= ImVec2(0.5f, 0.5f);
|
|
|
|
|
window->DrawList->AddLine(center + ImVec2(+cross_extent, +cross_extent),
|
|
|
|
|
center + ImVec2(-cross_extent, -cross_extent),
|
|
|
|
|
cross_col, 1.0f);
|
|
|
|
|
window->DrawList->AddLine(center + ImVec2(+cross_extent, -cross_extent),
|
|
|
|
|
center + ImVec2(-cross_extent, +cross_extent),
|
|
|
|
|
cross_col, 1.0f);
|
|
|
|
|
|
|
|
|
|
return pressed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HeaderDeleteButton(const char* label) {
|
|
|
|
|
ImGuiWindow* window = ImGui::GetCurrentWindow();
|
|
|
|
|
ImGuiContext& g = *GImGui;
|
2021-12-03 17:23:18 -08:00
|
|
|
ImGuiLastItemData last_item_backup = g.LastItemData;
|
2020-09-12 10:55:46 -07:00
|
|
|
ImGuiID id = window->GetID(label);
|
|
|
|
|
float button_size = g.FontSize;
|
2021-12-03 17:23:18 -08:00
|
|
|
float button_x = ImMax(
|
|
|
|
|
g.LastItemData.Rect.Min.x,
|
|
|
|
|
g.LastItemData.Rect.Max.x - g.Style.FramePadding.x * 2.0f - button_size);
|
|
|
|
|
float button_y = g.LastItemData.Rect.Min.y;
|
2020-09-12 10:55:46 -07:00
|
|
|
bool rv = DeleteButton(
|
|
|
|
|
window->GetID(reinterpret_cast<void*>(static_cast<intptr_t>(id) + 1)),
|
|
|
|
|
ImVec2(button_x, button_y));
|
2021-12-03 17:23:18 -08:00
|
|
|
g.LastItemData = last_item_backup;
|
2020-09-12 10:55:46 -07:00
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace glass
|