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;
|
||||
|
@ -20,13 +20,14 @@ namespace Dawn {
|
||||
|
||||
class UICanvas : public SceneItemComponent {
|
||||
protected:
|
||||
Camera *camera = nullptr;
|
||||
struct UIMenu *currentMenu = nullptr;
|
||||
std::function<void()> evtCamResize;
|
||||
|
||||
void onRenderTargetResize(float_t w, float_t h);
|
||||
void onSceneUpdate();
|
||||
|
||||
public:
|
||||
StateProperty<struct UIMenu*> currentMenu;
|
||||
StateProperty<Camera*> camera;
|
||||
|
||||
/**
|
||||
* Creates a UI Canvas Scene Item Element, and attaches it to the provided
|
||||
* scene.
|
||||
@ -47,13 +48,6 @@ namespace Dawn {
|
||||
*/
|
||||
UICanvas(SceneItem *item);
|
||||
|
||||
/**
|
||||
* Sets the camera used by the UI canvas.
|
||||
*
|
||||
* @param camera Camera to set for the UI canvas.
|
||||
*/
|
||||
void setCamera(Camera *camera);
|
||||
|
||||
/**
|
||||
* Construct and append a UI item to this UI Canvas.
|
||||
*
|
||||
|
Reference in New Issue
Block a user