Search in sources :

Example 1 with GLFW_KEY_ESCAPE

use of org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE in project legui by SpinyOwl.

the class Demo method initialize.

private void initialize() {
    if (!GLFW.glfwInit()) {
        throw new RuntimeException("Can't initialize GLFW");
    }
    ChainErrorCallback errorCallback = new ChainErrorCallback();
    errorCallback.add(GLFWErrorCallback.createPrint(System.err));
    errorCallback.add(GLFWErrorCallback.createThrow());
    glfwSetErrorCallback(errorCallback);
    glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
    GLFWKeyCallbackI glfwKeyCallbackI = (w1, key, code, action, mods) -> running = !(key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE);
    GLFWWindowCloseCallbackI glfwWindowCloseCallbackI = w -> running = false;
    frame = new Frame(width, height);
    window = glfwCreateWindow(width, height, title, NULL, NULL);
    glfwSetWindowPos(window, 50, 50);
    glfwShowWindow(window);
    createGuiElements(frame);
    context = new Context(window);
    keeper = new DefaultCallbackKeeper();
    CallbackKeeper.registerCallbacks(window, keeper);
    keeper.getChainKeyCallback().add(glfwKeyCallbackI);
    keeper.getChainWindowCloseCallback().add(glfwWindowCloseCallbackI);
    systemEventProcessor = new SystemEventProcessorImpl();
    SystemEventProcessor.addDefaultCallbacks(keeper, systemEventProcessor);
    running = true;
}
Also used : ChainErrorCallback(com.spinyowl.cbchain.impl.ChainErrorCallback) GLFW.glfwDestroyWindow(org.lwjgl.glfw.GLFW.glfwDestroyWindow) GLFW.glfwSwapBuffers(org.lwjgl.glfw.GLFW.glfwSwapBuffers) GLFW_RELEASE(org.lwjgl.glfw.GLFW.GLFW_RELEASE) GL.setCapabilities(org.lwjgl.opengl.GL.setCapabilities) GLFW.glfwCreateWindow(org.lwjgl.glfw.GLFW.glfwCreateWindow) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFW.glfwSwapInterval(org.lwjgl.glfw.GLFW.glfwSwapInterval) GLFW.glfwWindowHint(org.lwjgl.glfw.GLFW.glfwWindowHint) ChainErrorCallback(com.spinyowl.cbchain.impl.ChainErrorCallback) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) EventProcessor(com.spinyowl.legui.listener.processor.EventProcessor) NULL(org.lwjgl.system.MemoryUtil.NULL) GL_COLOR_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT) Renderer(com.spinyowl.legui.system.renderer.Renderer) GLFW.glfwSetErrorCallback(org.lwjgl.glfw.GLFW.glfwSetErrorCallback) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) GLFW_TRUE(org.lwjgl.glfw.GLFW.GLFW_TRUE) GL.createCapabilities(org.lwjgl.opengl.GL.createCapabilities) GL11.glClear(org.lwjgl.opengl.GL11.glClear) GLFW.glfwShowWindow(org.lwjgl.glfw.GLFW.glfwShowWindow) LayoutManager(com.spinyowl.legui.system.layout.LayoutManager) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) GLFW_DOUBLEBUFFER(org.lwjgl.glfw.GLFW.GLFW_DOUBLEBUFFER) GL_STENCIL_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_STENCIL_BUFFER_BIT) Context(com.spinyowl.legui.system.context.Context) Frame(com.spinyowl.legui.component.Frame) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) GLFW.glfwTerminate(org.lwjgl.glfw.GLFW.glfwTerminate) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) GL11.glClearColor(org.lwjgl.opengl.GL11.glClearColor) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) GLFW.glfwSetWindowPos(org.lwjgl.glfw.GLFW.glfwSetWindowPos) GLFW(org.lwjgl.glfw.GLFW) GLFW.glfwWaitEvents(org.lwjgl.glfw.GLFW.glfwWaitEvents) AnimatorProvider(com.spinyowl.legui.animation.AnimatorProvider) GLFW.glfwMakeContextCurrent(org.lwjgl.glfw.GLFW.glfwMakeContextCurrent) TimeUnit(java.util.concurrent.TimeUnit) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor) EventProcessorProvider(com.spinyowl.legui.listener.processor.EventProcessorProvider) GLCapabilities(org.lwjgl.opengl.GLCapabilities) Vector2i(org.joml.Vector2i) GLFW_KEY_ESCAPE(org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE) Context(com.spinyowl.legui.system.context.Context) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) Frame(com.spinyowl.legui.component.Frame) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl)

Example 2 with GLFW_KEY_ESCAPE

use of org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE 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 3 with GLFW_KEY_ESCAPE

use of org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE in project legui by SpinyOwl.

the class MultipleWindowsMultipleThreadsExample method initialize.

private static void initialize() {
    if (!GLFW.glfwInit()) {
        throw new RuntimeException("Can't initialize GLFW");
    }
    glfwSetErrorCallback(GLFWErrorCallback.createPrint(System.err));
    GLFWKeyCallbackI glfwKeyCallbackI = (w1, key, code, action, mods) -> running = !(key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE);
    GLFWWindowCloseCallbackI glfwWindowCloseCallbackI = w -> running = false;
    Frame frame = new Frame(WIDTH, HEIGHT);
    createGuiElements(frame);
    for (int i = 0; i < WINDOW_COUNT; i++) {
        windows[i] = glfwCreateWindow(WIDTH, HEIGHT, "Multiple Windows Multiple Threads Example " + i, NULL, NULL);
        glfwSetWindowPos(windows[i], 50, 50 + (HEIGHT + 50) * i);
        glfwShowWindow(windows[i]);
        frames[i] = frame;
        contexts[i] = new Context(windows[i]);
        keepers[i] = new DefaultCallbackKeeper();
        CallbackKeeper.registerCallbacks(windows[i], keepers[i]);
        keepers[i].getChainKeyCallback().add(glfwKeyCallbackI);
        keepers[i].getChainWindowCloseCallback().add(glfwWindowCloseCallbackI);
        systemEventProcessors[i] = new SystemEventProcessorImpl();
        SystemEventProcessor.addDefaultCallbacks(keepers[i], systemEventProcessors[i]);
    }
    running = true;
}
Also used : GLFW.glfwDestroyWindow(org.lwjgl.glfw.GLFW.glfwDestroyWindow) GLFW_RELEASE(org.lwjgl.glfw.GLFW.GLFW_RELEASE) GL.setCapabilities(org.lwjgl.opengl.GL.setCapabilities) Button(com.spinyowl.legui.component.Button) GLFW.glfwCreateWindow(org.lwjgl.glfw.GLFW.glfwCreateWindow) GLFW.glfwSwapInterval(org.lwjgl.glfw.GLFW.glfwSwapInterval) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) NULL(org.lwjgl.system.MemoryUtil.NULL) Renderer(com.spinyowl.legui.system.renderer.Renderer) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) GLFW.glfwShowWindow(org.lwjgl.glfw.GLFW.glfwShowWindow) LayoutManager(com.spinyowl.legui.system.layout.LayoutManager) CursorEnterEventListener(com.spinyowl.legui.listener.CursorEnterEventListener) GL_STENCIL_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_STENCIL_BUFFER_BIT) CursorEnterEvent(com.spinyowl.legui.event.CursorEnterEvent) ColorConstants(com.spinyowl.legui.style.color.ColorConstants) Frame(com.spinyowl.legui.component.Frame) RadioButtonGroup(com.spinyowl.legui.component.RadioButtonGroup) GLFW.glfwTerminate(org.lwjgl.glfw.GLFW.glfwTerminate) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) Label(com.spinyowl.legui.component.Label) GLFW(org.lwjgl.glfw.GLFW) GLFW.glfwWaitEvents(org.lwjgl.glfw.GLFW.glfwWaitEvents) GLFW.glfwMakeContextCurrent(org.lwjgl.glfw.GLFW.glfwMakeContextCurrent) EventProcessorProvider(com.spinyowl.legui.listener.processor.EventProcessorProvider) List(java.util.List) Vector2i(org.joml.Vector2i) GLFW.glfwSwapBuffers(org.lwjgl.glfw.GLFW.glfwSwapBuffers) Component(com.spinyowl.legui.component.Component) MouseClickEventListener(com.spinyowl.legui.listener.MouseClickEventListener) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) RadioButton(com.spinyowl.legui.component.RadioButton) ArrayList(java.util.ArrayList) GL_COLOR_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT) GLFW.glfwSetErrorCallback(org.lwjgl.glfw.GLFW.glfwSetErrorCallback) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) GL.createCapabilities(org.lwjgl.opengl.GL.createCapabilities) GL11.glClear(org.lwjgl.opengl.GL11.glClear) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) Context(com.spinyowl.legui.system.context.Context) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) GL11.glClearColor(org.lwjgl.opengl.GL11.glClearColor) IOException(java.io.IOException) GLFW.glfwSetWindowPos(org.lwjgl.glfw.GLFW.glfwSetWindowPos) AnimatorProvider(com.spinyowl.legui.animation.AnimatorProvider) MouseClickEvent(com.spinyowl.legui.event.MouseClickEvent) TimeUnit(java.util.concurrent.TimeUnit) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor) GLCapabilities(org.lwjgl.opengl.GLCapabilities) SimpleLineBorder(com.spinyowl.legui.style.border.SimpleLineBorder) GLFW_KEY_ESCAPE(org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE) Context(com.spinyowl.legui.system.context.Context) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) Frame(com.spinyowl.legui.component.Frame) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl)

Example 4 with GLFW_KEY_ESCAPE

use of org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE in project legui by SpinyOwl.

the class MultipleWindowsExample method main.

public static void main(String[] args) {
    System.setProperty("joml.nounsafe", Boolean.TRUE.toString());
    System.setProperty("java.awt.headless", Boolean.TRUE.toString());
    if (!GLFW.glfwInit()) {
        throw new RuntimeException("Can't initialize GLFW");
    }
    glfwSetErrorCallback(GLFWErrorCallback.createPrint(System.err));
    int N = 3;
    long[] windows = new long[N];
    Renderer[] renderers = new NvgRenderer[N];
    Context[] contexts = new Context[N];
    Frame[] frames = new Frame[N];
    CallbackKeeper[] keepers = new DefaultCallbackKeeper[N];
    SystemEventProcessor[] systemEventProcessors = new SystemEventProcessor[N];
    GLFWKeyCallbackI glfwKeyCallbackI = (w1, key, code, action, mods) -> running = !(key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE);
    GLFWWindowCloseCallbackI glfwWindowCloseCallbackI = w -> running = false;
    for (int i = 0; i < N; i++) {
        windows[i] = glfwCreateWindow(WIDTH, HEIGHT, "Multiple Windows Example " + (i + 1), NULL, NULL);
        glfwShowWindow(windows[i]);
        glfwMakeContextCurrent(windows[i]);
        GL.createCapabilities();
        glfwSwapInterval(0);
        glfwSetWindowPos(windows[i], 50, 50 + (HEIGHT + 50) * i);
        // Renderer which will render our ui components.
        renderers[i] = new NvgRenderer();
        renderers[i].initialize();
        createGuiElements(frames[i] = new Frame(WIDTH, HEIGHT));
        contexts[i] = new Context(windows[i]);
        keepers[i] = new DefaultCallbackKeeper();
        CallbackKeeper.registerCallbacks(windows[i], keepers[i]);
        keepers[i].getChainKeyCallback().add(glfwKeyCallbackI);
        keepers[i].getChainWindowCloseCallback().add(glfwWindowCloseCallbackI);
        systemEventProcessors[i] = new SystemEventProcessorImpl();
        SystemEventProcessor.addDefaultCallbacks(keepers[i], systemEventProcessors[i]);
    }
    running = true;
    while (running) {
        for (int i = 0; i < N; i++) {
            glfwMakeContextCurrent(windows[i]);
            GL.getCapabilities();
            glfwSwapInterval(0);
            contexts[i].updateGlfwWindow();
            Vector2i windowSize = contexts[i].getFramebufferSize();
            glClearColor(1, 1, 1, 1);
            glViewport(0, 0, windowSize.x, windowSize.y);
            glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
            renderers[i].render(frames[i], contexts[i]);
            glfwPollEvents();
            glfwSwapBuffers(windows[i]);
            systemEventProcessors[i].processEvents(frames[i], contexts[i]);
            EventProcessorProvider.getInstance().processEvents();
            // When everything done we need to relayout components.
            LayoutManager.getInstance().layout(frames[i]);
        }
    }
    for (int i = 0; i < N; i++) {
        renderers[i].destroy();
        glfwDestroyWindow(windows[i]);
    }
    glfwTerminate();
}
Also used : Context(com.spinyowl.legui.system.context.Context) GLFW.glfwDestroyWindow(org.lwjgl.glfw.GLFW.glfwDestroyWindow) GLFW.glfwSwapBuffers(org.lwjgl.glfw.GLFW.glfwSwapBuffers) GLFW_RELEASE(org.lwjgl.glfw.GLFW.GLFW_RELEASE) Component(com.spinyowl.legui.component.Component) MouseClickEventListener(com.spinyowl.legui.listener.MouseClickEventListener) Button(com.spinyowl.legui.component.Button) GLFW.glfwCreateWindow(org.lwjgl.glfw.GLFW.glfwCreateWindow) GLFW.glfwPollEvents(org.lwjgl.glfw.GLFW.glfwPollEvents) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFW.glfwSwapInterval(org.lwjgl.glfw.GLFW.glfwSwapInterval) RadioButton(com.spinyowl.legui.component.RadioButton) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) NULL(org.lwjgl.system.MemoryUtil.NULL) ArrayList(java.util.ArrayList) GL_COLOR_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT) Renderer(com.spinyowl.legui.system.renderer.Renderer) GLFW.glfwSetErrorCallback(org.lwjgl.glfw.GLFW.glfwSetErrorCallback) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) GL11.glClear(org.lwjgl.opengl.GL11.glClear) GLFW.glfwShowWindow(org.lwjgl.glfw.GLFW.glfwShowWindow) LayoutManager(com.spinyowl.legui.system.layout.LayoutManager) CursorEnterEventListener(com.spinyowl.legui.listener.CursorEnterEventListener) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) GL_STENCIL_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_STENCIL_BUFFER_BIT) CursorEnterEvent(com.spinyowl.legui.event.CursorEnterEvent) Context(com.spinyowl.legui.system.context.Context) ColorConstants(com.spinyowl.legui.style.color.ColorConstants) Frame(com.spinyowl.legui.component.Frame) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) RadioButtonGroup(com.spinyowl.legui.component.RadioButtonGroup) GLFW.glfwTerminate(org.lwjgl.glfw.GLFW.glfwTerminate) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) GL11.glClearColor(org.lwjgl.opengl.GL11.glClearColor) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) Label(com.spinyowl.legui.component.Label) GLFW.glfwSetWindowPos(org.lwjgl.glfw.GLFW.glfwSetWindowPos) GLFW(org.lwjgl.glfw.GLFW) GLFW.glfwMakeContextCurrent(org.lwjgl.glfw.GLFW.glfwMakeContextCurrent) MouseClickEvent(com.spinyowl.legui.event.MouseClickEvent) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor) EventProcessorProvider(com.spinyowl.legui.listener.processor.EventProcessorProvider) List(java.util.List) Vector2i(org.joml.Vector2i) SimpleLineBorder(com.spinyowl.legui.style.border.SimpleLineBorder) GLFW_KEY_ESCAPE(org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE) GL(org.lwjgl.opengl.GL) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) Frame(com.spinyowl.legui.component.Frame) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) Renderer(com.spinyowl.legui.system.renderer.Renderer) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) Vector2i(org.joml.Vector2i) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor)

Example 5 with GLFW_KEY_ESCAPE

use of org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE in project legui by SpinyOwl.

the class SingleClassExample2 method main.

public static void main(String[] args) throws IOException {
    System.setProperty("joml.nounsafe", Boolean.TRUE.toString());
    System.setProperty("java.awt.headless", Boolean.TRUE.toString());
    if (!GLFW.glfwInit()) {
        throw new RuntimeException("Can't initialize GLFW");
    }
    long window = glfwCreateWindow(WIDTH, HEIGHT, "Legui", NULL, NULL);
    glfwShowWindow(window);
    glfwMakeContextCurrent(window);
    GL.createCapabilities();
    glfwSwapInterval(0);
    // Firstly we need to create frame component for window.
    Frame frame = new Frame(WIDTH, HEIGHT);
    // we can add elements here or on the fly
    // We need to create legui context which shared by renderer and event processor.
    // Also we need to pass event processor for ui events such as click on component, key typing and
    // etc.
    Context context = new Context(window);
    // We need to create callback keeper which will hold all of callbacks.
    // These callbacks will be used in initialization of system event processor
    // (will be added callbacks which will push system events to event queue and after that
    // processed by SystemEventProcessor)
    CallbackKeeper keeper = new DefaultCallbackKeeper();
    // register callbacks for window. Note: all previously binded callbacks will be unbinded.
    CallbackKeeper.registerCallbacks(window, keeper);
    GLFWKeyCallbackI glfwKeyCallbackI = (w1, key, code, action, mods) -> running = !(key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE);
    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, glfwKeyCallbackI);
    // glfwSetWindowCloseCallback(window, glfwWindowCloseCallbackI);
    // 
    // Right:
    keeper.getChainKeyCallback().add(glfwKeyCallbackI);
    keeper.getChainWindowCloseCallback().add(glfwWindowCloseCallbackI);
    // Event processor for system events. System events should be processed and translated to gui
    // events.
    SystemEventProcessor systemEventProcessor = new SystemEventProcessorImpl();
    SystemEventProcessor.addDefaultCallbacks(keeper, systemEventProcessor);
    // Also we need to create renderer provider
    // and create renderer which will render our ui components.
    Renderer renderer = new NvgRenderer();
    // 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.initialize();
    AtomicBoolean needToUpdate = new AtomicBoolean(true);
    keeper.getChainKeyCallback().add((w, key, scancode, action, mods) -> {
        if (key == GLFW_KEY_F1 && action == GLFW_RELEASE)
            needToUpdate.set(true);
    });
    // createUI(frame);
    while (running) {
        if (needToUpdate.getAndSet(false))
            createUI(frame);
        // 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);
        // render frame
        renderer.render(frame, context);
        // poll events to callbacks
        glfwPollEvents();
        glfwSwapBuffers(window);
        // Now we need to process events. Firstly we need to process system events.
        systemEventProcessor.processEvents(frame, context);
        // When system events are translated to GUI events we need to process them.
        // This event processor calls listeners added to ui components
        EventProcessorProvider.getInstance().processEvents();
        // When everything done we need to relayout components.
        LayoutManager.getInstance().layout(frame);
        // Run animations. Should be also called cause some components use animations for updating
        // state.
        AnimatorProvider.getAnimator().runAnimations();
    }
    // And when rendering is ended we need to destroy renderer
    renderer.destroy();
    glfwDestroyWindow(window);
    glfwTerminate();
}
Also used : Context(com.spinyowl.legui.system.context.Context) COLUMN(com.spinyowl.legui.style.flex.FlexStyle.FlexDirection.COLUMN) GLFW.glfwDestroyWindow(org.lwjgl.glfw.GLFW.glfwDestroyWindow) GLFW_RELEASE(org.lwjgl.glfw.GLFW.GLFW_RELEASE) Button(com.spinyowl.legui.component.Button) GLFW.glfwCreateWindow(org.lwjgl.glfw.GLFW.glfwCreateWindow) GLFW.glfwPollEvents(org.lwjgl.glfw.GLFW.glfwPollEvents) GLFW.glfwSwapInterval(org.lwjgl.glfw.GLFW.glfwSwapInterval) ROW(com.spinyowl.legui.style.flex.FlexStyle.FlexDirection.ROW) GL11.glViewport(org.lwjgl.opengl.GL11.glViewport) NULL(org.lwjgl.system.MemoryUtil.NULL) ABSOLUTE(com.spinyowl.legui.style.Style.PositionType.ABSOLUTE) Renderer(com.spinyowl.legui.system.renderer.Renderer) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) GLFW.glfwShowWindow(org.lwjgl.glfw.GLFW.glfwShowWindow) LayoutManager(com.spinyowl.legui.system.layout.LayoutManager) GL_STENCIL_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_STENCIL_BUFFER_BIT) ColorUtil(com.spinyowl.legui.style.color.ColorUtil) FLEX(com.spinyowl.legui.style.Style.DisplayType.FLEX) ColorConstants(com.spinyowl.legui.style.color.ColorConstants) Frame(com.spinyowl.legui.component.Frame) GLFW.glfwTerminate(org.lwjgl.glfw.GLFW.glfwTerminate) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) JustifyContent(com.spinyowl.legui.style.flex.FlexStyle.JustifyContent) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) Label(com.spinyowl.legui.component.Label) GLFW(org.lwjgl.glfw.GLFW) GLFW.glfwMakeContextCurrent(org.lwjgl.glfw.GLFW.glfwMakeContextCurrent) EventProcessorProvider(com.spinyowl.legui.listener.processor.EventProcessorProvider) Vector2i(org.joml.Vector2i) Vector2f(org.joml.Vector2f) AlignContent(com.spinyowl.legui.style.flex.FlexStyle.AlignContent) GLFW.glfwSwapBuffers(org.lwjgl.glfw.GLFW.glfwSwapBuffers) Component(com.spinyowl.legui.component.Component) Intersector(com.spinyowl.legui.intersection.Intersector) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GL_COLOR_BUFFER_BIT(org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT) RELATIVE(com.spinyowl.legui.style.Style.PositionType.RELATIVE) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) GL11.glClear(org.lwjgl.opengl.GL11.glClear) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) Context(com.spinyowl.legui.system.context.Context) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) FontRegistry(com.spinyowl.legui.style.font.FontRegistry) GL11.glClearColor(org.lwjgl.opengl.GL11.glClearColor) GLFW_KEY_F1(org.lwjgl.glfw.GLFW.GLFW_KEY_F1) IOException(java.io.IOException) AlignItems(com.spinyowl.legui.style.flex.FlexStyle.AlignItems) AnimatorProvider(com.spinyowl.legui.animation.AnimatorProvider) Panel(com.spinyowl.legui.component.Panel) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor) SimpleLineBorder(com.spinyowl.legui.style.border.SimpleLineBorder) Widget(com.spinyowl.legui.component.Widget) GLFW_KEY_ESCAPE(org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE) GL(org.lwjgl.opengl.GL) GLFWKeyCallbackI(org.lwjgl.glfw.GLFWKeyCallbackI) Frame(com.spinyowl.legui.component.Frame) CallbackKeeper(com.spinyowl.legui.system.context.CallbackKeeper) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GLFWWindowCloseCallbackI(org.lwjgl.glfw.GLFWWindowCloseCallbackI) DefaultCallbackKeeper(com.spinyowl.legui.system.context.DefaultCallbackKeeper) SystemEventProcessorImpl(com.spinyowl.legui.system.handler.processor.SystemEventProcessorImpl) Renderer(com.spinyowl.legui.system.renderer.Renderer) NvgRenderer(com.spinyowl.legui.system.renderer.nvg.NvgRenderer) Vector2i(org.joml.Vector2i) SystemEventProcessor(com.spinyowl.legui.system.handler.processor.SystemEventProcessor)

Aggregations

Frame (com.spinyowl.legui.component.Frame)9 Context (com.spinyowl.legui.system.context.Context)9 LayoutManager (com.spinyowl.legui.system.layout.LayoutManager)9 Renderer (com.spinyowl.legui.system.renderer.Renderer)9 Vector2i (org.joml.Vector2i)9 GLFW_KEY_ESCAPE (org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE)9 GLFW_RELEASE (org.lwjgl.glfw.GLFW.GLFW_RELEASE)9 GLFW.glfwCreateWindow (org.lwjgl.glfw.GLFW.glfwCreateWindow)9 GLFW.glfwDestroyWindow (org.lwjgl.glfw.GLFW.glfwDestroyWindow)9 GLFW.glfwMakeContextCurrent (org.lwjgl.glfw.GLFW.glfwMakeContextCurrent)9 GLFW.glfwShowWindow (org.lwjgl.glfw.GLFW.glfwShowWindow)9 GLFW.glfwSwapBuffers (org.lwjgl.glfw.GLFW.glfwSwapBuffers)9 GLFW.glfwSwapInterval (org.lwjgl.glfw.GLFW.glfwSwapInterval)9 GLFW.glfwTerminate (org.lwjgl.glfw.GLFW.glfwTerminate)9 GLFWKeyCallbackI (org.lwjgl.glfw.GLFWKeyCallbackI)9 GLFWWindowCloseCallbackI (org.lwjgl.glfw.GLFWWindowCloseCallbackI)9 NULL (org.lwjgl.system.MemoryUtil.NULL)9 AnimatorProvider (com.spinyowl.legui.animation.AnimatorProvider)8 EventProcessorProvider (com.spinyowl.legui.listener.processor.EventProcessorProvider)8 CallbackKeeper (com.spinyowl.legui.system.context.CallbackKeeper)8