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.edit.plugin;
8 
9 import std.experimental.logger;
10 
11 import pluginlib;
12 import voxelman.core.events;
13 import derelict.imgui.imgui;
14 import dlib.math.utils;
15 import voxelman.utils.textformatter;
16 
17 import voxelman.client.plugin;
18 import voxelman.worldinteraction.plugin;
19 import voxelman.graphics.plugin;
20 import voxelman.net.plugin;
21 import voxelman.eventdispatcher.plugin;
22 import voxelman.input.keybindingmanager;
23 
24 import voxelman.edit.tools.filltool;
25 
26 shared static this()
27 {
28 	pluginRegistry.regClientPlugin(new EditPlugin);
29 }
30 
31 abstract class ITool
32 {
33 	string name;
34 	size_t id;
35 	void onUpdate() {}
36 	void onMainActionPress() {}
37 	void onMainActionRelease() {}
38 	void onSecondaryActionPress() {}
39 	void onSecondaryActionRelease() {}
40 	void onTertiaryActionPress() {}
41 	void onTertiaryActionRelease() {}
42 	void onRotateAction() {}
43 }
44 
45 final class NullTool : ITool
46 {
47 	this() { name = "voxelman.edit.null_tool"; }
48 }
49 
50 import voxelman.utils.mapping;
51 class EditPlugin : IPlugin
52 {
53 	mixin IdAndSemverFrom!(voxelman.edit.plugininfo);
54 
55 	size_t selectedTool;
56 	ClientPlugin clientPlugin;
57 	Mapping!ITool tools;
58 	NullTool nullTool;
59 	FillTool fillTool;
60 
61 	override void registerResources(IResourceManagerRegistry resmanRegistry)
62 	{
63 		auto keyBindingsMan = resmanRegistry.getResourceManager!KeyBindingManager;
64 		keyBindingsMan.registerKeyBinding(new KeyBinding(PointerButton.PB_1, "key.mainAction", &onMainActionPress, &onMainActionRelease));
65 		keyBindingsMan.registerKeyBinding(new KeyBinding(PointerButton.PB_2, "key.secondaryAction", &onSecondaryActionPress, &onSecondaryActionRelease));
66 		keyBindingsMan.registerKeyBinding(new KeyBinding(PointerButton.PB_3, "key.tertiaryAction", &onTertiaryActionPress, &onTertiaryActionRelease));
67 		keyBindingsMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_RIGHT, "key.next_tool", null, &nextTool));
68 		keyBindingsMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_LEFT, "key.prev_tool", null, &prevTool));
69 		keyBindingsMan.registerKeyBinding(new KeyBinding(KeyCode.KEY_R, "key.rotateAction", null, &onRotateAction));
70 	}
71 
72 	override void preInit() {
73 		nullTool = new NullTool;
74 		fillTool = new FillTool;
75 		registerTool(fillTool);
76 	}
77 
78 	override void init(IPluginManager pluginman)
79 	{
80 		fillTool.connection = pluginman.getPlugin!NetClientPlugin;
81 		fillTool.worldInteraction = pluginman.getPlugin!WorldInteractionPlugin;
82 		fillTool.graphics = pluginman.getPlugin!GraphicsPlugin;
83 
84 		clientPlugin = pluginman.getPlugin!ClientPlugin;
85 		EventDispatcherPlugin evDispatcher = pluginman.getPlugin!EventDispatcherPlugin;
86 		evDispatcher.subscribeToEvent(&onUpdateEvent);
87 	}
88 
89 	void registerTool(ITool tool)
90 	{
91 		assert(tool);
92 		tools.put(tool);
93 	}
94 
95 	void nextTool(string) {
96 		selectedTool = selectedTool+1;
97 		if (selectedTool > tools.length-1)
98 			selectedTool = 0;
99 	}
100 
101 	void prevTool(string) {
102 		selectedTool = clamp(selectedTool-1, 0, tools.length-1);
103 	}
104 
105 	ITool currentTool() @property {
106 		if (selectedTool < tools.length)
107 			return tools[selectedTool];
108 		return nullTool;
109 	}
110 
111 	void onUpdateEvent(ref UpdateEvent event) {
112 		igBegin("Debug");
113 		igTextf("Tool: %s", currentTool.name);
114 		igEnd();
115 		currentTool.onUpdate();
116 	}
117 	void onMainActionPress(string key) {
118 		if (!clientPlugin.mouseLocked) return;
119 		currentTool.onMainActionPress();
120 	}
121 	void onMainActionRelease(string key) {
122 		if (!clientPlugin.mouseLocked) return;
123 		currentTool.onMainActionRelease();
124 	}
125 	void onSecondaryActionPress(string key) {
126 		if (!clientPlugin.mouseLocked) return;
127 		currentTool.onSecondaryActionPress();
128 	}
129 	void onSecondaryActionRelease(string key) {
130 		if (!clientPlugin.mouseLocked) return;
131 		currentTool.onSecondaryActionRelease();
132 	}
133 	void onTertiaryActionPress(string key) {
134 		if (!clientPlugin.mouseLocked) return;
135 		currentTool.onTertiaryActionPress();
136 	}
137 	void onTertiaryActionRelease(string key) {
138 		if (!clientPlugin.mouseLocked) return;
139 		currentTool.onTertiaryActionRelease();
140 	}
141 	void onRotateAction(string key) {
142 		if (!clientPlugin.mouseLocked) return;
143 		currentTool.onRotateAction();
144 	}
145 }