Class PolyUI
-
- All Implemented Interfaces:
public final class PolyUIPolyUI is a declarative UI framework developed for, and by Polyfrost.
It is designed to be lightweight, fast, extensible and easy to use, while still being very powerful. Make sure to check out the examples for more information.
PolyUI is split into two parts, being PolyUI and its logic, and it's accompanying Renderer implementation. This allows us to have PolyUI in many places, including inside Minecraft! It is declarative, meaning that you don't have to worry about the logic of the UI, just the layout and the components.
PolyUI has the policy of 'render what you need ONLY WHEN you need it'. Most of the time, PolyUI will be drawing frame buffers to the screen instead of drawing directly to the screen, as long as they are suitably complex for it to be worth it; or not drawing at all! This allows us to have a very fast rendering pipeline, and allows us to have a lot of components on screen at once, without a performance hit.
Rendering can be requested by components, and if so, it will be rendered during the next frame. This should only be requested if it is necessary, for example to do an animation or something.
During a render cycle, PolyUI will systematically go through every layout, and render it to its framebuffer or to the screen. Each layout will then render its components and child layouts, and so on. Rendering happens in three steps:
preRender: This will do pre-rendering logic, such as setting up transformations, updating animations, and more.
render: This is where the actual rendering happens.
postRender: This will do post-rendering logic, such as cleaning up transformations.
Check out some components to see how this works.
Components are the interactive parts of the UI, such as buttons, text fields, etc.
Layouts are the containers for components, such as a grid layout, or a flex layout, etc. They are responsible for positioning and sizing the components.
Properties are the shared states or tokens for the components. They describe default values, and can be overridden by the components.
Interactions are driven by events, which thanks to Kotlin's inlining are a zero-overhead way of distributing events, such as mouse clicks, or key presses.
PolyUI also supports a variety of animations and transitions, which can be used to make your UI more dynamic, along with dynamically adding and removing components.
-
-
Field Summary
Fields Modifier and Type Field Description public final static LoggerLOGGERpublic static FontFamilydefaultFontspublic static FontmonospaceFontpublic static PolyImagedefaultImagepublic final static ByteINIT_NOT_STARTEDpublic final static ByteINIT_SETUPpublic final static ByteINIT_COMPLETEprivate final Settingssettingsprivate Colorscolorsprivate FontFamilyfontsprivate Windowwindowprivate Stringclipboardprivate final PixelLayoutmasterprivate final EventManagereventManagerprivate final KeyBinderkeyBinderprivate final Translatortranslatorprivate final PropertyManagerpropertyManagerprivate final Booleandrewprivate final FloatmouseXprivate final FloatmouseYprivate final BooleanmouseDownprivate Cursorcursorprivate Floatwidthprivate Floatheightprivate final Longdeltaprivate Focusablefocusedprivate final Integerfpsprivate final FloatlongestFrameprivate final FloatshortestFrameprivate final FloatavgFrameprivate final Rendererrenderer
-
Constructor Summary
Constructors Constructor Description PolyUI(String translationDirectory, Renderer renderer, Settings settings, Colors colors, Drawable drawables)PolyUI(String translationDirectory, Renderer renderer, Settings settings, Drawable drawables)PolyUI(String translationDirectory, Renderer renderer, Drawable drawables)PolyUI(Renderer renderer, Drawable drawables)
-
Method Summary
Modifier and Type Method Description final SettingsgetSettings()final ColorsgetColors()Colors attached to this PolyUI instance. final UnitsetColors(Colors colors)final FontFamilygetFonts()Fonts attached to this PolyUI instance. final UnitsetFonts(FontFamily fonts)final WindowgetWindow()The window this polyUI was opened with. final UnitsetWindow(Window window)The window this polyUI was opened with. final StringgetClipboard()final UnitsetClipboard(String clipboard)final PixelLayoutgetMaster()This is the root layout of the UI. final EventManagergetEventManager()final KeyBindergetKeyBinder()final TranslatorgetTranslator()final PropertyManagergetPropertyManager()final BooleangetDrew()weather this PolyUI instance drew on this frame. final FloatgetMouseX()final FloatgetMouseY()final BooleangetMouseDown()final CursorgetCursor()The cursor currently being displayed. final UnitsetCursor(Cursor cursor)final FloatgetWidth()final UnitsetWidth(Float width)final FloatgetHeight()final UnitsetHeight(Float height)final LonggetDelta()the time since the last frame, in nanoseconds. final FocusablegetFocused()final UnitsetFocused(Focusable focused)final IntegergetFps()final FloatgetLongestFrame()final FloatgetShortestFrame()final FloatgetAvgFrame()final RenderergetRenderer()final Unitresize(Integer newWidth, Integer newHeight, Float pixelRatio, Boolean force)Resize this PolyUI instance. final Unitrender()final UnitdrawDebugOverlay(Float x, Float y)draw the debug overlay text. final UnitaddHook(Function1<Renderer, Boolean> func)add a function to be executed on the main thread, after rendering the UI. final PolyUIremoveComponent(Drawable drawable)final PolyUIaddComponent(Drawable drawable)final PolyUIaddComponents(Drawable drawables)final PolyUIaddComponents(Collection<Drawable> drawables)final PolyUIevery(Long nanos, Integer repeats, Function0<Unit> func)add a function that is called every nanos nanoseconds. final PolyUIuntil(Long nanos, Long forNanos, Function0<Unit> func)Add a function that is called every nanos nanoseconds until forNanos nanoseconds have passed. final PolyUIdoWhile(Long everyNanos, Function0<Boolean> condition, Function0<Unit> func)Add a function that is called every everyNanos nanoseconds until condition returns true. final PolyUIafter(Long timeNanos, Function0<Unit> func)Add a function that is called after timeNanos nanoseconds. final ArrayList<Component>getComponentsIn(Float x, Float y, Float width, Float height)Return all components in the given rectangle. final Booleanfocus(Focusable focusable)Sets the focus to the specified focusable element. final Booleanunfocus()final StringgetKeyName(Integer key)Return the key name for the given key code. final StringdebugPrint()print this PolyUI instance's components and children in a list, like this:
The string is also returned.PolyUI(800.0 x 800.0) with 0 components and 2 layouts: PixelLayout@5bcab519 [Draggable](20.0x570.0, 540.0 x 150.0) Text@6eebc39e(40.0x570.0, 520.0x32.0) Block@2f686d1f(40.0x600.0, 120.0x120.0) Block@7085bdee(220.0x600.0, 120.0x120.0) Image@6fd02e5(400.0x600.0, 120.0x120.0 (auto)) ... 2 more FlexLayout@73846619 [Scrollable](20.0x30.0, 693.73267 x 409.47168, buffered, needsRedraw) Block@32e6e9c3(20.0x30.0, 61.111263x42.21167) Block@5056dfcb(86.11127x30.0, 40.909004x76.32132) Block@6574b225(132.02026x30.0, 52.75415x52.59597) Block@2669b199(189.77441x30.0, 76.59671x45.275665)final Longtimed(String msg, Function0<Unit> block)Time the block and return how long it took, as well as logging with the msg if debug is active. StringtoString()final Unitcleanup()cleanup the polyUI instance. -
-
Method Detail
-
getSettings
final Settings getSettings()
-
getColors
final Colors getColors()
Colors attached to this PolyUI instance. This contains all the colors needed for the UI.
Note that changing this value can be an expensive operation while the UI is running, as it has to update all the components.
-
getFonts
final FontFamily getFonts()
Fonts attached to this PolyUI instance. This contains all the colors needed for the UI.
Note that changing this value can be an expensive operation while the UI is running, as it has to update all the components.
-
setFonts
final Unit setFonts(FontFamily fonts)
-
getClipboard
final String getClipboard()
-
setClipboard
final Unit setClipboard(String clipboard)
-
getMaster
final PixelLayout getMaster()
This is the root layout of the UI. It is the parent of all other layouts.
-
getEventManager
final EventManager getEventManager()
-
getKeyBinder
final KeyBinder getKeyBinder()
-
getTranslator
final Translator getTranslator()
-
getPropertyManager
final PropertyManager getPropertyManager()
-
getDrew
final Boolean getDrew()
weather this PolyUI instance drew on this frame.
You can use this value to implement a 'render pausing' function, if you are using a dual-buffered rendering implementation (like OpenGL/GLFW) by enabling this flag:
if (polyUI.drew) glfwSwapBuffers(handle)This is very handy as it will drastically reduce required system resources. If you are looking for more efficiency optimizations, see the settings for max fps and framebuffer settings.
-
getMouseDown
final Boolean getMouseDown()
-
getDelta
final Long getDelta()
the time since the last frame, in nanoseconds. It is used internally a lot for animations, etc.
-
getFocused
final Focusable getFocused()
-
setFocused
final Unit setFocused(Focusable focused)
-
getLongestFrame
final Float getLongestFrame()
-
getShortestFrame
final Float getShortestFrame()
-
getAvgFrame
final Float getAvgFrame()
-
getRenderer
final Renderer getRenderer()
-
resize
final Unit resize(Integer newWidth, Integer newHeight, Float pixelRatio, Boolean force)
Resize this PolyUI instance.
-
drawDebugOverlay
final Unit drawDebugOverlay(Float x, Float y)
draw the debug overlay text. It is right-aligned.
-
addHook
final Unit addHook(Function1<Renderer, Boolean> func)
add a function to be executed on the main thread, after rendering the UI. a renderer is provided if you want to do any rendering.
-
removeComponent
final PolyUI removeComponent(Drawable drawable)
-
addComponent
final PolyUI addComponent(Drawable drawable)
-
addComponents
final PolyUI addComponents(Drawable drawables)
-
addComponents
final PolyUI addComponents(Collection<Drawable> drawables)
-
every
final PolyUI every(Long nanos, Integer repeats, Function0<Unit> func)
add a function that is called every nanos nanoseconds.
-
doWhile
final PolyUI doWhile(Long everyNanos, Function0<Boolean> condition, Function0<Unit> func)
Add a function that is called every everyNanos nanoseconds until condition returns true.
-
after
final PolyUI after(Long timeNanos, Function0<Unit> func)
Add a function that is called after timeNanos nanoseconds.
-
getComponentsIn
final ArrayList<Component> getComponentsIn(Float x, Float y, Float width, Float height)
Return all components in the given rectangle.
-
focus
final Boolean focus(Focusable focusable)
Sets the focus to the specified focusable element.
- Parameters:
focusable- the element to set focus on
-
getKeyName
final String getKeyName(Integer key)
Return the key name for the given key code.
-
debugPrint
final String debugPrint()
print this PolyUI instance's components and children in a list, like this:
PolyUI(800.0 x 800.0) with 0 components and 2 layouts: PixelLayout@5bcab519 [Draggable](20.0x570.0, 540.0 x 150.0) Text@6eebc39e(40.0x570.0, 520.0x32.0) Block@2f686d1f(40.0x600.0, 120.0x120.0) Block@7085bdee(220.0x600.0, 120.0x120.0) Image@6fd02e5(400.0x600.0, 120.0x120.0 (auto)) ... 2 more FlexLayout@73846619 [Scrollable](20.0x30.0, 693.73267 x 409.47168, buffered, needsRedraw) Block@32e6e9c3(20.0x30.0, 61.111263x42.21167) Block@5056dfcb(86.11127x30.0, 40.909004x76.32132) Block@6574b225(132.02026x30.0, 52.75415x52.59597) Block@2669b199(189.77441x30.0, 76.59671x45.275665)The string is also returned.
-
-
-
-