| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Button.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace easy2d | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	namespace ui | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		Button::Button() | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 			: enabled_(true) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			, is_selected_(false) | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 			, click_callback_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			, status_(Status::Normal) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-26 13:09:58 +08:00
										 |  |  | 			SetResponsible(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 16:12:15 +08:00
										 |  |  | 			AddListener(Event::MouseHover, Closure(this, &Button::UpdateStatus)); | 
					
						
							|  |  |  | 			AddListener(Event::MouseOut, Closure(this, &Button::UpdateStatus)); | 
					
						
							|  |  |  | 			AddListener(Event::MouseBtnDown, Closure(this, &Button::UpdateStatus)); | 
					
						
							|  |  |  | 			AddListener(Event::MouseBtnUp, Closure(this, &Button::UpdateStatus)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 		Button::Button(const Callback& click) | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 			: Button() | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 			this->SetClickCallback(click); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Button::Button(Callback const & click, Callback const & pressed, Callback const & mouse_over, Callback const & mouse_out) | 
					
						
							|  |  |  | 			: Button() | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			this->SetClickCallback(click); | 
					
						
							|  |  |  | 			this->SetPressedCallback(pressed); | 
					
						
							|  |  |  | 			this->SetMouseOverCallback(mouse_over); | 
					
						
							|  |  |  | 			this->SetMouseOutCallback(mouse_out); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  | 		Button::~Button() | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		bool Button::IsEnable() const | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return enabled_; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		void Button::SetEnabled(bool enabled) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (enabled_ != enabled) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				enabled_ = enabled; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 		void Button::SetClickCallback(const Callback& func) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 			click_callback_ = func; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		void Button::SetPressedCallback(const Callback & func) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			pressed_callback_ = func; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		void Button::SetMouseOverCallback(const Callback & func) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			mouse_over_callback_ = func; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		void Button::SetMouseOutCallback(const Callback & func) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			mouse_out_callback_ = func; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		void Button::SetStatus(Status status) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (status_ != status) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				status_ = status; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 		void Button::UpdateStatus(Event const& evt) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 			E2D_ASSERT(MouseEvent::Check(evt.type)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-26 13:09:58 +08:00
										 |  |  | 			if (enabled_ && (evt.target == this)) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-12 16:12:15 +08:00
										 |  |  | 				if (evt.type == Event::MouseHover) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 					SetStatus(Status::Hover); | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					if (mouse_over_callback_) | 
					
						
							|  |  |  | 						mouse_over_callback_(); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-02-12 16:12:15 +08:00
										 |  |  | 				else if (evt.type == Event::MouseOut) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 					SetStatus(Status::Normal); | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					if (mouse_out_callback_) | 
					
						
							|  |  |  | 						mouse_out_callback_(); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-02-12 16:12:15 +08:00
										 |  |  | 				else if (evt.type == Event::MouseBtnDown && status_ == Status::Hover) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 					SetStatus(Status::Pressed); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (pressed_callback_) | 
					
						
							|  |  |  | 						pressed_callback_(); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-02-12 16:12:15 +08:00
										 |  |  | 				else if (evt.type == Event::MouseBtnUp && status_ == Status::Pressed) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 					SetStatus(Status::Hover); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 19:29:32 +08:00
										 |  |  | 					if (click_callback_) | 
					
						
							|  |  |  | 						click_callback_(); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |