<commit_before>// Copyright (C) 2015 <NAME>
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp

#include <NDK/Console.hpp>
#include <Nazara/Core/Unicode.hpp>
#include <Nazara/Lua/LuaInstance.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/World.hpp>

///TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so

namespace Ndk
{
	namespace
	{
		const char s_inputPrefix[] = "> ";
		constexpr std::size_t s_inputPrefixSize = Nz::CountOf(s_inputPrefix) - 1;
	}

	Console::Console(World& world, const Nz::Vector2f& size, Nz::LuaInstance& instance) :
	m_historyPosition(0),
	m_defaultFont(Nz::Font::GetDefault()),
	m_instance(instance),
	m_size(size),
	m_opened(false),
	m_characterSize(24)
	{
		Nz::MaterialRef backgroundMaterial = Nz::Material::New();
		backgroundMaterial->Enable(Nz::RendererParameter_Blend, true);
		backgroundMaterial->Enable(Nz::RendererParameter_DepthBuffer, false);
		backgroundMaterial->SetDstBlend(Nz::BlendFunc_InvSrcAlpha);
		backgroundMaterial->SetSrcBlend(Nz::BlendFunc_SrcAlpha);

		// History bakckground
		m_historyBackgroundSprite = Nz::Sprite::New();
		m_historyBackgroundSprite->SetColor(Nz::Color(80, 80, 160, 128));
		m_historyBackgroundSprite->SetMaterial(backgroundMaterial);

		m_historyBackground = world.CreateEntity();
		m_historyBackground->Enable(m_opened);
		m_historyBackground->AddComponent<Ndk::GraphicsComponent>().Attach(m_historyBackgroundSprite, -1);
		m_historyBackground->AddComponent<Ndk::NodeComponent>().SetParent(this);

		// History
		m_historyDrawer.SetCharacterSize(m_characterSize);
		m_historyDrawer.SetColor(Nz::Color(200, 200, 200));
		m_historyDrawer.SetFont(m_defaultFont);

		m_historyTextSprite = Nz::TextSprite::New();

		m_history = world.CreateEntity();
		m_history->Enable(m_opened);
		m_history->AddComponent<Ndk::GraphicsComponent>().Attach(m_historyTextSprite);

		Ndk::NodeComponent& historyNode = m_history->AddComponent<Ndk::NodeComponent>();
		historyNode.SetParent(this);

		// Input background
		m_inputBackgroundSprite = Nz::Sprite::New();
		m_inputBackgroundSprite->SetColor(Nz::Color(255, 255, 255, 200));
		m_inputBackgroundSprite->SetMaterial(backgroundMaterial);

		m_inputBackground = world.CreateEntity();
		m_inputBackground->Enable(m_opened);
		m_inputBackground->AddComponent<Ndk::GraphicsComponent>().Attach(m_inputBackgroundSprite, -1);
		m_inputBackground->AddComponent<Ndk::NodeComponent>().SetParent(this);

		// Input
		m_inputDrawer.SetColor(Nz::Color::Black);
		m_inputDrawer.SetCharacterSize(m_characterSize);
		m_inputDrawer.SetFont(m_defaultFont);
		m_inputDrawer.SetText(s_inputPrefix);

		m_inputTextSprite = Nz::TextSprite::New();
		m_inputTextSprite->Update(m_inputDrawer);

		m_input = world.CreateEntity();
		m_input->Enable(m_opened);
		m_input->AddComponent<Ndk::GraphicsComponent>().Attach(m_inputTextSprite);

		Ndk::NodeComponent& inputNode = m_input->AddComponent<Ndk::NodeComponent>();
		inputNode.SetParent(this);

		Layout();
	}

	void Console::AddLine(const Nz::String& text, const Nz::Color& color)
	{
		AddLineInternal(text, color);
		RefreshHistory();
	}

	void Console::Clear()
	{
		m_historyLines.clear();
		RefreshHistory();
	}

	void Console::SendCharacter(char32_t character)
	{
		switch (character)
		{
			case '\b':
			{
				Nz::String input = m_inputDrawer.GetText();
				if (input.GetLength() <= s_inputPrefixSize) // Prevent removal of the input prefix
					return; // Ignore if no user character is there

				input.Resize(-1, Nz::String::HandleUtf8);
				m_inputDrawer.SetText(input);
				break;
			}

			case '\r':
			case '\n':
				ExecuteInput();
				break;

			default:
			{
				if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control)
					return;

				m_inputDrawer.AppendText(Nz::String::Unicode(character));
				break;
			}
		}

		m_inputTextSprite->Update(m_inputDrawer);
	}

	void Console::SendEvent(Nz::WindowEvent event)
	{
		switch (event.type)
		{
			case Nz::WindowEventType_TextEntered:
				SendCharacter(event.text.character);
				break;

			case Nz::WindowEventType_KeyPressed:
			{
				switch (event.key.code)
				{
					case Nz::Keyboard::Down:
					case Nz::Keyboard::Up:
						if (event.key.code == Nz::Keyboard::Up)
							m_historyPosition = std::min<std::size_t>(m_commandHistory.size(), m_historyPosition + 1);
						else
						{
							if (m_historyPosition > 1)
								m_historyPosition--;
							else if (m_historyPosition == 0)
								m_historyPosition = 1;
						}

						Nz::String text = m_commandHistory[m_commandHistory.size() - m_historyPosition];
						m_inputDrawer.SetText(s_inputPrefix + text);
						m_inputTextSprite->Update(m_inputDrawer);
						break;
				}
				break;
			}
		}
	}

	void Console::SetCharacterSize(unsigned int size)
	{
		m_characterSize = size;
		Layout();
	}

	void Console::SetSize(const Nz::Vector2f& size)
	{
		m_size = size;
		m_historyBackgroundSprite->SetSize(m_size);
		Layout();
	}

	void Console::SetTextFont(Nz::FontRef font)
	{
		Layout();
	}
<commit_msg>Sdk/Console: Fix SetCharacterSize not changing size of history and input<commit_after>// Copyright (C) 2015 <NAME>
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp

#include <NDK/Console.hpp>
#include <Nazara/Core/Unicode.hpp>
#include <Nazara/Lua/LuaInstance.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/World.hpp>

///TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so

namespace Ndk
{
	namespace
	{
		const char s_inputPrefix[] = "> ";
		constexpr std::size_t s_inputPrefixSize = Nz::CountOf(s_inputPrefix) - 1;
	}

	Console::Console(World& world, const Nz::Vector2f& size, Nz::LuaInstance& instance) :
	m_historyPosition(0),
	m_defaultFont(Nz::Font::GetDefault()),
	m_instance(instance),
	m_size(size),
	m_opened(false),
	m_characterSize(24)
	{
		Nz::MaterialRef backgroundMaterial = Nz::Material::New();
		backgroundMaterial->Enable(Nz::RendererParameter_Blend, true);
		backgroundMaterial->Enable(Nz::RendererParameter_DepthBuffer, false);
		backgroundMaterial->SetDstBlend(Nz::BlendFunc_InvSrcAlpha);
		backgroundMaterial->SetSrcBlend(Nz::BlendFunc_SrcAlpha);

		// History bakckground
		m_historyBackgroundSprite = Nz::Sprite::New();
		m_historyBackgroundSprite->SetColor(Nz::Color(80, 80, 160, 128));
		m_historyBackgroundSprite->SetMaterial(backgroundMaterial);

		m_historyBackground = world.CreateEntity();
		m_historyBackground->Enable(m_opened);
		m_historyBackground->AddComponent<Ndk::GraphicsComponent>().Attach(m_historyBackgroundSprite, -1);
		m_historyBackground->AddComponent<Ndk::NodeComponent>().SetParent(this);

		// History
		m_historyDrawer.SetCharacterSize(m_characterSize);
		m_historyDrawer.SetColor(Nz::Color(200, 200, 200));
		m_historyDrawer.SetFont(m_defaultFont);

		m_historyTextSprite = Nz::TextSprite::New();

		m_history = world.CreateEntity();
		m_history->Enable(m_opened);
		m_history->AddComponent<Ndk::GraphicsComponent>().Attach(m_historyTextSprite);

		Ndk::NodeComponent& historyNode = m_history->AddComponent<Ndk::NodeComponent>();
		historyNode.SetParent(this);

		// Input background
		m_inputBackgroundSprite = Nz::Sprite::New();
		m_inputBackgroundSprite->SetColor(Nz::Color(255, 255, 255, 200));
		m_inputBackgroundSprite->SetMaterial(backgroundMaterial);

		m_inputBackground = world.CreateEntity();
		m_inputBackground->Enable(m_opened);
		m_inputBackground->AddComponent<Ndk::GraphicsComponent>().Attach(m_inputBackgroundSprite, -1);
		m_inputBackground->AddComponent<Ndk::NodeComponent>().SetParent(this);

		// Input
		m_inputDrawer.SetColor(Nz::Color::Black);
		m_inputDrawer.SetCharacterSize(m_characterSize);
		m_inputDrawer.SetFont(m_defaultFont);
		m_inputDrawer.SetText(s_inputPrefix);

		m_inputTextSprite = Nz::TextSprite::New();
		m_inputTextSprite->Update(m_inputDrawer);

		m_input = world.CreateEntity();
		m_input->Enable(m_opened);
		m_input->AddComponent<Ndk::GraphicsComponent>().Attach(m_inputTextSprite);

		Ndk::NodeComponent& inputNode = m_input->AddComponent<Ndk::NodeComponent>();
		inputNode.SetParent(this);

		Layout();
	}

	void Console::AddLine(const Nz::String& text, const Nz::Color& color)
	{
		AddLineInternal(text, color);
		RefreshHistory();
	}

	void Console::Clear()
	{
		m_historyLines.clear();
		RefreshHistory();
	}

	void Console::SendCharacter(char32_t character)
	{
		switch (character)
		{
			case '\b':
			{
				Nz::String input = m_inputDrawer.GetText();
				if (input.GetLength() <= s_inputPrefixSize) // Prevent removal of the input prefix
					return; // Ignore if no user character is there

				input.Resize(-1, Nz::String::HandleUtf8);
				m_inputDrawer.SetText(input);
				break;
			}

			case '\r':
			case '\n':
				ExecuteInput();
				break;

			default:
			{
				if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control)
					return;

				m_inputDrawer.AppendText(Nz::String::Unicode(character));
				break;
			}
		}

		m_inputTextSprite->Update(m_inputDrawer);
	}

	void Console::SendEvent(Nz::WindowEvent event)
	{
		switch (event.type)
		{
			case Nz::WindowEventType_TextEntered:
				SendCharacter(event.text.character);
				break;

			case Nz::WindowEventType_KeyPressed:
			{
				switch (event.key.code)
				{
					case Nz::Keyboard::Down:
					case Nz::Keyboard::Up:
						if (event.key.code == Nz::Keyboard::Up)
							m_historyPosition = std::min<std::size_t>(m_commandHistory.size(), m_historyPosition + 1);
						else
						{
							if (m_historyPosition > 1)
								m_historyPosition--;
							else if (m_historyPosition == 0)
								m_historyPosition = 1;
						}

						Nz::String text = m_commandHistory[m_commandHistory.size() - m_historyPosition];
						m_inputDrawer.SetText(s_inputPrefix + text);
						m_inputTextSprite->Update(m_inputDrawer);
						break;
				}
				break;
			}
		}
	}

	void Console::SetCharacterSize(unsigned int size)
	{
		m_characterSize = size;

		m_historyDrawer.SetCharacterSize(m_characterSize);
		m_historyTextSprite->Update(m_historyDrawer);
		m_inputDrawer.SetCharacterSize(m_characterSize);
		m_inputTextSprite->Update(m_inputDrawer);

		Layout();
	}

	void Console::SetSize(const Nz::Vector2f& size)
	{
		m_size = size;
		m_historyBackgroundSprite->SetSize(m_size);
		Layout();
	}

	void Console::SetTextFont(Nz::FontRef font)
	{
		Layout();
	}

{'fraction_non_alphanumeric': 0.1333458894434504, 'fraction_numerical': 0.009605424239570582, 'mean_word_length': 5.156521739130435, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2229080', 'n_tokens_mistral': 4126, 'n_tokens_neox': 3583, 'n_words': 561}