// Copyright (c) 2022 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "UICanvas.hpp" #include "game/DawnGame.hpp" #include "UIComponent.hpp" using namespace Dawn; UICanvas * UICanvas::create(Scene *scene) { auto item = scene->createSceneItem(); return item->addComponent(); } UICanvas::UICanvas(SceneItem *item) : SceneItemComponent(item), camera(nullptr) { } float_t UICanvas::getWidth() { return w; } float_t UICanvas::getHeight() { return h; } float_t UICanvas::getContentWidth() { return this->getWidth(); } float_t UICanvas::getContentHeight() { return this->getHeight(); } void UICanvas::getProjectionAndView(glm::mat4 *proj, glm::mat4 *view) { assertNotNull(proj); assertNotNull(view); switch(this->drawType) { case UI_DRAW_TYPE_WORLD_ABSOLUTE: *proj = camera->getProjection(); *view = camera->transform->getWorldTransform(); break; case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE: *proj = glm::ortho( 0.0f, camera->getRenderTarget()->getWidth(), camera->getRenderTarget()->getHeight(), 0.0f ); *view = glm::mat4(1.0f); break; default: assertUnreachable(); } } void UICanvas::onStart() { if(camera == nullptr) camera = getScene()->findComponent(); useEffectWithTeardown([&]{ if(camera == nullptr) return evtRenderResize = [&] {}; this->w = camera->getRenderTarget()->getWidth(); this->h = camera->getRenderTarget()->getHeight(); return evtRenderResize = useEvent([&](float_t w, float_t h){ this->w = w; this->h = h; auto comps = this->item->findChildren(); auto itComps = comps.begin(); while(itComps != comps.end()) { (*itComps)->alignmentNeedsUpdating = true; ++itComps; } }, camera->eventRenderTargetResized); }, camera)(); } void UICanvas::onDispose() { }