too much to remember

added radio buttons
added size animations
added menus (still needs cleaning)
This commit is contained in:
Macocian Adrian Radu
2020-05-18 23:42:39 +03:00
parent 202610764b
commit 970734f20f
19 changed files with 1024 additions and 28 deletions

BIN
resources/icons/circle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 275 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 336 B

View File

@@ -18,9 +18,63 @@
Name="Panel" Name="Panel"
Row="0" Row="0"
Column="1"> Column="1">
<Border <RadioButtons
Thickness="10" Size="1.0f, 0.3f">
Color="#ff0000"/> <RadioButton
Text="Iubesc"
Name="Iubesc"
Size="300, 20"
Icon="circle"
/>
<RadioButton
Text="Ioana"
Name="Ioana"
Size="300, 20"
Icon="circle"
/>
<RadioButton
Text="Mult"
Name="Mult"
Size="300, 20"
Icon="circle"
/>
</RadioButtons>
<DropDown
BackgroundColor="#222222"
Name="DropDown"
Label="Drop Down Menu"
Location="0.0f, 0.5f"
ContentHeight="30"
ContentWidth="100"
ClosedSize="200, 30">
<Button
Name="Button1"
Label="B1"/>
<Button
Name="Button2"
Label="B2"/>
<SideDropDown
Name="SideDropDown"
Label="SideDropDown"
ClosedWidth="100"
ClosedHeight="30"
ContentHeight="30"
ContentWidth="100">
<Button
Name="Button3"
Label="Button3"/>
<Button
Name="Button4"
Label="Button4"/>
</SideDropDown>
</DropDown>
<InputTextBox
BackgroundColor="#999999"
AccentColor="#aaaaaa"
ForegroundColor="#ffffff"
Location="0.0f, 0.8f"
Size="300, 60"/>
</Panel> </Panel>
</GridPanel> </GridPanel>

View File

@@ -0,0 +1,35 @@
package guiTree.Animations;
import guiTree.Helper.Debugger;
import guiTree.Helper.Point2;
import guiTree.Visual;
public class SizeAnimation implements AnimationInterface {
private Point2<Float> from;
private Point2<Integer> to;
private Point2<Float> offset;
private Visual element;
public SizeAnimation(Visual v, Point2<Integer> from, Point2<Integer> to, int ms) {
this.from = new Point2<>(from.x.floatValue(), from.y.floatValue());
this.to = to;
this.offset = new Point2<>((float)(to.x - from.x) * 1000 / FPS / ms, (float)(to.y - from.y) * 1000 / FPS / ms);
this.element = v;
Debugger.log("Created size animation for " + v.getName() + " from width: " + from.x + " height: " + from.y + " to width: " + to.x + " height: " + to.y, Debugger.Tag.ANIMATIONS);
}
@Override
public boolean step() {
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.setSize(to.x, to.y);
Debugger.log("Animation for " + element.getName() + " finished", Debugger.Tag.ANIMATIONS);
return true;
}
from.x += offset.x;
from.y += offset.y;
element.setSize(Math.round(from.x), Math.round(from.y));
element.update();
return false;
}
}

View File

@@ -2,6 +2,7 @@ package guiTree.Components;
import guiTree.Animations.ColorAnimation; import guiTree.Animations.ColorAnimation;
import guiTree.Helper.Debugger; import guiTree.Helper.Debugger;
import guiTree.Helper.Point2;
import guiTree.Visual; import guiTree.Visual;
import guiTree.events.MouseAdapter; import guiTree.events.MouseAdapter;
@@ -12,7 +13,7 @@ import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
public class Button extends Visual { public class Button extends MenuItem {
private String label; private String label;
private BufferedImage icon; private BufferedImage icon;
private int round = -1; private int round = -1;
@@ -140,4 +141,44 @@ public class Button extends Visual {
public BufferedImage getIcon() { public BufferedImage getIcon() {
return icon; return icon;
} }
@Override
public Point2<Integer> getClosedSize() {
return new Point2<>(getWidth(), getHeight());
}
@Override
public Point2<Integer> getOpenedSize() {
return getClosedSize();
}
@Override
public void setClosedSize(Integer width, Integer height) {
setSize(width, height);
}
@Override
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

@@ -13,6 +13,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
public class CheckBox extends Visual { public class CheckBox extends Visual {
public static int CHECKBOX_CLICKED = 3;
private BufferedImage icon; private BufferedImage icon;
private boolean hovered; private boolean hovered;
private boolean marked; private boolean marked;
@@ -45,6 +46,7 @@ public class CheckBox extends Visual {
addAnimation(new ColorAnimation(CheckBox.this, getForegroundColor(), getAccentColor(), 100)); addAnimation(new ColorAnimation(CheckBox.this, getForegroundColor(), getAccentColor(), 100));
} }
marked = !marked; marked = !marked;
notifyParent(CHECKBOX_CLICKED);
Debugger.log("Calling repaint from pressed: " + getName(), Debugger.Tag.PAINTING); Debugger.log("Calling repaint from pressed: " + getName(), Debugger.Tag.PAINTING);
update(); update();
} }
@@ -76,7 +78,17 @@ public class CheckBox extends Visual {
} }
public void setMarked(boolean marked) { public void setMarked(boolean marked) {
if(this.marked != marked) {
if (!this.marked) {
addAnimation(new ColorAnimation(this, getBackgroundColor(), getForegroundColor(), 100));
} else {
addAnimation(new ColorAnimation(this, getForegroundColor(), getBackgroundColor(), 100));
}
}
this.marked = marked; this.marked = marked;
update();
} }
public String getText() { public String getText() {
@@ -85,10 +97,12 @@ public class CheckBox extends Visual {
public void setText(String text) { public void setText(String text) {
this.text = text; this.text = text;
update();
} }
public void setIcon(BufferedImage icon) { public void setIcon(BufferedImage icon) {
this.icon = icon; this.icon = icon;
update();
} }
public void setIcon(String url) { public void setIcon(String url) {
@@ -97,6 +111,7 @@ public class CheckBox extends Visual {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
update();
} }
@Override @Override

View File

@@ -0,0 +1,262 @@
package guiTree.Components;
import guiTree.Animations.SizeAnimation;
import guiTree.Helper.Point2;
import guiTree.Visual;
import guiTree.events.MouseAdapter;
import javafx.css.Size;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
public class DropDown extends ToggleButton implements Menu{
private List<MenuItem> items;
private boolean isOpen;
private boolean elementWidthSet;
private boolean elementHeightSet;
private int elementHeight;
private int elementWidth;
private Point2<Integer> closedSize;
private Point2<Integer> openedSize;
public DropDown() {
super();
items = new ArrayList<>();
isOpen = false;
elementHeight = 0;
elementWidth = 0;
elementWidthSet = false;
elementHeightSet = false;
closedSize = new Point2<>(getWidth(), getHeight());
openedSize = new Point2<>(getWidth(), getHeight());
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
if(isOpen) {
close();
return;
}
open();
}
});
}
@Override
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(0);
openedSize.x = Math.max(openedSize.x, ((MenuItem) v).getOpenedWidth());
if(items.size() == 0) {
v.setLocationY(getHeight());
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());
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);
openedSize.y -= v.getHeight();
if(!elementWidthSet) {
if(((MenuItem) v).getClosedWidth() == elementWidth) {
setElementWidth(items.stream().mapToInt(MenuItem::getClosedWidth).max().orElse(0));
}
}
if(isOpen) {
super.removeVisual(v);
}
}
public void setClosedSize(Integer width, Integer height) {
setClosedWidth(width);
setClosedHeight(height);
}
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);
}
private void setElementWidth(Integer width) {
elementWidth = width;
items.forEach(f -> f.setClosedWidth(width));
}
public void setContentWidth(Integer width) {
if(width < 0) {
elementWidthSet = false;
return;
}
elementWidth = width;
items.forEach(f -> f.setClosedWidth(width));
elementWidthSet = true;
}
public void setContentHeight(Integer height) {
if(height < 0) {
elementHeightSet = false;
return;
}
elementHeight = height;
items.forEach(f -> {
f.setLocationY(items.indexOf(f) * elementHeight);
f.setClosedHeight(height);
});
elementHeightSet = true;
}
public void open() {
isOpen = true;
items.forEach(super::addVisual);
addAnimation(new SizeAnimation(this, closedSize, openedSize, 70));
}
public void close() {
isOpen = false;
items.forEach(super::removeVisual);
addAnimation(new SizeAnimation(this, openedSize, 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;
// }
// @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 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;
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();
int iconX = (getWidth() - iconWidth - textWidth) / 2;
int iconY = (closedSize.y - iconHeight - textHeight) / 2;
Graphics2D g2 = imageBuffer.createGraphics();
g2.drawImage(getIcon(), iconX, iconY, null);
g2.dispose();
}
g.dispose();
}
}

View File

@@ -0,0 +1,204 @@
package guiTree.Components;
import guiTree.Animations.ColorAnimation;
import guiTree.Helper.Point2;
import guiTree.Visual;
import guiTree.events.KeyAdapter;
import guiTree.events.MouseAdapter;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
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 FontMetrics fontMetrics;
private int paragraphSpacing;
public InputTextBox() {
this(true, "");
}
public InputTextBox(Boolean visible) {
this(visible, "");
}
public InputTextBox(String title) {
this(true, title);
}
public InputTextBox(Boolean visible, String title) {
super();
this.title = title;
this.visible = visible;
lines = new ArrayList<>();
lines.add(new StringBuilder());
caretPosition = new Point2<>(0, 0);
paragraphSpacing = 1;
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
caretPosition = getCaretPosition(mouseEvent.getX(), mouseEvent.getY());
System.out.println("Caret Position: " + caretPosition);
addAnimation(new ColorAnimation(InputTextBox.this, getAccentColor(), getForegroundColor(), 100));
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
setCursor(new Cursor(Cursor.TEXT_CURSOR));
addAnimation(new ColorAnimation(InputTextBox.this, getBackgroundColor(), getAccentColor(), 100));
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
addAnimation(new ColorAnimation(InputTextBox.this, getAccentColor(), getBackgroundColor(), 100));
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent) {
if(keyEvent.getKeyCode() == KeyEvent.VK_UP) {
if(caretPosition.y > 0){
if(caretPosition.x > lines.get(caretPosition.y - 1).length()) {
caretPosition.x = lines.get(caretPosition.y - 1).length();
}
caretPosition.y --;
update();
}
}
if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {
if(caretPosition.y < lines.size() - 1){
if(caretPosition.x > lines.get(caretPosition.y + 1).length()) {
caretPosition.x = lines.get(caretPosition.y + 1).length();
}
caretPosition.y ++;
update();
}
}
if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
if(caretPosition.x == 0){
if(caretPosition.y > 0) {
caretPosition.y --;
caretPosition.x = lines.get(caretPosition.y).length();
update();
}
return;
}
caretPosition.x --;
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;
update();
}
return;
}
caretPosition.x ++;
update();
}
}
@Override
public void keyTyped(KeyEvent keyEvent) {
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;
update();
return;
}
if(keyEvent.getKeyChar() == '\b') {
if(caretPosition.x == 0) {
if(caretPosition.y == 0) {
return;
}
caretPosition.y --;
caretPosition.x = lines.get(caretPosition.y).length();
lines.set(caretPosition.y, lines.get(caretPosition.y).append(currentLine));
lines.remove(currentLine);
update();
return;
}
currentLine.deleteCharAt(caretPosition.x - 1);
lines.set(caretPosition.y, currentLine);
caretPosition.x --;
update();
return;
}
lines.set(caretPosition.y, lines.get(caretPosition.y).insert(caretPosition.x, (Object)keyEvent.getKeyChar()));
caretPosition.x ++;
update();
}
});
}
@Override
public void setFont(Font font) {
super.setFont(font);
fontMetrics = null;
}
private Point2<Integer> getCaretPosition(int x, int y) {
y -= lines.size() * paragraphSpacing;
y /= fontMetrics.getHeight();
if(y > lines.size() - 1) {
return new Point2<>(lines.get(lines.size() - 1).length(), lines.size() - 1);
}
String currentLine = lines.get(y).toString();
for(int i = 0; i < currentLine.length(); i++) {
if(x < fontMetrics.charWidth(currentLine.charAt(i)) / 2) {
return new Point2<>(i, y);
}
x -= fontMetrics.charWidth(currentLine.charAt(i));
}
return new Point2<>(currentLine.length(), y);
}
@Override
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
System.out.println("Caret: " + caretPosition);
g.setColor(getPaintColor());
g.fillRect(0, 0, getWidth(), getHeight());
if(fontMetrics == null) {
fontMetrics = g.getFontMetrics();
return;
}
int y = fontMetrics.getHeight();
g.setColor(getFontColor());
for(StringBuilder line: lines) {
if(caretPosition.y == lines.indexOf(line) && isFocused()) {
int x = 0;
for(int i = 0; i < caretPosition.x; i++) {
x += fontMetrics.charWidth(line.charAt(i));
}
g.drawLine(x, y - fontMetrics.getHeight() + 3, x, y + 3);
}
// g.drawString(line.toString(), 0, fontMetrics.getHeight() * (lines.indexOf(line) + 1));
g.drawString(line.toString(), 0, y);
y += fontMetrics.getHeight();
}
}
}

View File

@@ -0,0 +1,6 @@
package guiTree.Components;
public interface Menu {
void open();
void close();
}

View File

@@ -0,0 +1,38 @@
package guiTree.Components;
import guiTree.Helper.Point2;
import guiTree.Visual;
public abstract class MenuItem extends Visual {
public abstract Point2<Integer> getClosedSize();
public abstract Point2<Integer> getOpenedSize();
public abstract void setClosedSize(Integer width, Integer height);
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());
}
public void setClosedHeight(Integer height) {
setClosedSize(getClosedWidth(), height);
}
public void setOpenedWidth(Integer width) {
setOpenedSize(width, getOpenedHeight());
}
public void setOpenedHeight(Integer height) {
setOpenedSize(getOpenedWidth(), height);
}
}

View File

@@ -13,7 +13,7 @@ public class Panel extends Visual {
public Panel() { public Panel() {
super(); super();
overlapping = false; overlapping = true;
visuals = new ArrayList<>(); visuals = new ArrayList<>();
} }
@@ -97,10 +97,10 @@ public class Panel extends Visual {
} }
@Override @Override
public void handleNotification(int notify) { public void handleNotification(Visual v, int notify) {
if(notify == TitleBar.CLOSE || notify == TitleBar.MAXIMIZE || if(notify == TitleBar.CLOSE || notify == TitleBar.MAXIMIZE ||
notify == TitleBar.MINIMIZE || notify == TitleBar.NORMALIZE) { notify == TitleBar.MINIMIZE || notify == TitleBar.NORMALIZE) {
notifyParent(notify); notifyParent(v, notify);
// return; // return;
} }
// if(notify == SIZE_CHANGED || notify == LOCATION_CHANGED) { // if(notify == SIZE_CHANGED || notify == LOCATION_CHANGED) {

View File

@@ -0,0 +1,35 @@
package guiTree.Components;
import java.awt.*;
import java.awt.image.BufferedImage;
public class RadioButton extends CheckBox {
public RadioButton() {
super();
}
@Override
public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(getPaintColor());
g.drawOval(1, 1, getHeight() - 2, getHeight() - 2);
if(isMarked()) {
if(getHeight() > 9) {
g.fillOval(5, 5, getHeight() - 9, getHeight() - 9);
}
}
g.setColor(getFontColor());
if(getFont() != null) {
g.setFont(getFont());
}
int textHeight = g.getFontMetrics().getHeight();
g.drawString(getText(), getHeight() + 10, getHeight() / 2 + textHeight / 4);
g.dispose();
}
}

View File

@@ -0,0 +1,26 @@
package guiTree.Components;
import guiTree.Visual;
public class RadioButtons extends CheckBoxList{
public void handleNotification(Visual v, int notify) {
if(notify == CheckBox.CHECKBOX_CLICKED) {
for(CheckBox cb: getActiveBoxes()) {
if(!cb.equals(v)) {
cb.setMarked(false);
}
}
}
update();
}
public int getActiveButton() {
for(CheckBox rb: getActiveBoxes()) {
if(rb.isMarked()) {
return getActiveBoxes().indexOf(rb);
}
}
return -1;
}
}

View File

@@ -127,16 +127,12 @@ public class ScrollPanel extends Visual {
} }
@Override @Override
public void handleNotification(int notify) { public void handleNotification(Visual v, int notify) {
if(notify == Slider.SLIDER_MOVED) { if (notify == Slider.SLIDER_MOVED) {
setLocations(); setLocations();
} }
} }
@Override
public void handleNotification(Visual v, int notify) {
}
@Override @Override
public void paint(BufferedImage imageBuffer) { public void paint(BufferedImage imageBuffer) {
Graphics2D g = imageBuffer.createGraphics(); Graphics2D g = imageBuffer.createGraphics();

View File

@@ -0,0 +1,284 @@
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 SideDropDown() {
items = new ArrayList<>();
isOpen = false;
elementHeight = 0;
elementWidth = 0;
closedSize = new Point2<>(0, 0);
openedSize = new Point2<>(0, 0);
setIcon("arrow_right_black");
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent mouseEvent) {
addAnimation(new ColorAnimation(SideDropDown.this, getBackgroundColor(), getForegroundColor(), 100));
open();
update();
Debugger.log("Calling repaint from entered: " + getName(), Debugger.Tag.PAINTING);
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
addAnimation(new ColorAnimation(SideDropDown.this, getForegroundColor(), getBackgroundColor(), 100));
close();
update();
Debugger.log("Calling repaint from exited: " + getName(), Debugger.Tag.PAINTING);
}
});
}
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);
}
isOpen = true;
}
public void close() {
addAnimation(new SizeAnimation(this, new Point2<>(openedSize.x, openedSize.y), new Point2<>(closedSize.x, closedSize.y), 70));
for(Visual v: items) {
super.removeVisual(v);
}
isOpen = false;
}
public void setLabel(String label) {
this.label = label;
}
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();
}
}

View File

@@ -155,7 +155,7 @@ public class Text extends Visual {
int startX; int startX;
int endX; int endX;
if(startIndex > endIndex) { if(startIndex > endIndex) {
endX = startIndex; endX = startIndex - 1;
startX = endIndex; startX = endIndex;
} }
else { else {

View File

@@ -15,7 +15,6 @@ import java.io.IOException;
public class ToggleButton extends Visual { public class ToggleButton extends Visual {
private String label; private String label;
private Boolean pressed; private Boolean pressed;
private Boolean hovered;
private BufferedImage icon; private BufferedImage icon;
public ToggleButton() { public ToggleButton() {
@@ -35,7 +34,6 @@ public class ToggleButton extends Visual {
this.label = label; this.label = label;
this.icon = icon; this.icon = icon;
pressed = false; pressed = false;
hovered = false;
this.addMouseListener(new MouseAdapter() { this.addMouseListener(new MouseAdapter() {
@Override @Override
public void mousePressed(MouseEvent mouseEvent) { public void mousePressed(MouseEvent mouseEvent) {

View File

@@ -252,6 +252,10 @@ public class Visual {
return this.height; return this.height;
} }
public Point2<Float> getRelativeSize() {
return new Point2<>(relativeWidth, relativeHeight);
}
public int getLocationX() { public int getLocationX() {
return this.locationX; return this.locationX;
} }
@@ -268,6 +272,10 @@ public class Visual {
return new Point2<>(relativeX, relativeY); return new Point2<>(relativeX, relativeY);
} }
public boolean isFocused() {
return this == focused;
}
public Font getFont() { public Font getFont() {
return font; return font;
} }
@@ -347,23 +355,14 @@ public class Visual {
} }
public void handleNotification(int notify) {
}
public void notifyParent(Visual v, int notify) { public void notifyParent(Visual v, int notify) {
if(parent != null) { if(parent != null) {
if(v == null) { parent.handleNotification(v, notify);
parent.handleNotification(notify);
}
else {
parent.handleNotification(v, notify);
}
} }
} }
public void notifyParent(int notify) { public void notifyParent(int notify) {
notifyParent(null, notify); notifyParent(this, notify);
} }
public void addAnimation(AnimationInterface animation) { public void addAnimation(AnimationInterface animation) {
@@ -476,6 +475,9 @@ public class Visual {
mouseListener.mousePressed(entered.createMouseEvent(mouseEvent)); mouseListener.mousePressed(entered.createMouseEvent(mouseEvent));
} }
entered.pressed = true; entered.pressed = true;
if(focused != null) {
focused.update();
}
focused = entered; focused = entered;
Debugger.log("Pressed " + entered.name, Debugger.Tag.LISTENER); Debugger.log("Pressed " + entered.name, Debugger.Tag.LISTENER);
} }

View File

@@ -196,7 +196,7 @@ public class Window extends Visual implements Runnable{
} }
@Override @Override
public void handleNotification(int notify) { public void handleNotification(Visual v, int notify) {
switch(notify) { switch(notify) {
case TitleBar.CLOSE: { case TitleBar.CLOSE: {
dispose(); dispose();