YSTest: InputManager.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

InputManager.cpp
浏览该文件的文档.
1 /*
2 © 2012-2017 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "Helper/YModules.h"
29 #include YFM_Helper_InputManager // for ystdex::erase_left;
30 #include YFM_YCLib_Input
31 #include YFM_Helper_Environment
32 #include YFM_Helper_HostRenderer // for Host::Window, Host::RenderWindow;
33 #include YFM_Helper_GUIApplication // for FetchGUIHost;
34 #if YCL_Win32
35 # include YFM_YSLib_UI_YControl // for UI::CallEvent;
36 #elif YCL_Android
37 # include YFM_YSLib_UI_YDesktop // for Desktop converting to IWidget;
38 #endif
39
40 namespace YSLib
41 {
42
43 using namespace UI;
44
45 namespace Devices
46 {
47
48 InputManager::InputManager()
49 : GUI_state(FetchGUIState()), cursor_state(), host(FetchGUIHost())
50 {}
51
52 #if YCL_DS
53 # define YF_Impl_CursorValid
54 #elif YF_Hosted
55 # define YF_Impl_CursorValid if(cursor_state != Point::Invalid)
56 #else
57 # error "Unsupported platform found."
58 #endif
59 void
60 InputManager::DispatchInput(IWidget& wgt)
61 {
62 auto& st(GUI_state.get());
63 const auto disp([&](const KeyInput& keyset, VisualEvent key_evt,
64 VisualEvent touch_evt){
65 if(keyset[KeyCodes::Primary] || keyset[KeyCodes::Secondary]
66 || keyset[KeyCodes::Tertiary])
67 {
68 YF_Impl_CursorValid
69 {
70 CursorEventArgs e(wgt, keyset, cursor_state);
71
72 st.ResponseCursor(e, touch_evt);
73 }
74 }
75 else if(keyset.any())
76 {
77 KeyEventArgs e(wgt, keyset);
78
79 st.ResponseKey(e, key_evt);
80 }
81 });
82 KeyInput keys(platform_ex::FetchKeyUpState());
83
84 disp(keys, KeyUp, TouchUp);
85 #if YCL_Win32
86 YF_Impl_CursorValid
87 {
88 CursorEventArgs e(wgt, keys, cursor_state);
89
90 st.ResponseCursor(e, CursorOver);
91 }
92 #endif
93 keys = platform_ex::FetchKeyDownState();
94 disp(keys, KeyDown, TouchDown);
95 keys = platform_ex::FetchKeyState();
96 disp(keys, KeyHeld, TouchHeld);
97 #if YCL_Win32
98 if(const auto p_wnd = host.get().GetForegroundWindow())
99 {
100 auto& input_host(p_wnd->InputHost);
101 const UI::WheelDelta raw_mouse(input_host.RawMouseButton);
102
103 if(raw_mouse != 0)
104 {
105 CursorWheelEventArgs e(wgt, raw_mouse, keys, cursor_state);
106
107 st.ResponseCursor(e, CursorWheel);
108 input_host.RawMouseButton = 0;
109 }
110
111 // TODO: Blocked. Use C++14 lambda initializers to simplify
112 // implementation.
113 const auto p_input(st.ExternalTextInputFocusPtr);
114
115 input_host.AccessInputString([=, &st](String& ustr){
116 if(YB_UNLIKELY(p_input != p_text_focus_cache))
117 {
118 ustr.clear();
119 yunseq(p_text_focus_cache = p_input,
120 caret_location_cache = Point::Invalid);
121 }
122 else if(p_input)
123 {
124 // XXX: Save actual key state.
125 KeyInput k;
126 size_t n(0);
127
128 for(const auto c : ustr)
129 {
130 ++n;
131 YTraceDe(Informative, "Host character %d found.", int(c));
132 if(platform::IsPrint(c))
133 // XXX: Redundant nonzero test on character value.
134 CallInputEvent(c, k, *p_input);
135 else
136 break;
137 }
138 ystdex::erase_left(n, ustr);
139 // TODO: Resolve duplicate locking.
140 if(st.CaretLocation != caret_location_cache)
141 {
142 caret_location_cache = st.CaretLocation;
143 p_wnd->UpdateTextInputFocus(Deref(p_text_focus_cache),
144 caret_location_cache);
145 }
146 }
147 });
148 }
149 #endif
150 }
151
152 observer_ptr<IWidget>
153 InputManager::Update()
154 {
155 using namespace platform::KeyCodes;
156
157 // FIXME: [DS] crashing after sleeping(default behavior of closing then
158 // reopening lid) on real machine due to LibNDS default interrupt
159 // handler for power management.
160 // platform::AllowSleep(true);
161 platform_ex::UpdateKeyStates();
162
163 #if YF_Hosted
164 observer_ptr<Host::Window> p_wnd;
165
166 #endif
167 #if YCL_Win32
168 tie(p_wnd, cursor_state) = host.get().MapCursor();
169 if(!p_wnd)
170 return make_observer(&host.get().Desktop);
171 #elif YF_Hosted
172 // TODO: Determine which inactive window should be used.
173 cursor_state = host.get().MapCursor().second;
174 if(const auto p_render_wnd = dynamic_cast<Host::RenderWindow*>(p_wnd.get()))
175 return make_observer(&p_render_wnd->GetRenderer().GetWidgetRef());
176 #elif YCL_DS
177 if(platform_ex::FetchKeyState()[Touch])
178 cursor_state = platform_ex::FetchCursor();
179 #endif
180 #if YCL_Android
181 return make_observer(&host.get().Desktop);
182 #else
183 return {};
184 #endif
185 }
186 #undef YF_Impl_CursorValid
187
188 } // namespace Devices;
189
190 } // namespace YSLib;
191
YTraceDe
#define YTraceDe(...)
YCLib 默认调试跟踪。
Definition: Debug.h:313
YSLib::UI::KeyDown
键接触开始。
Definition: YWidgetEvent.h:387
YSLib::UI::CursorWheel
指针设备滚轮输入。
Definition: YWidgetEvent.h:408
YSLib::UI::TouchUp
屏幕接触结束。
Definition: YWidgetEvent.h:390
YSLib::Devices::InputManager::DispatchInput
void DispatchInput(UI::IWidget &)
向指定部件分发响应输入状态。
Definition: InputManager.cpp:60
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
UI
YSLib::UI::CursorEventArgs
指针设备输入事件参数类。
Definition: YWidgetEvent.h:210
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:326
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:451
platform::KeyCodes
本机按键编码。
Definition: Keys.h:203
YSLib::Devices::InputManager::Update
observer_ptr< UI::IWidget > Update()
更新输入状态。
Definition: InputManager.cpp:153
platform::KeyCodes::DS::Tertiary
Definition: Keys.h:265
YSLib::UI::wgt
IWidget & wgt
Definition: YControl.h:112
YSLib::UI::TouchDown
屏幕接触开始。
Definition: YWidgetEvent.h:391
YB_UNLIKELY
#define YB_UNLIKELY(_expr)
分支预测提示。
Definition: ydef.h:464
ystdex::erase_left
enable_if_t< is_class< decay_t< _tString > >::value, _tString && > erase_left(typename string_traits< _tString >::size_type pos, _tString &&str)
删除字符串中指定位置或指定字符最先出现的位置的左侧的字符串。
Definition: string.hpp:565
YSLib::UI::TouchHeld
屏幕接触保持。
Definition: YWidgetEvent.h:392
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1022
YSLib::UI::FetchGUIState
GUIState & FetchGUIState() ynothrow
Definition: YGUI.cpp:508
YSLib::UI::VisualEvent
VisualEvent
标准控件事件空间。
Definition: YWidgetEvent.h:370
platform_ex::FetchKeyDownState
YF_API platform::KeyInput FetchKeyDownState()
取键按下状态。
Definition: Input.cpp:143
platform_ex::UpdateKeyStates
YF_API void UpdateKeyStates()
更新按键状态。
Definition: Input.cpp:170
YSLib::Drawing::GBinaryGroup< SPos >::Invalid
static const GBinaryGroup Invalid
无效（不在屏幕坐标系中）对象。
Definition: YGDIBase.h:58
YSLib::Devices::InputManager::cursor_state
Drawing::Point cursor_state
指针设备光标位置。
Definition: InputManager.h:57
ystdex::lref::get
_type & get() const
Definition: ref.hpp:108
YSLib::UI::CallInputEvent
YF_API void CallInputEvent(IWidget &, const String &, const KeyInput &)
调用指定部件的 TextInput 事件处理指定的输入。
Definition: YGUI.cpp:120
YSLib::UI::CursorOver
指针设备光标悬停。
Definition: YWidgetEvent.h:403
platform::KeyInput
std::bitset< KeyBitsetWidth > KeyInput
按键并行位宽。
Definition: Keys.h:67
YSLib::Devices::InputManager::GUI_state
lref< UI::GUIState > GUI_state
GUI 状态。
Definition: InputManager.h:52
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
YSLib::UI::KeyEventArgs
按键输入事件参数类。
Definition: YWidgetEvent.h:186
YModules.h
YSLib::UI::KeyUp
键接触结束。
Definition: YWidgetEvent.h:386
platform::KeyCodes::DS::Primary
static yconstexpr const NativeSet Primary
Definition: Keys.h:241
YSLib::UI::CursorWheelEventArgs
滚轮事件参数。
Definition: YWidgetEvent.h:250
platform_ex::FetchKeyUpState
YF_API platform::KeyInput FetchKeyUpState()
取键释放状态。
Definition: Input.cpp:151
platform::Descriptions::Informative
Definition: YCommon.h:169
platform_ex::FetchKeyState
YF_API const platform::KeyInput & FetchKeyState()
取按键状态。
Definition: Input.cpp:127
CHRLib::c
c
Definition: CharacterProcessing.h:81
YSLib::UI::KeyHeld
键接触保持。
Definition: YWidgetEvent.h:388
platform::KeyCodes::DS::Touch
Definition: Keys.h:230
YSLib::Devices::InputManager::host
lref< GUIHost > host
GUI 宿主。
Definition: InputManager.h:62
YSLib::FetchGUIHost
FetchGUIHost
Definition: GUIApplication.h:296
YSLib::UI::WheelDelta
ptrdiff_t WheelDelta
滚轮度量：以角度计量的转动滚轮的幅度。
Definition: YWidgetEvent.h:243
platform::KeyCodes::DS::Secondary
Definition: Keys.h:263
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: YString.h:46