Skip to content

Commit

Permalink
Merge pull request #3 from phyohtetarkar/develop
Browse files Browse the repository at this point in the history
fixed mouse position focus
  • Loading branch information
phyohtetarkar authored Nov 6, 2023
2 parents 860ae35 + 16d97fe commit 256ccae
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 65 deletions.
22 changes: 21 additions & 1 deletion core/src/aurelienribon/bodyeditor/canvas/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,30 @@ public void dispose() {
// Public API
// -------------------------------------------------------------------------

public static Vector2 getCanvasBound(float x, float y) {
Vector2 vec2 = new Vector2(1, 1);
int w = Gdx.graphics.getWidth() - OFFSET_X;
if (x >= w) {
vec2.x = 0;
}
if (y <= OFFSET_Y) {
vec2.y = 0;
}

return vec2;
};

public Vector2 screenToWorld(int x, int y) {
int w = Gdx.graphics.getWidth() - OFFSET_X;
int h = Gdx.graphics.getHeight() - OFFSET_Y;
Vector3 v3 = worldCamera.unproject(new Vector3(x, y, 0), 0, 0, w, h);
Vector2 bound = getCanvasBound(x, y);
if (bound.x == 0) {
x = w - 5;
}
if (bound.y == 0) {
y = OFFSET_Y + 4;
}
Vector3 v3 = worldCamera.unproject(new Vector3(x, y, 0), 0, 0,w, h);
return new Vector2(v3.x, v3.y);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public PanZoomInputProcessor(Canvas canvas) {
public boolean touchDown(int x, int y, int pointer, int button) {
if (button != Buttons.RIGHT) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

Vector2 p = canvas.screenToWorld(x, y);
lastTouch.set(p);
return false;
Expand All @@ -30,6 +33,8 @@ public boolean touchDown(int x, int y, int pointer, int button) {
@Override
public boolean touchDragged(int x, int y, int pointer) {
if (!Gdx.input.isButtonPressed(Buttons.RIGHT)) return false;
Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

Vector2 p = canvas.screenToWorld(x, y);
Vector2 delta = new Vector2(p).sub(lastTouch);
Expand All @@ -41,6 +46,9 @@ public boolean touchDragged(int x, int y, int pointer) {

@Override
public boolean scrolled(float amountX, float amountY) {
Vector2 bound = Canvas.getCanvasBound(Gdx.input.getX(), Gdx.input.getY());
if (bound.x == 0 || bound.y == 0) return false;

if (zoomLevel == zoomLevels[0] && amountY < 0) {
zoomLevel = zoomLevels[1];
} else if (zoomLevel == zoomLevels[zoomLevels.length - 1] && amountY > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public boolean touchDown(int x, int y, int pointer, int button) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);

if (bound.isZero()) return false;

List<ShapeModel> shapes = model.getShapes();
ShapeModel lastShape = shapes.isEmpty() ? null : shapes.get(shapes.size() - 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public boolean touchDown(int x, int y, int pointer, int button) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);

if (bound.isZero()) return false;

draggedPoint = screen.nearestPoint;

if (draggedPoint == null) {
Expand Down Expand Up @@ -65,6 +69,9 @@ public boolean touchUp(int x, int y, int pointer, int button) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

if (draggedPoint != null) {
draggedPoint = null;
model.computePhysics();
Expand Down Expand Up @@ -96,6 +103,9 @@ public boolean touchDragged(int x, int y, int pointer) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

if (draggedPoint != null) {
Vector2 p = canvas.alignedScreenToWorld(x, y);
model.clearPhysics();
Expand All @@ -121,6 +131,9 @@ public boolean mouseMoved(int x, int y) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

// Nearest point computation

Vector2 p = canvas.screenToWorld(x, y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public boolean touchDown(int x, int y, int pointer, int button) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

Vector2 p = canvas.screenToWorld(x, y);
screen.ballThrowP1 = p;
screen.ballThrowP2 = p;
Expand All @@ -43,6 +46,9 @@ public boolean touchUp(int x, int y, int pointer, int button) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

Vector2 p1 = screen.ballThrowP1;
Vector2 p2 = screen.ballThrowP2;
Vector2 delta = new Vector2(p2).sub(p1);
Expand All @@ -60,6 +66,9 @@ public boolean touchDragged(int x, int y, int pointer) {
RigidBodyModel model = Ctx.bodies.getSelectedModel();
if (model == null) return false;

Vector2 bound = Canvas.getCanvasBound(x, y);
if (bound.isZero()) return false;

Vector2 p = canvas.screenToWorld(x, y);
screen.ballThrowP2 = p;
return false;
Expand Down
67 changes: 48 additions & 19 deletions core/src/aurelienribon/bodyeditor/ui/OptionsActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

import aurelienribon.bodyeditor.Settings;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.kotcrab.vis.ui.widget.VisCheckBox;
import com.kotcrab.vis.ui.widget.VisLabel;
import com.kotcrab.vis.ui.widget.VisSelectBox;
import com.kotcrab.vis.ui.widget.VisTable;

import java.util.Set;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.kotcrab.vis.ui.VisUI;
import com.kotcrab.vis.ui.widget.*;

/**
* @author phyohtetarkar
*/
public class OptionsActor extends VisTable {
public class OptionsActor extends VisScrollPane {

public OptionsActor() {
super(true);
super(new VisTable(true));

VisTable table = (VisTable) getActor();

VisCheckBox cbDrawBackground = new VisCheckBox("Draw background image", Settings.isImageDrawn);
VisCheckBox cbDrawConvexPolygon = new VisCheckBox("Draw convex polygons", Settings.isPolygonDrawn);
VisCheckBox cbDrawGridWithGap = new VisCheckBox("Draw grid with gap", Settings.isGridShown);
Expand Down Expand Up @@ -69,19 +72,45 @@ public void changed(ChangeEvent event, Actor actor) {
}
});

left().top().pad(12);
table.left().top().pad(12);

VisLabel label = new VisLabel("Options");
add(label).left().colspan(3);
row();
add(cbDrawBackground).left().padTop(4);
add(cbDrawConvexPolygon).left();
add(cbDrawGridWithGap).left();
add(selectBox).left();
row();
add(cbDrawShapes).left();
add(cbDebugPhysics).left();
add(cbEnableSnapGrid).colspan(2).left();
table.add(label).left().colspan(3);
table.row();
table.add(cbDrawBackground).left().padTop(4);
table.add(cbDrawConvexPolygon).left();
table.add(cbDrawGridWithGap).left();
table.add(selectBox).left();
table.row();
table.add(cbDrawShapes).left();
table.add(cbDebugPhysics).left();
table.add(cbEnableSnapGrid).colspan(2).left();

Drawable bg = VisUI.getSkin().getDrawable("window-bg");
table.setBackground(bg);

pack();

addListener(new InputListener() {
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
Stage stage = event.getStage();
boolean hovered = hit(x ,y, isTouchable()) != null;
if (hasScrollFocus() && stage != null && !hovered) {
stage.setScrollFocus(null);
}
}

@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
Stage stage = event.getStage();
boolean hovered = hit(x ,y, isTouchable()) != null;
if (!hasScrollFocus() && stage != null && hovered) {
stage.setScrollFocus(OptionsActor.this);
}

}
});
}

}
36 changes: 29 additions & 7 deletions core/src/aurelienribon/bodyeditor/ui/RigidBodiesListActor.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package aurelienribon.bodyeditor.ui;

import aurelienribon.bodyeditor.Ctx;
import aurelienribon.bodyeditor.RigidBodiesManager;
import aurelienribon.bodyeditor.models.RigidBodyModel;
import aurelienribon.utils.notifications.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;
import com.kotcrab.vis.ui.widget.ListView;
import com.kotcrab.vis.ui.widget.VisScrollPane;
Expand All @@ -22,11 +23,32 @@ public RigidBodiesListActor(RigidBodyListAdapter adapter) {
getMainTable().getListView().getMainTable().top();

VisScrollPane scrollPane = getScrollPane();
scrollPane.getListeners().forEach(eventListener -> {
if (eventListener instanceof ActorGestureListener) {
return;
// scrollPane.getListeners().forEach(eventListener -> {
// if (eventListener instanceof ActorGestureListener) {
// return;
// }
// scrollPane.removeListener(eventListener);
// });

scrollPane.addListener(new InputListener() {
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
Stage stage = event.getStage();
boolean hovered = scrollPane.hit(x ,y, scrollPane.isTouchable()) != null;
if (scrollPane.hasScrollFocus() && stage != null && !hovered) {
stage.setScrollFocus(null);
}
}

@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
Stage stage = event.getStage();
boolean hovered = scrollPane.hit(x ,y, scrollPane.isTouchable()) != null;
if (!scrollPane.hasScrollFocus() && stage != null && hovered) {
stage.setScrollFocus(scrollPane);
}

}
scrollPane.removeListener(eventListener);
});

Ctx.bodies.getModels().addListChangedListener((source, added, removed) -> {
Expand Down
75 changes: 37 additions & 38 deletions core/src/com/phyohtet/bodyeditor/PhysicsBodyEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.HdpiUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
Expand All @@ -28,7 +28,7 @@
*/
public class PhysicsBodyEditor extends Game {

public static final String version = "v1.0.0";
public static final String version = "v1.0.1";

SpriteBatch batch;

Expand All @@ -52,48 +52,14 @@ public void create() {
VisTable table = new VisTable(false);
table.setFillParent(true);

OptionsActor optionsActor = new OptionsActor();
VisScrollPane optionsPanel = new VisScrollPane(optionsActor);
optionsPanel.setFadeScrollBars(false);
optionsPanel.getListeners().forEach(eventListener -> {
if (eventListener instanceof ActorGestureListener) {
return;
}
optionsPanel.removeListener(eventListener);
});
Actor optionsPanel = buildOptionsPanel();

RigidBodyListAdapter adapter = new RigidBodyListAdapter(new Array<>());
RigidBodiesListActor rigidBodiesListActor = new RigidBodiesListActor(adapter);

VisLabel versionLabel = new VisLabel(version);
versionLabel.setColor(1, 1, 1, 0.8f);


VisTable rightPanel = new VisTable(false);
rightPanel.top().left();
rightPanel.add(new HeadingActor()).fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(new ProjectActor()).fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(new RigidBodiesListHeaderActor(adapter)).fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(rigidBodiesListActor.getMainTable()).expandY().fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(versionLabel).right().padRight(10).padTop(8).padBottom(8).expandX();
Actor rightPanel = buildRightPanel();

table.add(optionsPanel).height(Canvas.OFFSET_Y).expandX().fillX().left().top();
table.addSeparator(true);
table.add(rightPanel).width(Canvas.OFFSET_X).top().expandY().fill();

Drawable bg = VisUI.getSkin().getDrawable("window-bg");
optionsActor.setBackground(bg);
rightPanel.setBackground(bg);


stage.addActor(table);

input.addProcessor(stage);
Expand Down Expand Up @@ -127,4 +93,37 @@ public void dispose() {
batch.dispose();
VisUI.dispose();
}

private Actor buildOptionsPanel() {
return new OptionsActor();
}

private Actor buildRightPanel() {
RigidBodyListAdapter adapter = new RigidBodyListAdapter(new Array<>());
RigidBodiesListActor rigidBodiesListActor = new RigidBodiesListActor(adapter);

VisLabel versionLabel = new VisLabel(version);
versionLabel.setColor(1, 1, 1, 0.8f);

VisTable rightPanel = new VisTable(false);
rightPanel.top().left();
rightPanel.add(new HeadingActor()).fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(new ProjectActor()).fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(new RigidBodiesListHeaderActor(adapter)).fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(rigidBodiesListActor.getMainTable()).expandY().fill();
rightPanel.row();
rightPanel.addSeparator();
rightPanel.add(versionLabel).right().padRight(10).padTop(8).padBottom(8).expandX();

Drawable bg = VisUI.getSkin().getDrawable("window-bg");
rightPanel.setBackground(bg);

return rightPanel;
}
}

0 comments on commit 256ccae

Please sign in to comment.