added key events,

moved create thread in window constructor,
added point2 hashcode
added gridpanel without merge
This commit is contained in:
Macocian Adrian Radu
2020-04-10 17:13:19 +03:00
parent eb4ce47a5c
commit 2e4d83085a
11 changed files with 234 additions and 33 deletions

View File

@@ -0,0 +1,129 @@
package guiTree.Components;
import guiTree.Visual;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;
import java.util.List;
import guiTree.Helper.Point2;
public class GridPanel extends Visual {
private int columnCount;
private int rowsCount;
private Map<Integer, List<Visual>> childrenCols;
private Map<Integer, List<Visual>> childrenRows;
private Map<Integer, Integer> rowSizes;
private Map<Integer, Integer> columnSizes;
private Map<Point2<Integer>, Integer> rowPadding;
private Map<Point2<Integer>, Integer> columnPadding;
public GridPanel(){
super();
childrenCols = new TreeMap<>();
childrenRows = new TreeMap<>();
rowsCount = 0;
columnCount = 0;
rowSizes = new HashMap<>();
columnSizes = new HashMap<>();
rowPadding = new HashMap<>();
columnPadding = new HashMap<>();
}
private void refresh() {
rowsCount = 0;
columnCount = 0;
for(int i: childrenRows.keySet()) {
if(childrenRows.get(i).size() != 0 && !rowSizes.containsKey(i)) {
rowsCount++;
}
}
for(int i: childrenCols.keySet()) {
if(childrenCols.get(i).size() != 0 && !columnSizes.containsKey(i)) {
columnCount++;
}
}
updateSize();
}
public void setSize() {
super.setSize();
updateSize();
}
public void updateSize() {
if(rowsCount == 0 && columnCount == 0) {
return;
}
int setHeights = 0;
int setWidths = 0;
for(int i: rowSizes.keySet()) {
setHeights += rowSizes.get(i);
}
for(int i: columnSizes.keySet()) {
setWidths += columnSizes.get(i);
}
int height = (getHeight() - setHeights) / rowsCount;
int width = (getWidth() - setWidths) / columnCount;
int locationY = 0;
for(int i: childrenRows.keySet()) {
int actualHeight = rowSizes.getOrDefault(i, height);
for(Visual v: childrenRows.get(i)) {
v.setHeight(actualHeight);
v.setLocationY(locationY);
}
locationY += actualHeight;
}
int locationX = 0;
for(int i: childrenCols.keySet()) {
int actualWidth = columnSizes.getOrDefault(i, width);
for(Visual v: childrenCols.get(i)) {
v.setWidth(actualWidth);
v.setLocationX(locationX);
}
locationX += actualWidth;
}
}
public void setRowPadding(int row, int col, int padding) {
rowPadding.put(new Point2<>(row, col), padding);
}
public void setColumnPadding(int row, int col, int padding) {
columnPadding.put(new Point2<>(row, col), padding);
}
public void setRowSize(int row, int height) {
rowSizes.put(row, height);
refresh();
}
public void setColumnSize(int column, int width) {
columnSizes.put(column, width);
refresh();
}
public void addVisual(Visual v, int row, int col) {
super.addVisual(v);
childrenCols.computeIfAbsent(col, k -> new ArrayList<>());
childrenRows.computeIfAbsent(row, k -> new ArrayList<>());
childrenCols.get(col).add(v);
childrenRows.get(row).add(v);
v.setLocation(-1.0f, -1.0f);
refresh();
}
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
g.setColor(getBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
}
}

View File

@@ -115,6 +115,11 @@ public class Panel extends Visual {
g.setColor(getBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight());
if(getHasBorder()) {
g.setColor(getBorderColor());
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
g.dispose();
}
}

View File

@@ -45,6 +45,10 @@ public class ScrollPanel extends Visual {
horizontalScrollBar.setSliderSize((float)getWidth() / getFarthestX());
horizontalScrollBar.setLocation(0, getHeight());
}
else {
removeVisual(horizontalScrollBar);
horizontalScrollBar = null;
}
if (getFarthestY() > getHeight()) {
if (verticalScrollBar == null) {
@@ -57,6 +61,10 @@ public class ScrollPanel extends Visual {
verticalScrollBar.setSliderSize((float)getHeight() / getFarthestY());
verticalScrollBar.setLocation(getWidth(), 0);
}
else {
removeVisual(verticalScrollBar);
verticalScrollBar = null;
}
}
@Override
@@ -125,6 +133,10 @@ public class ScrollPanel extends Visual {
}
}
@Override
public void handleNotification(Visual v, int notify) {
}
@Override
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
@@ -203,10 +215,17 @@ public class ScrollPanel extends Visual {
private static class VisualLocation {
Visual v;
Point2<Integer> originalLocation;
Point2<Float> originalRelativeLocation;
public VisualLocation(Visual v) {
this.v = v;
originalLocation = v.getLocation();
originalRelativeLocation = v.getRelativeLocation();
}
public void updateLocation() {
originalLocation = v.getLocation();
originalRelativeLocation = v.getRelativeLocation();
}
}
}

View File

@@ -2,7 +2,6 @@ package guiTree;
import guiTree.Helper.Debugger;
import guiTree.Helper.Timer;
import guiTree.events.KeyEventGetter;
import javax.swing.*;
import java.awt.*;

View File

@@ -2,7 +2,7 @@ package guiTree.Helper;
public class Debugger {
public enum Tag {
LISTENER(false),
LISTENER(true),
PAINTING(false),
FPS(true),
ANIMATIONS(false),

View File

@@ -17,4 +17,9 @@ public class Point2<T> {
public String toString() {
return "Point2 x:" + x + " y: " + y;
}
@Override
public int hashCode() {
return (x.toString() + ", " + y.toString()).hashCode();
}
}

View File

@@ -1,4 +1,4 @@
package guiTree.events;
package guiTree;
import guiTree.Window;
@@ -14,16 +14,16 @@ public class KeyEventGetter implements KeyListener {
@Override
public void keyTyped(KeyEvent keyEvent) {
callingWindow.keyTyped(keyEvent);
}
@Override
public void keyPressed(KeyEvent keyEvent) {
callingWindow.keyPressed(keyEvent);
}
@Override
public void keyReleased(KeyEvent keyEvent) {
callingWindow.keyReleased(keyEvent);
}
}

View File

@@ -9,10 +9,12 @@ import guiTree.events.MouseListener;
import guiTree.events.MouseWheelListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Visual {
@@ -61,7 +63,7 @@ public class Visual {
private Boolean active;
private Boolean dirty;
private static Visual entered;
private Boolean focused;
private static Visual focused;
private Boolean pressed;
/*--------------------------------------------------------------------
@@ -88,7 +90,6 @@ public class Visual {
this.dirty = true;
this.hasBorder = false;
this.active = this instanceof Window;
this.focused = false;
this.pressed = false;
this.width = width;
@@ -133,7 +134,7 @@ public class Visual {
}
}
propagateDirt();
notifyParent(SIZE_CHANGED);
notifyParent(this, SIZE_CHANGED);
}
public void setHeight(Integer height) {
@@ -176,7 +177,7 @@ public class Visual {
calculateAbsoluteLocation();
propagateDirt();
notifyParent(LOCATION_CHANGED);
notifyParent(this, LOCATION_CHANGED);
}
public void setLocation(Float x, Float y) {
@@ -267,6 +268,10 @@ public class Visual {
return new Point2<>(locationX, locationY);
}
public Point2<Float> getRelativeLocation() {
return new Point2<>(relativeX, relativeY);
}
public Font getFont() {
return font;
}
@@ -328,9 +333,13 @@ public class Visual {
if(this.active) {
child.activate();
}
propagateDirt();
}
public void removeVisual(Visual child) {
if(child == null) {
return;
}
this.children.remove(child);
child.setParent(null);
child.imageBuffer = null;
@@ -342,16 +351,29 @@ public class Visual {
this.parent = parent;
}
public void handleNotification(Visual v, int notify) {
}
public void handleNotification(int notify) {
}
public void notifyParent(int notify) {
public void notifyParent(Visual v, int notify) {
if(parent != null) {
parent.handleNotification(notify);
if(v == null) {
parent.handleNotification(notify);
}
else {
parent.handleNotification(v, notify);
}
}
}
public void notifyParent(int notify) {
notifyParent(null, notify);
}
public void addAnimation(AnimationInterface animation) {
animations.add(animation);
}
@@ -381,7 +403,7 @@ public class Visual {
clearImageBuffer();
this.paint(imageBuffer);
for (Visual v : children) {
if(v.dirty && v.active) {
if (v.dirty && v.active) {
v.revalidate();
}
imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
@@ -439,7 +461,6 @@ public class Visual {
for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mouseClicked(mouseEvent);
}
entered.focused = true;
Debugger.log("Clicked " + entered.name, Debugger.Tag.LISTENER);
}
@@ -456,6 +477,7 @@ public class Visual {
mouseListener.mousePressed(mouseEvent);
}
entered.pressed = true;
focused = entered;
Debugger.log("Pressed " + entered.name, Debugger.Tag.LISTENER);
}
@@ -540,13 +562,43 @@ public class Visual {
Debugger.log("Moved " + this.name, Debugger.Tag.LISTENER);
}
void keyPressed(KeyEvent keyEvent) {
if(focused == null) {
return;
}
for(KeyListener keyListener: focused.keyListeners) {
keyListener.keyPressed(keyEvent);
}
Debugger.log("Key " + keyEvent.paramString() + " Pressed " + focused.name, Debugger.Tag.LISTENER);
}
void keyReleased(KeyEvent keyEvent) {
if(focused == null) {
return;
}
for(KeyListener keyListener: focused.keyListeners) {
keyListener.keyReleased(keyEvent);
}
Debugger.log("Key " + keyEvent.paramString() + " Released " + focused.name, Debugger.Tag.LISTENER);
}
void keyTyped(KeyEvent keyEvent) {
if(focused == null) {
return;
}
for(KeyListener keyListener: focused.keyListeners) {
keyListener.keyTyped(keyEvent);
}
Debugger.log("Key " + keyEvent.paramString() + " Typed " + focused.name, Debugger.Tag.LISTENER);
}
void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) {
if(entered.focused) {
for(MouseWheelListener mouseWheelListener: entered.mouseWheelListeners) {
if(focused != null) {
for(MouseWheelListener mouseWheelListener: focused.mouseWheelListeners) {
mouseWheelListener.mouseWheelMoved(mouseWheelEvent);
}
Debugger.log("Wheel Moved " + focused.name, Debugger.Tag.LISTENER);
}
Debugger.log("Wheel Moved " + this.name, Debugger.Tag.LISTENER);
}
/*--------------------------------------------------------------------

View File

@@ -55,6 +55,10 @@ public class Window extends Visual implements Runnable{
bar.setBackgroundColor(Color.GRAY);
this.setTitleBar(bar);
close = false;
Thread paintThread = new Thread(this);
paintThread.setName("Painting Thread");
paintThread.start();
}
@Override
@@ -70,7 +74,6 @@ public class Window extends Visual implements Runnable{
contentPanel.setSize(width, height);
}
Debugger.log("Calling repaint from window set size: ", Debugger.Tag.PAINTING);
repaint();
}
public void setFrameImageBuffer(BufferedImage imageBuffer){

View File

@@ -70,10 +70,6 @@ public class XAMLParser {
rootObject = parseNode(rootNode);
if(rootObject instanceof Window) {
((Window) rootObject).repaint();
Thread windowThread = new Thread((Window) rootObject);
windowThread.setName("Painting Thread");
windowThread.start();
return (Window) rootObject;
}
return null;