Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function DxElement:drag()
- if self.dragging then
- local cx, cy = getCursorPosition()
- cx = cx * SCREEN_WIDTH
- cy = cy * SCREEN_HEIGHT
- if not self.dragInitialX then
- self.dragInitialX, self.dragInitialY = cx - self.x, cy - self.y
- end
- if self.parent then
- self.baseX, self.baseY = self.baseX + (cx - self.dragInitialX) - self.x, self.baseY + (cy - self.dragInitialY) - self.y
- local rootElement = self.parent:getRootElement()
- local baseOffsetX, baseOffsetY = self.baseX, self.baseY
- local minX, minY, maxX, maxY = self.bounds.min.x, self.bounds.min.y, self.bounds.max.x, self.bounds.max.y
- local topLevelChild = self:getTopLevelChild()
- for i,e in ipairs(self:getInheritedParents()) do
- if e ~= rootElement then
- baseOffsetX, baseOffsetY = baseOffsetX + e.baseX, baseOffsetY + e.baseY
- end
- end
- if rootElement.x + baseOffsetX + maxX > SCREEN_WIDTH then
- self.baseX = SCREEN_WIDTH - rootElement.x - maxX - (baseOffsetX-self.baseX)
- end
- if rootElement.y + baseOffsetY + maxY > SCREEN_HEIGHT then
- self.baseY = SCREEN_HEIGHT - rootElement.y - maxY - (baseOffsetY-self.baseY)
- end
- if self.baseX + minX < -(rootElement.x) - (self ~= topLevelChild and topLevelChild.baseX or 0) then
- self.baseX = -(rootElement.x) - minX - (self ~= topLevelChild and topLevelChild.baseX or 0)
- end
- if self.baseY + minY < -(rootElement.y) - (self ~= topLevelChild and topLevelChild.baseY or 0) then
- self.baseY = -(rootElement.y) - minY - (self ~= topLevelChild and topLevelChild.baseY or 0)
- end
- else
- self.x, self.y = cx - self.dragInitialX, cy - self.dragInitialY
- end
- end
- end
- function DxElement:forceInBounds()
- if not self:getProperty("force_in_bounds") then
- return false
- end
- local targetArea = {
- x = 0,
- y = 0,
- width = SCREEN_WIDTH,
- height = SCREEN_HEIGHT
- }
- if self.parent then
- if self:getProperty("drag_preview") then
- if self.dragging then
- return false
- end
- end
- targetArea.width, targetArea.height = self.parent.width, self.parent.height
- if (self.baseX + self.width) > (targetArea.width) then
- self.baseX = (targetArea.width) - self.width
- elseif (self.baseX) < targetArea.x then
- self.baseX = targetArea.x
- end
- if (self.baseY + self.height) > (targetArea.height) then
- self.baseY = (targetArea.height) - self.height
- elseif (self.baseY) < targetArea.y then
- self.baseY = targetArea.y
- end
- else
- for i,e in ipairs(self:getInheritedChildren()) do
- if e.dragging then
- return false
- end
- end
- local minX, minY, maxX, maxY = self.bounds.min.x, self.bounds.min.y, self.bounds.max.x, self.bounds.max.y
- if (self.x + maxX) > (targetArea.x + targetArea.width) then
- self.x = (targetArea.x + targetArea.width) - maxX
- elseif (self.x + minX) < targetArea.x then
- self.x = targetArea.y - minX
- end
- if (self.y + maxY) > (targetArea.y + targetArea.height) then
- self.y = (targetArea.y + targetArea.height) - maxY
- elseif (self.y + minY) < targetArea.y then
- self.y = targetArea.y - minY
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement