Skip to content

Commit

Permalink
make options configurable through menu, add option to set rudder curs…
Browse files Browse the repository at this point in the history
…or position
  • Loading branch information
smiley22 committed Jun 25, 2019
1 parent 390e492 commit 10353d6
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 7 deletions.
36 changes: 29 additions & 7 deletions BetterMouseYoke/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -164,6 +168,7 @@ PLUGIN_API void XPluginDisable(void) {
if (loop_id)
XPLMDestroyFlightLoop(loop_id);
loop_id = NULL;
menu_deinit();
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions BetterMouseYoke/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions Util/Util.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ClCompile Include="dllmain.c" />
<ClCompile Include="ini.c" />
<ClCompile Include="log.c" />
<ClCompile Include="menu.c" />
<ClCompile Include="path.c" />
<ClCompile Include="snd.c" />
<ClCompile Include="time.c" />
Expand Down
54 changes: 54 additions & 0 deletions Util/menu.c
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions Util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
#include <stdbool.h>
Expand Down Expand Up @@ -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_ */

0 comments on commit 10353d6

Please sign in to comment.