Magic_Game/src/kiwano/platform/Input.h

142 lines
3.2 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - 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.
#pragma once
2019-10-11 21:55:29 +08:00
#include <kiwano/macros.h>
#include <kiwano/math/math.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2020-01-16 18:33:42 +08:00
#include <kiwano/core/event/Event.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/Component.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2019-12-20 17:49:16 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɻ<EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>̵İ<EFBFBD><EFBFBD><EFBFBD>״̬
*/
2019-04-11 14:40:54 +08:00
class KGE_API Input
: public Singleton<Input>
, public UpdateComponent
, public EventComponent
{
2019-12-20 17:49:16 +08:00
friend Singleton<Input>;
public:
2019-12-20 17:49:16 +08:00
/**
* \~chinese
2020-01-17 16:46:17 +08:00
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param key <EFBFBD><EFBFBD>ֵ
2019-12-20 17:49:16 +08:00
*/
2020-01-17 16:46:17 +08:00
bool IsDown(KeyCode key) const;
2019-12-20 17:49:16 +08:00
/**
* \~chinese
2020-01-17 16:46:17 +08:00
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ձ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param key <EFBFBD><EFBFBD>ֵ
2019-12-20 17:49:16 +08:00
*/
2020-01-17 16:46:17 +08:00
bool WasPressed(KeyCode key) const;
2019-12-20 17:49:16 +08:00
/**
* \~chinese
2020-01-17 16:46:17 +08:00
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̰<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>̧<EFBFBD><EFBFBD>
* @param key <EFBFBD><EFBFBD>ֵ
2019-12-20 17:49:16 +08:00
*/
2020-01-17 16:46:17 +08:00
bool WasReleased(KeyCode key) const;
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param btn <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*/
bool IsDown(MouseButton btn) const;
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ձ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param btn <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*/
bool WasPressed(MouseButton btn) const;
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>̧<EFBFBD><EFBFBD>
* @param btn <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*/
bool WasReleased(MouseButton btn) const;
2019-12-20 17:49:16 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> x <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD> x <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2020-01-16 18:33:42 +08:00
float GetMouseX() const;
2019-12-20 17:49:16 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> y <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD> y <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2020-01-16 18:33:42 +08:00
float GetMouseY() const;
2019-12-20 17:49:16 +08:00
/**
* \~chinese
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2020-01-16 18:33:42 +08:00
Point GetMousePos() const;
public:
2019-08-12 14:51:54 +08:00
void SetupComponent() override {}
void DestroyComponent() override {}
void AfterUpdate() override;
2020-01-16 18:33:42 +08:00
void HandleEvent(Event* evt) override;
2019-12-23 18:05:08 +08:00
private:
Input();
~Input();
2020-01-17 16:46:17 +08:00
void UpdateKey(KeyCode key, bool down);
void UpdateButton(MouseButton btn, bool down);
2020-01-16 18:33:42 +08:00
void UpdateMousePos(const Point& pos);
2019-12-23 18:05:08 +08:00
private:
2020-01-17 16:46:17 +08:00
static const int KEY_NUM = int(KeyCode::Last);
static const int BUTTON_NUM = int(MouseButton::Last);
2020-01-17 16:46:17 +08:00
bool want_update_keys_;
bool want_update_buttons_;
2020-01-16 18:33:42 +08:00
Point mouse_pos_;
2020-01-17 16:46:17 +08:00
enum KeyIndex : size_t
{
Current = 0,
Prev
};
std::array<bool, BUTTON_NUM> buttons_[2];
std::array<bool, KEY_NUM> keys_[2];
};
}