improved efficiency

This commit is contained in:
rmaco
2020-03-21 17:36:45 +02:00
parent 35c7a7721b
commit bce51befc2
5 changed files with 55 additions and 52 deletions

View File

@@ -12,7 +12,7 @@ public class Main {
try{
Window window = XAMLParser.parse("ui.xml");
assert window != null;
window.revalidate();
window.repaint();
Button button = (Button)window.findByName("button3");
button.addMouseListener(new MouseAdapter() {
@Override
@@ -24,11 +24,11 @@ public class Main {
else {
panel.setOverlapping(true);
}
window.revalidate();
window.repaint();
}
});
window.revalidate();
window.repaint();
System.out.println(Float.parseFloat("3"));
long now;
long prev = 0;

View File

@@ -38,30 +38,30 @@ public class Button extends Visual {
@Override
public void mousePressed(MouseEvent mouseEvent) {
pressed = true;
revalidate();
repaint();
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
pressed = false;
revalidate();
repaint();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
hovered = true;
revalidate();
repaint();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
hovered = false;
revalidate();
repaint();
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
revalidate();
repaint();
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
revalidate();
repaint();
}
});
}

View File

@@ -40,6 +40,8 @@ public class Visual {
private Float relativeHeight;
private Integer locationX;
private Integer locationY;
private Float relativeX;
private Float relativeY;
private Color backgroundColor;
private Color foregroundColor;
private Color fontColor;
@@ -78,6 +80,8 @@ public class Visual {
this.height = height;
this.relativeWidth = -1.0f;
this.relativeHeight = -1.0f;
this.relativeX = -1.0f;
this.relativeY = -1.0f;
this.locationX = 0;
this.locationY = 0;
@@ -107,6 +111,9 @@ public class Visual {
if(v.relativeHeight > 0.0 || v.relativeWidth > 0.0) {
v.setSize();
}
if(v.relativeX >= 0.0 || v.relativeY >= 0.0) {
v.setLocation();
}
}
this.dirty = true;
this.notifyParent(SIZE_CHANGED);
@@ -124,11 +131,30 @@ public class Visual {
setSize();
}
public void setLocation() {
if(parent != null) {
if(relativeX >= 0.0) {
locationX = Math.round(relativeX * parent.width);
}
if(relativeY >= 0.0) {
locationY = Math.round(relativeY * parent.height);
}
}
this.dirty = true;
notifyParent(LOCATION_CHANGED);
}
public void setLocation(Float x, Float y) {
relativeX = x;
relativeY = y;
setLocation();
}
public void setLocation(Integer x, Integer y) {
this.locationX = x;
this.locationY = y;
this.dirty = true;
notifyParent(LOCATION_CHANGED);
setLocation();
}
public void setLocationX(Integer x) {
@@ -251,22 +277,20 @@ public class Visual {
}
}
private void repaint() {
this.paint(imageBuffer);
public void repaint() {
if(!this.active){
return;
}
if(this.dirty) {
this.revalidate();
return;
}
for(Visual v: children) {
v.repaint();
imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
}
}
public void revalidate() {
if(!this.active){
return;
}
private void revalidate() {
clearImageBuffer();
this.paint(imageBuffer);
for (Visual v : children) {
@@ -280,7 +304,7 @@ public class Visual {
}
Window window = (Window)this;
window.setFrameImageBuffer(imageBuffer);
window.repaint();
window.revalidate();
}
public void paint(BufferedImage imageBuffer) {
@@ -449,7 +473,6 @@ public class Visual {
private void activate() {
this.active = true;
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for(Visual child: children) {
child.activate();
}

View File

@@ -21,7 +21,6 @@ public class Window extends Visual {
private TitleBar titleBar;
private Panel mainPanel;
private Panel contentPanel;
private ResizeListener windowResizeListener;
private Point2d oldSize;
private Point2d oldLocation;
@@ -31,25 +30,7 @@ public class Window extends Visual {
this.setUndecorated(true);
this.addWindowStateListener(e -> {
this.setSize(getWidth(), getHeight());
revalidate();
});
Direction[] directions = {Direction.SOUTH, Direction.EAST, Direction.WEST};
windowResizeListener = new ResizeListener(directions, new Point2d(getWidth(), getHeight()), new Point2d(getLocationX(), getLocationY()));
this.addMouseListener(windowResizeListener);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent mouseEvent) {
setSize(windowResizeListener.size.x, windowResizeListener.size.y);
setLocation(windowResizeListener.location.x, windowResizeListener.location.y);
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
frame.setCursor(windowResizeListener.cursor);
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
frame.setCursor(windowResizeListener.cursor);
}
repaint();
});
this.mainPanel = new Panel();
@@ -77,9 +58,8 @@ public class Window extends Visual {
else {
contentPanel.setSize(width, height);
}
windowResizeListener.setSize(width, height);
mainPanel.setSize(width, height);
revalidate();
repaint();
}
public void setFrameImageBuffer(BufferedImage imageBuffer){
@@ -87,7 +67,7 @@ public class Window extends Visual {
this.frame.repaint();
}
public void repaint() {
public void revalidate() {
this.frame.repaint();
}
@@ -171,7 +151,6 @@ public class Window extends Visual {
public void setLocation(int x, int y) {
this.frame.setLocation(x, y);
windowResizeListener.setLocation(x, y);
}
public void setMainPanel(Panel panel) {