Skip to content

Commit 3fa352e

Browse files
committed
added mouse keys and onmouse events update
1 parent 64140fc commit 3fa352e

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

.attach_pid38976

Whitespace-only changes.
497 Bytes
Binary file not shown.

src/JavaGameEngine/Backend/ComponentHandler.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ public static LinkedList<Component> getObjects() {
2222
return objects;
2323
}
2424

25+
public static LinkedList<Component> getAllObjects() {
26+
27+
return getAllObjects_(objects);
28+
}
29+
30+
private static LinkedList<Component> getAllObjects_(LinkedList<Component> parent) {
31+
for(Component component : parent){
32+
parent.addAll(component.getChildren());
33+
return parent;
34+
}
35+
return parent;
36+
}
37+
2538
public static void removeObject(Component object)
2639
{
2740
ComponentHandler.objects.remove((object));

src/JavaGameEngine/Backend/GameWorld.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ public void keyReleased(KeyEvent e) {
4646
@Override
4747
public void mousePressed(MouseEvent e) {
4848
super.mousePressed(e);
49-
for(Component comp : ComponentHandler.getObjects()){
50-
51-
if(insideComp(comp)){
52-
comp.onMousePressed();
53-
}
54-
}
5549
Input.addMouseButton(e);
5650
}
5751
@Override
@@ -68,18 +62,6 @@ public void mouseDragged(MouseEvent e) {
6862
@Override
6963
public void mouseMoved(MouseEvent e) {
7064
super.mouseMoved(e);
71-
for(Component comp : ComponentHandler.getObjects()){
72-
if(insideComp(comp)){
73-
if(!comp.isMouseInside()){
74-
comp.onMouseEntered();
75-
comp.setMouseInside(true);
76-
}
77-
}
78-
else if (comp.isMouseInside()){
79-
comp.onMouseExit();
80-
comp.setMouseInside(false);
81-
}
82-
}
8365
Input.setMousePosition(new Vector2((float) e.getPoint().getX(), (float) e.getPoint().getY()));
8466
}
8567
};

src/JavaGameEngine/Backend/Input/Input.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class Input {
1010
private static LinkedList<Integer> keyDowns = new LinkedList<>();
1111
private static boolean isPressed = false;
12+
private static boolean mouseIsPressed = false;
1213
private static Vector2 mousePosition=new Vector2(0,0);
1314

1415

@@ -24,22 +25,39 @@ public static void setMousePosition(Vector2 mousePosition) {
2425
public static void addMouseButton(MouseEvent e) {
2526
if(!isMouseDown(e.getButton()))
2627
mouseButtonDowns.add(new Integer(e.getButton()));
28+
29+
if(!mouseIsPressed)
30+
{
31+
mouseIsPressed = true;
32+
}
2733
}
2834
public static void removeMouseButton(MouseEvent e) {
35+
2936
if(isMouseDown(e.getButton()))
3037
mouseButtonDowns.remove(new Integer(e.getButton()));
3138
}
3239
public static boolean isMouseDown(int mouseButtonDown)
3340
{
3441
return(mouseButtonDowns.contains(mouseButtonDown));
3542
}
43+
44+
public static boolean isMouseDown() {
45+
return mouseButtonDowns.size() > 0;
46+
}
47+
48+
public static boolean isMousePressed(){
49+
boolean temp = mouseIsPressed;
50+
mouseIsPressed = false;
51+
return temp;
52+
}
3653
/**
3754
* @param keyCode the key to check
3855
* @return true if the keyCode is held down
3956
*/
4057
public static boolean isKeyDown(int keyCode) {
4158
return(keyDowns.contains(keyCode));
4259
}
60+
4361
public static void addKey(KeyEvent e){
4462
if(!isKeyDown(e.getKeyCode()))
4563
keyDowns.add(new Integer(e.getKeyCode()));

src/JavaGameEngine/Backend/Input/Keys.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ public class Keys {
1010
public static final int LEFTARROW = 37;
1111
public static final int RIGHTARROW = 40;
1212
public static final int SPACE = 32;
13+
public static final int RIGHTCLICK = 1;
14+
public static final int LEFTCLICK = 3;
15+
public static final int MIDDLECLICK = 2;
1316

1417
}

src/JavaGameEngine/Components/Component.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package JavaGameEngine.Components;
22

33
import JavaGameEngine.Backend.ComponentHandler;
4+
import JavaGameEngine.Backend.Input.Input;
45
import JavaGameEngine.Backend.UpdateThread;
56
import JavaGameEngine.Components.Collider.Collider;
67
import JavaGameEngine.Components.Collider.SquareCollider;
@@ -178,7 +179,7 @@ public void removeChild(Component child){
178179
* @param c the object to instantiate
179180
*/
180181
public void instantiate(Component c){
181-
//UpdateThread.newObjects.add(c);
182+
UpdateThread.newObjects.add(c);
182183
}
183184
/**
184185
* This method will destroy this object it will remove it from the component handler
@@ -205,15 +206,48 @@ public void update() {
205206
float y = (parent.getPosition().getY()-((getScale().getY()/2)));
206207

207208
setPosition(new Vector2(x,y).add(getLocalPosition())); // we get the parents position and we add our localPosition
209+
/* update this in the setScale and setRotation instead
210+
setRotation(parent.getRotation().add(getLocalRotation()));
211+
setScale(parent.getScale().add(getLocalScale()));
212+
*/
208213

209-
setRotation(parent.getRotation().add(getLocalRotation()));
210-
setScale(parent.getScale().add(getLocalScale()));
211214
}
212215
if(components.size()>0){
213216
updateChildren(); // updates all the children
214217
}
218+
//mouse enter and exit
219+
if(insideComp()){
220+
if(!isMouseInside()){
221+
onMouseEntered();
222+
setMouseInside(true);
223+
}
224+
}
225+
else if (isMouseInside()){
226+
onMouseExit();
227+
setMouseInside(false);
228+
}
229+
if(isMouseInside()&&Input.isMousePressed()){
230+
onMousePressed();
231+
}
215232
}
233+
private boolean insideComp(){
234+
float width = getScale().getX()/2;
235+
float height = getScale().getY()/2;
236+
237+
float xMin = getPosition().getX()-width;
238+
float xMax = getPosition().getX()+width;
216239

240+
float yMin = getPosition().getY()-height;
241+
float yMax = getPosition().getY()+height;
242+
243+
float mx = Input.getMousePosition().getX();
244+
float my = Input.getMousePosition().getY();
245+
246+
if(mx>xMin&&mx<xMax&&my>yMin&&my<yMax){
247+
return true;
248+
}
249+
return false;
250+
}
217251
public void updateChildren(){
218252
LinkedList<Component> s = getChildren();
219253
for (Component component :s) {

0 commit comments

Comments
 (0)