Mouse block dragging.
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
79a410a563
commit
8ef5b04212
BIN
Meteo_Engine.jar
BIN
Meteo_Engine.jar
Binary file not shown.
@ -270,51 +270,59 @@ public class Board {
|
||||
g.drawString(Integer.toString(blockData.size()),4,Meteo.SCREEN_HEIGHT-20);
|
||||
}
|
||||
}
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
//System.out.println("Clicked: "+e.getPoint());
|
||||
}
|
||||
public void mousePressed(MouseEvent e) {
|
||||
//System.out.println("Pressed: "+e.getPoint());
|
||||
//Adjust Y coordinate based on where the board is positioned.
|
||||
clickBlock=null;
|
||||
clumpClickId=null;
|
||||
outer:
|
||||
for (int i=0;i<blockData.size();i++) {
|
||||
BlockClump bc = blockData.get(i);
|
||||
for (int j=0;j<bc.getBlocks().size();j++) {
|
||||
Block b = bc.getBlocks().get(j);
|
||||
if (new Rectangle(b.draw_x,b.draw_y,block_width,block_height).contains(e.getPoint())) {
|
||||
clickBlock=b;
|
||||
clumpClickId=bc;
|
||||
break outer;
|
||||
public void handleMouse(MouseQueue mq) {
|
||||
MouseEvent e = mq.ev;
|
||||
switch (mq.type) {
|
||||
case CLICK:
|
||||
//System.out.println("Clicked: "+e.getPoint());
|
||||
break;
|
||||
case DRAG:
|
||||
//System.out.println("Dragged: "+e.getPoint());
|
||||
if (clumpClickId!=null&&clickBlock!=null) {
|
||||
List<Block> adjacentBlocks = clumpClickId.getBlocks().stream().filter((block)->Math.abs(clickBlock.y-block.y)==1&&clickBlock.x==block.x).collect(Collectors.toList());
|
||||
for (Block b : adjacentBlocks) {
|
||||
if (new Rectangle(0,b.draw_y,Meteo.SCREEN_WIDTH,block_height).contains(e.getPoint())) {
|
||||
int newY = b.y;
|
||||
b.y=clickBlock.y;
|
||||
clickBlock.y = newY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
//System.out.println("Released: "+e.getPoint());
|
||||
}
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
//System.out.println("Entered: "+e.getPoint());
|
||||
}
|
||||
public void mouseExited(MouseEvent e) {
|
||||
//System.out.println("Exited: "+e.getPoint());
|
||||
}
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
//System.out.println("Dragged: "+e.getPoint());
|
||||
if (clumpClickId!=null&&clickBlock!=null) {
|
||||
List<Block> adjacentBlocks = clumpClickId.getBlocks().stream().filter((block)->Math.abs(clickBlock.y-block.y)==1).collect(Collectors.toList());
|
||||
for (Block b : adjacentBlocks) {
|
||||
if (new Rectangle(b.draw_x,b.draw_y,block_width,block_height).contains(e.getPoint())) {
|
||||
int oldY = clickBlock.y;
|
||||
clickBlock.y = b.y;
|
||||
b.y=oldY;
|
||||
break;
|
||||
break;
|
||||
case ENTER:
|
||||
//System.out.println("Entered: "+e.getPoint());
|
||||
break;
|
||||
case EXIT:
|
||||
//System.out.println("Exited: "+e.getPoint());
|
||||
break;
|
||||
case MOVE:
|
||||
//System.out.println("Moved: "+e.getPoint());
|
||||
break;
|
||||
case PRESS:
|
||||
//System.out.println("Pressed: "+e.getPoint());
|
||||
//Adjust Y coordinate based on where the board is positioned.
|
||||
clickBlock=null;
|
||||
clumpClickId=null;
|
||||
outer:
|
||||
for (int i=0;i<blockData.size();i++) {
|
||||
BlockClump bc = blockData.get(i);
|
||||
for (int j=0;j<bc.getBlocks().size();j++) {
|
||||
Block b = bc.getBlocks().get(j);
|
||||
if (new Rectangle(b.draw_x,b.draw_y,block_width,block_height).contains(e.getPoint())) {
|
||||
clickBlock=b;
|
||||
clumpClickId=bc;
|
||||
break outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RELEASE:
|
||||
//System.out.println("Released: "+e.getPoint());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
//System.out.println("Moved: "+e.getPoint());
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package sig;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
@ -21,8 +23,15 @@ public class Meteo implements MouseListener,MouseMotionListener{
|
||||
public final static long TIMEPERTICK = 16666667l;
|
||||
public static DebugMode DEBUG_DRAWING = DebugMode.MODE2;
|
||||
|
||||
public static List<MouseQueue> MOUSE_QUEUE = new ArrayList<MouseQueue>();
|
||||
|
||||
public static void runGameLoop() {
|
||||
FRAMECOUNT++;
|
||||
for (int i=0;i<MOUSE_QUEUE.size();i++) {
|
||||
MouseQueue mq = MOUSE_QUEUE.get(i);
|
||||
b.handleMouse(mq);
|
||||
}
|
||||
MOUSE_QUEUE.clear();
|
||||
b.run(FRAMECOUNT);
|
||||
}
|
||||
|
||||
@ -75,36 +84,36 @@ public class Meteo implements MouseListener,MouseMotionListener{
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
b.mouseClicked(e);
|
||||
MOUSE_QUEUE.add(new MouseQueue(MouseEventType.CLICK,e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
b.mousePressed(e);
|
||||
MOUSE_QUEUE.add(new MouseQueue(MouseEventType.PRESS,e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
b.mouseReleased(e);
|
||||
MOUSE_QUEUE.add(new MouseQueue(MouseEventType.RELEASE,e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
b.mouseEntered(e);
|
||||
MOUSE_QUEUE.add(new MouseQueue(MouseEventType.ENTER,e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
b.mouseExited(e);
|
||||
MOUSE_QUEUE.add(new MouseQueue(MouseEventType.EXIT,e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
b.mouseDragged(e);
|
||||
MOUSE_QUEUE.add(new MouseQueue(MouseEventType.DRAG,e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
b.mouseMoved(e);
|
||||
MOUSE_QUEUE.add(new MouseQueue(MouseEventType.MOVE,e));
|
||||
}
|
||||
}
|
11
src/sig/MouseEventType.java
Normal file
11
src/sig/MouseEventType.java
Normal file
@ -0,0 +1,11 @@
|
||||
package sig;
|
||||
|
||||
public enum MouseEventType {
|
||||
CLICK,
|
||||
PRESS,
|
||||
RELEASE,
|
||||
ENTER,
|
||||
EXIT,
|
||||
DRAG,
|
||||
MOVE;
|
||||
}
|
12
src/sig/MouseQueue.java
Normal file
12
src/sig/MouseQueue.java
Normal file
@ -0,0 +1,12 @@
|
||||
package sig;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class MouseQueue {
|
||||
MouseEventType type;
|
||||
MouseEvent ev;
|
||||
MouseQueue(MouseEventType type,MouseEvent ev) {
|
||||
this.type=type;
|
||||
this.ev=ev;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user