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 BooleanisOnMacpublic final static FontFamilydefaultFontspublic final static FontmonospaceFontpublic final static PolyImagedefaultImagepublic 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 FontmonospaceFontprivate Windowwindowprivate final BooleancanPauseRenderingprivate final BooleancanUseFramebuffersprivate final FloatpixelRatioprivate Stringclipboardprivate final Drawablemasterprivate final InputManagerinputManagerprivate final KeyBinderkeyBinderprivate final Translatortranslatorprivate final Clockclockprivate LayoutControllerlayoutControllerprivate final Booleandrewprivate final FloatmouseXprivate final FloatmouseYprivate final BooleanmouseDownprivate Cursorcursorprivate final Vec2sizeprivate final Vec2iSizeprivate final Longdeltaprivate final Debuggerdebuggerprivate final Rendererrenderer
-
Constructor Summary
Constructors Constructor Description PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Align masterAlignment, Colors colors, Float width, Float height)PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Align masterAlignment, Float width, Float height)PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Float width, Float height)PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, Float width, Float height)PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Float width, Float height)PolyUI(Component components, Renderer renderer, Settings settings, Float width, Float height)PolyUI(Component components, Renderer renderer, Float width, Float height)PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Align masterAlignment, Colors colors, Vec2 size)
-
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 FontgetMonospaceFont()Monospace font attached to this PolyUI instance. final UnitsetMonospaceFont(Font monospaceFont)Monospace font attached to this PolyUI instance. final WindowgetWindow()The window this polyUI was opened with. final UnitsetWindow(Window window)final BooleangetCanPauseRendering()final BooleangetCanUseFramebuffers()final FloatgetPixelRatio()final StringgetClipboard()final UnitsetClipboard(String clipboard)final DrawablegetMaster()This is the root layout of the UI. final InputManagergetInputManager()final KeyBindergetKeyBinder()final TranslatorgetTranslator()final ClockgetClock()final LayoutControllergetLayoutController()final UnitsetLayoutController(LayoutController layoutController)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 Vec2getInitialSize()final LonggetDelta()the time since the last frame, in nanoseconds. final DebuggergetDebugger()the debugger attached to this PolyUI instance. final RenderergetRenderer()final Unitresize(Float newWidth, Float newHeight, Boolean force)Resize this PolyUI instance. final Longrender()Perform a render cycle. final Clock.Executorevery(Long nanos, Integer repeats, Function0<Unit> func)add a function that is called every nanos nanoseconds. final UnitaddExecutor(Clock.Executor executor)final BooleanremoveExecutor(Clock.Executor executor)Remove a clock-based executor from this PolyUI instance. final Unitfocus(Inputtable focusable)Sets the focus to the specified focusable element. final Unitunfocus()final StringgetKeyName(Integer key)Return the key name for the given key code, or "Unknown" if the key is not mapped / no window is present. 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. final static Longtimed(Boolean log, String msg, Function0<Unit> block)Time the block and return how long it took, as well as logging with the msg if log is true.-
-
Constructor Detail
-
PolyUI
PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Align masterAlignment, Colors colors, Float width, Float height)
-
PolyUI
PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Align masterAlignment, Float width, Float height)
-
PolyUI
PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Float width, Float height)
-
PolyUI
PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, Float width, Float height)
-
PolyUI
PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Float width, Float height)
-
PolyUI
PolyUI(Component components, Renderer renderer, Settings settings, Float width, Float height)
-
PolyUI
PolyUI(Component components, Renderer renderer, Settings settings, InputManager inputManager, Translator translator, PolyColor backgroundColor, Align masterAlignment, Colors colors, Vec2 size)
-
-
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 fonts 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 fonts 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
-
getMonospaceFont
final Font getMonospaceFont()
Monospace font attached to this PolyUI instance. This is used for debugging and other purposes.
-
setMonospaceFont
final Unit setMonospaceFont(Font monospaceFont)
Monospace font attached to this PolyUI instance. This is used for debugging and other purposes.
-
getWindow
final Window getWindow()
The window this polyUI was opened with.
Since
v1.1.3, a window is not required to be set. If it is not set, the clipboard, cursor and render hooks will not work.- Since:
0.18.3
-
getCanPauseRendering
final Boolean getCanPauseRendering()
-
getCanUseFramebuffers
final Boolean getCanUseFramebuffers()
-
getPixelRatio
final Float getPixelRatio()
-
getClipboard
final String getClipboard()
-
setClipboard
final Unit setClipboard(String clipboard)
-
getMaster
final Drawable getMaster()
This is the root layout of the UI. It is the parent of all other layouts.
-
getInputManager
final InputManager getInputManager()
-
getKeyBinder
final KeyBinder getKeyBinder()
-
getTranslator
final Translator getTranslator()
-
getLayoutController
final LayoutController getLayoutController()
-
setLayoutController
final Unit setLayoutController(LayoutController layoutController)
-
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()
-
getInitialSize
final Vec2 getInitialSize()
-
getDelta
final Long getDelta()
the time since the last frame, in nanoseconds. It is used internally a lot for animations, etc.
-
getDebugger
final Debugger getDebugger()
the debugger attached to this PolyUI instance.
- Since:
1.1.7
-
getRenderer
final Renderer getRenderer()
-
resize
final Unit resize(Float newWidth, Float newHeight, Boolean force)
Resize this PolyUI instance.
-
render
final Long render()
Perform a render cycle.
- Returns:
(revised 1.1.7) the maximum amount of time that can be waited, as long as no events are dispatched, that can be waited before render needs to be called again.
-
every
final Clock.Executor every(Long nanos, Integer repeats, Function0<Unit> func)
add a function that is called every nanos nanoseconds.
Note that the requested time is not guaranteed to be accurate, especially on small values (less than 5ms). It will be executed at the end of the next frame. The executor will overshoot much more often that it will undershoot.
From testing, a requested time of 1 second is about 99.3% accurate.
(+7ms, -0.01ms)
-
addExecutor
final Unit addExecutor(Clock.Executor executor)
-
removeExecutor
final Boolean removeExecutor(Clock.Executor executor)
Remove a clock-based executor from this PolyUI instance.
- Returns:
trueif the executor was successfully removed.- Since:
1.0.5
-
focus
final Unit focus(Inputtable 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, or "Unknown" if the key is not mapped / no window is present.
-
cleanup
final Unit cleanup()
cleanup the polyUI instance. This will delete all resources, and render this instance unusable.
-
-
-
-