add test example for drag&drop 89/190289/15
authorjunqing.ma <junqing.ma@samsung.com>
Sun, 30 Sep 2018 02:28:38 +0000 (10:28 +0800)
committerjunqing.ma <junqing.ma@samsung.com>
Fri, 7 Dec 2018 07:54:58 +0000 (15:54 +0800)
Change-Id: I4e04066ee86c48087502b06f147c132fba663c17

com.samsung.dali-demo.xml
examples-reel/dali-examples-reel.cpp
examples/drag-and-drop/drag-and-drop-example.cpp [new file with mode: 0755]
resources/po/en_GB.po
resources/po/en_US.po
resources/po/zn_CH.po
shared/dali-demo-strings.h

index 1487faf..9b759cf 100644 (file)
        <ui-application appid="web-view.example" exec="/usr/apps/com.samsung.dali-demo/bin/web-view.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
                <label>Web View</label>
        </ui-application>
+       <ui-application appid="drag-and-drop.example" exec="/usr/apps/com.samsung.dali-demo/bin/drag-and-drop.example" nodisplay="true" multiple="false" type="c++app" taskmanage="true">
+               <label>Drag And Drop</label>
+       </ui-application>
 
        <privileges>
                <privilege>http://tizen.org/privilege/mediastorage</privilege>
index 5bd2874..24d07a1 100644 (file)
@@ -44,6 +44,7 @@ int DALI_EXPORT_API main(int argc, char **argv)
   demo.AddExample(Example("clipping.example", DALI_DEMO_STR_TITLE_CLIPPING));
   demo.AddExample(Example("clipping-draw-order.example", DALI_DEMO_STR_TITLE_CLIPPING_DRAW_ORDER));
   demo.AddExample(Example("dissolve-effect.example", DALI_DEMO_STR_TITLE_DISSOLVE_TRANSITION));
+  demo.AddExample(Example("drag-and-drop.example", DALI_DEMO_STR_TITLE_DRAG_AND_DROP));
   demo.AddExample(Example("effects-view.example", DALI_DEMO_STR_TITLE_EFFECTS_VIEW));
   demo.AddExample(Example("flex-container.example", DALI_DEMO_STR_TITLE_FLEXBOX_PLAYGROUND));
   demo.AddExample(Example("frame-callback.example", DALI_DEMO_STR_TITLE_FRAME_CALLBACK));
diff --git a/examples/drag-and-drop/drag-and-drop-example.cpp b/examples/drag-and-drop/drag-and-drop-example.cpp
new file mode 100755 (executable)
index 0000000..ef11be7
--- /dev/null
@@ -0,0 +1,289 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <dali-toolkit/dali-toolkit.h>
+#include <dali/integration-api/debug.h>
+#include <dali/devel-api/actors/actor-devel.h>
+#include <dali-toolkit/devel-api/drag-drop-detector/drag-and-drop-detector.h>
+
+using namespace Dali;
+using Dali::Toolkit::TextLabel;
+using namespace Dali::Toolkit;
+
+namespace
+{
+Vector4 TEXT_LABEL_COLOR[] =
+{
+  Color::MAGENTA,
+  Color::YELLOW,
+  Color::CYAN,
+  Color::BLUE,
+  Color::MAGENTA,
+  Color::YELLOW,
+  Color::CYAN,
+  Color::BLUE
+};
+
+const float TEXT_LABEL_POSITION_X = 100.0f;
+const float TEXT_LABEL_POSITION_START_Y = 50.0f;
+const float TEXT_LABEL_WIDTH = 250.0f;
+const float TEXT_LABEL_HEIGHT = 70.0f;
+const unsigned int TEXT_LABEL_NUM = sizeof(TEXT_LABEL_COLOR) / sizeof(TEXT_LABEL_COLOR[0]);
+
+#if defined(DEBUG_ENABLED)
+  Debug::Filter* gDragAndDropFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DRAG_AND_DROP_EXAMPLE");
+#endif
+}
+
+//This example shows how to use drag and drop function by several simple TextActors
+class DragAndDropExample : public ConnectionTracker
+{
+public:
+
+  DragAndDropExample( Application& application )
+  : mApplication( application ),
+    mDragIndex(-1),
+    mDragRealIndex(-1),
+    mDroppedFinished(false)
+  {
+    // Connect to the Application's Init signal
+    mApplication.InitSignal().Connect( this, &DragAndDropExample::Create );
+  }
+
+  // The Init signal is received once (only) during the Application lifetime
+  void Create( Application& application )
+  {
+    Stage stage = Stage::GetCurrent();
+    stage.SetBackgroundColor( Color::WHITE );
+
+    mDragAndDropDetector = Dali::Toolkit::DragAndDropDetector::New();
+
+    // Respond to key events
+    stage.KeyEventSignal().Connect( this, &DragAndDropExample::OnKeyEvent );
+
+    TextLabel hintText = TextLabel::New("please drag one textlabel, move and drop on other textlabel");
+    hintText.SetPosition(0.0f, 700.0f);
+    hintText.SetParentOrigin(ParentOrigin::TOP_LEFT);
+    hintText.SetAnchorPoint(AnchorPoint::TOP_LEFT);
+    hintText.SetProperty(TextLabel::Property::MULTI_LINE, true);
+    stage.Add(hintText);
+
+    for(unsigned int i = 0 ; i < TEXT_LABEL_NUM; i++)
+    {
+      std::string str = "textlabel ";
+      mTextLabel[i] = TextLabel::New(str + std::to_string(i));
+      mTextLabel[i].SetParentOrigin(ParentOrigin::TOP_LEFT);
+      mTextLabel[i].SetAnchorPoint(AnchorPoint::TOP_LEFT);
+      mTextLabel[i].SetName("textlabel " + std::to_string(i));
+      mTextLabel[i].SetLeaveRequired(true);
+      mTextLabel[i].SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
+      mTextLabel[i].SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
+      mTextLabel[i].SetBackgroundColor(TEXT_LABEL_COLOR[i]);
+
+      mTextLabel[i].SetSize(TEXT_LABEL_WIDTH, TEXT_LABEL_HEIGHT);
+      mTextLabel[i].SetPosition(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * i);
+      mDragAndDropDetector.Attach(mTextLabel[i]);
+
+      mRect[i] = Rect<float>(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * i, TEXT_LABEL_WIDTH, TEXT_LABEL_HEIGHT);
+      mOrder[i] = i;
+
+      stage.Add(mTextLabel[i]);
+    }
+
+    mDragAndDropDetector.StartedSignal().Connect(this, &DragAndDropExample::OnStart);
+    mDragAndDropDetector.EnteredSignal().Connect(this, &DragAndDropExample::OnEnter);
+    mDragAndDropDetector.ExitedSignal().Connect(this, &DragAndDropExample::OnExit);
+    mDragAndDropDetector.MovedSignal().Connect(this, &DragAndDropExample::OnMoved);
+    mDragAndDropDetector.DroppedSignal().Connect(this, &DragAndDropExample::OnDropped);
+    mDragAndDropDetector.EndedSignal().Connect(this, &DragAndDropExample::OnEnd);
+  }
+
+  void OnKeyEvent( const KeyEvent& event )
+  {
+    if( event.state == KeyEvent::Down )
+    {
+      if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
+      {
+        mApplication.Quit();
+      }
+    }
+  }
+
+  void OnStart(Control control, Dali::Toolkit::DragAndDropDetector detector)
+  {
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---OnStart---\n");
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetName().c_str());
+
+    control.SetOpacity(0.1f);
+    Vector2 screenPos  = detector.GetCurrentScreenPosition();
+    control.ScreenToLocal(mDragLocalPos.x, mDragLocalPos.y,screenPos.x, screenPos.y );
+    Rect<float> targetRect(screenPos.x, screenPos.y, 0.0f, 0.0f);
+
+    for(unsigned int i = 0; i < TEXT_LABEL_NUM; i++)
+    {
+      if(mRect[i].Contains(targetRect))
+      {
+        mDragIndex = i;
+      }
+    }
+
+    mDragRealIndex = mOrder[mDragIndex];
+  }
+
+  void OnEnter(Control control, Dali::Toolkit::DragAndDropDetector detector)
+  {
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---OnEnter---\n");
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetName().c_str());
+  }
+
+  void OnExit(Control control, Dali::Toolkit::DragAndDropDetector detector)
+  {
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---OnExit---\n");
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetName().c_str());
+  }
+
+  void OnMoved(Control control, Dali::Toolkit::DragAndDropDetector detector)
+  {
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::Verbose, "---OnMoved---\n");
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::Verbose, "---control name is %s---\n", control.GetName().c_str());
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::Verbose, "---coordinate is (%f, %f)---\n", detector.GetCurrentScreenPosition().x, detector.GetCurrentScreenPosition().y);
+  }
+
+  void OnDropped(Control control, Dali::Toolkit::DragAndDropDetector detector)
+  {
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---OnDropped---\n");
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetName().c_str());
+
+    Vector2 screenPos  = detector.GetCurrentScreenPosition();
+    Rect<float> targetRect(screenPos.x, screenPos.y, 0.0f, 0.0f);
+    int droppedIndex = -1;
+    for(unsigned int i = 0; i < TEXT_LABEL_NUM; i++)
+    {
+      if(mRect[i].Contains(targetRect))
+      {
+        droppedIndex = i;
+      }
+    }
+
+    Animation mAnimation = Animation::New(0.5f);
+
+    if(droppedIndex > mDragIndex)
+    {
+      for(int i = mDragIndex + 1; i <= droppedIndex; i++)
+      {
+        float y = mTextLabel[mOrder[i]].GetCurrentPosition().y;
+        mAnimation.AnimateTo(Property(mTextLabel[mOrder[i]], Actor::Property::POSITION), Vector3(TEXT_LABEL_POSITION_X, y - TEXT_LABEL_HEIGHT, 0.0f), AlphaFunction::EASE_OUT);
+        mAnimation.Play();
+      }
+
+      int tmpId = mOrder[mDragIndex];
+      for(int i = mDragIndex; i < droppedIndex; i++)
+      {
+        mOrder[i] = mOrder[i+1];
+      }
+
+      mOrder[droppedIndex] = tmpId;
+    }
+    else if(droppedIndex < mDragIndex)
+    {
+
+      for(int i = mDragIndex - 1; i >= droppedIndex; i--)
+      {
+        float y = mTextLabel[mOrder[i]].GetCurrentPosition().y;
+        mAnimation.AnimateTo(Property(mTextLabel[mOrder[i]], Actor::Property::POSITION), Vector3(TEXT_LABEL_POSITION_X, y + TEXT_LABEL_HEIGHT, 0.0f), AlphaFunction::EASE_OUT);
+        mAnimation.Play();
+      }
+
+      int tmpId = mOrder[mDragIndex];
+      for(int i = mDragIndex; i > droppedIndex; i--)
+      {
+        mOrder[i] = mOrder[i-1];
+      }
+
+      mOrder[droppedIndex] = tmpId;
+
+    }
+
+
+    Vector2 pos = detector.GetCurrentScreenPosition();
+    Vector2 localPos;
+    control.GetParent().ScreenToLocal(localPos.x, localPos.y, pos.x, pos.y);
+
+    KeyFrames k0 = KeyFrames::New();
+    k0.Add(0.0f, Vector3(localPos.x - mDragLocalPos.x, localPos.y - mDragLocalPos.y, 0.0f));
+    k0.Add(1.0f, Vector3(control.GetCurrentPosition().x, control.GetCurrentPosition().y, 0.0f));
+
+    KeyFrames k1 = KeyFrames::New();
+    k1.Add(0.0f, 0.1f);
+    k1.Add(1.0f, 1.0f);
+
+    Animation dropAnimation = Animation::New(0.5f);
+    dropAnimation.FinishedSignal().Connect(this, &DragAndDropExample::DropAnimationFinished);
+    dropAnimation.AnimateBetween(Property(mTextLabel[mDragRealIndex], Actor::Property::POSITION), k0, AlphaFunction::EASE_OUT);
+    dropAnimation.AnimateBetween(Property(mTextLabel[mDragRealIndex], DevelActor::Property::OPACITY), k1, AlphaFunction::EASE_OUT);
+    dropAnimation.Play();
+
+    mDroppedFinished = true;
+  }
+
+  void DropAnimationFinished(Animation& animation)
+  {
+    for(unsigned int i = 0 ; i < TEXT_LABEL_NUM; i++)
+    {
+      mDragAndDropDetector.Attach(mTextLabel[i]);
+    }
+  }
+
+  void OnEnd(Control control, Dali::Toolkit::DragAndDropDetector detector)
+  {
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---OnEnd---\n");
+    DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetName().c_str());
+
+    control.SetOpacity(1.0f);
+
+    if(mDroppedFinished)
+    {
+      mDragAndDropDetector.DetachAll();
+    }
+
+    mDroppedFinished = false;
+  }
+
+private:
+  Application&  mApplication;
+  Dali::Toolkit::DragAndDropDetector mDragAndDropDetector;
+
+  TextLabel mTextLabel[TEXT_LABEL_NUM];
+  Rect<float> mRect[TEXT_LABEL_NUM];
+
+  int mOrder[TEXT_LABEL_NUM];
+  int mDragIndex;
+  int mDragRealIndex;
+
+  Vector2 mDragLocalPos;
+
+  bool mDroppedFinished;
+
+};
+
+int DALI_EXPORT_API main( int argc, char **argv )
+{
+  Application application = Application::New( &argc, &argv );
+  DragAndDropExample test( application );
+  application.MainLoop();
+  return 0;
+}
index 342d10d..6fa2a7d 100755 (executable)
@@ -52,6 +52,9 @@ msgstr "Cube Effect"
 msgid "DALI_DEMO_STR_TITLE_DISSOLVE_TRANSITION"
 msgstr "Dissolve Effect"
 
+msgid "DALI_DEMO_STR_TITLE_DRAG_AND_DROP"
+msgstr "Drag and Drop"
+
 msgid "DALI_DEMO_STR_TITLE_EFFECTS_VIEW"
 msgstr "Effects View"
 
index 50460ee..550a508 100755 (executable)
@@ -52,6 +52,9 @@ msgstr "Cube Effect"
 msgid "DALI_DEMO_STR_TITLE_DISSOLVE_TRANSITION"
 msgstr "Dissolve Effect"
 
+msgid "DALI_DEMO_STR_TITLE_DRAG_AND_DROP"
+msgstr "Drag and Drop"
+
 msgid "DALI_DEMO_STR_TITLE_EFFECTS_VIEW"
 msgstr "Effects View"
 
index 5897486..68dfa53 100755 (executable)
@@ -31,6 +31,9 @@ msgstr "方块切换效果"
 msgid "DALI_DEMO_STR_TITLE_DISSOLVE_TRANSITION"
 msgstr "冰消瓦解切换效果"
 
+msgid "DALI_DEMO_STR_TITLE_DRAG_AND_DROP"
+msgstr "拖放"
+
 msgid "DALI_DEMO_STR_TITLE_EFFECTS_VIEW"
 msgstr "效果视图"
 
index e0fcb42..46b49bb 100644 (file)
@@ -50,6 +50,7 @@ extern "C"
 #define DALI_DEMO_STR_TITLE_CONTACT_CARDS               dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_CONTACT_CARDS")
 #define DALI_DEMO_STR_TITLE_CUBE_TRANSITION             dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_CUBE_TRANSITION")
 #define DALI_DEMO_STR_TITLE_DISSOLVE_TRANSITION         dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_DISSOLVE_TRANSITION")
+#define DALI_DEMO_STR_TITLE_DRAG_AND_DROP               dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_DRAG_AND_DROP")
 #define DALI_DEMO_STR_TITLE_EFFECTS_VIEW                dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_EFFECTS_VIEW")
 #define DALI_DEMO_STR_TITLE_EMOJI_TEXT                  dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_EMOJI_TEXT")
 #define DALI_DEMO_STR_TITLE_FPP_GAME                    dgettext(DALI_DEMO_DOMAIN_LOCAL, "DALI_DEMO_STR_TITLE_FPP_GAME")
@@ -138,6 +139,7 @@ extern "C"
 #define DALI_DEMO_STR_TITLE_CONTACT_CARDS               "Contact Cards"
 #define DALI_DEMO_STR_TITLE_CUBE_TRANSITION             "Cube Effect"
 #define DALI_DEMO_STR_TITLE_DISSOLVE_TRANSITION         "Dissolve Effect"
+#define DALI_DEMO_STR_TITLE_DRAG_AND_DROP               "Drag and Drop"
 #define DALI_DEMO_STR_TITLE_EFFECTS_VIEW                "Effects View"
 #define DALI_DEMO_STR_TITLE_EMOJI_TEXT                  "Emoji Text"
 #define DALI_DEMO_STR_TITLE_FPP_GAME                    "First Person Game"