done panel with overlapping

This commit is contained in:
rmaco
2020-03-19 22:02:43 +02:00
parent c9a5a55618
commit 9fbfb6b3fb
12 changed files with 483 additions and 112 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 277 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 B

View File

@@ -1,10 +1,12 @@
<?xml version="1.0"?>
<Window Name="window" Visible="true" Size="640, 480">
<!-- <Panel Name="MainPanel" BackgroundColor="#FFFFFF" Size="1.0, 1.0" Overlapping="false">-->
<Button Name="button1" BackgroundColor="#990000" Size="200, 200" Label="button1">
<Button Name="button4" BackgroundColor="#009999" Size="100, 100" Icon="square_white">
<Button Name="button2" BackgroundColor="#000099" Size="50, 50" Label="button2"/>
</Button>
</Button>
<Button Name="button6" Location="350, 350" Size="100, 100"/>
<Button Name="button6" BackgroundColor="#555555" Location="350, 350" Size="100, 100"/>
<Button Name="button3" BackgroundColor="#009900" Size="100, 100" Location="300, 300" Icon="close_black"/>
<!-- </Panel>-->
</Window>

View File

@@ -1,15 +1,11 @@
import guiTree.Components.Button;
import guiTree.Components.TitleBar;
import guiTree.Components.Panel;
import guiTree.Window;
import guiTree.events.MouseAdapter;
import parser.XAMLParser;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.event.MouseEvent;
public class Main {
public static void main(String[] args) {
@@ -17,17 +13,20 @@ public class Main {
Window window = XAMLParser.parse("ui.xml");
assert window != null;
window.revalidate();
Button button = (Button)window.findByName("button1");
BufferedImage icon = null;
try {
icon = ImageIO.read(new File("resources\\icons\\square_white.png"));
} catch (IOException e) {
e.printStackTrace();
Button button = (Button)window.findByName("button3");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
Panel panel = window.getMainPanel();
if(panel.getOverlapping()) {
panel.setOverlapping(false);
}
TitleBar bar = new TitleBar("Working Title", icon);
bar.setBackgroundColor(Color.GRAY);
window.setTitleBar(bar);
else {
panel.setOverlapping(true);
}
window.revalidate();
}
});
window.revalidate();
long now;

View File

@@ -37,36 +37,30 @@ public class Button extends Visual {
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
System.out.println("Pressed: " + getName());
pressed = true;
revalidate();
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
System.out.println("Released: " + getName());
pressed = false;
revalidate();
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
System.out.println("Entered: " + getName());
hovered = true;
revalidate();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
System.out.println("Exited: " + getName());
hovered = false;
revalidate();
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
System.out.println("DRAGGING: " + getName());
revalidate();
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
System.out.println("Moved: " + getName());
revalidate();
}
});

View File

@@ -0,0 +1,107 @@
package guiTree.Components;
import guiTree.Visual;
import java.util.ArrayList;
import java.util.List;
public class Panel extends Visual {
List<Visual> visuals;
private Boolean overlapping;
public Panel() {
overlapping = false;
visuals = new ArrayList<>();
}
public void setOverlapping(Boolean overlapping) {
this.overlapping = overlapping;
reposition();
}
public Boolean getOverlapping() {
return this.overlapping;
}
private void calculateSize(Visual v) {
}
private void calculatePosition(Visual v) {
if(!overlapping) {
boolean ok = false;
while(!ok) {
ok = true;
for (int i = 0; i < visuals.size(); i++) {
Visual v2 = visuals.get(i);
if (isOverlapping(v, v2) && v != v2) {
System.out.println(v + " Overlapping with: " + v2);
if(v.getLocationX() + v.getWidth() + v2.getWidth() > this.getWidth()) {
if(v.getLocationY() + v.getHeight() + v2.getHeight() > this.getHeight()) {
break;
}
else {
v.setLocationY(v2.getLocationY() + v2.getHeight());
}
}
else {
v.setLocationX(v2.getLocationX() + v2.getWidth());
}
i = 0;
}
}
if(v.getLocationX() + v.getWidth() > this.getWidth()) {
ok = false;
v.setLocationY(v.getLocationY() + 10);
}
if(v.getLocationY() > this.getHeight()) {
v.setLocation(0, 0);
break;
}
}
}
}
private void reposition() {
for(int i = visuals.size() - 1; i >= 0; i--) {
calculatePosition(visuals.get(i));
}
}
@Override
public void addVisual(Visual v) {
calculatePosition(v);
super.addVisual(v);
visuals.add(v);
}
private Boolean isOverlapping(Visual v1, Visual v2) {
int l1x = v1.getLocationX();
int r1x = v1.getLocationX() + v1.getWidth();
int l1y = v1.getLocationY();
int r1y = v1.getLocationY() + v1.getHeight();
int l2x = v2.getLocationX();
int r2x = v2.getLocationX() + v2.getWidth();
int l2y = v2.getLocationY();
int r2y = v2.getLocationY() + v2.getHeight();
if(l1x > r2x || l2x > r1x) {
return false;
}
return l1y <= r2y && l2y <= r1y;
}
@Override
public void handleNotification(int notify) {
if(notify == TitleBar.CLOSE || notify == TitleBar.MAXIMIZE ||
notify == TitleBar.MINIMIZE || notify == TitleBar.NORMALIZE) {
notifyParent(notify);
// return;
}
// if(notify == SIZE_CHANGED || notify == LOCATION_CHANGED) {
// reposition();
// }
}
}

View File

@@ -1,6 +1,5 @@
package guiTree.Components;
import guiTree.Helper.Point2d;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
@@ -9,9 +8,10 @@ import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
public class TitleBar extends Visual {
public static final int MINIMIZE = 1;
public static final int MAXIMIZE = 2;
public static final int CLOSE = 3;
public static final int CLOSE = 10;
public static final int MINIMIZE = 11;
public static final int NORMALIZE = 12;
public static final int MAXIMIZE = 13;
private BufferedImage icon;
private String title;
@@ -60,22 +60,39 @@ public class TitleBar extends Visual {
this.setLocation(0, 0);
setButtonLocation();
this.addVisual(close);
this.addVisual(minimize);
this.addVisual(maximize);
minimize.setName("minimize");
maximize.setName("maximize");
close.setName("close");
close.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(3);
public void mouseReleased(MouseEvent mouseEvent) {
notifyParent(CLOSE);
}
});
maximize.addMouseListener(new MouseAdapter() {
private boolean isMaximized = false;
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(2);
public void mouseReleased(MouseEvent mouseEvent) {
if(isMaximized) {
notifyParent(NORMALIZE);
maximize.setIcon("square_white");
isMaximized = false;
}
else {
maximize.setIcon("normalize_white");
notifyParent(MAXIMIZE);
isMaximized = true;
}
}
});
minimize.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
notifyParent(1);
public void mouseReleased(MouseEvent mouseEvent) {
notifyParent(MINIMIZE);
}
});
}
@@ -134,15 +151,9 @@ public class TitleBar extends Visual {
private void setButtonLocation() {
int buttonOffset = this.getWidth() - close.getWidth();
close.setLocation(buttonOffset, 0);
close.setName("close");
buttonOffset -= maximize.getWidth();
maximize.setLocation(buttonOffset, 0);
maximize.setName("maximize");
buttonOffset -= minimize.getWidth();
minimize.setLocation(buttonOffset, 0);
minimize.setName("minimize");
this.addVisual(close);
this.addVisual(minimize);
this.addVisual(maximize);
}
}

View File

@@ -0,0 +1,9 @@
package guiTree.Listeners;
public enum Direction {
NORTH,
SOUTH,
WEST,
EAST,
ALL
}

View File

@@ -0,0 +1,186 @@
package guiTree.Listeners;
import guiTree.Helper.Point2d;
import guiTree.events.MouseAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;
public class ResizeListener extends MouseAdapter {
public Point2d location;
public Point2d size;
public Cursor cursor;
private Boolean moving = false;
private Boolean resizing = false;
private Point2d initialLocation;
private int resizeDelta = 5;
private Boolean NORTH;
private Boolean SOUTH;
private Boolean WEST;
private Boolean EAST;
public ResizeListener(Direction[] directions, Point2d size, Point2d location) {
this.cursor = new Cursor(Cursor.DEFAULT_CURSOR);
this.size = size;
this.location = location;
NORTH = false;
SOUTH = false;
WEST = false;
EAST = false;
for(Direction dir:directions) {
if(dir == Direction.NORTH) {
NORTH = true;
}
if(dir == Direction.SOUTH) {
SOUTH = true;
}
if(dir == Direction.WEST) {
WEST = true;
}
if(dir == Direction.EAST) {
EAST = true;
}
if(dir == Direction.ALL) {
NORTH = true;
SOUTH = true;
WEST = true;
EAST = true;
}
}
}
public void setLocation(int x, int y) {
location.x = x;
location.y = y;
}
public void setSize(int x, int y) {
size.x = x;
size.y = y;
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
this.cursor = new Cursor(Cursor.DEFAULT_CURSOR);
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
if(moving) {
setLocation(mouseEvent.getXOnScreen() - initialLocation.x, mouseEvent.getYOnScreen() - initialLocation.y);
}
else if(!resizing){
this.resizing = true;
this.resize(mouseEvent);
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
if(cursor.getType() != Cursor.DEFAULT_CURSOR){
this.moving = false;
}
else {
this.moving = true;
this.resizing = false;
}
initialLocation = new Point2d(mouseEvent.getX(), mouseEvent.getY());
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
moving = false;
}
@Override
public void mouseMoved(MouseEvent mouseEvent) {
if(mouseEvent.getX() < resizeDelta || mouseEvent.getX() > size.x - resizeDelta ||
mouseEvent.getY() < resizeDelta || mouseEvent.getY() > size.y - resizeDelta) {
this.setResizeCursor(mouseEvent);
}
else {
cursor = new Cursor(Cursor.DEFAULT_CURSOR);
}
}
private void setResizeCursor(MouseEvent e){
if(e.getX() <= resizeDelta) {
if(e.getY() <= resizeDelta && (NORTH && WEST)) {
cursor = new Cursor(Cursor.NW_RESIZE_CURSOR);
}
else if(e.getY() >= size.y - resizeDelta && (SOUTH && WEST)){
cursor = new Cursor(Cursor.SW_RESIZE_CURSOR);
}
else if(WEST){
cursor = new Cursor(Cursor.W_RESIZE_CURSOR);
}
}
else if(e.getX() >= size.x - resizeDelta){
if(e.getY() <= resizeDelta && (NORTH && EAST)){
cursor = new Cursor(Cursor.NE_RESIZE_CURSOR);
}
else if(e.getY() >= size.y - resizeDelta && (SOUTH && EAST)){
cursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
}
else if(EAST){
cursor = new Cursor(Cursor.E_RESIZE_CURSOR);
}
}
else{
if(e.getY() <= resizeDelta && (NORTH)){
cursor = new Cursor(Cursor.N_RESIZE_CURSOR);
}
else if(e.getY() >= size.y - resizeDelta && (SOUTH)){
cursor = new Cursor(Cursor.S_RESIZE_CURSOR);
}
}
}
private void resize(MouseEvent e) {
switch (cursor.getType()) {
case Cursor.N_RESIZE_CURSOR:
setSize(size.x, size.y + initialLocation.y - e.getYOnScreen());
setLocation(location.x, e.getYOnScreen());
initialLocation.y = e.getYOnScreen();
break;
case Cursor.NE_RESIZE_CURSOR:
setSize(size.x + e.getXOnScreen() - initialLocation.x, size.y + initialLocation.y - e.getYOnScreen());
setLocation(location.x, e.getYOnScreen());
initialLocation.x = e.getXOnScreen();
initialLocation.y = e.getYOnScreen();
break;
case Cursor.NW_RESIZE_CURSOR:
setSize(size.x + initialLocation.x - e.getXOnScreen(), size.y + initialLocation.y - e.getYOnScreen());
setLocation(e.getXOnScreen(), e.getYOnScreen());
initialLocation.x = e.getXOnScreen();
initialLocation.y = e.getYOnScreen();
break;
case Cursor.E_RESIZE_CURSOR:
setSize(size.x + e.getXOnScreen() - initialLocation.x, size.y);
initialLocation.x = e.getXOnScreen();
break;
case Cursor.W_RESIZE_CURSOR:
setSize(size.x + initialLocation.x - e.getXOnScreen(), size.y);
setLocation(e.getXOnScreen(), location.y);
initialLocation.x = e.getXOnScreen();
break;
case Cursor.SE_RESIZE_CURSOR:
setSize(size.x + e.getXOnScreen() - initialLocation.x, size.y + e.getYOnScreen() - initialLocation.y);
initialLocation.x = e.getXOnScreen();
initialLocation.y = e.getYOnScreen();
break;
case Cursor.S_RESIZE_CURSOR:
setSize(size.x, size.y + e.getYOnScreen() - initialLocation.y);
initialLocation.y = e.getYOnScreen();
break;
case Cursor.SW_RESIZE_CURSOR:
setSize(size.x + initialLocation.x - e.getXOnScreen(), size.y + e.getYOnScreen() - initialLocation.y);
setLocation(e.getXOnScreen(), location.y);
initialLocation.x = e.getXOnScreen();
initialLocation.y = e.getYOnScreen();
break;
}
this.resizing = false;
}
}

View File

@@ -12,6 +12,11 @@ import java.util.ArrayList;
import java.util.List;
public class Visual {
/*--------------------------------------------------------------------
Constant Values
---------------------------------------------------------------------*/
public static final int SIZE_CHANGED = 1;
public static final int LOCATION_CHANGED = 2;
/*--------------------------------------------------------------------
Tree Elements
@@ -57,6 +62,7 @@ public class Visual {
this.mouseListeners = new ArrayList<>();
this.keyListeners = new ArrayList<>();
this.parent = null;
this.name = "";
this.backgroundColor = Color.WHITE;
this.foregroundColor = Color.BLUE;
this.fontColor = Color.BLACK;
@@ -88,12 +94,27 @@ public class Visual {
initializeImageBuffer();
this.dirty = true;
this.notifyParent(SIZE_CHANGED);
}
// public void setSize(Float width, Float height) {
// this.width = Math.round(this.parent.width * width);
// this.height = Math.round(this.parent.height * height);
// }
public void setLocation(Integer x, Integer y) {
this.locationX = x;
this.locationY = y;
this.dirty = true;
notifyParent(LOCATION_CHANGED);
}
public void setLocationX(Integer x) {
this.locationX = x;
}
public void setLocationY(Integer y) {
this.locationY = y;
}
public void setBackgroundColor(Color backgroundColor) {
@@ -120,11 +141,11 @@ public class Visual {
}
private void calculateInitialLocation() {
if(this.locationX == 0) {
this.locationX = 1;
if(this.locationX <= 0) {
this.locationX = 0;
}
if(this.locationY == 0){
this.locationY = 1;
if(this.locationY <= 0){
this.locationY = 0;
}
}
@@ -185,15 +206,9 @@ public class Visual {
}
public void addVisual(Visual child) {
this.addVisual(child, -1, -1);
}
public void addVisual(Visual child, int x, int y) {
this.children.add(child);
child.setParent(this);
if(x == -1 && y == -1) {
child.calculateInitialLocation();
}
child.calculateInitialSize();
if(this.active) {
@@ -220,8 +235,10 @@ public class Visual {
}
public void notifyParent(int notify) {
if(parent != null) {
this.parent.handleNotification(notify);
}
}
private void repaint() {
this.paint(imageBuffer);
@@ -410,6 +427,9 @@ public class Visual {
Helper Methods
---------------------------------------------------------------------*/
private void initializeImageBuffer(){
if(this.width == 0 || this.height == 0) {
return;
}
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
clearImageBuffer();
}

View File

@@ -2,21 +2,29 @@ package guiTree;
import guiTree.Components.TitleBar;
import guiTree.Helper.Point2d;
import guiTree.Listeners.Direction;
import guiTree.Listeners.ResizeListener;
import guiTree.events.MouseAdapter;
import guiTree.Components.Panel;
import javax.tools.Tool;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Window extends Visual {
public CustomFrame frame;
private TitleBar titleBar;
private Panel mainPanel;
private ResizeListener windowResizeListener;
private Point2d oldSize;
private Point2d oldLocation;
public Window()
{
public Window() {
super();
this.frame = new CustomFrame(this);
this.setUndecorated(true);
@@ -24,16 +32,48 @@ public class Window extends Visual {
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);
}
});
this.mainPanel = new Panel();
super.addVisual(mainPanel);
BufferedImage icon = null;
try {
icon = ImageIO.read(new File("resources\\icons\\square_white.png"));
} catch (IOException e) {
e.printStackTrace();
}
TitleBar bar = new TitleBar("Working Title", icon);
bar.setBackgroundColor(Color.GRAY);
this.setTitleBar(bar);
}
@Override
public void setSize(Integer width, Integer height)
{
public void setSize(Integer width, Integer height) {
this.frame.setSize(width, height);
super.setSize(width, height);
if(this.titleBar != null) {
this.titleBar.setSize(this.getWidth(), titleBar.getHeight());
}
windowResizeListener.setSize(width, height);
mainPanel.setSize(width, height);
revalidate();
}
@@ -78,12 +118,25 @@ public class Window extends Visual {
titleBar.setSize(this.getWidth(), titleBar.getHeight());
if(this.getTitleBar() != null) {
this.removeVisual(this.titleBar);
mainPanel.removeVisual(this.titleBar);
}
this.titleBar = titleBar;
this.addVisual(titleBar, 0, 0);
this.titleBar.addMouseListener(new TitleBarMouseListener());
mainPanel.addVisual(titleBar);
this.titleBar.addMouseListener(new MouseAdapter() {
private int startX;
private int startY;
@Override
public void mousePressed(MouseEvent mouseEvent) {
startX = mouseEvent.getX();
startY = mouseEvent.getY();
}
@Override
public void mouseDragged(MouseEvent mouseEvent) {
setLocation(mouseEvent.getXOnScreen() - startX, mouseEvent.getYOnScreen() - startY);
}
});
}
public TitleBar getTitleBar() {
@@ -104,66 +157,57 @@ public class Window extends Visual {
frame.setState(state);
}
private void moveTo(int x, int y) {
public int getState() {
return frame.getState();
}
public void setLocation(int x, int y) {
this.frame.setLocation(x, y);
windowResizeListener.setLocation(x, y);
}
public void setMainPanel(Panel panel) {
this.removeVisual(mainPanel);
this.addVisual(panel);
this.mainPanel = panel;
}
public Panel getMainPanel() {
return this.mainPanel;
}
@Override
public void handleNotification(int notify) {
switch(notify) {
case TitleBar.CLOSE:
case TitleBar.CLOSE: {
dispose();
break;
case TitleBar.MINIMIZE:
}
case TitleBar.MINIMIZE: {
setState(Frame.ICONIFIED);
break;
case TitleBar.MAXIMIZE:
}
case TitleBar.MAXIMIZE: {
Rectangle screenBounds = frame.getGraphicsConfiguration().getBounds();
oldSize = new Point2d(getWidth(), getHeight());
oldLocation = new Point2d(frame.getX(), frame.getY());
this.setSize(screenBounds.width, screenBounds.height);
this.moveTo(screenBounds.x, screenBounds.y);
this.setLocation(screenBounds.x, screenBounds.y);
setState(Frame.MAXIMIZED_BOTH);
break;
}
case TitleBar.NORMALIZE: {
Rectangle screenBounds = frame.getGraphicsConfiguration().getBounds();
this.setSize(oldSize.x, oldSize.y);
this.setLocation(oldLocation.x, oldLocation.y);
setState(Frame.NORMAL);
break;
}
private class WindowMouseListener extends MouseAdapter{
private Boolean resizing;
private Boolean moving;
private Point2d initialLocation;
@Override
public void mousePressed(MouseEvent mouseEvent) {
this.initialLocation = new Point2d(mouseEvent.getX(), mouseEvent.getY());
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
moving = false;
resizing = false;
}
}
private class TitleBarMouseListener extends MouseAdapter {
private Boolean moving = false;
private Point2d initialLocation;
@Override
public void mouseDragged(MouseEvent mouseEvent) {
if(moving) {
moveTo(mouseEvent.getXOnScreen() - initialLocation.x, mouseEvent.getYOnScreen() - initialLocation.y);
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
moving = true;
initialLocation = new Point2d(mouseEvent.getX(), mouseEvent.getY());
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
moving = false;
}
public void addVisual(Visual v) {
mainPanel.addVisual(v);
}
}

View File

@@ -143,7 +143,6 @@ public class XAMLParser {
if(parentObject instanceof Visual && childObject instanceof Visual) {
addVisual((Visual) parentObject, (Visual) childObject);
}
System.out.println("\nCurrent Element :" + childNode.getNodeName());
}
}
return parentObject;