pubnix/lib/neovision/src/window.cpp

98 lines
2.3 KiB
C++

#include <cstdint>
#include <memory>
#include "neovision/application.h"
#include "neovision/window.h"
namespace neovision {
Window::Window(): View()
{
}
void Window::Initialize()
{
View::Initialize();
/* If we have an application, we try to set our size to something reasonable
to begin with - otherwise all windows would default to {0,0} and be
invisible by default. */
const std::shared_ptr<Application> app = TheApp.lock();
if (app == nullptr) return; // No application instantiated yet.
const Vector2D appSize = app->Size();
const std::uint32_t sX = appSize.X() / 2;
const std::uint32_t sY = appSize.Y() / 2;
this->Size({sX, sY});
}
void Window::OnDraw()
{
const Vector2D s = Size();
const std::uint32_t x1{0};
const std::uint32_t y1{0};
const std::uint32_t x2{s.X()};
const std::uint32_t y2{s.Y()};
// Top border
for (std::uint32_t x = (x1 + 1); x < x2; ++x)
{
WriteAt({x, y1}, L"");
}
// Bottom border
for (std::uint32_t x = (x1 + 1); x < x2; ++x)
{
WriteAt({x, y2}, L"");
}
// Left border
for (std::uint32_t y = (y1 + 1); y < y2; ++y)
{
WriteAt({x1, y}, L"");
}
// Right border
for (std::uint32_t y = (y1 + 1); y < y2; ++y)
{
WriteAt({x2, y}, L"");
}
// Corners
WriteAt({x1, y1}, L"");
WriteAt({x2, y1}, L"");
WriteAt({x1, y2}, L"");
WriteAt({x2, y2}, L"");
// Close button
WriteAt({x1 + 2, y1}, L"[■]");
// Maximize button
WriteAt({x2 - 4, y1}, L"[↕]");
// Title
const std::wstring titleStr = L" " + m_title + L" ";
const float winMid = float(s.X()) / 2.0f;
const float titleMid = float(titleStr.length()) / 2.0f;
const std::uint32_t titlePos = winMid - titleMid;
WriteAt({titlePos, y1}, titleStr);
// Contents
for (std::uint32_t y = y1 + 1; y < y2 ; ++y)
{
const std::wstring line(x2 - x1 - 1, L' ');
WriteAt({x1 + 1, y}, line);
}
WriteAt({x1 + 1, y1 + 1}, m_content);
View::OnDraw();
}
void Window::OnMouse(const MouseEventData&)
{
m_content = L"Click!";
SetDirty();
}
std::wstring Window::Title() const
{
return m_title;
}
void Window::Title(const std::wstring& title)
{
m_title = title;
}
} // namespace neovision