From 713264a5bc36b0686f8c7827b9fed3beb24b1c19 Mon Sep 17 00:00:00 2001
From: aarnaude <antoine.arnaudet@polytech-lille.net>
Date: Wed, 10 Jan 2018 13:39:01 +0100
Subject: [PATCH] ajout drag n drop/bug drop mauvaise zone + manque precision drop

---
 .idea/misc.xml                                          |  2 +-
 app/src/main/java/tonio/noa/TutorialScene1Activity.java |  1 -
 app/src/main/java/tonio/noa/TutorialScene2Activity.java | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 app/src/main/res/drawable/blue_rectangle.xml            |  6 ++++++
 app/src/main/res/drawable/red_rectangle.xml             |  6 ++++++
 app/src/main/res/layout/tutorial2_display.xml           | 55 ++++++++++++++++++++++++++++++++++++++++++++-----------
 6 files changed, 140 insertions(+), 14 deletions(-)
 create mode 100644 app/src/main/res/drawable/blue_rectangle.xml
 create mode 100644 app/src/main/res/drawable/red_rectangle.xml

diff --git a/.idea/misc.xml b/.idea/misc.xml
index 75dac50..3963879 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -24,7 +24,7 @@
       </value>
     </option>
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">
diff --git a/app/src/main/java/tonio/noa/TutorialScene1Activity.java b/app/src/main/java/tonio/noa/TutorialScene1Activity.java
index 882bfe0..68668a3 100644
--- a/app/src/main/java/tonio/noa/TutorialScene1Activity.java
+++ b/app/src/main/java/tonio/noa/TutorialScene1Activity.java
@@ -2,7 +2,6 @@ package tonio.noa;
 
 import android.content.Intent;
 import android.os.Bundle;
-import android.view.View;
 
 /**
  * Created by tonio on 22/11/17.
diff --git a/app/src/main/java/tonio/noa/TutorialScene2Activity.java b/app/src/main/java/tonio/noa/TutorialScene2Activity.java
index cf672d7..758ef19 100644
--- a/app/src/main/java/tonio/noa/TutorialScene2Activity.java
+++ b/app/src/main/java/tonio/noa/TutorialScene2Activity.java
@@ -1,23 +1,105 @@
 package tonio.noa;
 
+import android.content.ClipData;
+import android.os.Build;
 import android.os.Bundle;
+import android.view.DragEvent;
+import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewGroup;
+import android.widget.LinearLayout;
+import android.widget.RelativeLayout;
+import android.widget.Toast;
 
 /**
  * Created by psyk on 09/01/18.
  */
 
-public class TutorialScene2Activity extends MyPlayActivity {
+public class TutorialScene2Activity extends MyPlayActivity implements View.OnTouchListener,View.OnDragListener{
+
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
 
         super.onCreate(savedInstanceState);
         setContentView(R.layout.tutorial2_display);
+
+        findViewById(R.id.blue_rectangle).setOnTouchListener(this);
+
+        findViewById(R.id.ts2l1).setOnDragListener(this);
+        findViewById(R.id.ts2r1).setOnDragListener(this);
+    }
+
+    @Override
+    public boolean onTouch(View view, MotionEvent event) {
+        if (event.getAction() == MotionEvent.ACTION_DOWN) {
+            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
+
+            ClipData data = ClipData.newPlainText("id", view.getResources().getResourceEntryName(view.getId()));
+
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+                view.startDragAndDrop(data, shadowBuilder, view, 0);
+            } else {
+                view.startDrag(data, shadowBuilder, view, 0);
+            }
+
+            view.setVisibility(View.INVISIBLE);
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public boolean onDrag(View v, DragEvent event) {
+        switch (event.getAction()) {
+            // signal for the start of a drag and drop operation
+            case DragEvent.ACTION_DRAG_STARTED:
+                // do nothing
+                break;
+
+            // the drag point has entered the bounding box of the View
+            case DragEvent.ACTION_DRAG_ENTERED:
+                // do nothing
+                break;
+
+            // the user has moved the drag shadow outside the bounding box of the View
+            case DragEvent.ACTION_DRAG_EXITED:
+                // do nothing
+                break;
+
+            // the drag and drop operation has concluded
+            case DragEvent.ACTION_DRAG_ENDED:
+                this.next();
+                break;
+
+            //drag shadow has been released,the drag point is within the bounding box of the View
+            case DragEvent.ACTION_DROP:
+                View view = (View) event.getLocalState();
+                // we want to make sure it is dropped only to left and right parent view
+                if (v.getId() == R.id.ts2l1 || v.getId() == R.id.ts2r1) {
+
+                    ViewGroup source = (ViewGroup) view.getParent();
+                    source.removeView(view);
+
+                    RelativeLayout target = (RelativeLayout) v;
+                    target.addView(view);
+
+                    String id = event.getClipData().getItemAt(0).getText().toString();
+                    Toast.makeText(this, id + " dropped!", Toast.LENGTH_SHORT).show();
+
+                    bravoPage(view);
+                }
+                // make view visible as we set visibility to invisible while starting drag
+                view.setVisibility(View.VISIBLE);
+                break;
+        }
+
+        return true;
     }
 
     @Override
     protected void next() {
         finish();
     }
+
 }
diff --git a/app/src/main/res/drawable/blue_rectangle.xml b/app/src/main/res/drawable/blue_rectangle.xml
new file mode 100644
index 0000000..8f3bb3f
--- /dev/null
+++ b/app/src/main/res/drawable/blue_rectangle.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle">
+    <solid android:color="@android:color/holo_blue_light"/>
+    <corners android:radius="5dp"/>
+</shape>
\ No newline at end of file
diff --git a/app/src/main/res/drawable/red_rectangle.xml b/app/src/main/res/drawable/red_rectangle.xml
new file mode 100644
index 0000000..6f8e3bc
--- /dev/null
+++ b/app/src/main/res/drawable/red_rectangle.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle">
+    <solid android:color="@android:color/holo_red_light"/>
+    <corners android:radius="5dp"/>
+</shape>
\ No newline at end of file
diff --git a/app/src/main/res/layout/tutorial2_display.xml b/app/src/main/res/layout/tutorial2_display.xml
index 7046d3b..4baa0a5 100644
--- a/app/src/main/res/layout/tutorial2_display.xml
+++ b/app/src/main/res/layout/tutorial2_display.xml
@@ -4,20 +4,53 @@
     android:layout_height="match_parent"
     android:orientation="vertical">
 
+<LinearLayout
+    android:id="@+id/ts2l1"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:gravity="center"
+    android:orientation="vertical">
+
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_alignParentTop="true"
-        android:layout_centerHorizontal="true"
-        android:text="Tutoriel scène 2" />
+        android:layout_margin="5dp"
+        android:textSize="15sp"
+        android:text="Fais glisser le rectangle bleu sur le rectangle rouge." />
 
-    <Button
-        android:id="@+id/button_id_home"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_alignParentBottom="true"
-        android:layout_alignParentLeft="true"
-        android:onClick="backHome"
-        android:text="@string/home" />
+    <ImageView
+        android:id="@+id/blue_rectangle"
+        android:layout_width="50dp"
+        android:layout_height="50dp"
+        android:layout_margin="50dp"
+        android:background="@drawable/blue_rectangle"/>
+
+</LinearLayout>
+
+    <RelativeLayout
+        android:id="@+id/ts2r1"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:layout_below="@id/ts2l1"
+        android:orientation="vertical">
+
+        <ImageView
+            android:id="@+id/red_rectangle"
+            android:layout_width="50dp"
+            android:layout_height="50dp"
+            android:layout_centerInParent="true"
+            android:background="@drawable/red_rectangle" />
+
+        <Button
+            android:id="@+id/button_id_home"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentBottom="true"
+            android:layout_alignParentLeft="true"
+            android:layout_margin="5dp"
+            android:onClick="backHome"
+            android:text="@string/home" />
+
+    </RelativeLayout>
 
 </RelativeLayout>
\ No newline at end of file
--
libgit2 0.21.2