Rework to UIMessageBox
This commit is contained in:
parent
7fb2f6deb5
commit
abb6f59568
|
@ -17,7 +17,9 @@ MainWindow
|
||||||
|
|
||||||
TextList
|
TextList
|
||||||
id: characterList
|
id: characterList
|
||||||
anchors.fill: parent
|
anchors.top: parent.top
|
||||||
|
anchors.left: parent.left
|
||||||
|
anchors.right: characterListScrollBar.left
|
||||||
anchors.bottom: accountStatusLabel.top
|
anchors.bottom: accountStatusLabel.top
|
||||||
margin-bottom: 5
|
margin-bottom: 5
|
||||||
padding: 1
|
padding: 1
|
||||||
|
@ -26,9 +28,10 @@ MainWindow
|
||||||
|
|
||||||
VerticalScrollBar
|
VerticalScrollBar
|
||||||
id: characterListScrollBar
|
id: characterListScrollBar
|
||||||
anchors.top: characterList.top
|
anchors.top: parent.top
|
||||||
anchors.bottom: characterList.bottom
|
anchors.bottom: accountStatusLabel.top
|
||||||
anchors.right: characterList.right
|
anchors.right: parent.right
|
||||||
|
margin-bottom: 5
|
||||||
step: 14
|
step: 14
|
||||||
pixels-scroll: true
|
pixels-scroll: true
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
MessageBoxLabel < Label
|
MessageBoxLabel < Label
|
||||||
id: messageBoxLabel
|
id: messageBoxLabel
|
||||||
|
text-auto-resize: true
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
anchors.horizontalCenter: parent.horizontalCenter
|
||||||
anchors.top: parent.top
|
anchors.top: parent.top
|
||||||
|
|
||||||
MessageBoxRightButton < Button
|
MessageBoxButtonHolder < UIWidget
|
||||||
id: messageBoxRightButton
|
id: buttonHolder
|
||||||
|
margin-top: 10
|
||||||
anchors.bottom: parent.bottom
|
anchors.bottom: parent.bottom
|
||||||
anchors.right: parent.right
|
|
||||||
width: 64
|
MessageBoxButton < Button
|
||||||
visible: true
|
margin-left: 10
|
||||||
|
width: 80
|
||||||
|
|
|
@ -3,56 +3,78 @@ if not UIWindow then dofile 'uiwindow' end
|
||||||
-- @docclass
|
-- @docclass
|
||||||
UIMessageBox = extends(UIWindow)
|
UIMessageBox = extends(UIWindow)
|
||||||
|
|
||||||
MessageBoxOk = 1
|
|
||||||
MessageBoxCancel = 2
|
|
||||||
|
|
||||||
-- messagebox cannot be created from otui files
|
-- messagebox cannot be created from otui files
|
||||||
UIMessageBox.create = nil
|
UIMessageBox.create = nil
|
||||||
|
|
||||||
function UIMessageBox.display(title, message, flags)
|
function UIMessageBox.display(title, message, buttons, onEnterCallback, onEscapeCallback)
|
||||||
local messagebox = UIMessageBox.internalCreate()
|
local messageBox = UIMessageBox.internalCreate()
|
||||||
rootWidget:addChild(messagebox)
|
rootWidget:addChild(messageBox)
|
||||||
|
|
||||||
messagebox:setStyle('MainWindow')
|
messageBox:setStyle('MainWindow')
|
||||||
messagebox:setText(title)
|
messageBox:setText(title)
|
||||||
|
|
||||||
local messageLabel = g_ui.createWidget('MessageBoxLabel', messagebox)
|
local messageLabel = g_ui.createWidget('MessageBoxLabel', messageBox)
|
||||||
messageLabel:setText(message)
|
messageLabel:setText(message)
|
||||||
messageLabel:resizeToText()
|
|
||||||
|
|
||||||
-- setup messagebox first button
|
local buttonsWidth = 0
|
||||||
local buttonRight = g_ui.createWidget('MessageBoxRightButton', messagebox)
|
local buttonsHeight = 0
|
||||||
|
|
||||||
if flags == MessageBoxOk then
|
local anchor = AnchorRight
|
||||||
buttonRight:setText('Ok')
|
if buttons.anchor then anchor = buttons.anchor end
|
||||||
connect(buttonRight, { onClick = function(self) self:getParent():ok() end })
|
|
||||||
connect(messagebox, { onEnter = function(self) self:ok() end })
|
local buttonHolder = g_ui.createWidget('MessageBoxButtonHolder', messageBox)
|
||||||
connect(messagebox, { onEscape = function(self) self:ok() end })
|
buttonHolder:addAnchor(anchor, 'parent', anchor)
|
||||||
elseif flags == MessageBoxCancel then
|
|
||||||
buttonRight:setText('Cancel')
|
for i=1,#buttons do
|
||||||
connect(buttonRight, { onClick = function(self) self:getParent():cancel() end })
|
local button = messageBox:addButton(buttons[i].text, buttons[i].callback)
|
||||||
connect(messagebox, { onEnter = function(self) self:cancel() end })
|
if i == 1 then
|
||||||
connect(messagebox, { onEscape = function(self) self:cancel() end })
|
button:setMarginLeft(0)
|
||||||
|
button:addAnchor(AnchorBottom, 'parent', AnchorBottom)
|
||||||
|
button:addAnchor(AnchorLeft, 'parent', AnchorLeft)
|
||||||
|
buttonsHeight = button:getHeight()
|
||||||
|
else
|
||||||
|
button:addAnchor(AnchorBottom, 'prev', AnchorBottom)
|
||||||
|
button:addAnchor(AnchorLeft, 'prev', AnchorRight)
|
||||||
|
end
|
||||||
|
buttonsWidth = buttonsWidth + button:getWidth() + button:getMarginLeft()
|
||||||
end
|
end
|
||||||
|
|
||||||
messagebox:setWidth(math.max(messageLabel:getWidth(), messagebox:getTextSize().width) + messagebox:getPaddingLeft() + messagebox:getPaddingRight())
|
buttonHolder:setWidth(buttonsWidth)
|
||||||
messagebox:setHeight(messageLabel:getHeight() + messagebox:getPaddingTop() + messagebox:getPaddingBottom() + buttonRight:getHeight() + 10)
|
buttonHolder:setHeight(buttonsHeight)
|
||||||
|
|
||||||
--messagebox:lock()
|
if onEnterCallback then connect(messageBox, { onEnter = onEnterCallback }) end
|
||||||
|
if onEscapeCallback then connect(messageBox, { onEscape = onEscapeCallback }) end
|
||||||
|
|
||||||
return messagebox
|
messageBox:setWidth(math.max(messageLabel:getWidth(), messageBox:getTextSize().width, buttonHolder:getWidth()) + messageBox:getPaddingLeft() + messageBox:getPaddingRight())
|
||||||
|
messageBox:setHeight(messageLabel:getHeight() + messageBox:getPaddingTop() + messageBox:getPaddingBottom() + buttonHolder:getHeight() + buttonHolder:getMarginTop())
|
||||||
|
return messageBox
|
||||||
end
|
end
|
||||||
|
|
||||||
function displayInfoBox(title, message)
|
function displayInfoBox(title, message)
|
||||||
return UIMessageBox.display(title, message, MessageBoxOk)
|
local defaultCallback = function(self) self:ok() end
|
||||||
|
return UIMessageBox.display(title, message, {{text='Ok', callback=defaultCallback}}, defaultCallback, defaultCallback)
|
||||||
end
|
end
|
||||||
|
|
||||||
function displayErrorBox(title, message)
|
function displayErrorBox(title, message)
|
||||||
return UIMessageBox.display(title, message, MessageBoxOk)
|
local defaultCallback = function(self) self:ok() end
|
||||||
|
return UIMessageBox.display(title, message, {{text='Ok', callback=defaultCallback}}, defaultCallback, defaultCallback)
|
||||||
end
|
end
|
||||||
|
|
||||||
function displayCancelBox(title, message)
|
function displayCancelBox(title, message)
|
||||||
return UIMessageBox.display(title, message, MessageBoxCancel)
|
local defaultCallback = function(self) self:cancel() end
|
||||||
|
return UIMessageBox.display(title, message, {{text='Cancel', callback=defaultCallback}}, defaultCallback, defaultCallback)
|
||||||
|
end
|
||||||
|
|
||||||
|
function displayGeneralBox(title, message, buttons, onEnterCallback, onEscapeCallback)
|
||||||
|
return UIMessageBox.display(title, message, buttons, onEnterCallback, onEscapeCallback)
|
||||||
|
end
|
||||||
|
|
||||||
|
function UIMessageBox:addButton(text, callback)
|
||||||
|
local buttonHolder = self:getChildById('buttonHolder')
|
||||||
|
local button = g_ui.createWidget('MessageBoxButton', buttonHolder)
|
||||||
|
button:setText(text)
|
||||||
|
connect(button, { onClick = callback })
|
||||||
|
return button
|
||||||
end
|
end
|
||||||
|
|
||||||
function UIMessageBox:ok()
|
function UIMessageBox:ok()
|
||||||
|
|
|
@ -263,4 +263,21 @@ function tr(s, ...)
|
||||||
return string.format(s, ...)
|
return string.format(s, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function getOppositeAnchor(anchor)
|
||||||
|
if anchor == AnchorLeft then
|
||||||
|
return AnchorRight
|
||||||
|
elseif anchor == AnchorRight then
|
||||||
|
return AnchorLeft
|
||||||
|
elseif anchor == AnchorTop then
|
||||||
|
return AnchorBottom
|
||||||
|
elseif anchor == AnchorBottom then
|
||||||
|
return AnchorTop
|
||||||
|
elseif anchor == AnchorVerticalCenter then
|
||||||
|
return AnchorHorizontalCenter
|
||||||
|
elseif anchor == AnchorHorizontalCenter then
|
||||||
|
return AnchorVerticalCenter
|
||||||
|
end
|
||||||
|
return anchor
|
||||||
|
end
|
||||||
|
|
||||||
-- @}
|
-- @}
|
|
@ -13,8 +13,6 @@ exitWindow = nil
|
||||||
|
|
||||||
function init()
|
function init()
|
||||||
g_ui.importStyle('styles/countwindow.otui')
|
g_ui.importStyle('styles/countwindow.otui')
|
||||||
g_ui.importStyle('styles/logoutwindow.otui')
|
|
||||||
g_ui.importStyle('styles/exitwindow.otui')
|
|
||||||
|
|
||||||
connect(g_game, { onGameStart = show,
|
connect(g_game, { onGameStart = show,
|
||||||
onGameEnd = hide }, true)
|
onGameEnd = hide }, true)
|
||||||
|
@ -122,32 +120,18 @@ function tryExit()
|
||||||
if exitWindow then
|
if exitWindow then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
exitWindow = g_ui.createWidget('ExitWindow', rootWidget)
|
|
||||||
local exitButton = exitWindow:getChildById('buttonExit')
|
|
||||||
local logButton = exitWindow:getChildById('buttonLogout')
|
|
||||||
local cancelButton = exitWindow:getChildById('buttonCancel')
|
|
||||||
|
|
||||||
local exitFunc = function()
|
local exitFunc = function() logout() forceExit() end
|
||||||
logout() -- try logout anyway
|
local logoutFunc = function() logout() exitWindow:destroy() exitWindow = nil end
|
||||||
forceExit()
|
local cancelFunc = function() exitWindow:destroy() exitWindow = nil end
|
||||||
end
|
|
||||||
local logoutFunc = function()
|
|
||||||
logout()
|
|
||||||
logButton:getParent():destroy()
|
|
||||||
exitWindow = nil
|
|
||||||
end
|
|
||||||
local cancelFunc = function()
|
|
||||||
cancelButton:getParent():destroy()
|
|
||||||
exitWindow = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
exitWindow.onEscape = cancelFunc
|
exitWindow = displayGeneralBox('Exit', tr("If you shut down the program, your character might stay in the game.\nClick on 'Logout' to ensure that you character leaves the game properly.\nClick on 'Exit' if you want to exit the program without logging out your character."), {
|
||||||
exitWindow.onEnter = logoutFunc
|
{ text='Force Exit', callback=exitFunc },
|
||||||
|
{ text='Logout', callback=logoutFunc },
|
||||||
|
{ text='Cancel', callback=cancelFunc },
|
||||||
|
anchor=AnchorHorizontalCenter}, logoutFunc, cancelFunc)
|
||||||
|
|
||||||
exitButton.onClick = exitFunc
|
return true
|
||||||
logButton.onClick = logoutFunc
|
|
||||||
cancelButton.onClick = cancelFunc
|
|
||||||
return true -- signal closing
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function logout()
|
function logout()
|
||||||
|
@ -161,25 +145,14 @@ function tryLogout()
|
||||||
if logoutWindow then
|
if logoutWindow then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
logoutWindow = g_ui.createWidget('LogoutWindow', rootWidget)
|
|
||||||
local yesButton = logoutWindow:getChildById('buttonYes')
|
|
||||||
local noButton = logoutWindow:getChildById('buttonNo')
|
|
||||||
|
|
||||||
local logoutFunc = function()
|
local yesCallback = function() logout() logoutWindow:destroy() logoutWindow=nil end
|
||||||
logout()
|
local noCallback = function() logoutWindow:destroy() logoutWindow=nil end
|
||||||
yesButton:getParent():destroy()
|
|
||||||
logoutWindow = nil
|
|
||||||
end
|
|
||||||
local cancelFunc = function()
|
|
||||||
noButton:getParent():destroy()
|
|
||||||
logoutWindow = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
logoutWindow.onEnter = logoutFunc
|
logoutWindow = displayGeneralBox('Logout', tr('Are you sure you want to logout?'), {
|
||||||
logoutWindow.onEscape = cancelFunc
|
{ text='Yes', callback=yesCallback },
|
||||||
|
{ text='No', callback=noCallback },
|
||||||
yesButton.onClick = logoutFunc
|
anchor=AnchorHorizontalCenter}, yesCallback, noCallback)
|
||||||
noButton.onClick = cancelFunc
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function onMouseGrabberRelease(self, mousePosition, mouseButton)
|
function onMouseGrabberRelease(self, mousePosition, mouseButton)
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
ExitWindow < MainWindow
|
|
||||||
id: exitWindow
|
|
||||||
!text: tr('Exit')
|
|
||||||
size: 570 135
|
|
||||||
|
|
||||||
Label
|
|
||||||
!text: tr("If you shut down the program, your character might stay in the game.\nClick on 'Logout' to ensure that you character leaves the game properly.\nClick on 'Exit' if you want to exit the program without logging out your character.")
|
|
||||||
text-auto-resize: true
|
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
|
||||||
anchors.top: parent.top
|
|
||||||
margin-top: 2
|
|
||||||
|
|
||||||
Button
|
|
||||||
id: buttonExit
|
|
||||||
!text: tr('Force Exit')
|
|
||||||
width: 80
|
|
||||||
anchors.right: next.left
|
|
||||||
anchors.bottom: parent.bottom
|
|
||||||
margin-right: 5
|
|
||||||
|
|
||||||
Button
|
|
||||||
id: buttonLogout
|
|
||||||
!text: tr('Logout')
|
|
||||||
width: 80
|
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
|
||||||
anchors.bottom: parent.bottom
|
|
||||||
margin-left: 5
|
|
||||||
|
|
||||||
Button
|
|
||||||
id: buttonCancel
|
|
||||||
!text: tr('Cancel')
|
|
||||||
width: 80
|
|
||||||
anchors.left: prev.right
|
|
||||||
anchors.bottom: parent.bottom
|
|
||||||
margin-left: 5
|
|
|
@ -1,27 +0,0 @@
|
||||||
LogoutWindow < MainWindow
|
|
||||||
id: logoutWindow
|
|
||||||
!text: tr('Logout')
|
|
||||||
size: 230 100
|
|
||||||
|
|
||||||
Label
|
|
||||||
!text: tr('Are you sure you want to logout?')
|
|
||||||
text-auto-resize: true
|
|
||||||
anchors.horizontalCenter: parent.horizontalCenter
|
|
||||||
anchors.top: parent.top
|
|
||||||
margin-top: 2
|
|
||||||
|
|
||||||
Button
|
|
||||||
id: buttonYes
|
|
||||||
!text: tr('Yes')
|
|
||||||
width: 64
|
|
||||||
anchors.right: parent.horizontalCenter
|
|
||||||
anchors.bottom: parent.bottom
|
|
||||||
margin-right: 5
|
|
||||||
|
|
||||||
Button
|
|
||||||
id: buttonNo
|
|
||||||
!text: tr('No')
|
|
||||||
width: 64
|
|
||||||
anchors.left: parent.horizontalCenter
|
|
||||||
anchors.bottom: parent.bottom
|
|
||||||
margin-left: 5
|
|
|
@ -22,6 +22,7 @@ end
|
||||||
|
|
||||||
function UIGameMap:onDragLeave(droppedWidget, mousePos)
|
function UIGameMap:onDragLeave(droppedWidget, mousePos)
|
||||||
self.currentDragThing = nil
|
self.currentDragThing = nil
|
||||||
|
self.hoveredWho = nil
|
||||||
g_mouse.restoreCursor()
|
g_mouse.restoreCursor()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue