| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | // Copyright (c) 2016-2018 Easy2D - Nomango
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 15:14:55 +08:00
										 |  |  | #include "ActionGroup.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  | #include "logs.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace easy2d | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	//-------------------------------------------------------
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	// ActionGroup
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	//-------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	ActionGroup::ActionGroup() | 
					
						
							|  |  |  | 		: sequence_(true) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	ActionGroup::ActionGroup(Array<ActionPtr> const& actions, bool sequence) | 
					
						
							|  |  |  | 		: sequence_(sequence) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->Add(actions); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	ActionGroup::~ActionGroup() | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	void ActionGroup::Init(NodePtr const& target) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 		if (actions_.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 			Done(); | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		current_ = actions_.First(); | 
					
						
							|  |  |  | 		current_->Restart(target);	// init first action
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!sequence_) | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 			// init all actions
 | 
					
						
							|  |  |  | 			for (; current_; current_ = current_->NextItem()) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				current_->Restart(target); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	void ActionGroup::Update(NodePtr const& target, Duration dt) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 		if (sequence_) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 			if (current_) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 				current_->UpdateStep(target, dt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (current_->IsDone()) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					current_ = current_->NextItem(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (current_) | 
					
						
							|  |  |  | 						current_->Restart(target);	// init next action
 | 
					
						
							|  |  |  | 					else | 
					
						
							|  |  |  | 						Complete(target); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 			bool done = true; | 
					
						
							|  |  |  | 			for (current_ = actions_.First(); current_; current_ = current_->NextItem()) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (!current_->IsDone()) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					done = false; | 
					
						
							|  |  |  | 					current_->UpdateStep(target, dt); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (done) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				Complete(target); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	void ActionGroup::Add(ActionPtr const& action) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (action) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 			actions_.PushBack(action); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	void ActionGroup::Add(Array<ActionPtr> const& actions) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 		for (const auto& action : actions) | 
					
						
							|  |  |  | 			Add(action); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	ActionPtr ActionGroup::Clone() const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 		auto group = new (std::nothrow) ActionGroup(); | 
					
						
							|  |  |  | 		if (group) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 			for (auto action = actions_.First(); action; action = action->NextItem()) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  | 				if (action) | 
					
						
							|  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 					group->Add(action->Clone()); | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 		return group; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 	ActionPtr ActionGroup::Reverse() const | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 		auto group = new (std::nothrow) ActionGroup(); | 
					
						
							|  |  |  | 		if (group && !actions_.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-09 22:45:27 +08:00
										 |  |  | 			for (auto action = actions_.Last(); action; action = action->PrevItem()) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 				group->Add(action->Reverse()); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-11 18:57:28 +08:00
										 |  |  | 		return group; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |