Sdk/Widgets: Add TextAreaWidget (experimental)
This commit is contained in:
parent
7df732b927
commit
3d79db4c52
|
|
@ -1,4 +1,4 @@
|
|||
// This file was automatically generated on 27 Oct 2016 at 18:53:37
|
||||
// This file was automatically generated on 27 Oct 2016 at 21:39:00
|
||||
|
||||
#pragma once
|
||||
|
||||
|
|
@ -7,5 +7,6 @@
|
|||
|
||||
#include <NDK/Widgets/ButtonWidget.hpp>
|
||||
#include <NDK/Widgets/LabelWidget.hpp>
|
||||
#include <NDK/Widgets/TextAreaWidget.hpp>
|
||||
|
||||
#endif // NDK_WIDGETS_GLOBAL_HPP
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
// 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_WIDGETS_TEXTAREAWIDGET_HPP
|
||||
#define NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
||||
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
#include <NDK/BaseWidget.hpp>
|
||||
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
||||
#include <Nazara/Graphics/TextSprite.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class World;
|
||||
|
||||
class NDK_API TextAreaWidget : public BaseWidget
|
||||
{
|
||||
public:
|
||||
TextAreaWidget(BaseWidget* parent = nullptr);
|
||||
TextAreaWidget(const TextAreaWidget&) = delete;
|
||||
TextAreaWidget(TextAreaWidget&&) = default;
|
||||
~TextAreaWidget() = default;
|
||||
|
||||
void AppendText(const Nz::String& text);
|
||||
|
||||
//virtual TextAreaWidget* Clone() const = 0;
|
||||
|
||||
std::size_t GetHoveredGlyph(float x, float y) const;
|
||||
|
||||
void ResizeToContent() override;
|
||||
|
||||
void SetText(const Nz::String& text);
|
||||
|
||||
TextAreaWidget& operator=(const TextAreaWidget&) = delete;
|
||||
TextAreaWidget& operator=(TextAreaWidget&&) = default;
|
||||
|
||||
private:
|
||||
void RefreshCursor();
|
||||
|
||||
void OnMouseEnter() override;
|
||||
void OnMouseButtonPress(int /*x*/, int /*y*/, Nz::Mouse::Button button) override;
|
||||
void OnMouseMoved(int x, int y, int deltaX, int deltaY) override;
|
||||
void OnMouseExit() override;
|
||||
void OnTextEntered(char32_t character, bool repeated) override;
|
||||
|
||||
void Write(const Nz::String& text);
|
||||
|
||||
EntityHandle m_cursorEntity;
|
||||
EntityHandle m_textEntity;
|
||||
Nz::SimpleTextDrawer m_drawer;
|
||||
Nz::SpriteRef m_cursorSprite;
|
||||
Nz::TextSpriteRef m_textSprite;
|
||||
std::size_t m_cursorPosition;
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Widgets/TextAreaWidget.inl>
|
||||
|
||||
#endif // NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
// 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 <NDK/Widgets/TextAreaWidget.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,176 @@
|
|||
// 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 <NDK/Widgets/TextAreaWidget.hpp>
|
||||
#include <Nazara/Core/Unicode.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <limits>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
TextAreaWidget::TextAreaWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent),
|
||||
m_cursorPosition(0U)
|
||||
{
|
||||
m_cursorSprite = Nz::Sprite::New();
|
||||
m_cursorSprite->SetColor(Nz::Color(192, 192, 192));
|
||||
m_cursorSprite->SetSize(1.f, m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight);
|
||||
|
||||
m_cursorEntity = CreateEntity();
|
||||
m_cursorEntity->AddComponent<GraphicsComponent>().Attach(m_cursorSprite, 10);
|
||||
m_cursorEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_cursorEntity->Enable(false);
|
||||
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
void TextAreaWidget::AppendText(const Nz::String& text)
|
||||
{
|
||||
m_drawer.AppendText(text);
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
}
|
||||
|
||||
std::size_t TextAreaWidget::GetHoveredGlyph(float x, float y) const
|
||||
{
|
||||
std::size_t glyphCount = m_drawer.GetGlyphCount();
|
||||
if (glyphCount > 0)
|
||||
{
|
||||
std::size_t lineCount = m_drawer.GetLineCount();
|
||||
std::size_t line = 0U;
|
||||
for (; line < lineCount - 1; ++line)
|
||||
{
|
||||
Nz::Rectf lineBounds = m_drawer.GetLine(line).bounds;
|
||||
if (lineBounds.GetMaximum().y > y)
|
||||
break;
|
||||
}
|
||||
|
||||
std::size_t upperLimit = (line != lineCount - 1) ? m_drawer.GetLine(line + 1).glyphIndex : glyphCount;
|
||||
upperLimit = std::min(upperLimit, glyphCount);
|
||||
|
||||
std::size_t i = m_drawer.GetLine(line).glyphIndex;
|
||||
for (; i < upperLimit - 1; ++i)
|
||||
{
|
||||
Nz::Rectf bounds = m_drawer.GetGlyph(i).bounds;
|
||||
if (x < bounds.x + bounds.width)
|
||||
break;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void TextAreaWidget::ResizeToContent()
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
}
|
||||
|
||||
void TextAreaWidget::SetText(const Nz::String& text)
|
||||
{
|
||||
m_drawer.SetText(text);
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
}
|
||||
|
||||
void TextAreaWidget::RefreshCursor()
|
||||
{
|
||||
std::size_t lineCount = m_drawer.GetLineCount();
|
||||
std::size_t line = 0U;
|
||||
for (std::size_t i = line + 1; i < lineCount; ++i)
|
||||
{
|
||||
if (m_drawer.GetLine(i).glyphIndex > m_cursorPosition)
|
||||
break;
|
||||
|
||||
line = i;
|
||||
}
|
||||
|
||||
Nz::Rectf bounds = m_drawer.GetGlyph(m_cursorPosition).bounds;
|
||||
m_cursorEntity->GetComponent<Ndk::NodeComponent>().SetPosition(bounds.x, m_drawer.GetLine(line).bounds.y);
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnMouseEnter()
|
||||
{
|
||||
m_cursorEntity->Enable(true);
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnMouseButtonPress(int x, int y, Nz::Mouse::Button button)
|
||||
{
|
||||
if (button == Nz::Mouse::Left)
|
||||
{
|
||||
GrabKeyboard();
|
||||
|
||||
m_cursorPosition = GetHoveredGlyph(x, y);
|
||||
RefreshCursor();
|
||||
}
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnMouseMoved(int x, int y, int deltaX, int deltaY)
|
||||
{
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnMouseExit()
|
||||
{
|
||||
m_cursorEntity->Enable(false);
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnTextEntered(char32_t character, bool /*repeated*/)
|
||||
{
|
||||
switch (character)
|
||||
{
|
||||
case '\b':
|
||||
{
|
||||
Nz::String text = m_drawer.GetText();
|
||||
|
||||
text.Resize(-1, Nz::String::HandleUtf8);
|
||||
m_drawer.SetText(text);
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
break;
|
||||
}
|
||||
|
||||
case '\r':
|
||||
case '\n':
|
||||
Write(Nz::String('\n'));
|
||||
break;
|
||||
|
||||
default:
|
||||
{
|
||||
if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control)
|
||||
break;
|
||||
|
||||
Write(Nz::String::Unicode(character));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextAreaWidget::Write(const Nz::String& text)
|
||||
{
|
||||
if (m_cursorPosition >= m_drawer.GetGlyphCount())
|
||||
{
|
||||
AppendText(text);
|
||||
m_cursorPosition = m_drawer.GetGlyphCount();
|
||||
}
|
||||
else
|
||||
{
|
||||
Nz::String currentText = m_drawer.GetText();
|
||||
currentText.Insert(m_cursorPosition, text);
|
||||
SetText(currentText);
|
||||
|
||||
m_cursorPosition += text.GetSize();
|
||||
}
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue