TestEnums

From X-Plane SDK
Revision as of 14:28, 26 December 2012 by Sbarbour (Talk | contribs)

Jump to: navigation, search




/*
	TestEnums Example
	Written by Sandy Barbour - 23/04/2004
	
	This examples shows how to send key and joystick commands
*/

#include "XPLMPlugin.h"
#include "XPLMUtilities.h"
#include "XPLMProcessing.h"
#include "XPLMMenus.h"
#include "XPLMGraphics.h"
#include "XPLMPlanes.h"
#include "XPLMDataAccess.h"
#include "XPWidgets.h"
#include "XPStandardWidgets.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#if IBM
#include <windows.h>
#endif


#define MAX_KEY_COMMANDS 95
#define MAX_JOYSTICK_COMMANDS 95
#define MAX_VIEW_ENUMS 21
#define MAX_AP_STATES 12

static int MenuItem1, KeyCommandLinePosition, JoystickCommandLinePosition, ViewEnumsLinePosition, AutopilotStateLinePosition;

// Widgets
static XPWidgetID TestEnumsWidget = NULL, TestEnumsWindow = NULL;
static XPWidgetID SendKeyCommandButton = NULL, KeyCommandText = NULL, KeyCommandIDText = NULL, KeyUpArrow = NULL, KeyDownArrow = NULL;
static XPWidgetID SendJoystickCommandButtonPress = NULL, SendJoystickCommandButtonRelease = NULL, JoystickCommandText = NULL, JoystickCommandIDText = NULL, JoystickUpArrow = NULL, JoystickDownArrow = NULL;
static XPWidgetID SendViewEnumsButton = NULL, ViewEnumsText = NULL, ViewEnumsIDText = NULL, ViewEnumsUpArrow = NULL, ViewEnumsDownArrow = NULL;
static XPWidgetID SendAutopilotStateButton = NULL, AutopilotStateText = NULL, AutopilotStateIDText = NULL, AutopilotStateUpArrow = NULL, AutopilotStateDownArrow = NULL;
static XPWidgetID V6CheckBox = {NULL};

static XPLMDataRef gViewType = NULL, gAutopilotState = NULL;

typedef struct _MESSAGE_STRUCT
{
	char MessageText[80];
	int MessageEnum;
} MESSAGE_STRUCT;

typedef struct _MESSAGE_STRUCT2
{
	char MessageText[80];
	int MessageEnumV6;
	int MessageEnumV7;
} MESSAGE_STRUCT2;

static MESSAGE_STRUCT KeyCommands[MAX_KEY_COMMANDS] =
{
	{"Pause Sim", xplm_key_pause},
	{"Reverse Thrust", xplm_key_revthrust},
	{"Jettison", xplm_key_jettison},
    {"Brakes Regular", xplm_key_brakesreg},
    {"Brakes Max", xplm_key_brakesmax},
	{"Toggle Gear", xplm_key_gear},
    {"Time Down", xplm_key_timedn},
    {"Time Up", xplm_key_timeup},
    {"FADEC", xplm_key_fadec},
    {"AP Disconnect", xplm_key_otto_dis},
    {"AP Auto Throttle", xplm_key_otto_atr},
    {"AP Air Speed", xplm_key_otto_asi},
    {"AP Heading", xplm_key_otto_hdg},
    {"AP GPS", xplm_key_otto_gps},
    {"AP Wing Leveler", xplm_key_otto_lev},
    {"AP HNAV", xplm_key_otto_hnav},
    {"AP Altitude", xplm_key_otto_alt},
    {"AP Vertical Speed", xplm_key_otto_vvi},
    {"AP VNAV", xplm_key_otto_vnav},
    {"AP NAV1", xplm_key_otto_nav1},
    {"AP NAV2", xplm_key_otto_nav2},
    {"TARG Down", xplm_key_targ_dn},
    {"TARG Up", xplm_key_targ_up},
    {"Heading Down", xplm_key_hdgdn},
    {"Heading Up", xplm_key_hdgup},
    {"Barometer Down", xplm_key_barodn},
    {"Barometer Up", xplm_key_baroup},
    {"OBS1 Down", xplm_key_obs1dn},
    {"OBS1 Up", xplm_key_obs1up},
    {"OBS2 Down", xplm_key_obs2dn},
    {"OBS2 Up", xplm_key_obs2up},
    {"COM1 1", xplm_key_com1_1},
    {"COM1 2", xplm_key_com1_2},
	{"COM1 3", xplm_key_com1_3},
    {"COM1 4", xplm_key_com1_4},
    {"NAV1 1", xplm_key_nav1_1},
    {"NAV1 2", xplm_key_nav1_2},
    {"NAV1 3", xplm_key_nav1_3},
    {"NAV1 4", xplm_key_nav1_4},
    {"COM2 1", xplm_key_com2_1},
    {"COM2 2", xplm_key_com2_2},
	{"COM2 3", xplm_key_com2_3},
	{"COM2 4", xplm_key_com2_4},
	{"NAV2 1", xplm_key_nav2_1},
	{"NAV2 2", xplm_key_nav2_2},
	{"NAV2 3", xplm_key_nav2_3},
	{"NAV2 4", xplm_key_nav2_4},
	{"ADF 1", xplm_key_adf_1},
	{"ADF 2", xplm_key_adf_2},
	{"ADF 3", xplm_key_adf_3},
	{"ADF 4", xplm_key_adf_4},
	{"ADF 5", xplm_key_adf_5},
	{"ADF 6", xplm_key_adf_6},
	{"XPDR 1", xplm_key_transpon_1},
	{"XPDR 2", xplm_key_transpon_2},
	{"XPDR 3", xplm_key_transpon_3},
	{"XPDR 4", xplm_key_transpon_4},
	{"XPDR 5", xplm_key_transpon_5},
	{"XPDR 6", xplm_key_transpon_6},
	{"XPDR 7", xplm_key_transpon_7},
	{"XPDR 8", xplm_key_transpon_8},
	{"Flaps Up", xplm_key_flapsup},
	{"Flaps Down", xplm_key_flapsdn},
	{"Carb Heat Off", xplm_key_cheatoff},
	{"Carb Heat On", xplm_key_cheaton},
	{"Speed Brake Off", xplm_key_sbrkoff},
	{"Speed Brake On", xplm_key_sbrkon},
	{"Aileron Trim Left", xplm_key_ailtrimL},
	{"Aileron Trim Right", xplm_key_ailtrimR},
	{"Rudder Trim Left", xplm_key_rudtrimL},
	{"Rudder Trim Right", xplm_key_rudtrimR},
	{"Elevator Trim Down", xplm_key_elvtrimD},
	{"Elevator Trim Up", xplm_key_elvtrimU},
	{"Forward", xplm_key_forward},
	{"Down", xplm_key_down},
	{"Left", xplm_key_left},
	{"Right", xplm_key_right},
	{"Back", xplm_key_back},
	{"Tower", xplm_key_tower},
	{"Runway", xplm_key_runway},
	{"Chase", xplm_key_chase},
	{"Free 1", xplm_key_free1},
	{"Free 2", xplm_key_free2},
	{"Spot", xplm_key_spot},
	{"Full Screen 1", xplm_key_fullscrn1},
	{"Full Screen 2", xplm_key_fullscrn2},
	{"Transparent", xplm_key_tanspan},
	{"Smoke", xplm_key_smoke},
	{"Map", xplm_key_map},
	{"Zoom In", xplm_key_zoomin},
	{"Zoom Out", xplm_key_zoomout},
	{"Cycle Dump", xplm_key_cycledump},
	{"Replay", xplm_key_replay},
	{"Transponder ID", xplm_key_tranID},
	{"Max", xplm_key_max}
};

static MESSAGE_STRUCT JoystickCommands[MAX_JOYSTICK_COMMANDS] =
{
	{"Nothing", xplm_joy_nothing},
	{"Start All", xplm_joy_start_all},
	{"Start 0", xplm_joy_start_0},
	{"Start 1", xplm_joy_start_1},
	{"Start 2", xplm_joy_start_2},
	{"Start 3", xplm_joy_start_3},
	{"Start 4", xplm_joy_start_4},
	{"Start 5", xplm_joy_start_5},
	{"Start 6", xplm_joy_start_6},
	{"Start 7", xplm_joy_start_7},
	{"Throttle Up", xplm_joy_throt_up},
	{"Throttle Down", xplm_joy_throt_dn},
	{"Prop Up", xplm_joy_prop_up},
	{"Prop Down", xplm_joy_prop_dn},
	{"Mixture Up", xplm_joy_mixt_up},
	{"mixture Down", xplm_joy_mixt_dn},
	{"Carb Toggle", xplm_joy_carb_tog},
	{"Carb On", xplm_joy_carb_on},
	{"Carb Off", xplm_joy_carb_off},
	{"Trim Elevator", xplm_joy_trev},
	{"Trim Up", xplm_joy_trm_up},
	{"Trim Down", xplm_joy_trm_dn},
	{"Rotor Trim Up", xplm_joy_rot_trm_up},
	{"Rotor Trim Down", xplm_joy_rot_trm_dn},
	{"Rudder Left", xplm_joy_rud_lft},
	{"Rudder Centre", xplm_joy_rud_cntr},
	{"Rudder Right", xplm_joy_rud_rgt},
	{"Aileron Left", xplm_joy_ail_lft},
	{"Aileron Centre", xplm_joy_ail_cntr},
	{"Aileron Right", xplm_joy_ail_rgt},
	{"B Rudder Left", xplm_joy_B_rud_lft},
	{"B Rudder Right", xplm_joy_B_rud_rgt},
	{"Look Up", xplm_joy_look_up},
	{"Look Down", xplm_joy_look_dn},
	{"Look Left", xplm_joy_look_lft},
	{"Look Right", xplm_joy_look_rgt},
	{"Glance Left", xplm_joy_glance_l},
	{"Glance Right", xplm_joy_glance_r},
	{"View Forward No HUD", xplm_joy_v_fnh},
	{"View Forward HUD", xplm_joy_v_fwh},
	{"View Transparent", xplm_joy_v_tra},
	{"View Tower", xplm_joy_v_twr},
	{"View Runway", xplm_joy_v_run},
   	{"View Chase", xplm_joy_v_cha},
	{"View Free 1", xplm_joy_v_fr1},
	{"View Free 2", xplm_joy_v_fr2},
    {"View Spo", xplm_joy_v_spo},
	{"Flaps Up", xplm_joy_flapsup},
	{"Flaps Down", xplm_joy_flapsdn},
	{"Vector Sweep Forward", xplm_joy_vctswpfwd},
	{"Vector Sweep Aft", xplm_joy_vctswpaft},
	{"Gear Toggle", xplm_joy_gear_tog},
	{"Gear Up", xplm_joy_gear_up},
	{"Gear Down", xplm_joy_gear_down},
	{"Left Brake", xplm_joy_lft_brake},
	{"Right Brake", xplm_joy_rgt_brake},
	{"Brakes Regular", xplm_joy_brakesREG},
	{"Brakes Max", xplm_joy_brakesMAX},
	{"Speed Brake", xplm_joy_speedbrake},
	{"AP Disconnect", xplm_joy_ott_dis},
	{"AP Auto Throttle", xplm_joy_ott_atr},
	{"AP Airspeed", xplm_joy_ott_asi},
	{"AP Heading", xplm_joy_ott_hdg},
	{"AP Altitude", xplm_joy_ott_alt},
	{"AP Vertical Speed", xplm_joy_ott_vvi},
	{"Timer Start", xplm_joy_tim_start},
	{"Timer Reset", xplm_joy_tim_reset},
	{"ECAM Up", xplm_joy_ecam_up},
	{"ECAM Down", xplm_joy_ecam_dn},
	{"FADEC", xplm_joy_fadec},
	{"Yaw Damper", xplm_joy_yaw_damp},
	{"Arificial Stability", xplm_joy_art_stab},
	{"Chute", xplm_joy_chute},
	{"JATO", xplm_joy_JATO},
	{"Arrester", xplm_joy_arrest},
	{"Jettison", xplm_joy_jettison},
	{"Fuel Dump", xplm_joy_fuel_dump},
	{"Puff Smoke", xplm_joy_puffsmoke},
	{"Pre-Rotate", xplm_joy_prerotate},
	{"UL Pre-Rotate", xplm_joy_UL_prerot},
	{"UL Collect", xplm_joy_UL_collec},
	{"TOGA", xplm_joy_TOGA},
	{"Shutdown", xplm_joy_shutdown},
	{"Contact ATC", xplm_joy_con_atc},
	{"Fail Now", xplm_joy_fail_now},
	{"Pause", xplm_joy_pause},
	{"Rocket Up", xplm_joy_rock_up},
	{"Rocket Down", xplm_joy_rock_dn},
	{"Rocket Left", xplm_joy_rock_lft},
	{"Rocket Right", xplm_joy_rock_rgt},
	{"Rocket Forward", xplm_joy_rock_for},
	{"Rocket Aft", xplm_joy_rock_aft},
	{"Idle HILO", xplm_joy_idle_hilo},
	{"Landing Lights", xplm_joy_lanlights},
	{"Max", xplm_joy_max}
};

static MESSAGE_STRUCT2 ViewEnums[MAX_VIEW_ENUMS] = 
{
	{"Forward", 600, 1000},
	{"Forward 04", 601, 1001},
	{"Forward 08", 602, 1002},
	{"Left 45", 604, 1004},
	{"Right 45", 605, 1005},
	{"Left 90", 606, 1006},
	{"Right 90", 607, 1007},
	{"Left 130", 608, 1008},
	{"Right 130", 609, 1009},
	{"Backward", 610, 1010},
	{"Left Up", 611, 1011},
	{"Right Up", 612, 1012},
	{"Tower", 614, 1014},
	{"Runway", 615, 1015},
	{"Chase", 617, 1017},
	{"Free 1", 618, 1018},
	{"Free 2", 619, 1019},
	{"Spot", 620, 1020},
	{"Lin", 0, 1021},
	{"Forward HUD", 622, 1023},
	{"Forward No HUD", 623, 1024}
};

static MESSAGE_STRUCT AutopilotStates[MAX_AP_STATES] = 
{
	{"Auto Throttle", 1},
	{"Heading Hold", 2},
	{"Wing Leveler On", 4},
	{"Airspeed Hold", 8},
	{"VVI Climb", 16},
	{"Altitude Hold", 32},
	{"Level Change", 64},
	{"Auto Sync", 128},
	{"HNAV Armed", 256},
	{"HNAV Engaged", 512},
	{"VNAV Armed", 1024},
	{"VNAV Engaged", 2048}
};

static int SystemBufferIndex = 0;
static char SystemMessagesBuffer[109][255];

static void UpdateSystemBuffer(char *pBuffer);
static void UpdateSystemListBox(int ScrollBarPosition);
static void TestEnumsMenuHandler(void *, void *);

static void CreateTestEnumsWidget(int x1, int y1, int w, int h);


static int TestEnumsHandler(
						XPWidgetMessage			inMessage,
						XPWidgetID				inWidget,
						intptr_t				inParam1,
						intptr_t				inParam2);


PLUGIN_API int XPluginStart(
						char *		outName,
						char *		outSig,
						char *		outDesc)
{
	XPLMMenuID	id;
	int			item;

	strcpy(outName, "Test Enums");
	strcpy(outSig, "xpsdk.examples.TestEnums");
	strcpy(outDesc, "A plug-in that tests Enums.");

	// Create our menu
	item = XPLMAppendMenuItem(XPLMFindPluginsMenu(), "TestEnums", NULL, 1);
	id = XPLMCreateMenu("TestEnums", XPLMFindPluginsMenu(), item, TestEnumsMenuHandler, NULL);
	XPLMAppendMenuItem(id, "Test Panel", (void *)"TestEnums", 1);

	gViewType = XPLMFindDataRef("sim/graphics/view/view_type");
	gAutopilotState = XPLMFindDataRef("sim/cockpit/autopilot/autopilot_state");

	// Flag to tell us if the widget is being displayed.
	MenuItem1 = 0;
	KeyCommandLinePosition = 0;
	JoystickCommandLinePosition = 0;
	ViewEnumsLinePosition = 0;

	return 1;
}

PLUGIN_API void	XPluginStop(void)
{
}

PLUGIN_API int XPluginEnable(void)
{
	return 1;
}

PLUGIN_API void XPluginDisable(void)
{
}

PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg, void * inParam)
{
}


void TestEnumsMenuHandler(void * mRef, void * iRef)
{
	// If menu selected create our widget dialog
}