diff --git a/TestScripts/Java Tests/XPlaneConnectTest.java b/TestScripts/Java Tests/XPlaneConnectTest.java index c8c08f1..3667ebd 100644 --- a/TestScripts/Java Tests/XPlaneConnectTest.java +++ b/TestScripts/Java Tests/XPlaneConnectTest.java @@ -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 public void testSendDREF() throws IOException { diff --git a/xpcPlugin/XPCPlugin.cpp b/xpcPlugin/XPCPlugin.cpp index f65dac8..5ed485e 100755 --- a/xpcPlugin/XPCPlugin.cpp +++ b/xpcPlugin/XPCPlugin.cpp @@ -507,19 +507,19 @@ int handleTEXT(char *buf, int len) updateLog("[TEXT] ERROR: Length less than 14 bytes", 39); return -1; } - int msgLen = buf[13]; + size_t msgLen = (unsigned char)buf[13]; if (msgLen == 0) { - updateLog("[TEXT] Text cleared", 19); XPCClearMessage(); + updateLog("[TEXT] Text cleared", 19); } else { - updateLog("[TEXT] Text set", 15); int x = *((int*)(buf + 5)); int y = *((int*)(buf + 9)); strncpy(msg, buf + 14, msgLen); XPCSetMessage(x, y, msg); + updateLog("[TEXT] Text set", 15); } return 0; } diff --git a/xpcPlugin/XPlaneConnect/64/win.xpl b/xpcPlugin/XPlaneConnect/64/win.xpl index 8459a08..e287685 100644 Binary files a/xpcPlugin/XPlaneConnect/64/win.xpl and b/xpcPlugin/XPlaneConnect/64/win.xpl differ diff --git a/xpcPlugin/XPlaneConnect/win.xpl b/xpcPlugin/XPlaneConnect/win.xpl index 7fdd2bf..3d8f45a 100644 Binary files a/xpcPlugin/XPlaneConnect/win.xpl and b/xpcPlugin/XPlaneConnect/win.xpl differ diff --git a/xpcPlugin/xpcDrawing.cpp b/xpcPlugin/xpcDrawing.cpp index 3f334a8..16bb1b4 100644 --- a/xpcPlugin/xpcDrawing.cpp +++ b/xpcPlugin/xpcDrawing.cpp @@ -5,18 +5,23 @@ #include //Internal Memory -static int msgEnabled = 0; +static bool msgEnabled = false; static int msgX = -1; static int msgY = -1; -static char* msgVal = NULL; -static float rgb[4] = { 0.5F, 1.0F, 0.5F }; +static char msgVal[256] = { 0 }; +static size_t newLineCount = 0; +static size_t newLines[64] = { 0 }; +static float rgb[3] = { 0.25F, 1.0F, 0.25F }; //Internal Functions 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; } @@ -24,52 +29,39 @@ static int MessageDrawCallback(XPLMDrawingPhase inPhase, int inIsBefore, void * //Public Functions void XPCClearMessage() { - if (msgVal != NULL) - { - free(msgVal); - } - msgVal = NULL; - if (msgEnabled) - { - XPLMUnregisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL); - msgEnabled = 0; - } + XPLMUnregisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL); + msgEnabled = false; } void XPCSetMessage(int x, int y, char* msg) { //Determine size of message and clear instead if the message string //is empty. - size_t len = strlen(msg); + size_t len = strnlen(msg, 255); if (len == 0) { XPCClearMessage(); return; } - //Try to size msgVal to fit the new message. If we fail for any - //reason, clear the message and bail. - if (msgVal != NULL) + //Set the message, location, and mark new lines. + strncpy(msgVal, msg, len); + 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; msgY = y < 0 ? 600 : y; + + //Enable drawing if necessary if (!msgEnabled) { XPLMRegisterDrawCallback(MessageDrawCallback, xplm_Phase_LastCockpit, 0, NULL); - msgEnabled = 1; + msgEnabled = true; } } \ No newline at end of file