No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uiminiwindow.lua 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. -- @docclass
  2. UIMiniWindow = extends(UIWindow)
  3. function UIMiniWindow.create()
  4. local miniwindow = UIMiniWindow.internalCreate()
  5. return miniwindow
  6. end
  7. function UIMiniWindow:getClassName()
  8. return 'UIMiniWindow'
  9. end
  10. function UIMiniWindow:open(dontSave)
  11. self:setVisible(true)
  12. if not dontSave then
  13. self:setSettings({closed = false})
  14. end
  15. signalcall(self.onOpen, self)
  16. end
  17. function UIMiniWindow:close(dontSave)
  18. self:setVisible(false)
  19. if not dontSave then
  20. self:setSettings({closed = true})
  21. end
  22. signalcall(self.onClose, self)
  23. end
  24. function UIMiniWindow:minimize(dontSave)
  25. self:setOn(true)
  26. self:getChildById('contentsPanel'):hide()
  27. self:getChildById('miniwindowScrollBar'):hide()
  28. self:getChildById('bottomResizeBorder'):hide()
  29. self:getChildById('minimizeButton'):setOn(true)
  30. self.savedHeight = self:getHeight()
  31. self:setHeight(self.minimizedHeight)
  32. if not dontSave then
  33. self:setSettings({minimized = true})
  34. end
  35. signalcall(self.onMinimize, self)
  36. end
  37. function UIMiniWindow:maximize(dontSave)
  38. self:setOn(false)
  39. self:getChildById('contentsPanel'):show()
  40. self:getChildById('miniwindowScrollBar'):show()
  41. self:getChildById('bottomResizeBorder'):show()
  42. self:getChildById('minimizeButton'):setOn(false)
  43. self:setHeight(self.savedHeight)
  44. if not dontSave then
  45. self:setSettings({minimized = false})
  46. end
  47. signalcall(self.onMaximize, self)
  48. end
  49. function UIMiniWindow:onSetup()
  50. self:getChildById('closeButton').onClick =
  51. function()
  52. self:close()
  53. end
  54. self:getChildById('minimizeButton').onClick =
  55. function()
  56. if self:isOn() then
  57. self:maximize()
  58. else
  59. self:minimize()
  60. end
  61. end
  62. local oldParent = self:getParent()
  63. local settings = g_settings.getNode('MiniWindows')
  64. if settings then
  65. local selfSettings = settings[self:getId()]
  66. if selfSettings then
  67. if selfSettings.parentId then
  68. local parent = rootWidget:recursiveGetChildById(selfSettings.parentId)
  69. if parent then
  70. if parent:getClassName() == 'UIMiniWindowContainer' and selfSettings.index and parent:isOn() then
  71. self.miniIndex = selfSettings.index
  72. parent:scheduleInsert(self, selfSettings.index)
  73. elseif selfSettings.position then
  74. self:setParent(parent)
  75. self:setPosition(topoint(selfSettings.position))
  76. self:bindRectToParent()
  77. end
  78. end
  79. end
  80. if selfSettings.minimized then
  81. self:minimize(true)
  82. end
  83. if selfSettings.closed then
  84. self:close(true)
  85. end
  86. end
  87. end
  88. local newParent = self:getParent()
  89. self.miniLoaded = true
  90. if oldParent and oldParent:getClassName() == 'UIMiniWindowContainer' then
  91. oldParent:order()
  92. end
  93. if newParent and newParent:getClassName() == 'UIMiniWindowContainer' and newParent ~= oldParent then
  94. newParent:order()
  95. end
  96. end
  97. function UIMiniWindow:onDragEnter(mousePos)
  98. local parent = self:getParent()
  99. if not parent then return false end
  100. if parent:getClassName() == 'UIMiniWindowContainer' then
  101. local containerParent = parent:getParent()
  102. parent:removeChild(self)
  103. containerParent:addChild(self)
  104. parent:saveChildren()
  105. end
  106. local oldPos = self:getPosition()
  107. self.movingReference = { x = mousePos.x - oldPos.x, y = mousePos.y - oldPos.y }
  108. self:setPosition(oldPos)
  109. self.free = true
  110. return true
  111. end
  112. function UIMiniWindow:onDragMove(mousePos, mouseMoved)
  113. local oldMousePosY = mousePos.y - mouseMoved.y
  114. local children = rootWidget:recursiveGetChildrenByMarginPos(mousePos)
  115. local overAnyWidget = false
  116. for i=1,#children do
  117. local child = children[i]
  118. if child:getParent():getClassName() == 'UIMiniWindowContainer' then
  119. overAnyWidget = true
  120. local childCenterY = child:getY() + child:getHeight() / 2
  121. if child == self.movedWidget and mousePos.y < childCenterY and oldMousePosY < childCenterY then
  122. break
  123. end
  124. if self.movedWidget then
  125. self.setMovedChildMargin(0)
  126. self.setMovedChildMargin = nil
  127. end
  128. if mousePos.y < childCenterY then
  129. self.setMovedChildMargin = function(v) child:setMarginTop(v) end
  130. self.movedIndex = 0
  131. else
  132. self.setMovedChildMargin = function(v) child:setMarginBottom(v) end
  133. self.movedIndex = 1
  134. end
  135. self.movedWidget = child
  136. self.setMovedChildMargin(self:getHeight())
  137. break
  138. end
  139. end
  140. if not overAnyWidget and self.movedWidget then
  141. self.setMovedChildMargin(0)
  142. self.movedWidget = nil
  143. end
  144. return UIWindow.onDragMove(self, mousePos, mouseMoved)
  145. end
  146. function UIMiniWindow:onMousePress()
  147. local parent = self:getParent()
  148. if not parent then return false end
  149. if parent:getClassName() ~= 'UIMiniWindowContainer' then
  150. self:raise()
  151. return true
  152. end
  153. end
  154. function UIMiniWindow:onDragLeave(droppedWidget, mousePos)
  155. if self.movedWidget then
  156. self.setMovedChildMargin(0)
  157. self.movedWidget = nil
  158. self.setMovedChildMargin = nil
  159. self.movedIndex = nil
  160. end
  161. local parent = self:getParent()
  162. if parent then
  163. if parent:getClassName() == 'UIMiniWindowContainer' then
  164. parent:saveChildren()
  165. else
  166. self:saveParentPosition(parent:getId(), self:getPosition())
  167. end
  168. end
  169. end
  170. function UIMiniWindow:onFocusChange(focused)
  171. -- miniwindows only raises when its outside MiniWindowContainers
  172. if not focused then return end
  173. local parent = self:getParent()
  174. if parent and parent:getClassName() ~= 'UIMiniWindowContainer' then
  175. self:raise()
  176. end
  177. end
  178. function UIMiniWindow:setSettings(data)
  179. if not self.save then return end
  180. local settings = g_settings.getNode('MiniWindows')
  181. if not settings then
  182. settings = {}
  183. end
  184. local id = self:getId()
  185. if not settings[id] then
  186. settings[id] = {}
  187. end
  188. for key,value in pairs(data) do
  189. settings[id][key] = value
  190. end
  191. g_settings.setNode('MiniWindows', settings)
  192. end
  193. function UIMiniWindow:saveParentPosition(parentId, position)
  194. local selfSettings = {}
  195. selfSettings.parentId = parentId
  196. selfSettings.position = pointtostring(position)
  197. self:setSettings(selfSettings)
  198. end
  199. function UIMiniWindow:saveParentIndex(parentId, index)
  200. local selfSettings = {}
  201. selfSettings.parentId = parentId
  202. selfSettings.index = index
  203. self:setSettings(selfSettings)
  204. end
  205. function UIMiniWindow:disableResize()
  206. self:getChildById('bottomResizeBorder'):disable()
  207. end