Browse Source

tools-imgui: expose console_send_*() so other parts of the editor can use them

Daniele Bartolini 6 years ago
parent
commit
edf410d392
4 changed files with 45 additions and 48 deletions
  1. 10 15
      tools-imgui/level_editor.cpp
  2. 9 10
      tools-imgui/tool_api.h
  3. 14 10
      tools-imgui/widgets/console.h
  4. 12 13
      tools-imgui/widgets/console.inl

+ 10 - 15
tools-imgui/level_editor.cpp

@@ -13,6 +13,7 @@
 #include "core/json/json_object.h"
 #include "core/json/sjson.h"
 #include "core/math/vector2.h"
+#include "core/network/ip_address.h"
 #include "core/network/socket.h"
 #include "core/strings/dynamic_string.h"
 #include "device/device.h"
@@ -42,6 +43,9 @@ namespace crown
 static u16 _width = 1280;
 static u16 _height = 720;
 
+static struct LevelEditor* s_editor;
+static TCPSocket _client;
+
 struct Pivot
 {
 	enum Enum
@@ -884,7 +888,7 @@ struct LevelEditor
 		for (;;)
 		{
 			u32 msg_len = 0;
-			ReadResult rr = _console._client.read_nonblock(&msg_len, sizeof(msg_len));
+			ReadResult rr = _client.read_nonblock(&msg_len, sizeof(msg_len));
 
 			if (rr.error == ReadResult::WOULDBLOCK)
 				break;
@@ -892,7 +896,7 @@ struct LevelEditor
 			if (ReadResult::SUCCESS == rr.error)
 			{
 				char msg[8192];
-				rr = _console._client.read(msg, msg_len);
+				rr = _client.read(msg, msg_len);
 				msg[msg_len] = '\0';
 				message_count++;
 				// logi(LEVEL_EDITOR, "count: %d", message_count);
@@ -1036,7 +1040,7 @@ struct LevelEditor
 
 		if (ImGui::BeginDock("Console", &_console._open, ImGuiWindowFlags_NoScrollbar))
 		{
-			console_draw(_console);
+			console_draw(_console, _client);
 		}
 		ImGui::EndDock();
 
@@ -1053,16 +1057,7 @@ struct LevelEditor
 
 	void send_command(StringStream& ss)
 	{
-		TempAllocator4096 ta;
-		StringStream out(ta);
-		out << "{\"type\":\"script\",\"script\":\"";
-		out << string_stream::c_str(ss);
-		out << "\"}";
-
-		const char* cmd = string_stream::c_str(out);
-		const u32 size = strlen32(cmd);
-		_console._client.write(&size, sizeof(u32));
-		_console._client.write(cmd, size);
+		console_send_script(_client, string_stream::c_str(ss));
 	}
 
 	void tool_send_state()
@@ -1319,12 +1314,11 @@ struct LevelEditor
 	}
 };
 
-LevelEditor* s_editor;
-
 void tool_init()
 {
 	const DeviceOptions& opt = device()->_device_options;
 	s_editor = CE_NEW(default_allocator(), LevelEditor)(opt._source_dir);
+	_client.connect(IP_ADDRESS_LOOPBACK, CROWN_DEFAULT_CONSOLE_PORT);
 }
 
 void tool_update(f32 dt)
@@ -1334,6 +1328,7 @@ void tool_update(f32 dt)
 
 void tool_shutdown()
 {
+	_client.close();
 	CE_DELETE(default_allocator(), s_editor);
 }
 

+ 9 - 10
tools-imgui/tool_api.h

@@ -130,13 +130,13 @@ void mouse_move(StringStream& out, f32 x, f32 y)
 void key_down(StringStream& out, const char* key)
 {
 	out << "LevelEditor:key_down(";
-	out << "\\\"" << key << "\\\"" << ")";
+	out << "'" << key << "'" << ")";
 }
 
 void key_up(StringStream& out, const char* key)
 {
 	out << "LevelEditor:key_up(";
-	out << "\\\"" << key << "\\\"" << ")";
+	out << "'" << key << "'" << ")";
 }
 
 void set_grid_size(StringStream& out, f32 size)
@@ -188,19 +188,18 @@ void set_tool_type(StringStream& out, const ToolType::Enum tt)
 	out << ")";
 }
 
-void set_snap_mode(StringStream& out
-	, const SnapMode::Enum sm)
+void set_snap_mode(StringStream& out, const SnapMode::Enum sm)
 {
-	out << "LevelEditor:set_snap_mode(\\\"";
+	out << "LevelEditor:set_snap_mode('";
 	out << (sm == SnapMode::RELATIVE ? "relative" : "absolute");
-	out << "\\\")";
+	out << "')";
 }
 
 void set_reference_system(StringStream& out, const ReferenceSystem::Enum rs)
 {
-	out << "LevelEditor:set_reference_system(\\\"";
+	out << "LevelEditor:set_reference_system('";
 	out << (rs == ReferenceSystem::LOCAL ? "local" : "world");
-	out << "\\\")";
+	out << "')";
 }
 
 void spawn_unit(StringStream& out
@@ -324,8 +323,8 @@ void set_placeable(StringStream& out
 	, const char* name)
 {
 	out << "LevelEditor:set_placeable(";
-	out << "\\\"" << type << "\\\"" << ",";
-	out << "\\\"" << name << "\\\"" << ")";
+	out << "'" << type << "'" << ",";
+	out << "'" << name << "'" << ")";
 }
 
 void selection_set(StringStream& /*out*/, const Array<Guid>& /*ids*/)

+ 14 - 10
tools-imgui/widgets/console.h

@@ -1,10 +1,12 @@
+/*
+ * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
+ * License: https://github.com/dbartolini/crown/blob/master/LICENSE
+ */
+
 #pragma once
 
-#include "config.h"
 #include "core/containers/vector.h"
-#include "core/memory/temp_allocator.h"
-#include "core/network/ip_address.h"
-#include "core/network/socket.h"
+#include "core/network/types.h"
 #include "core/strings/dynamic_string.h"
 #include "device/log.h"
 
@@ -18,7 +20,6 @@ namespace crown
 
 	struct Console
 	{
-		TCPSocket _client;
 		ConsoleLog _items[1024];
 		u32 _num_items;
 		Vector<DynamicString> _history;
@@ -33,20 +34,23 @@ namespace crown
 		///
 		Console();
 
-		///
-		~Console();
-
 		///
 		void add_log(LogSeverity::Enum severity, const char* message);
 	};
 
 	// Draws the console
-	void console_draw(Console& client);
+	void console_draw(Console& console, TCPSocket& client);
 
 	// Helpers
-	void console_execute_command(Console& console, const char* command);
+	void console_execute_command(Console& console, TCPSocket& client, const char* command);
 
 	// Scroll the console to the latest log message
 	void console_scroll_to_bottom(Console& console);
 
+	///
+	void console_send_script(TCPSocket& client, const char* lua);
+
+	///
+	void console_send_command(TCPSocket& client, char* cmd);
+
 } // namespace crown

+ 12 - 13
tools-imgui/widgets/console.inl

@@ -1,4 +1,11 @@
+/*
+ * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
+ * License: https://github.com/dbartolini/crown/blob/master/LICENSE
+ */
+
 #include "core/memory/temp_allocator.h"
+#include "core/network/ip_address.h"
+#include "core/network/socket.h"
 #include "core/strings/string_stream.h"
 #include <string.h> // strtok_r
 #if CROWN_COMPILER_MSVC
@@ -25,7 +32,7 @@ static void console_sanitize_json_string(StringStream& json, const char* str)
 	}
 }
 
-static void console_send_script(TCPSocket& client, const char* lua)
+void console_send_script(TCPSocket& client, const char* lua)
 {
 	TempAllocator1024 ta;
 	StringStream json(ta);
@@ -37,7 +44,7 @@ static void console_send_script(TCPSocket& client, const char* lua)
 	console_send(client, string_stream::c_str(json));
 }
 
-static void console_send_command(TCPSocket& client, char* cmd)
+void console_send_command(TCPSocket& client, char* cmd)
 {
 	TempAllocator1024 ta;
 	StringStream json(ta);
@@ -68,8 +75,6 @@ Console::Console()
 	, _history_pos(-1)
 	, _scroll_to_bottom(true)
 {
-	_client.connect(IP_ADDRESS_LOOPBACK, CROWN_DEFAULT_CONSOLE_PORT);
-
 	for (u32 i = 0; i < countof(_items); ++i)
 		_items[i].severity = LogSeverity::COUNT;
 
@@ -86,11 +91,6 @@ Console::Console()
 	memset(_input_text, 0, sizeof(_input_text));
 }
 
-Console::~Console()
-{
-	_client.close();
-}
-
 void Console::add_log(LogSeverity::Enum severity, const char* message)
 {
 	_items[_num_items].severity = severity;
@@ -135,7 +135,7 @@ int console_inputtext_callback(ImGuiInputTextCallbackData* data)
 	return 0;
 }
 
-void console_draw(Console& console)
+void console_draw(Console& console, TCPSocket& client)
 {
 	if (!console._open)
 		return;
@@ -208,7 +208,7 @@ void console_draw(Console& console)
 		if (console._input_text[0])
 		{
 			console.add_log(LogSeverity::LOG_INFO, console._input_text);
-			console_execute_command(console, console._input_text);
+			console_execute_command(console, client, console._input_text);
 		}
 
 		strcpy(console._input_text, "");
@@ -221,9 +221,8 @@ void console_draw(Console& console)
 	ImGui::PopItemWidth();
 }
 
-void console_execute_command(Console& console, const char* command)
+void console_execute_command(Console& console, TCPSocket& client, const char* command)
 {
-	TCPSocket& client = console._client;
 	Vector<DynamicString>& history = console._history;
 	Vector<DynamicString>& commands = console._commands;