mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 19:44:54 +02:00
move tooltip and messagebox to modules
This commit is contained in:
82
modules/messagebox/messagebox.lua
Normal file
82
modules/messagebox/messagebox.lua
Normal file
@@ -0,0 +1,82 @@
|
||||
MessageBox = {}
|
||||
MessageBox.__index = MessageBox
|
||||
|
||||
-- messagebox flags
|
||||
MessageBoxOk = 1
|
||||
MessageBoxCancel = 2
|
||||
|
||||
function MessageBox.create(title, text, flags)
|
||||
local box = {}
|
||||
setmetatable(box, MessageBox)
|
||||
|
||||
-- create messagebox window
|
||||
local window = UI.loadAndDisplayLocked('/messagebox/messagebox.otui')
|
||||
window:setTitle(title)
|
||||
|
||||
local label = window:getChildById('messageBoxLabel')
|
||||
label:setStyle('Label')
|
||||
label:setText(text)
|
||||
label:resizeToText()
|
||||
|
||||
-- set window size based on label size
|
||||
window:setWidth(math.max(label:getWidth() + 48, 120))
|
||||
window:setHeight(label:getHeight() + 64)
|
||||
window:updateParentLayout()
|
||||
|
||||
-- setup messagebox first button
|
||||
local buttonRight = window:getChildById('messageBoxRightButton')
|
||||
|
||||
if flags == MessageBoxOk then
|
||||
buttonRight:setText("Ok")
|
||||
box.onOk = EmptyFunction
|
||||
buttonRight.onClick = function()
|
||||
box.onOk()
|
||||
box:destroy()
|
||||
end
|
||||
window.onEnter = buttonRight.onClick
|
||||
window.onEscape = buttonRight.onClick
|
||||
elseif flags == MessageBoxCancel then
|
||||
buttonRight:setText("Cancel")
|
||||
box.onCancel = EmptyFunction
|
||||
buttonRight.onClick = function()
|
||||
box.onCancel()
|
||||
box:destroy()
|
||||
end
|
||||
window.onEnter = buttonRight.onClick
|
||||
window.onEscape = buttonRight.onClick
|
||||
end
|
||||
|
||||
box.window = window
|
||||
return box
|
||||
end
|
||||
|
||||
function MessageBox:destroy()
|
||||
if self.onDestroy then
|
||||
self.onDestroy()
|
||||
self.onDestroy = nil
|
||||
end
|
||||
if self.window then
|
||||
self.window:destroy()
|
||||
self.window = nil
|
||||
end
|
||||
self.onOk = nil
|
||||
self.onCancel = nil
|
||||
end
|
||||
|
||||
-- shortcuts for creating message boxes
|
||||
function displayMessageBox(title, text, flags)
|
||||
return MessageBox.create(title, text, flags)
|
||||
end
|
||||
|
||||
function displayErrorBox(title, text)
|
||||
return MessageBox.create(title, text, MessageBoxOk)
|
||||
end
|
||||
|
||||
function displayInfoBox(title, text)
|
||||
return MessageBox.create(title, text, MessageBoxOk)
|
||||
end
|
||||
|
||||
function displayCancelBox(title, text)
|
||||
return MessageBox.create(title, text, MessageBoxCancel)
|
||||
end
|
||||
|
12
modules/messagebox/messagebox.otmod
Normal file
12
modules/messagebox/messagebox.otmod
Normal file
@@ -0,0 +1,12 @@
|
||||
Module
|
||||
name: messagebox
|
||||
description: Manages message boxes
|
||||
author: OTClient team
|
||||
website: https://github.com/edubart/otclient
|
||||
autoLoad: true
|
||||
dependencies:
|
||||
- core
|
||||
|
||||
onLoad: |
|
||||
require 'messagebox'
|
||||
return true
|
21
modules/messagebox/messagebox.otui
Normal file
21
modules/messagebox/messagebox.otui
Normal file
@@ -0,0 +1,21 @@
|
||||
Window
|
||||
id: messageBoxWindow
|
||||
anchors.centerIn: parent
|
||||
height: 80
|
||||
width: 120
|
||||
|
||||
Label
|
||||
id: messageBoxLabel
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.top: parent.top
|
||||
margin.top: 27
|
||||
margin.bottom : 27
|
||||
|
||||
Button
|
||||
id: messageBoxRightButton
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.right: parent.right
|
||||
margin.right: 10
|
||||
margin.bottom: 10
|
||||
width: 64
|
||||
visible: true
|
Reference in New Issue
Block a user