optimize widget destruction

This commit is contained in:
Eduardo Bart 2012-04-09 17:52:39 -03:00
parent 353aa5215e
commit 5425d344ba
6 changed files with 57 additions and 21 deletions

View File

@ -7,9 +7,10 @@ function RadioGroup.create()
end
function RadioGroup:destroy()
while #self.widgets ~= 0 do
self:removeWidget(self.widgets[1])
for k,widget in pairs(self.widgets) do
widget.onMousePress = nil
end
self.widgets = {}
end
function RadioGroup:addWidget(widget)
@ -25,6 +26,13 @@ function RadioGroup:removeWidget(widget)
table.removevalue(self.widgets, widget)
end
function RadioGroup:destroy()
for k,widget in pairs(self.widgets) do
widget.onMousePress = nil
end
self.widgets = {}
end
function RadioGroup:selectWidget(selectedWidget)
if selectedWidget == self.selectedWidget then return end

View File

@ -59,7 +59,7 @@ function NPCTrade.init()
end
function NPCTrade.terminate()
radioTabs:destroy()
--radioTabs:destroy()
radioTabs = nil
npcWindow:destroy()
npcWindow = nil
@ -229,7 +229,10 @@ function NPCTrade.createItemsOnPanel()
offerSelected = nil
itemsPanel:destroyChildren()
if radioItems then
radioItems:destroy()
end
radioItems = RadioGroup.create()
for i, v in pairs(cacheItems) do

View File

@ -74,6 +74,9 @@ bool UIGridLayout::internalUpdate()
{
bool changed = false;
UIWidgetPtr parentWidget = getParentWidget();
if(!parentWidget)
return false;
UIWidgetList widgets = parentWidget->getChildren();
Rect clippingRect = parentWidget->getClippingRect();

View File

@ -27,6 +27,7 @@
void UILayout::update()
{
//logTraceCounter();
if(m_updateDisabled)
return;
@ -35,12 +36,13 @@ void UILayout::update()
return;
}
UIWidgetPtr parentWidget = getParentWidget();
if(!parentWidget || parentWidget->isDestroyed())
return;
m_updating = true;
internalUpdate();
if(UIWidgetPtr parentWidget = getParentWidget()) {
if(!parentWidget->isDestroyed())
parentWidget->onLayoutUpdate();
}
parentWidget->onLayoutUpdate();
m_updating = false;
}

View File

@ -659,21 +659,19 @@ void UIWidget::bindRectToParent()
setRect(boundRect);
}
void UIWidget::destroy()
void UIWidget::internalDestroy()
{
if(m_destroyed)
logWarning("attempt to destroy widget '", m_id, "' two times");
m_destroyed = true;
setVisible(false);
setEnabled(false);
// remove itself from parent
if(UIWidgetPtr parent = getParent())
parent->removeChild(asUIWidget());
destroyChildren();
m_visible = false;
m_enabled = false;
m_parent.reset();
m_focusedChild = nullptr;
m_layout = nullptr;
m_lockedChildren.clear();
for(const UIWidgetPtr& child : m_children)
child->internalDestroy();
m_children.clear();
callLuaField("onDestroy");
@ -682,10 +680,31 @@ void UIWidget::destroy()
g_ui.onWidgetDestroy(asUIWidget());
}
void UIWidget::destroy()
{
if(m_destroyed)
logWarning("attempt to destroy widget '", m_id, "' two times");
// hold itself reference
UIWidgetPtr self = asUIWidget();
m_destroyed = true;
// remove itself from parent
if(UIWidgetPtr parent = getParent())
parent->removeChild(self);
internalDestroy();
}
void UIWidget::destroyChildren()
{
UILayoutPtr layout = getLayout();
if(layout)
layout->disableUpdates();
while(!m_children.empty())
getFirstChild()->destroy();
m_children[0]->destroy();
layout->enableUpdates();
}
void UIWidget::setId(const std::string& id)

View File

@ -164,6 +164,7 @@ protected:
bool hasState(Fw::WidgetState state);
private:
void internalDestroy();
void updateState(Fw::WidgetState state);
void updateStates();
void updateChildrenIndexStates();