// Copyright (c) 2022 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "UICanvas.hpp" #include "scene/Scene.hpp" #include "ui/UIComponent.hpp" #include "game/DawnGame.hpp" using namespace Dawn; UICanvas * UICanvas::create(Scene *scene) { auto item = scene->createSceneItem(); return item->addComponent(); } UICanvas::UICanvas(SceneItem *item) : SceneItemComponent(item) { } void UICanvas::onRenderTargetResize(float_t w, float_t h){ auto it = this->children.begin(); while(it != this->children.end()) { (*it)->updatePositions(); ++it; } } void UICanvas::setCamera(Camera *camera) { assertTrue(camera != this->camera); if(this->camera != nullptr) { this->camera->eventRenderTargetResized.removeListener( this, &UICanvas::onRenderTargetResize ); } this->camera = camera; if(this->camera != nullptr) { this->camera->eventRenderTargetResized.addListener( this, &UICanvas::onRenderTargetResize ); } } float_t UICanvas::getWidth() { if(this->camera == nullptr) { return this->getGame()->renderManager.getBackBuffer()->getWidth(); } return this->camera->getRenderTarget()->getWidth(); } float_t UICanvas::getHeight() { if(this->camera == nullptr) { return this->getGame()->renderManager.getBackBuffer()->getHeight(); } return this->camera->getRenderTarget()->getHeight(); } void UICanvas::onStart() { if(this->camera == nullptr) { auto camera = this->getScene()->findComponent(); this->setCamera(camera); } } void UICanvas::onDispose() { if(this->camera != nullptr) { this->camera->eventRenderTargetResized.removeListener( this, &UICanvas::onRenderTargetResize ); } auto it = this->children.begin(); while(it != this->children.end()) { delete *it; ++it; } }