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)
|
2019-01-27 22:08:36 +08:00
|
|
|
, want_update_(false)
|
2018-11-08 00:21:59 +08:00
|
|
|
{
|
2018-11-22 19:31:44 +08:00
|
|
|
ZeroMemory(keys_, sizeof(keys_));
|
2019-01-27 22:08:36 +08:00
|
|
|
ZeroMemory(keys_pressed_, sizeof(keys_pressed_));
|
|
|
|
|
ZeroMemory(keys_released_, sizeof(keys_released_));
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
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-25 15:33:02 +08:00
|
|
|
HRESULT Input::Init(HWND hwnd, 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;
|
|
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
void Input::Update()
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
if (want_update_)
|
|
|
|
|
{
|
|
|
|
|
want_update_ = false;
|
|
|
|
|
|
|
|
|
|
ZeroMemory(keys_pressed_, sizeof(keys_pressed_));
|
|
|
|
|
ZeroMemory(keys_released_, sizeof(keys_released_));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Input::UpdateKey(int key, bool down)
|
|
|
|
|
{
|
|
|
|
|
if (down && !keys_[key])
|
|
|
|
|
keys_pressed_[key] = true;
|
|
|
|
|
if (!down && keys_[key])
|
|
|
|
|
keys_released_[key] = true;
|
|
|
|
|
|
|
|
|
|
keys_[key] = down;
|
|
|
|
|
|
|
|
|
|
want_update_ = true;
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
bool Input::IsDown(KeyCode code)
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
return keys_[static_cast<int>(code)];
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
bool Input::IsDown(MouseButton btn)
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
return keys_[static_cast<int>(btn)];
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
bool Input::WasPressed(KeyCode code)
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
return keys_pressed_[static_cast<int>(code)];
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
bool Input::WasPressed(MouseButton btn)
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
return keys_pressed_[static_cast<int>(btn)];
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
bool Input::WasReleased(KeyCode code)
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
return keys_released_[static_cast<int>(code)];
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
bool Input::WasReleased(MouseButton btn)
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
return keys_released_[static_cast<int>(btn)];
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
float Input::GetMouseX()
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-25 15:33:02 +08:00
|
|
|
return GetMousePos().x;
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
float Input::GetMouseY()
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-25 15:33:02 +08:00
|
|
|
return GetMousePos().y;
|
2018-11-22 19:31:44 +08:00
|
|
|
}
|
|
|
|
|
|
2019-01-24 12:21:01 +08:00
|
|
|
Point Input::GetMousePos()
|
2018-11-22 19:31:44 +08:00
|
|
|
{
|
2019-01-27 22:08:36 +08:00
|
|
|
Point mouse_pos = Point{};
|
|
|
|
|
if (HWND active_window = ::GetForegroundWindow())
|
|
|
|
|
{
|
|
|
|
|
if (active_window == hwnd_ || ::IsChild(active_window, hwnd_))
|
|
|
|
|
{
|
|
|
|
|
POINT pos;
|
|
|
|
|
if (::GetCursorPos(&pos) && ::ScreenToClient(hwnd_, &pos))
|
|
|
|
|
mouse_pos = Point((float)pos.x, (float)pos.y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mouse_pos;
|
2018-10-03 18:04:04 +08:00
|
|
|
}
|
2018-11-08 00:21:59 +08:00
|
|
|
}
|