Skip to content

Getting started

Stanislav edited this page Jan 2, 2022 · 20 revisions

So, let's go.

The '(!!)' mark is placed near the most important thoughts(such as key features or most common mistakes)

Chapter 1. GUI class

Note: In this chapter I will explain how to deal with existing adapters, which are Minecraft Forge adapters. Dealing with your own adapters can be different.

Firstly, we need main class. Built-in adapters provides three classes for it: ExtendedGui, ExtendedGuiScreen and ExtendedGuiContainer, which are adapters to Gui, GuiScreen and GuiContainer respectively. It contains some additional methods to interact with GExt, I should see at ExtendedGuiScreen, using others should be the same.

public class GuiTest extends ExtendedGuiScreen {

    @Override
    public void initLayout() {
        // here we start building structure
    }
}

Chapter 2. Constructing components

GExt provides builders as the easiest solution to control GUI content. Class Graphics contains static methods to provide builders. It's designed to make simple syntax with high customization.

For example, this code constructs plain label:

Graphics.label()
  .text("Hello, world")   // label should be rendered as text "Hello world"
  .placeAt(50, 50)        // and it should be placed at (50, 50), counted from parent **(!!)**
  .build()

Label's builder contains additional methods for extended properties. It seems you can(not always!) find them in this wiki in component-overview section. All actual information about builders and features provided by components should be in javadoc. This wiki contains general overview information and, of course, updated later than code.

Chapter 3. Let's compose!

The first key feature of the layout engine is what? relative positioning. Let's do it! GExt provides components called 'layouts'. It implements IGraphicsLayout interface, so you can look up for it's API. In simple words, these components can consume other components and transform it according to self rules(for example, coordinates).

Now we have this:

  • BasicLayout - a very basic layout, that does completely nothing with components, except assigning ID. All layouts inherit this behaviour.
  • GPanel - adds ability to assign scrollbars. This enables scrolling the content.
  • GGrid - places components in grid. Grid parameters should be passed in constructor.
  • GList - places components in line. Horisontal or vertical - should be passed in constructor.
  • GTabPanel - a list that manages the content of the other container

Containers itself can be created by the same way as other graphics components. For details see Chapter 2.

Great! Now we can simply add component:

container.addComponent(depth, component)

This is adding to self-created container. In root container (Extended*) different method should be used:

add(depth, component) // depth is the z-coordinate(screen-orthogonate). You should assign higher values to place component to the top level. This affects only display things, no mouse events and so on.

or

add(component) // equivalent to add(0, component) 

Chapter 4. Creating new component

Oh, you are still alive...

Creating new component is simple - every class implements IGraphicsComponent interface becomes a graphics component. But in practice extening GBasic class is more suitable - it contains a very basic default implementation of an empty graphics component. Then you should override some of plenty functions in it to get functionality that you need. You can find information about API in package com.github.stannismod.gext.api.

Chapter 5. Creating root layout

You are so strong...

Ok, now you are looking for an integration of your GUI abstraction to GExt layouts. Let's get it!

Firstly, create an extension of your GUI class(for example, Gui) and implement IRootLayout interface:

public class ExtendedGui extends Gui implements IRootLayout {
    // some code here
}

Secondly, implement method provided by interface:

@Override
public @NotNull IGraphicsLayout<IGraphicsComponent> layout() {
    return layout; // your root container here
}

Note: Creating the root IGraphicsLayout instance(layout component) and returning it in the corresponding method should be a good solution. Now we using it in default adapters, where you can see an example

(!!) So, you created a GUI Root adapter. What else? To maintain GExt working right with it, you must implement the entry points of user input, drawing method and so on. You can grab all you need from the default adapters for your version.

GUI structure can be specified in initLayout() method, which IRootLayout interface also provides for you.

Chapter 6. Writing an adapter

// TODO

Chapter 7. Context menu

// TODO