blob: d9bee62e2073a22d236c2fa46297dddcdc4af09d [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/DataSource.h"
6
7#include <fmt/format.h>
8
9#include "glass/ContextInternal.h"
10
11using namespace glass;
12
13wpi::sig::Signal<const char*, DataSource*> DataSource::sourceCreated;
14
Austin Schuh75263e32022-02-22 18:05:32 -080015DataSource::DataSource(std::string_view id)
16 : m_id{id}, m_name{gContext->sourceNameStorage.GetString(m_id)} {
17 gContext->sources.try_emplace(m_id, this);
Austin Schuh812d0d12021-11-04 20:16:48 -070018 sourceCreated(m_id.c_str(), this);
19}
20
21DataSource::DataSource(std::string_view id, int index)
22 : DataSource{fmt::format("{}[{}]", id, index)} {}
23
24DataSource::DataSource(std::string_view id, int index, int index2)
25 : DataSource{fmt::format("{}[{},{}]", id, index, index2)} {}
26
27DataSource::~DataSource() {
28 if (!gContext) {
29 return;
30 }
Austin Schuh75263e32022-02-22 18:05:32 -080031 gContext->sources.erase(m_id);
Austin Schuh812d0d12021-11-04 20:16:48 -070032}
33
34void DataSource::LabelText(const char* label, const char* fmt, ...) const {
35 va_list args;
36 va_start(args, fmt);
37 LabelTextV(label, fmt, args);
38 va_end(args);
39}
40
41// Add a label+text combo aligned to other label+value widgets
42void DataSource::LabelTextV(const char* label, const char* fmt,
43 va_list args) const {
44 ImGui::PushID(label);
Austin Schuh75263e32022-02-22 18:05:32 -080045 ImGui::LabelTextV("##input", fmt, args); // NOLINT
Austin Schuh812d0d12021-11-04 20:16:48 -070046 ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
47 ImGui::Selectable(label);
48 ImGui::PopID();
49 EmitDrag();
50}
51
52bool DataSource::Combo(const char* label, int* current_item,
53 const char* const items[], int items_count,
54 int popup_max_height_in_items) const {
55 ImGui::PushID(label);
56 bool rv = ImGui::Combo("##input", current_item, items, items_count,
57 popup_max_height_in_items);
58 ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
59 ImGui::Selectable(label);
60 EmitDrag();
61 ImGui::PopID();
62 return rv;
63}
64
65bool DataSource::SliderFloat(const char* label, float* v, float v_min,
66 float v_max, const char* format,
67 float power) const {
68 ImGui::PushID(label);
69 bool rv = ImGui::SliderFloat("##input", v, v_min, v_max, format, power);
70 ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
71 ImGui::Selectable(label);
72 EmitDrag();
73 ImGui::PopID();
74 return rv;
75}
76
77bool DataSource::InputDouble(const char* label, double* v, double step,
78 double step_fast, const char* format,
79 ImGuiInputTextFlags flags) const {
80 ImGui::PushID(label);
81 bool rv = ImGui::InputDouble("##input", v, step, step_fast, format, flags);
82 ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
83 ImGui::Selectable(label);
84 EmitDrag();
85 ImGui::PopID();
86 return rv;
87}
88
89bool DataSource::InputInt(const char* label, int* v, int step, int step_fast,
90 ImGuiInputTextFlags flags) const {
91 ImGui::PushID(label);
92 bool rv = ImGui::InputInt("##input", v, step, step_fast, flags);
93 ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x);
94 ImGui::Selectable(label);
95 EmitDrag();
96 ImGui::PopID();
97 return rv;
98}
99
100void DataSource::EmitDrag(ImGuiDragDropFlags flags) const {
101 if (ImGui::BeginDragDropSource(flags)) {
102 auto self = this;
103 ImGui::SetDragDropPayload("DataSource", &self, sizeof(self)); // NOLINT
Austin Schuh75263e32022-02-22 18:05:32 -0800104 const char* name = GetName().c_str();
Austin Schuh812d0d12021-11-04 20:16:48 -0700105 ImGui::TextUnformatted(name[0] == '\0' ? m_id.c_str() : name);
106 ImGui::EndDragDropSource();
107 }
108}
109
110DataSource* DataSource::Find(std::string_view id) {
111 auto it = gContext->sources.find(id);
112 if (it == gContext->sources.end()) {
113 return nullptr;
114 }
Austin Schuh75263e32022-02-22 18:05:32 -0800115 return it->getValue();
Austin Schuh812d0d12021-11-04 20:16:48 -0700116}