YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ywgtevt.h
浏览该文件的文档.
1 /*
2  © 2010-2014 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 #ifndef YSL_INC_UI_ywgtevt_h_
29 #define YSL_INC_UI_ywgtevt_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_UI_YComponent
33 #include YFM_YSLib_Core_YEvent
34 #include YFM_YSLib_Core_YString
35 #include YFM_YSLib_Core_YDevice // for Devices::KeyInputDevice::Tester,
36 // Devices::KeyInputDevice::DefaultTest;
37 
38 namespace YSLib
39 {
40 
41 namespace UI
42 {
43 
53 
54 
60 {
61 private:
62  IWidget* pSender;
63 
64 public:
65  explicit
66  UIEventArgs(IWidget& wgt)
67  : pSender(&wgt)
68  {}
75  virtual DefDeDtor(UIEventArgs)
76 
82 
83  DefGetter(const ynothrow, IWidget&, Sender, *pSender)
84  PDefH(void, SetSender, IWidget& wgt)
85  ImplExpr(pSender = &wgt)
86 };
87 
88 
94 {
95 public:
101  {
102  Bubble = 0,
103  Tunnel = 1,
104  Direct = 2
105  };
106 
108  bool Handled;
109 
110  RoutedEventArgs(IWidget& wgt, RoutingStrategy strategy = Direct)
111  : UIEventArgs(wgt),
112  Strategy(strategy), Handled(false)
113  {}
114 };
115 
116 
122 {
123 public:
129  Tester{Devices::KeyInputDevice::DefaultTest};
135 
140  InputEventArgs(IWidget&, const KeyInput& = {}, RoutingStrategy = Direct);
141 
146  DefCvt(const ynothrow, const KeyInput&, Keys)
147 
148 
152  PDefHOp(bool, [], KeyIndex code) const
153  ImplRet(Tester(Keys, code))
154 
159  DefGetter(const ynothrow, const KeyInput&, Keys, Keys)
160 };
161 
162 
168 {
169 public:
170  using InputType = KeyInput;
171 
175  KeyEventArgs(IWidget&, const InputType& = {}, RoutingStrategy = Direct);
176 };
177 
178 
183 struct YF_API CursorEventArgs : public InputEventArgs
184 {
185 public:
187 
190 
195  CursorEventArgs(IWidget&, const KeyInput&, const InputType& = {},
196  RoutingStrategy = Direct);
197 
202  DefCvt(const ynothrow, const InputType&, Position)
203 };
204 
205 
210 using WheelDelta = ptrdiff_t;
211 
212 
218 {
219 private:
221 
222 public:
223  CursorWheelEventArgs(IWidget&, WheelDelta, const KeyInput&,
224  const InputType& = {}, RoutingStrategy = Direct);
225 
226  DefGetter(const ynothrow, WheelDelta, Delta, delta)
227 };
228 
229 
234 class YF_API TextInputEventArgs : public InputEventArgs
235 {
236 public:
238 
239  TextInputEventArgs(IWidget&, String, const KeyInput& = {},
240  RoutingStrategy = Direct);
241  TextInputEventArgs(InputEventArgs, String);
242 };
243 
244 
251 template<typename _type>
253 {
255  using ValueType = _type;
256 
258 
262  template<typename... _tParams>
263  yconstfn
264  GValueEventArgs(IWidget& wgt, _tParams&&... args)
265  : UIEventArgs(wgt),
266  Value(yforward(args)...)
267  {}
268  DefCvt(const ynothrow, ValueType, Value)
269 };
270 
271 
276 struct YF_API PaintEventArgs : public UIEventArgs, public PaintContext
277 {
278  PaintEventArgs(IWidget&);
279  PaintEventArgs(IWidget&, const PaintContext&);
280  PaintEventArgs(IWidget&, const Drawing::Graphics&, const Drawing::Point&,
281  const Drawing::Rect&);
282 };
283 
284 
285 //事件处理器类型。
286 DeclDelegate(HUIEvent, UIEventArgs&&)
287 DeclDelegate(HInputEvent, InputEventArgs&&)
288 DeclDelegate(HKeyEvent, KeyEventArgs&&)
290 DeclDelegate(HCursorEvent, CursorEventArgs&&)
291 DeclDelegate(HPaintEvent, PaintEventArgs&&)
296 DeclDelegate(HCursorWheelEvent, CursorWheelEventArgs&&)
301 DeclDelegate(HTextInputEvent, TextInputEventArgs&&)
302 //DefDelegate(HPointEvent, Drawing::Point&&)
303 //DefDelegate(HSizeEvent, Size&&)
304 
305 
306 #define DefEventTypeMapping(_name, _tEventHandler) \
307  template<> \
308  struct EventTypeMapping<_name> \
309  { \
310  using HandlerType = _tEventHandler; \
311  };
312 
313 
318 enum VisualEvent
319 {
320 // AutoSizeChanged,
321 // BackColorChanged,
322 // ForeColorChanged,
323 // LocationChanged,
324 // MarginChanged,
325 // VisibleChanged,
326 
327 // EnabledChanged,
328 
329  //视图变更事件。
330  Move,
331  Resize,
332 
333  // GUI 输入事件。
334  KeyUp,
335  KeyDown,
336  KeyHeld,
337  KeyPress,
338  TouchUp,
339  TouchDown,
340  TouchHeld,
341  Click,
342 
346  ClickAcross,
351  CursorOver,
356  CursorWheel,
361  TextInput,
362 
363  // GUI 输出事件。
364  Paint,
365 
366  //焦点事件。
367  GotFocus,
368  LostFocus,
369 
370  //边界事件。
371  Enter,
372  Leave
373 // TextChanged,
374 // FontChanged,
375 // FontColorChanged,
376 // FontSizeChanged,
377 };
378 
379 
380 template<VisualEvent>
382 {
383  //定义 HandlerType 的默认值可能会导致运行期 dynamic_cast 失败。
384 // using HandlerType = HEvent;
385 };
386 
387 DefEventTypeMapping(Move, HUIEvent)
388 DefEventTypeMapping(Resize, HUIEvent)
389 
390 DefEventTypeMapping(KeyUp, HKeyEvent)
391 DefEventTypeMapping(KeyDown, HKeyEvent)
392 DefEventTypeMapping(KeyHeld, HKeyEvent)
393 DefEventTypeMapping(KeyPress, HKeyEvent)
395 DefEventTypeMapping(CursorOver, HCursorEvent)
396 DefEventTypeMapping(TouchUp, HCursorEvent)
397 DefEventTypeMapping(TouchDown, HCursorEvent)
398 DefEventTypeMapping(TouchHeld, HCursorEvent)
399 DefEventTypeMapping(Click, HCursorEvent)
401 DefEventTypeMapping(ClickAcross, HCursorEvent)
403 DefEventTypeMapping(CursorWheel, HCursorWheelEvent)
405 DefEventTypeMapping(TextInput, HTextInputEvent)
406 
407 DefEventTypeMapping(Paint, HPaintEvent)
408 
409 DefEventTypeMapping(GotFocus, HUIEvent)
410 DefEventTypeMapping(LostFocus, HUIEvent)
411 
412 DefEventTypeMapping(Enter, HCursorEvent)
413 DefEventTypeMapping(Leave, HCursorEvent)
414 
415 
420 namespace EventMapping
421 {
422 
424 using ItemType = GIHEvent<UIEventArgs&&>;
425 using PairType = pair<VisualEvent, MappedType>;
426 using MapType = map<VisualEvent, MappedType>;
427 using SearchResult = pair<typename MapType::iterator, bool>; \
429 
430 } // namespace EventMapping;
431 
433 
434 
441 {
442 public:
444  BadEvent(const std::string& msg = "")
445  : LoggedEvent(msg)
446  {}
447 };
448 
449 
456 {
457 public:
458  UIEventSignal(const std::string& msg = "")
459  : MessageSignal(msg)
460  {}
461 };
462 
463 
469 {
470 private:
471  bool enabled;
472 
473 public:
477  AController(bool b = true)
478  : enabled(b)
479  {}
484  virtual DefDeDtor(AController)
485 
486  DefPred(const ynothrow, Enabled, enabled)
487 
492  DeclIEntry(EventMapping::ItemType& GetItem(const VisualEvent&))
497  virtual EventMapping::ItemType&
498  GetItemRef(const VisualEvent& id, EventMapping::MappedType(&)())
499  {
500  return GetItem(id);
501  }
502 
503  DefSetter(bool, Enabled, enabled)
504 
505  /*
506  \brief 复制实例。
507  \since build 409
508  */
509  DeclIEntry(AController* clone() const override)
510 };
511 
512 
514 template<class _tEventHandler>
515 size_t
516 DoEvent(AController& controller, const VisualEvent& id,
517  typename EventArgsHead<typename _tEventHandler::TupleType>::type&& e)
518 {
519  try
520  {
521  return dynamic_cast<EventT(typename _tEventHandler)&>(
522  controller.GetItem(id))(std::move(e));
523  }
524  catch(std::out_of_range&)
525  {}
526  catch(std::bad_cast&)
527  {}
528  return 0;
529 }
530 
531 
535 template<VisualEvent _vID>
538 {
539  return EventMapping::MappedType(new GEventWrapper<EventT(typename
541 }
542 
547 GetEvent(EventMapping::MapType&, const VisualEvent&,
549 
556 template<VisualEvent _vID>
558 FetchEvent(VisualEventMap& m)
559 {
560  return dynamic_cast<EventT(typename EventTypeMapping<_vID>::HandlerType)&>(
561  GetEvent(m, _vID, NewEvent<_vID>));
562 }
573 template<VisualEvent _vID>
574 EventT(typename EventTypeMapping<_vID>::HandlerType)&
575 FetchEvent(AController& controller)
576 {
577  return dynamic_cast<EventT(typename EventTypeMapping<_vID>::HandlerType)&>(
578  controller.GetItemRef(_vID, NewEvent<_vID>));
579 }
580 
582 
583 template<class _tWidget, VisualEvent _vID>
584 using GWidgetHandlerAdaptor = GHandlerAdaptor<_tWidget,
586 
587 template<VisualEvent _vID, class _tWidget, typename _fCallable>
589 MakeWidgetHandlerAdaptor(_tWidget& wgt, _fCallable&& f)
590 {
592 }
593 
594 template<VisualEvent _vID, class _tTarget, class _tWidget, typename _fCallable>
596 AddWidgetHandlerAdaptor(_tTarget&& target, _tWidget& wgt, _fCallable&& f)
597 {
598  return FetchEvent<_vID>(yforward(target))
600 }
602 
603 
609 {
610 public:
613 
617  explicit
618  WidgetController(bool = {});
619 
620  ImplI(AController) EventMapping::ItemType&
621  GetItem(const VisualEvent&);
622 
624  ImplI(AController) DefClone(const override, WidgetController)
625 };
626 
627 } // namespace UI;
628 
629 } // namespace YSLib;
630 
631 #endif
632 
KeyInput Keys
包含的按键代码。
Definition: ywgtevt.h:134
#define DefDeDtor(_t)
定义默认析构函数。
Definition: YBaseMacro.h:146
std::size_t KeyIndex
按键索引类型:标识特定的按键在按键集合中的位置。
Definition: Keys.h:42
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:172
GWidgetHandlerAdaptor< _tWidget, _vID > MakeWidgetHandlerAdaptor(_tWidget &wgt, _fCallable &&f)
Definition: ywgtevt.h:589
控制器抽象类。
Definition: ywgtevt.h:468
GEventPointerWrapper< UIEventArgs && > MappedType
映射项类型。
Definition: ywgtevt.h:423
按键输入事件参数类。
Definition: ywgtevt.h:167
yconstfn const string _tParams && args
Definition: Loader.h:111
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
部件绘制参数。
Definition: ywgtevt.h:276
#define YF_API
Definition: Platform.h:64
GIHEvent< UIEventArgs && > ItemType
Definition: ywgtevt.h:424
EventT(typename EventTypeMapping< _vID >::HandlerType)&FetchEvent(IWidget &wgt)
取部件事件。
Definition: ycontrol.h:131
YF_API EventMapping::ItemType & GetEvent(EventMapping::MapType &, const VisualEvent &, EventMapping::MappedType(&)())
在事件映射表中取指定 id 对应的事件。
可动态复制的抽象基类。
Definition: utility.hpp:142
u8 EventPriority
事件优先级。
Definition: yevt.hpp:204
文本输入事件参数。
Definition: ywgtevt.h:234
bool Handled
事件已经被处理。
Definition: ywgtevt.h:108
用户界面事件参数基类。
Definition: ywgtevt.h:59
yconstexpr EventPriority BoundaryPriority(0x60)
pair< VisualEvent, MappedType > PairType
Definition: ywgtevt.h:425
事件包装类模板。
Definition: yevt.hpp:772
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
#define ImplI(...)
Definition: YBaseMacro.h:308
IWidget * pSender
Definition: ywgtevt.h:62
map< VisualEvent, MappedType > MapType
映射表类型。
Definition: ywgtevt.h:426
pair< typename MapType::iterator, bool > SearchResult
搜索表结果类型。
Definition: ywgtevt.h:428
PDefH(void, Activate, Console &console, Drawing::Color fc=Drawing::ColorSpace::White) ImplExpr(Activate(console
激活:使用指定屏幕、有效性、前景色和默认背景色。
YSLib 标准字符串(使用 UCS-2 作为内部编码)。
Definition: ystring.h:47
GBinaryGroup< SPos > Point
屏幕二维点(直角坐标表示)。
Definition: ygdibase.h:235
yconstexpr EventPriority ForegroundPriority(0x40)
#define ImplExpr(...)
Definition: YBaseMacro.h:93
RoutingStrategy Strategy
事件路由策略。
Definition: ywgtevt.h:107
_tWidget & wgt
Definition: ywgtevt.h:596
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
GEventWrapper< EventT(HPaintEvent), UIEventArgs && > Paint
Definition: ywgtevt.h:612
事件项类型。
Definition: yevt.hpp:804
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
部件控制器。
Definition: ywgtevt.h:608
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
DeclDelegate(HScrollEvent, ScrollEventArgs) class YF_API ATrack
轨道。
Definition: scroll.h:118
#define DefSetter(_t, _n, _m)
Definition: YBaseMacro.h:188
滚轮事件参数。
Definition: ywgtevt.h:217
EventMapping::MapType VisualEventMap
Definition: ywgtevt.h:432
size_t DoEvent(AController &controller, const VisualEvent &id, typename EventArgsHead< typename _tEventHandler::TupleType >::type &&e)
Definition: ywgtevt.h:516
二维图形接口上下文。
Definition: ygdibase.h:721
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
_tWidget _fCallable && f
Definition: ywgtevt.h:597
简单 UI 事件参数类。
Definition: ywgtevt.h:252
yconstfn GValueEventArgs(IWidget &wgt, _tParams &&...args)
构造:使用部件引用和值。
Definition: ywgtevt.h:264
#define DefEventTypeMapping(_name, _tEventHandler)
#define DefClone(_q, _t)
动态复制。
Definition: YBaseMacro.h:221
错误或不存在的部件事件异常。
Definition: ywgtevt.h:440
const IWidget &wgt ImplRet(wgt.GetView().IsVisible()) bool YF_API Contains(const IWidget &
判断点是否在部件的可视区域内。
记录日志的异常事件类。
Definition: yexcept.h:58
GHandlerAdaptor< _tWidget, typename EventTypeMapping< _vID >::HandlerType::BaseType > GWidgetHandlerAdaptor
Definition: ywgtevt.h:585
RoutedEventArgs(IWidget &wgt, RoutingStrategy strategy=Direct)
Definition: ywgtevt.h:110
ptrdiff_t WheelDelta
滚轮度量:以角度计量的转动滚轮的幅度。
Definition: ywgtevt.h:210
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
BadEvent(const std::string &msg="")
Definition: ywgtevt.h:444
路由事件参数基类。
Definition: ywgtevt.h:93
RoutingStrategy
事件路由策略枚举。
Definition: ywgtevt.h:100
AController(bool b=true)
构造:使用指定可用性。
Definition: ywgtevt.h:477
bool enabled
控件可用性。
Definition: ywgtevt.h:471
#define DeclIEntry(_sig)
Definition: YBaseMacro.h:314
KeyInput InputType
输入类型。
Definition: ywgtevt.h:170
UIEventArgs(IWidget &wgt)
Definition: ywgtevt.h:66
EventMapping::MappedType NewEvent()
构造指针指向的 VisualEvent 指定的事件对象。
Definition: ywgtevt.h:537
指针设备输入事件参数类。
Definition: ywgtevt.h:183
std::bitset< KeyBitsetWidth > KeyInput
按键并行位宽。
Definition: Keys.h:68
输入事件参数类。
Definition: ywgtevt.h:121
用户事件信号:表示携带用户事件的消息处理中断的异常。
Definition: ywgtevt.h:455
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
_type ValueType
值类型。
Definition: ywgtevt.h:255
消息信号:表示单一处理中断的异常。
Definition: ymsg.h:288
std::function< bool(const KeyInput &, KeyIndex)> Tester
输入测试器:判断是否在指定索引上激活输入状态。
Definition: ydevice.h:95
yconstexpr EventPriority BackgroundPriority(0xC0)
用户界面绘制优先级。
UIEventSignal(const std::string &msg="")
Definition: ywgtevt.h:458
事件处理器适配器模板。
Definition: yevt.hpp:731