#include "dl_ui_background.h" #ifndef DL_DISABLE_RENDER #include "render/dl_graphics.h" #include "ui/dl_ui_manager.h" #include "system/dl_system.h" namespace dl::UI { void Background::RefreshSize() { Position size = _cptElemData._size; if (_bWindowW) size.w = g_system->GetWindowSize().w / g_ui->GetScale(); if (_bWindowH) size.h = g_system->GetWindowSize().h / g_ui->GetScale(); size += _sizeExtra; switch (_ui._type) { case Type::SPRITE: _ui._spr->SetQuad(createQuad(size, false) + _cptElemData._pos); break; case Type::SPRITE9: _ui._spr9->SetRect(createRect(toPoint(_cptElemData._pos), toSize(size))); break; default: break; } } dl::UI::Background* Background::Clone() { Background* ret = new Background; ret->_coor = _coor->Clone(); switch (_ui._type) { case Type::SPRITE: ret->_ui._spr = g_factory->CloneSprite(_ui._spr); ret->_ui._spr->SetCoor(ret->_coor); break; case Type::SPRITE9: ret->_ui._spr9 = _ui._spr9->Clone(); ret->_ui._spr9->GetCoor()->SetParent(ret->_coor); break; default: break; } ret->_ui._type = _ui._type; return ret; } void Background::Set(gcSprite spr) { Clear(); _ui._spr = spr; _ui._type = Type::SPRITE; _ui._spr->SetUI(true); _ui._spr->SetCoor(_coor); _cptElemData._size = _ui._spr->GetSize(); } void Background::Set(Sprite9* spr9) { Clear(); _ui._spr9 = spr9; _ui._type = Type::SPRITE9; _ui._spr9->SetUI(true); _ui._spr9->GetCoor()->SetParent(_coor); _cptElemData._size = toPosition(_ui._spr9->GetSize()); } void Background::Clear() { switch (_ui._type) { case Type::SPRITE: break; case Type::SPRITE9: delete _ui._spr9; _ui._spr9 = nullptr; break; default: break; } _ui._type = Type::NONE; } bool Background::IsPickup() { switch (_ui._type) { case Type::SPRITE: return _ui._spr->IsPickup(); case Type::SPRITE9: return _ui._spr9->IsPickup(); default: return false; } } void Background::Render() { switch (_ui._type) { case Type::SPRITE: _ui._spr->Render(); break; case Type::SPRITE9: _ui._spr9->Render(); break; default: break; } if (_cbRender) _cbRender(this); } void Background::SetOrder(float order) { switch (_ui._type) { case Type::SPRITE: _ui._spr->SetOrder(order); break; case Type::SPRITE9: _ui._spr9->SetOrder(order); break; default: break; } } void Background::SetUI(bool ui, bool change_sampler) { switch (_ui._type) { case Type::SPRITE: _ui._spr->SetUI(ui, change_sampler); break; case Type::SPRITE9: _ui._spr9->SetUI(ui, change_sampler); break; default: break; } } void Background::SetOffset(const Position& offset) { _cptElemData._pos = offset; RefreshSize(); } Position Background::GetOffset() { return _cptElemData._pos; } void Background::SetSize(const Position& size) { _cptElemData._size = size; RefreshSize(); } void Background::SetSizeExtra(const Position& size) { _sizeExtra = size; RefreshSize(); } Position Background::GetSize() { return _cptElemData._size; } void Background::RenderDebug() { { Position p0 = _cptElemData._pos; Position p1 = p0 + GetSize(); g_gfx->SetCoor(_coor); g_gfx->SetColor(createColor(ColorDef::PURPLE, 1.0f)); g_gfx->Rectangle2D(p0, p1); } } dl::Position Background::GetSizeReal() { switch (_ui._type) { case Type::SPRITE: return _ui._spr->GetSize(); case Type::SPRITE9: return toPosition(_ui._spr9->GetSize()); default: break; } return {}; } float Background::GetOrder() { switch (_ui._type) { case Type::SPRITE: return _ui._spr->GetOrder(); case Type::SPRITE9: return _ui._spr9->GetOrder(); default: break; } return 0; } void Background::SetColor(const Color& col) { switch (_ui._type) { case Type::SPRITE: return _ui._spr->SetColor(col); case Type::SPRITE9: return _ui._spr9->SetColor(col); default: break; } } } #endif