Browse Source

Dispatching AnimationEnd event

Michael 7 years ago
parent
commit
2e765efb48

+ 5 - 2
Samples/basic/animation/data/animation.rml

@@ -82,8 +82,10 @@
 			margin: 0;
 			transform: translateX(100%);
 		}
-		#text_align {
-			text-align: left;
+		#animation_event {
+			position: relative;
+			margin: 0;
+			top: 50px; left: 50px;
 		}
 		/* -- TRANSITION TESTS */
 		#transition_test {
@@ -128,6 +130,7 @@
 	<div style="font-size: 1.5em; text-align: left;">Mixed units tests</div>
 	<div class="container"><div class="plain" id="abs_rel">Pixel vs percentage.</div></div>
 	<div class="container"><div class="plain" id="abs_rel_transform">Pixel vs percentage transform.</div></div>
+	<div class="container"><div class="plain" id="animation_event">Animation event</div></div>
 </div>
 <div style="width: 20%; height: 80%;position: absolute; left: 75%;" id="transition_tests">
 	<div style="font-size: 1.5em; text-align: left;">Transition tests</div>

+ 15 - 2
Samples/basic/animation/src/main.cpp

@@ -122,8 +122,9 @@ public:
 				el->Animate("transform", p, 1.5f, Tween{}, -1, true);
 			}
 			{
-				auto el = document->GetElementById("text_align");
-				//el->Animate("text-align", Property(3, Property::KEYWORD), 2.0f, Tween{}, -1, true);
+				auto el = document->GetElementById("animation_event");
+				el->Animate("top", Property(Math::RandomReal(250.f), Property::PX), 1.5f, Tween{ Tween::Cubic, Tween::InOut });
+				el->Animate("left", Property(Math::RandomReal(250.f), Property::PX), 1.5f, Tween{ Tween::Cubic, Tween::InOut });
 			}
 
 			document->Show();
@@ -209,6 +210,8 @@ public:
 
 	void ProcessEvent(Rocket::Core::Event& event) override
 	{
+		using namespace Rocket::Core;
+
 		if(value == "exit")
 			Shell::RequestExit();
 
@@ -251,6 +254,15 @@ public:
 				el->SetClass("move_me", !el->IsClassSet("move_me"));
 			}
 		}
+		if (event == "animationend")
+		{
+			auto el = event.GetTargetElement();
+			if (el->GetId() == "animation_event")
+			{
+				el->Animate("top", Property(Math::RandomReal(200.f), Property::PX), 1.2f, Tween{ Tween::Cubic, Tween::InOut });
+				el->Animate("left", Property(Math::RandomReal(100.f), Property::PERCENT), 0.8f, Tween{ Tween::Cubic, Tween::InOut });
+			}
+		}
 	}
 
 	void OnDetach(Rocket::Core::Element* element) override { delete this; }
@@ -357,6 +369,7 @@ int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv)
 	window = new DemoWindow("Animation sample", Rocket::Core::Vector2f(81, 100), context);
 	window->GetDocument()->AddEventListener("keydown", new Event("hello"));
 	window->GetDocument()->AddEventListener("keyup", new Event("hello"));
+	window->GetDocument()->AddEventListener("animationend", new Event("hello"));
 
 
 	Shell::EventLoop(GameLoop);

+ 17 - 4
Source/Core/Element.cpp

@@ -189,10 +189,23 @@ void Element::UpdateAnimations()
 				SetProperty(animation.GetPropertyName(), property);
 		}
 
-		animations.erase(
-			std::remove_if(animations.begin(), animations.end(), [](const ElementAnimation& animation) { return animation.IsComplete(); }),
-			animations.end()
-		);
+		auto it_completed = std::remove_if(animations.begin(), animations.end(), [](const ElementAnimation& animation) { return animation.IsComplete(); });
+
+		std::vector<Dictionary> dictionary_list;
+		dictionary_list.reserve(animations.end() - it_completed);
+
+		for (auto it = it_completed; it != animations.end(); ++it)
+		{
+			auto& dictionary = dictionary_list.emplace_back();
+			dictionary.Set(TRANSITION, it->IsTransition());
+			dictionary.Set("property", it->GetPropertyName());
+		}
+
+		// Need to erase elements before submitting event, so that we can add new animations of same property in the handler
+		animations.erase(it_completed, animations.end());
+
+		for (auto& dictionary : dictionary_list)
+			DispatchEvent(ANIMATIONEND, dictionary);
 	}
 }
 

+ 1 - 0
Source/Core/StringCache.cpp

@@ -130,6 +130,7 @@ const String DRAGDROP = "dragdrop";
 const String DRAGOUT = "dragout";
 const String DRAGEND = "dragend";
 const String RESIZE = "resize";
+const String ANIMATIONEND = "animationend";
 
 }
 }

+ 1 - 0
Source/Core/StringCache.h

@@ -137,6 +137,7 @@ extern const String DRAGOUT;
 extern const String DRAGEND;
 extern const String DRAGOVER;
 extern const String RESIZE;
+extern const String ANIMATIONEND;
 
 }
 }