Difference between revisions of "Custom Command"

From X-Plane SDK
Jump to: navigation, search
 
(4 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.
 +
//
  
<br>  
+
#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>
  
// Custom Commands
+
XPLMDataRef gHeadPositionXDataRef = NULL;
//
+
 
// This example program illustrates creating a custom command.  In this case clicking a generic trigger on
+
XPLMCommandRef MyCommand = NULL;
// 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
+
int    MyCommandHandler(XPLMCommandRef        inCommand,
// purposes, this example uses a custom command:  BlueSideUpBob/ViewPoint/MoveRight
+
XPLMCommandPhase     inPhase,
//
+
void *               inRefcon);
// For this example to work it is necessary to create a generic trigger on the panel of your aircraft keyed
+
 
// to the command: BlueSideUpBob/ViewPoint/MoveRight.
+
PLUGIN_API int XPluginStart(
//
+
char *        outName,
+
char *        outSig,
+
char *        outDesc)
#define XPLM200 =1;
+
{
+
// Plugin Info
#include "[[XPLMPlugin]].h"
+
strcpy(outName, "CommandControl");
#include "[[XPLMDisplay]].h"
+
strcpy(outSig, "BlueSideUpBob.Example.CommandControl");
#include "[[XPLMGraphics]].h"
+
strcpy(outDesc, "This example illustrates creating and sending a custom command to X-Plane using a generic trigger.");
#include "[[XPLMProcessing]].h"
+
 
#include "[[XPLMDataAccess]].h"
+
// Create the test command, this will move the pilots point of view 10 cm to the right.
#include "[[XPLMMenus]].h"
+
MyCommand = XPLMCreateCommand("BSUB/ViewPoint/MoveRight", "Move Right");
#include "[[XPLMUtilities]].h"
+
 
#include "[[XPWidgets]].h"
+
// Register our custom command
#include "[[XPStandardWidgets]].h"
+
XPLMRegisterCommandHandler(MyCommand,              // in Command name
#include "[[XPLMScenery]].h"
+
MyCommandHandler,      // in Handler
#include &lt;string.h&gt;
+
1,                      // Receive input before plugin windows.
#include &lt;stdio.h&gt;
+
(void *) 0);            // inRefcon.
#include &lt;stdlib.h&gt;
+
 
+
gHeadPositionXDataRef = XPLMFindDataRef("sim/aircraft/view/acf_peX");
[[XPLMDataRef]] gHeadPositionXDataRef = NULL;
+
 
+
return 1;
[[XPLMCommandRef]] MyCommand = NULL;
+
}
+
 
+
PLUGIN_API void    XPluginStop(void)
int    MyCommandHandler([[XPLMCommandRef]]       inCommand,
+
{
                    [[XPLMCommandPhase]]    inPhase,
+
XPLMUnregisterCommandHandler(MyCommand, MyCommandHandler, 0, 0);
                    void *               inRefcon);
+
}
+
 
PLUGIN_API int XPluginStart(
+
 
    char *        outName,
+
PLUGIN_API void XPluginDisable(void)
    char *        outSig,
+
{
    char *        outDesc)
+
}
{
+
 
+
PLUGIN_API int XPluginEnable(void)
+
{
// 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 XPluginReceiveMessage(
+
XPLMPluginID    inFromWho,
// Create the test command, this will move the pilots point of view 10 cm to the right.
+
long            inMessage,
MyCommand = [[XPLMCreateCommand]]("BlueSideUpBob/ViewPoint/MoveRight", "Move Right");
+
void *          inParam)
+
{
// Register our custom command
+
 
  [[XPLMRegisterCommandHandler]](MyCommand,              // in Command name
+
}
                            MyCommandHandler,      // in Handler
+
 
                            1,                      // Receive input before plugin windows.
+
int    MyCommandHandler(XPLMCommandRef      inCommand,
                            (void *) 0);            // inRefcon.
+
XPLMCommandPhase    inPhase,
+
void *              inRefcon)
  gHeadPositionXDataRef = [[XPLMFindDataRef]]("sim/aircraft/view/acf_peX");
+
{
+
 
  return 1;
+
//  Use the structure below to have the command executed
}
+
//  continuously while the button is being held down.
+
if (inPhase == xplm_CommandContinue)
+
{
PLUGIN_API void    XPluginStop(void)
+
XPLMSetDataf(gHeadPositionXDataRef, XPLMGetDataf(gHeadPositionXDataRef) + .1);
{
+
}
[[XPLMUnregisterCommandHandler]](MyCommand, MyCommandHandler, 0, 0);
+
 
}
+
//  Use this structure to have the command executed on button up only.
+
if (inPhase == xplm_CommandEnd)
+
{
PLUGIN_API void XPluginDisable(void)
+
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.
PLUGIN_API int XPluginEnable(void)
+
return 0;
{
+
}
return 1;
+
</sample_code>
}
+
+
+
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 == 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;
}