Struct Reference

This reference is meant to be a complementary resource to the Nuklear Usage Guide.

LWJGL has a special type of class which represents structs in C libraries such as Nuklear. These classes are used for efficiently handling memory. To create a struct of a given type, if you intend to keep the struct as a property of your object then use the static create() method of the struct's class. If the struct is just for temporary use then create a memory stack and use the mallocStack() method instead as shown below.

// Creating a permanent struct
NkColor color = NkColor.create();

// Creating a temporary struct
try(MemoryStack stack = MemoryStack.stackPush()) {
  NkColor tempColor = NkColor.mallocStack(stack);

  // Use it before leaving the try{} block
  tempColor.r((byte)255).g(0).b(0).a((byte)255);
  ctx.style().window().fixed_background().set(tempColor);
}

The properties of the struct in Java are represented as methods. A method with no parameters returns the property's value. Passing in a parameter will set the property's value to the value that was passed in. When setting a property, the method returns the struct itself, allowing you to set multiple properties at once by chaining them. Structs can also be set by using an associated Nuklear function, such as nk_rgba() for the NkColor struct.

NkColor color = NkColor.create();
// Set properties
color.r((byte)255).g(0).b(0).a((byte)255);

// Get properties
int r = color.r();
inr g = color.g();
int b = color.b();
int a = color.a();

// Set properties using a Nuklear function
nk_rgba((byte)255, 0, 0, (byte)255, color);

The rest of this section will be dedicated to showing the properties of each struct and the functions that can be used to set the struct's values.

NkColor

Represents a color to be used when styling.

Properties

byte r() // Red channel between 0 to 255
byte g() // Green channel between 0 and 255
byte b() // Blue channel between 0 and 255
byte a() // Alpha channel between 0 and 255
NkRect r(byte value) // Set the red channel
NkRect g(byte value) // Set the green channel
NkRect b(byte value) // Set the blue channel
NkRect a(byte value) // Set the alpha channel

Associated method

Sets the values of the NkColor that was passed in and returns that same NkColor.

NkColor nk_rgba(byte r, byte g, byte b, byte a, NkColor color)

Examples

NkColor color = NkColor.create().r((byte) 255).g((byte) 255).b((byte) 255).a((byte) 255);
NkColor color = NkColor.create();
nk_rgba((byte) 255, (byte) 255, (byte) 255, (byte) 255, color);

NkImage

Represents a bitmap or a slice of a bitmap to be used when styling.

To generate the image struct, you need to be able to reference the image. When using OpenGL, this is done by using the texture ID.

The following code is what we need to get a texture into OpenGL. There are plenty of OpenGL tutorials online and I need to focus on explaining Nuklear, so I won't give a detailed explanation here.

IntBuffer w    = BufferUtils.createIntBuffer(1);
IntBuffer h    = BufferUtils.createIntBuffer(1);
IntBuffer comp = BufferUtils.createIntBuffer(1);

// getResourceAsByteBuffer() is a function I created that returns the bytes of a
// file given a path to a file. The code is not short, so it is omitted here.
ByteBuffer filedata = getResourceAsByteBuffer("/texture.png");

// These are the properties we need to define a texture in OpenGL
ByteBuffer textureData = stbi_load_from_memory(filedata, w, h, comp, 4);
int textureWidth = w.get(0);
int textureHeight = h.get(0);

// Generate OpenGL texture
int textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

// Set the texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// Store the texture data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

// Enable mipmapping
glGenerateMipmap(GL_TEXTURE_2D);

// Unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);

The textureID variable from the previous code can now be used to create the NkImage struct as shown in the following code. In this code, we pass a function to the NkImage, what the function does is take the NkHandle object that Nuklear passes in and sets its .id() property to be the ID of the texture in OpenGL.

// Create an NkImage using the entire texture
NkImage image = NkImage.create();
image.handle(it -> it.id(textureID));

Associated method: nk_subimage_id()

nk_subimage_id(int textureID, short textureWidth, short textureHeight, NkRect rect, NkImage image);

This method allows us to create an NkImage that only uses a slice of an image file. It takes the texture information and a rectangle, creates the slice and then stores the slice into the NkImage that was passed in.

In the next example, instead of showing entire texture in the image, we just show a slice of it. This is good for when you have a lot of buttons and want to give each one an icon. Put all of those icons in one texture and then just cut slices from it.

Create an NkImage using a slice of the texture
// This uses the textureID, textureWidth and textureHeight variables that were created above
NkImage image = NkImage.create();
NkRect rect = NkRect.create();
nk_rect(0, 0, 50, 50, rect); // Cut a slice from the texture of size 50x50 that starts at the top left corner
nk_subimage_id(textureID, (short) textureWidth, (short) textureHeight, rect, image);

NkRect

Represents a rectangle, usually used when defining sizes of widgets.

Properties

float x() // X coordinate of the top left corner
float y() // Y coordinate of the top left corner
float w() // Width of the rectangle
float h() // Height of the rectangle
NkRect x(float value) // Set the value of X
NkRect y(float value) // Set the value of Y
NkRect w(float value) // Set the width
NkRect h(float value) // Set the height

Associated method

Sets the values of the NkRect that was passed in and returns that same NkRect.

NkRect nk_rect(float x, float y, float w, float height, NkRect rect)

Examples

NkRect rect = NkRect.create().x(10).y(10).w(200).h(120);
NkRect rect = NkRect.create();
nk_rect(10, 10, 200, 120, rect);

NkStyleButton

See Styling the Window for a list of all of the properties and their usage.

NkStyleWindowHeader

See Styling the Window for a list of all of the properties and their usage.

NkVec2

Represents a 2D vector, usually used when setting style values that can be different horizontally than vertically.

Properties

float x() // Horizontal length
float y() // Vertical length
NkRect x(float value) // Sets the horizontal length
NkRect y(float value) // Set the vertical length

Associated method

Assigns the x and y values to the .x() and .y() properties of the NkVec2 that was passed in and then returns that same NkVec2.

NkVec2 nk_vec2(float x, float y, NkVec2 data)

Examples

NkVec2 padding = NkVec2.create().x(4).y(8);
NkVec2 padding = context.style().window().padding();
nk_vec2(4, 8, padding);