YSTest: ywgtevt.h 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ywgtevt.h
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2010 - 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 #ifndef YSL_INC_UI_ywgtevt_h_
29 #define YSL_INC_UI_ywgtevt_h_ 1
30
31 #include "ycomp.h"
32 #include "../Core/yevt.hpp"
33
34 YSL_BEGIN
35
36 YSL_BEGIN_NAMESPACE(UI)
37
38
43 yconstexpr EventPriority BackgroundPriority(0xC0);
44 yconstexpr EventPriority BoundaryPriority(0x60);
45 yconstexpr EventPriority ForegroundPriority(0x40);
47
48
54 struct YF_API UIEventArgs
55 {
56 private:
57 IWidget* pSender;
58
59 public:
60 explicit
61 UIEventArgs(IWidget& wgt)
62 : pSender(&wgt)
63 {}
68 inline DefDeCopyCtor(UIEventArgs)
69
70
74 inline DefDeCopyAssignment(UIEventArgs)
75
76 DefGetter(const ynothrow, IWidget&, Sender, *pSender)
77 PDefH(void, SetSender, IWidget& wgt)
78 ImplExpr(pSender = &wgt)
79 };
80
81
87 struct YF_API RoutedEventArgs : public UIEventArgs
88 {
89 public:
91 typedef enum
92 {
93 Bubble = 0,
94 Tunnel = 1,
95 Direct = 2
96 } RoutingStrategy;
97
98 RoutingStrategy Strategy;
99 bool Handled;
100
101 RoutedEventArgs(IWidget& wgt, RoutingStrategy strategy = Direct)
102 : UIEventArgs(wgt),
103 Strategy(strategy), Handled(false)
104 {}
105 };
106
107
113 typedef Drawing::Point MScreenPositionEventArgs;
114
115
121 struct YF_API InputEventArgs : public RoutedEventArgs
122 {
123 public:
128 KeyInput Keys;
129
134 InputEventArgs(IWidget&, const KeyInput& = {}, RoutingStrategy = Direct);
135
140 DefCvt(const ynothrow, const KeyInput&, Keys)
141
142
146 DefGetter(const ynothrow, const KeyInput&, Keys, Keys)
147 };
148
149
155 struct YF_API KeyEventArgs : public InputEventArgs
156 {
157 public:
158 typedef KeyInput InputType;
159
163 KeyEventArgs(IWidget&, const InputType& = {}, RoutingStrategy = Direct);
164 };
165
166
172 struct YF_API TouchEventArgs : public InputEventArgs,
173 public MScreenPositionEventArgs
174 {
175 public:
176 typedef Drawing::Point InputType;
177
182 TouchEventArgs(IWidget&, const KeyInput&, const InputType& = {},
183 RoutingStrategy = Direct);
184 };
185
186
194 template<typename _type>
195 struct GValueEventArgs : public UIEventArgs
196 {
198 typedef _type ValueType;
199
200 ValueType Value;
201
205 template<typename... _tParams>
206 yconstfn
207 GValueEventArgs(IWidget& wgt, _tParams&&... args)
208 : UIEventArgs(wgt),
209 Value(yforward(args)...)
210 {}
211 DefCvt(const ynothrow, ValueType, Value)
212 };
213
214
220 struct YF_API PaintEventArgs : public UIEventArgs, public PaintContext
221 {
222 PaintEventArgs(IWidget&);
223 PaintEventArgs(IWidget&, const PaintContext&);
224 PaintEventArgs(IWidget&, const Drawing::Graphics&, const Drawing::Point&,
225 const Drawing::Rect&);
226 };
227
228
229 //事件处理器类型。
230 DeclDelegate(HUIEvent, UIEventArgs&&)
231 DeclDelegate(HInputEvent, InputEventArgs&&)
232 DeclDelegate(HKeyEvent, KeyEventArgs&&)
233 DeclDelegate(HTouchEvent, TouchEventArgs&&)
234 DeclDelegate(HPaintEvent, PaintEventArgs&&)
235 //DefDelegate(HPointEvent, Drawing::Point&&)
236 //DefDelegate(HSizeEvent, Size&&)
237
238
239 #define DefEventTypeMapping(_name, _tEventHandler) \
240 template<> \
241 struct EventTypeMapping<_name> \
242 { \
243 typedef _tEventHandler HandlerType; \
244 };
245
246
251 typedef enum
252 {
253 // AutoSizeChanged,
254 // BackColorChanged,
255 // ForeColorChanged,
256 // LocationChanged,
257 // MarginChanged,
258 // VisibleChanged,
259
260 // EnabledChanged,
261
262 //视图变更事件。
263 Move,
264 Resize,
265
266 //图形用户界面输入事件。
267 KeyUp,
268 KeyDown,
269 KeyHeld,
270 KeyPress,
271 TouchUp,
272 TouchDown,
273 TouchHeld,
274 TouchMove,
275 Click,
276
277 //图形用户界面输出事件。
278 Paint,
279
280 //焦点事件。
281 GotFocus,
282 LostFocus,
283
284 //边界事件。
285 Enter,
286 Leave
287 // TextChanged,
288 // FontChanged,
289 // FontColorChanged,
290 // FontSizeChanged,
291 } VisualEvent;
292
293
294 template<VisualEvent>
295 struct EventTypeMapping
296 {
297 //定义 HandlerType 的默认值可能会导致运行期 dynamic_cast 失败。
298 // typedef HEvent HandlerType;
299 };
300
301 DefEventTypeMapping(Move, HUIEvent)
302 DefEventTypeMapping(Resize, HUIEvent)
303
304 DefEventTypeMapping(KeyUp, HKeyEvent)
305 DefEventTypeMapping(KeyDown, HKeyEvent)
306 DefEventTypeMapping(KeyHeld, HKeyEvent)
307 DefEventTypeMapping(KeyPress, HKeyEvent)
308 DefEventTypeMapping(TouchUp, HTouchEvent)
309 DefEventTypeMapping(TouchDown, HTouchEvent)
310 DefEventTypeMapping(TouchHeld, HTouchEvent)
311 DefEventTypeMapping(TouchMove, HTouchEvent)
312 DefEventTypeMapping(Click, HTouchEvent)
313
314 DefEventTypeMapping(Paint, HPaintEvent)
315
316 DefEventTypeMapping(GotFocus, HUIEvent)
317 DefEventTypeMapping(LostFocus, HUIEvent)
318
319 DefEventTypeMapping(Enter, HTouchEvent)
320 DefEventTypeMapping(Leave, HTouchEvent)
321
322
327 YSL_BEGIN_NAMESPACE(EventMapping)
328
329 typedef GEventPointerWrapper<UIEventArgs&&> MappedType;
330 typedef GIHEvent<UIEventArgs&&> ItemType;
331 typedef pair<VisualEvent, MappedType> PairType;
332 typedef map<VisualEvent, MappedType> MapType;
333 typedef pair<typename MapType::iterator, bool> SearchResult; \
335
336 YSL_END_NAMESPACE(EventMapping)
337
338 typedef EventMapping::MapType VisualEventMap;
339
340
342 struct YF_API BadEvent
343 {};
344
345
350 class YF_API AController
351 {
352 private:
353 bool enabled;
354
355 public:
359 AController(bool b = true)
360 : enabled(b)
361 {}
366 virtual DefEmptyDtor(AController)
367
368 DefPred(const ynothrow, Enabled, enabled)
369
374 DeclIEntry(EventMapping::ItemType& GetItem(const VisualEvent&))
379 virtual EventMapping::ItemType&
380 GetItemRef(const VisualEvent& id, EventMapping::MappedType(&)())
381 {
382 return GetItem(id);
383 }
384
385 DefSetter(bool, Enabled, enabled)
386
387 /*
388 \brief 复制实例。
389 \since build 350
390 */
391 DeclIEntry(AController* Clone() const)
392 };
393
394
395 template<class _tEventHandler>
396 size_t
397 DoEvent(AController& controller, const VisualEvent& id,
398 typename _tEventHandler::EventArgsType&& e)
399 {
400 try
401 {
402 return dynamic_cast< EventT(typename _tEventHandler)&>(
403 controller.GetItem(id))(std::move(e));
404 }
405 catch(std::out_of_range&)
406 {}
407 catch(std::bad_cast&)
408 {}
409 return 0;
410 }
411 template<class _tEventHandler>
412 inline size_t
413 DoEvent(AController& controller, const VisualEvent& id,
414 typename _tEventHandler::EventArgsType& e)
415 {
416 return DoEvent<_tEventHandler>(controller, id, std::move(e));
417 }
418
419
424 class YF_API WidgetController : public AController
425 {
426 public:
428 GEventWrapper<EventT(HPaintEvent), UIEventArgs&&> Paint;
429
433 explicit
434 WidgetController(bool = false);
435
436 ImplI(AController) EventMapping::ItemType&
437 GetItem(const VisualEvent&);
438
439 ImplI(AController) DefClone(const override, WidgetController, Clone)
440 };
441
442 YSL_END_NAMESPACE(UI)
443
444 YSL_END
445
446 #endif
447