mirror of
https://github.com/airstruck/luigi.git
synced 2025-11-18 12:25:06 +00:00
rework mouse buttons, fixes #18
This commit is contained in:
@@ -76,8 +76,6 @@ end)
|
||||
local Backend = require 'luigi.backend'
|
||||
layout.menuQuit:onPress(Backend.quit)
|
||||
|
||||
Backend.setWindowIcon('logo.png')
|
||||
|
||||
-- show the main layout
|
||||
layout:show()
|
||||
|
||||
|
||||
@@ -302,6 +302,18 @@ local isMouseDown = function ()
|
||||
return sdl.getMouseState(nil, nil) > 0
|
||||
end
|
||||
|
||||
local buttonIds = {
|
||||
[sdl.BUTTON_LEFT] = 'left',
|
||||
[sdl.BUTTON_MIDDLE] = 'middle',
|
||||
[sdl.BUTTON_RIGHT] = 'right',
|
||||
-- [sdl.BUTTON_X1] = 'x1',
|
||||
-- [sdl.BUTTON_X2] = 'x2',
|
||||
}
|
||||
|
||||
local function getMouseButtonId (value)
|
||||
return value and buttonIds[value] or value
|
||||
end
|
||||
|
||||
function Backend.show (layout)
|
||||
local input = layout.input
|
||||
|
||||
@@ -312,10 +324,10 @@ function Backend.show (layout)
|
||||
return input:handleReshape(layout, width, height)
|
||||
end)
|
||||
hook(layout, 'mousepressed', function (x, y, button)
|
||||
return input:handlePressStart(layout, button, x, y)
|
||||
return input:handlePressStart(layout, getMouseButtonId(button), x, y)
|
||||
end)
|
||||
hook(layout, 'mousereleased', function (x, y, button)
|
||||
return input:handlePressEnd(layout, button, x, y)
|
||||
return input:handlePressEnd(layout, getMouseButtonId(button), x, y)
|
||||
end)
|
||||
hook(layout, 'mousemoved', function (x, y, dx, dy)
|
||||
if isMouseDown() then
|
||||
|
||||
@@ -90,16 +90,22 @@ local getMouseButtonId, isMouseDown
|
||||
|
||||
if love._version_minor < 10 then
|
||||
getMouseButtonId = function (value)
|
||||
return value == 'l' and 1
|
||||
or value == 'r' and 2
|
||||
or value == 'm' and 3
|
||||
return value == 'l' and 'left'
|
||||
or value == 'r' and 'right'
|
||||
or value == 'm' and 'middle'
|
||||
or value == 'x1' and 4
|
||||
or value == 'x2' and 5
|
||||
or value
|
||||
end
|
||||
isMouseDown = function ()
|
||||
return love.mouse.isDown('l', 'r', 'm')
|
||||
end
|
||||
else
|
||||
getMouseButtonId = function (value)
|
||||
return value
|
||||
return value == 1 and 'left'
|
||||
or value == 2 and 'right'
|
||||
or value == 3 and 'middle'
|
||||
or value
|
||||
end
|
||||
isMouseDown = function ()
|
||||
return love.mouse.isDown(1, 2, 3)
|
||||
|
||||
@@ -12,7 +12,7 @@ local function renderSingle (self, x, y, font, text, color)
|
||||
love.graphics.push('all')
|
||||
love.graphics.setColor(color or { 0, 0, 0 })
|
||||
love.graphics.setFont(font.loveFont)
|
||||
love.graphics.print(text, x, y)
|
||||
love.graphics.print(text, math.floor(x), math.floor(y))
|
||||
love.graphics.pop()
|
||||
|
||||
self.height = font:getLineHeight()
|
||||
@@ -34,11 +34,14 @@ local function renderMulti (self, x, y, font, text, color, align, limit)
|
||||
local w = line.width
|
||||
|
||||
if align == 'left' then
|
||||
love.graphics.print(text, x, top + y)
|
||||
love.graphics.print(text,
|
||||
math.floor(x), math.floor(top + y))
|
||||
elseif align == 'right' then
|
||||
love.graphics.print(text, limit - w + x, top + y)
|
||||
love.graphics.print(text,
|
||||
math.floor(limit - w + x), math.floor(top + y))
|
||||
elseif align == 'center' then
|
||||
love.graphics.print(text, (limit - w) / 2 + x, top + y)
|
||||
love.graphics.print(text,
|
||||
math.floor((limit - w) / 2 + x), math.floor(top + y))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -111,10 +111,10 @@ function Input:handlePressedMove (layout, x, y)
|
||||
hit = nil
|
||||
widget = layout.root
|
||||
end
|
||||
for button = 1, 3 do
|
||||
for _, button in ipairs { 'left', 'middle', 'right' } do
|
||||
local originWidget = self.pressedWidgets[button]
|
||||
local passedWidget = self.passedWidgets[button]
|
||||
if originWidget then
|
||||
local passedWidget = self.passedWidgets[button]
|
||||
originWidget:bubbleEvent('PressDrag', {
|
||||
hit = hit,
|
||||
newTarget = widget,
|
||||
@@ -129,7 +129,9 @@ function Input:handlePressedMove (layout, x, y)
|
||||
x = x, y = y
|
||||
})
|
||||
else
|
||||
originWidget.pressed = (widget == originWidget) or nil
|
||||
if button == 'left' then
|
||||
originWidget.pressed = (widget == originWidget) or nil
|
||||
end
|
||||
if passedWidget then
|
||||
passedWidget:bubbleEvent('PressLeave', {
|
||||
hit = hit,
|
||||
@@ -161,10 +163,12 @@ function Input:handlePressStart (layout, button, x, y, widget, accelerator)
|
||||
widget = layout.root
|
||||
end
|
||||
if hit then
|
||||
widget.pressed = true
|
||||
self.pressedWidgets[button] = widget
|
||||
self.passedWidgets[button] = widget
|
||||
widget:focus()
|
||||
if button == 'left' then
|
||||
widget.pressed = true
|
||||
widget:focus()
|
||||
end
|
||||
end
|
||||
widget:bubbleEvent('PressStart', {
|
||||
hit = hit,
|
||||
@@ -184,7 +188,7 @@ function Input:handlePressEnd (layout, button, x, y, widget, accelerator)
|
||||
end
|
||||
local originWidget = self.pressedWidgets[button]
|
||||
if not originWidget then return end
|
||||
if hit then
|
||||
if hit and button == 'left' then
|
||||
originWidget.pressed = nil
|
||||
end
|
||||
widget:bubbleEvent('PressEnd', {
|
||||
|
||||
@@ -270,7 +270,7 @@ function Layout:addDefaultHandlers ()
|
||||
local acceleratedWidget = entry and entry[event.key]
|
||||
if acceleratedWidget then
|
||||
acceleratedWidget.hovered = true
|
||||
self.input:handlePressStart(self, event.key, event.x, event.y,
|
||||
self.input:handlePressStart(self, 'left', event.x, event.y,
|
||||
acceleratedWidget, event.key)
|
||||
return false
|
||||
end
|
||||
@@ -289,7 +289,7 @@ function Layout:addDefaultHandlers ()
|
||||
local widget = self.focusedWidget
|
||||
if widget and event.key == 'space' or event.key == ' '
|
||||
or event.key == 'return' then
|
||||
self.input:handlePressStart(self, event.key, event.x, event.y,
|
||||
self.input:handlePressStart(self, 'left', event.x, event.y,
|
||||
widget, event.key)
|
||||
return false
|
||||
end
|
||||
@@ -297,25 +297,25 @@ function Layout:addDefaultHandlers ()
|
||||
|
||||
self:onKeyRelease(function (event)
|
||||
|
||||
-- space / enter presses focused widget
|
||||
local widget = self.focusedWidget
|
||||
if widget and event.key == 'space' or event.key == ' '
|
||||
or event.key == 'return' then
|
||||
self.input:handlePressEnd(self, event.key, event.x, event.y,
|
||||
widget, event.key)
|
||||
return false
|
||||
end
|
||||
|
||||
-- accelerators
|
||||
local entry = self.accelerators[event.modifierFlags]
|
||||
local acceleratedWidget = entry and entry[event.key]
|
||||
|
||||
if acceleratedWidget then
|
||||
acceleratedWidget.hovered = false
|
||||
self.input:handlePressEnd(self, event.key, event.x, event.y,
|
||||
self.input:handlePressEnd(self, 'left', event.x, event.y,
|
||||
acceleratedWidget, event.key)
|
||||
return false
|
||||
end
|
||||
|
||||
-- space / enter presses focused widget
|
||||
local widget = self.focusedWidget
|
||||
if widget and event.key == 'space' or event.key == ' '
|
||||
or event.key == 'return' then
|
||||
self.input:handlePressEnd(self, 'left', event.x, event.y,
|
||||
widget, event.key)
|
||||
return false
|
||||
end
|
||||
end)
|
||||
|
||||
self:onWheelMove(function (event)
|
||||
|
||||
@@ -8,48 +8,76 @@ return function (config)
|
||||
local textColor = config.textColor or { 0, 0, 0 }
|
||||
local highlight = config.highlight or { 0x19, 0xAE, 0xFF }
|
||||
|
||||
local button_pressed = resources .. 'button_pressed.png'
|
||||
local button_focused = resources .. 'button_focused.png'
|
||||
local button_hovered = resources .. 'button_hovered.png'
|
||||
local button = resources .. 'button.png'
|
||||
|
||||
local check_checked_pressed = resources .. 'check_checked_pressed.png'
|
||||
local check_unchecked_pressed = resources .. 'check_unchecked_pressed.png'
|
||||
local check_checked_focused = resources .. 'check_checked_focused.png'
|
||||
local check_unchecked_focused = resources .. 'check_unchecked_focused.png'
|
||||
local check_checked = resources .. 'check_checked.png'
|
||||
local check_unchecked = resources .. 'check_unchecked.png'
|
||||
|
||||
local radio_checked_pressed = resources .. 'radio_checked_pressed.png'
|
||||
local radio_unchecked_pressed = resources .. 'radio_unchecked_pressed.png'
|
||||
local radio_checked_focused = resources .. 'radio_checked_focused.png'
|
||||
local radio_unchecked_focused = resources .. 'radio_unchecked_focused.png'
|
||||
local radio_checked = resources .. 'radio_checked.png'
|
||||
local radio_unchecked = resources .. 'radio_unchecked.png'
|
||||
|
||||
local triangle_left = resources .. 'triangle_left.png'
|
||||
local triangle_up = resources .. 'triangle_up.png'
|
||||
local triangle_right = resources .. 'triangle_right.png'
|
||||
local triangle_down = resources .. 'triangle_down.png'
|
||||
|
||||
local text_focused = resources .. 'text_focused.png'
|
||||
local text = resources .. 'text.png'
|
||||
|
||||
local function getButtonSlices (self)
|
||||
return self.pressed and resources .. 'button_pressed.png'
|
||||
or self.focused and resources .. 'button_focused.png'
|
||||
or self.hovered and resources .. 'button_hovered.png'
|
||||
or resources .. 'button.png'
|
||||
return self.pressed and button_pressed
|
||||
or self.focused and button_focused
|
||||
or self.hovered and button_hovered
|
||||
or button
|
||||
end
|
||||
|
||||
local function getCheckOrRadioIcon (self)
|
||||
local prefix = resources .. self.type
|
||||
local function getCheckIcon (self)
|
||||
if self.pressed then
|
||||
if self.value then
|
||||
return prefix .. '_checked_pressed.png'
|
||||
else
|
||||
return prefix .. '_unchecked_pressed.png'
|
||||
end
|
||||
elseif self.focused then
|
||||
if self.value then
|
||||
return prefix .. '_checked_focused.png'
|
||||
else
|
||||
return prefix .. '_unchecked_focused.png'
|
||||
end
|
||||
else
|
||||
if self.value then
|
||||
return prefix .. '_checked.png'
|
||||
else
|
||||
return prefix .. '_unchecked.png'
|
||||
end
|
||||
return self.value and check_checked_pressed
|
||||
or check_unchecked_pressed
|
||||
end
|
||||
if self.focused then
|
||||
return self.value and check_checked_focused
|
||||
or check_unchecked_focused
|
||||
end
|
||||
return self.value and check_checked or check_unchecked
|
||||
end
|
||||
|
||||
local function getControlHeight (self)
|
||||
return self.flow == 'x' and 32
|
||||
return self.flow == 'x' and self.minheight
|
||||
end
|
||||
|
||||
local function getControlWidth (self)
|
||||
return self.flow ~= 'x' and 32
|
||||
return self.flow ~= 'x' and self.minwidth
|
||||
end
|
||||
|
||||
local function getMenuItemBackground (self)
|
||||
return self.active and highlight
|
||||
end
|
||||
|
||||
local function getRadioIcon (self)
|
||||
if self.pressed then
|
||||
return self.value and radio_checked_pressed
|
||||
or radio_unchecked_pressed
|
||||
end
|
||||
if self.focused then
|
||||
return self.value and radio_checked_focused
|
||||
or radio_unchecked_focused
|
||||
end
|
||||
return self.value and radio_checked or radio_unchecked
|
||||
end
|
||||
|
||||
local function getSashBackground (self)
|
||||
return self.hovered and highlight or lineColor
|
||||
end
|
||||
@@ -63,22 +91,22 @@ return function (config)
|
||||
end
|
||||
|
||||
local function getStepperBeforeIcon (self)
|
||||
return self.parent.flow == 'x' and resources .. 'triangle_left.png'
|
||||
or resources .. 'triangle_up.png'
|
||||
return self.parent.flow == 'x' and triangle_left or triangle_up
|
||||
end
|
||||
|
||||
local function getStepperAfterIcon (self)
|
||||
return self.parent.flow == 'x' and resources .. 'triangle_right.png'
|
||||
or resources .. 'triangle_down.png'
|
||||
return self.parent.flow == 'x' and triangle_right or triangle_down
|
||||
end
|
||||
|
||||
local function getTextSlices (self)
|
||||
return self.focused and resources .. 'text_focused.png'
|
||||
or resources .. 'text.png'
|
||||
return self.focused and text_focused or text
|
||||
end
|
||||
|
||||
return {
|
||||
control = {
|
||||
|
||||
-- generic types for widgets to inherit
|
||||
|
||||
Control = {
|
||||
flow = 'x',
|
||||
height = getControlHeight,
|
||||
width = getControlWidth,
|
||||
@@ -89,28 +117,29 @@ return function (config)
|
||||
margin = 2,
|
||||
color = textColor,
|
||||
},
|
||||
|
||||
Line = {
|
||||
minheight = 24,
|
||||
margin = 0,
|
||||
padding = 4,
|
||||
align = 'left middle',
|
||||
},
|
||||
|
||||
-- widget types
|
||||
|
||||
button = {
|
||||
type = 'control',
|
||||
type = { 'Control' },
|
||||
padding = 6,
|
||||
slices = getButtonSlices,
|
||||
focusable = true,
|
||||
},
|
||||
check = {
|
||||
type = 'control',
|
||||
type = { 'Line', 'Control' },
|
||||
focusable = true,
|
||||
icon = getCheckOrRadioIcon,
|
||||
margin = 0,
|
||||
padding = 4,
|
||||
align = 'left middle',
|
||||
minheight = 24,
|
||||
icon = getCheckIcon,
|
||||
},
|
||||
label = {
|
||||
type = 'control',
|
||||
align = 'left bottom',
|
||||
margin = 0,
|
||||
padding = 4,
|
||||
minheight = 24,
|
||||
height = 24,
|
||||
type = { 'Line', 'Control' },
|
||||
},
|
||||
menu = {
|
||||
flow = 'x',
|
||||
@@ -132,23 +161,18 @@ return function (config)
|
||||
color = textColor,
|
||||
},
|
||||
progress = {
|
||||
type = 'control',
|
||||
type = { 'Control' },
|
||||
slices = resources .. 'button_pressed.png',
|
||||
},
|
||||
['progress.bar'] = {
|
||||
slices = resources .. 'progress.png',
|
||||
minwidth = 12,
|
||||
minheight= 22,
|
||||
minheight = 22,
|
||||
},
|
||||
radio = {
|
||||
type = 'control',
|
||||
type = { 'Line', 'Control' },
|
||||
focusable = true,
|
||||
color = textColor,
|
||||
icon = getCheckOrRadioIcon,
|
||||
margin = 0,
|
||||
padding = 4,
|
||||
align = 'left middle',
|
||||
minheight = 24,
|
||||
icon = getRadioIcon,
|
||||
},
|
||||
sash = {
|
||||
background = getSashBackground,
|
||||
@@ -156,36 +180,32 @@ return function (config)
|
||||
width = getSashWidth,
|
||||
},
|
||||
slider = {
|
||||
type = 'control',
|
||||
type = { 'Control' },
|
||||
slices = resources .. 'button_pressed.png',
|
||||
},
|
||||
['slider.thumb'] = {
|
||||
type = 'button',
|
||||
type = { 'button' },
|
||||
align = 'middle center',
|
||||
margin = 0,
|
||||
minwidth = 32,
|
||||
minheight = 32,
|
||||
},
|
||||
status = {
|
||||
background = backColor,
|
||||
color = textColor,
|
||||
align = 'left middle',
|
||||
padding = 4,
|
||||
height = 22,
|
||||
type = { 'Line', 'Control' },
|
||||
},
|
||||
stepper = {
|
||||
type = 'control',
|
||||
type = { 'Control' },
|
||||
slices = resources .. 'button_pressed.png',
|
||||
},
|
||||
['stepper.after'] = {
|
||||
type = 'button',
|
||||
type = { 'button' },
|
||||
icon = getStepperAfterIcon,
|
||||
margin = 0,
|
||||
minwidth = 32,
|
||||
minheight = 32,
|
||||
},
|
||||
['stepper.before'] = {
|
||||
type = 'button',
|
||||
type = { 'button' },
|
||||
icon = getStepperBeforeIcon,
|
||||
margin = 0,
|
||||
minwidth = 32,
|
||||
@@ -205,7 +225,7 @@ return function (config)
|
||||
color = textColor,
|
||||
},
|
||||
text = {
|
||||
type = 'control',
|
||||
type = { 'Control' },
|
||||
align = 'left middle',
|
||||
slices = getTextSlices,
|
||||
padding = 6,
|
||||
|
||||
@@ -13,7 +13,8 @@ standard themes, the widget's value should be indicated in some other way.
|
||||
--]]--
|
||||
|
||||
return function (self)
|
||||
self:onPress(function ()
|
||||
self:onPress(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
self.value = not self.value
|
||||
end)
|
||||
|
||||
|
||||
@@ -87,6 +87,7 @@ local function deactivateSiblings (target)
|
||||
end
|
||||
|
||||
local function activate (event, ignoreIfNoneOpen)
|
||||
if event.button and event.button ~= 'left' then return end
|
||||
local target = event.target
|
||||
|
||||
while target.parent
|
||||
@@ -120,12 +121,13 @@ local function registerLayoutEvents (self)
|
||||
if self.parentMenu == self.rootMenu then
|
||||
deactivateSiblings(self.rootMenu[1])
|
||||
end
|
||||
else
|
||||
elseif event.button == 'left' then
|
||||
activate(event)
|
||||
end
|
||||
end)
|
||||
|
||||
menuLayout:onPress(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
for widget in event.target:eachAncestor(true) do
|
||||
if widget.type == 'menu.item' and #widget.items == 0 then
|
||||
menuLayout:hide()
|
||||
@@ -135,6 +137,7 @@ local function registerLayoutEvents (self)
|
||||
end)
|
||||
|
||||
menuLayout:onPressEnd(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
for widget in event.target:eachAncestor(true) do
|
||||
if widget.type == 'menu.item' and #widget.items == 0
|
||||
and event.target ~= event.origin then
|
||||
|
||||
@@ -64,7 +64,8 @@ in the same group change to `false`.
|
||||
@section end
|
||||
--]]--
|
||||
|
||||
self:onPress(function ()
|
||||
self:onPress(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
for _, widget in ipairs(groups[self.group]) do
|
||||
widget.value = widget == self
|
||||
end
|
||||
|
||||
@@ -63,6 +63,7 @@ return function (self)
|
||||
end)
|
||||
|
||||
self:onPressDrag(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
local axis = self.parent.flow
|
||||
if axis == 'x' then
|
||||
dimension = 'width'
|
||||
|
||||
@@ -21,7 +21,8 @@ return function (self)
|
||||
type = 'slider.thumb',
|
||||
}
|
||||
|
||||
local function unpress ()
|
||||
local function unpress (event)
|
||||
if event.button ~= 'left' then return end
|
||||
thumb.pressed = false -- don't make the thumb appear pushed in
|
||||
return false -- don't press thumb on focused keyboard activation
|
||||
end
|
||||
@@ -39,6 +40,7 @@ return function (self)
|
||||
end)
|
||||
|
||||
local function press (event)
|
||||
if event.button ~= 'left' then return end
|
||||
local x1, y1, w, h = self:getRectangle(true, true)
|
||||
local x2, y2 = x1 + w, y1 + h
|
||||
if self.flow == 'x' then
|
||||
|
||||
@@ -94,10 +94,12 @@ Contains the index in `items` of the item being displayed.
|
||||
end
|
||||
|
||||
before:onPress(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
if self.flow == 'x' then decrement() else increment() end
|
||||
end)
|
||||
|
||||
after:onPress(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
if self.flow == 'x' then increment() else decrement() end
|
||||
end)
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ local function scrollToCaret (self)
|
||||
end
|
||||
|
||||
local function findCaretFromText (self, text)
|
||||
local font = self.fontData
|
||||
local font = self:getFont()
|
||||
local x = self:getRectangle(true, true)
|
||||
return #text, font:getAdvance(text) + x - self.scrollX
|
||||
end
|
||||
@@ -178,12 +178,7 @@ local function insertText (self, newText)
|
||||
end
|
||||
|
||||
return function (self)
|
||||
if not self.fontData then
|
||||
self.fontData = Backend.Font(self.font, self.size)
|
||||
end
|
||||
|
||||
self.value = tostring(self.value or self.text or '')
|
||||
|
||||
self.text = ''
|
||||
|
||||
--[[--
|
||||
@@ -201,12 +196,14 @@ This color is used to indicate the selected range of text.
|
||||
|
||||
@attrib highlight
|
||||
--]]--
|
||||
|
||||
self:defineAttribute('highlight')
|
||||
|
||||
--[[--
|
||||
@section end
|
||||
--]]--
|
||||
if not self.highlight then
|
||||
self.highlight = { 0x80, 0x80, 0x80 }
|
||||
self.highlight = { 0x80, 0x80, 0x80, 0x80 }
|
||||
end
|
||||
|
||||
self.scrollX = 0
|
||||
@@ -214,11 +211,13 @@ This color is used to indicate the selected range of text.
|
||||
setCaretFromText(self, self.value)
|
||||
|
||||
self:onPressStart(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
self.startIndex, self.startX = findCaretFromPoint(self, event.x)
|
||||
self.endIndex, self.endX = self.startIndex, self.startX
|
||||
end)
|
||||
|
||||
self:onPressDrag(function (event)
|
||||
if event.button ~= 'left' then return end
|
||||
self.endIndex, self.endX = findCaretFromPoint(self, event.x)
|
||||
scrollToCaret(self)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user