Skip to content

Commit c91c7d7

Browse files
author
realPaulsen
committed
V1.1.1
New: - The immediate stop of Raycasts at certain Elements can be turned off - The Area, where Elements can be hit, are now defined with methods Fix/Update: - PUICore and PUIElement are now Threadsave - RaycastBox of PUIRotaryControl is now Oval - updated Demo
1 parent 73ac476 commit c91c7d7

File tree

4 files changed

+101
-50
lines changed

4 files changed

+101
-50
lines changed

src/com/paulsen/demo/Main.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
public class Main {
77

8-
public static void main(String[] args) {
9-
new Main();
10-
}
11-
128
public Main() {
139

1410
// initialize variables before using them in update/paint
@@ -26,6 +22,8 @@ public void run(PUIElement that) {
2622
}
2723
}
2824
});
25+
// if set to false: when pressed the eventchain doesnt stop => elements on layers behind this Button can be triggered as well
26+
darkmodeButton.setRaycastable(false);
2927

3028
PUIScrollPanel sp = new PUIScrollPanel(f);
3129

@@ -43,7 +41,23 @@ public void run(PUIElement that) {
4341
PUIRotaryControl rc = new PUIRotaryControl(f, 1);
4442

4543
PUISlider slider = new PUISlider(f);
44+
slider.setValue(0.5f);
4645
slider.setAlignment(ElementAlignment.HORIZONTAL);
46+
slider.addValueUpdateAction(new Runnable() {
47+
@Override
48+
public void run() {
49+
rc.setValueLength(slider.getValue());
50+
}
51+
});
52+
53+
PUISlider slider2 = new PUISlider(f);
54+
slider2.setAlignment(ElementAlignment.HORIZONTAL);
55+
slider2.addValueUpdateAction(new Runnable() {
56+
@Override
57+
public void run() {
58+
rc.setValueThickness((int) (67* slider2.getValue()));
59+
}
60+
});
4761

4862
// add test-Buttons for scrollpanel
4963
for (int i = 1; i <= 10; i++) {
@@ -77,9 +91,14 @@ public void update(int w, int h) {
7791

7892
// Set Position of other non-relative Elements
7993
darkmodeButton.setBounds(50, 50, 300, 100);
80-
slider.setBounds(50, 200, 300, 100);
94+
slider.setBounds(50, 200, 300, 50);
95+
slider2.setBounds(50, 250, 300, 50);
8196

8297
f.updateElements();
8398
}
8499

100+
public static void main(String[] args) {
101+
new Main();
102+
}
103+
85104
}

src/com/paulsen/ui/PUICore.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,15 @@
22

33
import javax.management.InvalidAttributeValueException;
44
import java.awt.event.*;
5-
import java.util.ArrayList;
65
import java.util.Comparator;
6+
import java.util.concurrent.CopyOnWriteArrayList;
77

88
public final class PUICore {
99

10-
/**
11-
* @return PUICore that has component <code>c</code> or <code>null</code> if
12-
* core with given component doesn't exist
13-
*/
14-
public static PUICore getCore(PUIFrame f) {
15-
for (PUICore core : registeredCores)
16-
if (core.f == f)
17-
return core;
18-
return null;
19-
}
20-
21-
private static ArrayList<PUICore> registeredCores = new ArrayList<>();
22-
10+
private static CopyOnWriteArrayList<PUICore> registeredCores = new CopyOnWriteArrayList<>();
2311
private PUIFrame f;
2412
// registered Elements for managing inputs from user
25-
private volatile ArrayList<PUIElement> elements = new ArrayList<>();
13+
private volatile CopyOnWriteArrayList<PUIElement> elements = new CopyOnWriteArrayList<>();
2614

2715
public PUICore(PUIFrame f) throws InvalidAttributeValueException {
2816
for (PUICore core : registeredCores)
@@ -31,10 +19,20 @@ public PUICore(PUIFrame f) throws InvalidAttributeValueException {
3119

3220
registeredCores.add(this);
3321
this.f = f;
34-
3522
init();
3623
}
3724

25+
/**
26+
* @return PUICore that has component <code>c</code> or <code>null</code> if
27+
* core with given component doesn't exist
28+
*/
29+
public static PUICore getCore(PUIFrame f) {
30+
for (PUICore core : registeredCores)
31+
if (core.f == f)
32+
return core;
33+
return null;
34+
}
35+
3836
private void init() {
3937
System.out.println("init PUICore");
4038

@@ -47,8 +45,12 @@ public void mouseClicked(MouseEvent e) {
4745
public void mousePressed(MouseEvent e) {
4846
int firstHitLayer = -1; //2D-Raycast from top to bottom
4947
for (PUIElement elem : elements)
50-
if ((firstHitLayer == -1 || firstHitLayer == elem.getInteractionLayer()) && elem.getBounds().contains(e.getPoint())) {
51-
firstHitLayer = elem.getInteractionLayer();
48+
if ((firstHitLayer == -1 || firstHitLayer == elem.getInteractionLayer()) && elem.contains(e.getPoint())) {
49+
if (elem.blocksRaycast()) {
50+
firstHitLayer = elem.getInteractionLayer();
51+
}else{
52+
System.out.println("let through");
53+
}
5254
for (MouseListener listener : elem.getMouseListeners())
5355
listener.mousePressed(e);
5456
} else if ((firstHitLayer != -1 && firstHitLayer != elem.getInteractionLayer())) {
@@ -93,8 +95,9 @@ public void mouseMoved(MouseEvent e) {
9395
public void mouseWheelMoved(MouseWheelEvent e) {
9496
int firstHitLayer = -1; //2D-Raycast from top to bottom
9597
for (PUIElement elem : elements)
96-
if ((firstHitLayer == -1 || firstHitLayer == elem.getInteractionLayer()) && elem.getBounds().contains(e.getPoint())) {
97-
firstHitLayer = elem.getInteractionLayer();
98+
if ((firstHitLayer == -1 || firstHitLayer == elem.getInteractionLayer()) && elem.contains(e.getPoint())) {
99+
if (elem.blocksRaycast())
100+
firstHitLayer = elem.getInteractionLayer();
98101
for (MouseWheelListener listener : elem.getMouseWheelListeners())
99102
listener.mouseWheelMoved(e);
100103
} else if ((firstHitLayer != -1 && firstHitLayer != elem.getInteractionLayer())) {

src/com/paulsen/ui/PUIElement.java

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,32 @@
88
import java.awt.event.MouseMotionListener;
99
import java.awt.event.MouseWheelListener;
1010
import java.util.ArrayList;
11+
import java.util.concurrent.CopyOnWriteArrayList;
1112

1213
public class PUIElement { // PaulsenUserInterfaceIntegratedElement
1314

14-
public static volatile ArrayList<PUIElement> registeredElements = new ArrayList<PUIElement>();
15+
// Static
16+
public static volatile CopyOnWriteArrayList<PUIElement> registeredElements = new CopyOnWriteArrayList<PUIElement>();
1517
public static boolean darkUIMode = false;
1618
public static Color darkBG_1 = new Color(47, 47, 47), darkBG_2 = new Color(57, 57, 57),
1719
darkOutline = new Color(81, 81, 81), darkText = new Color(235, 235, 235),
1820
darkSelected = new Color(196, 196, 196);
1921

20-
public enum ElementAlignment {
21-
HORIZONTAL, VERTICAL
22-
}
2322

2423
protected int x = 0, y = 0, w = 0, h = 0;
25-
private int drawLayer = 0;
26-
private int interactionLayer = 0;
2724
protected Color backgroundColor = Color.LIGHT_GRAY;
28-
2925
protected PUIPaintable paintInvoke, hoverOverlay, pressOverlay;
3026
protected PUIFrame frame;
3127
protected PUICore core;
32-
3328
protected ArrayList<PUIAction> actions = new ArrayList<>();
3429
protected ArrayList<MouseListener> mouseListeners = new ArrayList<>();
3530
protected ArrayList<MouseMotionListener> mouseMotionListeners = new ArrayList<>();
3631
protected ArrayList<MouseWheelListener> mouseWheelListeners = new ArrayList<>();
3732
protected Object metaData;
38-
3933
protected boolean updateFrameOnEvent = true, paintOverOnHover = true, paintOverOnPress = true, enabled = true;
40-
34+
protected boolean blockRaycast = true;
35+
private int drawLayer = 0;
36+
private int interactionLayer = 0;
4137
// TEMP-vars
4238
// pressed -> is pressed on Screen
4339
// isCurrentlyPressing -> is pressing on Element
@@ -189,6 +185,19 @@ public synchronized void draw(Graphics g) {
189185
}
190186
}
191187

188+
/**
189+
* Is used by the Eventsystem to determine if a position of the screen is part of this element
190+
* <p>
191+
* Is useful to overwrite if a new Element, based on PUIElement, is created that is not rectangular (e.g Circle, Polygon).
192+
* So that the EventSystem knows how to handle incomming MouseClicks on this Element
193+
*
194+
* @param p
195+
* @return TRUE if p in Frame-Space is part of the element and FALSE if not.
196+
*/
197+
public boolean contains(Point p) {
198+
return getBounds().contains(p);
199+
}
200+
192201
public ArrayList<PUIAction> getActionListeners() {
193202
return actions;
194203
}
@@ -301,19 +310,35 @@ public boolean isEnabled() {
301310
return enabled;
302311
}
303312

304-
public int getDrawLayer() {
305-
return drawLayer;
313+
public void setEnabled(boolean enabled) {
314+
this.enabled = enabled;
306315
}
307316

308-
public int getInteractionLayer() {
309-
return interactionLayer;
317+
public boolean blocksRaycast() {
318+
return blockRaycast;
319+
}
320+
321+
/**
322+
*
323+
* @param doesBlockRaycast if set to false: when pressed => the eventchain doesnt stop => elements on layers behind this Button can be triggered as well
324+
*/
325+
public void setRaycastable(boolean doesBlockRaycast) {
326+
this.blockRaycast = doesBlockRaycast;
327+
}
328+
329+
public int getDrawLayer() {
330+
return drawLayer;
310331
}
311332

312333
public void setDrawLayer(int drawLayer) {
313334
this.drawLayer = drawLayer;
314335
frame.rearrangeElements();
315336
}
316337

338+
public int getInteractionLayer() {
339+
return interactionLayer;
340+
}
341+
317342
public void setInteractionLayer(int interactionLayer) {
318343
this.interactionLayer = interactionLayer;
319344
core.rearrangeElements();
@@ -340,10 +365,6 @@ public ArrayList<MouseListener> getMouseListeners() {
340365
return mouseListeners;
341366
}
342367

343-
public void setEnabled(boolean enabled) {
344-
this.enabled = enabled;
345-
}
346-
347368
public Color getBackgroundColor() {
348369
return backgroundColor;
349370
}
@@ -396,12 +417,16 @@ public int getUserSelection(String title, ArrayList<String> comboBoxInput) {
396417
return getUserSelection(title, s);
397418
}
398419

420+
public Object getMetadata() {
421+
return metaData;
422+
}
423+
399424
public void setMetadata(Object o) {
400425
metaData = o;
401426
}
402427

403-
public Object getMetadata() {
404-
return metaData;
428+
public enum ElementAlignment {
429+
HORIZONTAL, VERTICAL
405430
}
406431

407432
}

src/com/paulsen/ui/PUIRotaryControl.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
import java.awt.event.MouseWheelEvent;
77
import java.awt.event.MouseWheelListener;
88
import java.util.ArrayList;
9-
import java.util.Comparator;
10-
import java.util.HashSet;
119

1210
public class PUIRotaryControl extends PUIElement {
1311

14-
private ArrayList<Runnable> valueUpdateAction = new ArrayList<Runnable>();
15-
1612
protected float valueLength = 0.5f;
1713
protected int rotationArea = 270;
1814
protected int valueThickness = 2;
1915
protected float mouseMultiplicator = 0.005f;
20-
16+
private ArrayList<Runnable> valueUpdateAction = new ArrayList<Runnable>();
2117
private float value = .5f;
2218
private Color valueColor = Color.GRAY;
2319
private Color backgroundColor = Color.LIGHT_GRAY;
@@ -142,6 +138,14 @@ private Point[] getRotPoints(float value) { // 2Points => outer/inner-point
142138
return p;
143139
}
144140

141+
/**
142+
* only allows events in a oval area.
143+
*/
144+
@Override
145+
public boolean contains(Point p) {
146+
return new Point(x + w / 2, y + h / 2).distanceSq(p.x, p.y) <= (w / 2) * (w / 2);
147+
}
148+
145149
@Override
146150
public synchronized void setBounds(int x, int y, int w, int h) {
147151
super.setBounds(x, y, w, w);

0 commit comments

Comments
 (0)