fixed rest of animations

fixed drop downs and side dropdowns
made isInside public to let element decide
finished input text box
This commit is contained in:
Macocian Adrian Radu
2020-05-25 20:40:51 +03:00
parent 970734f20f
commit 264ca35ad0
11 changed files with 466 additions and 378 deletions

View File

@@ -39,6 +39,14 @@
Icon="circle"
/>
</RadioButtons>
<InputTextBox
BackgroundColor="#999999"
AccentColor="#aaaaaa"
ForegroundColor="#ffffff"
Location="0.0f, 0.8f"
Size="300, 60"/>
<DropDown
BackgroundColor="#222222"
Name="DropDown"
@@ -68,13 +76,22 @@
Label="Button4"/>
</SideDropDown>
<SideDropDown
Name="SideDropDown"
Label="SideDropDown"
ClosedWidth="100"
ClosedHeight="30"
ContentHeight="30"
ContentWidth="100">
<Button
Name="Button5"
Label="Button5"/>
<Button
Name="Button6"
Label="Button6"/>
</SideDropDown>
</DropDown>
<InputTextBox
BackgroundColor="#999999"
AccentColor="#aaaaaa"
ForegroundColor="#ffffff"
Location="0.0f, 0.8f"
Size="300, 60"/>
</Panel>
</GridPanel>

View File

@@ -32,10 +32,10 @@ public class ColorAnimation implements AnimationInterface {
@Override
public boolean step() {
if(from.a > to.a - 1 && from.a < to.a + 1 &&
from.b > to.b - 1 && from.b < to.b + 1 &&
from.c > to.c - 1 && from.c < to.c + 1 &&
from.d > to.d - 1 && from.d < to.d + 1) {
if(((from.a + offset.a >= to.a && offset.a >= 0) || (from.a + offset.a <= to.a && offset.a <= 0)) &&
((from.b + offset.b >= to.b && offset.b >= 0) || (from.b + offset.b <= to.b && offset.b <= 0)) &&
((from.c + offset.c >= to.c && offset.c >= 0) || (from.c + offset.c <= to.c && offset.c <= 0)) &&
((from.d + offset.d >= to.d && offset.d >= 0) || (from.d + offset.d <= to.d && offset.d <= 0))) {
element.setPaintColor(new Color(to.a, to.b, to.c, to.d));
Debugger.log("Animation for " + element.getName() + " finished", Debugger.Tag.ANIMATIONS);
return true;

View File

@@ -20,7 +20,8 @@ public class LocationAnimation implements AnimationInterface {
@Override
public boolean step() {
if(from.x > to.x - 1 && from.x < to.x + 1 && from.y > to.y - 1 && from.y < to.y + 1) {
if(((from.x + offset.x >= to.x && offset.x >= 0) || (from.x + offset.x <= to.x && offset.x <= 0)) &&
((from.y + offset.y >= to.y && offset.y >= 0) || (from.y + offset.y <= to.y && offset.y <= 0))) {
element.setLocation(to.x, to.y);
Debugger.log("Animation for " + element.getName() + " finished", Debugger.Tag.ANIMATIONS);
return true;

View File

@@ -38,6 +38,11 @@ public class Border extends Visual {
update();
}
@Override
public boolean isInside(int x, int y) {
return false;
}
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();

View File

@@ -161,24 +161,4 @@ public class Button extends MenuItem {
public void setOpenedSize(Integer width, Integer height) {
setSize(width, height);
}
@Override
public int getClosedWidth() {
return getWidth();
}
@Override
public int getClosedHeight() {
return getHeight();
}
@Override
public int getOpenedWidth() {
return getWidth();
}
@Override
public int getOpenedHeight() {
return getHeight();
}
}

View File

@@ -1,18 +1,24 @@
package guiTree.Components;
import guiTree.Animations.ColorAnimation;
import guiTree.Animations.SizeAnimation;
import guiTree.Helper.Debugger;
import guiTree.Helper.Point2;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
import javafx.css.Size;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DropDown extends ToggleButton implements Menu{
public class DropDown extends MenuItem implements Menu{
private String label;
private BufferedImage icon;
private List<MenuItem> items;
private boolean isOpen;
private boolean elementWidthSet;
@@ -34,12 +40,32 @@ public class DropDown extends ToggleButton implements Menu{
openedSize = new Point2<>(getWidth(), getHeight());
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent mouseEvent) {
if(!isOpen) {
addAnimation(new ColorAnimation(DropDown.this, getBackgroundColor(), getAccentColor(), 100));
Debugger.log("Calling repaint from entered: " + getName(), Debugger.Tag.PAINTING);
update();
}
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
if(!isOpen) {
addAnimation(new ColorAnimation(DropDown.this, getAccentColor(), getBackgroundColor(), 100));
Debugger.log("Calling repaint from exited: " + getName(), Debugger.Tag.PAINTING);
update();
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
if(isOpen) {
addAnimation(new ColorAnimation(DropDown.this, getForegroundColor(), getAccentColor(), 100));
close();
return;
}
addAnimation(new ColorAnimation(DropDown.this, getAccentColor(), getForegroundColor(), 100));
open();
}
});
@@ -53,8 +79,8 @@ public class DropDown extends ToggleButton implements Menu{
}
if(!elementWidthSet) {
if(elementWidth < ((MenuItem) v).getClosedWidth()) {
setElementWidth(((MenuItem) v).getClosedWidth());
if(elementWidth < ((MenuItem) v).getClosedSize().x) {
setElementWidth(((MenuItem) v).getClosedSize().x);
}
}
else {
@@ -66,16 +92,16 @@ public class DropDown extends ToggleButton implements Menu{
}
v.setLocationX(0);
openedSize.x = Math.max(openedSize.x, ((MenuItem) v).getOpenedWidth());
openedSize.x = Math.max(openedSize.x, ((MenuItem) v).getOpenedSize().x);
if(items.size() == 0) {
v.setLocationY(getHeight());
openedSize.y = closedSize.y + ((MenuItem) v).getOpenedHeight();
openedSize.y = closedSize.y + ((MenuItem) v).getOpenedSize().y;
items.add((MenuItem) v);
return;
}
v.setLocationY(items.get(items.size() - 1).getLocationY() + items.get(items.size() - 1).getClosedHeight());
openedSize.y = Math.max(openedSize.y, v.getLocationY() + ((MenuItem) v).getOpenedHeight());
v.setLocationY(items.get(items.size() - 1).getLocationY() + items.get(items.size() - 1).getClosedSize().y);
openedSize.y = Math.max(openedSize.y, v.getLocationY() + ((MenuItem) v).getOpenedSize().y);
items.add((MenuItem)v);
}
@@ -97,8 +123,8 @@ public class DropDown extends ToggleButton implements Menu{
openedSize.y -= v.getHeight();
if(!elementWidthSet) {
if(((MenuItem) v).getClosedWidth() == elementWidth) {
setElementWidth(items.stream().mapToInt(MenuItem::getClosedWidth).max().orElse(0));
if(((MenuItem) v).getClosedSize().x == elementWidth) {
setElementWidth(items.stream().mapToInt(f -> f.getClosedSize().x).max().orElse(0));
}
}
@@ -107,11 +133,27 @@ public class DropDown extends ToggleButton implements Menu{
}
}
@Override
public Point2<Integer> getClosedSize() {
return closedSize;
}
@Override
public Point2<Integer> getOpenedSize() {
return openedSize;
}
public void setClosedSize(Integer width, Integer height) {
setClosedWidth(width);
setClosedHeight(height);
}
@Override
public void setOpenedSize(Integer width, Integer height) {
openedSize.x = width;
openedSize.y = height;
}
public void setClosedHeight(Integer height) {
closedSize.y = height;
if(openedSize.y < height) {
@@ -166,60 +208,46 @@ public class DropDown extends ToggleButton implements Menu{
}
public void open() {
System.out.println("Opening");
isOpen = true;
items.forEach(super::addVisual);
addAnimation(new SizeAnimation(this, closedSize, openedSize, 70));
addAnimation(new SizeAnimation(this, new Point2<>(getWidth(), getHeight()), openedSize, 70));
}
public void close() {
System.out.println("Closing");
isOpen = false;
items.forEach(super::removeVisual);
addAnimation(new SizeAnimation(this, openedSize, closedSize, 70));
addAnimation(new SizeAnimation(this, new Point2<>(getWidth(), getHeight()), closedSize, 70));
}
// public void open() {
// int width = elementWidth;
// int height = items.stream().mapToInt(f -> f.getOpenedHeight() + f.getLocationY()).max().orElse(0);
//
// if(width == 0) {
// width = items.stream().mapToInt(MenuItem::getClosedWidth).max().orElse(0);
// }
// int finalWidth = width;
// items.forEach(f -> f.setClosedWidth(finalWidth));
// int openedWidth = items.stream().mapToInt(MenuItem::getOpenedWidth).max().orElse(0);
//
// addAnimation(new SizeAnimation(this, new Point2<>(getWidth(), getHeight()), new Point2<>(Math.max(width, openedWidth), height), 70));
// for(Visual v: items) {
// super.addVisual(v);
// }
// isOpen = true;
// }
//
// public void close() {
// addAnimation(new SizeAnimation(this, new Point2<>(getWidth(), getHeight()), new Point2<>(closedWidth, closedHeight), 70));
// isOpen = false;
// }
public String getLabel() {
return label;
}
// @Override
// public void handleNotification(Visual v, int notify) {
// assert v instanceof MenuItem;
// MenuItem item = (MenuItem) v;
// if(notify == SIZE_CHANGED) {
// if(isOpen) {
// if(elementWidthSet) {
// item.setClosedWidth(elementWidth);
// }
// else {
// if(item.getClosedWidth() > elementWidth) {
// setElementWidth(item.getClosedWidth());
// }
// }
// if(elementHeightSet) {
// item.setClosedHeight(elementHeight);
// }
// }
// }
// }
public void setLabel(String label) {
this.label = label;
}
public void setIcon(BufferedImage icon) {
this.icon = icon;
}
public boolean isOpen() {
return isOpen;
}
public void setIcon(String url) {
try{
icon = ImageIO.read(new File("resources\\icons\\" + url + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getIcon() {
return icon;
}
public void paint(BufferedImage imageBuffer)
{
@@ -238,24 +266,25 @@ public class DropDown extends ToggleButton implements Menu{
g.setColor(this.getFontColor());
int textWidth = 0;
int textHeight = 0;
if(!getLabel().equals("")) {
textWidth = g.getFontMetrics().stringWidth(getLabel());
textHeight = g.getFontMetrics().getHeight();
}
g.drawString(getLabel(), (getWidth() - textWidth)/2, (closedSize.y + textHeight)/2);
//Draw Icon
if(getIcon() != null) {
int iconWidth = getIcon().getWidth();
int iconHeight = getIcon().getHeight();
if(icon != null) {
int iconWidth = icon.getWidth();
int iconHeight = icon.getHeight();
textWidth += iconWidth;
int iconX = (getWidth() - iconWidth - textWidth) / 2;
int iconX = closedSize.x - iconWidth - 3;
int iconY = (closedSize.y - iconHeight - textHeight) / 2;
Graphics2D g2 = imageBuffer.createGraphics();
g2.drawImage(getIcon(), iconX, iconY, null);
g2.drawImage(icon, iconX, iconY, null);
g2.dispose();
}
if(!label.equals("")) {
textWidth += g.getFontMetrics().stringWidth(label);
textHeight = g.getFontMetrics().getHeight();
g.drawString(label, (closedSize.x - textWidth)/2, closedSize.y/2 + textHeight/2);
}
g.dispose();
}

View File

@@ -2,22 +2,30 @@ package guiTree.Components;
import guiTree.Animations.ColorAnimation;
import guiTree.Helper.Point2;
import guiTree.Helper.Point4;
import guiTree.Visual;
import guiTree.events.KeyAdapter;
import guiTree.events.MouseAdapter;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class InputTextBox extends Visual {
private boolean visible;
private List<StringBuilder> lines;
private String title;
private Point2<Integer> caretPosition;
private Point2<Integer> startDragPosition;
private List<StringBuilder> selectedText;
private List<Point4<Integer>> selectionRectangles;
private FontMetrics fontMetrics;
private int paragraphSpacing;
@@ -36,17 +44,26 @@ public class InputTextBox extends Visual {
public InputTextBox(Boolean visible, String title) {
super();
this.title = title;
this.visible = visible;
lines = new ArrayList<>();
selectionRectangles = new ArrayList<>();
lines.add(new StringBuilder());
caretPosition = new Point2<>(0, 0);
startDragPosition = new Point2<>(0, 0);
paragraphSpacing = 1;
selectedText = new ArrayList<>();
addMouseListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent mouseEvent) {
caretPosition = getCaretPosition(mouseEvent.getX(), mouseEvent.getY());
setSelection();
update();
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
caretPosition = getCaretPosition(mouseEvent.getX(), mouseEvent.getY());
System.out.println("Caret Position: " + caretPosition);
startDragPosition = new Point2<>(caretPosition.x, caretPosition.y);
setSelection();
addAnimation(new ColorAnimation(InputTextBox.this, getAccentColor(), getForegroundColor(), 100));
}
@Override
@@ -71,7 +88,12 @@ public class InputTextBox extends Visual {
caretPosition.x = lines.get(caretPosition.y - 1).length();
}
caretPosition.y --;
if(!keyEvent.isShiftDown()) {
startDragPosition = new Point2<>(caretPosition);
}
setSelection();
update();
return;
}
}
@@ -81,7 +103,12 @@ public class InputTextBox extends Visual {
caretPosition.x = lines.get(caretPosition.y + 1).length();
}
caretPosition.y ++;
if(!keyEvent.isShiftDown()) {
startDragPosition = new Point2<>(caretPosition);
}
setSelection();
update();
return;
}
}
@@ -90,37 +117,87 @@ public class InputTextBox extends Visual {
if(caretPosition.y > 0) {
caretPosition.y --;
caretPosition.x = lines.get(caretPosition.y).length();
if(!keyEvent.isShiftDown()) {
startDragPosition = new Point2<>(caretPosition);
}
setSelection();
update();
}
return;
}
caretPosition.x --;
if(!keyEvent.isShiftDown()) {
startDragPosition = new Point2<>(caretPosition);
}
setSelection();
update();
return;
}
if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
if(caretPosition.x == lines.get(caretPosition.y).length()){
if(caretPosition.y + 1 < lines.size()) {
caretPosition.y++;
caretPosition.x = 0;
if(!keyEvent.isShiftDown()) {
startDragPosition = new Point2<>(caretPosition);
}
setSelection();
update();
}
return;
}
caretPosition.x ++;
if(!keyEvent.isShiftDown()) {
startDragPosition = new Point2<>(caretPosition);
}
setSelection();
update();
return;
}
if(keyEvent.getKeyCode() == KeyEvent.VK_C) {
if(keyEvent.isControlDown()) {
copyToClipboard();
return;
}
}
if(keyEvent.getKeyCode() == KeyEvent.VK_X) {
if(keyEvent.isControlDown()) {
cutToClipboard();
update();
return;
}
}
if(keyEvent.getKeyCode() == KeyEvent.VK_V) {
if(keyEvent.isControlDown()) {
pasteFromClipboard();
update();
return;
}
}
if(keyEvent.getKeyCode() == KeyEvent.VK_A) {
if(keyEvent.isControlDown()) {
selectAll();
update();
}
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
deleteSelection();
StringBuilder currentLine = lines.get(caretPosition.y);
if(keyEvent.getKeyChar() == '\n') {
caretPosition.y++;
lines.add(caretPosition.y, new StringBuilder(currentLine.substring(caretPosition.x)));
lines.set(caretPosition.y - 1, new StringBuilder(currentLine.substring(0, caretPosition.x)));
caretPosition.x = 0;
startDragPosition = new Point2<>(caretPosition);
update();
return;
}
@@ -140,12 +217,18 @@ public class InputTextBox extends Visual {
currentLine.deleteCharAt(caretPosition.x - 1);
lines.set(caretPosition.y, currentLine);
caretPosition.x --;
startDragPosition = new Point2<>(caretPosition);
update();
return;
}
if(keyEvent.isControlDown()){
return;
}
lines.set(caretPosition.y, lines.get(caretPosition.y).insert(caretPosition.x, (Object)keyEvent.getKeyChar()));
caretPosition.x ++;
startDragPosition = new Point2<>(caretPosition);
update();
}
});
@@ -157,6 +240,61 @@ public class InputTextBox extends Visual {
fontMetrics = null;
}
private void copyToClipboard() {
StringBuilder clipboardText = new StringBuilder();
for(StringBuilder line: selectedText) {
clipboardText.append(line).append('\n');
}
StringSelection stringSelection = new StringSelection(clipboardText.toString());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
private void cutToClipboard() {
copyToClipboard();
deleteSelection();
}
private void selectAll() {
caretPosition.x = lines.get(lines.size() - 1).length();
caretPosition.y = lines.size() - 1;
startDragPosition.x = 0;
startDragPosition.y = 0;
setSelection();
}
private void pasteFromClipboard() {
Transferable clipboardTransferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this);
String clipboardText = null;
try {
clipboardText = (String) clipboardTransferable.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
if(clipboardText == null) {
return;
}
clipboardText = clipboardText.substring(0, clipboardText.length() -1 );
if(clipboardText.indexOf('\n') == -1) {
lines.get(caretPosition.y).insert(caretPosition.x, clipboardText);
caretPosition.x += clipboardText.length();
startDragPosition = caretPosition;
}
}
private Point2<Integer> getPositionOnScreen(int x, int y){
String currentLine = lines.get(y).toString();
y *= fontMetrics.getHeight();
y += paragraphSpacing;
int width = 0;
for(int i = 1; i <= currentLine.length(); i++) {
width += fontMetrics.charWidth(currentLine.charAt(i - 1));
if(x == i) {
return new Point2<>(width, y);
}
}
return new Point2<>(0, y);
}
private Point2<Integer> getCaretPosition(int x, int y) {
y -= lines.size() * paragraphSpacing;
y /= fontMetrics.getHeight();
@@ -173,11 +311,104 @@ public class InputTextBox extends Visual {
return new Point2<>(currentLine.length(), y);
}
private void deleteSelection() {
selectionRectangles.clear();
selectedText.clear();
if(caretPosition.equals(startDragPosition)) {
return;
}
Point2<Integer> selectionStart;
Point2<Integer> selectionEnd;
if(caretPosition.compareTo(startDragPosition) < 0) {
selectionStart = new Point2<>(caretPosition);
selectionEnd = new Point2<>(startDragPosition);
}
else {
selectionStart = new Point2<>(startDragPosition);
selectionEnd = new Point2<>(caretPosition);
}
caretPosition = selectionStart;
if(selectionStart.y.equals(selectionEnd.y)) {
StringBuilder currentLine = lines.get(selectionStart.y);
int lineLength = currentLine.length();
int newLength = lineLength - selectionEnd.x + selectionStart.x;
currentLine.insert(selectionStart.x, currentLine.substring(selectionEnd.x));
currentLine = new StringBuilder(currentLine.substring(0, newLength));
lines.set(selectionStart.y, currentLine);
return;
}
while(selectionStart.y + 1 < selectionEnd.y) {
lines.remove(selectionStart.y + 1);
selectionEnd.y--;
}
StringBuilder currentLine = lines.get(selectionStart.y);
currentLine.insert(selectionStart.x, lines.get(selectionEnd.y).substring(selectionEnd.x));
currentLine = new StringBuilder(currentLine.substring(0, selectionStart.x + lines.get(selectionEnd.y).substring(selectionEnd.x).length()));
lines.remove((int)selectionEnd.y);
lines.set(selectionStart.y, currentLine);
}
private void setSelection() {
selectedText.clear();
selectionRectangles.clear();
if(caretPosition.equals(startDragPosition)) {
return;
}
Point2<Integer> selectionStart;
Point2<Integer> selectionEnd;
if(caretPosition.compareTo(startDragPosition) < 0) {
selectionStart = new Point2<>(caretPosition);
selectionEnd = new Point2<>(startDragPosition);
}
else {
selectionStart = new Point2<>(startDragPosition);
selectionEnd = new Point2<>(caretPosition);
}
if(selectionEnd.y.equals(selectionStart.y)) {
selectedText.add(new StringBuilder());
Point2<Integer> startRect = getPositionOnScreen(selectionStart.x, selectionStart.y);
Point2<Integer> endRect = getPositionOnScreen(selectionEnd.x, selectionEnd.y);
selectionRectangles.add(new Point4<>(startRect.x, startRect.y + 3, endRect.x, endRect.y + fontMetrics.getHeight() + 3));
StringBuilder currentLine = lines.get(selectionStart.y);
for(int x = selectionStart.x; x < selectionEnd.x; x++) {
selectedText.get(0).insert(selectedText.get(0).length(), currentLine.charAt(x));
}
return;
}
for(int y = selectionStart.y; y < selectionEnd.y; y++) {
StringBuilder currentLine = lines.get(y);
Point2<Integer> startRect = getPositionOnScreen(selectionStart.x, y);
Point2<Integer> endRect = getPositionOnScreen(currentLine.length(), y);
selectionRectangles.add(new Point4<>(startRect.x, startRect.y + 3, endRect.x, endRect.y + fontMetrics.getHeight() + 3));
selectedText.add(new StringBuilder());
int selectionIndex = 0;
for(int x = selectionStart.x; x < currentLine.length(); x++) {
selectedText.get(selectedText.size() - 1).insert(selectionIndex++, currentLine.charAt(x));
}
selectionStart.x = 0;
}
StringBuilder currentLine = lines.get(selectionEnd.y);
selectedText.add(new StringBuilder());
Point2<Integer> startRect = getPositionOnScreen(selectionStart.x, selectionEnd.y);
Point2<Integer> endRect = getPositionOnScreen(selectionEnd.x, selectionEnd.y);
selectionRectangles.add(new Point4<>(startRect.x, startRect.y + 3, endRect.x, endRect.y + fontMetrics.getHeight() + 3));
int selectionIndex = 0;
for(int x = 0; x < selectionEnd.x; x++) {
selectedText.get(selectedText.size() - 1).insert(selectionIndex++, currentLine.charAt(x));
}
}
@Override
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
System.out.println("Caret: " + caretPosition);
g.setColor(getPaintColor());
g.fillRect(0, 0, getWidth(), getHeight());
@@ -186,6 +417,11 @@ public class InputTextBox extends Visual {
return;
}
g.setColor(Color.BLUE);
for(Point4<Integer> rect: selectionRectangles) {
g.fillRect(rect.a, rect.b, rect.c - rect.a, rect.d - rect.b);
}
int y = fontMetrics.getHeight();
g.setColor(getFontColor());
for(StringBuilder line: lines) {
@@ -194,9 +430,10 @@ public class InputTextBox extends Visual {
for(int i = 0; i < caretPosition.x; i++) {
x += fontMetrics.charWidth(line.charAt(i));
}
g.setColor(Color.BLACK);
g.drawLine(x, y - fontMetrics.getHeight() + 3, x, y + 3);
}
// g.drawString(line.toString(), 0, fontMetrics.getHeight() * (lines.indexOf(line) + 1));
g.setColor(getFontColor());
g.drawString(line.toString(), 0, y);
y += fontMetrics.getHeight();
}

View File

@@ -12,27 +12,19 @@ public abstract class MenuItem extends Visual {
public abstract void setOpenedSize(Integer width, Integer height);
public abstract int getClosedWidth();
public abstract int getClosedHeight();
public abstract int getOpenedWidth();
public abstract int getOpenedHeight();
public void setClosedWidth(Integer width) {
setClosedSize(width, getClosedHeight());
setClosedSize(width, getClosedSize().y);
}
public void setClosedHeight(Integer height) {
setClosedSize(getClosedWidth(), height);
setClosedSize(getClosedSize().x, height);
}
public void setOpenedWidth(Integer width) {
setOpenedSize(width, getOpenedHeight());
setOpenedSize(width, getOpenedSize().y);
}
public void setOpenedHeight(Integer height) {
setOpenedSize(getOpenedWidth(), height);
setOpenedSize(getOpenedSize().x, height);
}
}

View File

@@ -1,284 +1,78 @@
package guiTree.Components;
import guiTree.Animations.ColorAnimation;
import guiTree.Animations.SizeAnimation;
import guiTree.Helper.Debugger;
import guiTree.Helper.Point2;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SideDropDown extends MenuItem implements Menu {
private List<MenuItem> items;
private boolean isOpen;
private boolean elementHeightSet;
private boolean elementWidthSet;
private int elementHeight;
private int elementWidth;
private Point2<Integer> closedSize;
private Point2<Integer> openedSize;
private String label;
private BufferedImage icon;
public class SideDropDown extends DropDown implements Menu {
private int elementCount;
public SideDropDown() {
items = new ArrayList<>();
isOpen = false;
elementHeight = 0;
elementWidth = 0;
closedSize = new Point2<>(0, 0);
openedSize = new Point2<>(0, 0);
super();
setIcon("arrow_right_black");
elementCount = 0;
removeAllMouseListeners();
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent mouseEvent) {
addAnimation(new ColorAnimation(SideDropDown.this, getBackgroundColor(), getForegroundColor(), 100));
if(!isOpen()) {
open();
update();
Debugger.log("Calling repaint from entered: " + getName(), Debugger.Tag.PAINTING);
addAnimation(new ColorAnimation(SideDropDown.this, getBackgroundColor(), getForegroundColor(), 70));
}
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
addAnimation(new ColorAnimation(SideDropDown.this, getForegroundColor(), getBackgroundColor(), 100));
if(isInside(mouseEvent.getXOnScreen(), mouseEvent.getYOnScreen())) {
return;
}
System.out.println("Exited somehow");
close();
update();
Debugger.log("Calling repaint from exited: " + getName(), Debugger.Tag.PAINTING);
addAnimation(new ColorAnimation(SideDropDown.this, getForegroundColor(), getBackgroundColor(), 70));
}
});
}
public void addVisual(Visual v) {
if(!(v instanceof MenuItem)) {
System.err.println("Trying to add incompatible type to menu");
return;
}
if(!elementWidthSet) {
if(elementWidth < ((MenuItem) v).getClosedWidth()) {
setElementWidth(((MenuItem) v).getClosedWidth());
}
}
else {
((MenuItem) v).setClosedWidth(elementWidth);
}
if(elementHeightSet) {
((MenuItem) v).setClosedHeight(elementHeight);
}
v.setLocationX(closedSize.x);
if(items.size() == 0) {
v.setLocationY(0);
openedSize.y = closedSize.y + ((MenuItem) v).getOpenedHeight();
items.add((MenuItem) v);
return;
}
v.setLocationY(items.get(items.size() - 1).getLocationY() + items.get(items.size() - 1).getClosedHeight());
openedSize.y = Math.max(openedSize.y, v.getLocationY() + ((MenuItem) v).getOpenedHeight());
openedSize.x = Math.max(openedSize.x, closedSize.x + ((MenuItem) v).getOpenedWidth());
items.add((MenuItem)v);
}
@Override
public void removeVisual(Visual v) {
if(!(v instanceof MenuItem)) {
return;
}
if(!items.contains(v)) {
return;
}
int removeIndex = items.indexOf(v);
for(int i = removeIndex; i < items.size(); i++) {
items.get(i).setLocationY(items.get(i).getLocationY() - v.getHeight());
}
items.remove(v);
if(isOpen) {
super.removeVisual(v);
}
}
@Override
public Point2<Integer> getClosedSize() {
return closedSize;
}
@Override
public Point2<Integer> getOpenedSize() {
return openedSize;
}
public void setClosedSize(Integer width, Integer height) {
setClosedWidth(width);
setClosedHeight(height);
}
@Override
public void setOpenedSize(Integer width, Integer height) {
setOpenedHeight(height);
setOpenedWidth(width);
}
@Override
public int getClosedWidth() {
return closedSize.x;
}
@Override
public int getClosedHeight() {
return closedSize.y;
}
@Override
public int getOpenedWidth() {
return openedSize.x;
}
@Override
public int getOpenedHeight() {
return openedSize.y;
}
public void setClosedHeight(Integer height) {
closedSize.y = height;
if(openedSize.y < height) {
openedSize.y = height;
}
if(!isOpen) {
setHeight(height);
}
}
public void setClosedWidth(Integer width) {
closedSize.x = width;
if(openedSize.x < width) {
openedSize.x = width;
}
if(!isOpen) {
setWidth(width);
}
}
public void setContentSize(Integer width, Integer height) {
setContentWidth(width);
setContentHeight(height);
}
public void setContentWidth(Integer width) {
if(width < 0) {
elementWidthSet = false;
return;
}
elementWidth = width;
elementWidthSet = true;
items.forEach(f -> f.setWidth(width));
}
public void setContentHeight(Integer height) {
if(height < 0) {
elementHeightSet = false;
return;
}
elementHeight = height;
elementHeightSet = true;
items.forEach(f -> {
f.setLocationY(items.indexOf(f) * elementHeight);
f.setHeight(height);
});
}
public void open() {
addAnimation(new SizeAnimation(this, new Point2<>(closedSize.x, closedSize.y), new Point2<>(openedSize.x, openedSize.y), 70));
for(Visual v: items) {
super.addVisual(v);
v.setLocationX(getClosedSize().x - 1);
v.addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent mouseEvent) {
if(!isInside(mouseEvent.getXOnScreen(), mouseEvent.getYOnScreen())) {
close();
addAnimation(new ColorAnimation(SideDropDown.this, getForegroundColor(), getBackgroundColor(), 70));
}
isOpen = true;
}
});
setOpenedWidth(Math.max(getOpenedSize().x, getClosedSize().x + ((MenuItem) v).getOpenedSize().x));
if(elementCount == 0) {
v.setLocationY(0);
setOpenedHeight(Math.max(getOpenedSize().y, v.getLocationY() + ((MenuItem) v).getOpenedSize().y));
elementCount++;
return;
}
public void close() {
addAnimation(new SizeAnimation(this, new Point2<>(openedSize.x, openedSize.y), new Point2<>(closedSize.x, closedSize.y), 70));
for(Visual v: items) {
elementCount++;
}
public void removeVisual(Visual v) {
super.removeVisual(v);
}
isOpen = false;
elementCount--;
}
public void setLabel(String label) {
this.label = label;
@Override
public boolean isInside(int x, int y) {
x = x - getAbsoluteX();
y = y - getAbsoluteY();
if(isOpen()) {
return x > 0 && y > 0 && x < getOpenedSize().x && y < getOpenedSize().y &&
(y < getClosedSize().y || x > getClosedSize().x);
}
public String getLabel() {
return this.label;
}
public void setIcon(BufferedImage icon) {
this.icon = icon;
}
public void setIcon(String url) {
try{
icon = ImageIO.read(new File("resources\\icons\\" + url + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getIcon() {
return icon;
}
private void setElementWidth(Integer width) {
elementWidth = width;
items.forEach(f -> f.setClosedWidth(width));
}
public void paint(BufferedImage imageBuffer)
{
//Get Graphics
Graphics2D g = imageBuffer.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(getPaintColor());
//Draw Button
g.fillRect(0, 0, closedSize.x, closedSize.y);
//Draw Label
if(getFont() != null) {
g.setFont(getFont());
}
g.setColor(this.getFontColor());
int textWidth = 0;
int textHeight = 0;
//Draw Icon
if(icon != null) {
int iconWidth = icon.getWidth();
int iconHeight = icon.getHeight();
textWidth += iconWidth;
int iconX = closedSize.x - iconWidth - 3;
int iconY = (closedSize.y - iconHeight - textHeight) / 2;
Graphics2D g2 = imageBuffer.createGraphics();
g2.drawImage(icon, iconX, iconY, null);
g2.dispose();
}
if(!label.equals("")) {
textWidth += g.getFontMetrics().stringWidth(label);
textHeight = g.getFontMetrics().getHeight();
g.drawString(label, (closedSize.x - textWidth)/2, closedSize.y/2 + textHeight/2);
}
g.dispose();
return x > 0 && y > 0 && x < getClosedSize().x && y < getClosedSize().y;
}
}

View File

@@ -9,6 +9,11 @@ public class Point2<T extends Comparable<T>> implements Comparable<Point2<T>> {
this.y = y;
}
public Point2(Point2<T> point2) {
x = point2.x;
y = point2.y;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Point2<?>)) {
@@ -30,9 +35,9 @@ public class Point2<T extends Comparable<T>> implements Comparable<Point2<T>> {
@Override
public int compareTo(Point2<T> tPoint2) {
int cmp = x.compareTo(tPoint2.x);
int cmp = y.compareTo(tPoint2.y);
if(cmp == 0) {
return y.compareTo(tPoint2.y);
return x.compareTo(tPoint2.x);
}
return cmp;
}

View File

@@ -245,11 +245,11 @@ public class Visual {
}
public int getWidth() {
return this.width;
return width;
}
public int getHeight() {
return this.height;
return height;
}
public Point2<Float> getRelativeSize() {
@@ -257,17 +257,29 @@ public class Visual {
}
public int getLocationX() {
return this.locationX;
return locationX;
}
public int getLocationY() {
return this.locationY;
return locationY;
}
public Point2<Integer> getLocation() {
return new Point2<>(locationX, locationY);
}
public int getAbsoluteX() {
return absoluteX;
}
public int getAbsoluteY() {
return absoluteY;
}
public Point2<Integer> getAbsoluteLocation() {
return new Point2<>(absoluteX, absoluteY);
}
public Point2<Float> getRelativeLocation() {
return new Point2<>(relativeX, relativeY);
}
@@ -373,6 +385,10 @@ public class Visual {
animations.remove(animation);
}
public void removeAllAnimations() {
animations.clear();
}
public void repaint() {
Debugger.log("Called repaint from " + name, Debugger.Tag.PAINTING);
for(int i = 0; i < animations.size(); i++) {
@@ -432,27 +448,39 @@ public class Visual {
---------------------------------------------------------------------*/
public void addMouseListener(MouseListener mouseListener) {
this.mouseListeners.add(mouseListener);
mouseListeners.add(mouseListener);
}
public void removeMouseListener(MouseListener mouseListener) {
this.mouseListeners.remove(mouseListener);
mouseListeners.remove(mouseListener);
}
public void removeAllMouseListeners() {
mouseListeners.clear();
}
public void addMouseWheelListener(MouseWheelListener mouseWheelListener) {
this.mouseWheelListeners.add(mouseWheelListener);
mouseWheelListeners.add(mouseWheelListener);
}
public void removeMouseWheelListener(MouseWheelListener mouseWheelListener) {
this.mouseWheelListeners.remove(mouseWheelListener);
mouseWheelListeners.remove(mouseWheelListener);
}
public void removeAllMouseWheelListeners() {
mouseWheelListeners.clear();
}
public void addKeyListener(KeyListener keyListener) {
this.keyListeners.add(keyListener);
keyListeners.add(keyListener);
}
public void removeKeyListener(KeyListener keyListener) {
this.keyListeners.remove(keyListener);
keyListeners.remove(keyListener);
}
public void removeAllKeyListeners() {
keyListeners.clear();
}
void mouseClicked(MouseEvent mouseEvent) {
@@ -645,7 +673,7 @@ public class Visual {
mouseEvent.getClickCount(), mouseEvent.isPopupTrigger(), mouseEvent.getButton());
}
private boolean isInside(int x, int y) {
public boolean isInside(int x, int y) {
return x > absoluteX && x < absoluteX + width && y > absoluteY && y < absoluteY + height;
}