Add mock part addition/deletion mechanism.

This commit is contained in:
kenkeiras 2017-07-09 21:32:05 -04:00
parent cd6b7c87d6
commit 5e583bed05
3 changed files with 99 additions and 3 deletions

View File

@ -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<Integer, Integer, Integer, Integer> _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<T, T1, T2, T3> {
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 + ")";
}
}
}

View File

@ -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());
}
}
}

View File

@ -29,6 +29,16 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/remove_part_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:visibility="gone"
android:background="@color/white"
app:srcCompat="@android:drawable/ic_delete" />
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/fab"
android:layout_width="wrap_content"