X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=examples%2Fdrag-and-drop%2Fdrag-and-drop-example.cpp;h=85e2d0bee5fd0c689a95406df94c810c2d3f9a51;hb=1b19fd140ff139b5854a1a62447faf31b175d8f6;hp=22e733f762eedc894f992c19d1896a2da6001df5;hpb=e3080a83ec85e14ec8cbf82831fd73b152c6e86a;p=platform%2Fcore%2Fuifw%2Fdali-demo.git diff --git a/examples/drag-and-drop/drag-and-drop-example.cpp b/examples/drag-and-drop/drag-and-drop-example.cpp index 22e733f..85e2d0b 100644 --- a/examples/drag-and-drop/drag-and-drop-example.cpp +++ b/examples/drag-and-drop/drag-and-drop-example.cpp @@ -16,9 +16,9 @@ */ #include -#include -#include #include +#include +#include using namespace Dali; using Dali::Toolkit::TextLabel; @@ -27,40 +27,40 @@ 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]); + { + 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]); + +const float DROP_ANIMATION_DURATION_S = 0.5f; #if defined(DEBUG_ENABLED) - Debug::Filter* gDragAndDropFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DRAG_AND_DROP_EXAMPLE"); +Debug::Filter* gDragAndDropFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DRAG_AND_DROP_EXAMPLE"); #endif -} +} // namespace //This example shows how to use drag and drop function by several simple TextActors class DragAndDropExample : public ConnectionTracker { public: - - DragAndDropExample( Application& application ) - : mApplication( application ), + DragAndDropExample(Application& application) + : mApplication(application), mDragIndex(-1), mDragRealIndex(-1) { // Connect to the Application's Init signal - mApplication.InitSignal().Connect( this, &DragAndDropExample::Create ); + mApplication.InitSignal().Connect(this, &DragAndDropExample::Create); } ~DragAndDropExample() @@ -69,40 +69,40 @@ public: } // The Init signal is received once (only) during the Application lifetime - void Create( Application& application ) + void Create(Application& application) { auto window = application.GetWindow(); - window.SetBackgroundColor( Color::WHITE ); + window.SetBackgroundColor(Color::WHITE); mDragAndDropDetector = Dali::Toolkit::DragAndDropDetector::New(); // Respond to key events - window.KeyEventSignal().Connect( this, &DragAndDropExample::OnKeyEvent ); + window.KeyEventSignal().Connect(this, &DragAndDropExample::OnKeyEvent); TextLabel hintText = TextLabel::New("please drag one textlabel, move and drop on other textlabel"); - hintText.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 700.0f)); - hintText.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT); - hintText.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT); + hintText.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 700.0f)); + hintText.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT); + hintText.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT); hintText.SetProperty(TextLabel::Property::MULTI_LINE, true); window.Add(hintText); - for(unsigned int i = 0 ; i < TEXT_LABEL_NUM; i++) + 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].SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT); - mTextLabel[i].SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT); - mTextLabel[i].SetProperty( Dali::Actor::Property::NAME,"textlabel " + std::to_string(i)); - mTextLabel[i].SetProperty( Actor::Property::LEAVE_REQUIRED,true); + mTextLabel[i] = TextLabel::New(str + std::to_string(i)); + mTextLabel[i].SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT); + mTextLabel[i].SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT); + mTextLabel[i].SetProperty(Dali::Actor::Property::NAME, "textlabel " + std::to_string(i)); + mTextLabel[i].SetProperty(Actor::Property::LEAVE_REQUIRED, 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].SetProperty( Actor::Property::SIZE, Vector2(TEXT_LABEL_WIDTH, TEXT_LABEL_HEIGHT) ); - mTextLabel[i].SetProperty( Actor::Property::POSITION, Vector2(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * i)); + mTextLabel[i].SetProperty(Actor::Property::SIZE, Vector2(TEXT_LABEL_WIDTH, TEXT_LABEL_HEIGHT)); + mTextLabel[i].SetProperty(Actor::Property::POSITION, Vector2(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * i)); mDragAndDropDetector.Attach(mTextLabel[i]); - mRect[i] = Rect(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * i, TEXT_LABEL_WIDTH, TEXT_LABEL_HEIGHT); + mRect[i] = Rect(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * i, TEXT_LABEL_WIDTH, TEXT_LABEL_HEIGHT); mOrder[i] = i; window.Add(mTextLabel[i]); @@ -116,11 +116,11 @@ public: mDragAndDropDetector.EndedSignal().Connect(this, &DragAndDropExample::OnEnd); } - void OnKeyEvent( const KeyEvent& event ) + void OnKeyEvent(const KeyEvent& event) { - if( event.GetState() == KeyEvent::DOWN ) + if(event.GetState() == KeyEvent::DOWN) { - if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) ) + if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK)) { mApplication.Quit(); } @@ -130,11 +130,11 @@ public: 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.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str()); + DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetProperty(Dali::Actor::Property::NAME).c_str()); - control.SetProperty( Actor::Property::OPACITY,0.1f); - Vector2 screenPos = detector.GetCurrentScreenPosition(); - control.ScreenToLocal(mDragLocalPos.x, mDragLocalPos.y,screenPos.x, screenPos.y ); + control.SetProperty(Actor::Property::OPACITY, 0.1f); + Vector2 screenPos = detector.GetCurrentScreenPosition(); + control.ScreenToLocal(mDragLocalPos.x, mDragLocalPos.y, screenPos.x, screenPos.y); Rect targetRect(screenPos.x, screenPos.y, 0.0f, 0.0f); for(unsigned int i = 0; i < TEXT_LABEL_NUM; i++) @@ -151,30 +151,30 @@ public: 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.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str()); + DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetProperty(Dali::Actor::Property::NAME).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.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str()); + DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetProperty(Dali::Actor::Property::NAME).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.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str()); + DALI_LOG_INFO(gDragAndDropFilter, Debug::Verbose, "---control name is %s---\n", control.GetProperty(Dali::Actor::Property::NAME).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.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str()); + DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetProperty(Dali::Actor::Property::NAME).c_str()); - Vector2 screenPos = detector.GetCurrentScreenPosition(); + Vector2 screenPos = detector.GetCurrentScreenPosition(); Rect targetRect(screenPos.x, screenPos.y, 0.0f, 0.0f); - int droppedIndex = -1; + int droppedIndex = -1; for(unsigned int i = 0; i < TEXT_LABEL_NUM; i++) { if(mRect[i].Contains(targetRect)) @@ -183,59 +183,58 @@ public: } } - Animation mAnimation = Animation::New(0.5f); + Animation mAnimation = Animation::New(DROP_ANIMATION_DURATION_S); if(droppedIndex > mDragIndex) { for(int i = mDragIndex + 1; i <= droppedIndex; i++) { - float y = mTextLabel[mOrder[i]].GetCurrentProperty< Vector3 >( Actor::Property::POSITION ).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.AnimateTo(Property(mTextLabel[mOrder[i]], Actor::Property::POSITION), + Vector3(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * (i - 1), 0.0f), + AlphaFunction::EASE_OUT); mAnimation.Play(); } int tmpId = mOrder[mDragIndex]; for(int i = mDragIndex; i < droppedIndex; i++) { - mOrder[i] = mOrder[i+1]; + 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]].GetCurrentProperty< Vector3 >( Actor::Property::POSITION ).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.AnimateTo(Property(mTextLabel[mOrder[i]], Actor::Property::POSITION), + Vector3(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * (i + 1), 0.0f), + AlphaFunction::EASE_OUT); mAnimation.Play(); } int tmpId = mOrder[mDragIndex]; for(int i = mDragIndex; i > droppedIndex; i--) { - mOrder[i] = mOrder[i-1]; + 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.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ).x, control.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ).y, 0.0f)); + k0.Add(1.0f, Vector3(TEXT_LABEL_POSITION_X, TEXT_LABEL_POSITION_START_Y + TEXT_LABEL_HEIGHT * droppedIndex, 0.0f)); KeyFrames k1 = KeyFrames::New(); k1.Add(0.0f, 0.1f); k1.Add(1.0f, 1.0f); - Animation dropAnimation = Animation::New(0.5f); + Animation dropAnimation = Animation::New(DROP_ANIMATION_DURATION_S); dropAnimation.AnimateBetween(Property(mTextLabel[mDragRealIndex], Actor::Property::POSITION), k0, AlphaFunction::EASE_OUT); dropAnimation.AnimateBetween(Property(mTextLabel[mDragRealIndex], Actor::Property::OPACITY), k1, AlphaFunction::EASE_OUT); dropAnimation.Play(); @@ -243,7 +242,7 @@ public: void DropAnimationFinished(Animation& animation) { - for(unsigned int i = 0 ; i < TEXT_LABEL_NUM; i++) + for(unsigned int i = 0; i < TEXT_LABEL_NUM; i++) { mDragAndDropDetector.Attach(mTextLabel[i]); } @@ -252,16 +251,16 @@ public: 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.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str()); + DALI_LOG_INFO(gDragAndDropFilter, Debug::General, "---control name is %s---\n", control.GetProperty(Dali::Actor::Property::NAME).c_str()); - control.SetProperty( Actor::Property::OPACITY,1.0f); + control.SetProperty(Actor::Property::OPACITY, 1.0f); } private: - Application& mApplication; + Application& mApplication; Dali::Toolkit::DragAndDropDetector mDragAndDropDetector; - TextLabel mTextLabel[TEXT_LABEL_NUM]; + TextLabel mTextLabel[TEXT_LABEL_NUM]; Rect mRect[TEXT_LABEL_NUM]; int mOrder[TEXT_LABEL_NUM]; @@ -269,13 +268,12 @@ private: int mDragRealIndex; Vector2 mDragLocalPos; - }; -int DALI_EXPORT_API main( int argc, char **argv ) +int DALI_EXPORT_API main(int argc, char** argv) { - Application application = Application::New( &argc, &argv ); - DragAndDropExample test( application ); + Application application = Application::New(&argc, &argv); + DragAndDropExample test(application); application.MainLoop(); return 0; }