more window stuff

This commit is contained in:
nobody
2015-12-17 20:30:00 -05:00
parent e2602348df
commit 8dd46e2bbc
6 changed files with 68 additions and 25 deletions

View File

@@ -3,6 +3,7 @@ local Backend = require 'luigi.backend'
local window = Layout {
type = 'window',
background = { 255, 255, 255 },
icon = 'logo.png',
title = 'Test window',
width = 300,
@@ -11,9 +12,37 @@ local window = Layout {
minheight = 100,
maxwidth = 640,
maxheight = 480,
{ type = 'button', id = 'maximize', text = 'Maximize' },
{ type = 'button', id = 'minimize', text = 'Minimize' },
{ type = 'button', id = 'restore', text = 'Restore' },
left = 400,
top = 400,
{ type = 'panel',
{
text = 'This is an example of the "window" widget type.',
align = 'middle center', wrap = true,
},
{ flow = 'x', height = 'auto',
{ type = 'button', id = 'maximize', text = 'Maximize' },
{ type = 'button', id = 'minimize', text = 'Minimize' },
{ type = 'button', id = 'restore', text = 'Restore' },
},
{ flow = 'x', height = 'auto',
{
{ type = 'label', text = 'Left' },
{ type = 'text', id = 'left' },
},
{
{ type = 'label', text = 'Top' },
{ type = 'text', id = 'top' },
},
{
{ type = 'label', text = 'Width' },
{ type = 'text', id = 'width' },
},
{
{ type = 'label', text = 'Height' },
{ type = 'text', id = 'height' },
},
},
}
}
window.maximize:onPress(function ()
@@ -25,6 +54,20 @@ end)
window.restore:onPress(function ()
window.root.maximized = false
end)
window:onReshape(function (event)
local w, h = Backend:getWindowSize()
-- use widget.attributes to do a raw update, avoid firing onChange
window.width.attributes.value = tostring(w)
window.height.attributes.value = tostring(h)
end)
window:onChange(function (event)
local target = event.target
if target.type ~= 'text' then return end
local id = target.id
if id and window.root.attributeDescriptors[id] then
window.root[id] = tonumber(event.value)
end
end)
window:show()

View File

@@ -1,9 +1,8 @@
local ROOT = (...):gsub('[^.]*$', '')
local Backend
if _G.love and _G.love._version_minor > 8 then
return require(ROOT .. 'backend.love')
else
return require(ROOT .. 'backend.ffisdl')
end

View File

@@ -11,8 +11,6 @@ local Renderer = Base:extend()
local imageCache = {}
local sliceCache = {}
local function intersectScissor (x, y, w, h)
local sx, sy, sw, sh = Backend.getScissor()
if not sx then
@@ -128,7 +126,7 @@ end
-- returns icon coordinates and rectangle with remaining space
function Renderer:positionIcon (widget, x1, y1, x2, y2)
if not widget.attributes.icon then
if not widget.icon then
return nil, nil, x1, y1, x2, y2
end
@@ -167,11 +165,7 @@ function Renderer:positionText (widget, x1, y1, x2, y2)
return nil, nil, x1, y1, x2, y2
end
if not widget.fontData then
widget.fontData = Font(widget.font, widget.size)
end
local font = widget.fontData
local font = widget:getFont()
local align = widget.align or ''
local horizontal = 'left'
@@ -206,12 +200,9 @@ function Renderer:positionText (widget, x1, y1, x2, y2)
end
function Renderer:renderIconAndText (widget)
if not (widget.icon or widget.text) then return end
local x, y, w, h = widget:getRectangle(true, true)
-- if the drawable area has no width or height, don't render
if w < 1 or h < 1 then
return
end
if w < 1 or h < 1 then return end
-- calculate position for icon and text based on alignment and padding
local iconX, iconY, x1, y1, x2, y2 = self:positionIcon(
@@ -219,7 +210,7 @@ function Renderer:renderIconAndText (widget)
local font, textX, textY = self:positionText(
widget, x1, y1, x2, y2)
local icon = widget.attributes.icon and self:loadImage(widget.icon)
local icon = widget.icon and self:loadImage(widget.icon)
local text = widget.text
local align = widget.align or ''
local padding = widget.padding or 0

View File

@@ -634,6 +634,13 @@ function Widget:getContentHeight ()
return height
end
function Widget:getFont ()
if not self.fontData then
self.fontData = Font(self.font, self.size)
end
return self.fontData
end
--[[--
Get x/y/width/height values describing a rectangle within the widget.

View File

@@ -277,7 +277,7 @@ This color is used to indicate the selected range of text.
local startX, endX = self.startX or 0, self.endX or 0
local x, y, w, h = self:getRectangle(true, true)
local width, height = endX - startX, h
local font = self.fontData
local font = self:getFont()
local color = self.color or { 0, 0, 0, 255 }
local textTop = math.floor(y + (h - font:getLineHeight()) / 2)

View File

@@ -51,6 +51,7 @@ Set to `false` to restore the size and position.
set = function (_, value)
if value == nil then return end
Backend.setWindowMaximized(value)
self.layout.root:reshape()
end,
get = Backend.getWindowMaximized
})
@@ -79,6 +80,7 @@ You can't change the border state of a fullscreen window.
set = function (_, value)
if value == nil then return end
Backend.setWindowBorderless(value)
self.layout.root:reshape()
end,
get = Backend.getWindowBorderless
})
@@ -93,6 +95,7 @@ of the window.
set = function (_, value)
if value == nil then return end
Backend.setWindowFullscreen(value)
self.layout.root:reshape()
end,
get = Backend.getWindowFullscreen
})
@@ -127,10 +130,8 @@ Window icon. Should be a string containing a path to an image.
icon = value
Backend.setWindowIcon(value)
end,
get = function () return icon end
-- get = function () return false end
})
self.attributes.icon = nil
--[[--
Maximum width of the window's client area.
@@ -193,7 +194,7 @@ Position of the window's top edge.
if value == nil then return end
Backend.setWindowTop(value)
end,
get = Backend.getWindowTop
-- get = Backend.getWindowTop
})
--[[--
@@ -206,7 +207,7 @@ Position of the window's left edge.
if value == nil then return end
Backend.setWindowLeft(value)
end,
get = Backend.getWindowLeft
-- get = Backend.getWindowLeft
})
--[[--
@@ -218,6 +219,7 @@ Width of the window's content area.
set = function (_, value)
if value == nil then return end
Backend.setWindowWidth(value)
self.layout.root:reshape()
end,
get = Backend.getWindowWidth
})
@@ -231,6 +233,7 @@ Height of the window's content area.
set = function (_, value)
if value == nil then return end
Backend.setWindowHeight(value)
self.layout.root:reshape()
end,
get = Backend.getWindowHeight
})