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 module voxelman.utils.messagewindow;
7 
8 struct MessageWindow
9 {
10 	import voxelman.utils.linebuffer : LineBuffer;
11 	LineBuffer lineBuffer;
12 	alias lineBuffer this;
13 	char[] inputBuf;
14 	void delegate(string command) messageHandler;
15 
16 	void init()
17 	{
18 		import std.array : uninitializedArray;
19 		inputBuf = uninitializedArray!(char[])(1024);
20 		inputBuf[0] = '\0';
21 	}
22 
23 	void draw(bool drawBorder = true)
24 	{
25 		import derelict.imgui.imgui;
26 		import std..string;
27 
28 		igPushStyleVarVec(ImGuiStyleVar_WindowPadding, ImVec2(0,0));
29 		igBeginChildEx(0, ImVec2(0,-igGetItemsLineHeightWithSpacing()),
30 			drawBorder, ImGuiWindowFlags_HorizontalScrollbar);
31 		lineBuffer.drawSelectable();
32 		igEndChild();
33 		igPopStyleVar();
34 
35 
36 		bool press = false;
37 
38 		igPushItemWidth(igGetContentRegionAvailWidth()-60);
39 		if (igInputText("##input", inputBuf.ptr, inputBuf.length, ImGuiInputTextFlags_EnterReturnsTrue, null, null))
40 		{
41 			press = true;
42 		}
43 
44 		igPopItemWidth();
45 		igSameLine();
46 		press = press || igButton("Enter");
47 
48 		if (press)
49 		{
50 			auto command = cast(string)(inputBuf.ptr.fromStringz).strip;
51 			if (command.length > 0)
52 			{
53 				messageHandler(command);
54 				inputBuf[] = '\0';
55 			}
56 			if (igIsItemHovered() || (igIsRootWindowOrAnyChildFocused() && !igIsAnyItemActive() && !igIsMouseClicked(0)))
57 				igSetKeyboardFocusHere(-2); // Auto focus input
58 		}
59 	}
60 }