use of com.spinyowl.legui.system.context.DefaultCallbackKeeper 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;
}
use of com.spinyowl.legui.system.context.DefaultCallbackKeeper 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;
}
use of com.spinyowl.legui.system.context.DefaultCallbackKeeper 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();
}
use of com.spinyowl.legui.system.context.DefaultCallbackKeeper 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();
}
use of com.spinyowl.legui.system.context.DefaultCallbackKeeper in project legui by SpinyOwl.
the class SingleClassExample 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, "Single Class Example", 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
createGuiElements(frame);
// 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();
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);
// 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();
update(context);
}
// And when rendering is ended we need to destroy renderer
renderer.destroy();
glfwDestroyWindow(window);
glfwTerminate();
}
Aggregations