User InterfaceΒΆ
The following script demonstrates how to use the UI service to display text and handle basic user input. It adds a panel to the left side of the screen, displaying the current thrust produced by the vessel and a button to set the throttle to maximum.
using System;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
using KRPC.Client.Services.UI;
class UserInterface
{
public static void Main ()
{
var conn = new Connection ("User Interface Example");
var canvas = conn.UI ().StockCanvas;
// Get the size of the game window in pixels
var screenSize = canvas.RectTransform.Size;
// Add a panel to contain the UI elements
var panel = canvas.AddPanel ();
// Position the panel on the left of the screen
var rect = panel.RectTransform;
rect.Size = Tuple.Create (200.0, 100.0);
rect.Position = Tuple.Create ((110-(screenSize.Item1)/2), 0.0);
// Add a button to set the throttle to maximum
var button = panel.AddButton ("Full Throttle");
button.RectTransform.Position = Tuple.Create (0.0, 20.0);
// Add some text displaying the total engine thrust
var text = panel.AddText ("Thrust: 0 kN");
text.RectTransform.Position = Tuple.Create (0.0, -20.0);
text.Color = Tuple.Create (1.0, 1.0, 1.0);
text.Size = 18;
// Set up a stream to monitor the throttle button
var buttonClicked = conn.AddStream (() => button.Clicked);
var vessel = conn.SpaceCenter ().ActiveVessel;
while (true) {
// Handle the throttle button being clicked
if (buttonClicked.Get ()) {
vessel.Control.Throttle = 1;
button.Clicked = false;
}
// Update the thrust text
text.Content = "Thrust: " + (vessel.Thrust/1000) + " kN";
System.Threading.Thread.Sleep (1000);
}
}
}
#include <chrono>
#include <thread>
#include <krpc.hpp>
#include <krpc/services/space_center.hpp>
#include <krpc/services/ui.hpp>
int main() {
krpc::Client conn = krpc::connect("User Interface Example");
krpc::services::SpaceCenter space_center(&conn);
krpc::services::UI ui(&conn);
auto canvas = ui.stock_canvas();
// Get the size of the game window in pixels
auto screen_size = canvas.rect_transform().size();
// Add a panel to contain the UI elements
auto panel = canvas.add_panel();
// Position the panel on the left of the screen
auto rect = panel.rect_transform();
rect.set_size(std::make_tuple(200, 100));
rect.set_position(std::make_tuple(110-(std::get<0>(screen_size)/2), 0));
// Add a button to set the throttle to maximum
auto button = panel.add_button("Full Throttle");
button.rect_transform().set_position(std::make_tuple(0, 20));
// Add some text displaying the total engine thrust
auto text = panel.add_text("Thrust: 0 kN");
text.rect_transform().set_position(std::make_tuple(0, -20));
text.set_color(std::make_tuple(1, 1, 1));
text.set_size(18);
// Set up a stream to monitor the throttle button
auto button_clicked = button.clicked_stream();
auto vessel = space_center.active_vessel();
while (true) {
// Handle the throttle button being clicked
if (button_clicked()) {
vessel.control().set_throttle(1);
button.set_clicked(false);
}
// Update the thrust text
text.set_content("Thrust: " + std::to_string((int)(vessel.thrust()/1000)) + " kN");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
import krpc.client.Connection;
import krpc.client.RPCException;
import krpc.client.Stream;
import krpc.client.StreamException;
import krpc.client.services.SpaceCenter;
import krpc.client.services.SpaceCenter.Vessel;
import krpc.client.services.UI;
import krpc.client.services.UI.Button;
import krpc.client.services.UI.Canvas;
import krpc.client.services.UI.Panel;
import krpc.client.services.UI.RectTransform;
import krpc.client.services.UI.Text;
import org.javatuples.Pair;
import org.javatuples.Triplet;
import java.io.IOException;
public class UserInterface {
public static void main(String[] args)
throws IOException, RPCException, InterruptedException, StreamException {
Connection connection = Connection.newInstance("User Interface Example");
SpaceCenter spaceCenter = SpaceCenter.newInstance(connection);
UI ui = UI.newInstance(connection);
Canvas canvas = ui.getStockCanvas();
// Get the size of the game window in pixels
Pair<Double, Double> screenSize = canvas.getRectTransform().getSize();
// Add a panel to contain the UI elements
Panel panel = canvas.addPanel(true);
// Position the panel on the left of the screen
RectTransform rect = panel.getRectTransform();
rect.setSize(new Pair<Double,Double>(200.0, 100.0));
rect.setPosition(
new Pair<Double,Double>((110-(screenSize.getValue0())/2), 0.0));
// Add a button to set the throttle to maximum
Button button = panel.addButton("Full Throttle", true);
button.getRectTransform().setPosition(new Pair<Double,Double>(0.0, 20.0));
// Add some text displaying the total engine thrust
Text text = panel.addText("Thrust: 0 kN", true);
text.getRectTransform().setPosition(new Pair<Double,Double>(0.0, -20.0));
text.setColor(new Triplet<Double,Double,Double>(1.0, 1.0, 1.0));
text.setSize(18);
// Set up a stream to monitor the throttle button
Stream<Boolean> buttonClicked = connection.addStream(button, "getClicked");
Vessel vessel = spaceCenter.getActiveVessel();
while (true) {
// Handle the throttle button being clicked
if (buttonClicked.get ()) {
vessel.getControl().setThrottle(1);
button.setClicked(false);
}
// Update the thrust text
text.setContent(String.format("Thrust: %.0f kN", (vessel.getThrust()/1000)));
Thread.sleep(1000);
}
}
}
local krpc = require 'krpc'
local platform = require 'krpc.platform'
local List = require 'pl.List'
local conn = krpc.connect('User Interface Example')
local canvas = conn.ui.stock_canvas
-- Get the size of the game window in pixels
local screen_size = canvas.rect_transform.size
-- Add a panel to contain the UI elements
local panel = canvas:add_panel()
-- Position the panel on the left of the screen
local rect = panel.rect_transform
rect.size = List{200, 100}
rect.position = List{110-(screen_size[1]/2), 0}
-- Add a button to set the throttle to maximum
local button = panel:add_button("Full Throttle")
button.rect_transform.position = List{0, 20}
-- Add some text displaying the total engine thrust
local text = panel:add_text("Thrust: 0 kN")
text.rect_transform.position = List{0, -20}
text.color = List{1, 1, 1}
text.size = 18
local vessel = conn.space_center.active_vessel
while true do
-- Handle the throttle button being clicked
if button.clicked then
vessel.control.throttle = 1
button.clicked = false
end
-- Update the thrust text
text.content = string.format('Thrust: %.1f kN', vessel.thrust/1000)
platform.sleep(0.1)
end
import time
import krpc
conn = krpc.connect(name='User Interface Example')
canvas = conn.ui.stock_canvas
# Get the size of the game window in pixels
screen_size = canvas.rect_transform.size
# Add a panel to contain the UI elements
panel = canvas.add_panel()
# Position the panel on the left of the screen
rect = panel.rect_transform
rect.size = (200, 100)
rect.position = (110-(screen_size[0]/2), 0)
# Add a button to set the throttle to maximum
button = panel.add_button("Full Throttle")
button.rect_transform.position = (0, 20)
# Add some text displaying the total engine thrust
text = panel.add_text("Thrust: 0 kN")
text.rect_transform.position = (0, -20)
text.color = (1, 1, 1)
text.size = 18
# Set up a stream to monitor the throttle button
button_clicked = conn.add_stream(getattr, button, 'clicked')
vessel = conn.space_center.active_vessel
while True:
# Handle the throttle button being clicked
if button_clicked():
vessel.control.throttle = 1
button.clicked = False
# Update the thrust text
text.content = 'Thrust: %d kN' % (vessel.thrust/1000)
time.sleep(0.1)