From 10353d6c1b9fc378b4f1c48d6d5563ae5c4f933f Mon Sep 17 00:00:00 2001 From: smiley22 Date: Tue, 25 Jun 2019 14:52:30 +0200 Subject: [PATCH] make options configurable through menu, add option to set rudder cursor position --- BetterMouseYoke/plugin.c | 36 +++++++++++++++++++++------ BetterMouseYoke/plugin.h | 1 + Util/Util.vcxproj | 1 + Util/menu.c | 54 ++++++++++++++++++++++++++++++++++++++++ Util/util.h | 12 +++++++++ 5 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 Util/menu.c diff --git a/BetterMouseYoke/plugin.c b/BetterMouseYoke/plugin.c index e3546ab..5b75bab 100644 --- a/BetterMouseYoke/plugin.c +++ b/BetterMouseYoke/plugin.c @@ -14,9 +14,9 @@ #define PLUGIN_DESCRIPTION "Does away with X-Plane's idiotic centered little box " \ "for mouse steering that has caused much grieve and " \ "countless loss of virtual lives." -#define PLUGIN_VERSION "1.4" +#define PLUGIN_VERSION "1.5" -#define RUDDER_DEFL_DIST 100 +#define RUDDER_DEFL_DIST 200 #define RUDDER_RET_SPEED 2.0f static XPLMCommandRef toggle_yoke_control; @@ -34,6 +34,8 @@ static float green[] = { 0, 1.0f, 0 }; static int set_pos; static int change_cursor; static int cursor_pos[2]; +static int set_rudder_pos; +static int rudder_return; static int rudder_defl_dist; static float yaw_ratio; static float rudder_ret_spd; @@ -85,8 +87,10 @@ PLUGIN_API int XPluginStart(char *name, char *sig, char *desc) { _log("init: joystick detected, unloading plugin"); return 0; } - set_pos = ini_geti("set_pos", 1); - change_cursor = ini_geti("change_cursor", 1); + if (!init_menu()) { + _log("init: could not init menu"); + return 0; + } rudder_defl_dist = ini_geti("rudder_deflection_distance", RUDDER_DEFL_DIST); rudder_ret_spd = ini_getf("rudder_return_speed", RUDDER_RET_SPEED); #ifdef IBM @@ -164,6 +168,7 @@ PLUGIN_API void XPluginDisable(void) { if (loop_id) XPLMDestroyFlightLoop(loop_id); loop_id = NULL; + menu_deinit(); } /** @@ -184,6 +189,18 @@ PLUGIN_API void XPluginReceiveMessage(XPLMPluginID from, int msg, void *param) { } } +int init_menu() { + menu_item_t items[] = { + { "Set Yoke Cursor", "set_pos", &set_pos, 1 }, + { "Set Rudder Cursor", "set_rudder_pos", &set_rudder_pos, 1 }, + { "Change Cursor Icon", "change_cursor", &change_cursor, 1 }, + { "Return Rudder", "rudder_return", &rudder_return, 1 } + }; + int num = sizeof(items) / sizeof(items[0]); + + return menu_init(PLUGIN_NAME, items, num); +} + int toggle_yoke_control_cb(XPLMCommandRef cmd, XPLMCommandPhase phase, void *ref) { if (phase != xplm_CommandBegin) return 1; @@ -232,7 +249,7 @@ float loop_cb(float last_call, float last_loop, int count, void *ref) { /* If user has disabled mouse yoke control, suspend loop. */ if (yoke_control_enabled == 0) { /* If rudder is still deflected, move it gradually back to zero. */ - if (yaw_ratio != 0) { + if (yaw_ratio != 0 && rudder_return) { long long now = get_time_ms(); float dt = (now - _last_time) / 1000.0f; _last_time = now; @@ -260,7 +277,7 @@ float loop_cb(float last_call, float last_loop, int count, void *ref) { XPLMSetDataf(yoke_roll_ratio, yoke_roll); XPLMSetDataf(yoke_pitch_ratio, yoke_pitch); /* If rudder is still deflected, move it gradually back to zero. */ - if (yaw_ratio != 0) { + if (yaw_ratio != 0 && rudder_return) { long long now = get_time_ms(); float dt = (now - _last_time) / 1000.0f; _last_time = now; @@ -290,8 +307,13 @@ int controlling_rudder(int *x, int *y) { if (!rudder_control) { if (change_cursor) set_cursor_bmp(CURSOR_RUDDER); - /* Remember current cursor position */ + /* Remember current cursor position. */ XPLMGetMouseLocationGlobal(cursor_pos, cursor_pos + 1); + /* Set rudder cursor position, if enabled. */ + if (set_rudder_pos) { + *x = *x + yaw_ratio * rudder_defl_dist; + set_cursor_pos(*x, *y); + } rudder_control = 1; } } else { diff --git a/BetterMouseYoke/plugin.h b/BetterMouseYoke/plugin.h index fbf896c..e96c3af 100644 --- a/BetterMouseYoke/plugin.h +++ b/BetterMouseYoke/plugin.h @@ -29,6 +29,7 @@ typedef enum { CURSOR_RUDDER } cursor_t; +int init_menu(); int toggle_yoke_control_cb(XPLMCommandRef cmd, XPLMCommandPhase phase, void *ref); int draw_cb(XPLMDrawingPhase phase, int before, void *ref); float loop_cb(float last_call, float last_loop, int count, void *ref); diff --git a/Util/Util.vcxproj b/Util/Util.vcxproj index a423110..ceb4ba9 100644 --- a/Util/Util.vcxproj +++ b/Util/Util.vcxproj @@ -23,6 +23,7 @@ + diff --git a/Util/menu.c b/Util/menu.c new file mode 100644 index 0000000..9ad18c8 --- /dev/null +++ b/Util/menu.c @@ -0,0 +1,54 @@ +/** + * Utility library for X-Plane 11 Plugins. + * + * Static library containing common functionality for stuff like logging and + * dealing with configuration files. Linked against by most plugins in the + * solution. + * + * Copyright 2019 Torben Könke. + */ +#include "util.h" + +static XPLMMenuID menu_id; +static menu_item_t menu_items[MAX_MENU_ITEMS]; + +static void menu_cb(void *menu_ref, void *item_ref) { + int index = (int)item_ref; + menu_item_t *item = &menu_items[index]; + XPLMMenuCheck check; + XPLMCheckMenuItemState(menu_id, index, &check); + int new_val = check == xplm_Menu_Checked ? 0 : 1; + + XPLMCheckMenuItem(menu_id, index, new_val ? xplm_Menu_Checked : + xplm_Menu_Unchecked); + if (item->ini_name) + ini_seti(item->ini_name, new_val); + if (item->var) + *(item->var) = new_val; +} + +int menu_init(const char *name, menu_item_t *items, int num) { + if (!(menu_id = XPLMCreateMenu(name, NULL, 0, menu_cb, NULL))) + return 0; + memcpy(menu_items, items, num * sizeof(menu_item_t)); + for (int i = 0; i < num; i++) { + int index = XPLMAppendMenuItem(menu_id, menu_items[i].name, i, 0); + if (index < 0) + return 0; + int val = menu_items[i].value; + if (menu_items[i].ini_name) + val = ini_geti(menu_items[i].ini_name, menu_items[i].value); + if (menu_items[i].var) + *(menu_items[i].var) = val; + XPLMCheckMenuItem(menu_id, index, val ? xplm_Menu_Checked : + xplm_Menu_Unchecked); + } + return 1; +} + +int menu_deinit() { + if (menu_id) + XPLMDestroyMenu(menu_id); + menu_id = NULL; + return 1; +} \ No newline at end of file diff --git a/Util/util.h b/Util/util.h index 120dc8f..5af28ea 100644 --- a/Util/util.h +++ b/Util/util.h @@ -26,6 +26,7 @@ #include "../XP/XPLMDataAccess.h" #include "../XP/XPLMPlugin.h" #include "../XP/XPLMPlanes.h" +#include "../XP/XPLMMenus.h" #include "../FMOD/fmod.h" #include #include @@ -95,4 +96,15 @@ void cmd_free(XPLMCommandRef *cmd, XPLMCommandCallback_f cb, void *data); /* time */ long long get_time_ms(); +/* menu */ +#define MAX_MENU_ITEMS 16 +typedef struct { + const char *name; + const char *ini_name; + int *var; + int value; +} menu_item_t; +int menu_init(const char *name, menu_item_t *items, int num); +int menu_deinit(); + #endif /* _UTIL_H_ */