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.gui.plugin;
8 
9 import voxelman.log;
10 import std..string : format;
11 import voxelman.math;
12 
13 import voxelman.platform.iwindow;
14 import voxelman.platform.input;
15 import voxelman.gui;
16 import voxelman.graphics;
17 import voxelman.graphics.plugin;
18 import voxelman.text.linebuffer;
19 
20 import pluginlib;
21 
22 import voxelman.core.config;
23 import voxelman.core.events;
24 import voxelman.world.storage;
25 
26 import voxelman.dbg.plugin;
27 import voxelman.eventdispatcher.plugin;
28 import voxelman.input.keybindingmanager;
29 
30 struct ClosePressedEvent {}
31 
32 
33 final class GuiPlugin : IPlugin
34 {
35 private:
36 	EventDispatcherPlugin evDispatcher;
37 	GraphicsPlugin graphics;
38 
39 public:
40 	GuiContext guictx;
41 	IWindow window;
42 	bool mouseLocked;
43 	bool isGuiDebuggerShown;
44 
45 	mixin IdAndSemverFrom!"voxelman.gui.plugininfo";
46 
47 	override void registerResources(IResourceManagerRegistry resmanRegistry)
48 	{
49 		auto keyBindingMan = resmanRegistry.getResourceManager!KeyBindingManager;
50 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_Q, "key.lockMouse", null, &onLockMouse));
51 		keyBindingMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_F9, "key.toggle_gui_dbg", null, (s){isGuiDebuggerShown.toggle_bool;}));
52 		auto res = resmanRegistry.getResourceManager!GraphicsResources;
53 		guictx = res.guictx;
54 	}
55 
56 	override void init(IPluginManager pluginman)
57 	{
58 		evDispatcher = pluginman.getPlugin!EventDispatcherPlugin;
59 		evDispatcher.subscribeToEvent(&onPreUpdateEvent);
60 
61 		auto debugClient = pluginman.getPlugin!DebugClient;
62 		debugClient.registerDebugGuiHandler(&showDebugSettings, SETTINGS_ORDER, "Q Lock mouse");
63 
64 		graphics = pluginman.getPlugin!GraphicsPlugin;
65 		window = graphics.window;
66 
67 		auto debugger_frame = createGuiDebugger(guictx.getRoot(1));
68 		debugger_frame.visible_if(() => isGuiDebuggerShown);
69 	}
70 
71 	private void showDebugSettings()
72 	{
73 		//igCheckbox("[Q] Lock mouse", &mouseLocked);
74 		updateMouseLock();
75 	}
76 
77 	private void onPreUpdateEvent(ref PreUpdateEvent event)
78 	{
79 		updateMouseLock();
80 	}
81 
82 	private void onLockMouse(string)
83 	{
84 		mouseLocked = !mouseLocked;
85 		updateMouseLock();
86 	}
87 
88 	private void updateMouseLock()
89 	{
90 		if (window.isCursorLocked != mouseLocked)
91 		{
92 			window.isCursorLocked = mouseLocked;
93 			if (mouseLocked)
94 				window.mousePosition = cast(ivec2)(window.size) / 2;
95 		}
96 	}
97 }