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