From a903859a3ca7501e753c02eb7af0c715da447dd9 Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Tue, 7 Apr 2015 15:02:24 -0700 Subject: [PATCH] Added support for the TEXT command to the Java client. --- Java/src/XPlaneConnect.java | 51 ++++++++++++++++++- TestScripts/Java Tests/XPlaneConnectTest.java | 10 ++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Java/src/XPlaneConnect.java b/Java/src/XPlaneConnect.java index cdc4a37..4a34981 100644 --- a/Java/src/XPlaneConnect.java +++ b/Java/src/XPlaneConnect.java @@ -457,7 +457,7 @@ public class XPlaneConnect implements AutoCloseable cur += 4; } } - bb.put(cur, (byte)aircraft); + bb.put(cur, (byte) aircraft); //Build and send message ByteArrayOutputStream os = new ByteArrayOutputStream(); @@ -586,6 +586,55 @@ public class XPlaneConnect implements AutoCloseable sendUDP(os.toByteArray()); } + /** + * Sets a message to be displayed on the screen in X-Plane at the default screen location. + * + * @param msg The message to display. Should not contain any newline characters. + * @throws IOException If the command cannot be sent. + */ + public void sendTEXT(String msg) throws IOException + { + sendTEXT(msg, -1, -1); + } + + /** + * Sets a message to be displayed on the screen in X-Plane at the specified coordinates. + * + * @param msg The message to display. Should not contain any newline characters. + * @param x The number of pixels from the right edge of the screen to display the text. + * @param y The number of pixels from the bottom edge of the screen to display the text. + * @throws IOException If the command cannot be sent. + */ + public void sendTEXT(String msg, int x, int y) throws IOException + { + //Preconditions + if(msg == null) + { + msg = ""; + } + + //Convert drefs to bytes. + byte[] msgBytes = msg.getBytes(StandardCharsets.UTF_8); + if(msgBytes.length > 255) + { + throw new IllegalArgumentException("msg must be less than 255 bytes in UTF-8."); + } + + ByteBuffer bb = ByteBuffer.allocate(8); + bb.order(ByteOrder.LITTLE_ENDIAN); + bb.putInt(0, x); + bb.putInt(4, y); + + //Build and send message + ByteArrayOutputStream os = new ByteArrayOutputStream(); + os.write("TEXT".getBytes(StandardCharsets.UTF_8)); + os.write(0xFF); //Placeholder for message length + os.write(bb.array()); + os.write(msgBytes.length); + os.write(msgBytes); + sendUDP(os.toByteArray()); + } + /** * Sets the port on which the client will receive data from X-Plane. * diff --git a/TestScripts/Java Tests/XPlaneConnectTest.java b/TestScripts/Java Tests/XPlaneConnectTest.java index 3cfc43e..c8c08f1 100644 --- a/TestScripts/Java Tests/XPlaneConnectTest.java +++ b/TestScripts/Java Tests/XPlaneConnectTest.java @@ -217,6 +217,16 @@ public class XPlaneConnectTest } } + @Test + public void testSendTEXT() throws IOException + { + String msg = "XPlaneConnect Java message test."; + try(XPlaneConnect xpc = new XPlaneConnect()) + { + xpc.sendTEXT(msg, 200, 400); + } + } + @Test public void testSendDREF() throws IOException {