reworked some content panel logic and listener logic

This commit is contained in:
rmaco
2020-03-20 17:43:06 +02:00
parent 9fbfb6b3fb
commit f77e0b8c8a
8 changed files with 64 additions and 40 deletions

View File

@@ -1,7 +1,7 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<Window Name="window" Visible="true" Size="640, 480"> <Window Name="window" Visible="true" Size="640, 480">
<!-- <Panel Name="MainPanel" BackgroundColor="#FFFFFF" Size="1.0, 1.0" Overlapping="false">--> <!-- <Panel Name="MainPanel" BackgroundColor="#FFFFFF" Size="1.0, 1.0" Overlapping="false">-->
<Button Name="button1" BackgroundColor="#990000" Size="200, 200" Label="button1"> <Button Name="button1" BackgroundColor="#990000" Size="0.5, 0.5" Label="button1">
<Button Name="button4" BackgroundColor="#009999" Size="100, 100" Icon="square_white"> <Button Name="button4" BackgroundColor="#009999" Size="100, 100" Icon="square_white">
<Button Name="button2" BackgroundColor="#000099" Size="50, 50" Label="button2"/> <Button Name="button2" BackgroundColor="#000099" Size="50, 50" Label="button2"/>
</Button> </Button>

View File

@@ -29,6 +29,7 @@ public class Main {
}); });
window.revalidate(); window.revalidate();
System.out.println(Float.parseFloat("3"));
long now; long now;
long prev = 0; long prev = 0;
// while(true) { // while(true) {

View File

@@ -1,5 +1,7 @@
package converters; package converters;
import com.sun.jdi.InvalidTypeException;
public interface ConverterInterface<T> { public interface ConverterInterface<T> {
T convert(String content); T convert(String content) throws InvalidTypeException;
} }

View File

@@ -1,9 +1,15 @@
package converters; package converters;
import com.sun.jdi.InvalidTypeException;
public class FloatConverter implements ConverterInterface<Float> { public class FloatConverter implements ConverterInterface<Float> {
@Override @Override
public Float convert(String content) { public Float convert(String content) throws InvalidTypeException {
float number = Float.parseFloat(content);
if(number > 1 || number < 0) {
throw new InvalidTypeException();
}
return Float.parseFloat(content); return Float.parseFloat(content);
} }
} }

View File

@@ -86,11 +86,11 @@ public class Panel extends Visual {
int l2y = v2.getLocationY(); int l2y = v2.getLocationY();
int r2y = v2.getLocationY() + v2.getHeight(); int r2y = v2.getLocationY() + v2.getHeight();
if(l1x > r2x || l2x > r1x) { if(l1x >= r2x || l2x >= r1x) {
return false; return false;
} }
return l1y <= r2y && l2y <= r1y; return l1y < r2y && l2y < r1y;
} }
@Override @Override

View File

@@ -56,7 +56,7 @@ public class TitleBar extends Visual {
maximize.setForegroundColor(Color.LIGHT_GRAY); maximize.setForegroundColor(Color.LIGHT_GRAY);
minimize.setForegroundColor(Color.LIGHT_GRAY); minimize.setForegroundColor(Color.LIGHT_GRAY);
this.setSize(1, 30); this.setSize(0, 30);
this.setLocation(0, 0); this.setLocation(0, 0);
setButtonLocation(); setButtonLocation();
@@ -97,7 +97,6 @@ public class TitleBar extends Visual {
}); });
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
Getters Getters
---------------------------------------------------------------------*/ ---------------------------------------------------------------------*/

View File

@@ -331,7 +331,6 @@ public class Visual {
if(entered != null && entered.pressed){ if(entered != null && entered.pressed){
return; return;
} }
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX; int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY; int mouseY = mouseEvent.getY() - offsetY;
for(Visual v: children) { for(Visual v: children) {
@@ -339,18 +338,16 @@ public class Visual {
mouseY > v.getLocationY() && mouseY > v.getLocationY() &&
mouseX < v.getWidth() + v.getLocationX() && mouseX < v.getWidth() + v.getLocationX() &&
mouseY < v.getHeight() + v.getLocationY()){ mouseY < v.getHeight() + v.getLocationY()){
front = false;
v.mouseEntered(mouseEvent, offsetX + v.locationX, offsetY + v.locationY); v.mouseEntered(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
return;
} }
} }
if(front) {
entered = this; entered = this;
for(MouseListener mouseListener: mouseListeners) { for(MouseListener mouseListener: mouseListeners) {
mouseListener.mouseEntered(mouseEvent); mouseListener.mouseEntered(mouseEvent);
} }
dirty = true; dirty = true;
} }
}
void mouseExited(MouseEvent mouseEvent) { void mouseExited(MouseEvent mouseEvent) {
if(entered == null) { if(entered == null) {
@@ -377,7 +374,6 @@ public class Visual {
if(entered != null && entered.pressed){ if(entered != null && entered.pressed){
return; return;
} }
boolean front = true;
int mouseX = mouseEvent.getX() - offsetX; int mouseX = mouseEvent.getX() - offsetX;
int mouseY = mouseEvent.getY() - offsetY; int mouseY = mouseEvent.getY() - offsetY;
if(entered != null) { if(entered != null) {
@@ -390,12 +386,10 @@ public class Visual {
} }
for(Visual v: children) { for(Visual v: children) {
if(v.isInside(mouseX, mouseY)) { if(v.isInside(mouseX, mouseY)) {
front = false;
v.mouseMoved(mouseEvent, offsetX + v.locationX, offsetY + v.locationY); v.mouseMoved(mouseEvent, offsetX + v.locationX, offsetY + v.locationY);
return;
} }
} }
if(front) {
if(this.isInside(mouseEvent.getX(), mouseEvent.getY())) {
if (this != entered && entered != null) { if (this != entered && entered != null) {
for (MouseListener mouseListener : entered.mouseListeners) { for (MouseListener mouseListener : entered.mouseListeners) {
mouseListener.mouseExited(mouseEvent); mouseListener.mouseExited(mouseEvent);
@@ -410,10 +404,8 @@ public class Visual {
mouseListener.mouseMoved(mouseEvent); mouseListener.mouseMoved(mouseEvent);
} }
} }
}
dirty = true; dirty = true;
} }
}
void mouseWheelMoved(MouseWheelEvent mouseWheelEvent, int offsetX, int offsetY) { void mouseWheelMoved(MouseWheelEvent mouseWheelEvent, int offsetX, int offsetY) {
if(focused) { if(focused) {
@@ -427,7 +419,7 @@ public class Visual {
Helper Methods Helper Methods
---------------------------------------------------------------------*/ ---------------------------------------------------------------------*/
private void initializeImageBuffer(){ private void initializeImageBuffer(){
if(this.width == 0 || this.height == 0) { if(this.width <= 0 || this.height <= 0) {
return; return;
} }
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
@@ -435,6 +427,9 @@ public class Visual {
} }
private void clearImageBuffer() { private void clearImageBuffer() {
if(imageBuffer == null) {
return;
}
Graphics g = this.imageBuffer.getGraphics(); Graphics g = this.imageBuffer.getGraphics();
g.setColor(backgroundColor); g.setColor(backgroundColor);
g.fillRect(0, 0, width, height); g.fillRect(0, 0, width, height);

View File

@@ -20,6 +20,7 @@ public class Window extends Visual {
public CustomFrame frame; public CustomFrame frame;
private TitleBar titleBar; private TitleBar titleBar;
private Panel mainPanel; private Panel mainPanel;
private Panel contentPanel;
private ResizeListener windowResizeListener; private ResizeListener windowResizeListener;
private Point2d oldSize; private Point2d oldSize;
private Point2d oldLocation; private Point2d oldLocation;
@@ -52,7 +53,7 @@ public class Window extends Visual {
}); });
this.mainPanel = new Panel(); this.mainPanel = new Panel();
super.addVisual(mainPanel); this.setMainPanel(mainPanel);
BufferedImage icon = null; BufferedImage icon = null;
try { try {
@@ -69,8 +70,12 @@ public class Window extends Visual {
public void setSize(Integer width, Integer height) { public void setSize(Integer width, Integer height) {
this.frame.setSize(width, height); this.frame.setSize(width, height);
super.setSize(width, height); super.setSize(width, height);
if(this.titleBar != null) { if(titleBar != null) {
this.titleBar.setSize(this.getWidth(), titleBar.getHeight()); titleBar.setSize(this.getWidth(), titleBar.getHeight());
contentPanel.setSize(width, height - titleBar.getHeight());
}
else {
contentPanel.setSize(width, height);
} }
windowResizeListener.setSize(width, height); windowResizeListener.setSize(width, height);
mainPanel.setSize(width, height); mainPanel.setSize(width, height);
@@ -122,6 +127,9 @@ public class Window extends Visual {
} }
this.titleBar = titleBar; this.titleBar = titleBar;
titleBar.setLocation(0, 0);
contentPanel.setLocation(0, titleBar.getHeight());
contentPanel.setSize(mainPanel.getWidth(), mainPanel.getHeight() - titleBar.getHeight());
mainPanel.addVisual(titleBar); mainPanel.addVisual(titleBar);
this.titleBar.addMouseListener(new MouseAdapter() { this.titleBar.addMouseListener(new MouseAdapter() {
private int startX; private int startX;
@@ -168,7 +176,20 @@ public class Window extends Visual {
public void setMainPanel(Panel panel) { public void setMainPanel(Panel panel) {
this.removeVisual(mainPanel); this.removeVisual(mainPanel);
this.addVisual(panel); contentPanel = new Panel();
contentPanel.setName("ContentPanel");
if(titleBar != null) {
panel.addVisual(titleBar);
contentPanel.setLocation(0, titleBar.getHeight());
contentPanel.setSize(panel.getWidth(), panel.getHeight() - titleBar.getHeight());
}
else {
contentPanel.setLocation(0, 0);
contentPanel.setSize(panel.getWidth(), panel.getHeight());
}
panel.setName("MainPanel");
panel.addVisual(contentPanel);
super.addVisual(panel);
this.mainPanel = panel; this.mainPanel = panel;
} }
@@ -208,6 +229,6 @@ public class Window extends Visual {
@Override @Override
public void addVisual(Visual v) { public void addVisual(Visual v) {
mainPanel.addVisual(v); contentPanel.addVisual(v);
} }
} }