Auto Trace Polygon (2/2)

This commit is contained in:
MiChinao
2020-08-01 11:37:35 +02:00
parent 87a2ba732e
commit 9a9ab5c9de
6 changed files with 163 additions and 18 deletions
@@ -221,4 +221,10 @@ public class StandardWidgetsFactory {
button.addListener(new CursorListener(Cursors.FINGER, facade));
return button;
}
public static VisSlider createSlider(float min, float max, float step) {
VisSlider slider = new VisSlider(min, max, step, false);
slider.addListener(new CursorListener(Cursors.FINGER, facade));
return slider;
}
}
@@ -65,6 +65,7 @@ public class BootstrapViewCommand extends SimpleCommand {
facade.registerMediator(new EditSpriteAnimationPanelMediator());
facade.registerMediator(new AboutDialogMediator());
facade.registerMediator(new SettingsDialogMediator());
facade.registerMediator(new AutoTraceDialogMediator());
facade.registerMediator(new RulersUIMediator());
facade.registerMediator(new FollowersUIMediator());
@@ -10,13 +10,19 @@ import games.rednblack.editor.HyperLap2DFacade;
public class ButtonToNotificationListener extends ClickListener{
private String notificationName;
private Object payload;
public ButtonToNotificationListener(String notificationName) {
this.notificationName = notificationName;
}
public ButtonToNotificationListener(String notificationName, Object payload) {
this.notificationName = notificationName;
this.payload = payload;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
HyperLap2DFacade.getInstance().sendNotification(notificationName);
HyperLap2DFacade.getInstance().sendNotification(notificationName, payload);
}
}
@@ -1,12 +1,116 @@
package games.rednblack.editor.view.ui.dialog;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.Align;
import com.kotcrab.vis.ui.widget.VisCheckBox;
import com.kotcrab.vis.ui.widget.VisLabel;
import com.kotcrab.vis.ui.widget.VisSlider;
import com.kotcrab.vis.ui.widget.VisTextButton;
import games.rednblack.editor.event.ButtonToNotificationListener;
import games.rednblack.editor.utils.RoundUtils;
import games.rednblack.h2d.common.H2DDialog;
import games.rednblack.h2d.common.view.ui.StandardWidgetsFactory;
public class AutoTraceDialog extends H2DDialog {
private static final String prefix = "games.rednblack.editor.view.ui.dialog.AutoTraceDialog";
public static final String OPEN_DIALOG = prefix + "OPEN_DIALOG";
public static final String AUTO_TRACE_BUTTON_CLICKED = prefix + ".AUTO_TRACE_BUTTON_CLICKED";
private final VisSlider hullSlider;
private final VisLabel hullValue;
private final VisSlider alphaSlider;
private final VisLabel alphaValue;
private final VisCheckBox multiPartDetection;
private final VisCheckBox holeDetection;
private final VisTextButton cancelButton;
private final VisTextButton autoTraceButton;
public AutoTraceDialog() {
super("Auto Trace");
addCloseButton();
hullSlider = StandardWidgetsFactory.createSlider(0.9f, 50f, 0.1f);
hullValue = StandardWidgetsFactory.createLabel("2.5", "default", Align.center);
alphaSlider = StandardWidgetsFactory.createSlider(0, 255, 1);
alphaValue = StandardWidgetsFactory.createLabel("128", "default", Align.center);
multiPartDetection = StandardWidgetsFactory.createCheckBox("Multi Part Detection");
holeDetection = StandardWidgetsFactory.createCheckBox("Hole Detection");
cancelButton = StandardWidgetsFactory.createTextButton("Cancel");
autoTraceButton = StandardWidgetsFactory.createTextButton("Auto Trace");
getContentTable().add("Hull Tolerance : ").left();
getContentTable().add(hullSlider).left();
getContentTable().add(hullValue).width(25);
getContentTable().row().padTop(10);
getContentTable().add("Alpha Tolerance : ").left();
getContentTable().add(alphaSlider).left();
getContentTable().add(alphaValue).width(25);
getContentTable().row().padTop(10);
getContentTable().add(multiPartDetection);
getContentTable().add(holeDetection);
getContentTable().row().padTop(10);
getButtonsTable().add(cancelButton).right();
getButtonsTable().add(autoTraceButton).right();
setListeners();
hullSlider.setValue(2.5f);
alphaSlider.setValue(128);
}
public float getHullTolerance() {
return hullSlider.getValue();
}
public int getAlphaTolerance() {
return (int) alphaSlider.getValue();
}
public boolean isHoleDetection() {
return holeDetection.isChecked();
}
public boolean isMultiPartDetection() {
return multiPartDetection.isChecked();
}
private void setListeners() {
hullSlider.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
hullValue.setText(RoundUtils.round(hullSlider.getValue(), 1) +"");
}
});
alphaSlider.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
alphaValue.setText((int)(alphaSlider.getValue())+"");
}
});
cancelButton.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
close();
}
});
autoTraceButton.addListener(new ButtonToNotificationListener(AUTO_TRACE_BUTTON_CLICKED));
}
}
@@ -1,17 +1,27 @@
package games.rednblack.editor.view.ui.dialog;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.math.Vector2;
import com.puremvc.patterns.mediator.SimpleMediator;
import com.puremvc.patterns.observer.Notification;
import games.rednblack.editor.HyperLap2DFacade;
import games.rednblack.editor.view.menu.HelpMenu;
import games.rednblack.editor.renderer.components.PolygonComponent;
import games.rednblack.editor.renderer.components.TextureRegionComponent;
import games.rednblack.editor.utils.poly.PolygonUtils;
import games.rednblack.editor.utils.poly.tracer.Tracer;
import games.rednblack.editor.view.stage.Sandbox;
import games.rednblack.editor.view.stage.UIStage;
import games.rednblack.h2d.common.MsgAPI;
import java.util.stream.Stream;
public class AutoTraceDialogMediator extends SimpleMediator<AutoTraceDialog> {
private static final String TAG = AutoTraceDialogMediator.class.getCanonicalName();
private static final String NAME = TAG;
private Entity entity;
public AutoTraceDialogMediator() {
super(NAME, new AutoTraceDialog());
}
@@ -25,7 +35,8 @@ public class AutoTraceDialogMediator extends SimpleMediator<AutoTraceDialog> {
@Override
public String[] listNotificationInterests() {
return new String[]{
AutoTraceDialog.OPEN_DIALOG,
AutoTraceDialog.AUTO_TRACE_BUTTON_CLICKED
};
}
@@ -36,9 +47,37 @@ public class AutoTraceDialogMediator extends SimpleMediator<AutoTraceDialog> {
UIStage uiStage = sandbox.getUIStage();
switch (notification.getName()) {
case HelpMenu.ABOUT_DIALOG_OPEN:
case AutoTraceDialog.OPEN_DIALOG:
entity = notification.getBody();
viewComponent.show(uiStage);
break;
case AutoTraceDialog.AUTO_TRACE_BUTTON_CLICKED:
addAutoTraceMesh();
break;
}
}
private void addAutoTraceMesh() {
PolygonComponent polygonComponent = entity.getComponent(PolygonComponent.class);
if (polygonComponent != null) {
TextureRegionComponent textureRegionComponent = entity.getComponent(TextureRegionComponent.class);
if (!textureRegionComponent.regionName.equals("") && textureRegionComponent.region != null) {
polygonComponent.vertices = Tracer.trace(textureRegionComponent.region, viewComponent.getHullTolerance(),
viewComponent.getAlphaTolerance(), viewComponent.isMultiPartDetection(),
viewComponent.isHoleDetection());
if (polygonComponent.vertices != null) {
Vector2[] points = Stream.of(polygonComponent.vertices)
.flatMap(Stream::of)
.toArray(Vector2[]::new);
polygonComponent.vertices = PolygonUtils.polygonize(points);
HyperLap2DFacade.getInstance().sendNotification(MsgAPI.ITEM_DATA_UPDATED, entity);
}
}
}
}
}
@@ -23,6 +23,7 @@ import com.badlogic.gdx.math.Vector2;
import games.rednblack.editor.renderer.components.TextureRegionComponent;
import games.rednblack.editor.utils.poly.PolygonUtils;
import games.rednblack.editor.utils.poly.tracer.Tracer;
import games.rednblack.editor.view.ui.dialog.AutoTraceDialog;
import games.rednblack.h2d.common.MsgAPI;
import com.puremvc.patterns.observer.Notification;
import games.rednblack.editor.HyperLap2DFacade;
@@ -125,19 +126,7 @@ public class UIPolygonComponentPropertiesMediator extends UIItemPropertiesMediat
}
private void addAutoTraceMesh() {
TextureRegionComponent textureRegionComponent = observableReference.getComponent(TextureRegionComponent.class);
if (!textureRegionComponent.regionName.equals("") && textureRegionComponent.region != null) {
polygonComponent.vertices = Tracer.trace(textureRegionComponent.region, 2.5f, 128, false, false);
if (polygonComponent.vertices != null) {
Vector2[] points = Stream.of(polygonComponent.vertices)
.flatMap(Stream::of)
.toArray(Vector2[]::new);
polygonComponent.vertices = PolygonUtils.polygonize(points);
HyperLap2DFacade.getInstance().sendNotification(MsgAPI.ITEM_DATA_UPDATED, observableReference);
}
}
facade.sendNotification(AutoTraceDialog.OPEN_DIALOG, observableReference);
}
private void copyMesh() {