Add simplistic modal for part instantiation.

This commit is contained in:
kenkeiras 2018-01-21 00:14:46 +01:00
parent f464902399
commit 8fa2e22d55
6 changed files with 118 additions and 18 deletions

View File

@ -478,4 +478,10 @@ class CanvasView extends View implements PartGrid {
this._viewOrigin = new Tuple2<>(0, 0);
this.invalidate();
}
public Tuple2<Integer, Integer> getCenteredOn() {
int xOffset = _viewOrigin.item1 + getWidth() / 2;
int yOffset = _viewOrigin.item2 + getHeight() / 2;
return new Tuple2<>(xOffset, yOffset);
}
}

View File

@ -1,17 +1,16 @@
package com.codigoparallevar.minicards;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.types.Part;
public class MainActivity extends AppCompatActivity {
CanvasView canvasView;
@ -25,7 +24,7 @@ public class MainActivity extends AppCompatActivity {
boolean devMode = false;
FloatingActionButton removePartFab;
private PartsHolder partsHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -43,6 +42,8 @@ public class MainActivity extends AppCompatActivity {
canvasView = (CanvasView) findViewById(R.id.canvasView);
canvasView.setParentActivity(this);
partsHolder = new PartsHolder(this);
removePartFab = (FloatingActionButton) findViewById(R.id.remove_part_fab);
canvasView.setDropZone(
removePartFab.getX(), removePartFab.getX() + removePartFab.getWidth(),
@ -59,18 +60,7 @@ public class MainActivity extends AppCompatActivity {
AddPartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (canvasView != null) {
Part buttonPart = new RoundButton(
canvasView,
500, 1200,
80, 100);
canvasView.addPart(buttonPart);
Part boxPart = new ColorBox(canvasView,
400, 1100,
500, 1200);
canvasView.addPart(boxPart);
}
partsHolder.openAddPartModal(canvasView);
}
});

View File

@ -0,0 +1,15 @@
package com.codigoparallevar.minicards;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.PartGrid;
import com.codigoparallevar.minicards.types.Tuple2;
public abstract class PartInstantiator {
protected abstract Part instantiate(PartGrid grid, Tuple2<Integer, Integer> center);
public Part build(CanvasView canvasView) {
Tuple2<Integer, Integer> center = canvasView.getCenteredOn();
return instantiate(canvasView, center);
}
}

View File

@ -0,0 +1,61 @@
package com.codigoparallevar.minicards;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
import com.codigoparallevar.minicards.parts.samples.ColorBox;
import com.codigoparallevar.minicards.types.Part;
import com.codigoparallevar.minicards.types.Tuple2;
import java.util.List;
import java.util.Vector;
class PartsHolder {
private final Context context;
private final static List<Tuple2<String, PartInstantiator>> BuiltInParts =
new Vector<Tuple2<String, PartInstantiator>>(){{
add(new Tuple2<String, PartInstantiator>("Round button", RoundButton.getInstantiator()));
add(new Tuple2<String, PartInstantiator>("Red/Green box", ColorBox.getInstantiator()));
}};
public PartsHolder(Context context) {
this.context = context;
}
public void openAddPartModal(final CanvasView canvasView) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose part type")
.setItems(getPartTypes(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if ((which >= 0) && (which < BuiltInParts.size())){
Log.d("Minicards partsHolder",
"Spawning " + BuiltInParts.get(which).item1);
PartInstantiator instantiator = BuiltInParts.get(which).item2;
PartsHolder.this.runInstantiator(instantiator, canvasView);
}
}
});
Dialog dialog = builder.create();
dialog.show();
}
private void runInstantiator(PartInstantiator instantiator, CanvasView canvasView) {
Part part = instantiator.build(canvasView);
canvasView.addPart(part);
}
private static String[] getPartTypes() {
String[] partTypes = new String[BuiltInParts.size()];
for (int i = 0; i < BuiltInParts.size(); i++){
partTypes[i] = BuiltInParts.get(i).item1;
}
return partTypes;
}
}

View File

@ -1,10 +1,10 @@
package com.codigoparallevar.minicards.parts.buttons;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.PartConnection;
import com.codigoparallevar.minicards.types.Tuple2;
@ -39,6 +39,8 @@ public class RoundButton implements Part {
private final int _pathRunWay = 200;
private List<OutputConnector> _outputConnectors;
private final RoundOutputConnector _pressedOuputConnector;
private final static int DEFAULT_INNER_RADIUS = 80;
private final static int DEFAULT_OUTER_RADIUS = 100;
private RoundButton(String id, PartGrid partGrid, int xCenter, int yCenter, int innerRadius, int outerRadius) {
@ -249,4 +251,15 @@ public class RoundButton implements Part {
input.unlink();
}
}
public static PartInstantiator getInstantiator() {
return new PartInstantiator() {
@Override
protected Part instantiate(PartGrid grid, Tuple2<Integer, Integer> center) {
return new RoundButton(grid,
center.item1, center.item2,
DEFAULT_INNER_RADIUS, DEFAULT_OUTER_RADIUS);
}
};
}
}

View File

@ -6,7 +6,9 @@ import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import com.codigoparallevar.minicards.PartInstantiator;
import com.codigoparallevar.minicards.ScrolledCanvas;
import com.codigoparallevar.minicards.types.Tuple2;
import com.codigoparallevar.minicards.types.connectors.input.InputConnector;
import com.codigoparallevar.minicards.types.Moveable;
import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector;
@ -25,6 +27,7 @@ import java.util.UUID;
public class ColorBox implements Part {
private static final String TOGGLE_INPUT_CONNECTOR_ID = "toggle_input_connector";
private static final int DEFAULT_SIDE_SIZE = 100;
private final String _id;
private final PartGrid _partGrid;
private boolean _activated;
@ -228,4 +231,16 @@ public class ColorBox implements Part {
public int getInputConnectorCenterY() {
return (getTop() + getBottom()) / 2;
}
public static PartInstantiator getInstantiator() {
final int halfSideSize = DEFAULT_SIDE_SIZE / 2;
return new PartInstantiator() {
@Override
protected Part instantiate(PartGrid grid, Tuple2<Integer, Integer> center) {
return new ColorBox(grid,
center.item1 - halfSideSize, center.item2 - halfSideSize,
center.item1 + halfSideSize, center.item2 + halfSideSize);
}
};
}
}