[sim] Use wpigui for simulator GUI

This commit is contained in:
Peter Johnson
2020-08-26 00:35:44 -07:00
parent b80fde4388
commit 5d1220e629
9 changed files with 73 additions and 396 deletions

View File

@@ -9,12 +9,11 @@
#include <cmath>
#include <GL/gl3w.h>
#include <hal/SimDevice.h>
#include <imgui.h>
#define IMGUI_DEFINE_MATH_OPERATORS
#include <hal/simulation/SimDeviceData.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <units/angle.h>
#include <units/length.h>
@@ -23,6 +22,7 @@
#include <wpi/json.h>
#include <wpi/raw_istream.h>
#include <wpi/raw_ostream.h>
#include <wpigui.h>
#include "GuiUtil.h"
#include "HALSimGui.h"
@@ -31,6 +31,8 @@
using namespace halsimgui;
namespace gui = wpi::gui;
namespace {
// Per-frame field data (not persistent)
@@ -64,10 +66,10 @@ class FieldInfo {
void WriteIni(ImGuiTextBuffer* out) const;
private:
bool LoadImageImpl(const wpi::Twine& fn);
bool LoadImageImpl(const char* fn);
std::string m_filename;
GLuint m_texture = 0;
ImTextureID m_texture = 0;
int m_imageWidth = 0;
int m_imageHeight = 0;
int m_top = 0;
@@ -114,10 +116,10 @@ class RobotInfo {
void WriteIni(ImGuiTextBuffer* out) const;
private:
bool LoadImageImpl(const wpi::Twine& fn);
bool LoadImageImpl(const char* fn);
std::string m_filename;
GLuint m_texture = 0;
ImTextureID m_texture = 0;
HAL_SimDeviceHandle m_devHandle = 0;
hal::SimDouble m_xHandle;
@@ -164,7 +166,7 @@ static void Field2DWriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler,
}
void FieldInfo::Reset() {
if (m_texture != 0) glDeleteTextures(1, &m_texture);
if (m_texture != 0) gui::DeleteTexture(m_texture);
m_texture = 0;
m_filename.clear();
m_imageWidth = 0;
@@ -182,7 +184,7 @@ void FieldInfo::LoadImage() {
if (wpi::StringRef(result[0]).endswith(".json")) {
LoadJson(result[0]);
} else {
LoadImageImpl(result[0]);
LoadImageImpl(result[0].c_str());
m_top = 0;
m_left = 0;
m_bottom = -1;
@@ -192,7 +194,7 @@ void FieldInfo::LoadImage() {
m_fileOpener.reset();
}
if (m_texture == 0 && !m_filename.empty()) {
if (!LoadImageImpl(m_filename)) m_filename.clear();
if (!LoadImageImpl(m_filename.c_str())) m_filename.clear();
}
}
@@ -274,7 +276,7 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
wpi::sys::path::append(pathname, image);
// load field image
if (!LoadImageImpl(pathname)) return;
if (!LoadImageImpl(pathname.c_str())) return;
// save to field info
m_filename = pathname.str();
@@ -286,15 +288,16 @@ void FieldInfo::LoadJson(const wpi::Twine& jsonfile) {
m_height = height;
}
bool FieldInfo::LoadImageImpl(const wpi::Twine& fn) {
bool FieldInfo::LoadImageImpl(const char* fn) {
wpi::outs() << "GUI: loading field image '" << fn << "'\n";
GLuint oldTexture = m_texture;
if (!LoadTextureFromFile(fn, &m_texture, &m_imageWidth, &m_imageHeight)) {
auto oldTexture = m_texture;
if (!gui::LoadTextureFromFile(fn, &m_texture, &m_imageWidth,
&m_imageHeight)) {
wpi::errs() << "GUI: could not read field image\n";
return false;
}
if (oldTexture != 0) glDeleteTextures(1, &oldTexture);
m_filename = fn.str();
if (oldTexture != 0) gui::DeleteTexture(oldTexture);
m_filename = fn;
return true;
}
@@ -332,9 +335,8 @@ FieldFrameData FieldInfo::GetFrameData() const {
void FieldInfo::Draw(ImDrawList* drawList, const ImVec2& windowPos,
const FieldFrameData& ffd) const {
if (m_texture != 0 && m_imageHeight != 0 && m_imageWidth != 0) {
drawList->AddImage(
reinterpret_cast<ImTextureID>(static_cast<uintptr_t>(m_texture)),
windowPos + ffd.imageMin, windowPos + ffd.imageMax);
drawList->AddImage(m_texture, windowPos + ffd.imageMin,
windowPos + ffd.imageMax);
}
// draw the field "active area" as a yellow boundary box
@@ -379,7 +381,7 @@ void FieldInfo::WriteIni(ImGuiTextBuffer* out) const {
}
void RobotInfo::Reset() {
if (m_texture != 0) glDeleteTextures(1, &m_texture);
if (m_texture != 0) gui::DeleteTexture(m_texture);
m_texture = 0;
m_filename.clear();
}
@@ -387,23 +389,23 @@ void RobotInfo::Reset() {
void RobotInfo::LoadImage() {
if (m_fileOpener && m_fileOpener->ready(0)) {
auto result = m_fileOpener->result();
if (!result.empty()) LoadImageImpl(result[0]);
if (!result.empty()) LoadImageImpl(result[0].c_str());
m_fileOpener.reset();
}
if (m_texture == 0 && !m_filename.empty()) {
if (!LoadImageImpl(m_filename)) m_filename.clear();
if (!LoadImageImpl(m_filename.c_str())) m_filename.clear();
}
}
bool RobotInfo::LoadImageImpl(const wpi::Twine& fn) {
bool RobotInfo::LoadImageImpl(const char* fn) {
wpi::outs() << "GUI: loading robot image '" << fn << "'\n";
GLuint oldTexture = m_texture;
if (!LoadTextureFromFile(fn, &m_texture, nullptr, nullptr)) {
auto oldTexture = m_texture;
if (!gui::LoadTextureFromFile(fn, &m_texture, nullptr, nullptr)) {
wpi::errs() << "GUI: could not read robot image\n";
return false;
}
if (oldTexture != 0) glDeleteTextures(1, &oldTexture);
m_filename = fn.str();
if (oldTexture != 0) gui::DeleteTexture(oldTexture);
m_filename = fn;
return true;
}
@@ -470,8 +472,7 @@ void RobotInfo::Draw(ImDrawList* drawList, const ImVec2& windowPos,
float hitRadius) const {
if (m_texture != 0) {
drawList->AddImageQuad(
reinterpret_cast<ImTextureID>(static_cast<uintptr_t>(m_texture)),
windowPos + rfd.corners[0], windowPos + rfd.corners[1],
m_texture, windowPos + rfd.corners[0], windowPos + rfd.corners[1],
windowPos + rfd.corners[2], windowPos + rfd.corners[3]);
} else {
drawList->AddQuad(windowPos + rfd.corners[0], windowPos + rfd.corners[1],