Added support for the TEXT command to the Java client.

This commit is contained in:
Jason Watkins
2015-04-07 15:02:24 -07:00
parent 46913596c5
commit a903859a3c
2 changed files with 60 additions and 1 deletions

View File

@@ -457,7 +457,7 @@ public class XPlaneConnect implements AutoCloseable
cur += 4; cur += 4;
} }
} }
bb.put(cur, (byte)aircraft); bb.put(cur, (byte) aircraft);
//Build and send message //Build and send message
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -586,6 +586,55 @@ public class XPlaneConnect implements AutoCloseable
sendUDP(os.toByteArray()); 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. * Sets the port on which the client will receive data from X-Plane.
* *

View File

@@ -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 @Test
public void testSendDREF() throws IOException public void testSendDREF() throws IOException
{ {