mirror of
https://github.com/airstruck/luigi.git
synced 2025-11-18 12:25:06 +00:00
unfinished window stuff
This commit is contained in:
@@ -76,6 +76,8 @@ end)
|
||||
local Backend = require 'luigi.backend'
|
||||
layout.menuQuit:onPress(Backend.quit)
|
||||
|
||||
Backend.setWindowIcon('logo.png')
|
||||
|
||||
-- show the main layout
|
||||
layout:show()
|
||||
|
||||
|
||||
18
example/window.lua
Normal file
18
example/window.lua
Normal file
@@ -0,0 +1,18 @@
|
||||
local Layout = require 'luigi.layout'
|
||||
|
||||
local window = Layout {
|
||||
type = 'window',
|
||||
icon = 'logo.png',
|
||||
title = 'Test window',
|
||||
width = 300,
|
||||
height = 200,
|
||||
minwidth = 200,
|
||||
minheight = 100,
|
||||
maxwidth = 640,
|
||||
maxheight = 480,
|
||||
}
|
||||
|
||||
window:show()
|
||||
|
||||
require 'luigi.backend'.run()
|
||||
|
||||
@@ -337,4 +337,91 @@ function Backend.show (layout)
|
||||
end)
|
||||
end
|
||||
|
||||
function Backend.setWindowMaximized (maximized)
|
||||
return maximized and sdl.maximizeWindow(window) or sdl.restoreWindow(window)
|
||||
end
|
||||
|
||||
function Backend.setWindowMinimized (minimized)
|
||||
return minimized and sdl.minimizeWindow(window) or sdl.restoreWindow(window)
|
||||
end
|
||||
|
||||
function Backend.setWindowBorderless (borderless)
|
||||
return sdl.setWindowBordered(window, not borderless)
|
||||
end
|
||||
|
||||
function Backend.setWindowFullscreen (fullscreen)
|
||||
return sdl.setWindowFullscreen(window, not not fullscreen)
|
||||
end
|
||||
|
||||
function Backend.setWindowGrab (grab)
|
||||
return sdl.setWindowGrab(window, not not grab)
|
||||
end
|
||||
|
||||
local SDL2_image = ffi.load 'SDL2_image'
|
||||
|
||||
function Backend.setWindowIcon (icon)
|
||||
-- XXX: is it safe to free this?
|
||||
local surface = ffi.gc(SDL2_image.IMG_Load(icon), sdl.freeSurface)
|
||||
|
||||
if surface == nil then
|
||||
error(ffi.string(sdl.getError()))
|
||||
end
|
||||
|
||||
sdl.setWindowIcon(window, surface)
|
||||
end
|
||||
|
||||
function Backend.setWindowMaxwidth (maxwidth)
|
||||
local w, h = IntOut(), IntOut()
|
||||
sdl.getWindowMaximumSize(window, w, h)
|
||||
sdl.setWindowMaximumSize(window, maxwidth, h[0] ~= nil and h[0] or math.huge)
|
||||
end
|
||||
|
||||
function Backend.setWindowMaxheight (maxheight)
|
||||
local w, h = IntOut(), IntOut()
|
||||
sdl.getWindowMaximumSize(window, w, h)
|
||||
sdl.setWindowMaximumSize(window, w[0] ~= nil and w[0] or math.huge, maxheight)
|
||||
end
|
||||
|
||||
function Backend.setWindowMinwidth (minwidth)
|
||||
local w, h = IntOut(), IntOut()
|
||||
sdl.getWindowMinimumSize(window, w, h)
|
||||
sdl.setWindowMinimumSize(window, minwidth, h[0] ~= nil and h[0] or 0)
|
||||
end
|
||||
|
||||
function Backend.setWindowMinheight (minheight)
|
||||
local w, h = IntOut(), IntOut()
|
||||
sdl.getWindowMinimumSize(window, w, h)
|
||||
sdl.setWindowMinimumSize(window, w[0] ~= nil and w[0] or 0, minheight)
|
||||
end
|
||||
|
||||
function Backend.setWindowTop (top)
|
||||
local x, y = IntOut(), IntOut()
|
||||
sdl.getWindowPosition(window, x, y)
|
||||
sdl.setWindowPosition(window, x[0] ~= nil and x[0] or 0, top)
|
||||
end
|
||||
|
||||
function Backend.setWindowLeft (left)
|
||||
local x, y = IntOut(), IntOut()
|
||||
sdl.getWindowPosition(window, x, y)
|
||||
sdl.setWindowPosition(window, left, y[0] ~= nil and y[0] or 0)
|
||||
end
|
||||
|
||||
function Backend.setWindowWidth (width)
|
||||
local w, h = IntOut(), IntOut()
|
||||
sdl.getWindowSize(window, w, h)
|
||||
sdl.setWindowSize(window, width, h[0] ~= nil and h[0] or 600)
|
||||
end
|
||||
|
||||
function Backend.setWindowHeight (height)
|
||||
local w, h = IntOut(), IntOut()
|
||||
sdl.getWindowSize(window, w, h)
|
||||
sdl.setWindowSize(window, w[0] ~= nil and w[0] or 800, height)
|
||||
end
|
||||
|
||||
function Backend.setWindowTitle (title)
|
||||
sdl.setWindowTitle(window, title)
|
||||
end
|
||||
|
||||
|
||||
|
||||
return Backend
|
||||
|
||||
@@ -128,7 +128,7 @@ end
|
||||
|
||||
-- returns icon coordinates and rectangle with remaining space
|
||||
function Renderer:positionIcon (widget, x1, y1, x2, y2)
|
||||
if not widget.icon then
|
||||
if not widget.attributes.icon then
|
||||
return nil, nil, x1, y1, x2, y2
|
||||
end
|
||||
|
||||
@@ -219,7 +219,7 @@ function Renderer:renderIconAndText (widget)
|
||||
local font, textX, textY = self:positionText(
|
||||
widget, x1, y1, x2, y2)
|
||||
|
||||
local icon = widget.icon and self:loadImage(widget.icon)
|
||||
local icon = widget.attributes.icon and self:loadImage(widget.icon)
|
||||
local text = widget.text
|
||||
local align = widget.align or ''
|
||||
local padding = widget.padding or 0
|
||||
|
||||
@@ -111,6 +111,7 @@ Widget.typeDecorators = {
|
||||
status = require(ROOT .. 'widget.status'),
|
||||
stepper = require(ROOT .. 'widget.stepper'),
|
||||
text = require(ROOT .. 'widget.text'),
|
||||
window = require(ROOT .. 'widget.window'),
|
||||
}
|
||||
|
||||
--[[--
|
||||
@@ -234,8 +235,8 @@ A table, optionally containing `get` and `set` functions (see `Attribute`).
|
||||
Return this widget for chaining.
|
||||
--]]--
|
||||
function Widget:defineAttribute (name, descriptor)
|
||||
local value = self[name]
|
||||
self.attributeDescriptors[name] = descriptor or {}
|
||||
local value = rawget(self, name)
|
||||
rawset(self, name, nil)
|
||||
self[name] = value
|
||||
return self
|
||||
|
||||
288
luigi/widget/window.lua
Normal file
288
luigi/widget/window.lua
Normal file
@@ -0,0 +1,288 @@
|
||||
--[[--
|
||||
Window widget.
|
||||
|
||||
Set properties of the window with this widget's attributes.
|
||||
This widget should only be used as the root widget of a layout.
|
||||
|
||||
@usage
|
||||
-- create a new window
|
||||
local window = Layout {
|
||||
type = 'window',
|
||||
icon = 'logo.png',
|
||||
text = 'Window Example',
|
||||
width = 800,
|
||||
height = 600,
|
||||
{ icon = 'logo.png', text = 'Window Example', align = 'middle center' },
|
||||
{ type = 'panel', flow = 'x', height = 'auto',
|
||||
{}, -- spacer
|
||||
{ type = 'button', id = 'quitButton', text = 'Quit' }
|
||||
}
|
||||
}
|
||||
|
||||
-- handle quit button
|
||||
window.quitButton:onPress(function ()
|
||||
os.exit()
|
||||
end)
|
||||
|
||||
-- show the window
|
||||
window:show()
|
||||
|
||||
@widget window
|
||||
--]]--
|
||||
|
||||
local ROOT = (...):gsub('[^.]*.[^.]*$', '')
|
||||
|
||||
local Backend = require(ROOT .. 'backend')
|
||||
|
||||
return function (self)
|
||||
--[[--
|
||||
Special Attributes
|
||||
|
||||
@section special
|
||||
--]]--
|
||||
|
||||
--[[--
|
||||
Maximized.
|
||||
|
||||
@attrib maximized
|
||||
--]]--
|
||||
local maximized
|
||||
|
||||
self:defineAttribute('maximized', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
maximized = value
|
||||
Backend.setWindowMaximized(maximized)
|
||||
end,
|
||||
get = function () return maximized end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Minimized.
|
||||
|
||||
@attrib minimized
|
||||
--]]--
|
||||
local minimized
|
||||
|
||||
self:defineAttribute('minimized', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
minimized = value
|
||||
Backend.setWindowMinimized(minimized)
|
||||
end,
|
||||
get = function () return minimized end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Borderless.
|
||||
|
||||
@attrib borderless
|
||||
--]]--
|
||||
local borderless
|
||||
|
||||
self:defineAttribute('borderless', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
borderless = value
|
||||
Backend.setWindowBorderless(borderless)
|
||||
end,
|
||||
get = function () return borderless end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Fullscreen.
|
||||
|
||||
@attrib fullscreen
|
||||
--]]--
|
||||
local fullscreen
|
||||
|
||||
self:defineAttribute('fullscreen', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
fullscreen = value
|
||||
Backend.setWindowFullscreen(fullscreen)
|
||||
end,
|
||||
get = function () return fullscreen end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Grab mouse.
|
||||
|
||||
@attrib grab
|
||||
--]]--
|
||||
local grab
|
||||
|
||||
self:defineAttribute('grab', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
grab = value
|
||||
Backend.setWindowGrab(grab)
|
||||
end,
|
||||
get = function () return grab end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Icon.
|
||||
|
||||
@attrib icon
|
||||
--]]--
|
||||
local icon
|
||||
|
||||
self:defineAttribute('icon', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
icon = value
|
||||
Backend.setWindowIcon(icon)
|
||||
end,
|
||||
get = function () return icon end
|
||||
})
|
||||
|
||||
self.attributes.icon = nil
|
||||
--[[--
|
||||
Maximum width.
|
||||
|
||||
@attrib maxwidth
|
||||
--]]--
|
||||
local maxwidth
|
||||
|
||||
self:defineAttribute('maxwidth', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
maxwidth = value
|
||||
Backend.setWindowMaxwidth(maxwidth)
|
||||
end,
|
||||
get = function () return maxwidth end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Maximum height.
|
||||
|
||||
@attrib maxheight
|
||||
--]]--
|
||||
local maxheight
|
||||
|
||||
self:defineAttribute('maxheight', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
maxheight = value
|
||||
Backend.setWindowMaxheight(maxheight)
|
||||
end,
|
||||
get = function () return maxheight end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Minimum width.
|
||||
|
||||
@attrib minwidth
|
||||
--]]--
|
||||
local minwidth
|
||||
|
||||
self:defineAttribute('minwidth', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
minwidth = value
|
||||
Backend.setWindowMinwidth(minwidth)
|
||||
end,
|
||||
get = function () return minwidth end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Minimum height.
|
||||
|
||||
@attrib minheight
|
||||
--]]--
|
||||
local minheight
|
||||
|
||||
self:defineAttribute('minheight', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
minheight = value
|
||||
Backend.setWindowMinheight(minheight)
|
||||
end,
|
||||
get = function () return minheight end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Top position.
|
||||
|
||||
@attrib top
|
||||
--]]--
|
||||
local top
|
||||
|
||||
self:defineAttribute('top', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
top = value
|
||||
Backend.setWindowTop(top)
|
||||
end,
|
||||
get = function () return top end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Left position.
|
||||
|
||||
@attrib left
|
||||
--]]--
|
||||
local left
|
||||
|
||||
self:defineAttribute('left', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
left = value
|
||||
Backend.setWindowLeft(left)
|
||||
end,
|
||||
get = function () return left end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Width.
|
||||
|
||||
@attrib width
|
||||
--]]--
|
||||
local width
|
||||
|
||||
self:defineAttribute('width', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
width = value
|
||||
Backend.setWindowWidth(width)
|
||||
end,
|
||||
get = function () return width end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Height.
|
||||
|
||||
@attrib height
|
||||
--]]--
|
||||
local height
|
||||
|
||||
self:defineAttribute('height', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
height = value
|
||||
Backend.setWindowHeight(height)
|
||||
end,
|
||||
get = function () return height end
|
||||
})
|
||||
|
||||
--[[--
|
||||
Title.
|
||||
|
||||
@attrib title
|
||||
--]]--
|
||||
local title
|
||||
|
||||
self:defineAttribute('title', {
|
||||
set = function (_, value)
|
||||
if not value then return end
|
||||
title = value
|
||||
Backend.setWindowTitle(title)
|
||||
end,
|
||||
get = function () return title end
|
||||
})
|
||||
|
||||
--[[--
|
||||
@section end
|
||||
--]]--
|
||||
end
|
||||
Reference in New Issue
Block a user