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:
Peter Johnson
2020-12-28 12:58:06 -08:00
committed by GitHub
parent 0291a3ff56
commit 2aed432b4b
634 changed files with 10716 additions and 3938 deletions

View File

@@ -26,8 +26,9 @@ static bool ConvertInt(Storage::Value* value) {
if (value->stringVal.empty()) {
return false;
} else {
if (wpi::StringRef{value->stringVal}.getAsInteger(10, value->intVal))
if (wpi::StringRef{value->stringVal}.getAsInteger(10, value->intVal)) {
return false;
}
}
return true;
}
@@ -37,8 +38,9 @@ static bool ConvertInt64(Storage::Value* value) {
if (value->stringVal.empty()) {
return false;
} else {
if (wpi::StringRef{value->stringVal}.getAsInteger(10, value->int64Val))
if (wpi::StringRef{value->stringVal}.getAsInteger(10, value->int64Val)) {
return false;
}
}
return true;
}
@@ -62,8 +64,9 @@ static bool ConvertFloat(Storage::Value* value) {
if (value->stringVal.empty()) {
return false;
} else {
if (std::sscanf(value->stringVal.c_str(), "%f", &value->floatVal) != 1)
if (std::sscanf(value->stringVal.c_str(), "%f", &value->floatVal) != 1) {
return false;
}
}
return true;
}
@@ -73,8 +76,9 @@ static bool ConvertDouble(Storage::Value* value) {
if (value->stringVal.empty()) {
return false;
} else {
if (std::sscanf(value->stringVal.c_str(), "%lf", &value->doubleVal) != 1)
if (std::sscanf(value->stringVal.c_str(), "%lf", &value->doubleVal) != 1) {
return false;
}
}
return true;
}
@@ -83,7 +87,9 @@ static void* GlassStorageReadOpen(ImGuiContext*, ImGuiSettingsHandler* handler,
const char* name) {
auto ctx = static_cast<Context*>(handler->UserData);
auto& storage = ctx->storage[name];
if (!storage) storage = std::make_unique<Storage>();
if (!storage) {
storage = std::make_unique<Storage>();
}
return storage.get();
}
@@ -193,25 +199,39 @@ static void Shutdown(Context* ctx) {}
Context* glass::CreateContext() {
Context* ctx = new Context;
if (!gContext) SetCurrentContext(ctx);
if (!gContext) {
SetCurrentContext(ctx);
}
Initialize(ctx);
return ctx;
}
void glass::DestroyContext(Context* ctx) {
if (!ctx) ctx = gContext;
if (!ctx) {
ctx = gContext;
}
Shutdown(ctx);
if (gContext == ctx) SetCurrentContext(nullptr);
if (gContext == ctx) {
SetCurrentContext(nullptr);
}
delete ctx;
}
Context* glass::GetCurrentContext() { return gContext; }
Context* glass::GetCurrentContext() {
return gContext;
}
void glass::SetCurrentContext(Context* ctx) { gContext = ctx; }
void glass::SetCurrentContext(Context* ctx) {
gContext = ctx;
}
void glass::ResetTime() { gContext->zeroTime = wpi::Now(); }
void glass::ResetTime() {
gContext->zeroTime = wpi::Now();
}
uint64_t glass::GetZeroTime() { return gContext->zeroTime; }
uint64_t glass::GetZeroTime() {
return gContext->zeroTime;
}
Storage::Value& Storage::GetValue(wpi::StringRef key) {
auto it = std::find(m_keys.begin(), m_keys.end(), key);
@@ -227,10 +247,12 @@ Storage::Value& Storage::GetValue(wpi::StringRef key) {
#define DEFUN(CapsName, LowerName, CType) \
CType Storage::Get##CapsName(wpi::StringRef key, CType defaultVal) const { \
auto it = std::find(m_keys.begin(), m_keys.end(), key); \
if (it == m_keys.end()) return defaultVal; \
if (it == m_keys.end()) \
return defaultVal; \
Value& value = *m_values[it - m_keys.begin()]; \
if (value.type != Value::k##CapsName) { \
if (!Convert##CapsName(&value)) value.LowerName##Val = defaultVal; \
if (!Convert##CapsName(&value)) \
value.LowerName##Val = defaultVal; \
} \
return value.LowerName##Val; \
} \
@@ -260,7 +282,8 @@ Storage::Value& Storage::GetValue(wpi::StringRef key) {
} else { \
Value& value = *m_values[it - m_keys.begin()]; \
if (value.type != Value::k##CapsName) { \
if (!Convert##CapsName(&value)) value.LowerName##Val = defaultVal; \
if (!Convert##CapsName(&value)) \
value.LowerName##Val = defaultVal; \
} \
return &value.LowerName##Val; \
} \
@@ -275,7 +298,9 @@ DEFUN(Double, double, double)
std::string Storage::GetString(wpi::StringRef key,
const std::string& defaultVal) const {
auto it = std::find(m_keys.begin(), m_keys.end(), key);
if (it == m_keys.end()) return defaultVal;
if (it == m_keys.end()) {
return defaultVal;
}
Value& value = *m_values[it - m_keys.begin()];
value.type = Value::kString;
return value.stringVal;
@@ -311,13 +336,17 @@ std::string* Storage::GetStringRef(wpi::StringRef key,
Storage& glass::GetStorage() {
auto& storage = gContext->storage[gContext->curId];
if (!storage) storage = std::make_unique<Storage>();
if (!storage) {
storage = std::make_unique<Storage>();
}
return *storage;
}
Storage& glass::GetStorage(wpi::StringRef id) {
auto& storage = gContext->storage[id];
if (!storage) storage = std::make_unique<Storage>();
if (!storage) {
storage = std::make_unique<Storage>();
}
return *storage;
}
@@ -326,8 +355,12 @@ static void PushIDStack(wpi::StringRef label_id) {
auto [label, id] = wpi::StringRef{label_id}.split("###");
// if no ###id, use label as id
if (id.empty()) id = label;
if (!gContext->curId.empty()) gContext->curId += "###";
if (id.empty()) {
id = label;
}
if (!gContext->curId.empty()) {
gContext->curId += "###";
}
gContext->curId += id;
}
@@ -361,7 +394,9 @@ bool glass::CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags) {
wpi::SmallString<64> openKey;
auto [name, id] = wpi::StringRef{label}.split("###");
// if no ###id, use name as id
if (id.empty()) id = name;
if (id.empty()) {
id = name;
}
openKey = id;
openKey += "###open";
@@ -376,7 +411,9 @@ bool glass::TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags) {
bool* open = GetStorage().GetBoolRef("open");
*open = ImGui::TreeNodeEx(
label, flags | (*open ? ImGuiTreeNodeFlags_DefaultOpen : 0));
if (!*open) PopIDStack();
if (!*open) {
PopIDStack();
}
return *open;
}

View File

@@ -14,7 +14,9 @@ DataSource::DataSource(const wpi::Twine& id) : m_id{id.str()} {
auto it = gContext->sources.try_emplace(m_id, this);
auto& srcName = it.first->getValue();
m_name = srcName.name.get();
if (!srcName.source) srcName.source = this;
if (!srcName.source) {
srcName.source = this;
}
sourceCreated(m_id.c_str(), this);
}
@@ -26,18 +28,30 @@ DataSource::DataSource(const wpi::Twine& id, int index, int index2)
wpi::Twine(index2) + wpi::Twine(']')} {}
DataSource::~DataSource() {
if (!gContext) return;
if (!gContext) {
return;
}
auto it = gContext->sources.find(m_id);
if (it == gContext->sources.end()) return;
if (it == gContext->sources.end()) {
return;
}
auto& srcName = it->getValue();
if (srcName.source == this) srcName.source = nullptr;
if (srcName.source == this) {
srcName.source = nullptr;
}
}
void DataSource::SetName(const wpi::Twine& name) { m_name->SetName(name); }
void DataSource::SetName(const wpi::Twine& name) {
m_name->SetName(name);
}
const char* DataSource::GetName() const { return m_name->GetName(); }
const char* DataSource::GetName() const {
return m_name->GetName();
}
void DataSource::PushEditNameId(int index) { m_name->PushEditNameId(index); }
void DataSource::PushEditNameId(int index) {
m_name->PushEditNameId(index);
}
void DataSource::PushEditNameId(const char* name) {
m_name->PushEditNameId(name);
@@ -134,6 +148,8 @@ void DataSource::EmitDrag(ImGuiDragDropFlags flags) const {
DataSource* DataSource::Find(wpi::StringRef id) {
auto it = gContext->sources.find(id);
if (it == gContext->sources.end()) return nullptr;
if (it == gContext->sources.end()) {
return nullptr;
}
return it->getValue().source;
}

View File

@@ -11,11 +11,15 @@
using namespace glass;
void MainMenuBar::AddMainMenu(std::function<void()> menu) {
if (menu) m_menus.emplace_back(std::move(menu));
if (menu) {
m_menus.emplace_back(std::move(menu));
}
}
void MainMenuBar::AddOptionMenu(std::function<void()> menu) {
if (menu) m_optionMenus.emplace_back(std::move(menu));
if (menu) {
m_optionMenus.emplace_back(std::move(menu));
}
}
void MainMenuBar::Display() {
@@ -24,7 +28,9 @@ void MainMenuBar::Display() {
if (!m_optionMenus.empty()) {
if (ImGui::BeginMenu("Options")) {
for (auto&& menu : m_optionMenus) {
if (menu) menu();
if (menu) {
menu();
}
}
ImGui::EndMenu();
}
@@ -33,7 +39,9 @@ void MainMenuBar::Display() {
wpi::gui::EmitViewMenu();
for (auto&& menu : m_menus) {
if (menu) menu();
if (menu) {
menu();
}
}
#if 0

View File

@@ -6,4 +6,6 @@
using namespace glass;
bool Model::IsReadOnly() { return false; }
bool Model::IsReadOnly() {
return false;
}

View File

@@ -28,7 +28,9 @@ void Window::SetVisibility(Visibility visibility) {
}
void Window::Display() {
if (!m_view) return;
if (!m_view) {
return;
}
if (!m_visible || !m_enabled) {
PushID(m_id);
m_view->Hidden();
@@ -36,22 +38,32 @@ void Window::Display() {
return;
}
if (m_posCond != 0) ImGui::SetNextWindowPos(m_pos, m_posCond);
if (m_sizeCond != 0) ImGui::SetNextWindowSize(m_size, m_sizeCond);
if (m_setPadding) ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, m_padding);
if (m_posCond != 0) {
ImGui::SetNextWindowPos(m_pos, m_posCond);
}
if (m_sizeCond != 0) {
ImGui::SetNextWindowSize(m_size, m_sizeCond);
}
if (m_setPadding) {
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, m_padding);
}
char label[128];
std::snprintf(label, sizeof(label), "%s###%s",
m_name.empty() ? m_id.c_str() : m_name.c_str(), m_id.c_str());
if (Begin(label, &m_visible, m_flags)) {
if (m_renamePopupEnabled) PopupEditName(nullptr, &m_name);
if (m_renamePopupEnabled) {
PopupEditName(nullptr, &m_name);
}
m_view->Display();
} else {
m_view->Hidden();
}
End();
if (m_setPadding) ImGui::PopStyleVar();
if (m_setPadding) {
ImGui::PopStyleVar();
}
}
bool Window::DisplayMenuItem(const char* label) {
@@ -83,11 +95,15 @@ void Window::IniReadLine(const char* lineStr) {
m_name = value;
} else if (name == "visible") {
int num;
if (value.getAsInteger(10, num)) return;
if (value.getAsInteger(10, num)) {
return;
}
m_visible = num;
} else if (name == "enabled") {
int num;
if (value.getAsInteger(10, num)) return;
if (value.getAsInteger(10, num)) {
return;
}
m_enabled = num;
}
}

View File

@@ -34,7 +34,9 @@ void WindowManager::IniSaver::IniWriteAll(ImGuiTextBuffer* out_buf) {
Window* WindowManager::AddWindow(wpi::StringRef id,
wpi::unique_function<void()> display) {
auto win = GetOrAddWindow(id, false);
if (!win) return nullptr;
if (!win) {
return nullptr;
}
if (win->HasView()) {
wpi::errs() << "GUI: ignoring duplicate window '" << id << "'\n";
return nullptr;
@@ -46,7 +48,9 @@ Window* WindowManager::AddWindow(wpi::StringRef id,
Window* WindowManager::AddWindow(wpi::StringRef id,
std::unique_ptr<View> view) {
auto win = GetOrAddWindow(id, false);
if (!win) return nullptr;
if (!win) {
return nullptr;
}
if (win->HasView()) {
wpi::errs() << "GUI: ignoring duplicate window '" << id << "'\n";
return nullptr;
@@ -76,7 +80,9 @@ Window* WindowManager::GetWindow(wpi::StringRef id) {
auto it = std::lower_bound(
m_windows.begin(), m_windows.end(), id,
[](const auto& elem, wpi::StringRef s) { return elem->GetId() < s; });
if (it == m_windows.end() || (*it)->GetId() != id) return nullptr;
if (it == m_windows.end() || (*it)->GetId() != id) {
return nullptr;
}
return it->get();
}

View File

@@ -10,7 +10,9 @@
using namespace glass;
void glass::DisplayAccelerometerDevice(AccelerometerModel* model) {
if (!model->Exists()) return;
if (!model->Exists()) {
return;
}
if (BeginDevice("BuiltInAccel")) {
// Range
{

View File

@@ -13,7 +13,9 @@ using namespace glass;
void glass::DisplayAnalogInput(AnalogInputModel* model, int index) {
auto voltageData = model->GetVoltageData();
if (!voltageData) return;
if (!voltageData) {
return;
}
// build label
std::string* name = GetStorage().GetStringRef("name");
@@ -34,11 +36,15 @@ void glass::DisplayAnalogInput(AnalogInputModel* model, int index) {
ImGui::PopStyleColor();
} else {
float val = voltageData->GetValue();
if (voltageData->SliderFloat(label, &val, 0.0, 5.0)) model->SetVoltage(val);
if (voltageData->SliderFloat(label, &val, 0.0, 5.0)) {
model->SetVoltage(val);
}
}
// context menu to change name
if (PopupEditName("name", name)) voltageData->SetName(name->c_str());
if (PopupEditName("name", name)) {
voltageData->SetName(name->c_str());
}
}
void glass::DisplayAnalogInputs(AnalogInputsModel* model,
@@ -58,6 +64,7 @@ void glass::DisplayAnalogInputs(AnalogInputsModel* model,
PopID();
hasAny = true;
});
if (!hasAny && !noneMsg.empty())
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.begin(), noneMsg.end());
}
}

View File

@@ -13,12 +13,16 @@ using namespace glass;
void glass::DisplayAnalogOutputsDevice(AnalogOutputsModel* model) {
int count = 0;
model->ForEachAnalogOutput([&](auto&, int) { ++count; });
if (count == 0) return;
if (count == 0) {
return;
}
if (BeginDevice("Analog Outputs")) {
model->ForEachAnalogOutput([&](auto& analogOut, int i) {
auto analogOutData = analogOut.GetVoltageData();
if (!analogOutData) return;
if (!analogOutData) {
return;
}
PushID(i);
// build label
@@ -34,7 +38,9 @@ void glass::DisplayAnalogOutputsDevice(AnalogOutputsModel* model) {
DeviceDouble(label, true, &value, analogOutData);
if (PopupEditName("name", name)) {
if (analogOutData) analogOutData->SetName(name->c_str());
if (analogOutData) {
analogOutData->SetName(name->c_str());
}
}
PopID();
});

View File

@@ -59,11 +59,12 @@ void DisplayDIOImpl(DIOModel* model, int index, bool outputsEnabled) {
}
} else {
const char* name = model->GetName();
if (name[0] != '\0')
if (name[0] != '\0') {
info.GetLabel(label, sizeof(label), name);
else
} else {
info.GetLabel(label, sizeof(label), model->IsInput() ? " In" : "Out",
index);
}
if (auto simDevice = model->GetSimDevice()) {
LabelSimDevice(label, simDevice);
} else {
@@ -87,8 +88,12 @@ void DisplayDIOImpl(DIOModel* model, int index, bool outputsEnabled) {
}
}
if (info.PopupEditName(index)) {
if (dpwmData) dpwmData->SetName(info.GetName());
if (dutyCycleData) dutyCycleData->SetName(info.GetName());
if (dpwmData) {
dpwmData->SetName(info.GetName());
}
if (dutyCycleData) {
dutyCycleData->SetName(info.GetName());
}
}
}
@@ -110,6 +115,7 @@ void glass::DisplayDIOs(DIOsModel* model, bool outputsEnabled,
ImGui::PopID();
});
ImGui::PopItemWidth();
if (!hasAny && !noneMsg.empty())
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.begin(), noneMsg.end());
}
}

View File

@@ -82,7 +82,9 @@ void glass::DisplayEncoder(EncoderModel* model) {
model->SetName(name->c_str());
}
if (!open) return;
if (!open) {
return;
}
ImGui::PushItemWidth(ImGui::GetFontSize() * 8);
// distance per pulse
@@ -94,7 +96,9 @@ void glass::DisplayEncoder(EncoderModel* model) {
// count
if (auto countData = model->GetCountData()) {
int value = countData->GetValue();
if (ImGui::InputInt("##input", &value)) model->SetCount(value);
if (ImGui::InputInt("##input", &value)) {
model->SetCount(value);
}
ImGui::SameLine();
if (ImGui::Button("Reset")) {
model->SetCount(0);
@@ -157,6 +161,7 @@ void glass::DisplayEncoders(EncodersModel* model, wpi::StringRef noneMsg) {
DisplayEncoder(&encoder);
PopID();
});
if (!hasAny && !noneMsg.empty())
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.begin(), noneMsg.end());
}
}

View File

@@ -36,8 +36,9 @@ void glass::DisplayGyro(GyroModel* m) {
m->IsReadOnly() ? ImGuiInputTextFlags_ReadOnly : ImGuiInputTextFlags_None;
auto value = angle->GetValue();
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
if (ImGui::InputDouble("Gyro Angle (Deg)", &value, 0.0, 0.0, "%.4f", flags))
if (ImGui::InputDouble("Gyro Angle (Deg)", &value, 0.0, 0.0, "%.4f", flags)) {
m->SetAngle(value);
}
// Draw the gyro indicator.
ImDrawList* draw = ImGui::GetWindowDrawList();

View File

@@ -42,7 +42,9 @@ void glass::DisplayLEDDisplay(LEDDisplayModel* model, int index) {
ImGui::Combo("Start", start, options, 4);
}
ImGui::Checkbox("Serpentine", serpentine);
if (*numColumns < 1) *numColumns = 1;
if (*numColumns < 1) {
*numColumns = 1;
}
ImGui::PopItemWidth();
// show as LED indicators
@@ -51,13 +53,17 @@ void glass::DisplayLEDDisplay(LEDDisplayModel* model, int index) {
storage.SetData(std::make_shared<IndicatorData>());
iData = storage.GetData<IndicatorData>();
}
if (length > static_cast<int>(iData->values.size()))
if (length > static_cast<int>(iData->values.size())) {
iData->values.resize(length);
if (length > static_cast<int>(iData->colors.size()))
}
if (length > static_cast<int>(iData->colors.size())) {
iData->colors.resize(length);
}
if (!running) {
iData->colors[0] = IM_COL32(128, 128, 128, 255);
for (int j = 0; j < length; ++j) iData->values[j] = -1;
for (int j = 0; j < length; ++j) {
iData->values[j] = -1;
}
} else {
for (int j = 0; j < length; ++j) {
iData->values[j] = j + 1;
@@ -79,10 +85,14 @@ void glass::DisplayLEDDisplays(LEDDisplaysModel* model) {
model->ForEachLEDDisplay([&](LEDDisplayModel& display, int i) {
hasAny = true;
if (model->GetNumLEDDisplays() > 1) ImGui::Text("LEDs[%d]", i);
if (model->GetNumLEDDisplays() > 1) {
ImGui::Text("LEDs[%d]", i);
}
PushID(i);
DisplayLEDDisplay(&display, i);
PopID();
});
if (!hasAny) ImGui::Text("No addressable LEDs");
if (!hasAny) {
ImGui::Text("No addressable LEDs");
}
}

View File

@@ -23,16 +23,22 @@ bool glass::DisplayPCMSolenoids(PCMModel* model, int index,
wpi::SmallVector<int, 16> channels;
model->ForEachSolenoid([&](SolenoidModel& solenoid, int j) {
if (auto data = solenoid.GetOutputData()) {
if (j >= static_cast<int>(channels.size())) channels.resize(j + 1);
if (j >= static_cast<int>(channels.size())) {
channels.resize(j + 1);
}
channels[j] = (outputsEnabled && data->GetValue()) ? 1 : -1;
}
});
if (channels.empty()) return false;
if (channels.empty()) {
return false;
}
// show nonexistent channels as empty
for (auto&& ch : channels) {
if (ch == 0) ch = -2;
if (ch == 0) {
ch = -2;
}
}
// build header label
@@ -81,17 +87,22 @@ void glass::DisplayPCMsSolenoids(PCMsModel* model, bool outputsEnabled,
bool hasAny = false;
model->ForEachPCM([&](PCMModel& pcm, int i) {
PushID(i);
if (DisplayPCMSolenoids(&pcm, i, outputsEnabled)) hasAny = true;
if (DisplayPCMSolenoids(&pcm, i, outputsEnabled)) {
hasAny = true;
}
PopID();
});
if (!hasAny && !noneMsg.empty())
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.begin(), noneMsg.end());
}
}
void glass::DisplayCompressorDevice(PCMModel* model, int index,
bool outputsEnabled) {
auto compressor = model->GetCompressor();
if (!compressor || !compressor->Exists()) return;
if (!compressor || !compressor->Exists()) {
return;
}
DisplayCompressorDevice(compressor, index, outputsEnabled);
}

View File

@@ -24,8 +24,9 @@ static float DisplayChannel(PDPModel& pdp, int channel) {
leftInfo.GetLabel(name, sizeof(name), "", channel);
double val = currentData->GetValue();
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 4);
if (currentData->InputDouble(name, &val, 0, 0, "%.3f"))
if (currentData->InputDouble(name, &val, 0, 0, "%.3f")) {
pdp.SetCurrent(channel, val);
}
width = ImGui::GetItemRectSize().x;
leftInfo.PopupEditName(channel);
ImGui::PopID();
@@ -69,7 +70,9 @@ void glass::DisplayPDP(PDPModel* model, int index) {
float width =
(std::max)(leftWidth, rightWidth) * 2 + ImGui::GetFontSize() * 4;
if (width > maxWidth) maxWidth = width;
if (width > maxWidth) {
maxWidth = width;
}
}
ImGui::Columns(1);
ImGui::Dummy(ImVec2(maxWidth, 0));
@@ -84,6 +87,7 @@ void glass::DisplayPDPs(PDPsModel* model, wpi::StringRef noneMsg) {
DisplayPDP(&pdp, i);
PopID();
});
if (!hasAny && !noneMsg.empty())
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.begin(), noneMsg.end());
}
}

View File

@@ -13,7 +13,9 @@ using namespace glass;
void glass::DisplayPWM(PWMModel* model, int index, bool outputsEnabled) {
auto data = model->GetSpeedData();
if (!data) return;
if (!data) {
return;
}
// build label
std::string* name = GetStorage().GetStringRef("name");
@@ -46,14 +48,16 @@ void glass::DisplayPWMs(PWMsModel* model, bool outputsEnabled,
hasAny = true;
PushID(i);
if (!first)
if (!first) {
ImGui::Separator();
else
} else {
first = false;
}
DisplayPWM(&pwm, i, outputsEnabled);
PopID();
});
if (!hasAny && !noneMsg.empty())
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.begin(), noneMsg.end());
}
}

View File

@@ -23,20 +23,29 @@ void glass::DisplayRelay(RelayModel* model, int index, bool outputsEnabled) {
bool forward = false;
bool reverse = false;
if (outputsEnabled) {
if (forwardData) forward = forwardData->GetValue();
if (reverseData) reverse = reverseData->GetValue();
if (forwardData) {
forward = forwardData->GetValue();
}
if (reverseData) {
reverse = reverseData->GetValue();
}
}
std::string* name = GetStorage().GetStringRef("name");
ImGui::PushID("name");
if (!name->empty())
if (!name->empty()) {
ImGui::Text("%s [%d]", name->c_str(), index);
else
} else {
ImGui::Text("Relay[%d]", index);
}
ImGui::PopID();
if (PopupEditName("name", name)) {
if (forwardData) forwardData->SetName(name->c_str());
if (reverseData) reverseData->SetName(name->c_str());
if (forwardData) {
forwardData->SetName(name->c_str());
}
if (reverseData) {
reverseData->SetName(name->c_str());
}
}
ImGui::SameLine();
@@ -57,15 +66,17 @@ void glass::DisplayRelays(RelaysModel* model, bool outputsEnabled,
model->ForEachRelay([&](RelayModel& relay, int i) {
hasAny = true;
if (!first)
if (!first) {
ImGui::Separator();
else
} else {
first = false;
}
PushID(i);
DisplayRelay(&relay, i, outputsEnabled);
PopID();
});
if (!hasAny && !noneMsg.empty())
if (!hasAny && !noneMsg.empty()) {
ImGui::TextUnformatted(noneMsg.begin(), noneMsg.end());
}
}

View File

@@ -23,11 +23,15 @@ void glass::DisplaySpeedController(SpeedControllerModel* m) {
}
// Add button to zero output.
if (ImGui::Button("Zero")) m->SetPercent(0.0);
if (ImGui::Button("Zero")) {
m->SetPercent(0.0);
}
ImGui::SameLine();
// Display a slider for the data.
float value = dc->GetValue();
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8);
if (dc->SliderFloat("% Output", &value, -1.0f, 1.0f)) m->SetPercent(value);
if (dc->SliderFloat("% Output", &value, -1.0f, 1.0f)) {
m->SetPercent(value);
}
}

View File

@@ -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 {

View File

@@ -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));

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -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

View File

@@ -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()) {

View File

@@ -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) {

View File

@@ -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();

View File

@@ -14,9 +14,15 @@ namespace glass {
void DrawLEDSources(const int* values, DataSource** sources, int numValues,
int cols, const ImU32* colors, float size, float spacing,
const LEDConfig& config) {
if (numValues == 0 || cols < 1) return;
if (size == 0) size = ImGui::GetFontSize() / 2.0;
if (spacing == 0) spacing = ImGui::GetFontSize() / 3.0;
if (numValues == 0 || cols < 1) {
return;
}
if (size == 0) {
size = ImGui::GetFontSize() / 2.0;
}
if (spacing == 0) {
spacing = ImGui::GetFontSize() / 3.0;
}
int rows = (numValues + cols - 1) / cols;
float inc = size + spacing;
@@ -73,12 +79,13 @@ void DrawLEDSources(const int* values, DataSource** sources, int numValues,
x += xinc;
}
}
if (values[i] > 0)
if (values[i] > 0) {
drawList->AddRectFilled(ImVec2(x, y), ImVec2(x + size, y + size),
colors[values[i] - 1]);
else if (values[i] < 0)
} else if (values[i] < 0) {
drawList->AddRect(ImVec2(x, y), ImVec2(x + size, y + size),
colors[-values[i] - 1], 0.0f, 0, 1.0);
}
if (sources) {
ImGui::SetCursorScreenPos(ImVec2(x - sized2, y - sized2));
if (sources[i]) {
@@ -97,7 +104,9 @@ void DrawLEDSources(const int* values, DataSource** sources, int numValues,
}
}
if (!sources) ImGui::Dummy(ImVec2(inc * cols, inc * rows));
if (!sources) {
ImGui::Dummy(ImVec2(inc * cols, inc * rows));
}
}
void DrawLEDs(const int* values, int numValues, int cols, const ImU32* colors,
@@ -120,15 +129,18 @@ bool DeleteButton(ImGuiID id, const ImVec2& pos) {
bool hovered, held;
bool pressed = ImGui::ButtonBehavior(bb, id, &hovered, &held);
if (is_clipped) return pressed;
if (is_clipped) {
return pressed;
}
// Render
ImU32 col =
ImGui::GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered);
ImVec2 center = bb.GetCenter();
if (hovered)
if (hovered) {
window->DrawList->AddCircleFilled(
center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f), col, 12);
}
ImU32 cross_col = ImGui::GetColorU32(ImGuiCol_Text);
window->DrawList->AddCircle(center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f),

View File

@@ -58,4 +58,6 @@ IniSaverBase::IniSaverBase(const wpi::Twine& typeName, IniSaverBackend* backend)
: m_typeName(typeName.str()),
m_backend{backend ? backend : GetSaverInstance()} {}
IniSaverBase::~IniSaverBase() { m_backend->Unregister(this); }
IniSaverBase::~IniSaverBase() {
m_backend->Unregister(this);
}

View File

@@ -75,7 +75,9 @@ void NameInfo::GetLabel(char* buf, size_t size, const char* defaultName,
}
bool NameInfo::ReadIni(wpi::StringRef name, wpi::StringRef value) {
if (name != "name") return false;
if (name != "name") {
return false;
}
size_t len = (std::min)(value.size(), sizeof(m_name) - 1);
std::memcpy(m_name, value.data(), len);
m_name[len] = '\0';
@@ -139,9 +141,13 @@ bool NameInfo::InputTextName(const char* label_id, ImGuiInputTextFlags flags) {
}
bool OpenInfo::ReadIni(wpi::StringRef name, wpi::StringRef value) {
if (name != "open") return false;
if (name != "open") {
return false;
}
int num;
if (value.getAsInteger(10, num)) return true;
if (value.getAsInteger(10, num)) {
return true;
}
m_open = num;
return true;
}
@@ -151,8 +157,12 @@ void OpenInfo::WriteIni(ImGuiTextBuffer* out) {
}
bool NameOpenInfo::ReadIni(wpi::StringRef name, wpi::StringRef value) {
if (NameInfo::ReadIni(name, value)) return true;
if (OpenInfo::ReadIni(name, value)) return true;
if (NameInfo::ReadIni(name, value)) {
return true;
}
if (OpenInfo::ReadIni(name, value)) {
return true;
}
return false;
}