added debugger and timer

improved paint efficiency
debugged relative position
removed bug in parser for empty attribute methods
This commit is contained in:
rmaco
2020-03-23 18:00:45 +02:00
parent bce51befc2
commit 53265227f7
13 changed files with 253 additions and 129 deletions

View File

@@ -1,13 +1,38 @@
<?xml version="1.0"?>
<Window Name="window" Visible="true" Size="640, 480">
<Panel Name="MainPanel" BackgroundColor="#999999" Size="1.0, 1.0" Overlapping="true">
<Button Name="button1" BackgroundColor="#990000" Size="0.5, 0.5" Location="0.0, 0.0" Label="button1">
<Button Name="button4" BackgroundColor="#009999" Size="0.3, 0.3" Icon="square_white">
<Button Name="button2" BackgroundColor="#000099" Size="50, 50" Label="button2"/>
<Window
Name="window"
Visible="true"
Size="640, 480"
Title="FANTASTICEST UI THINGY">
<Button
Name="button1"
BackgroundColor="#999999"
AccentColor="#666666"
ForegroundColor="#333333"
Size="0.5, 0.5"
Location="0.0, 0.0"
HasBorder="true"
Label="button1">
<Button
Name="button4"
BackgroundColor="#009999"
AccentColor="#006666"
ForegroundColor="#003333"
Size="0.5, 0.5"
Location="0.25, 0.25"
Icon="square_white"
HasBorder="true">
<Button
Name="button2"
BackgroundColor="#000099"
AccentColor="#000066"
ForegroundColor="#000033"
Size="0.5, 0.5"
Location="0.25, 0.25"
Label="button2"/>
</Button>
</Button>
</Button>
<Button Name="button6" BackgroundColor="#555555" Location="0.5, 0.5" Size="0.5, 0.5"/>
<Button Name="button3" BackgroundColor="#009900" Size="0.5, 0.5" Location="0.5, 0.0" Icon="close_black"/>
<!-- <Button Name="button3" BackgroundColor="#123456" Size="0.5, 0.5" Location="0.5, 0.0" Icon="minimize_white"/>-->
</Panel>
<Button Name="button3" BackgroundColor="#009900" Size="0.5, 0.5" Location="0.0, 0.5" Icon="close_black"/>
<Button Name="button3" BackgroundColor="#123456" Size="0.5, 0.5" Location="0.5, 0.0" Icon="minimize_white"/>
</Window>

View File

@@ -1,59 +1,39 @@
import guiTree.Components.Button;
import guiTree.Components.Panel;
import guiTree.Window;
import guiTree.events.MouseAdapter;
import parser.XAMLParser;
import java.awt.event.MouseEvent;
public class Main {
public static void main(String[] args) {
try{
Window window = XAMLParser.parse("ui.xml");
assert window != null;
window.repaint();
Button button = (Button)window.findByName("button3");
button.addMouseListener(new MouseAdapter() {
Button button1 = (Button)window.findByName("button1");
button1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
Panel panel = window.getMainPanel();
if(panel.getOverlapping()) {
panel.setOverlapping(false);
}
else {
panel.setOverlapping(true);
}
window.repaint();
System.out.println("Button x: " + button1.getLocationX() + " y: " + button1.getLocationY());
}
});
window.repaint();
System.out.println(Float.parseFloat("3"));
long now;
long prev = 0;
// while(true) {
// now = System.currentTimeMillis();
// if(now - prev >= 1000) {
// int x = button.getLocationX();
// int y = button.getLocationY();
// if(x + button.getWidth() >= window.getWidth()) {
// x = 0;
// if(y + button.getHeight() >= window.getHeight()) {
// y = 0;
// }
// else {
// y += 30;
// }
// }
// else {
// x += 30;
// }
// button.setLocation(x, y);
// prev = now;
// window.revalidate();
// }
// }
Button button2 = (Button)window.findByName("button2");
button2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("Button x: " + button2.getLocationX() + " y: " + button2.getLocationY());
}
});
Button button4 = (Button)window.findByName("button4");
button4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
System.out.println("Button x: " + button4.getLocationX() + " y: " + button4.getLocationY());
}
});
}catch (Exception e){
e.printStackTrace();
}

View File

@@ -1,5 +1,8 @@
package guiTree.Components;
import guiTree.Helper.Debugger;
import guiTree.Helper.Tag;
import guiTree.Helper.Timer;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
@@ -35,32 +38,41 @@ public class Button extends Visual {
pressed = false;
hovered = false;
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
pressed = false;
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
pressed = true;
Debugger.log("Pressed: " + getName(), Tag.LISTENER);
Debugger.log("Calling repaint from pressed: " + getName(), Tag.PAINTING);
repaint();
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
pressed = false;
Debugger.log("Calling repaint from released: " + getName(), Tag.PAINTING);
repaint();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
hovered = true;
Debugger.log("Calling repaint from entered: " + getName(), Tag.PAINTING);
repaint();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
hovered = false;
Debugger.log("Calling repaint from exited: " + getName(), Tag.PAINTING);
repaint();
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
repaint();
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
Debugger.log("Calling repaint from moved: " + getName(), Tag.PAINTING);
repaint();
}
});
@@ -78,18 +90,28 @@ public class Button extends Visual {
g.setComposite(AlphaComposite.Src);
//Choose background
if(hovered || pressed) {
g.setColor(this.getForegroundColor());
if(hovered) {
g.setColor(this.getAccentColor());
}
else {
g.setColor(this.getBackgroundColor());
}
if(pressed) {
g.setColor(this.getForegroundColor());
}
//Draw Button
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(this.getFontColor());
if(getHasBorder()) {
g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);
g.setColor(getBorderColor());
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
}
else {
g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
}
//Draw Label
g.setColor(this.getFontColor());
int textWidth = 0;
int textHeight = 0;
if(!label.equals("")) {

View File

@@ -72,8 +72,8 @@ public class Panel extends Visual {
@Override
public void addVisual(Visual v) {
calculatePosition(v);
super.addVisual(v);
calculatePosition(v);
visuals.add(v);
}

View File

@@ -52,9 +52,13 @@ public class TitleBar extends Visual {
close.setBackgroundColor(Color.GRAY);
maximize.setBackgroundColor(Color.GRAY);
minimize.setBackgroundColor(Color.GRAY);
close.setAccentColor(Color.RED);
maximize.setAccentColor(Color.LIGHT_GRAY);
minimize.setAccentColor(Color.LIGHT_GRAY);
close.setForegroundColor(Color.RED);
maximize.setForegroundColor(Color.LIGHT_GRAY);
minimize.setForegroundColor(Color.LIGHT_GRAY);
maximize.setForegroundColor(Color.DARK_GRAY);
minimize.setForegroundColor(Color.DARK_GRAY);
this.setSize(0, 30);
this.setLocation(0, 0);

View File

@@ -1,5 +1,7 @@
package guiTree;
import guiTree.Helper.Debugger;
import guiTree.Helper.Tag;
import guiTree.events.KeyEventGetter;
import guiTree.events.MouseWheelGetter;
@@ -37,8 +39,7 @@ public class CustomFrame extends JFrame {
}
@Override
public void paint(Graphics g)
{
public void paint(Graphics g) {
g.drawImage(imageBuffer, 0, 0, this.getWidth(), this.getHeight(), null);
}

View File

@@ -0,0 +1,11 @@
package guiTree.Helper;
public class Debugger {
private static Timer timer = new Timer();
public static void log(String message, Tag tag) {
if(tag.value) {
System.out.println("[" + tag.toString() + "] " + message);
}
}
}

View File

@@ -0,0 +1,12 @@
package guiTree.Helper;
public enum Tag {
LISTENER(false),
PAINTING(false);
public boolean value;
Tag(boolean value) {
this.value = value;
}
}

View File

@@ -0,0 +1,15 @@
package guiTree.Helper;
public class Timer {
private long now;
private long prev;
public void startTiming() {
prev = System.currentTimeMillis();
}
public long stopTiming() {
now = System.currentTimeMillis();
return now - prev;
}
}

View File

@@ -19,7 +19,7 @@ public class MouseEventGetter implements MouseInputListener {
@Override
public void mouseMoved(MouseEvent mouseEvent) {
callingWindow.mouseMoved(mouseEvent, 0, 0);
callingWindow.mouseMoved(mouseEvent);
}
@Override
@@ -39,7 +39,7 @@ public class MouseEventGetter implements MouseInputListener {
@Override
public void mouseEntered(MouseEvent mouseEvent) {
callingWindow.mouseEntered(mouseEvent, 0, 0);
callingWindow.mouseEntered(mouseEvent);
}
@Override

View File

@@ -1,5 +1,8 @@
package guiTree;
import guiTree.Helper.Debugger;
import guiTree.Helper.Tag;
import guiTree.Helper.Timer;
import guiTree.events.KeyListener;
import guiTree.events.MouseListener;
import guiTree.events.MouseWheelListener;
@@ -15,6 +18,7 @@ public class Visual {
/*--------------------------------------------------------------------
Constant Values
---------------------------------------------------------------------*/
public static final int SIZE_CHANGED = 1;
public static final int LOCATION_CHANGED = 2;
@@ -40,15 +44,20 @@ public class Visual {
private Float relativeHeight;
private Integer locationX;
private Integer locationY;
private Integer absoluteX;
private Integer absoluteY;
private Float relativeX;
private Float relativeY;
private Color backgroundColor;
private Color foregroundColor;
private Color accentColor;
private Color fontColor;
private Color borderColor;
private Boolean active;
private Boolean dirty;
public Boolean dirty;
private static Visual entered;
private Boolean focused;
private Boolean hasBorder;
private Boolean pressed;
@@ -70,8 +79,11 @@ public class Visual {
this.backgroundColor = Color.WHITE;
this.foregroundColor = Color.BLUE;
this.fontColor = Color.BLACK;
this.borderColor = Color.BLACK;
this.accentColor = Color.BLUE;
this.dirty = true;
this.hasBorder = false;
this.active = this instanceof Window;
this.focused = false;
this.pressed = false;
@@ -85,6 +97,8 @@ public class Visual {
this.locationX = 0;
this.locationY = 0;
this.absoluteX = 0;
this.absoluteY = 0;
}
/*--------------------------------------------------------------------
@@ -115,8 +129,8 @@ public class Visual {
v.setLocation();
}
}
this.dirty = true;
this.notifyParent(SIZE_CHANGED);
propagateDirt();
notifyParent(SIZE_CHANGED);
}
public void setSize(Integer width, Integer height) {
@@ -141,7 +155,8 @@ public class Visual {
}
}
this.dirty = true;
calculateAbsoluteLocation();
propagateDirt();
notifyParent(LOCATION_CHANGED);
}
@@ -167,25 +182,28 @@ public class Visual {
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
this.dirty = true;
propagateDirt();
}
public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor;
this.dirty = true;
propagateDirt();
}
public void setFontColor(Color fontColor) {
this.fontColor = fontColor;
}
private void calculateInitialLocation() {
if(this.locationX <= 0) {
this.locationX = 0;
}
if(this.locationY <= 0){
this.locationY = 0;
}
public void setAccentColor(Color accentColor) {
this.accentColor = accentColor;
}
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
public void setHasBorder(Boolean hasBorder) {
this.hasBorder = hasBorder;
}
/*--------------------------------------------------------------------
@@ -224,6 +242,18 @@ public class Visual {
return fontColor;
}
public Color getAccentColor() {
return accentColor;
}
public Color getBorderColor() {
return borderColor;
}
public Boolean getHasBorder() {
return hasBorder;
}
/*--------------------------------------------------------------------
Tree Methods
---------------------------------------------------------------------*/
@@ -247,7 +277,7 @@ public class Visual {
public void addVisual(Visual child) {
this.children.add(child);
child.setParent(this);
child.calculateInitialLocation();
child.setLocation();
child.setSize();
if(this.active) {
@@ -260,7 +290,7 @@ public class Visual {
child.setParent(null);
child.imageBuffer = null;
child.deactivate();
this.dirty = true;
propagateDirt();
}
private void setParent(Visual parent) {
@@ -273,24 +303,21 @@ public class Visual {
public void notifyParent(int notify) {
if(parent != null) {
this.parent.handleNotification(notify);
parent.handleNotification(notify);
}
}
public void repaint() {
if(!this.active){
return;
}
if(this.dirty) {
if(dirty && active) {
this.revalidate();
return;
}
for(Visual v: children) {
v.repaint();
}
}
private void revalidate() {
Timer timer = new Timer();
Debugger.log("Revalidating " + name, Tag.PAINTING);
timer.startTiming();
clearImageBuffer();
this.paint(imageBuffer);
for (Visual v : children) {
@@ -300,10 +327,15 @@ public class Visual {
this.dirty = false;
if(!(this instanceof Window)){
this.parent.revalidate();
long time = timer.stopTiming();
Debugger.log("Finished Revalidating " + name + ": " + time, Tag.PAINTING);
return;
}
Window window = (Window)this;
window.setFrameImageBuffer(imageBuffer);
long time = timer.stopTiming();
Debugger.log("Finished Revalidating " + name + ": " + time, Tag.PAINTING);
window.revalidate();
}
@@ -314,6 +346,7 @@ public class Visual {
/*--------------------------------------------------------------------
Listener Methods
---------------------------------------------------------------------*/
public void addMouseListener(MouseListener mouseListener) {
this.mouseListeners.add(mouseListener);
}
@@ -342,15 +375,17 @@ public class Visual {
for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mouseClicked(mouseEvent);
}
dirty = true;
entered.propagateDirt();
entered.focused = true;
Debugger.log("Clicked " + entered.name, Tag.LISTENER);
}
void mouseReleased(MouseEvent mouseEvent) {
for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mouseReleased(mouseEvent);
}
dirty = true;
Debugger.log("Released " + entered.name, Tag.LISTENER);
propagateDirt();
entered.pressed = false;
}
@@ -358,22 +393,23 @@ public class Visual {
for(MouseListener mouseListener: entered.mouseListeners) {
mouseListener.mousePressed(mouseEvent);
}
dirty = true;
entered.propagateDirt();
entered.pressed = true;
Debugger.log("Pressed " + entered.name, Tag.LISTENER);
}
void mouseEntered(MouseEvent mouseEvent, int offsetX, int offsetY) {
void mouseEntered(MouseEvent mouseEvent) {
if(entered != null && entered.pressed){
return;
}
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
int mouseX = mouseEvent.getX();
int mouseY = mouseEvent.getY();
for(Visual v: children) {
if(mouseX > v.getLocationX() &&
mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()){
v.mouseEntered(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
v.mouseEntered(mouseEvent);
return;
}
}
@@ -381,7 +417,8 @@ public class Visual {
for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseEntered(mouseEvent);
}
dirty = true;
Debugger.log("Entered " + entered.name, Tag.LISTENER);
propagateDirt();
}
void mouseExited(MouseEvent mouseEvent) {
@@ -394,34 +431,37 @@ public class Visual {
for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseExited(mouseEvent);
}
Debugger.log("Exited " + entered.name, Tag.LISTENER);
entered.propagateDirt();
entered = null;
dirty = true;
}
void mouseDragged(MouseEvent mouseEvent) {
for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseDragged(mouseEvent);
}
entered.dirty = true;
entered.propagateDirt();
Debugger.log("Dragged " + entered.name, Tag.LISTENER);
}
void mouseMoved(MouseEvent mouseEvent, int offsetX, int offsetY) {
void mouseMoved(MouseEvent mouseEvent) {
if(entered != null && entered.pressed){
return;
}
int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY;
int mouseX = mouseEvent.getX();
int mouseY = mouseEvent.getY();
if(entered != null) {
if (!entered.isInside(mouseEvent.getX(), mouseEvent.getY())) {
if (!entered.isInside(mouseX, mouseY)) {
for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseExited(mouseEvent);
}
Debugger.log("Exited " + entered.name, Tag.LISTENER);
entered = this;
}
}
for(Visual v: children) {
if(v.isInside(mouseX, mouseY)) {
v.mouseMoved(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
v.mouseMoved(mouseEvent);
return;
}
}
@@ -439,20 +479,23 @@ public class Visual {
mouseListener.mouseMoved(mouseEvent);
}
}
dirty = true;
Debugger.log("Moved " + this.name, Tag.LISTENER);
propagateDirt();
}
void mouseWheelMoved(MouseWheelEvent mouseWheelEvent, int offsetX, int offsetY) {
void mouseWheelMoved(MouseWheelEvent mouseWheelEvent) {
if(focused) {
for(MouseWheelListener mouseWheelListener: mouseWheelListeners) {
mouseWheelListener.mouseWheelMoved(mouseWheelEvent);
}
}
Debugger.log("Wheel Moved " + this.name, Tag.LISTENER);
}
/*--------------------------------------------------------------------
Helper Methods
---------------------------------------------------------------------*/
private void initializeImageBuffer(){
if(this.width <= 0 || this.height <= 0) {
return;
@@ -476,7 +519,6 @@ public class Visual {
for(Visual child: children) {
child.activate();
}
this.dirty = true;
}
private void deactivate() {
@@ -488,6 +530,27 @@ public class Visual {
}
private boolean isInside(int x, int y) {
return x > locationX && x < locationX + width && y > locationY && y < locationY + height;
return x > absoluteX && x < absoluteX + width && y > absoluteY && y < absoluteY + height;
}
private void calculateAbsoluteLocation() {
if(parent == null) {
absoluteX = locationX;
absoluteY = locationY;
}
else {
absoluteX = locationX + parent.absoluteX;
absoluteY = locationY + parent.absoluteY;
for(Visual v: children) {
v.calculateAbsoluteLocation();
}
}
}
private void propagateDirt() {
dirty = true;
if(parent != null) {
parent.propagateDirt();
}
}
}

View File

@@ -1,9 +1,9 @@
package guiTree;
import guiTree.Components.TitleBar;
import guiTree.Helper.Debugger;
import guiTree.Helper.Point2d;
import guiTree.Listeners.Direction;
import guiTree.Listeners.ResizeListener;
import guiTree.Helper.Tag;
import guiTree.events.MouseAdapter;
import guiTree.Components.Panel;
@@ -25,6 +25,10 @@ public class Window extends Visual {
private Point2d oldLocation;
public Window() {
this("");
}
public Window(String title) {
super();
this.frame = new CustomFrame(this);
this.setUndecorated(true);
@@ -42,7 +46,8 @@ public class Window extends Visual {
} catch (IOException e) {
e.printStackTrace();
}
TitleBar bar = new TitleBar("Working Title", icon);
TitleBar bar = new TitleBar(title, icon);
bar.setName("TitleBar");
bar.setBackgroundColor(Color.GRAY);
this.setTitleBar(bar);
}
@@ -51,6 +56,7 @@ public class Window extends Visual {
public void setSize(Integer width, Integer height) {
this.frame.setSize(width, height);
super.setSize(width, height);
mainPanel.setSize(width, height);
if(titleBar != null) {
titleBar.setSize(this.getWidth(), titleBar.getHeight());
contentPanel.setSize(width, height - titleBar.getHeight());
@@ -58,7 +64,7 @@ public class Window extends Visual {
else {
contentPanel.setSize(width, height);
}
mainPanel.setSize(width, height);
Debugger.log("Calling repaint from window set size: ", Tag.PAINTING);
repaint();
}
@@ -68,6 +74,7 @@ public class Window extends Visual {
}
public void revalidate() {
Debugger.log("Finished painting", Tag.PAINTING);
this.frame.repaint();
}
@@ -127,6 +134,10 @@ public class Window extends Visual {
});
}
public void setTitle(String title) {
titleBar.setTitle(title);
}
public TitleBar getTitleBar() {
return this.titleBar;
}
@@ -197,7 +208,6 @@ public class Window extends Visual {
break;
}
case TitleBar.NORMALIZE: {
Rectangle screenBounds = frame.getGraphicsConfiguration().getBounds();
this.setSize(oldSize.x, oldSize.y);
this.setLocation(oldLocation.x, oldLocation.y);
setState(Frame.NORMAL);

View File

@@ -41,26 +41,6 @@ public class XAMLParser {
}
}
private static Method getMethod(Object object, String methodName, List<Object> parameterList){
Method method;
Class<?>[] args = new Class[parameterList.size()];
for(Object o: parameterList){
try {
args[parameterList.indexOf(o)] = o.getClass();
} catch (NullPointerException e) {
System.err.println("Null Pointer Exception: " + methodName + " with parameters + " + parameterList.toString());
}
}
try {
method = object.getClass().getMethod(methodName, args);
return method;
} catch (NoSuchMethodException e) {
System.err.println("Method does not exist: " + methodName + " with parameters + " + parameterList.toString());
e.printStackTrace();
}
return null;
}
private static List<Method> getMethodsFromName(Object object, String methodName){
Method[] methods = object.getClass().getMethods();
List<Method> returnMethods = new ArrayList<>();
@@ -86,6 +66,7 @@ public class XAMLParser {
rootObject = parseNode(rootNode);
if(rootObject instanceof Window) {
((Window) rootObject).repaint();
return (Window) rootObject;
}
return null;
@@ -112,7 +93,7 @@ public class XAMLParser {
break;
}
}
if(primitiveAttributes.size() == types.length){
if(primitiveAttributes.size() == types.length && types.length == values.size()){
return primitiveAttributes;
}
}