mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
Add braces to C++ single-line loops and conditionals (NFC) (#2973)
This makes code easier to read and more consistent between C++ and Java. Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
@@ -27,7 +27,9 @@ void glass::DisplayCommandScheduler(CommandSchedulerModel* m) {
|
||||
ImGui::SameLine(pos);
|
||||
|
||||
ImGui::PushID(i);
|
||||
if (ImGui::Button("Cancel")) m->CancelCommand(i);
|
||||
if (ImGui::Button("Cancel")) {
|
||||
m->CancelCommand(i);
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
using namespace glass;
|
||||
|
||||
void glass::DisplayCommandSelector(CommandSelectorModel* m) {
|
||||
if (auto name = m->GetName()) ImGui::Text("%s", name);
|
||||
if (auto name = m->GetName()) {
|
||||
ImGui::Text("%s", name);
|
||||
}
|
||||
if (m->Exists()) {
|
||||
if (auto run = m->GetRunningData()) {
|
||||
bool running = run->GetValue();
|
||||
@@ -21,7 +23,9 @@ void glass::DisplayCommandSelector(CommandSelectorModel* m) {
|
||||
m->SetRunning(running);
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (running) ImGui::Text("Running...");
|
||||
if (running) {
|
||||
ImGui::Text("Running...");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
||||
|
||||
@@ -16,27 +16,37 @@ using namespace glass;
|
||||
|
||||
void DeviceTreeModel::Update() {
|
||||
for (auto&& display : m_displays) {
|
||||
if (display.first) display.first->Update();
|
||||
if (display.first) {
|
||||
display.first->Update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool DeviceTreeModel::Exists() {
|
||||
for (auto&& display : m_displays) {
|
||||
if (display.first && display.first->Exists()) return true;
|
||||
if (display.first && display.first->Exists()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DeviceTreeModel::Display() {
|
||||
for (auto&& display : m_displays) {
|
||||
if (display.second) display.second(display.first);
|
||||
if (display.second) {
|
||||
display.second(display.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void glass::HideDevice(const char* id) { gContext->deviceHidden[id] = true; }
|
||||
void glass::HideDevice(const char* id) {
|
||||
gContext->deviceHidden[id] = true;
|
||||
}
|
||||
|
||||
bool glass::BeginDevice(const char* id, ImGuiTreeNodeFlags flags) {
|
||||
if (gContext->deviceHidden[id]) return false;
|
||||
if (gContext->deviceHidden[id]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PushID(id);
|
||||
|
||||
@@ -49,11 +59,15 @@ bool glass::BeginDevice(const char* id, ImGuiTreeNodeFlags flags) {
|
||||
bool open = CollapsingHeader(label, flags);
|
||||
PopupEditName("name", name);
|
||||
|
||||
if (!open) PopID();
|
||||
if (!open) {
|
||||
PopID();
|
||||
}
|
||||
return open;
|
||||
}
|
||||
|
||||
void glass::EndDevice() { PopID(); }
|
||||
void glass::EndDevice() {
|
||||
PopID();
|
||||
}
|
||||
|
||||
static bool DeviceBooleanImpl(const char* name, bool readonly, bool* value) {
|
||||
if (readonly) {
|
||||
@@ -82,10 +96,11 @@ static bool DeviceDoubleImpl(const char* name, bool readonly, double* value) {
|
||||
static bool DeviceEnumImpl(const char* name, bool readonly, int* value,
|
||||
const char** options, int32_t numOptions) {
|
||||
if (readonly) {
|
||||
if (*value < 0 || *value >= numOptions)
|
||||
if (*value < 0 || *value >= numOptions) {
|
||||
ImGui::LabelText(name, "%d (unknown)", *value);
|
||||
else
|
||||
} else {
|
||||
ImGui::LabelText(name, "%s", options[*value]);
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return ImGui::Combo(name, value, options, numOptions);
|
||||
|
||||
@@ -15,19 +15,25 @@ static const char* stations[] = {"Red 1", "Red 2", "Red 3",
|
||||
"Blue 1", "Blue 2", "Blue 3"};
|
||||
|
||||
void glass::DisplayFMS(FMSModel* model, bool* matchTimeEnabled) {
|
||||
if (!model->Exists() || model->IsReadOnly()) return DisplayFMSReadOnly(model);
|
||||
if (!model->Exists() || model->IsReadOnly()) {
|
||||
return DisplayFMSReadOnly(model);
|
||||
}
|
||||
|
||||
// FMS Attached
|
||||
if (auto data = model->GetFmsAttachedData()) {
|
||||
bool val = data->GetValue();
|
||||
if (ImGui::Checkbox("FMS Attached", &val)) model->SetFmsAttached(val);
|
||||
if (ImGui::Checkbox("FMS Attached", &val)) {
|
||||
model->SetFmsAttached(val);
|
||||
}
|
||||
data->EmitDrag();
|
||||
}
|
||||
|
||||
// DS Attached
|
||||
if (auto data = model->GetDsAttachedData()) {
|
||||
bool val = data->GetValue();
|
||||
if (ImGui::Checkbox("DS Attached", &val)) model->SetDsAttached(val);
|
||||
if (ImGui::Checkbox("DS Attached", &val)) {
|
||||
model->SetDsAttached(val);
|
||||
}
|
||||
data->EmitDrag();
|
||||
}
|
||||
|
||||
@@ -35,15 +41,17 @@ void glass::DisplayFMS(FMSModel* model, bool* matchTimeEnabled) {
|
||||
if (auto data = model->GetAllianceStationIdData()) {
|
||||
int val = data->GetValue();
|
||||
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
|
||||
if (ImGui::Combo("Alliance Station", &val, stations, 6))
|
||||
if (ImGui::Combo("Alliance Station", &val, stations, 6)) {
|
||||
model->SetAllianceStationId(val);
|
||||
}
|
||||
data->EmitDrag();
|
||||
}
|
||||
|
||||
// Match Time
|
||||
if (auto data = model->GetMatchTimeData()) {
|
||||
if (matchTimeEnabled)
|
||||
if (matchTimeEnabled) {
|
||||
ImGui::Checkbox("Match Time Enabled", matchTimeEnabled);
|
||||
}
|
||||
|
||||
double val = data->GetValue();
|
||||
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
|
||||
@@ -74,7 +82,9 @@ void glass::DisplayFMS(FMSModel* model, bool* matchTimeEnabled) {
|
||||
|
||||
void glass::DisplayFMSReadOnly(FMSModel* model) {
|
||||
bool exists = model->Exists();
|
||||
if (!exists) ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
||||
if (!exists) {
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(96, 96, 96, 255));
|
||||
}
|
||||
|
||||
if (auto data = model->GetEStopData()) {
|
||||
ImGui::Selectable("E-Stopped: ");
|
||||
@@ -123,15 +133,18 @@ void glass::DisplayFMSReadOnly(FMSModel* model) {
|
||||
ImGui::Selectable("Match Time: ");
|
||||
data->EmitDrag();
|
||||
ImGui::SameLine();
|
||||
if (exists)
|
||||
if (exists) {
|
||||
ImGui::Text("%.1f", data->GetValue());
|
||||
else
|
||||
} else {
|
||||
ImGui::TextUnformatted("?");
|
||||
}
|
||||
}
|
||||
|
||||
wpi::SmallString<64> gameSpecificMessage;
|
||||
model->GetGameSpecificMessage(gameSpecificMessage);
|
||||
ImGui::Text("Game Specific: %s", exists ? gameSpecificMessage.c_str() : "?");
|
||||
|
||||
if (!exists) ImGui::PopStyleColor();
|
||||
if (!exists) {
|
||||
ImGui::PopStyleColor();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,7 +182,9 @@ void FieldInfo::LoadImage() {
|
||||
m_fileOpener.reset();
|
||||
}
|
||||
if (!m_texture && !m_pFilename->empty()) {
|
||||
if (!LoadImageImpl(m_pFilename->c_str())) m_pFilename->clear();
|
||||
if (!LoadImageImpl(m_pFilename->c_str())) {
|
||||
m_pFilename->clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,7 +266,9 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
|
||||
wpi::sys::path::append(pathname, image);
|
||||
|
||||
// load field image
|
||||
if (!LoadImageImpl(pathname.c_str())) return;
|
||||
if (!LoadImageImpl(pathname.c_str())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// save to field info
|
||||
*m_pFilename = pathname.str();
|
||||
@@ -292,8 +296,9 @@ bool FieldInfo::LoadImageImpl(const char* fn) {
|
||||
|
||||
FieldFrameData FieldInfo::GetFrameData(ImVec2 min, ImVec2 max) const {
|
||||
// fit the image into the window
|
||||
if (m_texture && m_imageHeight != 0 && m_imageWidth != 0)
|
||||
if (m_texture && m_imageHeight != 0 && m_imageWidth != 0) {
|
||||
gui::MaxFit(&min, &max, m_imageWidth, m_imageHeight);
|
||||
}
|
||||
|
||||
FieldFrameData ffd;
|
||||
ffd.imageMin = min;
|
||||
@@ -340,11 +345,15 @@ void ObjectGroupInfo::Reset() {
|
||||
void ObjectGroupInfo::LoadImage() {
|
||||
if (m_fileOpener && m_fileOpener->ready(0)) {
|
||||
auto result = m_fileOpener->result();
|
||||
if (!result.empty()) LoadImageImpl(result[0].c_str());
|
||||
if (!result.empty()) {
|
||||
LoadImageImpl(result[0].c_str());
|
||||
}
|
||||
m_fileOpener.reset();
|
||||
}
|
||||
if (!m_texture && !m_pFilename->empty()) {
|
||||
if (!LoadImageImpl(m_pFilename->c_str())) m_pFilename->clear();
|
||||
if (!LoadImageImpl(m_pFilename->c_str())) {
|
||||
m_pFilename->clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,10 +377,15 @@ ObjectFrameData::ObjectFrameData(FieldObjectModel& model,
|
||||
m_width2(ffd.scale * width / 2),
|
||||
m_length2(ffd.scale * length / 2),
|
||||
m_hitRadius((std::min)(m_width2, m_length2) / 2) {
|
||||
if (auto xData = model.GetXData()) m_x = xData->GetValue();
|
||||
if (auto yData = model.GetYData()) m_y = yData->GetValue();
|
||||
if (auto rotationData = model.GetRotationData())
|
||||
if (auto xData = model.GetXData()) {
|
||||
m_x = xData->GetValue();
|
||||
}
|
||||
if (auto yData = model.GetYData()) {
|
||||
m_y = yData->GetValue();
|
||||
}
|
||||
if (auto rotationData = model.GetRotationData()) {
|
||||
m_rot = rotationData->GetValue();
|
||||
}
|
||||
UpdateFrameData();
|
||||
}
|
||||
|
||||
@@ -418,22 +432,25 @@ int ObjectFrameData::IsHovered(const ImVec2& cursor) const {
|
||||
// only allow initiation of dragging when invisible button is hovered;
|
||||
// this prevents the window resize handles from simultaneously activating
|
||||
// the drag functionality
|
||||
if (!ImGui::IsItemHovered()) return 0;
|
||||
if (!ImGui::IsItemHovered()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float hitRadiusSquared = m_hitRadius * m_hitRadius;
|
||||
// it's within the hit radius of the center?
|
||||
if (gui::GetDistSquared(cursor, m_center) < hitRadiusSquared)
|
||||
if (gui::GetDistSquared(cursor, m_center) < hitRadiusSquared) {
|
||||
return 1;
|
||||
else if (gui::GetDistSquared(cursor, m_corners[0]) < hitRadiusSquared)
|
||||
} else if (gui::GetDistSquared(cursor, m_corners[0]) < hitRadiusSquared) {
|
||||
return 2;
|
||||
else if (gui::GetDistSquared(cursor, m_corners[1]) < hitRadiusSquared)
|
||||
} else if (gui::GetDistSquared(cursor, m_corners[1]) < hitRadiusSquared) {
|
||||
return 3;
|
||||
else if (gui::GetDistSquared(cursor, m_corners[2]) < hitRadiusSquared)
|
||||
} else if (gui::GetDistSquared(cursor, m_corners[2]) < hitRadiusSquared) {
|
||||
return 4;
|
||||
else if (gui::GetDistSquared(cursor, m_corners[3]) < hitRadiusSquared)
|
||||
} else if (gui::GetDistSquared(cursor, m_corners[3]) < hitRadiusSquared) {
|
||||
return 5;
|
||||
else
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool ObjectFrameData::HandleDrag(const ImVec2& cursor, int hitCorner,
|
||||
@@ -525,10 +542,14 @@ void glass::DisplayField2DSettings(Field2DModel* model) {
|
||||
}
|
||||
|
||||
model->ForEachFieldObjectGroup([&](auto& groupModel, auto name) {
|
||||
if (!groupModel.Exists()) return;
|
||||
if (!groupModel.Exists()) {
|
||||
return;
|
||||
}
|
||||
PushID(name);
|
||||
auto& objGroupRef = field->m_objectGroups[name];
|
||||
if (!objGroupRef) objGroupRef = std::make_unique<ObjectGroupInfo>();
|
||||
if (!objGroupRef) {
|
||||
objGroupRef = std::make_unique<ObjectGroupInfo>();
|
||||
}
|
||||
auto objGroup = objGroupRef.get();
|
||||
|
||||
wpi::SmallString<64> nameBuf = name;
|
||||
@@ -565,7 +586,9 @@ void glass::DisplayField2D(Field2DModel* model, const ImVec2& contentSize) {
|
||||
|
||||
// for dragging to work, there needs to be a button (otherwise the window is
|
||||
// dragged)
|
||||
if (contentSize.x <= 0 || contentSize.y <= 0) return;
|
||||
if (contentSize.x <= 0 || contentSize.y <= 0) {
|
||||
return;
|
||||
}
|
||||
ImVec2 cursorPos = windowPos + ImGui::GetCursorPos(); // screen coords
|
||||
ImGui::InvisibleButton("field", contentSize);
|
||||
|
||||
@@ -576,10 +599,14 @@ void glass::DisplayField2D(Field2DModel* model, const ImVec2& contentSize) {
|
||||
field->Draw(drawList, ffd);
|
||||
|
||||
model->ForEachFieldObjectGroup([&](auto& groupModel, auto name) {
|
||||
if (!groupModel.Exists()) return;
|
||||
if (!groupModel.Exists()) {
|
||||
return;
|
||||
}
|
||||
PushID(name);
|
||||
auto& objGroupRef = field->m_objectGroups[name];
|
||||
if (!objGroupRef) objGroupRef = std::make_unique<ObjectGroupInfo>();
|
||||
if (!objGroupRef) {
|
||||
objGroupRef = std::make_unique<ObjectGroupInfo>();
|
||||
}
|
||||
auto objGroup = objGroupRef.get();
|
||||
objGroup->LoadImage();
|
||||
|
||||
@@ -593,8 +620,9 @@ void glass::DisplayField2D(Field2DModel* model, const ImVec2& contentSize) {
|
||||
if (objGroup->m_dragState.object == 0 ||
|
||||
objGroup->m_dragState.object == i) {
|
||||
hitCorner = ofd.IsHovered(mousePos);
|
||||
if (ofd.HandleDrag(mousePos, hitCorner, &objGroup->m_dragState))
|
||||
if (ofd.HandleDrag(mousePos, hitCorner, &objGroup->m_dragState)) {
|
||||
objGroup->m_dragState.object = i;
|
||||
}
|
||||
}
|
||||
|
||||
// draw
|
||||
|
||||
@@ -23,7 +23,9 @@ void glass::DisplayPIDController(PIDControllerModel* m) {
|
||||
auto createTuningParameter = [](const char* name, double* v,
|
||||
std::function<void(double)> callback) {
|
||||
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 4);
|
||||
if (ImGui::InputDouble(name, v, 0.0, 0.0, "%.3f")) callback(*v);
|
||||
if (ImGui::InputDouble(name, v, 0.0, 0.0, "%.3f")) {
|
||||
callback(*v);
|
||||
}
|
||||
};
|
||||
|
||||
if (auto p = m->GetPData()) {
|
||||
|
||||
@@ -237,17 +237,23 @@ bool PlotSeries::ReadIni(wpi::StringRef name, wpi::StringRef value) {
|
||||
}
|
||||
if (name == "yAxis") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_yAxis = num;
|
||||
return true;
|
||||
} else if (name == "color") {
|
||||
unsigned int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_color = ImColor(num);
|
||||
return true;
|
||||
} else if (name == "marker") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_marker = num;
|
||||
return true;
|
||||
} else if (name == "weight") {
|
||||
@@ -255,17 +261,23 @@ bool PlotSeries::ReadIni(wpi::StringRef name, wpi::StringRef value) {
|
||||
return true;
|
||||
} else if (name == "digital") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_digital = num;
|
||||
return true;
|
||||
} else if (name == "digitalBitHeight") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_digitalBitHeight = num;
|
||||
return true;
|
||||
} else if (name == "digitalBitGap") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_digitalBitGap = num;
|
||||
return true;
|
||||
}
|
||||
@@ -281,10 +293,14 @@ void PlotSeries::WriteIni(ImGuiTextBuffer* out) {
|
||||
}
|
||||
|
||||
const char* PlotSeries::GetName() const {
|
||||
if (!m_name.empty()) return m_name.c_str();
|
||||
if (!m_name.empty()) {
|
||||
return m_name.c_str();
|
||||
}
|
||||
if (m_newValueConn.connected()) {
|
||||
auto sourceName = m_source->GetName();
|
||||
if (sourceName[0] != '\0') return sourceName;
|
||||
if (sourceName[0] != '\0') {
|
||||
return sourceName;
|
||||
}
|
||||
}
|
||||
return m_id.c_str();
|
||||
}
|
||||
@@ -311,19 +327,23 @@ PlotSeries::Action PlotSeries::EmitPlot(PlotView& view, double now, size_t i,
|
||||
GetterData getterData = {now, GetZeroTime() * 1.0e-6, m_data, size, offset};
|
||||
auto getter = [](void* data, int idx) {
|
||||
auto d = static_cast<GetterData*>(data);
|
||||
if (idx == d->size)
|
||||
if (idx == d->size) {
|
||||
return ImPlotPoint{
|
||||
d->now - d->zeroTime,
|
||||
d->data[d->offset == 0 ? d->size - 1 : d->offset - 1].y};
|
||||
}
|
||||
ImPlotPoint* point;
|
||||
if (d->offset + idx < d->size)
|
||||
if (d->offset + idx < d->size) {
|
||||
point = &d->data[d->offset + idx];
|
||||
else
|
||||
} else {
|
||||
point = &d->data[d->offset + idx - d->size];
|
||||
}
|
||||
return ImPlotPoint{point->x - d->zeroTime, point->y};
|
||||
};
|
||||
|
||||
if (m_color.w == IMPLOT_AUTO_COL.w) m_color = ImPlot::GetColormapColor(i);
|
||||
if (m_color.w == IMPLOT_AUTO_COL.w) {
|
||||
m_color = ImPlot::GetColormapColor(i);
|
||||
}
|
||||
ImPlot::SetNextLineStyle(m_color, m_weight);
|
||||
if (IsDigital()) {
|
||||
ImPlot::PushStyleVar(ImPlotStyleVar_DigitalBitHeight, m_digitalBitHeight);
|
||||
@@ -346,7 +366,9 @@ PlotSeries::Action PlotSeries::EmitPlot(PlotView& view, double now, size_t i,
|
||||
// Edit settings via popup
|
||||
Action rv = kNone;
|
||||
if (ImPlot::BeginLegendPopup(label)) {
|
||||
if (ImGui::Button("Close")) ImGui::CloseCurrentPopup();
|
||||
if (ImGui::Button("Close")) {
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
ImGui::Text("Edit series name:");
|
||||
ImGui::InputText("##editname", &m_name);
|
||||
if (ImGui::Button("Move Up")) {
|
||||
@@ -382,7 +404,9 @@ void PlotSeries::EmitSettings(size_t i) {
|
||||
{
|
||||
ImGui::ColorEdit3("Color", &m_color.x, ImGuiColorEditFlags_NoInputs);
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Default")) m_color = ImPlot::GetColormapColor(i);
|
||||
if (ImGui::Button("Default")) {
|
||||
m_color = ImPlot::GetColormapColor(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Line weight
|
||||
@@ -443,76 +467,107 @@ bool Plot::ReadIni(wpi::StringRef name, wpi::StringRef value) {
|
||||
return true;
|
||||
} else if (name == "visible") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_visible = num != 0;
|
||||
return true;
|
||||
} else if (name == "showPause") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_showPause = num != 0;
|
||||
return true;
|
||||
} else if (name == "lockPrevX") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_lockPrevX = num != 0;
|
||||
return true;
|
||||
} else if (name == "legend") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (num == 0)
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
if (num == 0) {
|
||||
m_plotFlags &= ~ImPlotFlags_Legend;
|
||||
else
|
||||
} else {
|
||||
m_plotFlags |= ImPlotFlags_Legend;
|
||||
}
|
||||
return true;
|
||||
} else if (name == "yaxis2") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (num == 0)
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
if (num == 0) {
|
||||
m_plotFlags &= ~ImPlotFlags_YAxis2;
|
||||
else
|
||||
} else {
|
||||
m_plotFlags |= ImPlotFlags_YAxis2;
|
||||
}
|
||||
return true;
|
||||
} else if (name == "yaxis3") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (num == 0)
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
if (num == 0) {
|
||||
m_plotFlags &= ~ImPlotFlags_YAxis3;
|
||||
else
|
||||
} else {
|
||||
m_plotFlags |= ImPlotFlags_YAxis3;
|
||||
}
|
||||
return true;
|
||||
} else if (name == "viewTime") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_viewTime = num / 1000.0;
|
||||
return true;
|
||||
} else if (name == "height") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_height = num;
|
||||
return true;
|
||||
} else if (name.startswith("y")) {
|
||||
auto [yAxisStr, yName] = name.split('_');
|
||||
int yAxis;
|
||||
if (yAxisStr.substr(1).getAsInteger(10, yAxis)) return false;
|
||||
if (yAxis < 0 || yAxis > 3) return false;
|
||||
if (yAxisStr.substr(1).getAsInteger(10, yAxis)) {
|
||||
return false;
|
||||
}
|
||||
if (yAxis < 0 || yAxis > 3) {
|
||||
return false;
|
||||
}
|
||||
if (yName == "min") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_axisRange[yAxis].min = num / 1000.0;
|
||||
return true;
|
||||
} else if (yName == "max") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_axisRange[yAxis].max = num / 1000.0;
|
||||
return true;
|
||||
} else if (yName == "lockMin") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_axisRange[yAxis].lockMin = num != 0;
|
||||
return true;
|
||||
} else if (yName == "lockMax") {
|
||||
int num;
|
||||
if (value.getAsInteger(10, num)) return true;
|
||||
if (value.getAsInteger(10, num)) {
|
||||
return true;
|
||||
}
|
||||
m_axisRange[yAxis].lockMax = num != 0;
|
||||
return true;
|
||||
}
|
||||
@@ -539,7 +594,9 @@ void Plot::WriteIni(ImGuiTextBuffer* out) {
|
||||
}
|
||||
|
||||
void Plot::DragDropTarget(PlotView& view, size_t i, bool inPlot) {
|
||||
if (!ImGui::BeginDragDropTarget()) return;
|
||||
if (!ImGui::BeginDragDropTarget()) {
|
||||
return;
|
||||
}
|
||||
// handle dragging onto a specific Y axis
|
||||
int yAxis = -1;
|
||||
if (inPlot) {
|
||||
@@ -576,12 +633,15 @@ void Plot::DragDropTarget(PlotView& view, size_t i, bool inPlot) {
|
||||
}
|
||||
|
||||
void Plot::EmitPlot(PlotView& view, double now, bool paused, size_t i) {
|
||||
if (!m_visible) return;
|
||||
if (!m_visible) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool lockX = (i != 0 && m_lockPrevX);
|
||||
|
||||
if (!lockX && m_showPause && ImGui::Button(m_paused ? "Resume" : "Pause"))
|
||||
if (!lockX && m_showPause && ImGui::Button(m_paused ? "Resume" : "Pause")) {
|
||||
m_paused = !m_paused;
|
||||
}
|
||||
|
||||
char label[128];
|
||||
std::snprintf(label, sizeof(label), "%s##plot", m_name.c_str());
|
||||
@@ -606,8 +666,12 @@ void Plot::EmitPlot(PlotView& view, double now, bool paused, size_t i) {
|
||||
m_axisRange[i].min, m_axisRange[i].max,
|
||||
m_axisRange[i].apply ? ImGuiCond_Always : ImGuiCond_Once, i);
|
||||
m_axisRange[i].apply = false;
|
||||
if (m_axisRange[i].lockMin) yFlags[i] |= ImPlotAxisFlags_LockMin;
|
||||
if (m_axisRange[i].lockMax) yFlags[i] |= ImPlotAxisFlags_LockMax;
|
||||
if (m_axisRange[i].lockMin) {
|
||||
yFlags[i] |= ImPlotAxisFlags_LockMin;
|
||||
}
|
||||
if (m_axisRange[i].lockMax) {
|
||||
yFlags[i] |= ImPlotAxisFlags_LockMax;
|
||||
}
|
||||
}
|
||||
|
||||
if (ImPlot::BeginPlot(label, nullptr, nullptr, ImVec2(-1, m_height),
|
||||
@@ -617,11 +681,14 @@ void Plot::EmitPlot(PlotView& view, double now, bool paused, size_t i) {
|
||||
ImGui::PushID(j);
|
||||
switch (m_series[j]->EmitPlot(view, now, j, i)) {
|
||||
case PlotSeries::kMoveUp:
|
||||
if (j > 0) std::swap(m_series[j - 1], m_series[j]);
|
||||
if (j > 0) {
|
||||
std::swap(m_series[j - 1], m_series[j]);
|
||||
}
|
||||
break;
|
||||
case PlotSeries::kMoveDown:
|
||||
if (j < (m_series.size() - 1))
|
||||
if (j < (m_series.size() - 1)) {
|
||||
std::swap(m_series[j], m_series[j + 1]);
|
||||
}
|
||||
break;
|
||||
case PlotSeries::kDelete:
|
||||
m_series.erase(m_series.begin() + j);
|
||||
@@ -647,7 +714,9 @@ void Plot::EmitSettingsLimits(int axis) {
|
||||
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 3.5);
|
||||
ImGui::InputDouble("Max", &m_axisRange[axis].max, 0, 0, "%.3f");
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Apply")) m_axisRange[axis].apply = true;
|
||||
if (ImGui::Button("Apply")) {
|
||||
m_axisRange[axis].apply = true;
|
||||
}
|
||||
|
||||
ImGui::TextUnformatted("Lock Axis");
|
||||
ImGui::SameLine();
|
||||
@@ -665,35 +734,45 @@ void Plot::EmitSettings(size_t i) {
|
||||
ImGui::Checkbox("Visible", &m_visible);
|
||||
ImGui::Checkbox("Show Pause Button", &m_showPause);
|
||||
ImGui::CheckboxFlags("Show Legend", &m_plotFlags, ImPlotFlags_Legend);
|
||||
if (i != 0) ImGui::Checkbox("Lock X-axis to previous plot", &m_lockPrevX);
|
||||
if (i != 0) {
|
||||
ImGui::Checkbox("Lock X-axis to previous plot", &m_lockPrevX);
|
||||
}
|
||||
ImGui::TextUnformatted("Primary Y-Axis");
|
||||
EmitSettingsLimits(0);
|
||||
ImGui::CheckboxFlags("2nd Y-Axis", &m_plotFlags, ImPlotFlags_YAxis2);
|
||||
if ((m_plotFlags & ImPlotFlags_YAxis2) != 0) EmitSettingsLimits(1);
|
||||
if ((m_plotFlags & ImPlotFlags_YAxis2) != 0) {
|
||||
EmitSettingsLimits(1);
|
||||
}
|
||||
ImGui::CheckboxFlags("3rd Y-Axis", &m_plotFlags, ImPlotFlags_YAxis3);
|
||||
if ((m_plotFlags & ImPlotFlags_YAxis3) != 0) EmitSettingsLimits(2);
|
||||
if ((m_plotFlags & ImPlotFlags_YAxis3) != 0) {
|
||||
EmitSettingsLimits(2);
|
||||
}
|
||||
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6);
|
||||
ImGui::InputFloat("View Time (s)", &m_viewTime, 0.1f, 1.0f, "%.1f");
|
||||
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6);
|
||||
if (ImGui::InputInt("Height", &m_height, 10)) {
|
||||
if (m_height < 0) m_height = 0;
|
||||
if (m_height < 0) {
|
||||
m_height = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlotView::Display() {
|
||||
if (ImGui::BeginPopupContextItem()) {
|
||||
if (ImGui::Button("Add plot"))
|
||||
if (ImGui::Button("Add plot")) {
|
||||
m_plots.emplace_back(std::make_unique<Plot>());
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < m_plots.size(); ++i) {
|
||||
auto& plot = m_plots[i];
|
||||
ImGui::PushID(i);
|
||||
|
||||
char name[64];
|
||||
if (!plot->GetName().empty())
|
||||
if (!plot->GetName().empty()) {
|
||||
std::snprintf(name, sizeof(name), "%s", plot->GetName().c_str());
|
||||
else
|
||||
} else {
|
||||
std::snprintf(name, sizeof(name), "Plot %d", static_cast<int>(i));
|
||||
}
|
||||
|
||||
char label[90];
|
||||
std::snprintf(label, sizeof(label), "%s###header%d", name,
|
||||
@@ -712,12 +791,16 @@ void PlotView::Display() {
|
||||
|
||||
if (open) {
|
||||
if (ImGui::Button("Move Up")) {
|
||||
if (i > 0) std::swap(m_plots[i - 1], plot);
|
||||
if (i > 0) {
|
||||
std::swap(m_plots[i - 1], plot);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Move Down")) {
|
||||
if (i < (m_plots.size() - 1)) std::swap(plot, m_plots[i + 1]);
|
||||
if (i < (m_plots.size() - 1)) {
|
||||
std::swap(plot, m_plots[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::SameLine();
|
||||
@@ -737,11 +820,14 @@ void PlotView::Display() {
|
||||
}
|
||||
|
||||
if (m_plots.empty()) {
|
||||
if (ImGui::Button("Add plot"))
|
||||
if (ImGui::Button("Add plot")) {
|
||||
m_plots.emplace_back(std::make_unique<Plot>());
|
||||
}
|
||||
|
||||
// Make "add plot" button a DND target for Plot
|
||||
if (!ImGui::BeginDragDropTarget()) return;
|
||||
if (!ImGui::BeginDragDropTarget()) {
|
||||
return;
|
||||
}
|
||||
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("Plot")) {
|
||||
auto ref = static_cast<const PlotSeriesRef*>(payload->Data);
|
||||
MovePlot(ref->view, ref->plotIndex, 0);
|
||||
@@ -758,7 +844,9 @@ void PlotView::Display() {
|
||||
|
||||
void PlotView::MovePlot(PlotView* fromView, size_t fromIndex, size_t toIndex) {
|
||||
if (fromView == this) {
|
||||
if (fromIndex == toIndex) return;
|
||||
if (fromIndex == toIndex) {
|
||||
return;
|
||||
}
|
||||
auto val = std::move(m_plots[fromIndex]);
|
||||
m_plots.insert(m_plots.begin() + toIndex, std::move(val));
|
||||
m_plots.erase(m_plots.begin() + fromIndex + (fromIndex > toIndex ? 1 : 0));
|
||||
@@ -778,7 +866,9 @@ void PlotView::MovePlotSeries(PlotView* fromView, size_t fromPlotIndex,
|
||||
auto& plotSeries = m_plots[fromPlotIndex]->m_series;
|
||||
auto val = std::move(plotSeries[fromSeriesIndex]);
|
||||
// only set Y-axis if actually set
|
||||
if (yAxis != -1) val->SetYAxis(yAxis);
|
||||
if (yAxis != -1) {
|
||||
val->SetYAxis(yAxis);
|
||||
}
|
||||
plotSeries.insert(plotSeries.begin() + toSeriesIndex, std::move(val));
|
||||
plotSeries.erase(plotSeries.begin() + fromSeriesIndex +
|
||||
(fromSeriesIndex > toSeriesIndex ? 1 : 0));
|
||||
@@ -833,7 +923,9 @@ void PlotProvider::DisplayMenu() {
|
||||
void PlotProvider::DisplayWindows() {
|
||||
// create views if not already created
|
||||
for (auto&& window : m_windows) {
|
||||
if (!window->HasView()) window->SetView(std::make_unique<PlotView>(this));
|
||||
if (!window->HasView()) {
|
||||
window->SetView(std::make_unique<PlotView>(this));
|
||||
}
|
||||
}
|
||||
WindowManager::DisplayWindows();
|
||||
}
|
||||
@@ -847,14 +939,20 @@ void* PlotProvider::IniSaver::IniReadOpen(const char* name) {
|
||||
wpi::StringRef seriesId;
|
||||
if (m_forSeries) {
|
||||
std::tie(plotNumStr, seriesId) = plotNumStr.split('#');
|
||||
if (seriesId.empty()) return nullptr;
|
||||
if (seriesId.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
unsigned int plotNum;
|
||||
if (plotNumStr.getAsInteger(10, plotNum)) return nullptr;
|
||||
if (plotNumStr.getAsInteger(10, plotNum)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// get or create window
|
||||
auto win = m_provider->GetOrAddWindow(viewId, true);
|
||||
if (!win) return nullptr;
|
||||
if (!win) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// get or create view
|
||||
auto view = static_cast<PlotView*>(win->GetView());
|
||||
@@ -864,12 +962,18 @@ void* PlotProvider::IniSaver::IniReadOpen(const char* name) {
|
||||
}
|
||||
|
||||
// get or create plot
|
||||
if (view->m_plots.size() <= plotNum) view->m_plots.resize(plotNum + 1);
|
||||
if (view->m_plots.size() <= plotNum) {
|
||||
view->m_plots.resize(plotNum + 1);
|
||||
}
|
||||
auto& plot = view->m_plots[plotNum];
|
||||
if (!plot) plot = std::make_unique<Plot>();
|
||||
if (!plot) {
|
||||
plot = std::make_unique<Plot>();
|
||||
}
|
||||
|
||||
// early exit for plot data
|
||||
if (!m_forSeries) return plot.get();
|
||||
if (!m_forSeries) {
|
||||
return plot.get();
|
||||
}
|
||||
|
||||
// get or create series
|
||||
return plot->m_series.emplace_back(std::make_unique<PlotSeries>(seriesId))
|
||||
@@ -880,10 +984,11 @@ void PlotProvider::IniSaver::IniReadLine(void* entry, const char* lineStr) {
|
||||
auto [name, value] = wpi::StringRef{lineStr}.split('=');
|
||||
name = name.trim();
|
||||
value = value.trim();
|
||||
if (m_forSeries)
|
||||
if (m_forSeries) {
|
||||
static_cast<PlotSeries*>(entry)->ReadIni(name, value);
|
||||
else
|
||||
} else {
|
||||
static_cast<Plot*>(entry)->ReadIni(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
void PlotProvider::IniSaver::IniWriteAll(ImGuiTextBuffer* out_buf) {
|
||||
|
||||
@@ -31,7 +31,9 @@ void glass::DisplayStringChooser(StringChooserModel* model) {
|
||||
if (ImGui::Selectable(option.c_str(), isSelected)) {
|
||||
model->SetSelected(option);
|
||||
}
|
||||
if (isSelected) ImGui::SetItemDefaultFocus();
|
||||
if (isSelected) {
|
||||
ImGui::SetItemDefaultFocus();
|
||||
}
|
||||
ImGui::PopID();
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
|
||||
Reference in New Issue
Block a user