Difference between revisions of "Custom Command"

From X-Plane SDK
Jump to: navigation, search
m
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Sample Code]]
+
{{SDK Example C 200}}
 +
<sample_code t="CustomCommands">// Custom Commands
 +
//
 +
// This example program illustrates creating a custom command.  In this case clicking a generic trigger on
 +
// your aircraft panel writes to the DataRef controlling the pilots head position either continuously or one
 +
// knotch at a time. We could use the existing position command: sim/view/move_right, however for illustrative
 +
// purposes, this example uses a custom command:  BSUB/ViewPoint/MoveRight
 +
//
 +
// For this example to work it is necessary to create a generic trigger on the panel of your aircraft keyed
 +
// to the command: BSUB/ViewPoint/MoveRight.
 +
//
 +
// Content added by BlueSideUpBob.
 +
//
  
// Custom Commands
+
#include "XPLMPlugin.h"
//
+
#include "XPLMDisplay.h"
// This example program illustrates creating a custom command.  In this case clicking a generic trigger on
+
#include "XPLMGraphics.h"
// your aircraft panel writes to the DataRef controlling the pilots head position either continuously or one
+
#include "XPLMProcessing.h"
// knotch at a time. We could use the existing position command: sim/view/move_right, however for illustrative
+
#include "XPLMDataAccess.h"
// purposes, this example uses a custom command:  BSUB/ViewPoint/MoveRight
+
#include "XPLMMenus.h"
//
+
#include "XPLMUtilities.h"
// For this example to work it is necessary to create a generic trigger on the panel of your aircraft keyed
+
#include "XPWidgets.h"
// to the command: BSUB/ViewPoint/MoveRight.
+
#include "XPStandardWidgets.h"
//
+
#include "XPLMScenery.h"
// Content added by BlueSideUpBob.
+
#include <string.h>
//
+
#include <stdio.h>
+
#include <stdlib.h>
+
 
#define XPLM200 =1;
+
XPLMDataRef gHeadPositionXDataRef = NULL;
+
 
#include "XPLMPlugin.h"
+
XPLMCommandRef MyCommand = NULL;
#include "XPLMDisplay.h"
+
 
#include "XPLMGraphics.h"
+
int    MyCommandHandler(XPLMCommandRef        inCommand,
#include "XPLMProcessing.h"
+
XPLMCommandPhase      inPhase,
#include "XPLMDataAccess.h"
+
void *                inRefcon);
#include "XPLMMenus.h"
+
 
#include "XPLMUtilities.h"
+
PLUGIN_API int XPluginStart(
#include "XPWidgets.h"
+
char *        outName,
#include "XPStandardWidgets.h"
+
char *        outSig,
#include "XPLMScenery.h"
+
char *        outDesc)
#include <string.h>
+
{
#include <stdio.h>
+
// Plugin Info
#include <stdlib.h>
+
strcpy(outName, "CommandControl");
+
strcpy(outSig, "BlueSideUpBob.Example.CommandControl");
XPLMDataRef gHeadPositionXDataRef = NULL;
+
strcpy(outDesc, "This example illustrates creating and sending a custom command to X-Plane using a generic trigger.");
+
 
XPLMCommandRef MyCommand = NULL;
+
// Create the test command, this will move the pilots point of view 10 cm to the right.
 
+
MyCommand = XPLMCreateCommand("BSUB/ViewPoint/MoveRight", "Move Right");
int    MyCommandHandler(XPLMCommandRef        inCommand,
+
 
                        XPLMCommandPhase      inPhase,
+
// Register our custom command
                        void *                inRefcon);
+
XPLMRegisterCommandHandler(MyCommand,              // in Command name
+
MyCommandHandler,      // in Handler
PLUGIN_API int XPluginStart(
+
1,                      // Receive input before plugin windows.
    char *        outName,
+
(void *) 0);            // inRefcon.
    char *        outSig,
+
 
    char *        outDesc)
+
gHeadPositionXDataRef = XPLMFindDataRef("sim/aircraft/view/acf_peX");
{
+
 
// Plugin Info
+
return 1;
strcpy(outName, "CommandControl");
+
}
strcpy(outSig, "BlueSideUpBob.Example.CommandControl");
+
 
strcpy(outDesc, "This example illustrates creating and sending a custom command to X-Plane using a generic trigger.");
+
PLUGIN_API void    XPluginStop(void)
+
{
// Create the test command, this will move the pilots point of view 10 cm to the right.
+
XPLMUnregisterCommandHandler(MyCommand, MyCommandHandler, 0, 0);
MyCommand = XPLMCreateCommand("BSUB/ViewPoint/MoveRight", "Move Right");
+
}
+
 
// Register our custom command
+
 
  XPLMRegisterCommandHandler(MyCommand,              // in Command name
+
PLUGIN_API void XPluginDisable(void)
                            MyCommandHandler,      // in Handler
+
{
                            1,                      // Receive input before plugin windows.
+
}
                            (void *) 0);            // inRefcon.
+
 
+
PLUGIN_API int XPluginEnable(void)
  gHeadPositionXDataRef = XPLMFindDataRef("sim/aircraft/view/acf_peX");
+
{
+
return 1;
  return 1;
+
}
}
+
 
+
PLUGIN_API void XPluginReceiveMessage(
+
XPLMPluginID    inFromWho,
PLUGIN_API void    XPluginStop(void)
+
long            inMessage,
{
+
void *          inParam)
XPLMUnregisterCommandHandler(MyCommand, MyCommandHandler, 0, 0);
+
{
}
+
 
+
}
+
 
PLUGIN_API void XPluginDisable(void)
+
int    MyCommandHandler(XPLMCommandRef      inCommand,
{
+
XPLMCommandPhase    inPhase,
+
void *              inRefcon)
}
+
{
+
 
+
//  Use the structure below to have the command executed
PLUGIN_API int XPluginEnable(void)
+
//  continuously while the button is being held down.
{
+
if (inPhase == xplm_CommandContinue)
return 1;
+
{
}
+
XPLMSetDataf(gHeadPositionXDataRef, XPLMGetDataf(gHeadPositionXDataRef) + .1);
+
}
+
 
PLUGIN_API void XPluginReceiveMessage(
+
//  Use this structure to have the command executed on button up only.
                XPLMPluginID    inFromWho,
+
if (inPhase == xplm_CommandEnd)
                long            inMessage,
+
{
                void *          inParam)
+
XPLMSetDataf(gHeadPositionXDataRef, XPLMGetDataf(gHeadPositionXDataRef) + .1);
{
+
}
+
 
}
+
// Return 1 to pass the command to plugin windows and X-Plane.
+
// Returning 0 disables further processing by X-Plane.
+
// In this case we might return 0 or 1 because X-Plane does not duplicate our command.
int    MyCommandHandler(XPLMCommandRef      inCommand,
+
return 0;
                    XPLMCommandPhase    inPhase,
+
}
                    void *              inRefcon)
+
</sample_code>
{
+
+
//  Use the structure below to have the command executed
+
//  continuously while the button is being held down.
+
+
if (inPhase == 1)
+
{
+
    XPLMSetDataf(gHeadPositionXDataRef, XPLMGetDataf(gHeadPositionXDataRef) + .1);
+
}
+
+
+
+
//  Use this structure to have the command executed on button up only.
+
if (inPhase == 2)
+
{
+
    XPLMSetDataf(gHeadPositionXDataRef, XPLMGetDataf(gHeadPositionXDataRef) + .1);
+
}
+
+
// Return 1 to pass the command to plugin windows and X-Plane.
+
// Returning 0 disables further processing by X-Plane.
+
// In this case we might return 0 or 1 because X-Plane does not duplicate our command.
+
+
return 0;
+
}
+

Latest revision as of 18:44, 31 March 2012



// Custom Commands
//
// This example program illustrates creating a custom command.  In this case clicking a generic trigger on
// your aircraft panel writes to the DataRef controlling the pilots head position either continuously or one
// knotch at a time. We could use the existing position command: sim/view/move_right, however for illustrative
// purposes, this example uses a custom command:  BSUB/ViewPoint/MoveRight
//
// For this example to work it is necessary to create a generic trigger on the panel of your aircraft keyed
// to the command: BSUB/ViewPoint/MoveRight.
//
// Content added by BlueSideUpBob.
//

#include "XPLMPlugin.h"
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
#include "XPLMProcessing.h"
#include "XPLMDataAccess.h"
#include "XPLMMenus.h"
#include "XPLMUtilities.h"
#include "XPWidgets.h"
#include "XPStandardWidgets.h"
#include "XPLMScenery.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

XPLMDataRef gHeadPositionXDataRef = NULL;

XPLMCommandRef MyCommand = NULL;

int    MyCommandHandler(XPLMCommandRef        inCommand,
					 XPLMCommandPhase      inPhase,
					 void *                inRefcon);

PLUGIN_API int XPluginStart(
					 char *        outName,
					 char *        outSig,
					 char *        outDesc)
{
	// Plugin Info
	strcpy(outName, "CommandControl");
	strcpy(outSig, "BlueSideUpBob.Example.CommandControl");
	strcpy(outDesc, "This example illustrates creating and sending a custom command to X-Plane using a generic trigger.");

	// Create the test command, this will move the pilots point of view 10 cm to the right.
	MyCommand = XPLMCreateCommand("BSUB/ViewPoint/MoveRight", "Move Right");

	// Register our custom command
	XPLMRegisterCommandHandler(MyCommand,              // in Command name
							 MyCommandHandler,       // in Handler
							 1,                      // Receive input before plugin windows.
							 (void *) 0);            // inRefcon.

	gHeadPositionXDataRef = XPLMFindDataRef("sim/aircraft/view/acf_peX");

	return 1;
}

PLUGIN_API void    XPluginStop(void)
{
	XPLMUnregisterCommandHandler(MyCommand, MyCommandHandler, 0, 0);
}


PLUGIN_API void XPluginDisable(void)
{
}

PLUGIN_API int XPluginEnable(void)
{
	return 1;
}

PLUGIN_API void XPluginReceiveMessage(
			 XPLMPluginID    inFromWho,
			 long            inMessage,
			 void *          inParam)
{

}

int    MyCommandHandler(XPLMCommandRef       inCommand,
				 XPLMCommandPhase     inPhase,
				 void *               inRefcon)
{

	//  Use the structure below to have the command executed
	//  continuously while the button is being held down.
	if (inPhase == xplm_CommandContinue)
	{
		XPLMSetDataf(gHeadPositionXDataRef, XPLMGetDataf(gHeadPositionXDataRef) + .1);
	}

	//  Use this structure to have the command executed on button up only.
	if (inPhase == xplm_CommandEnd)
	{
		XPLMSetDataf(gHeadPositionXDataRef, XPLMGetDataf(gHeadPositionXDataRef) + .1);
	}

	// Return 1 to pass the command to plugin windows and X-Plane.
	// Returning 0 disables further processing by X-Plane.
	// In this case we might return 0 or 1 because X-Plane does not duplicate our command.
	return 0;
}