YSTest: ywgtevt.h 源文件

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
49 yconstexpr EventPriority BackgroundPriority(0xC0);
50 yconstexpr EventPriority BoundaryPriority(0x60);
51 yconstexpr EventPriority ForegroundPriority(0x40);
53
54
59 struct YF_API UIEventArgs
60 {
61 private:
62 IWidget* pSender;
63
64 public:
65 explicit
66 UIEventArgs(IWidget& wgt)
67 : pSender(&wgt)
68 {}
73 DefDeCopyCtor(UIEventArgs)
75 virtual DefDeDtor(UIEventArgs)
76
81 DefDeCopyAssignment(UIEventArgs)
82
83 DefGetter(const ynothrow, IWidget&, Sender, *pSender)
84 PDefH(void, SetSender, IWidget& wgt)
85 ImplExpr(pSender = &wgt)
86 };
87
88
93 struct YF_API RoutedEventArgs : public UIEventArgs
94 {
95 public:
100 enum RoutingStrategy
101 {
102 Bubble = 0,
103 Tunnel = 1,
104 Direct = 2
105 };
106
107 RoutingStrategy Strategy;
108 bool Handled;
109
110 RoutedEventArgs(IWidget& wgt, RoutingStrategy strategy = Direct)
111 : UIEventArgs(wgt),
112 Strategy(strategy), Handled(false)
113 {}
114 };
115
116
121 struct YF_API InputEventArgs : public RoutedEventArgs
122 {
123 public:
128 Devices::KeyInputDevice::Tester
129 Tester{Devices::KeyInputDevice::DefaultTest};
134 KeyInput Keys;
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
167 struct YF_API KeyEventArgs : public InputEventArgs
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:
186 using InputType = Drawing::Point;
187
189 InputType Position;
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
217 class YF_API CursorWheelEventArgs : public CursorEventArgs
218 {
219 private:
220 WheelDelta delta;
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:
237 String Text;
238
239 TextInputEventArgs(IWidget&, String, const KeyInput& = {},
240 RoutingStrategy = Direct);
241 TextInputEventArgs(InputEventArgs, String);
242 };
243
244
251 template<typename _type>
252 struct GValueEventArgs : public UIEventArgs
253 {
255 using ValueType = _type;
256
257 ValueType Value;
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>
381 struct EventTypeMapping
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
423 using MappedType = GEventPointerWrapper<UIEventArgs&&>;
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
432 using VisualEventMap = EventMapping::MapType;
433
434
440 class YF_API BadEvent : public LoggedEvent
441 {
442 public:
444 BadEvent(const std::string& msg = "")
445 : LoggedEvent(msg)
446 {}
447 };
448
449
455 class YF_API UIEventSignal : public Messaging::MessageSignal
456 {
457 public:
458 UIEventSignal(const std::string& msg = "")
459 : MessageSignal(msg)
460 {}
461 };
462
463
468 class YF_API AController : public ystdex::cloneable
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>
536 EventMapping::MappedType
537 NewEvent()
538 {
539 return EventMapping::MappedType(new GEventWrapper<EventT(typename
540 EventTypeMapping<_vID>::HandlerType), UIEventArgs&&>());
541 }
542
546 YF_API EventMapping::ItemType&
547 GetEvent(EventMapping::MapType&, const VisualEvent&,
548 EventMapping::MappedType(&)());
549
556 template<VisualEvent _vID>
557 EventT(typename EventTypeMapping<_vID>::HandlerType)&
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,
585 typename EventTypeMapping<_vID>::HandlerType::BaseType>;
586
587 template<VisualEvent _vID, class _tWidget, typename _fCallable>
588 GWidgetHandlerAdaptor<_tWidget, _vID>
589 MakeWidgetHandlerAdaptor(_tWidget& wgt, _fCallable&& f)
590 {
591 return GWidgetHandlerAdaptor<_tWidget, _vID>(wgt, yforward(f));
592 }
593
594 template<VisualEvent _vID, class _tTarget, class _tWidget, typename _fCallable>
595 EventT(typename EventTypeMapping<_vID>::HandlerType)&
596 AddWidgetHandlerAdaptor(_tTarget&& target, _tWidget& wgt, _fCallable&& f)
597 {
598 return FetchEvent<_vID>(yforward(target))
599 += GWidgetHandlerAdaptor<_tWidget, _vID>(wgt, yforward(f));
600 }
602
603
608 class YF_API WidgetController : public AController
609 {
610 public:
612 GEventWrapper<EventT(HPaintEvent), UIEventArgs&&> Paint;
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
YSLib::UI::InputEventArgs::Keys
KeyInput Keys
包含的按键代码。
Definition: ywgtevt.h:134
YModules.h
DefDeDtor
#define DefDeDtor(_t)
定义默认析构函数。
Definition: YBaseMacro.h:146
platform::KeyIndex
std::size_t KeyIndex
按键索引类型：标识特定的按键在按键集合中的位置。
Definition: Keys.h:42
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:172
YSLib::UI::MakeWidgetHandlerAdaptor
GWidgetHandlerAdaptor< _tWidget, _vID > MakeWidgetHandlerAdaptor(_tWidget &wgt, _fCallable &&f)
Definition: ywgtevt.h:589
YSLib::UI::AController
控制器抽象类。
Definition: ywgtevt.h:468
YSLib::UI::EventMapping::MappedType
GEventPointerWrapper< UIEventArgs && > MappedType
映射项类型。
Definition: ywgtevt.h:423
YSLib::UI::GValueEventArgs::Value
ValueType Value
Definition: ywgtevt.h:257
YSLib::UI::KeyEventArgs
按键输入事件参数类。
Definition: ywgtevt.h:167
YSLib::UI::args
yconstfn const string _tParams && args
Definition: Loader.h:111
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
YSLib::UI::PaintEventArgs
部件绘制参数。
Definition: ywgtevt.h:276
YF_API
#define YF_API
Definition: Platform.h:64
YSLib::UI::EventMapping::ItemType
GIHEvent< UIEventArgs && > ItemType
Definition: ywgtevt.h:424
YSLib::UI::EventT
EventT(typename EventTypeMapping< _vID >::HandlerType)&FetchEvent(IWidget &wgt)
取部件事件。
Definition: ycontrol.h:131
YSLib::UI::GetEvent
YF_API EventMapping::ItemType & GetEvent(EventMapping::MapType &, const VisualEvent &, EventMapping::MappedType(&)())
在事件映射表中取指定 id 对应的事件。
YSLib::EventArgsHead
Definition: yevt.hpp:649
ystdex::cloneable
可动态复制的抽象基类。
Definition: utility.hpp:142
YSLib::EventPriority
u8 EventPriority
事件优先级。
Definition: yevt.hpp:204
YSLib::UI::TextInputEventArgs
文本输入事件参数。
Definition: ywgtevt.h:234
YSLib::UI::RoutedEventArgs::Handled
bool Handled
事件已经被处理。
Definition: ywgtevt.h:108
YSLib::UI::UIEventArgs
用户界面事件参数基类。
Definition: ywgtevt.h:59
YSLib::UI::BoundaryPriority
yconstexpr EventPriority BoundaryPriority(0x60)
YSLib::UI::EventMapping::PairType
pair< VisualEvent, MappedType > PairType
Definition: ywgtevt.h:425
YSLib::GEventWrapper
事件包装类模板。
Definition: yevt.hpp:772
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
ImplI
#define ImplI(...)
Definition: YBaseMacro.h:308
YSLib::UI::UIEventArgs::pSender
IWidget * pSender
Definition: ywgtevt.h:62
YSLib::UI::EventMapping::MapType
map< VisualEvent, MappedType > MapType
映射表类型。
Definition: ywgtevt.h:426
YSLib::UI::EventMapping::SearchResult
pair< typename MapType::iterator, bool > SearchResult
搜索表结果类型。
Definition: ywgtevt.h:428
YSLib::UI::TextInputEventArgs::Text
String Text
Definition: ywgtevt.h:237
YSLib::UI::CursorWheelEventArgs::delta
WheelDelta delta
Definition: ywgtevt.h:220
YSLib::UI::PDefH
PDefH(void, Activate, Console &console, Drawing::Color fc=Drawing::ColorSpace::White) ImplExpr(Activate(console
激活：使用指定屏幕、有效性、前景色和默认背景色。
YSLib::Text::String
YSLib 标准字符串（使用 UCS-2 作为内部编码）。
Definition: ystring.h:47
YSLib::Drawing::Point
GBinaryGroup< SPos > Point
屏幕二维点（直角坐标表示）。
Definition: ygdibase.h:235
YSLib::UI::ForegroundPriority
yconstexpr EventPriority ForegroundPriority(0x40)
ImplExpr
#define ImplExpr(...)
Definition: YBaseMacro.h:93
YSLib::UI::RoutedEventArgs::Strategy
RoutingStrategy Strategy
事件路由策略。
Definition: ywgtevt.h:107
YSLib::UI::wgt
_tWidget & wgt
Definition: ywgtevt.h:596
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::UI::WidgetController::Paint
GEventWrapper< EventT(HPaintEvent), UIEventArgs && > Paint
Definition: ywgtevt.h:612
YSLib::GEventPointerWrapper
事件项类型。
Definition: yevt.hpp:804
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: ygdibase.h:416
YSLib::UI::WidgetController
部件控制器。
Definition: ywgtevt.h:608
YSLib::string
GSStringTemplate< char >::basic_string string
Definition: ycont.h:164
YSLib::UI::DeclDelegate
DeclDelegate(HScrollEvent, ScrollEventArgs) class YF_API ATrack
轨道。
Definition: scroll.h:118
DefSetter
#define DefSetter(_t, _n, _m)
Definition: YBaseMacro.h:188
YSLib::Drawing::GBinaryGroup< SPos >
YSLib::UI::CursorWheelEventArgs
滚轮事件参数。
Definition: ywgtevt.h:217
YSLib::UI::VisualEventMap
EventMapping::MapType VisualEventMap
Definition: ywgtevt.h:432
YSLib::UI::CursorEventArgs::Position
InputType Position
Definition: ywgtevt.h:189
YSLib::UI::DoEvent
size_t DoEvent(AController &controller, const VisualEvent &id, typename EventArgsHead< typename _tEventHandler::TupleType >::type &&e)
Definition: ywgtevt.h:516
YSLib::Drawing::Graphics
二维图形接口上下文。
Definition: ygdibase.h:721
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
YSLib::UI::f
_tWidget _fCallable && f
Definition: ywgtevt.h:597
YSLib::UI::GValueEventArgs
简单 UI 事件参数类。
Definition: ywgtevt.h:252
YSLib::UI::GValueEventArgs::GValueEventArgs
yconstfn GValueEventArgs(IWidget &wgt, _tParams &&...args)
构造：使用部件引用和值。
Definition: ywgtevt.h:264
DefEventTypeMapping
#define DefEventTypeMapping(_name, _tEventHandler)
YSLib::UI::EventTypeMapping
Definition: ywgtevt.h:381
DefClone
#define DefClone(_q, _t)
动态复制。
Definition: YBaseMacro.h:221
YSLib::UI::BadEvent
错误或不存在的部件事件异常。
Definition: ywgtevt.h:440
YSLib::UI::ImplRet
const IWidget &wgt ImplRet(wgt.GetView().IsVisible()) bool YF_API Contains(const IWidget &
判断点是否在部件的可视区域内。
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: yexcept.h:58
YSLib::UI::GWidgetHandlerAdaptor
GHandlerAdaptor< _tWidget, typename EventTypeMapping< _vID >::HandlerType::BaseType > GWidgetHandlerAdaptor
Definition: ywgtevt.h:585
YSLib::UI::RoutedEventArgs::RoutedEventArgs
RoutedEventArgs(IWidget &wgt, RoutingStrategy strategy=Direct)
Definition: ywgtevt.h:110
YSLib::UI::WheelDelta
ptrdiff_t WheelDelta
滚轮度量：以角度计量的转动滚轮的幅度。
Definition: ywgtevt.h:210
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:462
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
YSLib::UI::BadEvent::BadEvent
BadEvent(const std::string &msg="")
Definition: ywgtevt.h:444
YSLib::UI::RoutedEventArgs
路由事件参数基类。
Definition: ywgtevt.h:93
YSLib::UI::RoutedEventArgs::RoutingStrategy
RoutingStrategy
事件路由策略枚举。
Definition: ywgtevt.h:100
YSLib::UI::AController::AController
AController(bool b=true)
构造：使用指定可用性。
Definition: ywgtevt.h:477
YSLib::UI::AController::enabled
bool enabled
控件可用性。
Definition: ywgtevt.h:471
DeclIEntry
#define DeclIEntry(_sig)
Definition: YBaseMacro.h:314
YSLib::UI::KeyEventArgs::InputType
KeyInput InputType
输入类型。
Definition: ywgtevt.h:170
YSLib::UI::UIEventArgs::UIEventArgs
UIEventArgs(IWidget &wgt)
Definition: ywgtevt.h:66
ystdex::any_ops::clone
Definition: any.h:310
YSLib::UI::NewEvent
EventMapping::MappedType NewEvent()
构造指针指向的 VisualEvent 指定的事件对象。
Definition: ywgtevt.h:537
YSLib::UI::CursorEventArgs
指针设备输入事件参数类。
Definition: ywgtevt.h:183
platform::KeyInput
std::bitset< KeyBitsetWidth > KeyInput
按键并行位宽。
Definition: Keys.h:68
YSLib::UI::InputEventArgs
输入事件参数类。
Definition: ywgtevt.h:121
YSLib::UI::UIEventSignal
用户事件信号：表示携带用户事件的消息处理中断的异常。
Definition: ywgtevt.h:455
DefDeCopyAssignment
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
YSLib::UI::GValueEventArgs::ValueType
_type ValueType
值类型。
Definition: ywgtevt.h:255
YSLib::Messaging::MessageSignal
消息信号：表示单一处理中断的异常。
Definition: ymsg.h:288
YSLib::Devices::KeyInputDevice::Tester
std::function< bool(const KeyInput &, KeyIndex)> Tester
输入测试器：判断是否在指定索引上激活输入状态。
Definition: ydevice.h:95
YSLib::UI::BackgroundPriority
yconstexpr EventPriority BackgroundPriority(0xC0)
用户界面绘制优先级。
YSLib::UI::UIEventSignal::UIEventSignal
UIEventSignal(const std::string &msg="")
Definition: ywgtevt.h:458
YSLib::GHandlerAdaptor
事件处理器适配器模板。
Definition: yevt.hpp:731