1 /**
2 Copyright: Copyright (c) 2015-2017 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module voxelman.input.plugin;
8 
9 import voxelman.log;
10 import voxelman.math;
11 
12 import pluginlib;
13 import voxelman.gui.plugin;
14 import voxelman.input.keybindingmanager;
15 
16 
17 final class InputPlugin : IPlugin
18 {
19 	GuiPlugin guiPlugin;
20 	KeyBindingManager keyBindingsMan;
21 
22 	mixin IdAndSemverFrom!"voxelman.input.plugininfo";
23 
24 	override void registerResourceManagers(void delegate(IResourceManager) registerRM)
25 	{
26 		registerRM(new KeyBindingManager);
27 	}
28 
29 	override void registerResources(IResourceManagerRegistry resmanRegistry)
30 	{
31 		keyBindingsMan = resmanRegistry.getResourceManager!KeyBindingManager;
32 	}
33 
34 	override void init(IPluginManager pluginman)
35 	{
36 		guiPlugin = pluginman.getPlugin!GuiPlugin;
37 	}
38 
39 	override void postInit()
40 	{
41 		guiPlugin.window.keyPressed.connect(&onKeyPressed);
42 		guiPlugin.window.keyReleased.connect(&onKeyReleased);
43 		guiPlugin.window.mousePressed.connect(&onMousePressed);
44 		guiPlugin.window.mouseReleased.connect(&onMouseReleased);
45 	}
46 
47 	void onKeyPressed(uint keyCode)
48 	{
49 		if (guiPlugin.igState.keyboardCaptured) return;
50 		if (auto binding = keyCode in keyBindingsMan.keyBindingsByCode)
51 		{
52 			KeyBinding* b = *binding;
53 			if (b.pressHandler)
54 				b.pressHandler(b.keyName);
55 		}
56 	}
57 
58 	void onKeyReleased(uint keyCode)
59 	{
60 		if (guiPlugin.igState.keyboardCaptured) return;
61 		if (auto binding = keyCode in keyBindingsMan.keyBindingsByCode)
62 		{
63 			KeyBinding* b = *binding;
64 			if (b.releaseHandler)
65 				b.releaseHandler(b.keyName);
66 		}
67 	}
68 
69 	void onMousePressed(uint keyCode)
70 	{
71 		if (guiPlugin.igState.mouseCaptured) return;
72 		if (auto binding = keyCode in keyBindingsMan.keyBindingsByCode)
73 		{
74 			KeyBinding* b = *binding;
75 			if (b.pressHandler)
76 				b.pressHandler(b.keyName);
77 		}
78 	}
79 
80 	void onMouseReleased(uint keyCode)
81 	{
82 		if (guiPlugin.igState.mouseCaptured) return;
83 		if (auto binding = keyCode in keyBindingsMan.keyBindingsByCode)
84 		{
85 			KeyBinding* b = *binding;
86 			if (b.releaseHandler)
87 				b.releaseHandler(b.keyName);
88 		}
89 	}
90 
91 	bool isKeyPressed(string keyName)
92 	{
93 		if (guiPlugin.igState.keyboardCaptured) return false;
94 		if (auto binding = keyName in keyBindingsMan.keyBindingsByName)
95 		{
96 			KeyBinding* b = *binding;
97 			return guiPlugin.window.isKeyPressed(b.keyCode);
98 		}
99 		else
100 			return false;
101 	}
102 
103 	ivec2 mousePosition() @property
104 	{
105 		return guiPlugin.window.mousePosition;
106 	}
107 
108 	ivec2 mousePosition(ivec2 newMousePosition) @property
109 	{
110 		guiPlugin.window.mousePosition = newMousePosition;
111 		return guiPlugin.window.mousePosition;
112 	}
113 }