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:
1
SDK/src/NDK/Resources/checkmark.png.h
Normal file
1
SDK/src/NDK/Resources/checkmark.png.h
Normal file
File diff suppressed because one or more lines are too long
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
185
SDK/src/NDK/Widgets/CheckboxWidget.cpp
Normal file
185
SDK/src/NDK/Widgets/CheckboxWidget.cpp
Normal 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 {});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user