feat(plugin): add menu and allow window to toggle

This commit is contained in:
Anthony Berg 2024-04-14 18:45:40 +01:00
parent 50515cc1ec
commit 0b8c2410c7
8 changed files with 160 additions and 69 deletions

View File

@ -1,6 +1,9 @@
file(GLOB PLUGIN_SRC "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") file(GLOB PLUGIN_SRC "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
file(GLOB PLUGIN_HDR "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
file(GLOB UI_SRC "${CMAKE_CURRENT_SOURCE_DIR}/ui/*.cpp")
file(GLOB UI_HDR "${CMAKE_CURRENT_SOURCE_DIR}/ui/*.h")
add_library(XPL-Checklist-Connector MODULE ${PLUGIN_SRC}) add_library(XPL-Checklist-Connector MODULE ${PLUGIN_SRC} ${PLUGIN_HDR} ${UI_SRC} ${UI_HDR})
# Set compile options for XPLM # Set compile options for XPLM
add_definitions(-DXPLM200=1 -DXPLM210=1 -DXPLM300=1 -DXPLM301=1 -DXPLM302=1 -DXPLM303=1 -DXPLM401=1) add_definitions(-DXPLM200=1 -DXPLM210=1 -DXPLM300=1 -DXPLM301=1 -DXPLM302=1 -DXPLM303=1 -DXPLM401=1)

View File

@ -1,5 +1,5 @@
#include "XPLMDisplay.h" #include "XPLMPlugin.h"
#include "XPLMGraphics.h" #include "ui/ui.h"
#include <cstring> #include <cstring>
#if IBM #if IBM
@ -32,16 +32,6 @@
#error This is made to be compiled against the XPLM401 SDK #error This is made to be compiled against the XPLM401 SDK
#endif #endif
// Opaque handle for the window
static XPLMWindowID g_window;
// Callbacks for creating window
void draw_window(XPLMWindowID in_window_id, void * in_refcon);
int dummy_mouse_handler(XPLMWindowID in_window_id, int x, int y, int is_down, void * in_refcon) { return 0; }
XPLMCursorStatus dummy_cursor_status_handler(XPLMWindowID in_window_id, int x, int y, void * in_refcon) { return xplm_CursorDefault; }
int dummy_wheel_handler(XPLMWindowID in_window_id, int x, int y, int wheel, int clicks, void * in_refcon) { return 0; }
void dummy_key_handler(XPLMWindowID in_window_id, char key, XPLMKeyFlags flags, char virtual_key, void * in_refcon, int losing_focus) { }
PLUGIN_API int XPluginStart(char * outName, char * outSig, char * outDesc) { PLUGIN_API int XPluginStart(char * outName, char * outSig, char * outDesc) {
// Defining plugin details // Defining plugin details
@ -49,40 +39,9 @@ PLUGIN_API int XPluginStart(char * outName, char * outSig, char * outDesc) {
strcpy(outSig, "io.anthonyberg.checklistplugin"); strcpy(outSig, "io.anthonyberg.checklistplugin");
strcpy(outDesc, "Plugin to connect and complete actions in X-Plane for the Checklist Tester"); strcpy(outDesc, "Plugin to connect and complete actions in X-Plane for the Checklist Tester");
XPLMCreateWindow_t params; UI::Init();
params.structSize = sizeof(params);
params.visible = 1;
params.drawWindowFunc = draw_window;
// Register mandatory "dummy" handlers
params.handleMouseClickFunc = dummy_mouse_handler;
params.handleRightClickFunc = dummy_mouse_handler;
params.handleMouseWheelFunc = dummy_wheel_handler;
params.handleKeyFunc = dummy_key_handler;
params.handleCursorFunc = dummy_cursor_status_handler;
params.refcon = nullptr;
params.layer = xplm_WindowLayerFloatingWindows;
// Newest X-Plane GUI styling
params.decorateAsFloatingWindow = xplm_WindowDecorationRoundRectangle;
// Set the window's initial bounds return 1;
int left, bottom, right, top;
XPLMGetScreenBoundsGlobal(&left, &top, &right, &bottom);
params.left = left + 50;
params.bottom = bottom + 150;
params.right = params.left + 200;
params.top = params.bottom + 200;
g_window = XPLMCreateWindowEx(&params);
// Make window free floating window
XPLMSetWindowPositioningMode(g_window, xplm_WindowPositionFree, -1);
// Window resizing limits
XPLMSetWindowResizingLimits(g_window, 200, 200, 300, 300);
XPLMSetWindowTitle(g_window, "Checklist Tester");
return g_window != nullptr;
} }
PLUGIN_API int XPluginEnable(void) { PLUGIN_API int XPluginEnable(void) {
@ -94,27 +53,6 @@ PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFromWho, int inMessage, voi
PLUGIN_API void XPluginDisable(void) { } PLUGIN_API void XPluginDisable(void) { }
PLUGIN_API void XPluginStop(void) { PLUGIN_API void XPluginStop(void) {
// Cleaning up the window // Cleaning up UI components
XPLMDestroyWindow(g_window); UI::Destroy();
g_window = nullptr;
}
void draw_window(XPLMWindowID in_window_id, void * in_refcon) {
// Set OpenGL state before drawing
XPLMSetGraphicsState(
0, // No fog
0, // 0 texture units
0, // No lighting
0, // No alpha testing
1, // Do alpha blend
1, // Do depth testing
0 // No depth writing
);
int l, t, r, b;
XPLMGetWindowGeometry(in_window_id, &l, &t, &r, &b);
float col_white[] = {1.0, 1.0, 1.0}; // RGB
XPLMDrawString(col_white, l + 10, t - 20, "Hello World!", nullptr, xplmFont_Proportional);
} }

View File

@ -0,0 +1,29 @@
#include <cstring>
#include "XPLMMenus.h"
#include "window.h"
#include "menu.h"
XPLMMenuID g_menu_id;
int g_menu_container_idx;
void menu_handler(void *, void *);
int create_menu() {
g_menu_container_idx = XPLMAppendMenuItem(XPLMFindPluginsMenu(), "Checklist Tester", nullptr, 0);
g_menu_id = XPLMCreateMenu("Checklist Tester", XPLMFindPluginsMenu(), g_menu_container_idx, menu_handler, nullptr);
XPLMAppendMenuItem(g_menu_id, "Open Checklist Tester", (void *)"Open Menu", 1);
return g_menu_id != nullptr;
}
void destroy_menu() {
XPLMDestroyMenu(g_menu_id);
}
void menu_handler(void * in_menu_ref, void * in_item_ref) {
if(!strcmp((const char *)in_item_ref, "Open Menu")) {
toggle_window();
}
}

View File

@ -0,0 +1,8 @@
#ifndef XPL_CHECKLIST_CONNECTOR_MENU_H
#define XPL_CHECKLIST_CONNECTOR_MENU_H
int create_menu();
void destroy_menu();
#endif //XPL_CHECKLIST_CONNECTOR_MENU_H

View File

@ -0,0 +1,14 @@
#include "menu.h"
#include "window.h"
#include "ui.h"
void UI::Init() {
create_menu();
create_window();
};
void UI::Destroy() {
destroy_window();
destroy_menu();
}

10
plugin/src/plugin/ui/ui.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef XPL_CHECKLIST_CONNECTOR_UI_H
#define XPL_CHECKLIST_CONNECTOR_UI_H
class UI {
public:
static void Init();
static void Destroy();
};
#endif //XPL_CHECKLIST_CONNECTOR_UI_H

View File

@ -0,0 +1,79 @@
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
#include "window.h"
static XPLMWindowID g_window;
// Callbacks for creating window
void draw_window(XPLMWindowID in_window_id, void * in_refcon);
int dummy_mouse_handler(XPLMWindowID in_window_id, int x, int y, int is_down, void * in_refcon) { return 0; }
XPLMCursorStatus dummy_cursor_status_handler(XPLMWindowID in_window_id, int x, int y, void * in_refcon) { return xplm_CursorDefault; }
int dummy_wheel_handler(XPLMWindowID in_window_id, int x, int y, int wheel, int clicks, void * in_refcon) { return 0; }
void dummy_key_handler(XPLMWindowID in_window_id, char key, XPLMKeyFlags flags, char virtual_key, void * in_refcon, int losing_focus) { }
int create_window() {
XPLMCreateWindow_t params;
params.structSize = sizeof(params);
params.visible = 0;
params.drawWindowFunc = draw_window;
// Register mandatory "dummy" handlers
params.handleMouseClickFunc = dummy_mouse_handler;
params.handleRightClickFunc = dummy_mouse_handler;
params.handleMouseWheelFunc = dummy_wheel_handler;
params.handleKeyFunc = dummy_key_handler;
params.handleCursorFunc = dummy_cursor_status_handler;
params.refcon = nullptr;
params.layer = xplm_WindowLayerFloatingWindows;
// Newest X-Plane GUI styling
params.decorateAsFloatingWindow = xplm_WindowDecorationRoundRectangle;
// Set the window's initial bounds
int left, bottom, right, top;
XPLMGetScreenBoundsGlobal(&left, &top, &right, &bottom);
params.left = left + 50;
params.bottom = bottom + 150;
params.right = params.left + 200;
params.top = params.bottom + 200;
g_window = XPLMCreateWindowEx(&params);
// Make window free floating window
XPLMSetWindowPositioningMode(g_window, xplm_WindowPositionFree, -1);
// Window resizing limits
XPLMSetWindowResizingLimits(g_window, 200, 200, 300, 300);
XPLMSetWindowTitle(g_window, "Checklist Tester");
return g_window != nullptr;
}
void draw_window(XPLMWindowID in_window_id, void * in_refcon) {
// Set OpenGL state before drawing
XPLMSetGraphicsState(
0, // No fog
0, // 0 texture units
0, // No lighting
0, // No alpha testing
1, // Do alpha blend
1, // Do depth testing
0 // No depth writing
);
int l, t, r, b;
XPLMGetWindowGeometry(in_window_id, &l, &t, &r, &b);
float col_white[] = {1.0, 1.0, 1.0}; // RGB
XPLMDrawString(col_white, l + 10, t - 20, "Hello World!", nullptr, xplmFont_Proportional);
}
void toggle_window() {
int state = XPLMGetWindowIsVisible(g_window);
XPLMSetWindowIsVisible(g_window, !state);
}
void destroy_window() {
XPLMDestroyWindow(g_window);
g_window = nullptr;
}

View File

@ -0,0 +1,10 @@
#ifndef XPL_CHECKLIST_CONNECTOR_WINDOW_H
#define XPL_CHECKLIST_CONNECTOR_WINDOW_H
int create_window();
void toggle_window();
void destroy_window();
#endif //XPL_CHECKLIST_CONNECTOR_WINDOW_H