2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2012-02-07 01:41:53 +01:00
|
|
|
UIMiniWindowContainer = extends(UIWidget)
|
|
|
|
|
|
|
|
function UIMiniWindowContainer.create()
|
|
|
|
local container = UIMiniWindowContainer.internalCreate()
|
2012-06-21 21:31:22 +02:00
|
|
|
container.scheduledWidgets = {}
|
2012-02-07 01:41:53 +01:00
|
|
|
container:setFocusable(false)
|
|
|
|
container:setPhantom(true)
|
|
|
|
return container
|
|
|
|
end
|
|
|
|
|
2012-08-14 17:43:48 +02:00
|
|
|
function UIMiniWindowContainer:getClassName()
|
|
|
|
return 'UIMiniWindowContainer'
|
|
|
|
end
|
|
|
|
|
2012-08-18 07:04:01 +02:00
|
|
|
function UIMiniWindowContainer:fitAll(noRemoveChild)
|
|
|
|
if not self:isVisible() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local sumHeight = 0
|
|
|
|
local children = self:getChildren()
|
|
|
|
for i=1,#children do
|
2012-08-21 07:35:08 +02:00
|
|
|
if children[i]:isVisible() then
|
|
|
|
sumHeight = sumHeight + children[i]:getHeight()
|
|
|
|
end
|
2012-08-18 07:04:01 +02:00
|
|
|
end
|
|
|
|
|
2012-08-21 07:35:08 +02:00
|
|
|
local selfHeight = self:getHeight() - (self:getMarginTop() + self:getMarginBottom() + self:getPaddingTop() + self:getPaddingBottom())
|
2012-08-18 07:04:01 +02:00
|
|
|
if sumHeight <= selfHeight then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local removeChildren = {}
|
|
|
|
|
2012-08-21 07:35:08 +02:00
|
|
|
-- try to resize noRemoveChild
|
|
|
|
local maximumHeight = selfHeight - (sumHeight - noRemoveChild:getHeight())
|
|
|
|
if noRemoveChild:getMinimumHeight() <= maximumHeight then
|
|
|
|
sumHeight = sumHeight - noRemoveChild:getHeight() + maximumHeight
|
|
|
|
addEvent(function() noRemoveChild:setHeight(maximumHeight) end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: try to resize another widget?
|
|
|
|
-- TODO: try to find another panel?
|
|
|
|
|
2012-08-18 07:04:01 +02:00
|
|
|
-- try to remove no-save widget
|
|
|
|
for i=#children,1,-1 do
|
2012-08-21 07:35:08 +02:00
|
|
|
if sumHeight <= selfHeight then
|
2012-08-18 07:04:01 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
local child = children[i]
|
|
|
|
if child ~= noRemoveChild and not child.save then
|
|
|
|
local childHeight = child:getHeight()
|
|
|
|
sumHeight = sumHeight - childHeight
|
|
|
|
table.insert(removeChildren, child)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- try to remove save widget
|
|
|
|
for i=#children,1,-1 do
|
2012-08-21 07:35:08 +02:00
|
|
|
if sumHeight <= selfHeight then
|
2012-08-18 07:04:01 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
local child = children[i]
|
|
|
|
if child ~= noRemoveChild then
|
|
|
|
local childHeight = child:getHeight()
|
|
|
|
sumHeight = sumHeight - childHeight
|
|
|
|
table.insert(removeChildren, child)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- close widgets
|
|
|
|
for i=1,#removeChildren do
|
|
|
|
removeChildren[i]:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-27 00:24:01 +02:00
|
|
|
function UIMiniWindowContainer:onDrop(widget, mousePos)
|
2012-03-28 21:09:45 +02:00
|
|
|
if widget:getClassName() == 'UIMiniWindow' then
|
2012-07-30 12:59:08 +02:00
|
|
|
local oldParent = widget:getParent()
|
|
|
|
if oldParent == self then
|
2012-08-14 17:43:48 +02:00
|
|
|
return true
|
2012-07-30 12:59:08 +02:00
|
|
|
end
|
2012-06-11 07:38:08 +02:00
|
|
|
|
2012-06-21 21:31:22 +02:00
|
|
|
if oldParent then
|
2012-07-30 12:59:08 +02:00
|
|
|
oldParent:removeChild(widget)
|
2012-06-21 21:31:22 +02:00
|
|
|
end
|
2012-06-11 07:38:08 +02:00
|
|
|
|
2012-07-30 12:59:08 +02:00
|
|
|
if widget.movedWidget then
|
2012-08-14 17:43:48 +02:00
|
|
|
local index = self:getChildIndex(widget.movedWidget)
|
|
|
|
self:insertChild(index + widget.movedIndex, widget)
|
2012-07-30 12:59:08 +02:00
|
|
|
else
|
2012-08-14 17:43:48 +02:00
|
|
|
self:addChild(widget)
|
2012-07-30 12:59:08 +02:00
|
|
|
end
|
2012-06-26 00:13:30 +02:00
|
|
|
|
2012-08-21 07:35:08 +02:00
|
|
|
self:fitAll(widget)
|
2012-03-28 21:09:45 +02:00
|
|
|
return true
|
|
|
|
end
|
2012-03-27 00:24:01 +02:00
|
|
|
end
|
|
|
|
|
2012-06-21 21:31:22 +02:00
|
|
|
function UIMiniWindowContainer:swapInsert(widget, index)
|
|
|
|
local oldParent = widget:getParent()
|
|
|
|
local oldIndex = self:getChildIndex(widget)
|
|
|
|
|
|
|
|
if oldParent == self and oldIndex ~= index then
|
|
|
|
local oldWidget = self:getChildByIndex(index)
|
|
|
|
self:removeChild(oldWidget)
|
|
|
|
self:insertChild(oldIndex, oldWidget)
|
|
|
|
self:removeChild(widget)
|
|
|
|
self:insertChild(index, widget)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIMiniWindowContainer:scheduleInsert(widget, index)
|
|
|
|
if index - 1 > self:getChildCount() then
|
|
|
|
if self.scheduledWidgets[index] then
|
2012-07-30 12:59:08 +02:00
|
|
|
pwarning('replacing scheduled widget id ' .. widget:getId())
|
2012-06-21 21:31:22 +02:00
|
|
|
end
|
|
|
|
self.scheduledWidgets[index] = widget
|
|
|
|
else
|
|
|
|
local oldParent = widget:getParent()
|
|
|
|
if oldParent ~= self then
|
2012-08-21 07:35:08 +02:00
|
|
|
if oldParent then
|
|
|
|
oldParent:removeChild(widget)
|
|
|
|
end
|
2012-06-21 21:31:22 +02:00
|
|
|
self:insertChild(index, widget)
|
|
|
|
|
2012-06-24 14:41:39 +02:00
|
|
|
while true do
|
|
|
|
local placed = false
|
|
|
|
for nIndex,nWidget in pairs(self.scheduledWidgets) do
|
|
|
|
if nIndex - 1 <= self:getChildCount() then
|
|
|
|
self:insertChild(nIndex, nWidget)
|
|
|
|
self.scheduledWidgets[nIndex] = nil
|
|
|
|
placed = true
|
|
|
|
break
|
|
|
|
end
|
2012-06-21 21:31:22 +02:00
|
|
|
end
|
2012-06-24 14:41:39 +02:00
|
|
|
if not placed then break end
|
2012-06-21 21:31:22 +02:00
|
|
|
end
|
2012-06-24 14:41:39 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIMiniWindowContainer:order()
|
|
|
|
local children = self:getChildren()
|
|
|
|
for i=1,#children do
|
|
|
|
if not children[i].miniLoaded then return end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i=1,#children do
|
|
|
|
if children[i].miniIndex then
|
|
|
|
self:swapInsert(children[i], children[i].miniIndex)
|
2012-06-21 21:31:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIMiniWindowContainer:saveChildren()
|
|
|
|
local children = self:getChildren()
|
2012-06-24 14:41:39 +02:00
|
|
|
local ignoreIndex = 0
|
2012-06-21 21:31:22 +02:00
|
|
|
for i=1,#children do
|
2012-06-24 14:41:39 +02:00
|
|
|
if children[i].save then
|
|
|
|
children[i]:saveParentIndex(self:getId(), i - ignoreIndex)
|
|
|
|
else
|
|
|
|
ignoreIndex = ignoreIndex + 1
|
|
|
|
end
|
2012-06-21 21:31:22 +02:00
|
|
|
end
|
|
|
|
end
|