Skip to content

Commit

Permalink
updated events, event handlers, window class, demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpinyOwl committed Nov 28, 2018
1 parent c5c23f5 commit 1c7dd27
Show file tree
Hide file tree
Showing 49 changed files with 428 additions and 268 deletions.
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemCharEvent implements SystemEvent {
public class SystemCharEvent extends SystemEvent {

public final long window;
public final int codePoint;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemCharModsEvent implements SystemEvent {
public class SystemCharModsEvent extends SystemEvent {

public final long window;
public final int codePoint;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemCursorEnterEvent implements SystemEvent {
public class SystemCursorEnterEvent extends SystemEvent {

public final long window;
public final boolean entered;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemCursorPosEvent implements SystemEvent {
public class SystemCursorPosEvent extends SystemEvent {

public final long window;
public final double posX;
Expand All @@ -15,8 +15,8 @@ public SystemCursorPosEvent(long window, double posX, double posY) {
this.window = window;
this.posX = posX;
this.posY = posY;
fx = (float) posX;
fy = (float) posY;
this.fx = (float) posX;
this.fy = (float) posY;
}

@Override
Expand Down
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemDropEvent implements SystemEvent {
public class SystemDropEvent extends SystemEvent {

public final long window;
public final String[] strings;
Expand Down
@@ -1,8 +1,24 @@
package com.spinyowl.spinygui.backend.core.event;

import com.spinyowl.spinygui.backend.core.event.handler.SystemEventHandler;
import com.spinyowl.spinygui.backend.core.event.handler.SystemEventHandlers;

/**
* Marker interface that defines tree of system events.
*/
public interface SystemEvent {
public abstract class SystemEvent {

private final SystemEventHandler eventHandler;

public SystemEvent(SystemEventHandler systemEventHandler) {
this.eventHandler = systemEventHandler;
}

public SystemEvent() {
this.eventHandler = SystemEventHandlers.getHandler(getClass());
}

public SystemEventHandler getEventHandler() {
return eventHandler;
}
}
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemFramebufferSizeEvent implements SystemEvent {
public class SystemFramebufferSizeEvent extends SystemEvent {

public final long window;
public final int width;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemKeyEvent implements SystemEvent {
public class SystemKeyEvent extends SystemEvent {

public final long window;
public final int key;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemMouseClickEvent implements SystemEvent {
public class SystemMouseClickEvent extends SystemEvent {

public final long window;
public final int button;
Expand Down
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class SystemScrollEvent implements SystemEvent {
public class SystemScrollEvent extends SystemEvent {

public final long window;
public final double offsetX;
Expand Down
@@ -1,12 +1,14 @@
package com.spinyowl.spinygui.backend.core.event;

import com.spinyowl.spinygui.backend.core.event.handler.SystemEventHandlers;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowCloseEvent implements SystemEvent {
public class SystemWindowCloseEvent extends SystemEvent {

public final long window;

public SystemWindowCloseEvent(long window) {
super(SystemEventHandlers.getSystemWindowCloseEventHandler());
this.window = window;
}

Expand Down
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowContentScaleEvent implements SystemEvent {
public class SystemWindowContentScaleEvent extends SystemEvent {
public final long window;
public final float scaleX;
public final float scaleY;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowFocusEvent implements SystemEvent {
public class SystemWindowFocusEvent extends SystemEvent {

public final long window;
public final boolean focused;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowIconifyEvent implements SystemEvent {
public class SystemWindowIconifyEvent extends SystemEvent {

public final long window;
public final boolean iconified;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowMaximizeEvent implements SystemEvent {
public class SystemWindowMaximizeEvent extends SystemEvent {
public final long window;
public final boolean maximized;

Expand Down
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowPosEvent implements SystemEvent {
public class SystemWindowPosEvent extends SystemEvent {

public final long window;
public final int posX;
Expand Down
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowRefreshEvent implements SystemEvent {
public class SystemWindowRefreshEvent extends SystemEvent {

public final long window;

Expand Down
Expand Up @@ -2,7 +2,7 @@

import org.apache.commons.lang3.builder.ToStringBuilder;

public class SystemWindowSizeEvent implements SystemEvent {
public class SystemWindowSizeEvent extends SystemEvent {

public final long window;
public final int width;
Expand Down
@@ -0,0 +1,13 @@
package com.spinyowl.spinygui.backend.core.event.handler;

import com.spinyowl.spinygui.backend.core.event.SystemCursorPosEvent;
import com.spinyowl.spinygui.core.system.service.ServiceHolder;

public class SystemCursorPosEventHandler implements SystemEventHandler<SystemCursorPosEvent> {
@Override
public void handle(SystemCursorPosEvent event) {
// ServiceHolder.


}
}
@@ -0,0 +1,44 @@
package com.spinyowl.spinygui.backend.core.event.handler;

import com.spinyowl.spinygui.backend.core.event.SystemEvent;
import com.spinyowl.spinygui.backend.core.event.SystemWindowCloseEvent;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class SystemEventHandlers {

public static final SystemEventHandler<SystemWindowCloseEvent> DEFAULT_SYSTEM_WINDOW_CLOSE_EVENT_HANDLER = new SystemWindowCloseEventHandler();
private static SystemEventHandler<SystemWindowCloseEvent> systemWindowCloseEventHandler = DEFAULT_SYSTEM_WINDOW_CLOSE_EVENT_HANDLER;

private static Map<Class<? extends SystemEvent>, SystemEventHandler> eventHandlerMap = new ConcurrentHashMap<>();

static {
setHandler(SystemWindowCloseEvent.class, systemWindowCloseEventHandler);
}

private SystemEventHandlers() {
}

public static <T extends SystemEvent, H extends SystemEventHandler<T>> void setHandler(Class<T> clazz, H handler) {
eventHandlerMap.put(clazz, handler);
}

public static <T extends SystemEvent> SystemEventHandler<T> getHandler(Class<T> clazz) {
return (SystemEventHandler<T>) eventHandlerMap.get(clazz);
}

public static SystemEventHandler<SystemWindowCloseEvent> getSystemWindowCloseEventHandler() {
return systemWindowCloseEventHandler;
}

public static void setSystemWindowCloseEventHandler(SystemEventHandler<SystemWindowCloseEvent> systemWindowCloseEventHandler) {
if (systemWindowCloseEventHandler != null) {
SystemEventHandlers.systemWindowCloseEventHandler = systemWindowCloseEventHandler;
} else {
SystemEventHandlers.systemWindowCloseEventHandler = DEFAULT_SYSTEM_WINDOW_CLOSE_EVENT_HANDLER;
}
setHandler(SystemWindowCloseEvent.class, SystemEventHandlers.systemWindowCloseEventHandler);
}

}
Expand Up @@ -3,7 +3,7 @@
import com.spinyowl.spinygui.backend.core.event.SystemWindowCloseEvent;
import com.spinyowl.spinygui.core.event.WindowCloseEvent;
import com.spinyowl.spinygui.core.event.processor.EventProcessor;
import com.spinyowl.spinygui.core.service.ServiceHolder;
import com.spinyowl.spinygui.core.system.service.ServiceHolder;

public class SystemWindowCloseEventHandler implements SystemEventHandler<SystemWindowCloseEvent> {
@Override
Expand Down
@@ -0,0 +1,89 @@
package com.spinyowl.spinygui.backend.core.event.handler.util;

import com.spinyowl.spinygui.core.component.base.Component;
import com.spinyowl.spinygui.core.component.base.Container;

import java.util.ArrayList;
import java.util.List;

public final class SehUtil {

private SehUtil() {
}

/**
* Used to find target component for provided layer and vector. Target means top component which intersected by provided point(vector).
*
* @param container root container to search.
* @param x x point coordinates to search.
* @param y y point coordinates to search.
* @return top component from layer intersected by vector.
*/
public static Component getTargetComponent(Container container, final float x, final float y) {
Component target = container;
List<Component> childComponents = container.getChildComponents();
for (Component child : childComponents) {
target = recursiveTargetComponentSearch(child, target, x, y);
}
return target;
}

/**
* Used to search target component (under point) in component. Target means top component which intersected by provided point(vector).
*
* @param component source component to search target.
* @param target current target.
* @param x x point coordinates to search.
* @param y y point coordinates to search.
* @return the top visible component under point.
*/
private static Component recursiveTargetComponentSearch(Component component, Component target, final float x, final float y) {
Component newtarget = target;
if (isaTarget(component, x, y)) {
newtarget = component;
List<Component> childComponents = component.getChildComponents();
for (Component child : childComponents) {
newtarget = recursiveTargetComponentSearch(child, newtarget, x, y);
}
}
return newtarget;
}


/**
* Used to search all components (under point) in component.
*
* @param container root container to search.
* @param x x point coordinates to search.
* @param y y point coordinates to search.
* @return all top visible components in layer under point(vector).
*/
public static List<Component> getTargetComponentList(Container container, final float x, final float y) {
List<Component> targetList = new ArrayList<>();
recursiveTargetComponentListSearch(container, targetList, x, y);
return targetList;
}


/**
* Used to search all components (under point) in component. New located target component will be added to target list.
*
* @param component source component to search target.
* @param targetList current target list.
* @param x x point coordinates to search.
* @param y y point coordinates to search.
*/
public static void recursiveTargetComponentListSearch(Component component, List<Component> targetList, final float x, final float y) {
if (isaTarget(component, x, y)) {
targetList.add(component);
List<Component> childComponents = component.getChildComponents();
for (Component child : childComponents) {
recursiveTargetComponentListSearch(child, targetList, x, y);
}
}
}

private static boolean isaTarget(Component component, float x, float y) {
return component.isVisible() && component.getIntersection().intersects(component, x, y);
}
}
Expand Up @@ -14,11 +14,6 @@
public class DefaultSystemEventProcessor extends SystemEventProcessor {

private Queue<SystemEvent> eventQueue = new LinkedBlockingQueue<>();
private Map<Class<? extends SystemEvent>, SystemEventHandler<? extends SystemEvent>> eventHandlerMap = new ConcurrentHashMap<>();

{
setHandler(SystemWindowCloseEvent.class, new SystemWindowCloseEventHandler());
}

@Override
public void pushEvent(SystemEvent event) {
Expand All @@ -36,18 +31,12 @@ public void processEvents() {


private void processEvent(SystemEvent event) {
SystemEventHandler handler = getHandler(event.getClass());
SystemEventHandler handler = event.getEventHandler();
if (handler != null) {
System.out.println("PROCESS EVENT: " + event);
handler.handle(event);
}
}

public <T extends SystemEvent, H extends SystemEventHandler<T>> void setHandler(Class<T> clazz, H handler) {
eventHandlerMap.put(clazz, handler);
}

public <T extends SystemEvent, H extends SystemEventHandler<T>> H getHandler(Class<T> clazz) {
return (H) eventHandlerMap.get(clazz);
}
}
Expand Up @@ -2,7 +2,6 @@

import com.spinyowl.spinygui.backend.core.event.*;
import com.spinyowl.spinygui.backend.core.event.processor.SystemEventProcessor;
import com.spinyowl.spinygui.core.service.ServiceHolder;
import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.*;

Expand Down

0 comments on commit 1c7dd27

Please sign in to comment.