Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. function DxElement:drag()
  2.     if self.dragging then
  3.         local cx, cy = getCursorPosition()
  4.  
  5.         cx = cx * SCREEN_WIDTH
  6.         cy = cy * SCREEN_HEIGHT
  7.        
  8.         if not self.dragInitialX then
  9.             self.dragInitialX, self.dragInitialY = cx - self.x, cy - self.y
  10.         end
  11.        
  12.         if self.parent then
  13.             self.baseX, self.baseY = self.baseX + (cx - self.dragInitialX) - self.x, self.baseY + (cy - self.dragInitialY) - self.y
  14.            
  15.             local rootElement = self.parent:getRootElement()
  16.             local baseOffsetX, baseOffsetY = self.baseX, self.baseY
  17.             local minX, minY, maxX, maxY = self.bounds.min.x, self.bounds.min.y, self.bounds.max.x, self.bounds.max.y
  18.             local topLevelChild = self:getTopLevelChild()
  19.            
  20.             for i,e in ipairs(self:getInheritedParents()) do
  21.                 if e ~= rootElement then
  22.                     baseOffsetX, baseOffsetY = baseOffsetX + e.baseX, baseOffsetY + e.baseY
  23.                 end
  24.             end
  25.            
  26.             if rootElement.x + baseOffsetX + maxX > SCREEN_WIDTH then
  27.                 self.baseX = SCREEN_WIDTH - rootElement.x - maxX - (baseOffsetX-self.baseX)
  28.             end
  29.            
  30.             if rootElement.y + baseOffsetY + maxY > SCREEN_HEIGHT then
  31.                 self.baseY = SCREEN_HEIGHT - rootElement.y - maxY - (baseOffsetY-self.baseY)
  32.             end    
  33.            
  34.             if self.baseX + minX < -(rootElement.x) - (self ~= topLevelChild and topLevelChild.baseX or 0) then
  35.                 self.baseX = -(rootElement.x) - minX  - (self ~= topLevelChild and topLevelChild.baseX or 0)
  36.             end
  37.            
  38.             if self.baseY + minY < -(rootElement.y) - (self ~= topLevelChild and topLevelChild.baseY or 0) then
  39.                 self.baseY = -(rootElement.y) - minY  - (self ~= topLevelChild and topLevelChild.baseY or 0)
  40.             end                        
  41.         else
  42.             self.x, self.y = cx - self.dragInitialX, cy - self.dragInitialY
  43.         end
  44.     end
  45. end
  46.  
  47. function DxElement:forceInBounds()
  48.     if not self:getProperty("force_in_bounds") then
  49.         return false
  50.     end
  51.    
  52.     local targetArea = {
  53.         x = 0,
  54.         y = 0,
  55.         width = SCREEN_WIDTH,
  56.         height = SCREEN_HEIGHT
  57.     }
  58.    
  59.     if self.parent then
  60.         if self:getProperty("drag_preview") then
  61.             if self.dragging then
  62.                 return false
  63.             end
  64.         end
  65.        
  66.         targetArea.width, targetArea.height = self.parent.width, self.parent.height
  67.        
  68.         if (self.baseX + self.width) > (targetArea.width) then
  69.             self.baseX = (targetArea.width) - self.width
  70.         elseif (self.baseX) < targetArea.x then
  71.             self.baseX = targetArea.x
  72.         end
  73.        
  74.         if (self.baseY + self.height) > (targetArea.height) then
  75.             self.baseY = (targetArea.height) - self.height
  76.         elseif (self.baseY) < targetArea.y then
  77.             self.baseY = targetArea.y          
  78.         end
  79.     else
  80.         for i,e in ipairs(self:getInheritedChildren()) do
  81.             if e.dragging then
  82.                 return false
  83.             end
  84.         end
  85.        
  86.         local minX, minY, maxX, maxY = self.bounds.min.x, self.bounds.min.y, self.bounds.max.x, self.bounds.max.y
  87.        
  88.         if (self.x + maxX) > (targetArea.x + targetArea.width) then
  89.             self.x = (targetArea.x + targetArea.width) - maxX
  90.         elseif (self.x + minX) < targetArea.x then
  91.             self.x = targetArea.y - minX       
  92.         end
  93.        
  94.         if (self.y + maxY) > (targetArea.y + targetArea.height) then
  95.             self.y = (targetArea.y + targetArea.height) - maxY
  96.         elseif (self.y + minY) < targetArea.y then
  97.             self.y = targetArea.y - minY           
  98.         end
  99.     end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement