Added CheckboxWidget (#130)

* Started Checkbox Widget (buggy)

* Added features

* Added enabling feature

* Almost finished Checkbox

* Bugfix

* Bugfixes

* Use a better name

* Optimizations

* Added explicit colors

* ...

* changed lots of things

* Almost finished CheckboxWidget

* Almost almost finished

* Use better UTF8 box

* Edited encode resources script to encode SDK resources

* Finished checkbox widget

* Forgot to delete old function

* Forgot to delete todo comment

* Fix Travis compilation

* Fix Travis compilation a second time

* Fix Travis Compilation a third time

* Fix indentation

* Fix Files encoding

* Moved CheckboxState enum in (new) Enum.hpp

* Probably looks like more generated now

* Reorder CheckboxWidget members

* Reorder checkbox state members

* Reorder members 2...

* Oops

* Reorder members functions

* Fix encoding

* Fix files encoding

* Forgot to fix one file encoding

* Fix SDK Server

* Fix encoding again -_-

* Oops

* Optimize Checkbox Widget

* Fix .gitignore

* Removed .vs in gitignore

* Moved Enums into Widgets folder

* Bugfix

* Fix Encode Resources script

* Remove double line feeds

* Rename SetNextState to SwitchToNextState
This commit is contained in:
S6066 2017-08-30 08:25:42 +00:00 committed by Jérôme Leclercq
parent 5aa072cee3
commit 55ca4a84ea
8 changed files with 424 additions and 2 deletions

View File

@ -5,6 +5,7 @@
#ifndef NDK_WIDGETS_GLOBAL_HPP
#define NDK_WIDGETS_GLOBAL_HPP
#include <NDK/Widgets/CheckboxWidget.hpp>
#include <NDK/Widgets/ButtonWidget.hpp>
#include <NDK/Widgets/LabelWidget.hpp>
#include <NDK/Widgets/TextAreaWidget.hpp>

View File

@ -0,0 +1,104 @@
// Copyright (C) 2017 Samy Bensaid
// 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_WIDGETS_CHECKBOXWIDGET_HPP
#define NDK_WIDGETS_CHECKBOXWIDGET_HPP
#include <NDK/Prerequesites.hpp>
#include <NDK/BaseWidget.hpp>
#include <NDK/Widgets/Enums.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Utility/AbstractTextDrawer.hpp>
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Rect.hpp>
namespace Ndk
{
class World;
class NDK_API CheckboxWidget : public BaseWidget
{
friend class Sdk;
public:
CheckboxWidget(BaseWidget* parent = nullptr);
CheckboxWidget(const CheckboxWidget&) = delete;
CheckboxWidget(CheckboxWidget&&) = default;
~CheckboxWidget() = default;
//virtual CheckboxWidget* Clone() const = 0;
inline void EnableAdaptativeMargin(bool enable = true);
inline void EnableCheckbox(bool enable = true);
inline void EnableTristate(bool enable = true);
inline bool IsCheckboxEnabled() const;
inline bool IsMarginAdaptative() const;
inline bool IsTristateEnabled() const;
inline const Nz::Vector2f& GetCheckboxSize() const;
inline Nz::Vector2f GetCheckboxBorderSize() const;
inline CheckboxState GetState() const;
inline float GetTextMargin() const;
inline void SetCheckboxSize(const Nz::Vector2f& size);
CheckboxState SwitchToNextState();
void SetState(CheckboxState state);
inline void SetTextMargin(float margin);
void ResizeToContent() override;
inline void UpdateText(const Nz::AbstractTextDrawer& drawer);
CheckboxWidget& operator=(const CheckboxWidget&) = delete;
CheckboxWidget& operator=(CheckboxWidget&&) = default;
NazaraSignal(OnStateChanged, const CheckboxWidget* /*checkbox*/);
private:
static bool Initialize();
static void Uninitialize();
void Layout() override;
void UpdateCheckbox();
void OnMouseButtonRelease(int x, int y, Nz::Mouse::Button button) override;
inline bool ContainsCheckbox(int x, int y) const;
EntityHandle m_checkboxBorderEntity;
EntityHandle m_checkboxBackgroundEntity;
EntityHandle m_checkboxContentEntity;
EntityHandle m_textEntity;
Nz::TextureRef m_checkMark;
Nz::SpriteRef m_checkboxContentSprite;
Nz::SpriteRef m_checkboxBorderSprite;
Nz::SpriteRef m_checkboxBackgroundSprite;
Nz::TextSpriteRef m_textSprite;
static Nz::Color s_backgroundColor;
static Nz::Color s_disabledBackgroundColor;
static Nz::Color s_disabledBorderColor;
static Nz::Color s_borderColor;
bool m_adaptativeMargin;
bool m_checkboxEnabled;
bool m_tristateEnabled;
static float s_borderScale;
float m_textMargin;
CheckboxState m_state;
};
}
#include <NDK/Widgets/CheckboxWidget.inl>
#endif // NDK_WIDGETS_CHECKBOXWIDGET_HPP

View File

@ -0,0 +1,91 @@
// Copyright (C) 2017 Samy Bensaid
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
namespace Ndk
{
inline void CheckboxWidget::EnableAdaptativeMargin(bool enable)
{
m_adaptativeMargin = enable;
Layout();
}
inline void CheckboxWidget::EnableCheckbox(bool enable)
{
m_checkboxEnabled = enable;
UpdateCheckbox();
}
inline void CheckboxWidget::EnableTristate(bool enable)
{
m_tristateEnabled = enable;
if (m_tristateEnabled && GetState() == CheckboxState_Tristate)
SetState(CheckboxState_Unchecked);
}
inline bool CheckboxWidget::IsCheckboxEnabled() const
{
return m_checkboxEnabled;
}
inline bool CheckboxWidget::IsMarginAdaptative() const
{
return m_adaptativeMargin;
}
inline bool CheckboxWidget::IsTristateEnabled() const
{
return m_tristateEnabled;
}
inline const Nz::Vector2f& CheckboxWidget::GetCheckboxSize() const
{
return m_checkboxBorderSprite->GetSize();
}
inline Nz::Vector2f CheckboxWidget::GetCheckboxBorderSize() const
{
return GetCheckboxSize() / s_borderScale;
}
inline CheckboxState CheckboxWidget::GetState() const
{
return m_state;
}
inline float CheckboxWidget::GetTextMargin() const
{
return m_textMargin;
}
inline void CheckboxWidget::SetCheckboxSize(const Nz::Vector2f& size)
{
m_checkboxBorderSprite->SetSize(size);
m_checkboxBackgroundSprite->SetSize(size - GetCheckboxBorderSize() * 2.f);
m_checkboxContentSprite->SetSize(GetCheckboxSize() - GetCheckboxBorderSize() * 2.f - Nz::Vector2f { 4.f, 4.f });
Layout();
}
inline void CheckboxWidget::SetTextMargin(float margin)
{
m_textMargin = margin;
Layout();
}
inline void CheckboxWidget::UpdateText(const Nz::AbstractTextDrawer& drawer)
{
m_textSprite->Update(drawer);
Layout();
}
inline bool CheckboxWidget::ContainsCheckbox(int x, int y) const
{
Nz::Vector2f checkboxSize = GetCheckboxSize();
Nz::Vector3f pos = m_checkboxBorderEntity->GetComponent<NodeComponent>().GetPosition(Nz::CoordSys_Local);
return x > pos.x && x < pos.x + checkboxSize.x &&
y > pos.y && y < pos.y + checkboxSize.y;
}
}

View File

@ -0,0 +1,22 @@
// Copyright (C) 2017 Samy Bensaid
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#pragma once
#ifndef NAZARA_ENUMS_SDK_HPP
#define NAZARA_ENUMS_SDK_HPP
namespace Ndk
{
enum CheckboxState
{
CheckboxState_Checked,
CheckboxState_Tristate,
CheckboxState_Unchecked,
CheckboxState_Max = CheckboxState_Unchecked
};
}
#endif // NAZARA_ENUMS_SDK_HPP

File diff suppressed because one or more lines are too long

View File

@ -36,6 +36,7 @@
#include <NDK/Systems/ParticleSystem.hpp>
#include <NDK/Systems/ListenerSystem.hpp>
#include <NDK/Systems/RenderSystem.hpp>
#include <NDK/Widgets/CheckboxWidget.hpp>
#endif
namespace Ndk
@ -114,6 +115,13 @@ namespace Ndk
InitializeSystem<ListenerSystem>();
InitializeSystem<ParticleSystem>();
InitializeSystem<RenderSystem>();
// Widgets
if (!CheckboxWidget::Initialize())
{
NazaraError("Failed to initialize Checkbox Widget");
return false;
}
#endif
NazaraNotice("Initialized: SDK");
@ -122,7 +130,6 @@ namespace Ndk
catch (const std::exception& e)
{
NazaraError("Failed to initialize NDK: " + Nz::String(e.what()));
return false;
}
}
@ -168,6 +175,11 @@ namespace Ndk
Nz::Physics3D::Uninitialize();
Nz::Utility::Uninitialize();
#ifndef NDK_SERVER
// Widgets
CheckboxWidget::Uninitialize();
#endif
NazaraNotice("Uninitialized: SDK");
}

View File

@ -0,0 +1,185 @@
// Copyright (C) 2017 Samy Bensaid
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <NDK/World.hpp>
#include <NDK/Widgets/CheckboxWidget.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <Nazara/Utility/SimpleTextDrawer.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Utility/Font.hpp>
#include <Nazara/Utility/FontGlyph.hpp>
#include <algorithm>
namespace Ndk
{
Nz::Color CheckboxWidget::s_backgroundColor { Nz::Color::White };
Nz::Color CheckboxWidget::s_disabledBackgroundColor { 201, 201, 201 };
Nz::Color CheckboxWidget::s_disabledBorderColor { 62, 62, 62 };
Nz::Color CheckboxWidget::s_borderColor { Nz::Color::Black };
float CheckboxWidget::s_borderScale { 16.f };
CheckboxWidget::CheckboxWidget(BaseWidget* parent) :
BaseWidget(parent),
m_adaptativeMargin { true },
m_checkboxEnabled { true },
m_tristateEnabled { false },
m_textMargin { 16.f },
m_state { CheckboxState_Unchecked }
{
m_checkboxBorderSprite = Nz::Sprite::New(Nz::Material::New("Basic2D"));
m_checkboxBackgroundSprite = Nz::Sprite::New(Nz::Material::New("Basic2D"));
m_checkboxContentSprite = Nz::Sprite::New(Nz::Material::New("Translucent2D"));
m_textSprite = Nz::TextSprite::New();
m_checkboxBorderEntity = CreateEntity();
m_checkboxBorderEntity->AddComponent<NodeComponent>().SetParent(this);
m_checkboxBorderEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxBorderSprite);
m_checkboxBackgroundEntity = CreateEntity();
m_checkboxBackgroundEntity->AddComponent<NodeComponent>().SetParent(this);
m_checkboxBackgroundEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxBackgroundSprite, 1);
m_checkboxContentEntity = CreateEntity();
m_checkboxContentEntity->AddComponent<NodeComponent>().SetParent(this);
m_checkboxContentEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxContentSprite, 2);
m_textEntity = CreateEntity();
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
m_checkMark = Nz::TextureLibrary::Get("Ndk::CheckboxWidget::checkmark");
SetCheckboxSize({ 32.f, 32.f });
UpdateCheckbox();
}
bool CheckboxWidget::Initialize()
{
const Nz::UInt8 r_checkmark[] =
{
#include <NDK/Resources/checkmark.png.h>
};
Nz::TextureRef checkmarkTexture = Nz::Texture::New();
if (!checkmarkTexture->LoadFromMemory(r_checkmark, sizeof(r_checkmark) / sizeof(r_checkmark[0])))
{
NazaraError("Failed to load embedded checkmark");
return false;
}
Nz::TextureLibrary::Register("Ndk::CheckboxWidget::checkmark", checkmarkTexture);
return true;
}
void CheckboxWidget::Uninitialize()
{
Nz::TextureLibrary::Unregister("Ndk::CheckboxWidget::checkmark");
}
void CheckboxWidget::SetState(CheckboxState state)
{
if (!m_checkboxEnabled)
return;
if (state == CheckboxState_Tristate)
m_tristateEnabled = true;
m_state = state;
UpdateCheckbox();
}
CheckboxState CheckboxWidget::SwitchToNextState()
{
if (!m_checkboxEnabled)
return m_state;
switch (m_state)
{
case CheckboxState_Unchecked:
SetState(CheckboxState_Checked);
break;
case CheckboxState_Checked:
SetState(m_tristateEnabled ? CheckboxState_Tristate : CheckboxState_Unchecked);
break;
case CheckboxState_Tristate:
SetState(CheckboxState_Unchecked);
break;
}
return m_state;
}
void CheckboxWidget::ResizeToContent()
{
Nz::Vector3f textSize = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
Nz::Vector2f checkboxSize = GetCheckboxSize();
Nz::Vector2f finalSize { checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin) + textSize.x, std::max(textSize.y, checkboxSize.y) };
SetContentSize(finalSize);
}
void CheckboxWidget::Layout()
{
BaseWidget::Layout();
Nz::Vector2f origin = GetContentOrigin();
Nz::Vector2f checkboxSize = GetCheckboxSize();
Nz::Vector2f borderSize = GetCheckboxBorderSize();
m_checkboxBorderEntity->GetComponent<NodeComponent>().SetPosition(origin);
m_checkboxBackgroundEntity->GetComponent<NodeComponent>().SetPosition(origin + borderSize);
Nz::Vector3f checkboxBox = m_checkboxContentSprite->GetBoundingVolume().obb.localBox.GetLengths();
m_checkboxContentEntity->GetComponent<NodeComponent>().SetPosition(origin.x + checkboxSize.x / 2.f - checkboxBox.x / 2.f,
origin.y + checkboxSize.y / 2.f - checkboxBox.y / 2.f);
Nz::Vector3f textBox = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
m_textEntity->GetComponent<NodeComponent>().SetPosition(origin.x + checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin),
origin.y + checkboxSize.y / 2.f - textBox.y / 2.f);
}
void CheckboxWidget::OnMouseButtonRelease(int x, int y, Nz::Mouse::Button button)
{
if (button == Nz::Mouse::Left && ContainsCheckbox(x, y) && IsCheckboxEnabled())
{
SwitchToNextState();
OnStateChanged(this);
}
}
void CheckboxWidget::UpdateCheckbox()
{
if (m_checkboxEnabled)
{
m_checkboxBorderSprite->SetColor(s_borderColor);
m_checkboxBackgroundSprite->SetColor(s_backgroundColor);
}
else
{
m_checkboxBorderSprite->SetColor(s_disabledBorderColor);
m_checkboxBackgroundSprite->SetColor(s_disabledBackgroundColor);
}
if (m_state == CheckboxState_Unchecked)
{
m_checkboxContentEntity->Enable(false);
return;
}
else if (m_state == CheckboxState_Checked)
{
m_checkboxContentEntity->Enable();
m_checkboxContentSprite->SetColor(Nz::Color::White);
m_checkboxContentSprite->SetTexture(m_checkMark, false);
}
else // Tristate
{
m_checkboxContentEntity->Enable();
m_checkboxContentSprite->SetColor(Nz::Color::Black);
m_checkboxContentSprite->SetTexture(Nz::TextureRef {});
}
}
}

View File

@ -5,8 +5,14 @@ ACTION.Function = function ()
print("Encoding resources ...")
local startClock = os.clock()
local modules = os.matchdirs("../src/Nazara/*")
table.insert(modules, "../SDK/src/NDK")
for k, modulePath in pairs(modules) do
local moduleName = modulePath:sub(15, -1)
local moduleName
if (modulePath:sub(4, 6) == "src") then
moduleName = modulePath:sub(15, -1)
else
moduleName = "SDK"
end
local files = os.matchfiles(modulePath .. "/Resources/**")
for k, filePath in pairs(files) do
if (filePath:sub(-2) ~= ".h") then