Advertisement
hnOsmium0001

JsonUI Java builder example

Oct 9th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import static jsonui.layout.properties.Length.*; // px(), fr(), auto(), etc.
  2.  
  3. public class Example {
  4.   public static void main(String[] args) {
  5.     UIBuilder bd = new UIBuilder();
  6.     UIScreen ui = bd
  7.       .begWindow("PrimaryWindow")
  8.         // Window child widget handling is different from panels
  9.         // all widgets are positioned by coordinate
  10.         .addChild(bd.builder(GridPanel.BUILDER)
  11.           // Conventinally we put a widget that covers the whole window as the "root"
  12.           .fillWindow()
  13.           .columns(auto(), px(200))
  14.           .rows(auto())
  15.           .gridGap(px(4))
  16.           .areas(
  17.             "Editor", "Toolbox"
  18.           )
  19.           // Using custom widget implementation
  20.           // #child(String, IWidget)
  21.           .child("Editor", new CADEditor())
  22.           // Using widget builder provided by lib
  23.           // #child(String, WidgetBuilder)
  24.           .child("Toolbox", bd.builder(StrictTablePanel.BUILDER)
  25.             .growDirection(Side.DOWN)
  26.             .overflowDirection(Side.RIGHT)
  27.             .gap(px(4))
  28.             // Native builder helper to execute a code block on stage
  29.             // Stage.INIT executes the code right after build; note that the widget is invalid which means changing subtree is not allowed here
  30.             // Stage.ATTACH executes the code after the widget became valid
  31.             // Stage.INITIAL_ATTACH executes the code after the widget became valid; this is conventionally used for modifying subtree based on data
  32.             // Stage.UPDATE executes the code on each clientTick
  33.             // Stage.RENDER executes the code on each render call
  34.             .exec(Stage.INITIAL_ATTACH, self -> {
  35.               // 'self' is the widget
  36.               for(ToolboxOption option : ToolboxOption.OPTIONS.values()) {
  37.                 self.child(option.getName(), option);
  38.               }
  39.             })
  40.           )
  41.         )
  42.         // Window doesn't support length units (yet)
  43.         .size(
  44.           w -> (int) (screenWidth() * (2F / 3F)),
  45.           w -> (int) (screenHeight() * (3F / 5F))
  46.         )
  47.         .position(
  48.           w -> Alignment.xForCenter(screenWidth(), w.getWidth()),
  49.           w -> Alignment.yForCenter(screenHeight(), w.getHeight())
  50.         )
  51.       .endWindow("PrimaryWindow")
  52.       .start("PrimaryWindow")
  53.       .allowJEI()
  54.       .allowFTBCoreSidebarButtons()
  55.       // Finishes building and return a GUI object
  56.       .finish();
  57.     Minecraft.getInstance().displayScreen(ui);
  58.   }
  59. }
  60.  
  61. public class CADEditor extends GridPanel<CADNode> {
  62.   // ...
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement