diff --git a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java index 0b2510e..f986694 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java +++ b/app/src/main/java/com/codigoparallevar/minicards/CanvasView.java @@ -1,5 +1,6 @@ package com.codigoparallevar.minicards; +import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; @@ -16,6 +17,7 @@ import com.codigoparallevar.minicards.parts.Part; import com.codigoparallevar.minicards.parts.Placeholder; import com.codigoparallevar.minicards.parts.buttons.RoundButton; import com.codigoparallevar.minicards.parts.types.Position; +import com.getbase.floatingactionbutton.FloatingActionsMenu; import org.json.JSONArray; import org.json.JSONException; @@ -47,6 +49,9 @@ class CanvasView extends View { @NonNull private String name = "default"; private final static float touchTimeForLongTouchInMillis = 500; + private boolean _isDragging = false; + private MainActivity parentActivity = null; + private Tuple _dropZone = new Tuple<>(0, 0, 0, 0); public CanvasView(Context context) { super(context); @@ -173,7 +178,16 @@ class CanvasView extends View { selectedPart.touched(); } } + else if (motionMode == MotionMode.Type.LongTouch) { + if (selectedPart != null) { + if (inDropZone(x, y)) { + Log.d("Canvas", "Deleting element" + selectedPart); + parts.remove(selectedPart); + } + } + } + _isDragging = false; motionMode = null; selectedPart = null; } @@ -186,6 +200,8 @@ class CanvasView extends View { case MotionEvent.ACTION_MOVE: { + Log.i("Canvas", "X: " + x + " Y: " + y + + " in drop zone " + _dropZone + " : " + inDropZone(x, y)); if (motionMode == null){ final Part nowSelectedPart = getPartOn(x, y); final boolean canWait = selectedPart == nowSelectedPart; @@ -193,6 +209,7 @@ class CanvasView extends View { } if (motionMode == MotionMode.Type.LongTouch){ if (selectedPart != null){ + _isDragging = true; selectedPart.move(x, y); } } @@ -206,9 +223,17 @@ class CanvasView extends View { } invalidate(); + if (parentActivity != null) { + parentActivity.invalidate(); + } return true; } + private boolean inDropZone(int x, int y) { + return (x >= _dropZone._x1) && (x <= _dropZone._x2) + && (y >= _dropZone._y1) && (y <= _dropZone._y2); + } + private void saveState() throws IOException { File filesDir = getContext().getFilesDir(); FileOutputStream fileOut = new FileOutputStream(filesDir + "/" + name); @@ -270,4 +295,36 @@ class CanvasView extends View { parts.add(part); invalidate(); } + + public boolean isDragging() { + return _isDragging; + } + + public void setParentActivity(MainActivity parentActivity) { + this.parentActivity = parentActivity; + } + + public void setDropZone(float x1, float x2, float y1, float y2) { + _dropZone = new Tuple<>((int) x1, (int) x2, (int) y1, (int) y2); + } + + private class Tuple { + + final T _x1; + final T1 _x2; + final T2 _y1; + final T3 _y2; + + public Tuple(T x1, T1 x2, T2 y1, T3 y2) { + _x1 = x1; + _x2 = x2; + _y1 = y1; + _y2 = y2; + } + + @Override + public String toString(){ + return "(" + _x1 + " - " + _x2 + ", " + _y1 + " - " + _y2 + ")"; + } + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java index 5635fb9..7d7dae8 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java +++ b/app/src/main/java/com/codigoparallevar/minicards/MainActivity.java @@ -1,6 +1,5 @@ package com.codigoparallevar.minicards; -import android.graphics.Canvas; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.ActionBar; @@ -9,16 +8,16 @@ import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; -import android.widget.Button; import com.codigoparallevar.minicards.parts.buttons.RoundButton; -import com.getbase.floatingactionbutton.AddFloatingActionButton; public class MainActivity extends AppCompatActivity { CanvasView canvasView; com.getbase.floatingactionbutton.AddFloatingActionButton AddPartButton; com.getbase.floatingactionbutton.FloatingActionButton SetDevModeButton; + com.getbase.floatingactionbutton.FloatingActionsMenu fabMenu; + FloatingActionButton removePartFab; @Override protected void onCreate(Bundle savedInstanceState) { @@ -34,13 +33,25 @@ public class MainActivity extends AppCompatActivity { // Use manually controlled canvas canvasView = (CanvasView) findViewById(R.id.canvasView); + canvasView.setParentActivity(this); + + removePartFab = (FloatingActionButton) findViewById(R.id.remove_part_fab); + canvasView.setDropZone( + removePartFab.getX(), removePartFab.getX() + removePartFab.getWidth(), + removePartFab.getY(), removePartFab.getY() + removePartFab.getHeight()); // Set callbacks to fab buttons + fabMenu = (com.getbase.floatingactionbutton.FloatingActionsMenu) + findViewById(R.id.fab); + AddPartButton = (com.getbase.floatingactionbutton.AddFloatingActionButton) findViewById(R.id.add_part_button); AddPartButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { + if (canvasView != null) { + canvasView.addPart(new RoundButton(500, 500, 50, 100)); + } Snackbar.make(v, "Add part", Snackbar.LENGTH_SHORT).show(); } }); @@ -65,4 +76,22 @@ public class MainActivity extends AppCompatActivity { return super.onTouchEvent(event); } + + public void invalidate() { + if (canvasView != null){ + if (canvasView.isDragging()){ + fabMenu.setVisibility(View.GONE); + removePartFab.setVisibility(View.VISIBLE); + Log.d("Main", "Changing visibility!"); + } + else { + fabMenu.setVisibility(View.VISIBLE); + removePartFab.setVisibility(View.GONE); + Log.d("Main", "Now changing visibility!"); + } + canvasView.setDropZone( + removePartFab.getX(), removePartFab.getX() + removePartFab.getWidth(), + removePartFab.getY(), removePartFab.getY() + removePartFab.getHeight()); + } + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index efa9f15..64ebf47 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -29,6 +29,16 @@ android:layout_width="match_parent" android:layout_height="match_parent" /> + +