Skip to content

Commit

Permalink
Fix dropdown crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianensis committed Jan 28, 2021
1 parent 18dff79 commit 0dfcfab
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 99 deletions.
2 changes: 1 addition & 1 deletion code/Core/DE_Macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace DE {
DE_GENERATE_NAME_VIRTUAL(Class);\
DE_GENERATE_ID_STATIC(Class);\
DE_GENERATE_ID_VIRTUAL(Class);\
DE_GENERATE_DYNAMIC_DESTRUCTOR_VIRTUAL(Class);
DE_GENERATE_DYNAMIC_DESTRUCTOR_VIRTUAL(Class);

#define DE_CLASS(Class)\
Class();\
Expand Down
71 changes: 10 additions & 61 deletions code/Scene/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,7 @@ GameObject::GameObject() : DE_Class() {

GameObject::~GameObject() {
DE_FREE(mComponentsMap);
DE_FREE(mCacheComponentsFirst);
DE_FREE(mTransform);
DE_FREE(mCacheComponentsLists);
}

void GameObject::setCacheLists(ClassId classId, List<Component*>* components) {
mLastClassIdList = classId;
mCacheComponentsLists->set(classId, components);
}

void GameObject::setCacheFirst(ClassId classId, Component* component) {
mLastClassIdFirst = classId;
mCacheComponentsFirst->set(classId, component);
}

void GameObject::tryCleanCache(ClassId classId) {
if(mLastClassIdFirst == classId) {
mCacheComponentsFirst->set(classId, nullptr);
}

if(mLastClassIdList == classId) {
mCacheComponentsLists->set(classId, nullptr);
}
}

bool GameObject::checkCacheFirst(ClassId classId) {
return mLastClassIdFirst == classId && mCacheComponentsFirst->get(classId);
}

bool GameObject::checkCacheLists(ClassId classId) {
return mLastClassIdList == classId && mCacheComponentsLists->contains(classId);
}

void GameObject::addComponent(Component *component, ClassId classId) {
Expand All @@ -70,17 +40,15 @@ void GameObject::addComponent(Component *component, ClassId classId) {

component->setGameObject(this);
component->init();

tryCleanCache(classId);
}

void GameObject::removeComponent(Component *component, ClassId classId) {
if (mComponentsMap->contains(classId) && ! component->getIsPendingToBeDestroyed() && ! component->getIsDestroyed()) {
List<Component*>* list = mComponentsMap->get(classId);
list->remove(list->find(component));
component->destroy();

tryCleanCache(classId);
if (!(component->getIsDestroyed() || component->getIsPendingToBeDestroyed())) {
component->destroy();
}
}
}

Expand All @@ -90,12 +58,6 @@ void GameObject::init() {
mComponentsMap = DE_NEW<ComponentsMap>();
mComponentsMap->init();

mCacheComponentsFirst = DE_NEW<CacheComponentsMap>();
mCacheComponentsFirst->init();

mCacheComponentsLists = DE_NEW<ComponentsMap>();
mCacheComponentsLists->init();

mTransform = DE_NEW<Transform>();
addComponent(mTransform);

Expand All @@ -105,38 +67,25 @@ void GameObject::init() {
List<Component*>* GameObject::getComponents(ClassId classId) {
List<Component*>* components = nullptr;

if(checkCacheLists(classId)) {
components = mCacheComponentsLists->get(classId);
} else {
if(mComponentsMap->contains(classId)) {
components = mComponentsMap->get(classId);
if(mComponentsMap->contains(classId)) {
components = mComponentsMap->get(classId);

if(components->isEmpty()){
components = nullptr;
}
if(components->isEmpty()){
components = nullptr;
}
}

setCacheLists(classId, components);

return components;
}

Component* GameObject::getFirstComponent(ClassId classId) {
Component* component = nullptr;
List<Component*>* components = getComponents(classId);

if(checkCacheFirst(classId)) {
component = mCacheComponentsFirst->get(classId);
} else {
List<Component*>* components = getComponents(classId);

if(components && !components->isEmpty()) {
component = components->getFirst().get();
}
if(components && !components->isEmpty()) {
component = components->getFirst().get();
}

setCacheFirst(classId, component);

return component;
}

Expand Down
15 changes: 1 addition & 14 deletions code/Scene/GameObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ class GameObject: public DE_Class {
using ComponentsMap = HashMap<ClassId, List<Component*>*>;
DE_M(ComponentsMap, ComponentsMap*)

DE_M(LastClassIdFirst, ClassId)
using CacheComponentsMap = HashMap<ClassId, Component*>;
DE_M(CacheComponentsFirst, CacheComponentsMap*)

DE_M(LastClassIdList, ClassId)
DE_M(CacheComponentsLists, ComponentsMap*)

void setCacheLists(ClassId classId, List<Component*>* components);
void setCacheFirst(ClassId classId, Component* component);
void tryCleanCache(ClassId classId);
bool checkCacheFirst(ClassId classId);
bool checkCacheLists(ClassId classId);

List<Component*>* getComponents(ClassId classId);
Component* getFirstComponent(ClassId classId);

Expand Down Expand Up @@ -77,7 +64,7 @@ class GameObject: public DE_Class {
}

bool isActive() const {
return mIsDestroyed || mIsPendingToBeDestroyed ? false : mIsActive;
return (mIsDestroyed || mIsPendingToBeDestroyed) ? false : mIsActive;
};

void setIsActive(bool isActive);
Expand Down
2 changes: 1 addition & 1 deletion code/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void Scene::updateComponents(GameObject *gameObject) {

void Scene::removeGameObject(GameObject *gameObject) {

if (!gameObject->getIsDestroyed()) {
if (!(gameObject->getIsDestroyed() || gameObject->getIsPendingToBeDestroyed())) {
auto it = mGameObjects->find(gameObject);
mGameObjects->remove(it);

Expand Down
9 changes: 5 additions & 4 deletions code/UI/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "Graphics/RenderContext.hpp"
#include "Containers/List.hpp"
#include "Containers/HashMap.hpp"
#include "Events/EventsManager.hpp"
#include "Scene/GameObject.hpp"

namespace DE {

Expand Down Expand Up @@ -220,10 +222,9 @@ Vector2 UI::getCharTextureCoordinates(c8 character) {
return mCharMap->get(character);
}

void UI::internalRemoveUIElement(const Iterator *it) {
/*auto castedIt = it->cast<UIElement*>();
mUIElements->remove(*castedIt);*/
}
void UI::setFocusedElement(UIElement* focusedElement) {
mFocusedElement = focusedElement;
};

void UI::terminate() {
DE_TRACE()
Expand Down
4 changes: 1 addition & 3 deletions code/UI/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class UI: public DE_Class, public Singleton<UI> {

Vector2 mDefaultFontSize = Vector2(0.04f / 2.0f, 0.04f);

void internalRemoveUIElement(const Iterator *it);

UIBuilder* mUIBuilder;

UIElement* mFocusedElement = nullptr;
Expand Down Expand Up @@ -79,7 +77,7 @@ class UI: public DE_Class, public Singleton<UI> {
// focus

UIElement* getFocusedElement() const { return mFocusedElement; };
void setFocusedElement(UIElement* focusedElement) { mFocusedElement = focusedElement; };
void setFocusedElement(UIElement* focusedElement);
};

}
Expand Down
65 changes: 57 additions & 8 deletions code/UI/UIDropdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ void UIDropdown::init() {

mEntries = DE_NEW<List<UIDropdownEntry>>();
mEntries->init();

setOnFocusLostCallback([this](UIElement* uiElement){
setEntriesVisibility(false);
});
}

void UIDropdown::onDestroy() {
Expand All @@ -57,6 +61,7 @@ UIDropdown* UIDropdown::addOption(const std::string& label, UIElementCallback on

void UIDropdown::toggle() {

// TODO : Temporary
if(mButtons->isEmpty()){
FOR_LIST(it, mEntries) {

Expand Down Expand Up @@ -86,18 +91,62 @@ void UIDropdown::toggle() {

mButtons->pushBack(button);
}
}
else {
FOR_LIST(it, mButtons){
getScene()->removeGameObject(it.get());
}

mButtons->clear();
setEntriesVisibility(false);
}

/*FOR_LIST(it, mButtons){
FOR_LIST(it, mButtons){
it.get()->setVisibility(!it.get()->isVisible());
}*/
}

// TODO : If I want to create-remove buttons, I have to implement TIMER NEXT FRAME!
//setEntriesVisibility(mButtons->isEmpty());
}

void UIDropdown::setEntriesVisibility(bool visible){
/*if(visible){
FOR_LIST(it, mEntries) {
const std::string& label = it.get().mLabel;
UIElementCallback onPressedCallback = it.get().mCallback;
Vector3 scale = getTransform()->getScale();
scale.x = scale.x * RenderContext::getAspectRatio();
UI::getInstance()->getBuilder()->saveData()->
setPosition(Vector2(-scale.x/2.0f,-scale.y * mButtons->getLength() - scale.y/2.0f))->
setSize(scale)->
setText(label)->
setAdjustSizeToText(true)->
setLayer(getRenderer()->getLayer() + 1)->
setIsAffectedByLayout(false)->
create(UIElementType::BUTTON);
UIButton* button = (UIButton*) UI::getInstance()->getBuilder()->getUIElement();
button->setOnPressedCallback(onPressedCallback);
//button->setVisibility(false);
Transform* t = button->getTransform();
t->setParent(getTransform());
UI::getInstance()->getBuilder()->restoreData();
mButtons->pushBack(button);
}
}
else {
if(!mButtons->isEmpty()){
FOR_LIST(it, mButtons){
getScene()->removeGameObject(it.get());
}
mButtons->clear();
}
}*/

FOR_LIST(it, mButtons){
it.get()->setVisibility(visible);
}
}

}
2 changes: 2 additions & 0 deletions code/UI/UIDropdown.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class UIDropdown: public UIButton {
List<UIButton*>* mButtons;
List<UIDropdownEntry>* mEntries;

void setEntriesVisibility(bool visible);

public:

DE_CLASS(UIDropdown)
Expand Down
7 changes: 3 additions & 4 deletions code/UI/UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ void UIElement::onChar(c8 character) {

void UIElement::onFocusLost() {
if(hasFocus()){
mOnFocusLostFunctor.execute();
UI::getInstance()->setFocusedElement(nullptr);
mOnFocusLostFunctor.execute();
}
}

Expand All @@ -135,7 +135,7 @@ void UIElement::onPressed() {
Collider* collider = getCollider();
// TODO : boolean to enable or disable : can be pressed?

if (collider){
if (collider && collider->isActive()){

Vector2 screenMousePosition(Input::getInstance()->getMousePosition());
Vector2 worldMousePosition = Vector2(
Expand Down Expand Up @@ -192,12 +192,11 @@ void UIElement::setComponentsCache() {
}

void UIElement::setVisibility(bool visibility) {
getRenderer()->setIsActive(visibility);
setIsActive(visibility);
}

bool UIElement::isVisible(){
return getRenderer()->isActive();
return isActive();
}

}
6 changes: 3 additions & 3 deletions config/engine.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ scene.maxNewObjectsToSpawn=20
scene.sortByYCoordinate=true
line.renderers.count=1000
scene.maxLayers=10
#scenes.length=0
scenes.length=1
scenes[0]=config/sceneTmp.conf
scenes.length=0
#scenes.length=1
#scenes[0]=config/sceneTmp.conf

0 comments on commit 0dfcfab

Please sign in to comment.