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 module voxelman.client.console;
7 
8 import voxelman.log;
9 import voxelman.gui;
10 import voxelman.gui.textedit.lineedit;
11 import voxelman.gui.textedit.messagelog;
12 import voxelman.gui.textedit.texteditorview;
13 import voxelman.gui.textedit.textmodel;
14 import voxelman.graphics;
15 import voxelman.math;
16 
17 struct Console
18 {
19 	MessageLog messages;
20 	MessageLogTextModel messagesModel;
21 	TextViewSettings editorSettings;
22 
23 	void delegate(string command) messageHandler;
24 	bool isConsoleShown;
25 	bool wasJustHidden;
26 
27 	WidgetProxy viewport;
28 	WidgetProxy input;
29 
30 	void create(GuiContext guictx, void delegate(string command) messageHandler)
31 	{
32 		guictx.style.pushColor(Colors.white);
33 
34 		this.messageHandler = messageHandler;
35 
36 		auto con = guictx.createWidget(WidgetType("console"))
37 			.hexpand
38 			.minSize(0, 200)
39 			.visible_if(() => isConsoleShown)
40 			.addBackground(Color4ub(128, 128, 128, 128))
41 			.setVLayout(0, padding4(0));
42 
43 		guictx.roots ~= con;
44 
45 		editorSettings.font = guictx.style.font;
46 		editorSettings.color = cast(Color4ub)Colors.white;
47 
48 		messages = MessageLog(null);
49 		messages.setClipboard = guictx.state.setClipboard;
50 		messagesModel = new MessageLogTextModel(&messages);
51 
52 		viewport = TextEditorViewportLogic.create(con, messagesModel, &editorSettings).hvexpand;
53 		hline(con);
54 		input = LineEdit.create(con, &onInput).hexpand;
55 
56 		viewport.get!TextEditorViewportData.autoscroll = true;
57 
58 		guictx.style.popColor;
59 	}
60 
61 	void onInput(string command)
62 	{
63 		if (messageHandler) messageHandler(command);
64 		LineEdit.clear(input);
65 	}
66 
67 	// These two handlers consume console char event
68 	void onConsolePressKey(string)
69 	{
70 		if (isConsoleShown)
71 		{
72 			isConsoleShown = false;
73 			wasJustHidden = true;
74 			input.setFocus(isConsoleShown);
75 		}
76 	}
77 
78 	void onConsoleReleaseKey(string)
79 	{
80 		if (!isConsoleShown && !wasJustHidden)
81 		{
82 			isConsoleShown = true;
83 			input.setFocus(isConsoleShown);
84 		}
85 		wasJustHidden = false;
86 	}
87 }