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.
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 final static BooleanisOnMacpublic static FontFamilydefaultFontspublic static FontmonospaceFontpublic static PolyImagedefaultImagepublic final static ByteINPUT_DISABLEDpublic final static ByteINPUT_NONEpublic final static ByteINPUT_HOVEREDpublic final static ByteINPUT_PRESSEDpublic final static ByteDANGERpublic final static ByteWARNINGpublic final static ByteSUCCESSprivate final Settingssettingsprivate Colorscolorsprivate FontFamilyfontsprivate Windowwindowprivate Stringclipboardprivate final Groupmasterprivate final EventManagereventManagerprivate final KeyBinderkeyBinderprivate final Translatortranslatorprivate Positionerpositionerprivate final Booleandrewprivate final FloatmouseXprivate final FloatmouseYprivate final BooleanmouseDownprivate Cursorcursorprivate final Vec2sizeprivate final Longdeltaprivate final Integerfpsprivate final FloatlongestFrameprivate final FloatshortestFrameprivate final FloatavgFrameprivate final Rendererrenderer
-
Constructor Summary
Constructors Constructor Description PolyUI(Renderer renderer, Settings settings, EventManager eventManager, Translator translator, Colors colors, Drawable drawables)PolyUI(Renderer renderer, Settings settings, EventManager eventManager, Translator translator, Drawable drawables)PolyUI(Renderer renderer, Settings settings, EventManager eventManager, Drawable drawables)PolyUI(Renderer renderer, Settings settings, 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)Colors attached to this PolyUI instance. final FontFamilygetFonts()Fonts attached to this PolyUI instance. final UnitsetFonts(FontFamily fonts)Fonts attached to this PolyUI instance. 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 GroupgetMaster()This is the root layout of the UI. final EventManagergetEventManager()final KeyBindergetKeyBinder()final TranslatorgetTranslator()final PositionergetPositioner()final UnitsetPositioner(Positioner positioner)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 Vec2getSize()final LonggetDelta()the time since the last frame, in nanoseconds. final IntegergetFps()final FloatgetLongestFrame()final FloatgetShortestFrame()final FloatgetAvgFrame()final RenderergetRenderer()final Unitresize(Float newWidth, Float 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 UnitforRemoval(LinkedList<Drawable> children, Integer index)Remove a drawable on the next frame where Drawable.canBeRemoved returns true. final UnitbeforeRender(Function1<Renderer, Boolean> func)Add a function to be executed just before a render cycle begins. final PolyUIevery(Long nanos, Integer repeats, Function0<Unit> func)add a function that is called every nanos nanoseconds. final Booleanfocus(Drawable 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. -
-
Constructor Detail
-
PolyUI
PolyUI(Renderer renderer, Settings settings, EventManager eventManager, Translator translator, Colors colors, Drawable drawables)
-
PolyUI
PolyUI(Renderer renderer, Settings settings, EventManager eventManager, Translator translator, Drawable drawables)
-
PolyUI
PolyUI(Renderer renderer, Settings settings, EventManager eventManager, Drawable drawables)
-
-
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.
- Since:
0.17.0
-
setColors
final Unit setColors(Colors colors)
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.
- Since:
0.17.0
-
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.
- Since:
0.22.0
-
setFonts
final Unit setFonts(FontFamily fonts)
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.
- Since:
0.22.0
-
getClipboard
final String getClipboard()
-
setClipboard
final Unit setClipboard(String clipboard)
-
getMaster
final Group 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()
-
getPositioner
final Positioner getPositioner()
-
setPositioner
final Unit setPositioner(Positioner positioner)
-
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.
-
getLongestFrame
final Float getLongestFrame()
-
getShortestFrame
final Float getShortestFrame()
-
getAvgFrame
final Float getAvgFrame()
-
getRenderer
final Renderer getRenderer()
-
resize
final Unit resize(Float newWidth, Float 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.
- Returns:
trueat any time to remove the hook.- Since:
0.18.5
-
forRemoval
final Unit forRemoval(LinkedList<Drawable> children, Integer index)
Remove a drawable on the next frame where Drawable.canBeRemoved returns true.
Once this function is called, it cannot be revoked. Calling this function twice with the same drawable will break.
-
beforeRender
final Unit beforeRender(Function1<Renderer, Boolean> func)
Add a function to be executed just before a render cycle begins.
Any modifications to the renderer at this point such as transformations will affect the rendering of the entire UI.
- Since:
0.24.3
-
every
final PolyUI every(Long nanos, Integer repeats, Function0<Unit> func)
add a function that is called every nanos nanoseconds.
-
focus
final Boolean focus(Drawable focusable)
Sets the focus to the specified focusable element.
- Parameters:
focusable- the element to set focus on- Returns:
true if focus was successfully set, false if the provided focusable is already focused
-
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.
-
-
-
-