Skip to content

Commit a566ee2

Browse files
author
realPaulsen
committed
V1.1.1a
New: - NEW PUICanvas is inherited by every Element and is responsible for the rendering; Can also be added alone to the PUIFrame as a layer to be used as a Non-Interactive-Drawingspace - PUIScrollList + can now have spaces between Elements + can now center an Element in the list
1 parent c91c7d7 commit a566ee2

File tree

8 files changed

+166
-46
lines changed

8 files changed

+166
-46
lines changed

src/com/paulsen/demo/Main.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@
33
import com.paulsen.ui.*;
44
import com.paulsen.ui.PUIElement.ElementAlignment;
55

6+
import java.awt.*;
7+
68
public class Main {
79

810
public Main() {
911

1012
// initialize variables before using them in update/paint
1113
PUIFrame f = new PUIFrame("Project-Library Demo", 600, 600);
1214

15+
16+
// Drawing a rectangle in the background
17+
PUICanvas canvas = new PUICanvas(f, new PUIPaintable() {
18+
@Override
19+
public void paint(Graphics g, int x, int y, int w, int h) {
20+
g.setColor(new Color(100, 100, 100));
21+
g.fillRoundRect(40, 40, w - 80, h - 80, 20, 20);
22+
}
23+
}, -1);
24+
25+
1326
PUIText darkmodeButton = new PUIText(f, "LIGHT", 2);
1427
darkmodeButton.addActionListener(new PUIAction() {
1528
@Override
@@ -55,7 +68,7 @@ public void run() {
5568
slider2.addValueUpdateAction(new Runnable() {
5669
@Override
5770
public void run() {
58-
rc.setValueThickness((int) (67* slider2.getValue()));
71+
rc.setValueThickness((int) (67 * slider2.getValue()));
5972
}
6073
});
6174

@@ -66,10 +79,15 @@ public void run() {
6679
@Override
6780
public void run(PUIElement that) {
6881
f.setTitle(((PUIText) that).getText());
82+
83+
// automatically centers clicked Element in the UI
84+
sp.center(that);
6985
}
7086
});
7187
sp.addElement(t);
7288
}
89+
// comment out if the size of the element inside of the panel should be further limited
90+
// sp.setElementSpacing(6,0,3,3);
7391

7492
// prevent different colors when hovering/pressing
7593
for (PUIElement e : PUIElement.registeredElements) {

src/com/paulsen/ui/PUICanvas.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.paulsen.ui;
2+
3+
import java.awt.*;
4+
5+
public class PUICanvas {
6+
7+
protected PUIFrame frame;
8+
protected PUIPaintable paint;
9+
10+
protected boolean visible = true/*, blockRaycast = false*/;
11+
12+
// other
13+
protected int drawLayer = 0;
14+
15+
public PUICanvas(PUIFrame f, PUIPaintable paint) {
16+
frame = f;
17+
this.paint = paint;
18+
init();
19+
}
20+
21+
public PUICanvas(PUIFrame f, PUIPaintable paint, int layer) {
22+
frame = f;
23+
this.paint = paint;
24+
drawLayer = layer;
25+
init();
26+
}
27+
28+
private void init() {
29+
if (frame != null)
30+
frame.add(this);
31+
}
32+
33+
public synchronized void draw(Graphics g) {
34+
if (g != null && frame != null)
35+
paint.paint(g, 0, 0, frame.w(), frame.h());
36+
}
37+
38+
public PUIFrame getFrame() {
39+
return frame;
40+
}
41+
42+
public PUIPaintable getDraw() {
43+
return paint;
44+
}
45+
46+
public void setDraw(PUIPaintable paintable) {
47+
this.paint = paintable;
48+
}
49+
50+
public boolean isVisible() {
51+
return visible;
52+
}
53+
54+
public void setVisible(boolean visible) {
55+
this.visible = visible;
56+
}
57+
58+
public int getDrawLayer() {
59+
return drawLayer;
60+
}
61+
62+
public void setDrawLayer(int drawLayer) {
63+
this.drawLayer = drawLayer;
64+
}
65+
}

src/com/paulsen/ui/PUICore.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public void mousePressed(MouseEvent e) {
4848
if ((firstHitLayer == -1 || firstHitLayer == elem.getInteractionLayer()) && elem.contains(e.getPoint())) {
4949
if (elem.blocksRaycast()) {
5050
firstHitLayer = elem.getInteractionLayer();
51-
}else{
52-
System.out.println("let through");
5351
}
5452
for (MouseListener listener : elem.getMouseListeners())
5553
listener.mousePressed(e);

src/com/paulsen/ui/PUIElement.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.ArrayList;
1111
import java.util.concurrent.CopyOnWriteArrayList;
1212

13-
public class PUIElement { // PaulsenUserInterfaceIntegratedElement
13+
public class PUIElement extends PUICanvas { // PaulsenUserInterfaceIntegratedElement
1414

1515
// Static
1616
public static volatile CopyOnWriteArrayList<PUIElement> registeredElements = new CopyOnWriteArrayList<PUIElement>();
@@ -22,7 +22,7 @@ public class PUIElement { // PaulsenUserInterfaceIntegratedElement
2222

2323
protected int x = 0, y = 0, w = 0, h = 0;
2424
protected Color backgroundColor = Color.LIGHT_GRAY;
25-
protected PUIPaintable paintInvoke, hoverOverlay, pressOverlay;
25+
protected PUIPaintable hoverOverlay, pressOverlay;
2626
protected PUIFrame frame;
2727
protected PUICore core;
2828
protected ArrayList<PUIAction> actions = new ArrayList<>();
@@ -32,20 +32,21 @@ public class PUIElement { // PaulsenUserInterfaceIntegratedElement
3232
protected Object metaData;
3333
protected boolean updateFrameOnEvent = true, paintOverOnHover = true, paintOverOnPress = true, enabled = true;
3434
protected boolean blockRaycast = true;
35-
private int drawLayer = 0;
3635
private int interactionLayer = 0;
3736
// TEMP-vars
3837
// pressed -> is pressed on Screen
3938
// isCurrentlyPressing -> is pressing on Element
4039
private boolean hovered = false, pressed = false, isCurrentlyPressing = false;
4140

4241
public PUIElement(PUIFrame f) {
42+
super(f, null);
4343
this.frame = f;
4444
init();
4545
initCore();
4646
}
4747

4848
public PUIElement(PUIFrame f, int layer) {
49+
super(f, null, layer);
4950
this.frame = f;
5051
init();
5152
initCore();
@@ -105,7 +106,7 @@ public void mouseClicked(MouseEvent e) {
105106
}
106107
});
107108

108-
paintInvoke = new PUIPaintable() {
109+
paint = new PUIPaintable() {
109110
@Override
110111
public void paint(Graphics g, int x, int y, int w, int h) {
111112
if (darkUIMode && backgroundColor == Color.LIGHT_GRAY) {
@@ -135,8 +136,6 @@ public void paint(Graphics g, int x, int y, int w, int h) {
135136
g.fillRect(x, y, w, h);
136137
}
137138
};
138-
if (frame != null)
139-
frame.add(this);
140139
}
141140

142141
private void initCore() {
@@ -161,6 +160,7 @@ private void initCore() {
161160
}
162161
}
163162

163+
@Override
164164
public synchronized void draw(Graphics g) {
165165
if (g == null)
166166
return;
@@ -171,8 +171,8 @@ public synchronized void draw(Graphics g) {
171171
} else
172172
hovered = false;
173173

174-
if (paintInvoke != null) {
175-
paintInvoke.paint(g, x, y, w, h);
174+
if (paint != null) {
175+
paint.paint(g, x, y, w, h);
176176
}
177177
if (hovered) {
178178
if (hovered && !pressed && paintOverOnHover) {
@@ -258,10 +258,6 @@ public int getH() {
258258
return h;
259259
}
260260

261-
public void setDraw(PUIPaintable paintInvoke) {
262-
this.paintInvoke = paintInvoke;
263-
}
264-
265261
public void setHoverOverlay(PUIPaintable paintInvoke) {
266262
hoverOverlay = paintInvoke;
267263
}
@@ -319,17 +315,13 @@ public boolean blocksRaycast() {
319315
}
320316

321317
/**
322-
*
323318
* @param doesBlockRaycast if set to false: when pressed => the eventchain doesnt stop => elements on layers behind this Button can be triggered as well
324319
*/
325320
public void setRaycastable(boolean doesBlockRaycast) {
326321
this.blockRaycast = doesBlockRaycast;
327322
}
328323

329-
public int getDrawLayer() {
330-
return drawLayer;
331-
}
332-
324+
@Override
333325
public void setDrawLayer(int drawLayer) {
334326
this.drawLayer = drawLayer;
335327
frame.rearrangeElements();

src/com/paulsen/ui/PUIFrame.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PUIFrame extends JFrame {
1313
private int maxFrameRate = 30;
1414

1515
// registered Elements for managing drawing elements
16-
private volatile ArrayList<PUIElement> elements = new ArrayList<>();
16+
private volatile ArrayList<PUICanvas> elements = new ArrayList<>();
1717

1818
private volatile PUIUpdatable updateElements;
1919
private volatile PUIPaintable paint; // called by canvas
@@ -22,8 +22,9 @@ public class PUIFrame extends JFrame {
2222
private int w = 100, h = 100;
2323
private String displayName = "PUIFrame"; // only for init
2424
private boolean hasInit = false, continuousDraw = false;
25+
private int minUpdateDelay = 0;
2526

26-
public PUIFrame(PUIInitializable puiInitializable) {
27+
public PUIFrame() {
2728
super();
2829
constructorInit();
2930
}
@@ -79,10 +80,12 @@ protected void paintComponent(Graphics g) {
7980
paint.paint(g, 0, 0, w, h);
8081

8182
// Prevent Concurrent-Modification-Errors
82-
ArrayList<PUIElement> tempEl = new ArrayList<>(elements);
83+
ArrayList<PUICanvas> tempEl = new ArrayList<>(elements);
8384
// Paints Elements
84-
for (PUIElement el : tempEl) {
85-
if (el != null)
85+
for (PUICanvas el : tempEl) {
86+
if (!(el instanceof PUIElement))
87+
g.setColor(Color.black);
88+
if (el != null && el.isVisible())
8689
el.draw(g);
8790
}
8891
}
@@ -91,8 +94,6 @@ protected void paintComponent(Graphics g) {
9194
add(canvas);
9295
}
9396

94-
private int minUpdateDelay = 0;
95-
9697
private void initTimer() {
9798
new Thread(new Runnable() {
9899
@Override
@@ -161,7 +162,7 @@ public boolean hasInit() {
161162
return hasInit;
162163
}
163164

164-
public synchronized void add(PUIElement element) {
165+
public synchronized void add(PUICanvas element) {
165166
if (element != null && !elements.contains(element)) {
166167
elements.add(element);
167168
rearrangeElements();
@@ -248,9 +249,9 @@ public void setContinuousDraw(boolean continuousUpdate) {
248249
* sets Draw-Order
249250
*/
250251
public void rearrangeElements() {
251-
Comparator<PUIElement> comp = new Comparator<PUIElement>() {
252+
Comparator<PUICanvas> comp = new Comparator<PUICanvas>() {
252253
@Override
253-
public int compare(PUIElement o1, PUIElement o2) {
254+
public int compare(PUICanvas o1, PUICanvas o2) {
254255
if (o1.getDrawLayer() < o2.getDrawLayer())
255256
return -1;
256257
if (o1.getDrawLayer() > o2.getDrawLayer())

src/com/paulsen/ui/PUIInitializable.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/com/paulsen/ui/PUIRotaryControl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public PUIRotaryControl(PUIFrame f, int layer) {
3636
}
3737

3838
private void init() {
39-
paintInvoke = new PUIPaintable() {
39+
paint = new PUIPaintable() {
4040
@Override
4141
public void paint(Graphics g, int x, int y, int w, int h) {
4242
if (w < 0)

0 commit comments

Comments
 (0)