1 /**
2 Copyright: Copyright (c) 2015-2018 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 import voxelman.dbg.plugin;
16 
17 
18 final class InputPlugin : IPlugin
19 {
20 	GuiPlugin guiPlugin;
21 	KeyBindingManager keyBindingsMan;
22 
23 	mixin IdAndSemverFrom!"voxelman.input.plugininfo";
24 
25 	override void registerResourceManagers(void delegate(IResourceManager) registerRM)
26 	{
27 		registerRM(new KeyBindingManager);
28 	}
29 
30 	override void registerResources(IResourceManagerRegistry resmanRegistry)
31 	{
32 		keyBindingsMan = resmanRegistry.getResourceManager!KeyBindingManager;
33 	}
34 
35 	override void init(IPluginManager pluginman)
36 	{
37 		guiPlugin = pluginman.getPlugin!GuiPlugin;
38 
39 		auto debugClient = pluginman.getPlugin!DebugClient;
40 		debugClient.registerDebugGuiHandler(&showDebugInput, DEBUG_ORDER, "Input");
41 	}
42 
43 	override void postInit()
44 	{
45 		guiPlugin.window.keyPressed.connect(&onKeyPressed);
46 		guiPlugin.window.keyReleased.connect(&onKeyReleased);
47 		guiPlugin.window.mousePressed.connect(&onMousePressed);
48 		guiPlugin.window.mouseReleased.connect(&onMouseReleased);
49 	}
50 
51 	void onKeyPressed(KeyCode keyCode, uint modifiers)
52 	{
53 		if (auto binding = keyCode in keyBindingsMan.keyBindingsByCode)
54 		{
55 			KeyBinding* b = *binding;
56 			if (guiPlugin.guictx.focusedWidget && !b.fireBeforeInput) return;
57 
58 			if (b.pressHandler)
59 				b.pressHandler(b.keyName);
60 		}
61 	}
62 
63 	void onKeyReleased(KeyCode keyCode, uint modifiers)
64 	{
65 		if (auto binding = keyCode in keyBindingsMan.keyBindingsByCode)
66 		{
67 			KeyBinding* b = *binding;
68 			if (guiPlugin.guictx.focusedWidget && !b.fireBeforeInput) return;
69 			if (b.releaseHandler)
70 				b.releaseHandler(b.keyName);
71 		}
72 	}
73 
74 	void onMousePressed(PointerButton button, uint modifiers)
75 	{
76 		if (auto binding = button in keyBindingsMan.keyBindingsByCode)
77 		{
78 			KeyBinding* b = *binding;
79 			if (guiPlugin.guictx.focusedWidget && !b.fireBeforeInput) return;
80 			if (b.pressHandler)
81 				b.pressHandler(b.keyName);
82 		}
83 	}
84 
85 	void onMouseReleased(PointerButton button, uint modifiers)
86 	{
87 		if (auto binding = button in keyBindingsMan.keyBindingsByCode)
88 		{
89 			KeyBinding* b = *binding;
90 			if (guiPlugin.guictx.focusedWidget && !b.fireBeforeInput) return;
91 			if (b.releaseHandler)
92 				b.releaseHandler(b.keyName);
93 		}
94 	}
95 
96 	bool isKeyPressed(string keyName)
97 	{
98 		if (guiPlugin.guictx.focusedWidget) 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 
119 	private void showDebugInput()
120 	{/*
121 		import derelict.glfw3.glfw3;
122 		import voxelman.text.textformatter;
123 		import std.string : fromStringz;
124 
125 		if (igCollapsingHeader("Input"))
126 		{
127 			if (igTreeNode("Joystick"))
128 			{
129 				foreach(joy; GLFW_JOYSTICK_1..GLFW_JOYSTICK_LAST+1)
130 				{
131 					if (glfwJoystickPresent(joy))
132 					{
133 						const char* name = glfwGetJoystickName(joy);
134 						auto strName = makeFormattedTextPtrs("Joystick %s: %s", joy, fromStringz(name));
135 						if (igTreeNode(strName.start))
136 						{
137 							int count;
138 
139 							const float* axes = glfwGetJoystickAxes(joy, &count);
140 							auto strAxes = makeFormattedTextPtrs("Axes: %s", count);
141 							if (igTreeNode(strAxes.start))
142 							{
143 								foreach(i, axis; axes[0..count])
144 								{
145 									igTextf("%s: %.2f", i, axis);
146 								}
147 								igTreePop();
148 							}
149 
150 							ubyte* buttons = glfwGetJoystickButtons(joy, &count);
151 							auto strButtons = makeFormattedTextPtrs("Buttons: %s", count);
152 							if (igTreeNode(strButtons.start))
153 							{
154 								foreach(i, button; buttons[0..count])
155 								{
156 									igTextf("%s: %s", i, button);
157 								}
158 								igTreePop();
159 							}
160 							igTreePop();
161 						}
162 					}
163 				}
164 				igTreePop();
165 			}
166 		}*/
167 	}
168 }