Updated more scene item coponents to state system
This commit is contained in:
@ -15,39 +15,11 @@ UICanvas * UICanvas::create(Scene *scene) {
|
||||
return item->addComponent<UICanvas>();
|
||||
}
|
||||
|
||||
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::onSceneUpdate() {
|
||||
if(this->currentMenu != nullptr) {
|
||||
this->currentMenu->onTick();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
UICanvas::UICanvas(SceneItem *item) :
|
||||
SceneItemComponent(item),
|
||||
camera(nullptr),
|
||||
currentMenu(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
float_t UICanvas::getWidth() {
|
||||
@ -64,34 +36,47 @@ float_t UICanvas::getHeight() {
|
||||
return this->camera->getRenderTarget()->getHeight();
|
||||
}
|
||||
|
||||
struct UIMenu * UICanvas::getCurrentMenu() {
|
||||
return this->currentMenu;
|
||||
}
|
||||
|
||||
void UICanvas::setCurrentMenu(struct UIMenu *menu) {
|
||||
if(this->currentMenu != nullptr) this->currentMenu->onInactive();
|
||||
this->currentMenu = menu;
|
||||
if(menu != nullptr) menu->onActive();
|
||||
}
|
||||
|
||||
void UICanvas::onStart() {
|
||||
if(this->camera == nullptr) {
|
||||
auto camera = this->getScene()->findComponent<Camera>();
|
||||
this->setCamera(camera);
|
||||
}
|
||||
useEffectWithTeardown([&]{
|
||||
if(this->camera == nullptr) return evtCamResize = [&]{};
|
||||
|
||||
auto it = this->children.begin();
|
||||
while(it != this->children.end()) {
|
||||
(*it)->updatePositions();
|
||||
++it;
|
||||
}
|
||||
|
||||
this->getScene()->eventSceneUpdate.addListener(this, &UICanvas::onSceneUpdate);
|
||||
return evtCamResize = useEvent([&](float_t w, float_t h){
|
||||
auto it = this->children.begin();
|
||||
while(it != this->children.end()) {
|
||||
(*it)->updatePositions();
|
||||
++it;
|
||||
}
|
||||
}, camera->event2RenderTargetResized);
|
||||
}, camera);
|
||||
|
||||
useEffectWithTeardown([&]{
|
||||
if(currentMenu != nullptr) currentMenu->onActive();
|
||||
|
||||
return [&] {
|
||||
if(currentMenu == nullptr) currentMenu->onInactive();
|
||||
};
|
||||
}, this->currentMenu);
|
||||
|
||||
// Scene Update
|
||||
useEventLegacy([&]{
|
||||
if(this->currentMenu == nullptr) return;
|
||||
this->currentMenu->onTick();
|
||||
}, getScene()->eventSceneUpdate);
|
||||
|
||||
// Find Camera if we need to.
|
||||
if(camera == nullptr) camera = this->getScene()->findComponent<Camera>();
|
||||
}
|
||||
|
||||
void UICanvas::onDispose() {
|
||||
if(this->camera != nullptr) {
|
||||
this->camera->eventRenderTargetResized.removeListener(
|
||||
this, &UICanvas::onRenderTargetResize
|
||||
);
|
||||
}
|
||||
|
||||
this->getScene()->eventSceneUpdate.removeListener(this, &UICanvas::onSceneUpdate);
|
||||
|
||||
auto it = this->children.begin();
|
||||
while(it != this->children.end()) {
|
||||
delete *it;
|
||||
|
Reference in New Issue
Block a user