Kiwano Engine v1.3.x
Input.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <array>
23#include <kiwano/core/Common.h>
24#include <kiwano/base/Module.h>
25#include <kiwano/platform/Keys.h>
26#include <kiwano/event/Event.h>
27#include <kiwano/macros.h>
28#include <kiwano/math/Math.h>
29
30namespace kiwano
31{
36class KGE_API Input final
37 : public Singleton<Input>
38 , public Module
39{
40 friend Singleton<Input>;
41
42public:
48 bool IsDown(KeyCode key) const;
49
55 bool WasPressed(KeyCode key) const;
56
62 bool WasReleased(KeyCode key) const;
68 bool IsDown(MouseButton btn) const;
69
75 bool WasPressed(MouseButton btn) const;
76
82 bool WasReleased(MouseButton btn) const;
83
89 float GetMouseX() const;
90
96 float GetMouseY() const;
97
103 Point GetMousePos() const;
104
105public:
106 void OnUpdate(UpdateModuleContext& ctx) override;
107
108 void HandleEvent(EventModuleContext& ctx) override;
109
110 ~Input();
111
112private:
113 Input();
114
115 void UpdateKey(KeyCode key, bool down);
116
117 void UpdateButton(MouseButton btn, bool down);
118
119 void UpdateMousePos(const Point& pos);
120
121private:
122 static const int KEY_NUM = int(KeyCode::Last);
123 static const int BUTTON_NUM = int(MouseButton::Last);
124
125 bool want_update_keys_;
126 bool want_update_buttons_;
127 Point mouse_pos_;
128
129 enum KeyIndex : size_t
130 {
131 Current = 0,
132 Prev
133 };
134
135 std::array<bool, BUTTON_NUM> buttons_[2];
136 std::array<bool, KEY_NUM> keys_[2];
137};
138} // namespace kiwano
时间模块上下文
Definition: Module.h:96
输入设备实例,可获取鼠标和键盘的按键状态
Definition: Input.h:39
基础模块
Definition: Module.h:111
Definition: Singleton.h:28
更新模块上下文
Definition: Module.h:83