| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | // 
 | 
					
						
							|  |  |  | // 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-08-12 18:30:42 +08:00
										 |  |  | #include "Actor.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | #include "Action.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | #include "Stage.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | #include "../base/logs.h"
 | 
					
						
							|  |  |  | #include "../renderer/render.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	namespace | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		float default_anchor_x = 0.f; | 
					
						
							|  |  |  | 		float default_anchor_y = 0.f; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetDefaultAnchor(float anchor_x, float anchor_y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		default_anchor_x = anchor_x; | 
					
						
							|  |  |  | 		default_anchor_y = anchor_y; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Actor::Actor() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		: visible_(true) | 
					
						
							|  |  |  | 		, update_pausing_(false) | 
					
						
							|  |  |  | 		, hover_(false) | 
					
						
							|  |  |  | 		, pressed_(false) | 
					
						
							|  |  |  | 		, responsible_(false) | 
					
						
							|  |  |  | 		, dirty_transform_(false) | 
					
						
							|  |  |  | 		, dirty_transform_inverse_(false) | 
					
						
							| 
									
										
										
										
											2019-07-29 13:42:13 +08:00
										 |  |  | 		, cascade_opacity_(false) | 
					
						
							| 
									
										
										
										
											2019-07-31 10:12:59 +08:00
										 |  |  | 		, show_border_(false) | 
					
						
							|  |  |  | 		, is_fast_transform_(true) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		, parent_(nullptr) | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		, stage_(nullptr) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		, hash_name_(0) | 
					
						
							|  |  |  | 		, z_order_(0) | 
					
						
							|  |  |  | 		, opacity_(1.f) | 
					
						
							| 
									
										
										
										
											2019-07-29 13:42:13 +08:00
										 |  |  | 		, displayed_opacity_(1.f) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		, anchor_(default_anchor_x, default_anchor_y) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::Update(Duration dt) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-05-03 15:27:18 +08:00
										 |  |  | 		if (!update_pausing_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			UpdateActions(this, dt); | 
					
						
							|  |  |  | 			UpdateTimers(dt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 15:27:18 +08:00
										 |  |  | 			if (cb_update_) | 
					
						
							|  |  |  | 				cb_update_(dt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 15:27:18 +08:00
										 |  |  | 			OnUpdate(dt); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!children_.IsEmpty()) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			ActorPtr next; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			for (auto child = children_.First(); child; child = next) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				next = child->NextItem(); | 
					
						
							|  |  |  | 				child->Update(dt); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::Render() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (!visible_) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		UpdateTransform(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (children_.IsEmpty()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			PrepareRender(); | 
					
						
							|  |  |  | 			OnRender(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// render children those are less than 0 in Z-Order
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			Actor* child = children_.First().Get(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			while (child) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (child->GetZOrder() >= 0) | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				child->Render(); | 
					
						
							|  |  |  | 				child = child->NextItem().Get(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			PrepareRender(); | 
					
						
							|  |  |  | 			OnRender(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			while (child) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				child->Render(); | 
					
						
							|  |  |  | 				child = child->NextItem().Get(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::RenderBorder() | 
					
						
							| 
									
										
										
										
											2019-07-31 10:12:59 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (show_border_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-05 09:07:58 +08:00
										 |  |  |             Rect bounds = GetBounds(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             auto renderer = Renderer::Instance(); | 
					
						
							|  |  |  |             renderer->SetTransform(transform_matrix_); | 
					
						
							|  |  |  |             renderer->FillRectangle(bounds, Color(Color::Red, .4f)); | 
					
						
							|  |  |  |             renderer->DrawRectangle(bounds, Color(Color::Red, .8f), 4.f); | 
					
						
							| 
									
										
										
										
											2019-07-31 10:12:59 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (auto child = children_.First(); child; child = child->NextItem()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			child->RenderBorder(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::Dispatch(Event& evt) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (!visible_) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		ActorPtr prev; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		for (auto child = children_.Last(); child; child = prev) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			prev = child->PrevItem(); | 
					
						
							|  |  |  | 			child->Dispatch(evt); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (responsible_ && MouseEvent::Check(evt.type)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (evt.type == Event::MouseMove) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (!evt.target && ContainsPoint(Point{ evt.mouse.x, evt.mouse.y })) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					evt.target = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (!hover_) | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						hover_ = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						Event hover = evt; | 
					
						
							|  |  |  | 						hover.type = Event::MouseHover; | 
					
						
							|  |  |  | 						EventDispatcher::Dispatch(hover); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else if (hover_) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					hover_ = false; | 
					
						
							|  |  |  | 					pressed_ = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Event out = evt; | 
					
						
							|  |  |  | 					out.target = this; | 
					
						
							|  |  |  | 					out.type = Event::MouseOut; | 
					
						
							|  |  |  | 					EventDispatcher::Dispatch(out); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (evt.type == Event::MouseBtnDown && hover_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				pressed_ = true; | 
					
						
							|  |  |  | 				evt.target = this; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (evt.type == Event::MouseBtnUp && pressed_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				pressed_ = false; | 
					
						
							|  |  |  | 				evt.target = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Event click = evt; | 
					
						
							|  |  |  | 				click.type = Event::Click; | 
					
						
							|  |  |  | 				EventDispatcher::Dispatch(click); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		EventDispatcher::Dispatch(evt); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Matrix const & Actor::GetTransformMatrix()  const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		UpdateTransform(); | 
					
						
							|  |  |  | 		return transform_matrix_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Matrix const & Actor::GetTransformInverseMatrix()  const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		UpdateTransform(); | 
					
						
							|  |  |  | 		if (dirty_transform_inverse_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 			transform_matrix_inverse_ = transform_matrix_.Invert(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			dirty_transform_inverse_ = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return transform_matrix_inverse_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::UpdateTransform() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (!dirty_transform_) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		dirty_transform_ = false; | 
					
						
							|  |  |  | 		dirty_transform_inverse_ = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		if (is_fast_transform_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			transform_matrix_ = Matrix::Translation(transform_.position); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// matrix multiplication is optimized by expression template
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:07:58 +08:00
										 |  |  |             transform_matrix_ = Matrix::SRT(transform_.position, transform_.scale, transform_.rotation); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!transform_.skew.IsOrigin()) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 transform_matrix_ = Matrix::Skewing(transform_.skew) * transform_matrix_; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 10:46:01 +08:00
										 |  |  | 		transform_matrix_.Translate(Point{ -size_.x * anchor_.x, -size_.y * anchor_.y }); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (parent_) | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-07-30 13:05:16 +08:00
										 |  |  | 			transform_matrix_ *= parent_->transform_matrix_; | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// update children's transform
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get()) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			child->dirty_transform_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::UpdateOpacity() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-07-29 13:42:13 +08:00
										 |  |  | 		if (parent_ && parent_->IsCascadeOpacityEnabled()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			displayed_opacity_ = opacity_ * parent_->displayed_opacity_; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-07-29 13:42:13 +08:00
										 |  |  | 			displayed_opacity_ = opacity_; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-29 13:42:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get()) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			child->UpdateOpacity(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetStage(Stage* scene) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		if (scene && stage_ != scene) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			stage_ = scene; | 
					
						
							|  |  |  | 			for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get()) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 				child->stage_ = scene; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::Reorder() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (parent_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			ActorPtr me = this; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			parent_->children_.Remove(me); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			Actor* sibling = parent_->children_.Last().Get(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 12:56:38 +08:00
										 |  |  | 			if (sibling && sibling->GetZOrder() > z_order_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				sibling = sibling->PrevItem().Get(); | 
					
						
							|  |  |  | 				while (sibling) | 
					
						
							|  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2019-06-06 12:56:38 +08:00
										 |  |  | 					if (sibling->GetZOrder() <= z_order_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 						break; | 
					
						
							|  |  |  | 					sibling = sibling->PrevItem().Get(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (sibling) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				parent_->children_.InsertAfter(me, sibling); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				parent_->children_.PushFront(me); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetZOrder(int zorder) | 
					
						
							| 
									
										
										
										
											2019-06-06 12:56:38 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (z_order_ != zorder) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			z_order_ = zorder; | 
					
						
							|  |  |  | 			Reorder(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetOpacity(float opacity) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (opacity_ == opacity) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-29 13:42:13 +08:00
										 |  |  | 		displayed_opacity_ = opacity_ = std::min(std::max(opacity, 0.f), 1.f); | 
					
						
							|  |  |  | 		UpdateOpacity(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetCascadeOpacityEnabled(bool enabled) | 
					
						
							| 
									
										
										
										
											2019-07-29 13:42:13 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (cascade_opacity_ == enabled) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cascade_opacity_ = enabled; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		UpdateOpacity(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetAnchorX(float anchor_x) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetAnchor(anchor_x, anchor_.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetAnchorY(float anchor_y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetAnchor(anchor_.x, anchor_y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetAnchor(float anchor_x, float anchor_y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (anchor_.x == anchor_x && anchor_.y == anchor_y) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		anchor_.x = anchor_x; | 
					
						
							|  |  |  | 		anchor_.y = anchor_y; | 
					
						
							|  |  |  | 		dirty_transform_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetAnchor(Point const& anchor) | 
					
						
							| 
									
										
										
										
											2019-04-08 14:15:27 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetAnchor(anchor.x, anchor.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetWidth(float width) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetSize(width, size_.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetHeight(float height) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetSize(size_.x, height); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetSize(const Size& size) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetSize(size.x, size.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetSize(float width, float height) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (size_.x == width && size_.y == height) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		size_.x = width; | 
					
						
							|  |  |  | 		size_.y = height; | 
					
						
							|  |  |  | 		dirty_transform_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetTransform(Transform const& transform) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		transform_ = transform; | 
					
						
							|  |  |  | 		dirty_transform_ = true; | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		is_fast_transform_ = false; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetVisible(bool val) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		visible_ = val; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetName(String const& name) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (!IsName(name)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			Object::SetName(name); | 
					
						
							|  |  |  | 			hash_name_ = std::hash<String>{}(name); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetPositionX(float x) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetPosition(x, transform_.position.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetPositionY(float y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetPosition(transform_.position.x, y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetPosition(const Point & p) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetPosition(p.x, p.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetPosition(float x, float y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (transform_.position.x == x && transform_.position.y == y) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		transform_.position.x = x; | 
					
						
							|  |  |  | 		transform_.position.y = y; | 
					
						
							|  |  |  | 		dirty_transform_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::Move(float x, float y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetPosition(transform_.position.x + x, transform_.position.y + y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::Move(const Point & v) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->Move(v.x, v.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetScaleX(float scale_x) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetScale(scale_x, transform_.scale.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetScaleY(float scale_y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetScale(transform_.scale.x, scale_y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetScale(float scale) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetScale(scale, scale); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetScale(float scale_x, float scale_y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (transform_.scale.x == scale_x && transform_.scale.y == scale_y) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		transform_.scale.x = scale_x; | 
					
						
							|  |  |  | 		transform_.scale.y = scale_y; | 
					
						
							|  |  |  | 		dirty_transform_ = true; | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		is_fast_transform_ = false; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetScale(Point const& scale) | 
					
						
							| 
									
										
										
										
											2019-04-08 14:15:27 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetScale(scale.x, scale.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetSkewX(float skew_x) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetSkew(skew_x, transform_.skew.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetSkewY(float skew_y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetSkew(transform_.skew.x, skew_y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetSkew(float skew_x, float skew_y) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (transform_.skew.x == skew_x && transform_.skew.y == skew_y) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		transform_.skew.x = skew_x; | 
					
						
							|  |  |  | 		transform_.skew.y = skew_y; | 
					
						
							|  |  |  | 		dirty_transform_ = true; | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		is_fast_transform_ = false; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetSkew(Point const& skew) | 
					
						
							| 
									
										
										
										
											2019-04-08 14:15:27 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		this->SetSkew(skew.x, skew.y); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetRotation(float angle) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (transform_.rotation == angle) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		transform_.rotation = angle; | 
					
						
							|  |  |  | 		dirty_transform_ = true; | 
					
						
							| 
									
										
										
										
											2019-07-29 12:58:17 +08:00
										 |  |  | 		is_fast_transform_ = false; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::AddChild(ActorPtr child) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		KGE_ASSERT(child && "Actor::AddChild failed, NULL pointer exception"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (child) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | #ifdef KGE_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (child->parent_) | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 				KGE_ERROR_LOG(L"The node to be added already has a parent"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			for (Actor* parent = parent_; parent; parent = parent->parent_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 				if (parent == child) | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 					KGE_ERROR_LOG(L"A node cannot be its own parent"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | #endif // KGE_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			children_.PushBack(child); | 
					
						
							|  |  |  | 			child->parent_ = this; | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			child->SetStage(this->stage_); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			child->dirty_transform_ = true; | 
					
						
							|  |  |  | 			child->UpdateOpacity(); | 
					
						
							| 
									
										
										
										
											2019-06-06 12:56:38 +08:00
										 |  |  | 			child->Reorder(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::AddChildren(Array<ActorPtr> const& children) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		for (const auto& node : children) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			this->AddChild(node); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Rect Actor::GetBounds() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return Rect(Point{}, size_); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Rect Actor::GetBoundingBox() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return GetTransformMatrix().Transform(GetBounds()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Array<ActorPtr> Actor::GetChildren(String const& name) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		Array<ActorPtr> children; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		size_t hash_code = std::hash<String>{}(name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get()) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			if (child->hash_name_ == hash_code && child->IsName(name)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				children.push_back(child); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return children; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	ActorPtr Actor::GetChild(String const& name) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		size_t hash_code = std::hash<String>{}(name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		for (Actor* child = children_.First().Get(); child; child = child->NextItem().Get()) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			if (child->hash_name_ == hash_code && child->IsName(name)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				return child; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nullptr; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	Actor::Children const & Actor::GetChildren() const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return children_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::RemoveFromParent() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (parent_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			parent_->RemoveChild(this); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::RemoveChild(ActorPtr child) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-04-08 14:15:27 +08:00
										 |  |  | 		RemoveChild(child.Get()); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::RemoveChild(Actor * child) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		KGE_ASSERT(child && "Actor::RemoveChild failed, NULL pointer exception"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (children_.IsEmpty()) | 
					
						
							| 
									
										
										
										
											2019-04-08 14:15:27 +08:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (child) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			child->parent_ = nullptr; | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 			if (child->stage_) child->SetStage(nullptr); | 
					
						
							|  |  |  | 			children_.Remove(ActorPtr(child)); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::RemoveChildren(String const& child_name) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (children_.IsEmpty()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		size_t hash_code = std::hash<String>{}(child_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 		Actor* next; | 
					
						
							|  |  |  | 		for (Actor* child = children_.First().Get(); child; child = next) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			next = child->NextItem().Get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (child->hash_name_ == hash_code && child->IsName(child_name)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				RemoveChild(child); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::RemoveAllChildren() | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		children_.Clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	void Actor::SetResponsible(bool enable) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		responsible_ = enable; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | 	bool Actor::ContainsPoint(const Point& point) const | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (size_.x == 0.f || size_.y == 0.f) | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Point local = GetTransformInverseMatrix().Transform(point); | 
					
						
							|  |  |  | 		return GetBounds().ContainsPoint(local); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void VisualNode::PrepareRender() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-05 09:07:58 +08:00
										 |  |  |         auto renderer = Renderer::Instance(); | 
					
						
							|  |  |  |         renderer->SetTransform(transform_matrix_); | 
					
						
							|  |  |  |         renderer->SetOpacity(displayed_opacity_); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |