User Interface

Almost all video games have 2D text or elements on the screen that acts as a HUD. This is accomplished in the Kakara Engine through the User Interface system. In order to add these types of elements to the screen you must use a Canvas. The Kakara Engine has two canvases built in: The ComponentCanvas, ObjectCanvas, and DebugCanvas.

Component Canvas

The ComponentCanvas displays UIComponents, which are 2D elements such as: Text, Sprites, Shapes, etc.

To add a ComponentCanvas you construct it in the loadGraphics method of a Scene.

ComponentCanvas componentCanvas = new ComponentCanvas(this);

Text text = new Text("Example Text", Font.getFont("Roboto"));
text.setTextAlign(TextAlign.CENTER);
text.setLineWidth(130);
text.addConstraint(new HorizontalCenterConstraint());
text.addConstraint(new VerticalCenterConstraint());
        
componentCanvas.add(text);

add(componentCanvas);

Object Canvas

The object canvas allows you to add 3D objects to the User Interface. 3D objects on the User Interface will always be at the same position on screen no mater what happens to the camera.

ObjectCanvas objectCanvas = new ObjectCanvas(this);
        
Mesh cubeMesh = new Mesh(CubeData.vertex, CubeData.texture, CubeData.normal, CubeData.indices);
Material material = new Material(new Texture(rm.getResource("Example_Cube.png"), this));
cubeMesh.setMaterial(material);
UIObject cube = new UIObject(cubeMesh);
cube.setPosition(500, 400);
cube.applyRotation(60, 60, 60);
cube.setScale(300);
objectCanvas.add(cube);
        
add(objectCanvas);

Debug Canvas

The debug canvas is a useful tool to help you debug your game. This canvas is not meant to be used in production.

DebugCanvas debugCanvas = new DebugCanvas();
add(debugCanvas);

Last updated