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, which can be dragged with the mouse. A vertical layout arranges its contents in a column: a button that sets the throttle to maximum, a slider that sets it directly, and text showing the current thrust produced by the vessel. A tooltip is shown while the mouse rests on the button.
#include <stdio.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define sleep(seconds) Sleep((seconds) * 1000)
#else
#include <unistd.h>
#endif
#include <krpc_cnano.h>
#include <krpc_cnano/services/space_center.h>
#include <krpc_cnano/services/std_lib.h>
#include <krpc_cnano/services/ui.h>
int main() {
krpc_connection_t conn;
krpc_open(&conn, "COM0");
krpc_connect(conn, "User Interface Example");
krpc_UI_Canvas_t canvas;
krpc_UI_StockCanvas(conn, &canvas);
krpc_UI_RectTransform_t canvas_rect;
krpc_UI_Canvas_RectTransform(conn, &canvas_rect, canvas);
krpc_tuple_double_double_t screen_size;
krpc_UI_RectTransform_Size(conn, &screen_size, canvas_rect);
// Add a panel to contain the UI elements. The user can drag it around the
// screen with the mouse.
krpc_UI_Panel_t panel;
krpc_UI_Canvas_AddPanel(conn, &panel, canvas, true);
krpc_UI_Panel_set_Draggable(conn, panel, true);
// Position the panel on the left of the screen
krpc_UI_RectTransform_t rect;
krpc_UI_Panel_RectTransform(conn, &rect, panel);
krpc_tuple_double_double_t panel_size = {200, 120};
krpc_UI_RectTransform_set_Size(conn, rect, &panel_size);
krpc_tuple_double_double_t panel_pos = {110 - (screen_size.e0 / 2), 0};
krpc_UI_RectTransform_set_Position(conn, rect, &panel_pos);
// Lay the contents of the panel out in a column
krpc_UI_Layout_t layout;
krpc_UI_Panel_AddVerticalLayout(conn, &layout, panel);
krpc_UI_Padding_t padding = {10, 10, 10, 10};
krpc_UI_Layout_set_Padding(conn, layout, &padding);
krpc_UI_Layout_set_Spacing(conn, layout, 6);
// Add a button to set the throttle to maximum, with a tooltip shown while
// the mouse rests on it
krpc_UI_Button_t button;
krpc_UI_Panel_AddButton(conn, &button, panel, "Full Throttle", true);
krpc_UI_Button_set_Tooltip(conn, button, "Set the throttle to maximum");
krpc_UI_LayoutElement_t button_element;
krpc_UI_Button_LayoutElement(conn, &button_element, button);
krpc_tuple_double_double_t button_size = {-1, 30};
krpc_UI_LayoutElement_set_PreferredSize(conn, button_element, &button_size);
// Add a slider to set the throttle by hand
krpc_UI_Slider_t slider;
krpc_UI_Panel_AddSlider(conn, &slider, panel, false, true);
krpc_UI_LayoutElement_t slider_element;
krpc_UI_Slider_LayoutElement(conn, &slider_element, slider);
krpc_tuple_double_double_t slider_size = {-1, 20};
krpc_UI_LayoutElement_set_PreferredSize(conn, slider_element, &slider_size);
// Add some text displaying the total engine thrust
krpc_UI_Text_t text;
krpc_UI_Panel_AddText(conn, &text, panel, "Thrust: 0 kN", true);
krpc_StdLib_ColorRGBA_t color = {1, 1, 1, 1};
krpc_UI_Text_set_Color(conn, text, &color);
krpc_UI_Text_set_Size(conn, text, 18);
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
while (true) {
// Handle the throttle button being clicked
bool clicked;
krpc_UI_Button_Clicked(conn, &clicked, button);
if (clicked) {
krpc_SpaceCenter_Control_t control;
krpc_SpaceCenter_Vessel_Control(conn, &control, vessel);
krpc_SpaceCenter_Control_set_Throttle(conn, control, 1);
krpc_UI_Slider_set_Value(conn, slider, 1);
krpc_UI_Button_set_Clicked(conn, button, false);
}
// Handle the user moving the throttle slider
bool changed;
krpc_UI_Slider_Changed(conn, &changed, slider);
if (changed) {
float value;
krpc_UI_Slider_Value(conn, &value, slider);
krpc_SpaceCenter_Control_t control;
krpc_SpaceCenter_Vessel_Control(conn, &control, vessel);
krpc_SpaceCenter_Control_set_Throttle(conn, control, value);
krpc_UI_Slider_set_Changed(conn, slider, false);
}
float thrust;
krpc_SpaceCenter_Vessel_Thrust(conn, &thrust, vessel);
char content[32];
snprintf(content, sizeof(content), "Thrust: %d kN", (int)(thrust / 1000));
krpc_UI_Text_set_Content(conn, text, content);
sleep(1);
}
}
using System;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;
using KRPC.Client.Services.StdLib;
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. The user can drag it around
// the screen with the mouse.
var panel = canvas.AddPanel ();
panel.Draggable = true;
// Position the panel on the left of the screen
var rect = panel.RectTransform;
rect.Size = Tuple.Create (200.0, 120.0);
rect.Position = Tuple.Create ((110-(screenSize.Item1)/2), 0.0);
// Lay the contents of the panel out in a column
var layout = panel.AddVerticalLayout ();
layout.Padding = new Padding (10, 10, 10, 10);
layout.Spacing = 6;
// Add a button to set the throttle to maximum, with a tooltip shown
// while the mouse rests on it
var button = panel.AddButton ("Full Throttle");
button.Tooltip = "Set the throttle to maximum";
button.LayoutElement.PreferredSize = Tuple.Create (-1.0, 30.0);
// Add a slider to set the throttle by hand
var slider = panel.AddSlider ();
slider.LayoutElement.PreferredSize = Tuple.Create (-1.0, 20.0);
// Add some text displaying the total engine thrust
var text = panel.AddText ("Thrust: 0 kN");
text.Color = new ColorRGBA (1, 1, 1, 1);
text.Size = 18;
// Set up streams to monitor the button and the slider
var buttonClicked = conn.AddStream (() => button.Clicked);
var sliderChanged = conn.AddStream (() => slider.Changed);
var vessel = conn.SpaceCenter ().ActiveVessel;
while (true) {
// Handle the throttle button being clicked
if (buttonClicked.Get ()) {
vessel.Control.Throttle = 1;
slider.Value = 1;
button.Clicked = false;
}
// Handle the user moving the throttle slider
if (sliderChanged.Get ()) {
vessel.Control.Throttle = slider.Value;
slider.Changed = 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/std_lib.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. The user can drag it around the
// screen with the mouse.
auto panel = canvas.add_panel();
panel.set_draggable(true);
// Position the panel on the left of the screen
auto rect = panel.rect_transform();
rect.set_size(std::make_tuple(200, 120));
rect.set_position(std::make_tuple(110-(std::get<0>(screen_size)/2), 0));
// Lay the contents of the panel out in a column
auto layout = panel.add_vertical_layout();
layout.set_padding(krpc::services::UI::Padding(10, 10, 10, 10));
layout.set_spacing(6);
// Add a button to set the throttle to maximum, with a tooltip shown while
// the mouse rests on it
auto button = panel.add_button("Full Throttle");
button.set_tooltip("Set the throttle to maximum");
button.layout_element().set_preferred_size(std::make_tuple(-1, 30));
// Add a slider to set the throttle by hand
auto slider = panel.add_slider();
slider.layout_element().set_preferred_size(std::make_tuple(-1, 20));
// Add some text displaying the total engine thrust
auto text = panel.add_text("Thrust: 0 kN");
text.set_color(krpc::services::StdLib::ColorRGBA(1, 1, 1, 1));
text.set_size(18);
// Set up streams to monitor the button and the slider
auto button_clicked = button.clicked_stream();
auto slider_changed = slider.changed_stream();
auto vessel = space_center.active_vessel().value();
while (true) {
// Handle the throttle button being clicked
if (button_clicked()) {
vessel.control().set_throttle(1);
slider.set_value(1);
button.set_clicked(false);
}
// Handle the user moving the throttle slider
if (slider_changed()) {
vessel.control().set_throttle(slider.value());
slider.set_changed(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.StdLib;
import krpc.client.services.UI;
import krpc.client.services.UI.Button;
import krpc.client.services.UI.Canvas;
import krpc.client.services.UI.Layout;
import krpc.client.services.UI.Panel;
import krpc.client.services.UI.RectTransform;
import krpc.client.services.UI.Slider;
import krpc.client.services.UI.Text;
import org.javatuples.Pair;
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. The user can drag it around
// the screen with the mouse.
Panel panel = canvas.addPanel(true);
panel.setDraggable(true);
// Position the panel on the left of the screen
RectTransform rect = panel.getRectTransform();
rect.setSize(new Pair<Double,Double>(200.0, 120.0));
rect.setPosition(
new Pair<Double,Double>((110-(screenSize.getValue0())/2), 0.0));
// Lay the contents of the panel out in a column
Layout layout = panel.addVerticalLayout();
layout.setPadding(new UI.Padding(10, 10, 10, 10));
layout.setSpacing(6);
// Add a button to set the throttle to maximum, with a tooltip shown
// while the mouse rests on it
Button button = panel.addButton("Full Throttle", true);
button.setTooltip("Set the throttle to maximum");
button.getLayoutElement().setPreferredSize(new Pair<Double,Double>(-1.0, 30.0));
// Add a slider to set the throttle by hand
Slider slider = panel.addSlider(false, true);
slider.getLayoutElement().setPreferredSize(new Pair<Double,Double>(-1.0, 20.0));
// Add some text displaying the total engine thrust
Text text = panel.addText("Thrust: 0 kN", true);
text.setColor(new StdLib.ColorRGBA(1f, 1f, 1f, 1f));
text.setSize(18);
// Set up streams to monitor the button and the slider
Stream<Boolean> buttonClicked = connection.addStream(button, "getClicked");
Stream<Boolean> sliderChanged = connection.addStream(slider, "getChanged");
Vessel vessel = spaceCenter.getActiveVessel();
while (true) {
// Handle the throttle button being clicked
if (buttonClicked.get ()) {
vessel.getControl().setThrottle(1);
slider.setValue(1);
button.setClicked(false);
}
// Handle the user moving the throttle slider
if (sliderChanged.get ()) {
vessel.getControl().setThrottle(slider.getValue());
slider.setChanged(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. The user can drag it around the
-- screen with the mouse.
local panel = canvas:add_panel()
panel.draggable = true
-- Position the panel on the left of the screen
local rect = panel.rect_transform
rect.size = List{200, 120}
rect.position = List{110-(screen_size[1]/2), 0}
-- Lay the contents of the panel out in a column
local layout = panel:add_vertical_layout()
layout.padding = conn.ui.Padding(10, 10, 10, 10)
layout.spacing = 6
-- Add a button to set the throttle to maximum, with a tooltip shown while the
-- mouse rests on it
local button = panel:add_button("Full Throttle")
button.tooltip = "Set the throttle to maximum"
button.layout_element.preferred_size = List{-1, 30}
-- Add a slider to set the throttle by hand
local slider = panel:add_slider()
slider.layout_element.preferred_size = List{-1, 20}
-- Add some text displaying the total engine thrust
local text = panel:add_text("Thrust: 0 kN")
text.color = conn.std_lib.ColorRGBA(1, 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
slider.value = 1
button.clicked = false
end
-- Handle the user moving the throttle slider
if slider.changed then
vessel.control.throttle = slider.value
slider.changed = 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. The user can drag it around the
# screen with the mouse.
panel = canvas.add_panel()
panel.draggable = True
# Position the panel on the left of the screen
rect = panel.rect_transform
rect.size = (200, 120)
rect.position = (110 - (screen_size[0] / 2), 0)
# Lay the contents of the panel out in a column
layout = panel.add_vertical_layout()
layout.padding = conn.ui.Padding(10, 10, 10, 10)
layout.spacing = 6
# Add a button to set the throttle to maximum, with a tooltip shown while the
# mouse rests on it
button = panel.add_button("Full Throttle")
button.tooltip = "Set the throttle to maximum"
button.layout_element.preferred_size = (-1, 30)
# Add a slider to set the throttle by hand
slider = panel.add_slider()
slider.layout_element.preferred_size = (-1, 20)
# Add some text displaying the total engine thrust
text = panel.add_text("Thrust: 0 kN")
text.color = conn.std_lib.ColorRGBA(1, 1, 1, 1)
text.size = 18
# Set up streams to monitor the button and the slider
button_clicked = conn.add_stream(getattr, button, "clicked")
slider_changed = conn.add_stream(getattr, slider, "changed")
vessel = conn.space_center.active_vessel
while True:
# Handle the throttle button being clicked
if button_clicked():
vessel.control.throttle = 1
slider.value = 1
button.clicked = False
# Handle the user moving the throttle slider
if slider_changed():
vessel.control.throttle = slider.value
slider.changed = False
# Update the thrust text
text.content = "Thrust: %d kN" % (vessel.thrust / 1000)
time.sleep(0.1)