diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 1119bca..e808942 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -478,4 +478,10 @@ class CanvasView extends View implements PartGrid { this._viewOrigin = new Tuple2<>(0, 0); this.invalidate(); } + + public Tuple2 getCenteredOn() { + int xOffset = _viewOrigin.item1 + getWidth() / 2; + int yOffset = _viewOrigin.item2 + getHeight() / 2; + return new Tuple2<>(xOffset, yOffset); + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java index 6018d58..390660a 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java @@ -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); } }); diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartInstantiator.java b/app/src/main/java/com/codigoparallevar/minicards/PartInstantiator.java new file mode 100644 index 0000000..87804c7 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/PartInstantiator.java @@ -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 center); + + public Part build(CanvasView canvasView) { + Tuple2 center = canvasView.getCenteredOn(); + + return instantiate(canvasView, center); + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java new file mode 100644 index 0000000..a772d42 --- /dev/null +++ b/app/src/main/java/com/codigoparallevar/minicards/PartsHolder.java @@ -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> BuiltInParts = + new Vector>(){{ + add(new Tuple2("Round button", RoundButton.getInstantiator())); + add(new Tuple2("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; + } +} diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java index f72a798..c27d6af 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java @@ -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 _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 center) { + return new RoundButton(grid, + center.item1, center.item2, + DEFAULT_INNER_RADIUS, DEFAULT_OUTER_RADIUS); + } + }; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java index 55819c4..3e08e40 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java @@ -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 center) { + return new ColorBox(grid, + center.item1 - halfSideSize, center.item2 - halfSideSize, + center.item1 + halfSideSize, center.item2 + halfSideSize); + } + }; + } }