1 /**
2 Copyright: Copyright (c) 2014 Andrey Penechko.
3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module anchovy.gui.application.eventaggregator;
8 
9 import anchovy.core.interfaces.iwindow;
10 import anchovy.gui;
11 
12 import anchovy.gui.application.application;
13 
14 class EventAggregator(WindowType)
15 {
16 	Application!WindowType application;
17 	IWindow window;
18 	ivec2 pointerPosition;
19 
20 	this(Application!WindowType app, IWindow window)
21 	{
22 		this.application = app;
23 		this.window = window;
24 		window.keyPressed.connect(&keyPressed);
25 		window.keyReleased.connect(&keyReleased);
26 		window.charEntered.connect(&charEntered);
27 		window.windowResized.connect(&windowResized);
28 		window.mousePressed.connect(&mousePressed);
29 		window.mouseReleased.connect(&mouseReleased);
30 		window.mouseMoved.connect(&mouseMoved);
31 		window.closePressed.connect(&closePressed);
32 	}
33 
34 	void keyPressed(uint keyCode)
35 	{
36 		application.context.eventDispatcher.keyPressed(cast(KeyCode)keyCode, getCurrentKeyModifiers());
37 	}
38 
39 	void keyReleased(uint keyCode)
40 	{
41 		application.context.eventDispatcher.keyReleased(cast(KeyCode)keyCode, getCurrentKeyModifiers());
42 	}
43 
44 	void charEntered(dchar unicode)
45 	{
46 		application.context.eventDispatcher.charEntered(unicode);
47 	}
48 
49 	void mousePressed(uint mouseButton)
50 	{
51 		application.context.eventDispatcher.pointerPressed(window.mousePosition, cast(PointerButton)mouseButton);
52 	}
53 
54 	void mouseReleased(uint mouseButton)
55 	{
56 		application.context.eventDispatcher.pointerReleased(window.mousePosition, cast(PointerButton)mouseButton);
57 	}
58 
59 	void windowResized(uvec2 newSize)
60 	{
61 		window.reshape(newSize);
62 		application.context.size = cast(ivec2)newSize;
63 	}
64 
65 	void mouseMoved(ivec2 position)
66 	{
67 		ivec2 deltaPos = position - pointerPosition;
68 		pointerPosition = position;
69 		application.context.eventDispatcher.pointerMoved(position, deltaPos);
70 	}
71 
72 	uint getCurrentKeyModifiers()
73 	{
74 		uint modifiers;
75 
76 		if (window.isKeyPressed(KeyCode.KEY_LEFT_SHIFT) || window.isKeyPressed(KeyCode.KEY_RIGHT_SHIFT))
77 			modifiers |= KeyModifiers.SHIFT;
78 
79 		if (window.isKeyPressed(KeyCode.KEY_LEFT_CONTROL) || window.isKeyPressed(KeyCode.KEY_RIGHT_CONTROL))
80 			modifiers |= KeyModifiers.CONTROL;
81 
82 		if (window.isKeyPressed(KeyCode.KEY_LEFT_ALT) || window.isKeyPressed(KeyCode.KEY_RIGHT_ALT))
83 			modifiers |= KeyModifiers.ALT;
84 
85 		return modifiers;
86 	}
87 
88 	void closePressed()
89 	{
90 		application.closePressed();
91 	}
92 }