Browse Source

Fix issue #21, but miniwindows can be a lot smarter

Henrique Santiago 8 years ago
parent
commit
2142ee765d

+ 19
- 16
modules/corelib/ui/uiminiwindow.lua View File

@@ -3,7 +3,6 @@ UIMiniWindow = extends(UIWindow)
3 3
 
4 4
 function UIMiniWindow.create()
5 5
   local miniwindow = UIMiniWindow.internalCreate()
6
-  miniwindow.isSetup = false
7 6
   return miniwindow
8 7
 end
9 8
 
@@ -22,6 +21,7 @@ function UIMiniWindow:open(dontSave)
22 21
 end
23 22
 
24 23
 function UIMiniWindow:close(dontSave)
24
+  if not self:isExplicitlyVisible() then return end
25 25
   self:setVisible(false)
26 26
 
27 27
   if not dontSave then
@@ -88,6 +88,7 @@ function UIMiniWindow:onSetup()
88 88
         if parent then
89 89
           if parent:getClassName() == 'UIMiniWindowContainer' and selfSettings.index and parent:isOn() then
90 90
             self.miniIndex = selfSettings.index
91
+            --addEvent(function() parent:scheduleInsert(self, selfSettings.index) end)
91 92
             parent:scheduleInsert(self, selfSettings.index)
92 93
           elseif selfSettings.position then
93 94
             self:setParent(parent)
@@ -109,8 +110,6 @@ function UIMiniWindow:onSetup()
109 110
         self:setHeight(selfSettings.height)
110 111
       end
111 112
     end
112
-
113
-    self.isSetup = true
114 113
   end
115 114
 
116 115
   local newParent = self:getParent()
@@ -119,24 +118,22 @@ function UIMiniWindow:onSetup()
119 118
 
120 119
   if self.save then
121 120
     if oldParent and oldParent:getClassName() == 'UIMiniWindowContainer' then
122
-      oldParent:order()
121
+      addEvent(function() oldParent:order() end)
123 122
     end
124 123
     if newParent and newParent:getClassName() == 'UIMiniWindowContainer' and newParent ~= oldParent then
125
-      newParent:order()
124
+      addEvent(function() newParent:order() end)
126 125
     end
127 126
   end
128 127
 
129 128
   if newParent and newParent:getClassName() == 'UIMiniWindowContainer' and self:isVisible() then
130
-    -- not on input event, must rework
131
-    --newParent:fitAll(self)
129
+    newParent:fitAll(self)
132 130
   end
133 131
 end
134 132
 
135 133
 function UIMiniWindow:onVisibilityChange(visible)
136 134
   local parent = self:getParent()
137 135
   if visible and parent and parent:getClassName() == 'UIMiniWindowContainer' then
138
-    -- not on input event, must rework
139
-    --parent:fitAll(self)
136
+    parent:fitAll(self)
140 137
   end
141 138
 end
142 139
 
@@ -220,7 +217,6 @@ function UIMiniWindow:onMousePress()
220 217
 end
221 218
 
222 219
 function UIMiniWindow:onFocusChange(focused)
223
-  -- miniwindows only raises when its outside MiniWindowContainers
224 220
   if not focused then return end
225 221
   local parent = self:getParent()
226 222
   if parent and parent:getClassName() ~= 'UIMiniWindowContainer' then
@@ -228,15 +224,12 @@ function UIMiniWindow:onFocusChange(focused)
228 224
   end
229 225
 end
230 226
 
231
-function UIMiniWindow:onGeometryChange(oldRect, rect)
232
-  if self.isSetup then
233
-    self:setSettings({height = rect.height})
234
-  end
227
+function UIMiniWindow:onHeightChange(height)
228
+  self:setSettings({height = height})
235 229
 
236 230
   local parent = self:getParent()
237 231
   if self:isVisible() and parent and parent:getClassName() == 'UIMiniWindowContainer' then
238
-    -- not on input event, must rework
239
-    --parent:fitAll(self)
232
+    parent:fitAll(self)
240 233
   end
241 234
 end
242 235
 
@@ -311,3 +304,13 @@ function UIMiniWindow:setContentMaximumHeight(height)
311 304
   local resizeBorder = self:getChildById('bottomResizeBorder')
312 305
   resizeBorder:setMaximum(minHeight + height)
313 306
 end
307
+
308
+function UIMiniWindow:getMinimumHeight()
309
+  local resizeBorder = self:getChildById('bottomResizeBorder')
310
+  return resizeBorder:getMinimum()
311
+end
312
+
313
+function UIMiniWindow:getMaximumHeight()
314
+  local resizeBorder = self:getChildById('bottomResizeBorder')
315
+  return resizeBorder:getMaximum()
316
+end

+ 20
- 10
modules/corelib/ui/uiminiwindowcontainer.lua View File

@@ -21,19 +21,31 @@ function UIMiniWindowContainer:fitAll(noRemoveChild)
21 21
   local sumHeight = 0
22 22
   local children = self:getChildren()
23 23
   for i=1,#children do
24
-    sumHeight = sumHeight + children[i]:getHeight()
24
+    if children[i]:isVisible() then
25
+      sumHeight = sumHeight + children[i]:getHeight()
26
+    end
25 27
   end
26 28
 
27
-  local selfHeight = self:getHeight()
29
+  local selfHeight = self:getHeight() - (self:getMarginTop() + self:getMarginBottom() + self:getPaddingTop() + self:getPaddingBottom())
28 30
   if sumHeight <= selfHeight then
29 31
     return
30 32
   end
31 33
 
32 34
   local removeChildren = {}
33 35
 
36
+  -- try to resize noRemoveChild
37
+  local maximumHeight = selfHeight - (sumHeight - noRemoveChild:getHeight())
38
+  if noRemoveChild:getMinimumHeight() <= maximumHeight then
39
+    sumHeight = sumHeight - noRemoveChild:getHeight() + maximumHeight
40
+    addEvent(function() noRemoveChild:setHeight(maximumHeight) end)
41
+  end
42
+
43
+  -- TODO: try to resize another widget?
44
+  -- TODO: try to find another panel?
45
+
34 46
   -- try to remove no-save widget
35 47
   for i=#children,1,-1 do
36
-    if sumHeight < selfHeight then
48
+    if sumHeight <= selfHeight then
37 49
       break
38 50
     end
39 51
 
@@ -47,7 +59,7 @@ function UIMiniWindowContainer:fitAll(noRemoveChild)
47 59
 
48 60
   -- try to remove save widget
49 61
   for i=#children,1,-1 do
50
-    if sumHeight < selfHeight then
62
+    if sumHeight <= selfHeight then
51 63
       break
52 64
     end
53 65
 
@@ -63,11 +75,6 @@ function UIMiniWindowContainer:fitAll(noRemoveChild)
63 75
   for i=1,#removeChildren do
64 76
     removeChildren[i]:close()
65 77
   end
66
-
67
-  -- dont let noRemoveChild be bigger than self
68
-  if noRemoveChild:getHeight() > selfHeight - 20 then
69
-    noRemoveChild:setHeight(selfHeight - 20)
70
-  end
71 78
 end
72 79
 
73 80
 function UIMiniWindowContainer:onDrop(widget, mousePos)
@@ -88,6 +95,7 @@ function UIMiniWindowContainer:onDrop(widget, mousePos)
88 95
       self:addChild(widget)
89 96
     end
90 97
 
98
+    self:fitAll(widget)
91 99
     return true
92 100
   end
93 101
 end
@@ -114,7 +122,9 @@ function UIMiniWindowContainer:scheduleInsert(widget, index)
114 122
   else
115 123
     local oldParent = widget:getParent()
116 124
     if oldParent ~= self then
117
-      oldParent:removeChild(widget)
125
+      if oldParent then
126
+        oldParent:removeChild(widget)
127
+      end
118 128
       self:insertChild(index, widget)
119 129
 
120 130
       while true do

+ 6
- 14
modules/corelib/ui/uiresizeborder.lua View File

@@ -37,23 +37,15 @@ function UIResizeBorder:onMouseMove(mousePos, mouseMoved)
37 37
     if self.vertical then
38 38
       local delta = mousePos.y - self:getY() - self:getHeight()/2
39 39
       local parent = self:getParent()
40
-      local newsize = math.min(math.max(parent:getHeight() + delta, self.minimum), self.maximum)
41
-      self.newsize = newsize
42
-      if not self.event or self.event:isExecuted() then
43
-        self.event = addEvent(function()
44
-          parent:setHeight(self.newsize)
45
-        end)
46
-      end
40
+      local newSize = math.min(math.max(parent:getHeight() + delta, self.minimum), self.maximum)
41
+      parent:setHeight(newSize)
42
+      signalcall(parent.onHeightChange, parent, newSize)
47 43
     else
48 44
       local delta = mousePos.x - self:getX() - self:getWidth()/2
49 45
       local parent = self:getParent()
50
-      local newsize = math.min(math.max(parent:getWidth() + delta, self.minimum), self.maximum)
51
-      self.newsize = newsize
52
-      if not self.event or self.event:isExecuted() then
53
-        self.event = addEvent(function()
54
-          parent:setWidth(self.newsize)
55
-        end)
56
-      end
46
+      local newSize = math.min(math.max(parent:getWidth() + delta, self.minimum), self.maximum)
47
+      parent:setWidth(newSize)
48
+      signalcall(parent.onWidthChange, parent, newSize)
57 49
     end
58 50
     return true
59 51
   end

+ 1
- 1
modules/game_battle/battle.lua View File

@@ -28,9 +28,9 @@ table.insert(LifeBarColors, {percentAbove = -1, color = '#4F0000' } )
28 28
 
29 29
 function init()
30 30
   g_ui.importStyle('battlebutton.otui')
31
-  battleWindow = g_ui.loadUI('battle.otui', modules.game_interface.getRightPanel())
32 31
   battleButton = TopMenu.addRightGameToggleButton('battleButton', tr('Battle') .. ' (Ctrl+B)', 'battle.png', toggle)
33 32
   battleButton:setOn(true)
33
+  battleWindow = g_ui.loadUI('battle.otui', modules.game_interface.getRightPanel())
34 34
   g_keyboard.bindKeyDown('Ctrl+B', toggle)
35 35
 
36 36
   -- this disables scrollbar auto hiding

+ 3
- 2
modules/game_healthinfo/healthinfo.lua View File

@@ -35,10 +35,11 @@ function init()
35 35
 
36 36
   connect(g_game, { onGameEnd = offline })
37 37
 
38
-  healthInfoWindow = g_ui.loadUI('healthinfo.otui', modules.game_interface.getRightPanel())
39 38
   healthInfoButton = TopMenu.addRightGameToggleButton('healthInfoButton', tr('Health Information'), 'healthinfo.png', toggle)
40
-  healthInfoWindow:disableResize()
41 39
   healthInfoButton:setOn(true)
40
+
41
+  healthInfoWindow = g_ui.loadUI('healthinfo.otui', modules.game_interface.getRightPanel())
42
+  healthInfoWindow:disableResize()
42 43
   healthBar = healthInfoWindow:recursiveGetChildById('healthBar')
43 44
   manaBar = healthInfoWindow:recursiveGetChildById('manaBar')
44 45
   healthLabel = healthInfoWindow:recursiveGetChildById('healthLabel')

+ 3
- 2
modules/game_inventory/inventory.lua View File

@@ -21,11 +21,12 @@ function init()
21 21
 
22 22
   g_keyboard.bindKeyDown('Ctrl+I', toggle)
23 23
 
24
+  inventoryButton = TopMenu.addRightGameToggleButton('inventoryButton', tr('Inventory') .. ' (Ctrl+I)', 'inventory.png', toggle)
25
+  inventoryButton:setOn(true)
26
+
24 27
   inventoryWindow = g_ui.loadUI('inventory.otui', modules.game_interface.getRightPanel())
25 28
   inventoryWindow:disableResize()
26 29
   inventoryPanel = inventoryWindow:getChildById('contentsPanel')
27
-  inventoryButton = TopMenu.addRightGameToggleButton('inventoryButton', tr('Inventory') .. ' (Ctrl+I)', 'inventory.png', toggle)
28
-  inventoryButton:setOn(true)
29 30
 
30 31
   refresh()
31 32
 end

+ 2
- 1
modules/game_skills/skills.lua View File

@@ -24,9 +24,10 @@ function init()
24 24
     onGameStart = refresh
25 25
   })
26 26
 
27
-  skillsWindow = g_ui.loadUI('skills.otui', modules.game_interface.getRightPanel())
28 27
   skillsButton = TopMenu.addRightGameToggleButton('skillsButton', tr('Skills') .. ' (Ctrl+S)', 'skills.png', toggle)
29 28
   skillsButton:setOn(true)
29
+  skillsWindow = g_ui.loadUI('skills.otui', modules.game_interface.getRightPanel())
30
+
30 31
   g_keyboard.bindKeyDown('Ctrl+S', toggle)
31 32
 
32 33
   refresh()

+ 1
- 1
modules/game_viplist/viplist.lua View File

@@ -10,9 +10,9 @@ function init()
10 10
 
11 11
   g_keyboard.bindKeyDown('Ctrl+P', toggle)
12 12
 
13
-  vipWindow = g_ui.loadUI('viplist.otui', modules.game_interface.getRightPanel())
14 13
   vipButton = TopMenu.addRightGameToggleButton('vipListButton', tr('VIP list') .. ' (Ctrl+P)', 'viplist.png', toggle)
15 14
   vipButton:setOn(true)
15
+  vipWindow = g_ui.loadUI('viplist.otui', modules.game_interface.getRightPanel())
16 16
 
17 17
   refresh()
18 18
 end

+ 1
- 1
src/framework/ui/uimanager.cpp View File

@@ -449,6 +449,6 @@ UIWidgetPtr UIManager::createWidgetFromOTML(const OTMLNodePtr& widgetNode, const
449 449
     } else
450 450
         stdext::throw_exception(stdext::format("unable to create widget of type '%s'", widgetType));
451 451
 
452
+    widget->callLuaField("onSetup");
452 453
     return widget;
453 454
 }
454
-

+ 1
- 5
src/framework/ui/uiwidget.cpp View File

@@ -177,7 +177,7 @@ void UIWidget::insertChild(int index, const UIWidgetPtr& child)
177 177
     index = index <= 0 ? (m_children.size() + index) : index-1;
178 178
 
179 179
     if(!(index >= 0 && (uint)index <= m_children.size())) {
180
-        g_logger.traceError("attemp to insert a child in an invalid index");
180
+        g_logger.traceError("attempt to insert a child in an invalid index");
181 181
         return;
182 182
     }
183 183
 
@@ -504,10 +504,6 @@ void UIWidget::applyStyle(const OTMLNodePtr& styleNode)
504 504
         callLuaField("onStyleApply", styleNode->tag(), styleNode);
505 505
 
506 506
         if(m_firstOnStyle) {
507
-            auto self = static_self_cast<UIWidget>();
508
-            g_dispatcher.addEvent([self] {
509
-                self->callLuaField("onSetup");
510
-            });
511 507
             // always focus new child
512 508
             if(isFocusable() && isExplicitlyVisible() && isExplicitlyEnabled())
513 509
                 focus();

Loading…
Cancel
Save