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

@@ -1,14 +1,7 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<Window <Window
Name="window" Name="Window"
Visible="true" Visible="True"
Size="640, 480" Title="Sudoku 1.0"
Title="FANTASTICEST UI THINGY"> Size="1024, 576">
<ScrollPanel Name="ScrollPane" BackgroundColor="#333333" Size="1.0, 1.0" Location="0.0, 0.0">
<ToggleButton Name="button3" BackgroundColor="#123456" AccentColor="#654321" Size="100, 100" Location="320, 240" Icon="minimize_white"/>
<Slider Name="Slider" Size="100, 20" Location="0, 0" Direction="horizontal" SliderSize="0.3"/>
<Slider Name="VSlider" Size="20, 100" Location="101, 0" SliderSize="0.5"/>
<Button Name="Button" BackgroundColor="#777777" AccentColor="#654321" Size="100, 100" Location="660, 0"/>
<Button Name="Button" BackgroundColor="#777777" AccentColor="#654321" Size="100, 100" Location="660, 660"/>
</ScrollPanel>
</Window> </Window>

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.setColor(getBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight()); g.fillRect(0, 0, getWidth(), getHeight());
if(getHasBorder()) {
g.setColor(getBorderColor());
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
g.dispose(); g.dispose();
} }
} }

View File

@@ -45,6 +45,10 @@ public class ScrollPanel extends Visual {
horizontalScrollBar.setSliderSize((float)getWidth() / getFarthestX()); horizontalScrollBar.setSliderSize((float)getWidth() / getFarthestX());
horizontalScrollBar.setLocation(0, getHeight()); horizontalScrollBar.setLocation(0, getHeight());
} }
else {
removeVisual(horizontalScrollBar);
horizontalScrollBar = null;
}
if (getFarthestY() > getHeight()) { if (getFarthestY() > getHeight()) {
if (verticalScrollBar == null) { if (verticalScrollBar == null) {
@@ -57,6 +61,10 @@ public class ScrollPanel extends Visual {
verticalScrollBar.setSliderSize((float)getHeight() / getFarthestY()); verticalScrollBar.setSliderSize((float)getHeight() / getFarthestY());
verticalScrollBar.setLocation(getWidth(), 0); verticalScrollBar.setLocation(getWidth(), 0);
} }
else {
removeVisual(verticalScrollBar);
verticalScrollBar = null;
}
} }
@Override @Override
@@ -125,6 +133,10 @@ public class ScrollPanel extends Visual {
} }
} }
@Override
public void handleNotification(Visual v, int notify) {
}
@Override @Override
public void paint(BufferedImage imageBuffer) { public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics(); Graphics2D g = imageBuffer.createGraphics();
@@ -203,10 +215,17 @@ public class ScrollPanel extends Visual {
private static class VisualLocation { private static class VisualLocation {
Visual v; Visual v;
Point2<Integer> originalLocation; Point2<Integer> originalLocation;
Point2<Float> originalRelativeLocation;
public VisualLocation(Visual v) { public VisualLocation(Visual v) {
this.v = v; this.v = v;
originalLocation = v.getLocation(); 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.Debugger;
import guiTree.Helper.Timer; import guiTree.Helper.Timer;
import guiTree.events.KeyEventGetter;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;

View File

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

View File

@@ -17,4 +17,9 @@ public class Point2<T> {
public String toString() { public String toString() {
return "Point2 x:" + x + " y: " + y; 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; import guiTree.Window;
@@ -14,16 +14,16 @@ public class KeyEventGetter implements KeyListener {
@Override @Override
public void keyTyped(KeyEvent keyEvent) { public void keyTyped(KeyEvent keyEvent) {
callingWindow.keyTyped(keyEvent);
} }
@Override @Override
public void keyPressed(KeyEvent keyEvent) { public void keyPressed(KeyEvent keyEvent) {
callingWindow.keyPressed(keyEvent);
} }
@Override @Override
public void keyReleased(KeyEvent keyEvent) { public void keyReleased(KeyEvent keyEvent) {
callingWindow.keyReleased(keyEvent);
} }
} }

View File

@@ -9,10 +9,12 @@ import guiTree.events.MouseListener;
import guiTree.events.MouseWheelListener; import guiTree.events.MouseWheelListener;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
public class Visual { public class Visual {
@@ -61,7 +63,7 @@ public class Visual {
private Boolean active; private Boolean active;
private Boolean dirty; private Boolean dirty;
private static Visual entered; private static Visual entered;
private Boolean focused; private static Visual focused;
private Boolean pressed; private Boolean pressed;
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
@@ -88,7 +90,6 @@ public class Visual {
this.dirty = true; this.dirty = true;
this.hasBorder = false; this.hasBorder = false;
this.active = this instanceof Window; this.active = this instanceof Window;
this.focused = false;
this.pressed = false; this.pressed = false;
this.width = width; this.width = width;
@@ -133,7 +134,7 @@ public class Visual {
} }
} }
propagateDirt(); propagateDirt();
notifyParent(SIZE_CHANGED); notifyParent(this, SIZE_CHANGED);
} }
public void setHeight(Integer height) { public void setHeight(Integer height) {
@@ -176,7 +177,7 @@ public class Visual {
calculateAbsoluteLocation(); calculateAbsoluteLocation();
propagateDirt(); propagateDirt();
notifyParent(LOCATION_CHANGED); notifyParent(this, LOCATION_CHANGED);
} }
public void setLocation(Float x, Float y) { public void setLocation(Float x, Float y) {
@@ -267,6 +268,10 @@ public class Visual {
return new Point2<>(locationX, locationY); return new Point2<>(locationX, locationY);
} }
public Point2<Float> getRelativeLocation() {
return new Point2<>(relativeX, relativeY);
}
public Font getFont() { public Font getFont() {
return font; return font;
} }
@@ -328,9 +333,13 @@ public class Visual {
if(this.active) { if(this.active) {
child.activate(); child.activate();
} }
propagateDirt();
} }
public void removeVisual(Visual child) { public void removeVisual(Visual child) {
if(child == null) {
return;
}
this.children.remove(child); this.children.remove(child);
child.setParent(null); child.setParent(null);
child.imageBuffer = null; child.imageBuffer = null;
@@ -342,16 +351,29 @@ public class Visual {
this.parent = parent; this.parent = parent;
} }
public void handleNotification(Visual v, int notify) {
}
public void handleNotification(int notify) { public void handleNotification(int notify) {
} }
public void notifyParent(int notify) { public void notifyParent(Visual v, int notify) {
if(parent != null) { 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) { public void addAnimation(AnimationInterface animation) {
animations.add(animation); animations.add(animation);
} }
@@ -381,7 +403,7 @@ public class Visual {
clearImageBuffer(); clearImageBuffer();
this.paint(imageBuffer); this.paint(imageBuffer);
for (Visual v : children) { for (Visual v : children) {
if(v.dirty && v.active) { if (v.dirty && v.active) {
v.revalidate(); v.revalidate();
} }
imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null); imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
@@ -439,7 +461,6 @@ public class Visual {
for(MouseListener mouseListener: entered.mouseListeners) { for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mouseClicked(mouseEvent); mouseListener.mouseClicked(mouseEvent);
} }
entered.focused = true;
Debugger.log("Clicked " + entered.name, Debugger.Tag.LISTENER); Debugger.log("Clicked " + entered.name, Debugger.Tag.LISTENER);
} }
@@ -456,6 +477,7 @@ public class Visual {
mouseListener.mousePressed(mouseEvent); mouseListener.mousePressed(mouseEvent);
} }
entered.pressed = true; entered.pressed = true;
focused = entered;
Debugger.log("Pressed " + entered.name, Debugger.Tag.LISTENER); Debugger.log("Pressed " + entered.name, Debugger.Tag.LISTENER);
} }
@@ -540,13 +562,43 @@ public class Visual {
Debugger.log("Moved " + this.name, Debugger.Tag.LISTENER); 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) { void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) {
if(entered.focused) { if(focused != null) {
for(MouseWheelListener mouseWheelListener: entered.mouseWheelListeners) { for(MouseWheelListener mouseWheelListener: focused.mouseWheelListeners) {
mouseWheelListener.mouseWheelMoved(mouseWheelEvent); 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); bar.setBackgroundColor(Color.GRAY);
this.setTitleBar(bar); this.setTitleBar(bar);
close = false; close = false;
Thread paintThread = new Thread(this);
paintThread.setName("Painting Thread");
paintThread.start();
} }
@Override @Override
@@ -70,7 +74,6 @@ public class Window extends Visual implements Runnable{
contentPanel.setSize(width, height); contentPanel.setSize(width, height);
} }
Debugger.log("Calling repaint from window set size: ", Debugger.Tag.PAINTING); Debugger.log("Calling repaint from window set size: ", Debugger.Tag.PAINTING);
repaint();
} }
public void setFrameImageBuffer(BufferedImage imageBuffer){ public void setFrameImageBuffer(BufferedImage imageBuffer){

View File

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