YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
InputManager.cpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2012 - 2013.
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/InputManager.h"
29 #include "YCLib/Input.h"
30 #include "Host.h"
31 
33 
34 using namespace UI;
35 
36 YSL_BEGIN_NAMESPACE(Devices)
37 
39  : GUI_state(FetchGUIState()), cursor_state()
40 #if YCL_HOSTED
41  , env(Host::FetchEnvironment())
42 #endif
43 {}
44 
45 #if YCL_DS
46 # define YCL_KEY_Touch KeyCodes::Touch
47 # define YCL_CURSOR_VALID
48 #elif YCL_MINGW32
49 # define YCL_KEY_Touch VK_LBUTTON
50 # define YCL_CURSOR_VALID if(cursor_state != Point::Invalid)
51 #else
52 # error Unsupported platform found!
53 #endif
54 void
56 {
57  const auto disp([&](const KeyInput& keyset, VisualEvent key_evt,
58  VisualEvent touch_evt){
59 #if YCL_MINGW32
60  if(keyset[YCL_KEY_Touch] || keyset[VK_RBUTTON])
61 #else
62  if(keyset[YCL_KEY_Touch])
63 #endif
64  {
65  YCL_CURSOR_VALID
66  {
67  TouchEventArgs e(wgt, keyset, cursor_state);
68 
69  GUI_state.get().ResponseTouch(e, touch_evt);
70  }
71  }
72  else if(keyset.any())
73  {
74  KeyEventArgs e(wgt, keyset);
75 
76  GUI_state.get().ResponseKey(e, key_evt);
77  }
78  });
80 
81  disp(keys, KeyUp, TouchUp);
83  disp(keys, KeyDown, TouchDown);
85 }
86 
87 IWidget*
89 {
90 #if YCL_MINGW32
91  const auto p_wnd(env.get().GetForegroundWindow());
92 
93  if(!p_wnd)
94  return nullptr;
95 
96 #endif
97  using namespace platform::KeyCodes;
98 
99  // FIXME: [DS] crashing after sleeping(default behavior of closing then
100  // reopening lid) on real machine due to LibNDS default interrupt
101  // handler for power management.
102 // platform::AllowSleep(true);
104 
105 #if YCL_MINGW32
106  const auto& key_st(platform_ex::FetchKeyState());
107 
108  if(key_st[YCL_KEY_Touch] || key_st[VK_RBUTTON])
109 #else
110  if(platform_ex::FetchKeyState()[YCL_KEY_Touch])
111 #endif
112  {
113  using namespace Drawing;
114 
115  platform::CursorInfo cursor;
116 
117  platform_ex::WriteCursor(cursor);
118 #if YCL_DS
119  cursor_state = cursor.operator Point();
120 #elif YCL_MINGW32
121  ::ScreenToClient(p_wnd->GetNativeHandle(), &cursor);
122 
123  const auto& pr(p_wnd->GetInputBounds());
124 
125  if(!(IsInInterval< ::LONG>(cursor.x, pr.first.X, pr.second.X)
126  && IsInInterval< ::LONG>(cursor.y, pr.first.Y, pr.second.Y)))
127  {
128  if(GUI_state.get().GetTouchDownPtr())
129  {
130  RestrictInInterval(cursor.x, pr.first.X, pr.second.X),
131  RestrictInInterval(cursor.y, pr.first.Y, pr.second.Y);
132  }
133  else
134  return nullptr;
135  }
136  yunseq(cursor_state.X = cursor.x - pr.first.X,
137  cursor_state.Y = cursor.y - pr.first.Y);
138 #endif
139  }
140 #if YCL_HOSTED
141  if(auto p_render_wnd = dynamic_cast<Host::RenderWindow*>(p_wnd))
142  return &p_render_wnd->GetRenderer().GetWidgetRef();
143 #endif
144  return nullptr;
145 }
146 #undef YCL_CURSOR_VALID
147 #undef YCL_KEY_Touch
148 
149 YSL_END_NAMESPACE(Devices)
150 
151 YSL_END
152