mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Add simulation GUI plugin
This uses Dear Imgui to provide a cross-platform integrated GUI for robot simulation. The GUI provides fully integrated DS and joystick support so it's not necessary to run the official DS.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2019 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace halsimgui {
|
||||
|
||||
/**
|
||||
* Draw a 2D array of LEDs.
|
||||
*
|
||||
* Values are indices into colors array. Positive values are filled (lit),
|
||||
* negative values are unfilled (dark / border only). The actual color index
|
||||
* is the absolute value of the value - 1. 0 values are not drawn at all
|
||||
* (an empty space is left).
|
||||
*
|
||||
* @param values values array
|
||||
* @param numValues size of values array
|
||||
* @param cols number of columns
|
||||
* @param colors colors array
|
||||
* @param size size of each LED (both horizontal and vertical)
|
||||
* @param spacing spacing between each LED (both horizontal and vertical)
|
||||
*/
|
||||
void DrawLEDs(int* values, int numValues, int cols, const ImU32* colors,
|
||||
float size = 8.0f, float spacing = 6.0f);
|
||||
|
||||
} // namespace halsimgui
|
||||
132
simulation/halsim_gui/src/main/native/include/HALSimGui.h
Normal file
132
simulation/halsim_gui/src/main/native/include/HALSimGui.h
Normal file
@@ -0,0 +1,132 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <functional>
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
|
||||
void HALSIMGUI_Add(void* param, void (*initialize)(void*));
|
||||
void HALSIMGUI_AddExecute(void* param, void (*execute)(void*));
|
||||
void HALSIMGUI_AddWindow(const char* name, void* param, void (*display)(void*),
|
||||
int32_t flags);
|
||||
void HALSIMGUI_AddMainMenu(void* param, void (*menu)(void*));
|
||||
void HALSIMGUI_AddOptionMenu(void* param, void (*menu)(void*));
|
||||
void HALSIMGUI_SetWindowVisibility(const char* name, int32_t visibility);
|
||||
void HALSIMGUI_SetDefaultWindowPos(const char* name, float x, float y);
|
||||
void HALSIMGUI_SetDefaultWindowSize(const char* name, float width,
|
||||
float height);
|
||||
|
||||
} // extern "C"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
namespace halsimgui {
|
||||
|
||||
class HALSimGui {
|
||||
public:
|
||||
static bool Initialize();
|
||||
static void Main(void*);
|
||||
static void Exit(void*);
|
||||
|
||||
/**
|
||||
* Adds feature to GUI. The initialize function is called once, immediately
|
||||
* after the GUI (both GLFW and Dear ImGui) are initialized.
|
||||
*
|
||||
* @param initialize initialization function
|
||||
* @param execute frame execution function
|
||||
*/
|
||||
static void Add(std::function<void()> initialize);
|
||||
|
||||
/**
|
||||
* Adds per-frame executor to GUI. The passed function is called on each
|
||||
* Dear ImGui frame prior to window and menu functions.
|
||||
*
|
||||
* @param execute frame execution function
|
||||
*/
|
||||
static void AddExecute(std::function<void()> execute);
|
||||
|
||||
/**
|
||||
* Adds window to GUI. The display function is called from within a
|
||||
* ImGui::Begin()/End() block. While windows can be created within the
|
||||
* execute function passed to AddExecute(), using this function ensures the
|
||||
* windows are consistently integrated with the rest of the GUI.
|
||||
*
|
||||
* On each Dear ImGui frame, AddExecute() functions are always called prior
|
||||
* to AddWindow display functions. Note that windows may be shaded or
|
||||
* completely hidden, in which case this function will not be called.
|
||||
* It's important to perform any processing steps that must be performed
|
||||
* every frame in the AddExecute() function.
|
||||
*
|
||||
* @param name name of the window (title bar)
|
||||
* @param display window contents display function
|
||||
* @param flags Dear ImGui window flags
|
||||
*/
|
||||
static void AddWindow(const char* name, std::function<void()> display,
|
||||
int flags = 0);
|
||||
|
||||
/**
|
||||
* Adds to GUI's main menu bar. The menu function is called from within a
|
||||
* ImGui::BeginMainMenuBar()/EndMainMenuBar() block. Usually it's only
|
||||
* appropriate to create a menu with ImGui::BeginMenu()/EndMenu() inside of
|
||||
* this function.
|
||||
*
|
||||
* On each Dear ImGui frame, AddExecute() functions are always called prior
|
||||
* to AddMainMenu menu functions.
|
||||
*
|
||||
* @param menu menu display function
|
||||
*/
|
||||
static void AddMainMenu(std::function<void()> menu);
|
||||
|
||||
/**
|
||||
* Adds to GUI's option menu. The menu function is called from within a
|
||||
* ImGui::BeginMenu()/EndMenu() block. Usually it's only appropriate to
|
||||
* create menu items inside of this function.
|
||||
*
|
||||
* On each Dear ImGui frame, AddExecute() functions are always called prior
|
||||
* to AddMainMenu menu functions.
|
||||
*
|
||||
* @param menu menu display function
|
||||
*/
|
||||
static void AddOptionMenu(std::function<void()> menu);
|
||||
|
||||
enum WindowVisibility { kHide = 0, kShow, kDisabled };
|
||||
|
||||
/**
|
||||
* Sets visibility of window added with AddWindow().
|
||||
*
|
||||
* @param name window name (same as name passed to AddWindow())
|
||||
* @param visibility 0=hide, 1=show, 2=disabled (force-hide)
|
||||
*/
|
||||
static void SetWindowVisibility(const char* name,
|
||||
WindowVisibility visibility);
|
||||
|
||||
/**
|
||||
* Sets default position of window added with AddWindow().
|
||||
*
|
||||
* @param name window name (same as name passed to AddWindow())
|
||||
* @param x x location of upper left corner
|
||||
* @param y y location of upper left corner
|
||||
*/
|
||||
static void SetDefaultWindowPos(const char* name, float x, float y);
|
||||
|
||||
/**
|
||||
* Sets default size of window added with AddWindow().
|
||||
*
|
||||
* @param name window name (same as name passed to AddWindow())
|
||||
* @param width width
|
||||
* @param height height
|
||||
*/
|
||||
static void SetDefaultWindowSize(const char* name, float width, float height);
|
||||
};
|
||||
|
||||
} // namespace halsimgui
|
||||
|
||||
#endif // __cplusplus
|
||||
91
simulation/halsim_gui/src/main/native/include/SimDeviceGui.h
Normal file
91
simulation/halsim_gui/src/main/native/include/SimDeviceGui.h
Normal file
@@ -0,0 +1,91 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2019 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <hal/Value.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include <functional>
|
||||
|
||||
#include <imgui.h>
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
|
||||
void HALSIMGUI_DeviceTreeHide(const char* name);
|
||||
void HALSIMGUI_DeviceTreeAdd(void* param, void (*execute)(void*));
|
||||
HAL_Bool HALSIMGUI_DeviceTreeDisplayValue(const char* name, HAL_Bool readonly,
|
||||
struct HAL_Value* value,
|
||||
const char** options,
|
||||
int32_t numOptions);
|
||||
HAL_Bool HALSIMGUI_DeviceTreeStartDevice(const char* label, int32_t flags);
|
||||
void HALSIMGUI_DeviceTreeFinishDevice(void);
|
||||
|
||||
} // extern "C"
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
namespace halsimgui {
|
||||
|
||||
class SimDeviceGui {
|
||||
public:
|
||||
static void Initialize();
|
||||
|
||||
/**
|
||||
* Hides device on tree.
|
||||
*
|
||||
* @param name device name
|
||||
*/
|
||||
static void Hide(const char* name);
|
||||
|
||||
/**
|
||||
* Adds device to tree. The execute function is called from within the
|
||||
* device tree window context on every frame, so it should implement an
|
||||
* TreeNodeEx() block for each device to display.
|
||||
*
|
||||
* @param execute execute function
|
||||
*/
|
||||
static void Add(std::function<void()> execute);
|
||||
|
||||
/**
|
||||
* Displays device value formatted the same way as SimDevice device values.
|
||||
*
|
||||
* @param name value name
|
||||
* @param readonly prevent value from being modified by the user
|
||||
* @param value value contents (modified in place)
|
||||
* @param options options array for enum values
|
||||
* @param numOptions size of options array for enum values
|
||||
* @return True if value was modified by the user
|
||||
*/
|
||||
static bool DisplayValue(const char* name, bool readonly, HAL_Value* value,
|
||||
const char** options = nullptr,
|
||||
int32_t numOptions = 0);
|
||||
|
||||
/**
|
||||
* Wraps ImGui::CollapsingHeader() to provide consistency and open
|
||||
* persistence. As with the ImGui function, returns true if the tree node
|
||||
* is expanded. If returns true, call StopDevice() to finish the block.
|
||||
*
|
||||
* @param label label
|
||||
* @param flags ImGuiTreeNodeFlags flags
|
||||
* @return True if expanded
|
||||
*/
|
||||
static bool StartDevice(const char* label, ImGuiTreeNodeFlags flags = 0);
|
||||
|
||||
/**
|
||||
* Finish a device block started with StartDevice().
|
||||
*/
|
||||
static void FinishDevice();
|
||||
};
|
||||
|
||||
} // namespace halsimgui
|
||||
|
||||
#endif // __cplusplus
|
||||
Reference in New Issue
Block a user