use of com.spinyowl.legui.system.context.Context 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.Context 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();
}
use of com.spinyowl.legui.system.context.Context 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.Context in project legui by SpinyOwl.
the class ScrollBarMouseClickEventListener method updateViewport.
/**
* Used to update viewport.
*
* @param event event.
* @param scrollBar scrollbar.
* @param maxValue maximum scrollbar value.
* @param minValue minimum scrollbar value.
* @param newValue new scrollbar value.
*/
private void updateViewport(Event event, ScrollBar scrollBar, float maxValue, float minValue, float newValue) {
float valueToUse = newValue;
if (newValue > maxValue) {
valueToUse = maxValue;
} else if (newValue < minValue) {
valueToUse = minValue;
}
Context context = event.getContext();
Frame frame = event.getFrame();
float curValue = scrollBar.getCurValue();
EventProcessorProvider.getInstance().pushEvent(new ScrollBarChangeValueEvent<>(scrollBar, context, frame, curValue, valueToUse));
scrollBar.setCurValue(valueToUse);
}
use of com.spinyowl.legui.system.context.Context in project legui by SpinyOwl.
the class NvgBufferedImageRGBARenderer method initialize.
@Override
public void initialize() {
NvgImageReferenceManager manager = NvgImageReferenceManager.getInstance();
manager.putImageReferenceProvider(BufferedImageRGBA.class, (image, context) -> {
int imageRef = 0;
Function<BufferedImageRGBA, String> getPath = i -> "TI::RGBA::" + i.hashCode();
if (image != null) {
String path = getPath.apply(image);
try {
imageRef = manager.getImageCache().get(path, () -> {
int reference = nvgCreateImageRGBA(context, image.getWidth(), image.getHeight(), 0, image.getImageData());
manager.getImageAssociationMap().put(getPath.apply(image), reference);
return reference;
});
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return imageRef;
});
}
Aggregations