Added support for newlines in TEXT command.

This commit is contained in:
Jason Watkins
2015-04-08 09:04:33 -07:00
parent f8372f6f4d
commit c3e284fc02
5 changed files with 48 additions and 36 deletions

View File

@@ -227,6 +227,26 @@ public class XPlaneConnectTest
} }
} }
@Test
public void testSendTEXT_Multiline() throws IOException
{
String msg = "XPlaneConnect Java message test.\nNow with new lines!\rAnd another...\r\nAnd now a double.";
try(XPlaneConnect xpc = new XPlaneConnect())
{
xpc.sendTEXT(msg, 200, 400);
}
}
@Test
public void testSendTEXT_Long() throws IOException
{
String msg = "XPlaneConnect Java message test.\nNow with new lines!\rAnd another...\r\nAnd now a double\nAnd finally a really long line because that seemed to break things.";
try(XPlaneConnect xpc = new XPlaneConnect())
{
xpc.sendTEXT(msg, 200, 400);
}
}
@Test @Test
public void testSendDREF() throws IOException public void testSendDREF() throws IOException
{ {

View File

@@ -507,19 +507,19 @@ int handleTEXT(char *buf, int len)
updateLog("[TEXT] ERROR: Length less than 14 bytes", 39); updateLog("[TEXT] ERROR: Length less than 14 bytes", 39);
return -1; return -1;
} }
int msgLen = buf[13]; size_t msgLen = (unsigned char)buf[13];
if (msgLen == 0) if (msgLen == 0)
{ {
updateLog("[TEXT] Text cleared", 19);
XPCClearMessage(); XPCClearMessage();
updateLog("[TEXT] Text cleared", 19);
} }
else else
{ {
updateLog("[TEXT] Text set", 15);
int x = *((int*)(buf + 5)); int x = *((int*)(buf + 5));
int y = *((int*)(buf + 9)); int y = *((int*)(buf + 9));
strncpy(msg, buf + 14, msgLen); strncpy(msg, buf + 14, msgLen);
XPCSetMessage(x, y, msg); XPCSetMessage(x, y, msg);
updateLog("[TEXT] Text set", 15);
} }
return 0; return 0;
} }

Binary file not shown.

Binary file not shown.

View File

@@ -5,18 +5,23 @@
#include <string.h> #include <string.h>
//Internal Memory //Internal Memory
static int msgEnabled = 0; static bool msgEnabled = false;
static int msgX = -1; static int msgX = -1;
static int msgY = -1; static int msgY = -1;
static char* msgVal = NULL; static char msgVal[256] = { 0 };
static float rgb[4] = { 0.5F, 1.0F, 0.5F }; static size_t newLineCount = 0;
static size_t newLines[64] = { 0 };
static float rgb[3] = { 0.25F, 1.0F, 0.25F };
//Internal Functions //Internal Functions
static int MessageDrawCallback(XPLMDrawingPhase inPhase, int inIsBefore, void * inRefcon) static int MessageDrawCallback(XPLMDrawingPhase inPhase, int inIsBefore, void * inRefcon)
{ {
if (msgVal) XPLMDrawString(rgb, msgX, msgY, msgVal, NULL, xplmFont_Basic);
int y = msgY - 16;
for (size_t i = 0; i < newLineCount; ++i)
{ {
XPLMDrawString(rgb, msgX, msgY, msgVal, NULL, xplmFont_Basic); XPLMDrawString(rgb, msgX, y, msgVal + newLines[i], NULL, xplmFont_Basic);
y -= 16;
} }
return 1; return 1;
} }
@@ -24,52 +29,39 @@ static int MessageDrawCallback(XPLMDrawingPhase inPhase, int inIsBefore, void *
//Public Functions //Public Functions
void XPCClearMessage() void XPCClearMessage()
{ {
if (msgVal != NULL) XPLMUnregisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL);
{ msgEnabled = false;
free(msgVal);
}
msgVal = NULL;
if (msgEnabled)
{
XPLMUnregisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL);
msgEnabled = 0;
}
} }
void XPCSetMessage(int x, int y, char* msg) void XPCSetMessage(int x, int y, char* msg)
{ {
//Determine size of message and clear instead if the message string //Determine size of message and clear instead if the message string
//is empty. //is empty.
size_t len = strlen(msg); size_t len = strnlen(msg, 255);
if (len == 0) if (len == 0)
{ {
XPCClearMessage(); XPCClearMessage();
return; return;
} }
//Try to size msgVal to fit the new message. If we fail for any //Set the message, location, and mark new lines.
//reason, clear the message and bail. strncpy(msgVal, msg, len);
if (msgVal != NULL) newLineCount = 0;
for (size_t i = 0; i < len && newLineCount < 64; ++i)
{ {
msgVal = (char*)realloc(msgVal, len); if (msgVal[i] == '\n' || msgVal[i] == '\r')
{
msgVal[i] = 0;
newLines[newLineCount++] = i + 1;
}
} }
else
{
msgVal = (char*)malloc(len);
}
if (!msgVal)
{
XPCClearMessage();
return;
}
//Set the message, location, and enable drawing if necessary.
strcpy(msgVal, msg);
msgX = x < 0 ? 10 : x; msgX = x < 0 ? 10 : x;
msgY = y < 0 ? 600 : y; msgY = y < 0 ? 600 : y;
//Enable drawing if necessary
if (!msgEnabled) if (!msgEnabled)
{ {
XPLMRegisterDrawCallback(MessageDrawCallback, xplm_Phase_LastCockpit, 0, NULL); XPLMRegisterDrawCallback(MessageDrawCallback, xplm_Phase_LastCockpit, 0, NULL);
msgEnabled = 1; msgEnabled = true;
} }
} }