From 0ac537bb65bb93c968d0ab8624d3d19fbc7ffccb Mon Sep 17 00:00:00 2001 From: lveyou <1339484752@qq.com> Date: Thu, 26 Oct 2023 21:00:57 +0800 Subject: [PATCH] =?UTF-8?q?*=20ui=E7=B3=BB=E7=BB=9F=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/base/dl_type_convert.h | 25 +++++++++++++++++ dl/render/dl_graphics.cpp | 52 ++++++++++++++++++++++++++++++----- dl/render/dl_graphics.h | 30 ++++++++++++++++---- dl/ui/dl_ui_button_base.h | 1 + dl/ui/dl_ui_button_sprite.cpp | 22 +++++++++++++++ dl/ui/dl_ui_button_sprite.h | 14 ++++++++++ dl/ui/dl_ui_manager.cpp | 32 +++++++++++++++++++++ dl/ui/dl_ui_manager.h | 28 +------------------ dltest/main.cpp | 5 ++++ 9 files changed, 170 insertions(+), 39 deletions(-) diff --git a/dl/base/dl_type_convert.h b/dl/base/dl_type_convert.h index bd7d1d33..8b4cdcaf 100644 --- a/dl/base/dl_type_convert.h +++ b/dl/base/dl_type_convert.h @@ -129,4 +129,29 @@ constexpr std::array toSizeDefault(const std::array return ret; } +/** +* @brief 长度+1 +*/ +template +constexpr std::array toLonger(const std::array& a, const T& v = {}) +{ + std::array ret; + for (int i = 0; i != N; ++i) + ret[i] = a[i]; + ret[N] = v; + return ret; +} +/** +* @brief 长度-1 +*/ +template +constexpr std::array toShorter(const std::array& a) +{ + std::array ret; + for (int i = 0; i != N - 1; ++i) + ret[i] = a[i]; + return ret; +} + + } diff --git a/dl/render/dl_graphics.cpp b/dl/render/dl_graphics.cpp index 2f23fb83..168dfe8c 100644 --- a/dl/render/dl_graphics.cpp +++ b/dl/render/dl_graphics.cpp @@ -1,6 +1,7 @@ #include "dl_graphics.h" #include "vk/dl_vk.h" +#include "math/dl_coor.h" namespace dl { @@ -10,25 +11,62 @@ namespace dl // 汉 void Graphics::Dot(const Position3& p) { - VertexGfx v; - v._pos = p; - v._color = _col; - g_vk->GfxPushDot(v); + std::array v; + v[0]._pos = toShorter(_mat * toLonger(p, 1.0_f)); + v[0]._color = _col[0]; + + g_vk->GfxPushDot(v[0]); } void Graphics::Line(const Position3& p0, const Position3& p1) { - g_vk->GfxPushLine({ p0, _col }, { p1, _col }); + std::array v; + v[0]._pos = toShorter(_mat * toLonger(p0, 1.0_f)); + v[1]._pos = toShorter(_mat * toLonger(p1, 1.0_f)); + v[0]._color = _col[0]; + v[1]._color = _col[1]; + + g_vk->GfxPushLine(v[0], v[1]); } void Graphics::Triangle(const Position3& p0, const Position3& p1, const Position3& p2) { - g_vk->GfxPushTriangle({ p0, _col }, { p1, _col }, { p2, _col }); + std::array v; + v[0]._pos = toShorter(_mat * toLonger(p0, 1.0_f)); + v[1]._pos = toShorter(_mat * toLonger(p1, 1.0_f)); + v[2]._pos = toShorter(_mat * toLonger(p2, 1.0_f)); + v[0]._color = _col[0]; + v[1]._color = _col[1]; + v[2]._color = _col[2]; + g_vk->GfxPushTriangle(v[0], v[1], v[2]); } void Graphics::TriangleSolid(const Position3& p0, const Position3& p1, const Position3& p2) { - g_vk->GfxPushTriangleSolid({ p0, _col }, { p1, _col }, { p2, _col }); + std::array v; + v[0]._pos = toShorter(_mat * toLonger(p0, 1.0_f)); + v[1]._pos = toShorter(_mat * toLonger(p1, 1.0_f)); + v[2]._pos = toShorter(_mat * toLonger(p2, 1.0_f)); + v[0]._color = _col[0]; + v[1]._color = _col[1]; + v[2]._color = _col[2]; + g_vk->GfxPushTriangleSolid(v[0], v[1], v[2]); +} + +void Graphics::RenderCoor(Coor* coor) +{ + SetMatrix(coor); + + Line({}, { 1, 0, 0 }); + Line({}, { 0, 1, 0 }); +} + +void Graphics::SetMatrix(Coor* coor) +{ + Matrix4 m_t = Mat::CreateTranslation(toLonger(coor->GetPosition())); + Matrix4 m_r = Mat::CreateRotation({ 0, 0, 1 }, coor->GetRotate()); + Matrix4 m_s = Mat::CreateScaling(toLonger(coor->GetPosition(), 1.0_f)); + _mat = m_t * m_r * m_s; } } diff --git a/dl/render/dl_graphics.h b/dl/render/dl_graphics.h index d7dbf819..18c69863 100644 --- a/dl/render/dl_graphics.h +++ b/dl/render/dl_graphics.h @@ -16,12 +16,18 @@ namespace dl { +class Coor; + class Graphics { public: - Graphics(): - _col{ColorDef::RED} - {} + Graphics() + { + for (auto& iter : _col) + { + iter = ColorDef::RED; + } + } //-----------------------------基础------------------------------ void Dot(const Position3& p); void Line(const Position3& p0, const Position3& p1); @@ -101,15 +107,29 @@ public: col = { cur - 4.0f, 1.0f, 0.0f, 1.0f }; else col = { 1.0f, 6.0f - cur, 0.0f, 1.0f }; - _col = col; + SetColor(col); ++i; Line(p0, p1); }); } + //! 绘制Coor + void RenderCoor(Coor* coor); + + //------------------------------参数设置-------------------------------- + //! 设置变换矩阵 + void SetMatrix(const Matrix4 mat) { _mat = mat; } + void SetMatrix(Coor* coor); + + //! 设置颜色 + void SetColor(const Color& col) + { + for (auto& iter : _col) + iter = col; + } private: Matrix4 _mat; - Color _col; + std::array _col; }; extern Graphics* g_gfx; diff --git a/dl/ui/dl_ui_button_base.h b/dl/ui/dl_ui_button_base.h index 34edfac6..b154d97d 100644 --- a/dl/ui/dl_ui_button_base.h +++ b/dl/ui/dl_ui_button_base.h @@ -226,5 +226,6 @@ private: }; using ButtonMode = Button::Mode; +using ButtonState = Button::State; } \ No newline at end of file diff --git a/dl/ui/dl_ui_button_sprite.cpp b/dl/ui/dl_ui_button_sprite.cpp index 8ae450d6..8870df10 100644 --- a/dl/ui/dl_ui_button_sprite.cpp +++ b/dl/ui/dl_ui_button_sprite.cpp @@ -126,4 +126,26 @@ void ButtonSprite::_create(const std::vector& mul_spr) _update_coor(); } +void ButtonSprite::SetSpriteQuad(Button::State state, const Quad& quad) +{ + if (state < Button::State::MAX_NUM) + _spr[state]->SetQuad(quad); + else + { + for (auto& spr : _spr) + spr->SetQuad(quad); + } +} + +void ButtonSprite::SetSpriteColor(Button::State state, const Color& color) +{ + if (state < Button::State::MAX_NUM) + _spr[state]->SetColor(color); + else + { + for (auto& spr : _spr) + spr->SetColor(color); + } +} + } \ No newline at end of file diff --git a/dl/ui/dl_ui_button_sprite.h b/dl/ui/dl_ui_button_sprite.h index 584565be..5042d324 100644 --- a/dl/ui/dl_ui_button_sprite.h +++ b/dl/ui/dl_ui_button_sprite.h @@ -48,6 +48,20 @@ public: void SetString(std::string_view str); + /** + * @brief 设置精灵quad + * @param[in] state 状态,传入MAX_NUM表示所有 + * @param[in] quad 大小 + * @note 会拉伸图像 + */ + void SetSpriteQuad(Button::State state, const Quad& quad); + /** + * @brief 设置精灵颜色 + * @param[in] state 状态,传入MAX_NUM表示所有 + * @param[in] quad 大小 + * @note 会拉伸图像 + */ + void SetSpriteColor(Button::State state, const Color& color); virtual bool IsPickup() override { diff --git a/dl/ui/dl_ui_manager.cpp b/dl/ui/dl_ui_manager.cpp index b5d74a9c..81d64e2b 100644 --- a/dl/ui/dl_ui_manager.cpp +++ b/dl/ui/dl_ui_manager.cpp @@ -2,6 +2,7 @@ #include "io/dl_file.h" #include "dl_string.h" +#include "render/dl_graphics.h" namespace dl::UI { @@ -156,6 +157,11 @@ ListBox* Manager::CreateListBoxDefault() { ButtonSprite* btn = g_ui->CreateButtonSpriteDefault(); btn->SetMode(ButtonMode::SWITCH); + btn->SetSpriteQuad(ButtonState::MAX_NUM, createQuad(Size{ 200, 16 }, false)); + btn->SetSpriteColor(ButtonState::NORMAL, ColorDef::GRAY); + btn->SetSpriteColor(ButtonState::OVER, ColorDef::GREEN); + btn->SetSpriteColor(ButtonState::DOWN, ColorDef::BLUE); + panel->Add(btn->GetCpt()); } ListBox* ret = ListBox::Create(panel); @@ -498,6 +504,32 @@ void Manager::_resize_panel_root() _panelRoot->SetBackgroundSize(toSizeNeg2Zero(toPosition(size) / _uiScale)); } +void Manager::RenderDebug() +{ + //_mulPriCoor.Clear(); + //_mulPriQuad.Clear(); + // + for (auto& node : _allPanel) + { + Panel* panel = node->GetValue(); + if (!panel->IsRender()) + continue; + g_gfx->RenderCoor(panel->_coor); + + //组件处理 + for (Components* cpts : panel->_ele) + { + if (Cpt::ElementData* comp = cpts->Get()) + { + g_gfx->SetMatrix(panel->_coor); + g_gfx->Rectangle( + toLonger(comp->GetOffset()), + toLonger(comp->GetOffset() + comp->GetSize())); + } + } + } +} + } diff --git a/dl/ui/dl_ui_manager.h b/dl/ui/dl_ui_manager.h index 3f4e3387..0b584bc5 100644 --- a/dl/ui/dl_ui_manager.h +++ b/dl/ui/dl_ui_manager.h @@ -180,33 +180,7 @@ public: /** * @brief 绘制调试信息 */ - void RenderDebug() - { - //_mulPriCoor.Clear(); - //_mulPriQuad.Clear(); - // - for (auto& node : _allPanel) - { - Panel* panel = node->GetValue(); - if (!panel->IsRender()) - continue; - //Panel Coor - //Primitive::Coor* pri_coor = _mulPriCoor.Get(panel->_coor); - //pri_coor->Render(); - ////组件处理 - //for (Components* cpts : panel->_ele) - //{ - // if (Cpt::ElementData* cpt = cpts->Get()) - // { - // Primitive::QuadLine* pri_quad = _mulPriQuad.Get(); - // pri_quad->GetCoor()->SetParent(panel->_coor); - // pri_quad->Set(createQuad(cpt->_size, false) + cpt->GetOffset()); - // pri_quad->SetColor(ColorDef::PURPLE); - // pri_quad->Render(); - // } - //} - } - } + void RenderDebug(); ~Manager() { diff --git a/dltest/main.cpp b/dltest/main.cpp index 24f5f778..ae1cdbbb 100644 --- a/dltest/main.cpp +++ b/dltest/main.cpp @@ -103,6 +103,11 @@ void Run() //g_gfx->Love(); //base->spr9->Render(); + + if (g_input->KeyState(KeyCode::F1)) + { + g_ui->RenderDebug(); + } } void Release()