Support Circle shapes for physics bodies

This commit is contained in:
fgnm
2021-09-12 19:35:24 +02:00
parent dfbc70b7af
commit 0c431080b4
11 changed files with 171 additions and 5 deletions
+1
View File
@@ -3,6 +3,7 @@
- [BREAK CHANGE] Update Spine Support to `4.0.18.1`
- Update libGDX to 1.10.1-SNAPSHOT
- Support mono space fonts
- Support Circle shapes for physics bodies
= Editor =
- Update LWJGL to 3.3.0-SNAPSHOT
@@ -91,6 +91,7 @@ public class BootstrapCommand extends SimpleCommand {
facade.registerCommand(MsgAPI.ACTION_UPDATE_PARTICLE_DATA, UpdateParticleDataCommand::new);
facade.registerCommand(MsgAPI.ACTION_UPDATE_TALOS_DATA, UpdateTalosDataCommand::new);
facade.registerCommand(MsgAPI.ACTION_UPDATE_BODY_LIGHT_DATA, UpdateLightBodyDataCommand::new);
facade.registerCommand(MsgAPI.ACTION_UPDATE_CIRCLE_SHAPE, UpdateCircleShapeCommand::new);
facade.registerCommand(MsgAPI.ACTION_UPDATE_PHYSICS_BODY_DATA, UpdatePhysicsDataCommand::new);
facade.registerCommand(MsgAPI.ACTION_UPDATE_SENSOR_DATA, UpdateSensorDataCommand::new);
facade.registerCommand(MsgAPI.ACTION_UPDATE_SHADER_DATA, UpdateShaderDataCommand::new);
@@ -20,7 +20,7 @@ package games.rednblack.editor.controller.commands;
import com.badlogic.gdx.math.Vector2;
import games.rednblack.editor.factory.ItemFactory;
import games.rednblack.editor.renderer.data.ShapeVO;
import games.rednblack.editor.renderer.data.PolygonShapeVO;
/**
* Created by azakhary on 10/21/2015.
@@ -30,7 +30,7 @@ public class CreatePrimitiveCommand extends EntityModifyRevertibleCommand {
@Override
public void doAction() {
Vector2 position = new Vector2(0, 0);
ShapeVO shape = ShapeVO.createRect(100f / sandbox.getPixelPerWU(), 100f / sandbox.getPixelPerWU());
PolygonShapeVO shape = PolygonShapeVO.createRect(100f / sandbox.getPixelPerWU(), 100f / sandbox.getPixelPerWU());
ItemFactory.get().createPrimitive(position, shape);
}
@@ -0,0 +1,54 @@
package games.rednblack.editor.controller.commands.component;
import games.rednblack.editor.HyperLap2DFacade;
import games.rednblack.editor.controller.commands.EntityModifyRevertibleCommand;
import games.rednblack.editor.renderer.components.CircleShapeComponent;
import games.rednblack.editor.renderer.components.physics.PhysicsBodyComponent;
import games.rednblack.editor.utils.runtime.EntityUtils;
import games.rednblack.editor.utils.runtime.SandboxComponentRetriever;
import games.rednblack.h2d.common.MsgAPI;
public class UpdateCircleShapeCommand extends EntityModifyRevertibleCommand {
private int entityId;
private float radiusBackup;
@Override
public void doAction() {
Object[] payload = getNotification().getBody();
int entity = (int) payload[0];
float radius = (float) payload[1];
entityId = EntityUtils.getEntityId(entity);
CircleShapeComponent circleShapeComponent = SandboxComponentRetriever.get(entity, CircleShapeComponent.class);
radiusBackup = circleShapeComponent.radius;
circleShapeComponent.radius = radius;
PhysicsBodyComponent physicsBodyComponent = SandboxComponentRetriever.get(entity, PhysicsBodyComponent.class);
physicsBodyComponent.scheduleRefresh();
HyperLap2DFacade.getInstance().sendNotification(MsgAPI.ITEM_DATA_UPDATED, entity);
}
@Override
public void undoAction() {
int entity = EntityUtils.getByUniqueId(entityId);
CircleShapeComponent circleShapeComponent = SandboxComponentRetriever.get(entity, CircleShapeComponent.class);
circleShapeComponent.radius = radiusBackup;
PhysicsBodyComponent physicsBodyComponent = SandboxComponentRetriever.get(entity, PhysicsBodyComponent.class);
physicsBodyComponent.scheduleRefresh();
HyperLap2DFacade.getInstance().sendNotification(MsgAPI.ITEM_DATA_UPDATED, entity);
}
public static Object payload(int entity, float radius) {
Object[] payload = new Object[2];
payload[0] = entity;
payload[1] = radius;
return payload;
}
}
@@ -135,7 +135,7 @@ public class ItemFactory implements IFactory {
return true;
}
public boolean createPrimitive(Vector2 position, ShapeVO shape) {
public boolean createPrimitive(Vector2 position, PolygonShapeVO shape) {
ColorPrimitiveVO vo = new ColorPrimitiveVO();
vo.shape = shape.clone();
vo.originX = vo.shape.polygons[0][2].x / 2;
@@ -23,6 +23,7 @@ import com.badlogic.gdx.utils.reflect.ReflectionException;
import games.rednblack.editor.HyperLap2DFacade;
import games.rednblack.editor.controller.commands.AddComponentToItemCommand;
import games.rednblack.editor.controller.commands.RemoveComponentFromItemCommand;
import games.rednblack.editor.renderer.components.CircleShapeComponent;
import games.rednblack.editor.renderer.components.PolygonComponent;
import games.rednblack.editor.renderer.components.ShaderComponent;
import games.rednblack.editor.renderer.components.light.LightBodyComponent;
@@ -192,6 +193,7 @@ public class UIMultiPropertyBoxMediator extends PanelMediator<UIMultiPropertyBox
ShaderComponent shaderComponent = SandboxComponentRetriever.get(entity, ShaderComponent.class);
LightBodyComponent lightComponent = SandboxComponentRetriever.get(entity, LightBodyComponent.class);
TypingLabelComponent typingLabelComponent = SandboxComponentRetriever.get(entity, TypingLabelComponent.class);
CircleShapeComponent circleShapeComponent = SandboxComponentRetriever.get(entity, CircleShapeComponent.class);
if (polygonComponent != null) {
mediatorNames.add(UIPolygonComponentPropertiesMediator.NAME);
@@ -211,6 +213,9 @@ public class UIMultiPropertyBoxMediator extends PanelMediator<UIMultiPropertyBox
if (typingLabelComponent != null) {
mediatorNames.add(UITypingLabelPropertiesMediator.NAME);
}
if (circleShapeComponent != null) {
mediatorNames.add(UICircleShapePropertiesMediator.NAME);
}
}
private void clearPropertyBoxes() {
@@ -60,6 +60,7 @@ public class UIBasicItemPropertiesMediator extends UIItemPropertiesMediator<UIBa
private TintComponent tintComponent;
public static final String POLYGON_COMPONENT_KEY = "Polygon Shape";
public static final String CIRCLE_SHAPE_COMPONENT_KEY = "Circle Shape";
public static final String PHYSICS_COMPONENT_KEY = "Physics";
public static final String SENSOR_COMPONENT_KEY = "Physics Sensors";
public static final String SHADER_COMPONENT_KEY = "Shader";
@@ -75,6 +76,7 @@ public class UIBasicItemPropertiesMediator extends UIItemPropertiesMediator<UIBa
@Override
public void onRegister() {
componentClassMap.put(POLYGON_COMPONENT_KEY, PolygonComponent.class);
componentClassMap.put(CIRCLE_SHAPE_COMPONENT_KEY, CircleShapeComponent.class);
componentClassMap.put(PHYSICS_COMPONENT_KEY, PhysicsBodyComponent.class);
componentClassMap.put(SENSOR_COMPONENT_KEY, SensorComponent.class);
componentClassMap.put(SHADER_COMPONENT_KEY, ShaderComponent.class);
@@ -0,0 +1,45 @@
package games.rednblack.editor.view.ui.properties.panels;
import com.badlogic.gdx.utils.Align;
import com.kotcrab.vis.ui.widget.VisLabel;
import com.kotcrab.vis.ui.widget.spinner.Spinner;
import games.rednblack.editor.HyperLap2DFacade;
import games.rednblack.editor.event.NumberSelectorOverlapListener;
import games.rednblack.editor.view.ui.properties.UIRemovableProperties;
import games.rednblack.h2d.common.view.ui.StandardWidgetsFactory;
public class UICircleShapeProperties extends UIRemovableProperties {
public static final String prefix = "games.rednblack.editor.view.ui.properties.panels.UICircleShapeProperties";
public static final String CLOSE_CLICKED = prefix + ".CLOSE_CLICKED";
private Spinner radiusSpinner;
public UICircleShapeProperties() {
super("Circle Shape");
radiusSpinner = StandardWidgetsFactory.createNumberSelector("default", 1.0f, 0.1f, 1000f, 0.1f);
mainTable.add(new VisLabel("Radius:", Align.right)).padRight(5).colspan(2).fillX();
mainTable.add(radiusSpinner).left().colspan(2);
mainTable.row().padTop(5);
addListeners();
}
public void setRadius(float radius) {
radiusSpinner.getTextField().setText(String.valueOf(radius));
}
public String getRadius() {
return radiusSpinner.getTextField().getText();
}
@Override
public void onRemove() {
HyperLap2DFacade.getInstance().sendNotification(CLOSE_CLICKED);
}
private void addListeners() {
radiusSpinner.addListener(new NumberSelectorOverlapListener(getUpdateEventName()));
}
}
@@ -0,0 +1,58 @@
package games.rednblack.editor.view.ui.properties.panels;
import games.rednblack.editor.HyperLap2DFacade;
import games.rednblack.editor.controller.commands.RemoveComponentFromItemCommand;
import games.rednblack.editor.controller.commands.component.UpdateCircleShapeCommand;
import games.rednblack.editor.renderer.components.CircleShapeComponent;
import games.rednblack.editor.utils.runtime.SandboxComponentRetriever;
import games.rednblack.editor.view.ui.properties.UIItemPropertiesMediator;
import games.rednblack.h2d.common.MsgAPI;
import org.apache.commons.lang3.ArrayUtils;
import org.puremvc.java.interfaces.INotification;
public class UICircleShapePropertiesMediator extends UIItemPropertiesMediator<UICircleShapeProperties> {
private static final String TAG = UICircleShapePropertiesMediator.class.getCanonicalName();
public static final String NAME = TAG;
public UICircleShapePropertiesMediator() {
super(NAME, new UICircleShapeProperties());
}
@Override
public String[] listNotificationInterests() {
String[] defaultNotifications = super.listNotificationInterests();
String[] notificationInterests = new String[]{
UICircleShapeProperties.CLOSE_CLICKED,
};
return ArrayUtils.addAll(defaultNotifications, notificationInterests);
}
@Override
public void handleNotification(INotification notification) {
super.handleNotification(notification);
switch (notification.getName()) {
case UICircleShapeProperties.CLOSE_CLICKED:
HyperLap2DFacade.getInstance().sendNotification(MsgAPI.ACTION_REMOVE_COMPONENT, RemoveComponentFromItemCommand.payload(observableReference, CircleShapeComponent.class));
break;
}
}
@Override
protected void translateObservableDataToView(int item) {
CircleShapeComponent component = SandboxComponentRetriever.get(item, CircleShapeComponent.class);
viewComponent.setRadius(component.radius);
}
@Override
protected void translateViewToItemData() {
CircleShapeComponent component = SandboxComponentRetriever.get(observableReference, CircleShapeComponent.class);
float radius = Float.parseFloat(viewComponent.getRadius());
if (component.radius != radius) {
Object payload = UpdateCircleShapeCommand.payload(observableReference, radius);
facade.sendNotification(MsgAPI.ACTION_UPDATE_CIRCLE_SHAPE, payload);
}
}
}