Search in sources :

Example 1 with Component

use of com.spinyowl.legui.component.Component in project legui by SpinyOwl.

the class Example method main.

// private static String json =
// IOUtil.loadResourceAsString("com/spinyowl/legui/demo/json.json", 1024);
public static void main(String[] args) {
    System.setProperty("joml.nounsafe", Boolean.TRUE.toString());
    System.setProperty("java.awt.headless", Boolean.TRUE.toString());
    if (!glfwInit()) {
        throw new RuntimeException("Can't initialize GLFW");
    }
    // create glfw window
    long window = glfwCreateWindow(WIDTH, HEIGHT, "Example", NULL, NULL);
    // show window
    glfwShowWindow(window);
    // make window current on thread
    glfwMakeContextCurrent(window);
    GL.createCapabilities();
    glfwSwapInterval(0);
    // read monitors
    PointerBuffer pointerBuffer = glfwGetMonitors();
    int remaining = pointerBuffer.remaining();
    monitors = new long[remaining];
    for (int i = 0; i < remaining; i++) {
        monitors[i] = pointerBuffer.get(i);
    }
    // create LEGUI theme and set it as default
    // Themes.setDefaultTheme(new FlatColoredTheme(
    // rgba(255, 255, 255, 1), // backgroundColor
    // rgba(176, 190, 197, 1), // borderColor
    // rgba(176, 190, 197, 1), // sliderColor
    // rgba(100, 181, 246, 1), // strokeColor
    // rgba(194, 219, 245, 1), // allowColor
    // rgba(239, 154, 154, 1), // denyColor
    // ColorConstants.transparent(), // shadowColor
    // ColorConstants.darkGray(), // text color
    // FontRegistry.getDefaultFont(), // font
    // FlatColoredTheme.FONT_SIZE
    // ));
    // Firstly we need to create frame component for window.
    // new Frame(WIDTH, HEIGHT);
    Frame frame = new Frame(WIDTH, HEIGHT);
    createGuiElements(frame, WIDTH, HEIGHT);
    // also we can create frame for example just by unmarshal it
    // frame = GsonMarshalUtil.unmarshal(json);
    // frame.setSize(WIDTH, HEIGHT);
    // We need to create legui instance one for window
    // which hold all necessary library components
    // or if you want some customizations you can do it by yourself.
    DefaultInitializer initializer = new DefaultInitializer(window, frame);
    GLFWKeyCallbackI exitOnEscCallback = (w1, key, code, action, mods) -> running = !(key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE);
    GLFWKeyCallbackI toggleFullscreenCallback = (w1, key, code, action, mods) -> toggleFullscreen = (key == GLFW_KEY_F && action == GLFW_RELEASE && (mods & GLFW_MOD_CONTROL) != 0);
    GLFWWindowCloseCallbackI glfwWindowCloseCallbackI = w -> running = false;
    // if we want to create some callbacks for system events you should create and put them to
    // keeper
    // 
    // Wrong:
    // glfwSetKeyCallback(window, exitOnEscCallback);
    // glfwSetWindowCloseCallback(window, glfwWindowCloseCallbackI);
    // 
    // Right:
    initializer.getCallbackKeeper().getChainKeyCallback().add(exitOnEscCallback);
    initializer.getCallbackKeeper().getChainKeyCallback().add(toggleFullscreenCallback);
    initializer.getCallbackKeeper().getChainWindowCloseCallback().add(glfwWindowCloseCallbackI);
    // Initialization finished, so we can start render loop.
    running = true;
    // Everything can be done in one thread as well as in separated threads.
    // Here is one-thread example.
    // before render loop we need to initialize renderer
    Renderer renderer = initializer.getRenderer();
    Animator animator = AnimatorProvider.getAnimator();
    renderer.initialize();
    long time = System.currentTimeMillis();
    int updCntr = 0;
    context = initializer.getContext();
    // context.setDebugEnabled(true);
    while (running) {
        // Before rendering we need to update context with window size and window framebuffer size
        // {
        // int[] windowWidth = {0}, windowHeight = {0};
        // GLFW.glfwGetWindowSize(window, windowWidth, windowHeight);
        // int[] frameBufferWidth = {0}, frameBufferHeight = {0};
        // GLFW.glfwGetFramebufferSize(window, frameBufferWidth, frameBufferHeight);
        // int[] xpos = {0}, ypos = {0};
        // GLFW.glfwGetWindowPos(window, xpos, ypos);
        // double[] mx = {0}, my = {0};
        // GLFW.glfwGetCursorPos(window, mx, my);
        // 
        // context.update(windowWidth[0], windowHeight[0],
        // frameBufferWidth[0], frameBufferHeight[0],
        // xpos[0], ypos[0],
        // mx[0], my[0]
        // );
        // }
        // Also we can do it in one line
        context.updateGlfwWindow();
        Vector2i windowSize = context.getFramebufferSize();
        glClearColor(1, 1, 1, 1);
        // Set viewport size
        glViewport(0, 0, windowSize.x, windowSize.y);
        // Clear screen
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        // We need to relayout components.
        if (gui.getGenerateEventsByLayoutManager().isChecked()) {
            LayoutManager.getInstance().layout(frame, context);
        } else {
            LayoutManager.getInstance().layout(frame);
        }
        // render frame
        renderer.render(frame, context);
        // poll events to callbacks
        glfwPollEvents();
        glfwSwapBuffers(window);
        animator.runAnimations();
        // Now we need to handle events. Firstly we need to handle system events.
        // And we need to know to which frame they should be passed.
        initializer.getSystemEventProcessor().processEvents(frame, context);
        // When system events are translated to GUI events we need to handle them.
        // This event processor calls listeners added to ui components
        initializer.getGuiEventProcessor().processEvents();
        // check toggle fullscreen flag and execute.
        if (toggleFullscreen) {
            if (fullscreen) {
                glfwSetWindowMonitor(window, NULL, 100, 100, WIDTH, HEIGHT, GLFW_DONT_CARE);
            } else {
                GLFWVidMode glfwVidMode = glfwGetVideoMode(monitors[0]);
                glfwSetWindowMonitor(window, monitors[0], 0, 0, glfwVidMode.width(), glfwVidMode.height(), glfwVidMode.refreshRate());
            }
            fullscreen = !fullscreen;
            toggleFullscreen = false;
        }
        update();
        updCntr++;
        if (System.currentTimeMillis() >= time + 1000) {
            time += 1000;
            glfwSetWindowTitle(window, "LEGUI Example. Updates per second: " + updCntr);
            updCntr = 0;
        }
    }
    // And when rendering is ended we need to destroy renderer
    renderer.destroy();
    glfwDestroyWindow(window);
    glfwTerminate();
}
Also used : GLFW.glfwDestroyWindow(org.lwjgl.glfw.GLFW.glfwDestroyWindow) GLFW.glfwSwapBuffers(org.lwjgl.glfw.GLFW.glfwSwapBuffers) DefaultInitializer(com.spinyowl.legui.DefaultInitializer) ColorUtil.rgba(com.spinyowl.legui.style.color.ColorUtil.rgba) GLFW_RELEASE(org.lwjgl.glfw.GLFW.GLFW_RELEASE) Component(com.spinyowl.legui.component.Component) GLFW.glfwCreateWindow(org.lwjgl.glfw.GLFW.glfwCreateWindow) GLFW.glfwSetWindowMonitor(org.lwjgl.glfw.GLFW.glfwSetWindowMonitor) FlatColoredTheme(com.spinyowl.legui.theme.colored.FlatColoredTheme) GLFW.glfwPollEvents(org.lwjgl.glfw.GLFW.glfwPollEvents) GLFW.glfwSetWindowTitle(org.lwjgl.glfw.GLFW.glfwSetWindowTitle) GLFW.glfwSwapInterval(org.lwjgl.glfw.GLFW.glfwSwapInterval) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) Themes(com.spinyowl.legui.theme.Themes) NULL(org.lwjgl.system.MemoryUtil.NULL) WindowSizeEventListener(com.spinyowl.legui.listener.WindowSizeEventListener) GL_COLOR_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT) Animator(com.spinyowl.legui.animation.Animator) Renderer(com.spinyowl.legui.system.renderer.Renderer) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) GL11.glClear(org.lwjgl.opengl.GL11.glClear) GLFW.glfwShowWindow(org.lwjgl.glfw.GLFW.glfwShowWindow) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode) LayoutManager(com.spinyowl.legui.system.layout.LayoutManager) GLFW_DONT_CARE(org.lwjgl.glfw.GLFW.GLFW_DONT_CARE) GL_STENCIL_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_STENCIL_BUFFER_BIT) Context(com.spinyowl.legui.system.context.Context) PositionType(com.spinyowl.legui.style.Style.PositionType) ColorConstants(com.spinyowl.legui.style.color.ColorConstants) Frame(com.spinyowl.legui.component.Frame) FontRegistry(com.spinyowl.legui.style.font.FontRegistry) GLFW.glfwTerminate(org.lwjgl.glfw.GLFW.glfwTerminate) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) GL11.glClearColor(org.lwjgl.opengl.GL11.glClearColor) WindowSizeEvent(com.spinyowl.legui.event.WindowSizeEvent) GLFW_MOD_CONTROL(org.lwjgl.glfw.GLFW.GLFW_MOD_CONTROL) PointerBuffer(org.lwjgl.PointerBuffer) AnimatorProvider(com.spinyowl.legui.animation.AnimatorProvider) GLFW.glfwMakeContextCurrent(org.lwjgl.glfw.GLFW.glfwMakeContextCurrent) GLFW.glfwGetMonitors(org.lwjgl.glfw.GLFW.glfwGetMonitors) DisplayType(com.spinyowl.legui.style.Style.DisplayType) Vector2i(org.joml.Vector2i) GLFW_KEY_F(org.lwjgl.glfw.GLFW.GLFW_KEY_F) GLFW_KEY_ESCAPE(org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE) GLFW.glfwInit(org.lwjgl.glfw.GLFW.glfwInit) GLFW.glfwGetVideoMode(org.lwjgl.glfw.GLFW.glfwGetVideoMode) GL(org.lwjgl.opengl.GL) DefaultInitializer(com.spinyowl.legui.DefaultInitializer) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) Frame(com.spinyowl.legui.component.Frame) PointerBuffer(org.lwjgl.PointerBuffer) Animator(com.spinyowl.legui.animation.Animator) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) Renderer(com.spinyowl.legui.system.renderer.Renderer) Vector2i(org.joml.Vector2i) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 2 with Component

use of com.spinyowl.legui.component.Component in project legui by SpinyOwl.

the class Example method update.

private static void update() {
    if (context != null) {
        Component mouseTargetGui = context.getMouseTargetGui();
        gui.getMouseTargetLabel().getTextState().setText("-> " + (mouseTargetGui == null ? null : mouseTargetGui.getClass().getSimpleName()));
        Component focusedGui = context.getFocusedGui();
        gui.getFocusedGuiLabel().getTextState().setText("-> " + (focusedGui == null ? null : focusedGui.getClass().getSimpleName()));
    }
}
Also used : Component(com.spinyowl.legui.component.Component)

Example 3 with Component

use of com.spinyowl.legui.component.Component in project legui by SpinyOwl.

the class MultipleWindowsMultipleThreadsExample method generateOnFly.

private static List<Component> generateOnFly() {
    List<Component> list = new ArrayList<>();
    Label label = new Label(20, 60, 200, 20);
    label.getTextState().setText("Generated on fly label");
    label.getStyle().setTextColor(ColorConstants.red());
    RadioButtonGroup group = new RadioButtonGroup();
    RadioButton radioButtonFirst = new RadioButton("First", 20, 90, 200, 20);
    RadioButton radioButtonSecond = new RadioButton("Second", 20, 110, 200, 20);
    radioButtonFirst.setRadioButtonGroup(group);
    radioButtonSecond.setRadioButtonGroup(group);
    list.add(label);
    list.add(radioButtonFirst);
    list.add(radioButtonSecond);
    return list;
}
Also used : ArrayList(java.util.ArrayList) Label(com.spinyowl.legui.component.Label) RadioButtonGroup(com.spinyowl.legui.component.RadioButtonGroup) RadioButton(com.spinyowl.legui.component.RadioButton) Component(com.spinyowl.legui.component.Component)

Example 4 with Component

use of com.spinyowl.legui.component.Component in project legui by SpinyOwl.

the class SingleClassExample method generateOnFly.

private static List<Component> generateOnFly() {
    List<Component> list = new ArrayList<>();
    Widget widget = new Widget(10, 50, 380, 240);
    widget.getContainer().getStyle().setDisplay(FLEX);
    widget.setDraggable(false);
    TabbedPanel tabbedPanel = new TabbedPanel();
    tabbedPanel.getStyle().setPosition(PositionType.RELATIVE);
    tabbedPanel.getStyle().getFlexStyle().setFlexGrow(1);
    tabbedPanel.getStyle().getFlexStyle().setFlexShrink(1);
    tabbedPanel.getStyle().setMargin(10F);
    tabbedPanel.getStyle().setBorder(new SimpleLineBorder(ColorConstants.black(), 1F));
    widget.getContainer().add(tabbedPanel);
    Tab tab1 = new Tab("Planes", new Label("Show all planes available"));
    Tab tab2 = new Tab("Cars", new Label("Show all cars available"));
    Tab tab3 = new Tab("Boats", new Label("Show all boats available"));
    tabbedPanel.addTab(tab1);
    tabbedPanel.addTab(tab2);
    tabbedPanel.addTab(tab3);
    AtomicInteger tabIdx = new AtomicInteger();
    Button addTabButton = new Button("Add tab", 180, 10, 70, 30);
    addTabButton.getListenerMap().addListener(MouseClickEvent.class, event -> {
        if (MouseClickAction.CLICK.equals(event.getAction())) {
            String tabName = "Tab #" + tabIdx.incrementAndGet();
            Component tabComponent = new Panel();
            tabComponent.getStyle().getBackground().setColor(ColorUtil.randomColor());
            tabComponent.getStyle().setPadding(10F, 20F);
            tabbedPanel.addTab(new Tab(tabName, tabComponent));
        }
    });
    Button removeTabButton = new Button("Remove tab", 260, 10, 70, 30);
    removeTabButton.getListenerMap().addListener(MouseClickEvent.class, event -> {
        if (MouseClickAction.CLICK.equals(event.getAction()) && tabbedPanel.tabCount() > 0) {
            tabbedPanel.removeTab(tabbedPanel.getCurrentTab());
        }
    });
    Button changeStripPosition = new Button("Switch", 340, 10, 50, 30);
    changeStripPosition.getListenerMap().addListener(MouseClickEvent.class, event -> {
        if (MouseClickAction.CLICK.equals(event.getAction())) {
            TabStripPosition current = tabbedPanel.getTabStripPosition();
            if (TOP == current) {
                tabbedPanel.setTabStripPosition(LEFT);
            } else if (LEFT == current) {
                tabbedPanel.setTabStripPosition(BOTTOM);
            } else if (BOTTOM == current) {
                tabbedPanel.setTabStripPosition(RIGHT);
            } else {
                tabbedPanel.setTabStripPosition(TOP);
            }
        }
    });
    widget.getTitle().getListenerMap().addListener(KeyEvent.class, event -> {
        if (event.getAction() == GLFW_RELEASE) {
            if (event.getKey() == GLFW_KEY_1) {
                tabbedPanel.setTabWidth(30);
            } else if (event.getKey() == GLFW_KEY_2) {
                tabbedPanel.setTabHeight(120);
            } else if (event.getKey() == GLFW_KEY_3) {
                tabbedPanel.setTabStripPosition(LEFT);
            } else if (event.getKey() == GLFW_KEY_4) {
                tabbedPanel.setTabWidth(120);
            } else if (event.getKey() == GLFW_KEY_5) {
                tabbedPanel.setTabHeight(30);
            } else if (event.getKey() == GLFW_KEY_6) {
                tabbedPanel.setTabStripPosition(TOP);
            }
        }
    });
    list.add(addTabButton);
    list.add(removeTabButton);
    list.add(changeStripPosition);
    list.add(widget);
    return list;
}
Also used : ArrayList(java.util.ArrayList) Widget(com.spinyowl.legui.component.Widget) Label(com.spinyowl.legui.component.Label) TabbedPanel(com.spinyowl.legui.component.TabbedPanel) SimpleLineBorder(com.spinyowl.legui.style.border.SimpleLineBorder) TabbedPanel(com.spinyowl.legui.component.TabbedPanel) Panel(com.spinyowl.legui.component.Panel) Tab(com.spinyowl.legui.component.TabbedPanel.Tab) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Button(com.spinyowl.legui.component.Button) TabStripPosition(com.spinyowl.legui.component.TabbedPanel.TabStripPosition) Component(com.spinyowl.legui.component.Component)

Example 5 with Component

use of com.spinyowl.legui.component.Component in project legui by SpinyOwl.

the class WidgetTreeExample method checkAndCollapse.

private void checkAndCollapse(Widget root, Widget widget) {
    Component parent = widget.getParent();
    if (parent != null) {
        // collapse sibling widgets
        for (Component sibling : parent.getChildComponents()) {
            if (sibling instanceof Widget && sibling != widget) {
                Widget siblingWidget = (Widget) sibling;
                collapseChildWidgets(siblingWidget);
                siblingWidget.setMinimized(true);
            }
        }
        Component pp = parent.getParent();
        if (pp instanceof Widget) {
            Widget parentWidget = (Widget) pp;
            if (parentWidget != root) {
                checkAndCollapse(root, parentWidget);
            }
        }
    }
}
Also used : Widget(com.spinyowl.legui.component.Widget) Component(com.spinyowl.legui.component.Component)

Aggregations

Component (com.spinyowl.legui.component.Component)56 ArrayList (java.util.ArrayList)13 Label (com.spinyowl.legui.component.Label)10 Button (com.spinyowl.legui.component.Button)8 Frame (com.spinyowl.legui.component.Frame)8 SimpleLineBorder (com.spinyowl.legui.style.border.SimpleLineBorder)8 ColorConstants (com.spinyowl.legui.style.color.ColorConstants)7 AnimatorProvider (com.spinyowl.legui.animation.AnimatorProvider)6 RadioButton (com.spinyowl.legui.component.RadioButton)6 RadioButtonGroup (com.spinyowl.legui.component.RadioButtonGroup)6 CursorEnterEvent (com.spinyowl.legui.event.CursorEnterEvent)6 EventProcessorProvider (com.spinyowl.legui.listener.processor.EventProcessorProvider)6 CallbackKeeper (com.spinyowl.legui.system.context.CallbackKeeper)6 Context (com.spinyowl.legui.system.context.Context)6 LayoutManager (com.spinyowl.legui.system.layout.LayoutManager)6 Renderer (com.spinyowl.legui.system.renderer.Renderer)6 Vector2i (org.joml.Vector2i)6 GLFW (org.lwjgl.glfw.GLFW)6 GLFW_KEY_ESCAPE (org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE)6 GLFW_RELEASE (org.lwjgl.glfw.GLFW.GLFW_RELEASE)6