mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
SCRIPT Move cc files
This commit is contained in:
committed by
Peter Johnson
parent
10b4a0c971
commit
7ca1be9bae
249
wpigui/src/main/native/include/wpi/gui/portable-file-dialogs.h
Normal file
249
wpigui/src/main/native/include/wpi/gui/portable-file-dialogs.h
Normal file
@@ -0,0 +1,249 @@
|
||||
//
|
||||
// Portable File Dialogs
|
||||
//
|
||||
// Copyright © 2018—2020 Sam Hocevar <sam@hocevar.net>
|
||||
//
|
||||
// This library is free software. It comes without any warranty, to
|
||||
// the extent permitted by applicable law. You can redistribute it
|
||||
// and/or modify it under the terms of the Do What the **** You Want
|
||||
// to Public License, Version 2, as published by the WTFPL Task Force.
|
||||
// See http://www.wtfpl.net/ for more details.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string> // std::string
|
||||
#include <memory> // std::shared_ptr
|
||||
#include <map> // std::map
|
||||
#include <vector>
|
||||
|
||||
namespace pfd
|
||||
{
|
||||
|
||||
enum class button
|
||||
{
|
||||
cancel = -1,
|
||||
ok,
|
||||
yes,
|
||||
no,
|
||||
abort,
|
||||
retry,
|
||||
ignore,
|
||||
};
|
||||
|
||||
enum class choice
|
||||
{
|
||||
ok = 0,
|
||||
ok_cancel,
|
||||
yes_no,
|
||||
yes_no_cancel,
|
||||
retry_cancel,
|
||||
abort_retry_ignore,
|
||||
};
|
||||
|
||||
enum class icon
|
||||
{
|
||||
info = 0,
|
||||
warning,
|
||||
error,
|
||||
question,
|
||||
};
|
||||
|
||||
// Additional option flags for various dialog constructors
|
||||
enum class opt : uint8_t
|
||||
{
|
||||
none = 0,
|
||||
// For file open, allow multiselect.
|
||||
multiselect = 0x1,
|
||||
// For file save, force overwrite and disable the confirmation dialog.
|
||||
force_overwrite = 0x2,
|
||||
// For folder select, force path to be the provided argument instead
|
||||
// of the last opened directory, which is the Microsoft-recommended,
|
||||
// user-friendly behaviour.
|
||||
force_path = 0x4,
|
||||
};
|
||||
|
||||
inline opt operator |(opt a, opt b) { return opt(uint8_t(a) | uint8_t(b)); }
|
||||
inline bool operator &(opt a, opt b) { return bool(uint8_t(a) & uint8_t(b)); }
|
||||
|
||||
// The settings class, only exposing to the user a way to set verbose mode
|
||||
// and to force a rescan of installed desktop helpers (zenity, kdialog…).
|
||||
class settings
|
||||
{
|
||||
public:
|
||||
static bool available();
|
||||
|
||||
static void verbose(bool value);
|
||||
static void rescan();
|
||||
|
||||
protected:
|
||||
explicit settings(bool resync = false);
|
||||
|
||||
bool check_program(std::string const &program);
|
||||
|
||||
constexpr bool is_osascript() const {
|
||||
#if __APPLE__
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
bool is_zenity() const {
|
||||
return flags(flag::has_zenity) ||
|
||||
flags(flag::has_matedialog) ||
|
||||
flags(flag::has_qarma);
|
||||
}
|
||||
bool is_kdialog() const { return flags(flag::has_kdialog); }
|
||||
|
||||
enum class flag
|
||||
{
|
||||
is_scanned = 0,
|
||||
is_verbose,
|
||||
|
||||
has_zenity,
|
||||
has_matedialog,
|
||||
has_qarma,
|
||||
has_kdialog,
|
||||
is_vista,
|
||||
|
||||
max_flag,
|
||||
};
|
||||
|
||||
// Static array of flags for internal state
|
||||
bool const &flags(flag in_flag) const;
|
||||
|
||||
// Non-const getter for the static array of flags
|
||||
bool &flags(flag in_flag);
|
||||
};
|
||||
|
||||
// Internal classes, not to be used by client applications
|
||||
namespace internal
|
||||
{
|
||||
|
||||
// Process wait timeout, in milliseconds
|
||||
constexpr int default_wait_timeout = 20;
|
||||
|
||||
class executor;
|
||||
|
||||
class dialog : protected settings
|
||||
{
|
||||
public:
|
||||
virtual ~dialog();
|
||||
bool ready(int timeout = default_wait_timeout) const;
|
||||
bool kill() const;
|
||||
|
||||
protected:
|
||||
explicit dialog();
|
||||
|
||||
std::vector<std::string> desktop_helper() const;
|
||||
static std::string buttons_to_name(choice _choice);
|
||||
static std::string get_icon_name(icon _icon);
|
||||
|
||||
std::string powershell_quote(std::string const &str) const;
|
||||
std::string osascript_quote(std::string const &str) const;
|
||||
std::string shell_quote(std::string const &str) const;
|
||||
|
||||
// Keep handle to executing command
|
||||
std::shared_ptr<executor> m_async;
|
||||
};
|
||||
|
||||
class file_dialog : public internal::dialog
|
||||
{
|
||||
protected:
|
||||
enum type
|
||||
{
|
||||
open,
|
||||
save,
|
||||
folder,
|
||||
};
|
||||
|
||||
file_dialog(type in_type,
|
||||
std::string const &title,
|
||||
std::string const &default_path = "",
|
||||
std::vector<std::string> const &filters = {},
|
||||
opt options = opt::none);
|
||||
|
||||
protected:
|
||||
std::string string_result();
|
||||
std::vector<std::string> vector_result();
|
||||
|
||||
class Impl;
|
||||
std::shared_ptr<Impl> m_impl;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
//
|
||||
// The notify widget
|
||||
//
|
||||
|
||||
class notify : public internal::dialog
|
||||
{
|
||||
public:
|
||||
notify(std::string const &title,
|
||||
std::string const &message,
|
||||
icon _icon = icon::info);
|
||||
};
|
||||
|
||||
//
|
||||
// The message widget
|
||||
//
|
||||
|
||||
class message : public internal::dialog
|
||||
{
|
||||
public:
|
||||
message(std::string const &title,
|
||||
std::string const &text,
|
||||
choice _choice = choice::ok_cancel,
|
||||
icon _icon = icon::info);
|
||||
|
||||
button result();
|
||||
|
||||
private:
|
||||
// Some extra logic to map the exit code to button number
|
||||
std::map<int, button> m_mappings;
|
||||
};
|
||||
|
||||
//
|
||||
// The open_file, save_file, and open_folder widgets
|
||||
//
|
||||
|
||||
class open_file : public internal::file_dialog
|
||||
{
|
||||
public:
|
||||
explicit open_file(std::string const &title,
|
||||
std::string const &default_path = "",
|
||||
std::vector<std::string> const &filters = { "All Files", "*" },
|
||||
opt options = opt::none)
|
||||
: file_dialog(type::open, title, default_path, filters, options)
|
||||
{}
|
||||
|
||||
std::vector<std::string> result() { return vector_result(); }
|
||||
};
|
||||
|
||||
class save_file : public internal::file_dialog
|
||||
{
|
||||
public:
|
||||
explicit save_file(std::string const &title,
|
||||
std::string const &default_path = "",
|
||||
std::vector<std::string> const &filters = { "All Files", "*" },
|
||||
opt options = opt::none)
|
||||
: file_dialog(type::save, title, default_path, filters, options)
|
||||
{}
|
||||
|
||||
std::string result() { return string_result(); }
|
||||
};
|
||||
|
||||
class select_folder : public internal::file_dialog
|
||||
{
|
||||
public:
|
||||
explicit select_folder(std::string const &title,
|
||||
std::string const &default_path = "",
|
||||
opt options = opt::none)
|
||||
: file_dialog(type::folder, title, default_path, {}, options)
|
||||
{}
|
||||
|
||||
std::string result() { return string_result(); }
|
||||
};
|
||||
|
||||
} // namespace pfd
|
||||
486
wpigui/src/main/native/include/wpi/gui/wpigui.hpp
Normal file
486
wpigui/src/main/native/include/wpi/gui/wpigui.hpp
Normal file
@@ -0,0 +1,486 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
extern "C" struct GLFWwindow;
|
||||
|
||||
namespace wpi::gui {
|
||||
|
||||
struct Context;
|
||||
|
||||
/**
|
||||
* Creates GUI context. Must be called prior to calling any other functions.
|
||||
*/
|
||||
void CreateContext();
|
||||
|
||||
/**
|
||||
* Destroys GUI context.
|
||||
*/
|
||||
void DestroyContext();
|
||||
|
||||
/**
|
||||
* Gets GUI context.
|
||||
*
|
||||
* @return context
|
||||
*/
|
||||
Context* GetCurrentContext();
|
||||
|
||||
/**
|
||||
* Sets GUI context.
|
||||
*
|
||||
* @param context context
|
||||
*/
|
||||
void SetCurrentContext(Context* context);
|
||||
|
||||
/**
|
||||
* Initializes the GUI.
|
||||
*
|
||||
* @param title main application window title
|
||||
* @param width main application window width
|
||||
* @param height main application window height
|
||||
* @param configFlags ImGui configuration flags
|
||||
*/
|
||||
bool Initialize(const char* title, int width, int height,
|
||||
ImGuiConfigFlags configFlags = ImGuiConfigFlags_None);
|
||||
|
||||
/**
|
||||
* Runs main GUI loop. On some OS'es this must be called from the main thread.
|
||||
* Does not return until Exit() is called.
|
||||
*/
|
||||
void Main();
|
||||
|
||||
/**
|
||||
* Exits main GUI loop when current loop iteration finishes.
|
||||
* Safe to call from any thread, including from within main GUI loop.
|
||||
*/
|
||||
void Exit();
|
||||
|
||||
/**
|
||||
* Adds initializer to GUI. The passed function is called once, immediately
|
||||
* after the GUI (both GLFW and Dear ImGui) are initialized in Initialize().
|
||||
* To have any effect, must be called prior to Initialize().
|
||||
*
|
||||
* @param initialize initialization function
|
||||
*/
|
||||
void AddInit(std::function<void()> initialize);
|
||||
|
||||
/**
|
||||
* Adds window scaler function. The passed function is called once during
|
||||
* Initialize() if the window scale is not 1.0. To have any effect, must
|
||||
* be called prior to Initialize().
|
||||
*
|
||||
* @param windowScaler window scaler function
|
||||
*/
|
||||
void AddWindowScaler(std::function<void(float scale)> windowScaler);
|
||||
|
||||
/**
|
||||
* Adds per-frame executor to GUI. The passed function is called on each
|
||||
* Dear ImGui frame prior to any of the late execute functions.
|
||||
*
|
||||
* @param execute frame execution function
|
||||
*/
|
||||
void AddEarlyExecute(std::function<void()> execute);
|
||||
|
||||
/**
|
||||
* Adds per-frame executor to GUI. The passed function is called on each
|
||||
* Dear ImGui frame after all of the early execute functions.
|
||||
*
|
||||
* @param execute frame execution function
|
||||
*/
|
||||
void AddLateExecute(std::function<void()> execute);
|
||||
|
||||
/**
|
||||
* Customizes save/load behavior.
|
||||
*
|
||||
* By default, the integrated ImGui functions are used for this;
|
||||
* ImGui::LoadIniSettingsFromDisk(io.IniFilename) is called at startup, and
|
||||
* ImGui default automatic save file handling is used via io.IniFilename.
|
||||
*
|
||||
* Calling this function results in the load function being called at startup,
|
||||
* io.IniFilename set to null (which disables ImGui's integrated file saving),
|
||||
* and the save function being called when io.WantSaveIniSettings is true.
|
||||
* The loadIni function should call ImGui::LoadIniSettingsFromMemory() to load
|
||||
* ImGui save data, and the save function should call
|
||||
* ImGui::SaveIniSettingsToMemory() to get ImGui save data.
|
||||
*
|
||||
* The load function is called PRIOR to AddInit() functions, and the loadIni
|
||||
* function is called AFTER to AddInit() functions. This allows initialize
|
||||
* functions that use custom storage to handle the loaded values, and initialize
|
||||
* functions that use INI storage to add hooks prior to the load INI occurring.
|
||||
*
|
||||
* This must be called prior to Initialize().
|
||||
*
|
||||
* @param load load function
|
||||
* @param loadIni load INI function
|
||||
* @param save save function; false is passed periodically, true is passed once
|
||||
* when the main loop is exiting
|
||||
*/
|
||||
void ConfigureCustomSaveSettings(std::function<void()> load,
|
||||
std::function<void()> loadIni,
|
||||
std::function<void(bool exiting)> save);
|
||||
|
||||
/**
|
||||
* Gets GLFW window handle.
|
||||
*/
|
||||
GLFWwindow* GetSystemWindow();
|
||||
|
||||
/**
|
||||
* Adds an application icon. Multiple icons (of different sizes) may be
|
||||
* set. This must be called prior to initialization to have an effect.
|
||||
*
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
* @return False if image data could not be read
|
||||
*/
|
||||
bool AddIcon(const unsigned char* data, int len);
|
||||
|
||||
inline bool AddIcon(std::string_view data) {
|
||||
return AddIcon(reinterpret_cast<const unsigned char*>(data.data()),
|
||||
static_cast<int>(data.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a font to the GUI. The passed function is called during
|
||||
* initialization as many times as necessary to create a range of sizes.
|
||||
*
|
||||
* @param name font name
|
||||
* @param makeFont font creation / loader function
|
||||
* @return Font index for later use with GetFont()
|
||||
*/
|
||||
int AddFont(
|
||||
const char* name,
|
||||
std::function<ImFont*(ImGuiIO& io, float size, const ImFontConfig* cfg)>
|
||||
makeFont);
|
||||
|
||||
/**
|
||||
* Adds a default font option. This is the font used for menus, etc. If the
|
||||
* font is selected by the user as the default font, the passed function is
|
||||
* called during initialization as many times as necessary to create a range of
|
||||
* sizes. If not selected, the font is not loaded (to always load a font,
|
||||
* use AddFont() instead).
|
||||
*
|
||||
* @param name font name
|
||||
* @param makeFont font creation / loader function
|
||||
*/
|
||||
void AddDefaultFont(
|
||||
const char* name,
|
||||
std::function<ImFont*(ImGuiIO& io, float size, const ImFontConfig* cfg)>
|
||||
makeFont);
|
||||
|
||||
/**
|
||||
* Gets a font added with AddFont() with the appropriate font size for
|
||||
* the current scaling of the GUI.
|
||||
*
|
||||
* @param font font index returned by AddFont()
|
||||
* @return Font pointer
|
||||
*/
|
||||
ImFont* GetFont(int font);
|
||||
|
||||
enum Style { kStyleClassic = 0, kStyleDark, kStyleLight, kStyleDeepDark };
|
||||
|
||||
/**
|
||||
* Sets the ImGui style. Using this function makes this setting persistent.
|
||||
*
|
||||
* @param style Style
|
||||
*/
|
||||
void SetStyle(Style style);
|
||||
|
||||
/**
|
||||
* Sets the FPS limit. Using this function makes this setting persistent.
|
||||
*
|
||||
* @param fps FPS (0=vsync)
|
||||
*/
|
||||
void SetFPS(int fps);
|
||||
|
||||
/**
|
||||
* Sets the clear (background) color.
|
||||
*
|
||||
* @param color Color
|
||||
*/
|
||||
void SetClearColor(ImVec4 color);
|
||||
|
||||
/**
|
||||
* Gets the (platform-specific) absolute directory for save files.
|
||||
*
|
||||
* @return Absolute path, including trailing "/". Empty string if directory
|
||||
* could not be determined.
|
||||
*/
|
||||
std::string GetPlatformSaveFileDir();
|
||||
|
||||
/**
|
||||
* Configures a save file (.ini) in a platform specific location. On Windows,
|
||||
* the .ini is saved in %APPDATA%; on macOS the .ini is saved in
|
||||
* ~/Library/Preferences; on Linux the .ini is stored in $XDG_CONFIG_HOME or
|
||||
* ~/.config if the former is not defined. This must be called before
|
||||
* gui::Initialize().
|
||||
*/
|
||||
void ConfigurePlatformSaveFile(const std::string& name);
|
||||
|
||||
/**
|
||||
* Emits a View menu (e.g. for a main menu bar) that allows setting of
|
||||
* style and zoom. Internally starts with ImGui::BeginMenu("View").
|
||||
*/
|
||||
void EmitViewMenu();
|
||||
|
||||
/**
|
||||
* Pixel formats for texture pixel data.
|
||||
*/
|
||||
enum PixelFormat { kPixelRGBA, kPixelBGRA };
|
||||
|
||||
/**
|
||||
* Creates a texture from pixel data.
|
||||
*
|
||||
* @param format pixel format
|
||||
* @param width image width
|
||||
* @param height image height
|
||||
* @param data pixel data
|
||||
* @return Texture
|
||||
*/
|
||||
ImTextureID CreateTexture(PixelFormat format, int width, int height,
|
||||
const unsigned char* data);
|
||||
|
||||
/**
|
||||
* Updates a texture from pixel data.
|
||||
* The passed-in width and height must match the width and height of the
|
||||
* texture.
|
||||
*
|
||||
* @param texture texture
|
||||
* @param format pixel format
|
||||
* @param width texture width
|
||||
* @param height texture height
|
||||
* @param data pixel data
|
||||
*/
|
||||
void UpdateTexture(ImTextureID texture, PixelFormat format, int width,
|
||||
int height, const unsigned char* data);
|
||||
|
||||
/**
|
||||
* Updates a texture from image data.
|
||||
* The pixel format of the texture must be RGBA. The passed-in width and
|
||||
* height must match the width and height of the texture. If the width and
|
||||
* height of the image differ from the passed-in width and height, a new
|
||||
* texture is created (note this may be inefficient).
|
||||
*
|
||||
* @param texture texture (pointer, may be updated)
|
||||
* @param width texture width
|
||||
* @param height texture height
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool UpdateTextureFromImage(ImTextureID* texture, int width, int height,
|
||||
const unsigned char* data, int len);
|
||||
|
||||
/**
|
||||
* Creates a texture from an image file.
|
||||
*
|
||||
* @param filename filename
|
||||
* @param out_texture texture (output)
|
||||
* @param out_width image width (output)
|
||||
* @param out_height image height (output)
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool CreateTextureFromFile(const char* filename, ImTextureID* out_texture,
|
||||
int* out_width, int* out_height);
|
||||
|
||||
/**
|
||||
* Creates a texture from image data.
|
||||
*
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
* @param out_texture texture (output)
|
||||
* @param out_width image width (output)
|
||||
* @param out_height image height (output)
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool CreateTextureFromImage(const unsigned char* data, int len,
|
||||
ImTextureID* out_texture, int* out_width,
|
||||
int* out_height);
|
||||
|
||||
/**
|
||||
* Deletes a texture.
|
||||
*
|
||||
* @param texture texture
|
||||
*/
|
||||
void DeleteTexture(ImTextureID texture);
|
||||
|
||||
/**
|
||||
* RAII wrapper around ImTextureID. Also keeps track of width, height, and
|
||||
* pixel format.
|
||||
*/
|
||||
class Texture {
|
||||
public:
|
||||
Texture() = default;
|
||||
|
||||
/**
|
||||
* Constructs a texture from pixel data.
|
||||
*
|
||||
* @param format pixel format
|
||||
* @param width image width
|
||||
* @param height image height
|
||||
* @param data pixel data
|
||||
*/
|
||||
Texture(PixelFormat format, int width, int height, const unsigned char* data)
|
||||
: m_format{format}, m_width{width}, m_height{height} {
|
||||
m_texture = CreateTexture(format, width, height, data);
|
||||
}
|
||||
|
||||
Texture(const Texture&) = delete;
|
||||
Texture(Texture&& oth)
|
||||
: m_texture{oth.m_texture},
|
||||
m_format{oth.m_format},
|
||||
m_width{oth.m_width},
|
||||
m_height{oth.m_height} {
|
||||
oth.m_texture = 0; // NOLINT
|
||||
}
|
||||
|
||||
Texture& operator=(const Texture&) = delete;
|
||||
Texture& operator=(Texture&& oth) {
|
||||
if (m_texture) {
|
||||
DeleteTexture(m_texture);
|
||||
}
|
||||
m_texture = oth.m_texture;
|
||||
oth.m_texture = 0; // NOLINT
|
||||
m_format = oth.m_format;
|
||||
m_width = oth.m_width;
|
||||
m_height = oth.m_height;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Texture() {
|
||||
if (m_texture) {
|
||||
DeleteTexture(m_texture);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates to true if the texture is valid.
|
||||
*/
|
||||
explicit operator bool() const { return m_texture; }
|
||||
|
||||
/**
|
||||
* Implicit conversion to ImTextureID.
|
||||
*/
|
||||
operator ImTextureID() const { return m_texture; } // NOLINT
|
||||
|
||||
/**
|
||||
* Gets the texture pixel format.
|
||||
*
|
||||
* @return pixel format
|
||||
*/
|
||||
PixelFormat GetFormat() const { return m_format; }
|
||||
|
||||
/**
|
||||
* Gets the texture width.
|
||||
*
|
||||
* @return width
|
||||
*/
|
||||
int GetWidth() const { return m_width; }
|
||||
|
||||
/**
|
||||
* Gets the texture height.
|
||||
*
|
||||
* @return height
|
||||
*/
|
||||
int GetHeight() const { return m_height; }
|
||||
|
||||
/**
|
||||
* Updates the texture from pixel data.
|
||||
* The image data size and format is assumed to match that of the texture.
|
||||
*
|
||||
* @param format pixel format
|
||||
* @param data pixel data
|
||||
*/
|
||||
void Update(const unsigned char* data) {
|
||||
UpdateTexture(m_texture, m_format, m_width, m_height, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the texture from image data.
|
||||
* The pixel format of the texture must be RGBA. If the width and height of
|
||||
* the image differ from the texture width and height, a new texture is
|
||||
* created (note this may be inefficient).
|
||||
*
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
*
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
bool UpdateFromImage(const unsigned char* data, int len) {
|
||||
return UpdateTextureFromImage(&m_texture, m_width, m_height, data, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a texture by loading an image file.
|
||||
*
|
||||
* @param filename filename
|
||||
*
|
||||
* @return Texture, or invalid (empty) texture on failure.
|
||||
*/
|
||||
static Texture CreateFromFile(const char* filename) {
|
||||
Texture texture;
|
||||
if (!CreateTextureFromFile(filename, &texture.m_texture, &texture.m_width,
|
||||
&texture.m_height)) {
|
||||
return {};
|
||||
}
|
||||
return texture;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a texture from image data.
|
||||
*
|
||||
* @param data image data
|
||||
* @param len image data length
|
||||
*
|
||||
* @return Texture, or invalid (empty) texture on failure.
|
||||
*/
|
||||
static Texture CreateFromImage(const unsigned char* data, int len) {
|
||||
Texture texture;
|
||||
if (!CreateTextureFromImage(data, len, &texture.m_texture, &texture.m_width,
|
||||
&texture.m_height)) {
|
||||
return {};
|
||||
}
|
||||
return texture;
|
||||
}
|
||||
|
||||
private:
|
||||
ImTextureID m_texture = nullptr;
|
||||
PixelFormat m_format = kPixelRGBA;
|
||||
int m_width = 0;
|
||||
int m_height = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get square of distance between two ImVec2's.
|
||||
*
|
||||
* @param a first ImVec
|
||||
* @param b second ImVec
|
||||
*
|
||||
* @return Distance^2 between a and b.
|
||||
*/
|
||||
inline float GetDistSquared(const ImVec2& a, const ImVec2& b) {
|
||||
float deltaX = b.x - a.x;
|
||||
float deltaY = b.y - a.y;
|
||||
return deltaX * deltaX + deltaY * deltaY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maximize fit in "window" while preserving aspect ratio. Min and max
|
||||
* passed-in values are modified such that the object maximally fits.
|
||||
*
|
||||
* @param min upper left corner of window (modified to fit)
|
||||
* @param max lower right corner of window (modified to fit)
|
||||
* @param width width of object to fit
|
||||
* @param height height of object to fit
|
||||
*/
|
||||
void MaxFit(ImVec2* min, ImVec2* max, float width, float height);
|
||||
|
||||
} // namespace wpi::gui
|
||||
92
wpigui/src/main/native/include/wpi/gui/wpigui_internal.hpp
Normal file
92
wpigui/src/main/native/include/wpi/gui/wpigui_internal.hpp
Normal file
@@ -0,0 +1,92 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <imgui.h>
|
||||
|
||||
namespace wpi::gui {
|
||||
|
||||
struct SavedSettings {
|
||||
bool loadedWidthHeight = false;
|
||||
int width;
|
||||
int height;
|
||||
bool maximized = false;
|
||||
int xPos = -1;
|
||||
int yPos = -1;
|
||||
int userScale = 2;
|
||||
int style = 0;
|
||||
int fps = 120;
|
||||
std::string defaultFontName = "Proggy Dotted";
|
||||
};
|
||||
|
||||
constexpr int kFontScaledLevels = 9;
|
||||
|
||||
struct Context : public SavedSettings {
|
||||
std::atomic_bool exit{false};
|
||||
|
||||
std::string title;
|
||||
int defaultWidth;
|
||||
int defaultHeight;
|
||||
bool isPlatformRendering{false};
|
||||
|
||||
GLFWwindow* window = nullptr;
|
||||
|
||||
std::function<void()> loadSettings;
|
||||
std::function<void()> loadIniSettings;
|
||||
std::function<void(bool exiting)> saveSettings;
|
||||
std::vector<std::function<void()>> initializers;
|
||||
std::vector<std::function<void(float scale)>> windowScalers;
|
||||
struct FontMaker {
|
||||
FontMaker(
|
||||
std::string name, bool defaultOnly,
|
||||
std::function<ImFont*(ImGuiIO& io, float size, const ImFontConfig* cfg)>
|
||||
func)
|
||||
: name{std::move(name)},
|
||||
defaultOnly{defaultOnly},
|
||||
func{std::move(func)} {}
|
||||
|
||||
std::string name;
|
||||
bool defaultOnly;
|
||||
std::function<ImFont*(ImGuiIO& io, float size, const ImFontConfig* cfg)>
|
||||
func;
|
||||
};
|
||||
std::vector<FontMaker> makeFonts;
|
||||
|
||||
ImVec4 clearColor = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
std::vector<std::function<void()>> earlyExecutors;
|
||||
std::vector<std::function<void()>> lateExecutors;
|
||||
|
||||
int fontScale = 2; // updated by main loop
|
||||
std::vector<ImFont*> fonts;
|
||||
|
||||
std::vector<GLFWimage> icons;
|
||||
|
||||
std::string iniPath = "imgui.ini";
|
||||
bool resetOnExit = false;
|
||||
|
||||
bool reloadFonts = false; // reload fonts in next PlatformRenderFrame()
|
||||
};
|
||||
|
||||
extern Context* gContext;
|
||||
|
||||
void PlatformCreateContext();
|
||||
void PlatformDestroyContext();
|
||||
void PlatformGlfwInitHints();
|
||||
void PlatformGlfwWindowHints();
|
||||
bool PlatformInitRenderer();
|
||||
void PlatformRenderFrame();
|
||||
void PlatformShutdown();
|
||||
void PlatformFramebufferSizeChanged(int width, int height);
|
||||
|
||||
void CommonRenderFrame();
|
||||
|
||||
} // namespace wpi::gui
|
||||
18
wpigui/src/main/native/include/wpi/gui/wpigui_openurl.hpp
Normal file
18
wpigui/src/main/native/include/wpi/gui/wpigui_openurl.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace wpi::gui {
|
||||
|
||||
/**
|
||||
* Opens a URL in the default browser.
|
||||
*
|
||||
* @param url URL to open
|
||||
*/
|
||||
void OpenURL(const std::string& url);
|
||||
|
||||
} // namespace wpi::gui
|
||||
Reference in New Issue
Block a user