125 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C
		
	
	
	
		
		
			
		
	
	
			125 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C
		
	
	
	
| 
								 | 
							
								// 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.
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								#pragma once
							 | 
						|||
| 
								 | 
							
								#include "include-forwards.h"
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								namespace easy2d
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
									using ActionCallback = std::function<void()>;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									class ActionManager;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									class Action
							 | 
						|||
| 
								 | 
							
										: public virtual Object
							 | 
						|||
| 
								 | 
							
										, protected IntrusiveListItem<ActionPtr>
							 | 
						|||
| 
								 | 
							
									{
							 | 
						|||
| 
								 | 
							
										friend class ActionManager;
							 | 
						|||
| 
								 | 
							
										friend class ActionGroup;
							 | 
						|||
| 
								 | 
							
										friend class IntrusiveList<ActionPtr>;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									public:
							 | 
						|||
| 
								 | 
							
										enum class Status
							 | 
						|||
| 
								 | 
							
										{
							 | 
						|||
| 
								 | 
							
											NotStarted,
							 | 
						|||
| 
								 | 
							
											Delayed,
							 | 
						|||
| 
								 | 
							
											Started,
							 | 
						|||
| 
								 | 
							
											Done,
							 | 
						|||
| 
								 | 
							
											Removeable
							 | 
						|||
| 
								 | 
							
										};
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										Action();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										virtual ~Action();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
							 | 
						|||
| 
								 | 
							
										inline void Resume()										{ running_ = true; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>
							 | 
						|||
| 
								 | 
							
										inline void Pause()											{ running_ = false; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
							 | 
						|||
| 
								 | 
							
										inline void Stop()											{ status_ = Status::Removeable; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD>ʱ
							 | 
						|||
| 
								 | 
							
										inline void SetDelay(Duration delay)						{ delay_ = delay; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (-1 Ϊ<><CEAA><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>)
							 | 
						|||
| 
								 | 
							
										inline void SetLoops(int loops)								{ loops_ = loops; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƴ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ڵ<EFBFBD>
							 | 
						|||
| 
								 | 
							
										inline void RemoveTargetWhenDone()							{ detach_target_ = true; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
							 | 
						|||
| 
								 | 
							
										inline void SetDoneCallback(ActionCallback const& cb)		{ cb_done_ = cb; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
							 | 
						|||
| 
								 | 
							
										inline void SetLoopDoneCallback(ActionCallback const& cb)	{ cb_loop_done_ = cb; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>
							 | 
						|||
| 
								 | 
							
										virtual ActionPtr Clone() const = 0;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ת
							 | 
						|||
| 
								 | 
							
										virtual ActionPtr Reverse() const = 0;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline void Done()											{ status_ = Status::Done; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline Status GetStatus() const								{ return status_; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline bool IsRunning() const								{ return running_; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline bool IsDone() const									{ return status_ == Status::Done || status_ == Status::Removeable; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline bool IsRemoveable() const							{ return status_ == Status::Removeable; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline int GetLoops() const									{ return loops_; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline Duration GetDelay() const							{ return delay_; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline Duration GetElapsed() const							{ return elapsed_; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline ActionCallback const& GetDoneCallback() const		{ return cb_done_; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										inline ActionCallback const& GetLoopDoneCallback() const	{ return cb_loop_done_; }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									protected:
							 | 
						|||
| 
								 | 
							
										virtual void Init(NodePtr const& target) {}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										virtual void Update(NodePtr const& target, Duration dt) {}
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										void UpdateStep(NodePtr const& target, Duration dt);
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										void Complete(NodePtr const& target);
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
										void Restart(NodePtr const& target);
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
									protected:
							 | 
						|||
| 
								 | 
							
										Status			status_;
							 | 
						|||
| 
								 | 
							
										bool			running_;
							 | 
						|||
| 
								 | 
							
										bool			detach_target_;
							 | 
						|||
| 
								 | 
							
										int				loops_;
							 | 
						|||
| 
								 | 
							
										int				loops_done_;
							 | 
						|||
| 
								 | 
							
										Duration		delay_;
							 | 
						|||
| 
								 | 
							
										Duration		elapsed_;
							 | 
						|||
| 
								 | 
							
										ActionCallback	cb_done_;
							 | 
						|||
| 
								 | 
							
										ActionCallback	cb_loop_done_;
							 | 
						|||
| 
								 | 
							
									};
							 | 
						|||
| 
								 | 
							
								}
							 |