Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. -- @docclass
  2. UIMiniWindow2 = extends(UIWindow, "UIMiniWindow2")
  3. local startDragPosition, startDragParent
  4.  
  5. function UIMiniWindow2.create()
  6. local miniwindow = UIMiniWindow2.internalCreate()
  7. miniwindow.UIMiniWindowContainer = true
  8. return miniwindow
  9. end
  10.  
  11. function UIMiniWindow2:open(dontSave)
  12. self:setVisible(true)
  13.  
  14. if not dontSave then
  15. self:setSettings({closed = false})
  16. end
  17.  
  18. signalcall(self.onOpen, self)
  19. end
  20.  
  21. function UIMiniWindow2:close(dontSave)
  22. if not self:isExplicitlyVisible() then return end
  23. self:setVisible(false)
  24.  
  25. if not dontSave then
  26. self:setSettings({closed = true})
  27. end
  28.  
  29. signalcall(self.onClose, self)
  30. end
  31.  
  32. function UIMiniWindow2:minimize(dontSave)
  33. self:setOn(true)
  34. self:getChildById('contentsPanel'):hide()
  35. self:getChildById('miniwindowScrollBar'):hide()
  36. self:getChildById('bottomResizeBorder'):hide()
  37. self:getChildById('minimizeButton'):setOn(true)
  38. self.maximizedHeight = self:getHeight()
  39. self:setHeight(self.minimizedHeight)
  40.  
  41. if not dontSave then
  42. self:setSettings({minimized = true})
  43. end
  44.  
  45. signalcall(self.onMinimize, self)
  46. end
  47.  
  48. function UIMiniWindow2:maximize(dontSave)
  49. self:setOn(false)
  50. self:getChildById('contentsPanel'):show()
  51. self:getChildById('miniwindowScrollBar'):show()
  52. self:getChildById('bottomResizeBorder'):show()
  53. self:getChildById('minimizeButton'):setOn(false)
  54. self:setHeight(self:getSettings('height') or self.maximizedHeight)
  55.  
  56. if not dontSave then
  57. self:setSettings({minimized = false})
  58. end
  59.  
  60. local parent = self:getParent()
  61. if parent and parent:getClassName() == 'UIMiniWindowContainer' then
  62. parent:fitAll(self)
  63. end
  64.  
  65. signalcall(self.onMaximize, self)
  66. end
  67.  
  68. function UIMiniWindow2:setup()
  69.  
  70. self:getChildById('minimizeButton').onClick =
  71. function()
  72. if self:isOn() then
  73. self:maximize()
  74. else
  75. self:minimize()
  76. end
  77. end
  78.  
  79. local oldParent = self:getParent()
  80.  
  81. local settings = g_settings.getNode('MiniWindows')
  82. if settings then
  83. local selfSettings = settings[self:getId()]
  84. if selfSettings then
  85. if selfSettings.parentId then
  86. local parent = rootWidget:recursiveGetChildById(selfSettings.parentId)
  87. if parent then
  88. if parent:getClassName() == 'UIMiniWindowContainer' and selfSettings.index and parent:isOn() then
  89. self.miniIndex = selfSettings.index
  90. parent:scheduleInsert(self, selfSettings.index)
  91. elseif selfSettings.position then
  92. self:setParent(parent, true)
  93. self:setPosition(topoint(selfSettings.position))
  94. end
  95. end
  96. end
  97.  
  98. if selfSettings.minimized then
  99. self:minimize(true)
  100. else
  101. if selfSettings.height and self:isResizeable() then
  102. self:setHeight(selfSettings.height)
  103. elseif selfSettings.height and not self:isResizeable() then
  104. self:eraseSettings({height = true})
  105. end
  106. end
  107.  
  108. if selfSettings.closed then
  109. self:close(true)
  110. end
  111. end
  112. end
  113.  
  114. local newParent = self:getParent()
  115.  
  116. self.miniLoaded = true
  117.  
  118. if self.save then
  119. if oldParent and oldParent:getClassName() == 'UIMiniWindowContainer' then
  120. addEvent(function() oldParent:order() end)
  121. end
  122. if newParent and newParent:getClassName() == 'UIMiniWindowContainer' and newParent ~= oldParent then
  123. addEvent(function() newParent:order() end)
  124. end
  125. end
  126.  
  127. self:fitOnParent()
  128. end
  129.  
  130. function UIMiniWindow2:onVisibilityChange(visible)
  131. self:fitOnParent()
  132. end
  133.  
  134. function UIMiniWindow2:onDragEnter(mousePos)
  135. local parent = self:getParent()
  136. if not parent then return false end
  137.  
  138. startDragPosition = self:getPosition()
  139. startDragParent = parent
  140. if parent:getClassName() == 'UIMiniWindowContainer' then
  141. local containerParent = parent:getParent()
  142. parent:removeChild(self)
  143. containerParent:addChild(self)
  144. parent:saveChildren()
  145. end
  146.  
  147. local oldPos = self:getPosition()
  148. self.movingReference = { x = mousePos.x - oldPos.x, y = mousePos.y - oldPos.y }
  149. self:setPosition(oldPos)
  150. self.free = true
  151. return true
  152. end
  153.  
  154. function UIMiniWindow2:onDragLeave(droppedWidget, mousePos)
  155. if self:getOpacity() == 0.5 then
  156. self:setOpacity(1.0)
  157. self:setPosition(startDragPosition)
  158. self:getParent():removeChild(self)
  159. self:setParent(startDragParent)
  160. self:getParent():saveChildren()
  161. startDragPosition = nil
  162. startDragParent = nil
  163. end
  164.  
  165. if self.movedWidget then
  166. self.setMovedChildMargin(self.movedOldMargin or 0)
  167. self.movedWidget = nil
  168. self.setMovedChildMargin = nil
  169. self.movedOldMargin = nil
  170. self.movedIndex = nil
  171. end
  172.  
  173. self:saveParent(self:getParent())
  174. end
  175.  
  176. function UIMiniWindow2:onDragMove(mousePos, mouseMoved)
  177. local oldMousePosY = mousePos.y - mouseMoved.y
  178. local children = rootWidget:recursiveGetChildrenByMarginPos(mousePos)
  179. local overAnyWidget = false
  180.  
  181. local overContainerPanel = false
  182.  
  183. for i=1,#children do
  184. local child = children[i]
  185. if child:getClassName() == 'UIMiniWindowContainer' then
  186. overContainerPanel = true
  187. end
  188. end
  189. if not overContainerPanel then
  190. self:setOpacity(0.5)
  191. else
  192. self:setOpacity(1.0)
  193. end
  194.  
  195. for i=1,#children do
  196. local child = children[i]
  197. if child:getParent():getClassName() == 'UIMiniWindowContainer' then
  198. overAnyWidget = true
  199.  
  200. local childCenterY = child:getY() + child:getHeight() / 2
  201. if child == self.movedWidget and mousePos.y < childCenterY and oldMousePosY < childCenterY then
  202. break
  203. end
  204.  
  205. if self.movedWidget then
  206. self.setMovedChildMargin(self.movedOldMargin or 0)
  207. self.setMovedChildMargin = nil
  208. end
  209.  
  210. if mousePos.y < childCenterY then
  211. self.movedOldMargin = child:getMarginTop()
  212. self.setMovedChildMargin = function(v) child:setMarginTop(v) end
  213. self.movedIndex = 0
  214. else
  215. self.movedOldMargin = child:getMarginBottom()
  216. self.setMovedChildMargin = function(v) child:setMarginBottom(v) end
  217. self.movedIndex = 1
  218. end
  219.  
  220. self.movedWidget = child
  221. self.setMovedChildMargin(self:getHeight())
  222. break
  223. end
  224. end
  225.  
  226. if not overAnyWidget and self.movedWidget then
  227. self.setMovedChildMargin(self.movedOldMargin or 0)
  228. self.movedWidget = nil
  229. end
  230.  
  231. return UIWindow.onDragMove(self, mousePos, mouseMoved)
  232. end
  233.  
  234. function UIMiniWindow2:onMousePress()
  235. local parent = self:getParent()
  236. if not parent then return false end
  237. if parent:getClassName() ~= 'UIMiniWindowContainer' then
  238. self:raise()
  239. return true
  240. end
  241. end
  242.  
  243. function UIMiniWindow2:onFocusChange(focused)
  244. if not focused then return end
  245. local parent = self:getParent()
  246. if parent and parent:getClassName() ~= 'UIMiniWindowContainer' then
  247. self:raise()
  248. end
  249. end
  250.  
  251. function UIMiniWindow2:onHeightChange(height)
  252. if not self:isOn() then
  253. self:setSettings({height = height})
  254. end
  255. self:fitOnParent()
  256. end
  257.  
  258. function UIMiniWindow2:getSettings(name)
  259. if not self.save then return nil end
  260. local settings = g_settings.getNode('MiniWindows')
  261. if settings then
  262. local selfSettings = settings[self:getId()]
  263. if selfSettings then
  264. return selfSettings[name]
  265. end
  266. end
  267. return nil
  268. end
  269.  
  270. function UIMiniWindow2:setSettings(data)
  271. if not self.save then return end
  272.  
  273. local settings = g_settings.getNode('MiniWindows')
  274. if not settings then
  275. settings = {}
  276. end
  277.  
  278. local id = self:getId()
  279. if not settings[id] then
  280. settings[id] = {}
  281. end
  282.  
  283. for key,value in pairs(data) do
  284. settings[id][key] = value
  285. end
  286.  
  287. g_settings.setNode('MiniWindows', settings)
  288. end
  289.  
  290. function UIMiniWindow2:eraseSettings(data)
  291. if not self.save then return end
  292.  
  293. local settings = g_settings.getNode('MiniWindows')
  294. if not settings then
  295. settings = {}
  296. end
  297.  
  298. local id = self:getId()
  299. if not settings[id] then
  300. settings[id] = {}
  301. end
  302.  
  303. for key,value in pairs(data) do
  304. settings[id][key] = nil
  305. end
  306.  
  307. g_settings.setNode('MiniWindows', settings)
  308. end
  309.  
  310. function UIMiniWindow2:saveParent(parent)
  311. local parent = self:getParent()
  312. if parent then
  313. if parent:getClassName() == 'UIMiniWindowContainer' then
  314. parent:saveChildren()
  315. else
  316. self:saveParentPosition(parent:getId(), self:getPosition())
  317. end
  318. end
  319. end
  320.  
  321. function UIMiniWindow2:saveParentPosition(parentId, position)
  322. local selfSettings = {}
  323. selfSettings.parentId = parentId
  324. selfSettings.position = pointtostring(position)
  325. self:setSettings(selfSettings)
  326. end
  327.  
  328. function UIMiniWindow2:saveParentIndex(parentId, index)
  329. local selfSettings = {}
  330. selfSettings.parentId = parentId
  331. selfSettings.index = index
  332. self:setSettings(selfSettings)
  333. self.miniIndex = index
  334. end
  335.  
  336. function UIMiniWindow2:disableResize()
  337. self:getChildById('bottomResizeBorder'):disable()
  338. end
  339.  
  340. function UIMiniWindow2:enableResize()
  341. self:getChildById('bottomResizeBorder'):enable()
  342. end
  343.  
  344. function UIMiniWindow2:fitOnParent()
  345. local parent = self:getParent()
  346. if self:isVisible() and parent and parent:getClassName() == 'UIMiniWindowContainer' then
  347. parent:fitAll(self)
  348. end
  349. end
  350.  
  351. function UIMiniWindow2:setParent(parent, dontsave)
  352. UIWidget.setParent(self, parent)
  353. if not dontsave then
  354. self:saveParent(parent)
  355. end
  356. self:fitOnParent()
  357. end
  358.  
  359. function UIMiniWindow2:setHeight(height)
  360. UIWidget.setHeight(self, height)
  361. signalcall(self.onHeightChange, self, height)
  362. end
  363.  
  364. function UIMiniWindow2:setContentHeight(height)
  365. local contentsPanel = self:getChildById('contentsPanel')
  366. local minHeight = contentsPanel:getMarginTop() + contentsPanel:getMarginBottom() + contentsPanel:getPaddingTop() + contentsPanel:getPaddingBottom()
  367.  
  368. local resizeBorder = self:getChildById('bottomResizeBorder')
  369. resizeBorder:setParentSize(minHeight + height)
  370. end
  371.  
  372. function UIMiniWindow2:setContentMinimumHeight(height)
  373. local contentsPanel = self:getChildById('contentsPanel')
  374. local minHeight = contentsPanel:getMarginTop() + contentsPanel:getMarginBottom() + contentsPanel:getPaddingTop() + contentsPanel:getPaddingBottom()
  375.  
  376. local resizeBorder = self:getChildById('bottomResizeBorder')
  377. resizeBorder:setMinimum(minHeight + height)
  378. end
  379.  
  380. function UIMiniWindow2:setContentMaximumHeight(height)
  381. local contentsPanel = self:getChildById('contentsPanel')
  382. local minHeight = contentsPanel:getMarginTop() + contentsPanel:getMarginBottom() + contentsPanel:getPaddingTop() + contentsPanel:getPaddingBottom()
  383.  
  384. local resizeBorder = self:getChildById('bottomResizeBorder')
  385. resizeBorder:setMaximum(minHeight + height)
  386. end
  387.  
  388. function UIMiniWindow2:getMinimumHeight()
  389. local resizeBorder = self:getChildById('bottomResizeBorder')
  390. return resizeBorder:getMinimum()
  391. end
  392.  
  393. function UIMiniWindow2:getMaximumHeight()
  394. local resizeBorder = self:getChildById('bottomResizeBorder')
  395. return resizeBorder:getMaximum()
  396. end
  397.  
  398. function UIMiniWindow2:isResizeable()
  399. local resizeBorder = self:getChildById('bottomResizeBorder')
  400. return resizeBorder:isExplicitlyVisible() and resizeBorder:isEnabled()
  401. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement