Magic_Game/src/core/Input.cpp

112 lines
2.7 KiB
C++
Raw Normal View History

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.
#include "input.h"
2018-11-15 17:59:18 +08:00
#include "logs.h"
#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)
, want_update_(false)
2018-11-08 00:21:59 +08:00
{
2018-11-22 19:31:44 +08:00
ZeroMemory(keys_, sizeof(keys_));
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-03-10 13:44:02 +08:00
HRESULT Input::Init(HWND hwnd)
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
{
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-02-03 21:45:56 +08:00
bool Input::IsDown(int code_or_btn)
2018-11-22 19:31:44 +08:00
{
2019-02-03 21:45:56 +08:00
E2D_ASSERT(code_or_btn >= 0 && code_or_btn < 256);
return keys_[code_or_btn];
2018-11-22 19:31:44 +08:00
}
2019-02-03 21:45:56 +08:00
bool Input::WasPressed(int code_or_btn)
2018-11-22 19:31:44 +08:00
{
2019-02-03 21:45:56 +08:00
E2D_ASSERT(code_or_btn >= 0 && code_or_btn < 256);
return keys_pressed_[code_or_btn];
2018-11-22 19:31:44 +08:00
}
2019-02-03 21:45:56 +08:00
bool Input::WasReleased(int code_or_btn)
2018-11-22 19:31:44 +08:00
{
2019-02-03 21:45:56 +08:00
E2D_ASSERT(code_or_btn >= 0 && code_or_btn < 256);
return keys_released_[code_or_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
{
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
}