diff --git a/resources/ui.xml b/resources/ui.xml
index 1850d09..b3926bb 100644
--- a/resources/ui.xml
+++ b/resources/ui.xml
@@ -1,14 +1,7 @@
-
-
-
-
-
-
-
+ Name="Window"
+ Visible="True"
+ Title="Sudoku 1.0"
+ Size="1024, 576">
\ No newline at end of file
diff --git a/src/guiTree/Components/GridPanel.java b/src/guiTree/Components/GridPanel.java
new file mode 100644
index 0000000..b88b3d2
--- /dev/null
+++ b/src/guiTree/Components/GridPanel.java
@@ -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> childrenCols;
+ private Map> childrenRows;
+ private Map rowSizes;
+ private Map columnSizes;
+ private Map, Integer> rowPadding;
+ private Map, 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();
+ }
+}
diff --git a/src/guiTree/Components/Panel.java b/src/guiTree/Components/Panel.java
index eed332f..90ec3c5 100644
--- a/src/guiTree/Components/Panel.java
+++ b/src/guiTree/Components/Panel.java
@@ -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();
}
}
diff --git a/src/guiTree/Components/ScrollPanel.java b/src/guiTree/Components/ScrollPanel.java
index 2623e5a..7d170ed 100644
--- a/src/guiTree/Components/ScrollPanel.java
+++ b/src/guiTree/Components/ScrollPanel.java
@@ -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 originalLocation;
+ Point2 originalRelativeLocation;
public VisualLocation(Visual v) {
this.v = v;
originalLocation = v.getLocation();
+ originalRelativeLocation = v.getRelativeLocation();
+ }
+
+ public void updateLocation() {
+ originalLocation = v.getLocation();
+ originalRelativeLocation = v.getRelativeLocation();
}
}
}
diff --git a/src/guiTree/CustomFrame.java b/src/guiTree/CustomFrame.java
index 5d4523b..37e9d95 100644
--- a/src/guiTree/CustomFrame.java
+++ b/src/guiTree/CustomFrame.java
@@ -2,7 +2,6 @@ package guiTree;
import guiTree.Helper.Debugger;
import guiTree.Helper.Timer;
-import guiTree.events.KeyEventGetter;
import javax.swing.*;
import java.awt.*;
diff --git a/src/guiTree/Helper/Debugger.java b/src/guiTree/Helper/Debugger.java
index fd571d7..835ffd1 100644
--- a/src/guiTree/Helper/Debugger.java
+++ b/src/guiTree/Helper/Debugger.java
@@ -2,7 +2,7 @@ package guiTree.Helper;
public class Debugger {
public enum Tag {
- LISTENER(false),
+ LISTENER(true),
PAINTING(false),
FPS(true),
ANIMATIONS(false),
diff --git a/src/guiTree/Helper/Point2.java b/src/guiTree/Helper/Point2.java
index ea45229..611ce08 100644
--- a/src/guiTree/Helper/Point2.java
+++ b/src/guiTree/Helper/Point2.java
@@ -17,4 +17,9 @@ public class Point2 {
public String toString() {
return "Point2 x:" + x + " y: " + y;
}
+
+ @Override
+ public int hashCode() {
+ return (x.toString() + ", " + y.toString()).hashCode();
+ }
}
diff --git a/src/guiTree/events/KeyEventGetter.java b/src/guiTree/KeyEventGetter.java
similarity index 76%
rename from src/guiTree/events/KeyEventGetter.java
rename to src/guiTree/KeyEventGetter.java
index 6fdeaff..63c090c 100644
--- a/src/guiTree/events/KeyEventGetter.java
+++ b/src/guiTree/KeyEventGetter.java
@@ -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);
}
}
diff --git a/src/guiTree/Visual.java b/src/guiTree/Visual.java
index aee4459..4da1733 100644
--- a/src/guiTree/Visual.java
+++ b/src/guiTree/Visual.java
@@ -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 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);
}
/*--------------------------------------------------------------------
diff --git a/src/guiTree/Window.java b/src/guiTree/Window.java
index 3bf1301..703a630 100644
--- a/src/guiTree/Window.java
+++ b/src/guiTree/Window.java
@@ -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){
diff --git a/src/parser/XAMLParser.java b/src/parser/XAMLParser.java
index 3dc4d55..90fb713 100644
--- a/src/parser/XAMLParser.java
+++ b/src/parser/XAMLParser.java
@@ -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;