| 
									
										
										
										
											2018-10-03 22:02:46 +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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | #include "input.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  | #include "logs.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-15 01:02:05 +08:00
										 |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2018-10-03 18:04:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | namespace easy2d | 
					
						
							| 
									
										
										
										
											2018-10-03 18:04:04 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	Input::Input() | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 		: hwnd_(nullptr) | 
					
						
							|  |  |  | 		, scale_x_(1.f) | 
					
						
							|  |  |  | 		, scale_y_(1.f) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 		ZeroMemory(keys_, sizeof(keys_)); | 
					
						
							|  |  |  | 		ZeroMemory(keys_cache_, sizeof(keys_cache_)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	Input::~Input() | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 		E2D_LOG(L"Destroying input device"); | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	HRESULT Input::Init(HWND hwnd, float scale_x, float scale_y, bool debug) | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 		E2D_LOG(L"Initing input device"); | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		hwnd_ = hwnd; | 
					
						
							|  |  |  | 		scale_x_ = scale_x; | 
					
						
							|  |  |  | 		scale_y_ = scale_y; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return S_OK; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	void Input::Update() | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		memcpy(keys_cache_, keys_, sizeof(keys_cache_)); | 
					
						
							|  |  |  | 		GetKeyboardState(keys_); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	bool Input::IsDown(KeyCode code) | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return !!(keys_[static_cast<int>(code)] & 0x80); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	bool Input::IsDown(MouseButton btn) | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return !!(keys_[static_cast<int>(btn)] & 0x80); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	bool Input::WasPressed(KeyCode code) | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return !(keys_cache_[static_cast<int>(code)] & 0x80) | 
					
						
							|  |  |  | 			&& (keys_[static_cast<int>(code)] & 0x80); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	bool Input::WasPressed(MouseButton btn) | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return !(keys_cache_[static_cast<int>(btn)] & 0x80) | 
					
						
							|  |  |  | 			&& (keys_[static_cast<int>(btn)] & 0x80); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	bool Input::WasReleased(KeyCode code) | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return (keys_cache_[static_cast<int>(code)] & 0x80) | 
					
						
							|  |  |  | 			&& !(keys_[static_cast<int>(code)] & 0x80); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	bool Input::WasReleased(MouseButton btn) | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return (keys_cache_[static_cast<int>(btn)] & 0x80) | 
					
						
							|  |  |  | 			&& !(keys_[static_cast<int>(btn)] & 0x80); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	float Input::GetMouseX() | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		POINT pos; | 
					
						
							|  |  |  | 		::GetCursorPos(&pos); | 
					
						
							|  |  |  | 		::ScreenToClient(hwnd_, &pos); | 
					
						
							|  |  |  | 		return pos.x * scale_x_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	float Input::GetMouseY() | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		POINT pos; | 
					
						
							|  |  |  | 		::GetCursorPos(&pos); | 
					
						
							|  |  |  | 		::ScreenToClient(hwnd_, &pos); | 
					
						
							|  |  |  | 		return pos.y * scale_y_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	Point Input::GetMousePos() | 
					
						
							| 
									
										
										
										
											2018-11-22 19:31:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		POINT pos; | 
					
						
							|  |  |  | 		::GetCursorPos(&pos); | 
					
						
							|  |  |  | 		::ScreenToClient(hwnd_, &pos); | 
					
						
							|  |  |  | 		return Point{ pos.x * scale_x_, pos.y * scale_y_ }; | 
					
						
							| 
									
										
										
										
											2018-10-03 18:04:04 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | } |