From adf5946711dd8a6d56028d229dc3c5e100bdd879 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 7 Jan 2016 13:33:26 +0100 Subject: [PATCH 1/4] Core/Console: Add CountOf function Former-commit-id: 8ea1456bc8debae102ca616176febb15d3759d17 --- include/Nazara/Core/Algorithm.hpp | 2 ++ include/Nazara/Core/Algorithm.inl | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/Nazara/Core/Algorithm.hpp b/include/Nazara/Core/Algorithm.hpp index 408eb79cf..048c5d902 100644 --- a/include/Nazara/Core/Algorithm.hpp +++ b/include/Nazara/Core/Algorithm.hpp @@ -23,6 +23,8 @@ namespace Nz template auto Apply(O& object, F&& fn, Tuple&& t); template ByteArray ComputeHash(HashType hash, const T& v); template ByteArray ComputeHash(AbstractHash* hash, const T& v); + template constexpr std::size_t CountOf(T(&name)[N]) noexcept; + template std::size_t CountOf(const T& c); template void HashCombine(std::size_t& seed, const T& v); template diff --git a/include/Nazara/Core/Algorithm.inl b/include/Nazara/Core/Algorithm.inl index a23fea4e1..5977c6d42 100644 --- a/include/Nazara/Core/Algorithm.inl +++ b/include/Nazara/Core/Algorithm.inl @@ -62,6 +62,18 @@ namespace Nz return hash->End(); } + template + constexpr std::size_t CountOf(T(&name)[N]) noexcept + { + return N; + } + + template + std::size_t CountOf(const T& c) + { + return c.size(); + } + // Algorithme venant de CityHash par Google // http://stackoverflow.com/questions/8513911/how-to-create-a-good-hash-combine-with-64-bit-output-inspired-by-boosthash-co template From 55cfd66c7abcb8366e725b46b544f6ac8e87e28e Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 7 Jan 2016 13:43:53 +0100 Subject: [PATCH 2/4] Sdk: Add Console Former-commit-id: c80d3c9dbbb0391fdb48a2710f5b5bf7468c050d --- SDK/include/NDK/Console.hpp | 89 +++++++++++++++ SDK/include/NDK/Console.inl | 44 ++++++++ SDK/src/NDK/Console.cpp | 219 ++++++++++++++++++++++++++++++++++++ 3 files changed, 352 insertions(+) create mode 100644 SDK/include/NDK/Console.hpp create mode 100644 SDK/include/NDK/Console.inl create mode 100644 SDK/src/NDK/Console.cpp diff --git a/SDK/include/NDK/Console.hpp b/SDK/include/NDK/Console.hpp new file mode 100644 index 000000000..b594c1179 --- /dev/null +++ b/SDK/include/NDK/Console.hpp @@ -0,0 +1,89 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_CONSOLE_HPP +#define NDK_CONSOLE_HPP + +#include +#include +#include +#include +#include + +namespace Nz +{ + class LuaInstance; +} + +namespace Ndk +{ + class Entity; + + class NDK_API Console : public Nz::Node + { + public: + Console(World& world, const Nz::Vector2f& size, Nz::LuaInstance& instance); + Console(const Console& console) = delete; + Console(Console&& console) = default; + ~Console() = default; + + void AddLine(const Nz::String& text, const Nz::Color& color = Nz::Color::White); + + inline const EntityHandle& GetBackground() const; + inline unsigned int GetCharacterSize() const; + inline const EntityHandle& GetHistory() const; + inline const EntityHandle& GetInput() const; + inline const Nz::Vector2f& GetSize() const; + inline const Nz::FontRef& GetTextFont() const; + + inline bool IsVisible() const; + + void SendCharacter(char32_t character); + + void SetCharacterSize(unsigned int size); + void SetSize(const Nz::Vector2f& size); + void SetTextFont(Nz::FontRef font); + + void Show(bool show = true); + + Console& operator=(const Console& console) = delete; + Console& operator=(Console&& console) = default; + + private: + void AddLineInternal(const Nz::String& text, const Nz::Color& color = Nz::Color::White); + void ExecuteInput(); + void Layout(); + void RefreshHistory(); + + struct Line + { + Nz::Color color; + Nz::String text; + }; + + std::vector m_historyLines; + EntityOwner m_background; + EntityOwner m_history; + EntityOwner m_input; + EntityOwner m_inputBackground; + Nz::FontRef m_defaultFont; + Nz::LuaInstance& m_instance; + Nz::SpriteRef m_backgroundSprite; + Nz::SpriteRef m_inputBackgroundSprite; + Nz::SimpleTextDrawer m_historyDrawer; + Nz::SimpleTextDrawer m_inputDrawer; + Nz::TextSpriteRef m_historyTextSprite; + Nz::TextSpriteRef m_inputTextSprite; + Nz::Vector2f m_size; + bool m_opened; + unsigned int m_characterSize; + unsigned int m_maxHistoryLines; + }; +} + +#include + +#endif // NDK_CONSOLE_HPP diff --git a/SDK/include/NDK/Console.inl b/SDK/include/NDK/Console.inl new file mode 100644 index 000000000..d4c3d5e2d --- /dev/null +++ b/SDK/include/NDK/Console.inl @@ -0,0 +1,44 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include +#include "Console.hpp" + +namespace Ndk +{ + inline const EntityHandle& Console::GetBackground() const + { + return m_background; + } + + inline unsigned int Console::GetCharacterSize() const + { + return m_characterSize; + } + + inline const EntityHandle& Console::GetHistory() const + { + return m_history; + } + + inline const EntityHandle& Console::GetInput() const + { + return m_input; + } + + inline const Nz::Vector2f& Console::GetSize() const + { + return m_size; + } + + inline const Nz::FontRef& Console::GetTextFont() const + { + return m_defaultFont; + } + + inline bool Console::IsVisible() const + { + return m_opened; + } +} diff --git a/SDK/src/NDK/Console.cpp b/SDK/src/NDK/Console.cpp new file mode 100644 index 000000000..da16e3aa2 --- /dev/null +++ b/SDK/src/NDK/Console.cpp @@ -0,0 +1,219 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include +#include +#include +#include +#include +#include + +///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_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().Attach(m_historyBackgroundSprite, -1); + m_historyBackground->AddComponent().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().Attach(m_historyTextSprite); + + Ndk::NodeComponent& historyNode = m_history->AddComponent(); + 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().Attach(m_inputBackgroundSprite, -1); + m_inputBackground->AddComponent().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().Attach(m_inputTextSprite); + + Ndk::NodeComponent& inputNode = m_input->AddComponent(); + inputNode.SetParent(this); + + Layout(); + } + + void Console::AddLine(const Nz::String& text, const Nz::Color& color) + { + AddLineInternal(text, color); + 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::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(); + } + + void Console::Show(bool show) + { + if (m_opened != show) + { + m_historyBackground->Enable(show); + m_history->Enable(show); + m_input->Enable(show); + m_inputBackground->Enable(show); + + m_opened = show; + } + } + + void Console::AddLineInternal(const Nz::String& text, const Nz::Color& color) + { + m_historyLines.emplace_back(Line{color, text}); + } + + void Console::ExecuteInput() + { + Nz::String input = m_inputDrawer.GetText().SubString(s_inputPrefixSize); + m_inputDrawer.SetText(s_inputPrefix); + + AddLineInternal(input); + + if (!m_instance.Execute(input)) + AddLineInternal(m_instance.GetLastError(), Nz::Color::Red); + + RefreshHistory(); + } + + void Console::Layout() + { + unsigned int lineHeight = m_defaultFont->GetSizeInfo(m_characterSize).lineHeight; + + Ndk::NodeComponent& inputNode = m_input->GetComponent(); + inputNode.SetPosition(0.f, m_size.y - lineHeight - 5.f); + + float historyHeight = m_size.y - lineHeight - 5.f - 2.f; + m_historyBackgroundSprite->SetSize(m_size.x, historyHeight); + + m_maxHistoryLines = static_cast(std::ceil(historyHeight / lineHeight)); + + Ndk::NodeComponent& historyNode = m_history->GetComponent(); + historyNode.SetPosition(0.f, historyHeight - m_maxHistoryLines * lineHeight); + + Ndk::NodeComponent& inputBackgroundNode = m_inputBackground->GetComponent(); + inputBackgroundNode.SetPosition(0.f, historyHeight + 2.f); + + m_inputBackgroundSprite->SetSize(m_size.x, m_size.y - historyHeight); + } + + void Console::RefreshHistory() + { + m_historyDrawer.Clear(); + auto it = m_historyLines.end(); + if (m_historyLines.size() > m_maxHistoryLines) + it -= m_maxHistoryLines; + else + it = m_historyLines.begin(); + + for (unsigned int i = 0; i < m_maxHistoryLines; ++i) + { + if (m_maxHistoryLines - i <= m_historyLines.size() && it != m_historyLines.end()) + { + m_historyDrawer.AppendText(it->text); + ++it; + } + + m_historyDrawer.AppendText(Nz::String('\n')); + } + + m_historyTextSprite->Update(m_historyDrawer); + } +} From 8441dcfa2a368cfa830405238f312e921697e276 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 7 Jan 2016 18:07:03 +0100 Subject: [PATCH 3/4] Ndk/Console: Fix compilation Former-commit-id: 3579cc6a392f1e74134e035192c5ecdd3e779266 --- SDK/include/NDK/Console.hpp | 7 ++++--- SDK/include/NDK/Console.inl | 15 ++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/SDK/include/NDK/Console.hpp b/SDK/include/NDK/Console.hpp index b594c1179..1f0d36930 100644 --- a/SDK/include/NDK/Console.hpp +++ b/SDK/include/NDK/Console.hpp @@ -32,10 +32,11 @@ namespace Ndk void AddLine(const Nz::String& text, const Nz::Color& color = Nz::Color::White); - inline const EntityHandle& GetBackground() const; inline unsigned int GetCharacterSize() const; inline const EntityHandle& GetHistory() const; + inline const EntityHandle& GetHistoryBackground() const; inline const EntityHandle& GetInput() const; + inline const EntityHandle& GetInputBackground() const; inline const Nz::Vector2f& GetSize() const; inline const Nz::FontRef& GetTextFont() const; @@ -65,13 +66,13 @@ namespace Ndk }; std::vector m_historyLines; - EntityOwner m_background; + EntityOwner m_historyBackground; EntityOwner m_history; EntityOwner m_input; EntityOwner m_inputBackground; Nz::FontRef m_defaultFont; Nz::LuaInstance& m_instance; - Nz::SpriteRef m_backgroundSprite; + Nz::SpriteRef m_historyBackgroundSprite; Nz::SpriteRef m_inputBackgroundSprite; Nz::SimpleTextDrawer m_historyDrawer; Nz::SimpleTextDrawer m_inputDrawer; diff --git a/SDK/include/NDK/Console.inl b/SDK/include/NDK/Console.inl index d4c3d5e2d..5fb16bb1b 100644 --- a/SDK/include/NDK/Console.inl +++ b/SDK/include/NDK/Console.inl @@ -7,11 +7,6 @@ namespace Ndk { - inline const EntityHandle& Console::GetBackground() const - { - return m_background; - } - inline unsigned int Console::GetCharacterSize() const { return m_characterSize; @@ -22,11 +17,21 @@ namespace Ndk return m_history; } + inline const EntityHandle& Console::GetHistoryBackground() const + { + return m_historyBackground; + } + inline const EntityHandle& Console::GetInput() const { return m_input; } + inline const EntityHandle& Console::GetInputBackground() const + { + return m_inputBackground; + } + inline const Nz::Vector2f& Console::GetSize() const { return m_size; From ae6ba51380d7961883eda6a8d852676739c5574c Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 7 Jan 2016 18:07:51 +0100 Subject: [PATCH 4/4] Utility: Remove useless file Former-commit-id: e3b885151fe47d60bc4904605859b98d7b4c4110 --- include/Nazara/Utility/Image.inl.save-failed | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 include/Nazara/Utility/Image.inl.save-failed diff --git a/include/Nazara/Utility/Image.inl.save-failed b/include/Nazara/Utility/Image.inl.save-failed deleted file mode 100644 index 5cb8ef42f..000000000 --- a/include/Nazara/Utility/Image.inl.save-failed +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Utility module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include - -namespace Nz -{ - template - ImageRef Image::New(Args&&... args) - { - std::unique_ptr object(new Image(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } -} - -#include