TestCustomByteDataref

From X-Plane SDK
Jump to: navigation, search



/*
Test Custom Byte Dataref

Version 1.0.0.1        Intitial Sandy Barbour 10/01/2010

This plugin was used by me to do some quick tests on the Custom Byte Dataref Example plugin.
It may be useful to users for test purposes.

DISCLAIMER
You take this example as found.
*/

#include <stdio.h>
#include <string.h>
#include <math.h>
#include "XPLMDataAccess.h"
#include "XPLMMenus.h"
#include "XPLMProcessing.h"
#include "XPLMPlugin.h"
#include "XPLMUtilities.h"
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"

// Dataref stuff, change this if you change the Custom Byte Dataref Example
#define TCBDRE_LEN 15
#define TCBDRE_MAX_DATAREFS 10

// Used to display the dataref strings.
char Buffer[10][80];

XPLMWindowID TCBDRE_Window = NULL;
XPLMMenuID    TCBDRE_MenuId;
XPLMDataRef TCBDRE_TestDataRef[TCBDRE_MAX_DATAREFS] = {NULL};

// Dataref signatures.
// You will need to change these to match your own requirements
char TCBDRE_DatarefString[TCBDRE_MAX_DATAREFS][256] = 
{
    "xplanesdk/examples/byte_dataref/00",
    "xplanesdk/examples/byte_dataref/01",
    "xplanesdk/examples/byte_dataref/02",
    "xplanesdk/examples/byte_dataref/03",
    "xplanesdk/examples/byte_dataref/04",
    "xplanesdk/examples/byte_dataref/05",
    "xplanesdk/examples/byte_dataref/06",
    "xplanesdk/examples/byte_dataref/07",
    "xplanesdk/examples/byte_dataref/08",
    "xplanesdk/examples/byte_dataref/09"
};

//---------------------------------------------------------------------------

void TCBDRE_MenuHandler(void *, void *);
float   TCBDRE_FlightLoopCallback(
                                   float               inElapsedSinceLastCall,    
                                   float               inElapsedTimeSinceLastFlightLoop,    
                                   int                 inCounter,    
                                   void *              inRefcon);                                                                  

void TCBDRE_DrawWindowCallback(
                                   XPLMWindowID         inWindowID,    
                                   void *               inRefcon);    

void TCBDRE_HandleKeyCallback(
                                   XPLMWindowID         inWindowID,    
                                   char                 inKey,    
                                   XPLMKeyFlags         inFlags,    
                                   char                 inVirtualKey,    
                                   void *               inRefcon,    
                                   int                  losingFocus);    

int TCBDRE_HandleMouseClickCallback(
                                   XPLMWindowID         inWindowID,    
                                   int                  x,    
                                   int                  y,    
                                   XPLMMouseStatus      inMouse,    
                                   void *               inRefcon);    

//---------------------------------------------------------------------------

PLUGIN_API int XPluginStart(
                                                char *          outName,
                                                char *          outSig,
                                                char *          outDesc)
{                              
    int    MenuItem;
    strcpy(outName, "Test Custom Byte Dataref Example");
    strcpy(outSig, "xplanesdk.examples.test_custom_byte_dataref");
    strcpy(outDesc, "A plugin that test the custom byte dataref example");

    // Find them here as we are using a callback to read the datarefs and we don't want to slow it down.
    for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
        TCBDRE_TestDataRef[Item] = XPLMFindDataRef(TCBDRE_DatarefString[Item]);

    // Create our Test Menus
    MenuItem = XPLMAppendMenuItem(XPLMFindPluginsMenu(), "Test CBDR Example", NULL, 1);
    TCBDRE_MenuId = XPLMCreateMenu("Test CBDR Example", XPLMFindPluginsMenu(), MenuItem, TCBDRE_MenuHandler, NULL);
    XPLMAppendMenuItem(TCBDRE_MenuId, "Change String Test",        (void *)"ChangeStringTest", 1);
    XPLMAppendMenuItem(TCBDRE_MenuId, "Overflow String Test",    (void *)"OverflowStringTest", 1);
    XPLMAppendMenuItem(TCBDRE_MenuId, "Empty String Test",        (void *)"EmptyStringTest", 1);
    XPLMAppendMenuItem(TCBDRE_MenuId, "Insert Substring Test 1",    (void *)"InsertSubstringTest1", 1);
    XPLMAppendMenuItem(TCBDRE_MenuId, "Insert Substring Test 2",    (void *)"InsertSubstringTest2", 1);
    XPLMAppendMenuItem(TCBDRE_MenuId, "Insert Substring Test 3",    (void *)"InsertSubstringTest3", 1);
    XPLMAppendMenuItem(TCBDRE_MenuId, "Insert Substring Test 4",    (void *)"InsertSubstringTest4", 1);

    // Used to update the buffers we are using in the draw callback
    XPLMRegisterFlightLoopCallback(         
                    TCBDRE_FlightLoopCallback,   /* Callback */
                    1.0,                         /* Interval */
                    NULL);                       /* refcon not used. */                                                                                     

    TCBDRE_Window = XPLMCreateWindow(
                              50, 600, 200, 430,                  /* Area of the window. */
                              1,                                  /* Start visible. */
                              TCBDRE_DrawWindowCallback,          /* Callbacks */
                              TCBDRE_HandleKeyCallback,
                              TCBDRE_HandleMouseClickCallback,
                              NULL);                              /* Refcon - not used. */

    memset(Buffer, 0, sizeof(Buffer));

    return 1;
}

//---------------------------------------------------------------------------

PLUGIN_API int XPluginEnable(void)
{
    return 1;
}

//---------------------------------------------------------------------------

PLUGIN_API void XPluginDisable(void)
{
}

//---------------------------------------------------------------------------

PLUGIN_API void XPluginStop(void)
{
    XPLMDestroyMenu(TCBDRE_MenuId);
    XPLMUnregisterFlightLoopCallback(TCBDRE_FlightLoopCallback, NULL);
    XPLMDestroyWindow(TCBDRE_Window);
}

//---------------------------------------------------------------------------

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

//---------------------------------------------------------------------------
// Do some XPLMSetDatab() tests here.
//---------------------------------------------------------------------------

void TCBDRE_MenuHandler(void * mRef, void * iRef)
{
    char TempBuffer[TCBDRE_LEN+1];
    char TempBuffer2[40];

    // Change all the strings
    if (!strcmp((char *) iRef, "ChangeStringTest"))
    {
        for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
        {
            sprintf(TempBuffer, "Test Change %03d", Item+1);
            XPLMSetDatab(TCBDRE_TestDataRef[Item], TempBuffer, 0, TCBDRE_LEN);
        }
    }
    // Test with string longer than the dataref supports
    if (!strcmp((char *) iRef, "OverflowStringTest"))
    {
        for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
        {
            sprintf(TempBuffer2, "%03d Test Overflowing the buffer", Item+1);
            XPLMSetDatab(TCBDRE_TestDataRef[Item], TempBuffer2, 0, sizeof(TempBuffer2));
        }
    }
    // Clear the strings
    if (!strcmp((char *) iRef, "EmptyStringTest"))
    {
        strcpy(TempBuffer, "");
        for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
            XPLMSetDatab(TCBDRE_TestDataRef[Item], TempBuffer, 0, TCBDRE_LEN);
    }
    // Substring tests.
    // NOTE - These tests require data so they will do nothing if you have cleared the dataref with the test above.
    // Test with substring 1
    if (!strcmp((char *) iRef, "InsertSubstringTest1"))
    {
        for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
        {
            sprintf(TempBuffer, "[%03d]", Item+1);
            XPLMSetDatab(TCBDRE_TestDataRef[Item], TempBuffer, 5, 5);
        }
    }
    // Test with substring 2
    if (!strcmp((char *) iRef, "InsertSubstringTest2"))
    {
        for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
        {
            sprintf(TempBuffer, "[%03d]", Item+1);
            XPLMSetDatab(TCBDRE_TestDataRef[Item], TempBuffer, 0, 3);
        }
    }
    // Test with substring 3
    if (!strcmp((char *) iRef, "InsertSubstringTest3"))
    {
        for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
        {
            sprintf(TempBuffer, "[%03d]", Item+1);
            XPLMSetDatab(TCBDRE_TestDataRef[Item], TempBuffer, 14, 5);
        }
    }
    // Test with substring 4
    if (!strcmp((char *) iRef, "InsertSubstringTest4"))
    {
        for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
        {
            sprintf(TempBuffer, "X", Item+1);
            XPLMSetDatab(TCBDRE_TestDataRef[Item], TempBuffer, 12, 1);
        }
    }
}

//---------------------------------------------------------------------------

float   TCBDRE_FlightLoopCallback(
                               float              inElapsedSinceLastCall,    
                               float              inElapsedTimeSinceLastFlightLoop,    
                               int                inCounter,    
                               void *             inRefcon)
{
    char TempBuffer[TCBDRE_LEN+1];

    for (int Item=0; Item<TCBDRE_MAX_DATAREFS ; Item++)
    {
        memset(TempBuffer, 0, sizeof(TempBuffer));
        // Get the dataref values
        XPLMGetDatab(TCBDRE_TestDataRef[Item], TempBuffer, 0, TCBDRE_LEN);
        // Add null terminator
        TempBuffer[TCBDRE_LEN] = 0;
        // Copy to our string display buffers that are used in the draw callback
        strcpy(Buffer[Item], TempBuffer);
    }
    
    return 0.1f;
}

//---------------------------------------------------------------------------

void TCBDRE_DrawWindowCallback(
                                   XPLMWindowID         inWindowID,    
                                   void *               inRefcon)
{

    float        rgb [] = { 1.0, 1.0, 1.0 };
    int            l, t, r, b;

    XPLMGetWindowGeometry(inWindowID, &l, &t, &r, &b);
    XPLMDrawTranslucentDarkBox(l, t, r, b);

    // Display the strings that we copied in the FlightLoopCallback
    for (int i=0; i<10; i++)
        XPLMDrawString(rgb, l+10, (t-20) - (10*i), Buffer[i], NULL, xplmFont_Basic);
}                                   

//---------------------------------------------------------------------------
// Not used but still required
//---------------------------------------------------------------------------

void TCBDRE_HandleKeyCallback(
                                   XPLMWindowID         inWindowID,    
                                   char                 inKey,    
                                   XPLMKeyFlags         inFlags,    
                                   char                 inVirtualKey,    
                                   void *               inRefcon,    
                                   int                  losingFocus)
{
}                                   

//---------------------------------------------------------------------------
// Not used but still required
//---------------------------------------------------------------------------

int TCBDRE_HandleMouseClickCallback(
                                   XPLMWindowID         inWindowID,    
                                   int                  x,    
                                   int                  y,    
                                   XPLMMouseStatus      inMouse,    
                                   void *               inRefcon)
{
    return 1;
}                                      

//---------------------------------------------------------------------------