|
| 1 | +/** |
| 2 | + * |
| 3 | + * LiquidFunProcessing | Copyright 2017 Thomas Diewald - www.thomasdiewald.com |
| 4 | + * |
| 5 | + * https://github.com/diwi/LiquidFunProcessing.git |
| 6 | + * |
| 7 | + * Box2d / LiquidFun Library for Processing. |
| 8 | + * MIT License: https://opensource.org/licenses/MIT |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +package com.thomasdiewald.liquidfun.java.interaction; |
| 14 | + |
| 15 | +import org.jbox2d.callbacks.QueryCallback; |
| 16 | +import org.jbox2d.collision.AABB; |
| 17 | +import org.jbox2d.common.Vec2; |
| 18 | +import org.jbox2d.dynamics.Body; |
| 19 | +import org.jbox2d.dynamics.BodyDef; |
| 20 | +import org.jbox2d.dynamics.BodyType; |
| 21 | +import org.jbox2d.dynamics.Fixture; |
| 22 | +import org.jbox2d.dynamics.World; |
| 23 | +import org.jbox2d.dynamics.joints.MouseJoint; |
| 24 | +import org.jbox2d.dynamics.joints.MouseJointDef; |
| 25 | + |
| 26 | +import com.thomasdiewald.liquidfun.java.DwViewportTransform; |
| 27 | + |
| 28 | +import processing.core.PConstants; |
| 29 | +import processing.event.KeyEvent; |
| 30 | +import processing.event.MouseEvent; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * |
| 35 | + * @author Thomas Diewald |
| 36 | + * |
| 37 | + */ |
| 38 | +public class DwMouseDragBodies implements DwInteractionEvent, QueryCallback{ |
| 39 | + |
| 40 | + public World world; |
| 41 | + public DwViewportTransform vptransform; |
| 42 | + |
| 43 | + public boolean active = false; |
| 44 | + public AABB aabb = new AABB(); |
| 45 | + public Vec2 point = new Vec2(); |
| 46 | + public Fixture fixture = null; |
| 47 | + public MouseJoint mouse_joint; |
| 48 | + |
| 49 | + public final Vec2 mouse = new Vec2(); |
| 50 | + public float mult_dragforce = 1000000f; |
| 51 | + public boolean enable_static_drag = false; |
| 52 | + |
| 53 | + public int button = PConstants.LEFT; |
| 54 | +// public int button = PConstants.CENTER; |
| 55 | +// public int button = PConstants.RIGHT; |
| 56 | + |
| 57 | + public DwMouseDragBodies(World world, DwViewportTransform vptransform){ |
| 58 | + this.world = world; |
| 59 | + this.vptransform = vptransform; |
| 60 | + } |
| 61 | + |
| 62 | +// public boolean query(Vec2 world_xy){ |
| 63 | +// fixture = null; |
| 64 | +// point.set(world_xy); |
| 65 | +// aabb.lowerBound.set(point.x - 0.001f, point.y - 0.001f); |
| 66 | +// aabb.upperBound.set(point.x + 0.001f, point.y + 0.001f); |
| 67 | +// world.queryAABB(this, aabb); |
| 68 | +// return fixture != null; |
| 69 | +// } |
| 70 | + |
| 71 | + public boolean query(float screen_x, float screen_y){ |
| 72 | + vptransform.getScreen2box(screen_x, screen_y, mouse); |
| 73 | + fixture = null; |
| 74 | + point.set(mouse); |
| 75 | + aabb.lowerBound.set(point.x - 0.001f, point.y - 0.001f); |
| 76 | + aabb.upperBound.set(point.x + 0.001f, point.y + 0.001f); |
| 77 | + world.queryAABB(this, aabb); |
| 78 | + return fixture != null; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public void press(float screen_x, float screen_y) { |
| 83 | + if (query(screen_x, screen_y)) { |
| 84 | + Body body = fixture.getBody(); |
| 85 | + MouseJointDef def = new MouseJointDef(); |
| 86 | + def.bodyA = world.createBody(new BodyDef()); |
| 87 | + def.bodyB = fixture.getBody(); |
| 88 | + def.collideConnected = true; |
| 89 | + def.target.set(point); |
| 90 | + def.maxForce = mult_dragforce * body.getMass(); |
| 91 | + mouse_joint = (MouseJoint) world.createJoint(def); |
| 92 | + body.setAwake(true); |
| 93 | + } |
| 94 | + active = (mouse_joint != null); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + public void update(float screen_x, float screen_y) { |
| 99 | + if (active) { |
| 100 | + vptransform.getScreen2box(screen_x, screen_y, mouse); |
| 101 | + mouse_joint.setTarget(mouse); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + public void release(float screen_x, float screen_y) { |
| 107 | + if (active) { |
| 108 | + if(body_type_cpy != null){ |
| 109 | + fixture.getBody().setType(body_type_cpy); |
| 110 | + body_type_cpy = null; |
| 111 | + } |
| 112 | + fixture = null; |
| 113 | + world.destroyJoint(mouse_joint); |
| 114 | + mouse_joint = null; |
| 115 | + } |
| 116 | + active = false; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + public boolean is_enabled = true; |
| 122 | + |
| 123 | + @Override |
| 124 | + public void enable(boolean enable) { |
| 125 | + is_enabled = enable; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean isEnabled() { |
| 130 | + return is_enabled; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean isActive(){ |
| 137 | + return active; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void setMouseButton(int button){ |
| 142 | + this.button = button; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int getMouseButton(){ |
| 147 | + return button; |
| 148 | + } |
| 149 | + |
| 150 | + public BodyType body_type_cpy = null; |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean reportFixture(Fixture argFixture) { |
| 154 | + Body body = argFixture.getBody(); |
| 155 | + |
| 156 | + // static bodies can be moved too, but need to set to dynamic for the |
| 157 | + // mousejoint to work. |
| 158 | + body_type_cpy = body.getType(); |
| 159 | + if(enable_static_drag){ |
| 160 | + body.setType(BodyType.DYNAMIC); |
| 161 | + } |
| 162 | + |
| 163 | + if (body.getType() == BodyType.DYNAMIC) |
| 164 | + { |
| 165 | + if (argFixture.testPoint(point)) { |
| 166 | + fixture = argFixture; |
| 167 | + return false; |
| 168 | + } |
| 169 | + } |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + int mouseX_off = 0; |
| 175 | + int mouseY_off = 0; |
| 176 | + int mouseX; |
| 177 | + int mouseY; |
| 178 | + |
| 179 | + @Override |
| 180 | + public void mouseEvent(MouseEvent event){ |
| 181 | + if(!is_enabled){ |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + mouseX = mouseX_off + event.getX(); |
| 186 | + mouseY = mouseY_off + event.getY(); |
| 187 | + if(event.getButton() == this.button){ |
| 188 | + switch(event.getAction()){ |
| 189 | + case MouseEvent.PRESS: press (mouseX, mouseY); break; |
| 190 | + case MouseEvent.RELEASE: release(mouseX, mouseY); break; |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + public int key_combi = 0; |
| 196 | + public boolean key_combi_active = true; |
| 197 | + public boolean key_combi_enabled = true; |
| 198 | + |
| 199 | + @Override |
| 200 | + public void keyEvent(KeyEvent event){ |
| 201 | + switch(event.getAction()){ |
| 202 | + case KeyEvent.PRESS: |
| 203 | + key_combi_active = false; |
| 204 | + break; |
| 205 | + case KeyEvent.RELEASE: |
| 206 | + key_combi_active = true; |
| 207 | + break; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public void updateEvent(){ |
| 213 | + update(mouseX, mouseY); |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public void setMouseOffset(int mouseX_off, int mouseY_off){ |
| 218 | + this.mouseX_off = mouseX_off; |
| 219 | + this.mouseY_off = mouseY_off; |
| 220 | + } |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | +} |
| 225 | + |
0 commit comments