Simulation GUI: Add support for custom names (#2292)

This allows users to right click on just about any name in the GUI (e.g. "PWM[0]") and rename it (e.g. "Left Motor [0]"). The index portion is not editable. The name is saved into imgui.ini so it's persistent.
This commit is contained in:
Peter Johnson
2020-01-20 22:47:36 -08:00
committed by GitHub
parent bb184ed481
commit 9d7b087972
11 changed files with 232 additions and 97 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -7,18 +7,18 @@
#include "AnalogOutGui.h"
#include <cstdio>
#include <cstring>
#include <memory>
#include <hal/Ports.h>
#include <imgui.h>
#include <mockdata/AnalogOutData.h>
#include "IniSaver.h"
#include "IniSaverInfo.h"
#include "SimDeviceGui.h"
using namespace halsimgui;
static IniSaver<NameInfo> gAnalogOuts{"AnalogOut"}; // indexed by channel
static void DisplayAnalogOutputs() {
static const int numAnalog = HAL_GetNumAnalogOutputs();
static auto init = std::make_unique<bool[]>(numAnalog);
@@ -34,14 +34,20 @@ static void DisplayAnalogOutputs() {
if (SimDeviceGui::StartDevice("Analog Outputs")) {
for (int i = 0; i < numAnalog; ++i) {
if (!init[i]) continue;
char name[32];
std::snprintf(name, sizeof(name), "Out[%d]", i);
auto& info = gAnalogOuts[i];
char name[128];
info.GetName(name, sizeof(name), "Out", i);
HAL_Value value = HAL_MakeDouble(HALSIM_GetAnalogOutVoltage(i));
SimDeviceGui::DisplayValue(name, true, &value);
info.PopupEditName(i);
}
SimDeviceGui::FinishDevice();
}
}
void AnalogOutGui::Initialize() { SimDeviceGui::Add(DisplayAnalogOutputs); }
void AnalogOutGui::Initialize() {
gAnalogOuts.Initialize();
SimDeviceGui::Add(DisplayAnalogOutputs);
}