add Layout:placeNear convenience method

This commit is contained in:
airstruck
2016-01-19 14:36:25 -05:00
parent 78e02c190e
commit 93da8dedfa
2 changed files with 28 additions and 25 deletions

View File

@@ -274,6 +274,28 @@ function Layout:getWidgetAt (x, y, root)
if root:isAt(x, y) then return root end
end
function Layout:placeNear (left, top, w, h)
w, h = w or 0, h or 0
local root = self.root
-- place context menu left of cursor if there's no room to the right
local layoutWidth = root:getWidth()
local windowWidth, windowHeight = Backend.getWindowSize()
if left + w + layoutWidth > windowWidth then
left = left - layoutWidth - w
else
left = left + w
end
-- place context menu above cursor if there's no room below
local layoutHeight = root:getHeight()
if top + h + layoutHeight > windowHeight then
top = top - layoutHeight - h
else
top = top + h
end
root.left = left
root.top = top
end
-- Add handlers for keyboard shortcuts, tab focus, and mouse wheel scroll
function Layout:addDefaultHandlers ()
self.shortcuts = {}
@@ -290,25 +312,7 @@ function Layout:addDefaultHandlers ()
menu:bubbleEvent('PressStart', event)
-- make sure it fits in the window
-- TODO: open in a new borderless window under SDL?
local windowWidth, windowHeight = Backend.getWindowSize()
local left, top = event.x, event.y
local root = menu.menuLayout.root
-- place context menu left of cursor if there's no room to the right
local layoutWidth = root:getWidth()
if left + layoutWidth > windowWidth then
left = left - layoutWidth - 1
else
left = left + 1
end
-- place context menu above cursor if there's no room below
local layoutHeight = root:getHeight()
if top + layoutHeight > windowHeight then
top = top - layoutHeight - 1
else
top = top + 1
end
root.left = left
root.top = top
menu.menuLayout:placeNear(event.x - 1, event.y - 1, 2, 2)
return false
end)

View File

@@ -50,14 +50,13 @@ local function addLayoutChildren (self)
end
end
local isSubmenu = self.parentMenu and self.parentMenu.parentMenu
local x = isSubmenu and self:getWidth() or 0
local y = isSubmenu and 0 or self:getHeight()
root.left = self:getX() + x
root.top = self:getY() + y
root.height = height
root.width = textWidth + keyWidth + (root.padding or 0)
local isSubmenu = self.parentMenu and self.parentMenu.parentMenu
local w = isSubmenu and self:getWidth() or 0
local h = isSubmenu and 0 or self:getHeight()
self.menuLayout:placeNear(self:getX(), self:getY(), w, h)
end
local function show (self)