mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 03:24:55 +02:00
rework ui related scripting stuff
This commit is contained in:
4
data/modules/core/aliases.lua
Normal file
4
data/modules/core/aliases.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
-- some aliases
|
||||
loadUI = UI.load
|
||||
rootUI = UI.getRootContainer()
|
||||
|
4
data/modules/core/core.lua
Normal file
4
data/modules/core/core.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
require 'aliases'
|
||||
require 'enums'
|
||||
require 'util'
|
||||
|
8
data/modules/core/enums.lua
Normal file
8
data/modules/core/enums.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
-- AnchorPoint
|
||||
AnchorNone = 0
|
||||
AnchorTop = 1
|
||||
AnchorBottom = 2
|
||||
AnchorLeft = 3
|
||||
AnchorRight = 4
|
||||
AnchorVerticalCenter = 5
|
||||
AnchorHorizontalCenter = 6
|
8
data/modules/core/module.otml
Normal file
8
data/modules/core/module.otml
Normal file
@@ -0,0 +1,8 @@
|
||||
title: Core
|
||||
notes: Core utilities used by other modules
|
||||
author: edubart
|
||||
version: 1
|
||||
website: https://github.com/edubart/otclient
|
||||
enabled: true
|
||||
script: core.lua
|
||||
|
5
data/modules/core/util.lua
Normal file
5
data/modules/core/util.lua
Normal file
@@ -0,0 +1,5 @@
|
||||
function createEmptyFunction()
|
||||
local emptyFunction = function() end
|
||||
return emptyFunction
|
||||
end
|
||||
|
57
data/modules/mainmenu/entergame.lua
Normal file
57
data/modules/mainmenu/entergame.lua
Normal file
@@ -0,0 +1,57 @@
|
||||
function EnterGame_connectToLoginServer()
|
||||
-- create login protocol
|
||||
local protocolLogin = ProtocolLogin.new()
|
||||
|
||||
-- used to recreate enter game window
|
||||
local recreateEnterGame = function()
|
||||
loadUI("ui/entergamewindow")
|
||||
end
|
||||
|
||||
-- display loading message box
|
||||
local loadBox = displayCancelBox("Please wait", "Connecting..")
|
||||
|
||||
-- cancel loading callback
|
||||
loadBox.onCancel = function()
|
||||
-- cancel protocol and reacreate enter game window
|
||||
protocolLogin:cancel()
|
||||
recreateEnterGame()
|
||||
end
|
||||
|
||||
-- error callback
|
||||
protocolLogin.onError = function(error)
|
||||
-- destroy loading message box
|
||||
loadBox:destroy()
|
||||
|
||||
-- display error message
|
||||
local errorBox = displayErrorBox("Login Error", error)
|
||||
|
||||
-- cancel protocol and reacreate enter game window
|
||||
self.cancel()
|
||||
errorBox.onOk = recreateEnterGame
|
||||
end
|
||||
|
||||
-- motd callback
|
||||
protocolLogin.onMotd = function(motd)
|
||||
-- destroy loading message box
|
||||
loadBox:destroy()
|
||||
|
||||
-- display motd
|
||||
local motdNumber = string.sub(motd, 0, string.find(motd, "\n"))
|
||||
local motdText = string.sub(motd, string.find(motd, "\n") + 1, string.len(motd))
|
||||
local motdBox = displayInfoBox("Message of the day", motdText)
|
||||
|
||||
-- cancel protocol and reacreate enter game window
|
||||
self.cancel()
|
||||
motdBox.onOk = recreateEnterGame
|
||||
end
|
||||
|
||||
-- get account and password and login
|
||||
local enterGameWindow = rootUI:child("enterGameWindow")
|
||||
local account = enterGameWindow:child("accountNameTextEdit").text
|
||||
local password = enterGameWindow:child("passwordTextEdit").text
|
||||
protocolLogin:login(account, password)
|
||||
|
||||
-- destroy enter game window
|
||||
enterGameWindow:destroy()
|
||||
end
|
||||
|
16
data/modules/mainmenu/init.lua
Normal file
16
data/modules/mainmenu/init.lua
Normal file
@@ -0,0 +1,16 @@
|
||||
require 'entergame'
|
||||
|
||||
function initializeApplication()
|
||||
mainMenu = loadUI("ui/mainmenu")
|
||||
end
|
||||
|
||||
function terminateApplication()
|
||||
App.exit()
|
||||
end
|
||||
|
||||
-- here is where everything starts
|
||||
if not initialized then
|
||||
initializeApplication()
|
||||
App.onClose = terminateApplication
|
||||
initialized = true
|
||||
end
|
@@ -1,63 +0,0 @@
|
||||
-- menu state
|
||||
function onEnterMenuState()
|
||||
mainMenu = UI.load("mainmenu.yml")
|
||||
end
|
||||
|
||||
function onLeaveMenuState()
|
||||
|
||||
end
|
||||
|
||||
function onApplicationClose()
|
||||
onLeaveMenuState()
|
||||
App.exit()
|
||||
end
|
||||
|
||||
function enterGame_onOkClicked()
|
||||
local enterGameWindow = UI.getRootContainer():child("enterGameWindow")
|
||||
enterGameWindow.visible = false
|
||||
|
||||
local loadMessageBox = messageBox("Please wait", "Connecting..")
|
||||
loadMessageBox.onDestroy = function()
|
||||
--TODO: cancel protocol
|
||||
enterGameWindow.visible = true
|
||||
protocolLogin = nil
|
||||
end
|
||||
|
||||
protocolLogin = ProtocolLogin.new()
|
||||
protocolLogin.onError = function(error)
|
||||
loadMessageBox.onDestroy = nil
|
||||
loadMessageBox:destroy()
|
||||
local msgBox = messageBox("Login Error", error)
|
||||
msgBox.onDestroy = function()
|
||||
enterGameWindow.visible = true
|
||||
end
|
||||
protocolLogin = nil
|
||||
end
|
||||
protocolLogin.onMotd = function(motd)
|
||||
loadMessageBox.onDestroy = nil
|
||||
loadMessageBox:destroy()
|
||||
|
||||
local motdNumber = string.sub(motd, 0, string.find(motd, "\n"))
|
||||
local motdText = string.sub(motd, string.find(motd, "\n") + 1, string.len(motd))
|
||||
|
||||
local msgBox = messageBox("Message of the day", motdText)
|
||||
msgBox.onDestroy = function()
|
||||
enterGameWindow.visible = true
|
||||
end
|
||||
protocolLogin = nil
|
||||
end
|
||||
|
||||
local account = enterGameWindow:child("accountNameTextEdit").text
|
||||
local password = enterGameWindow:child("passwordTextEdit").text
|
||||
account = "tibialua0"
|
||||
password = "lua123456"
|
||||
|
||||
protocolLogin:login(account, password)
|
||||
end
|
||||
|
||||
-- here is where everything starts
|
||||
if not initialStateLoaded then
|
||||
onEnterMenuState()
|
||||
App.onClose = onApplicationClose
|
||||
initialStateLoaded = true
|
||||
end
|
@@ -3,7 +3,7 @@
|
||||
size: [236, 178]
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
onLoad: self.locked = true
|
||||
onLoad: self:setLocked()
|
||||
|
||||
%label#accountNameLabel
|
||||
text: Account name
|
||||
@@ -34,7 +34,7 @@
|
||||
anchors.top: parent.top
|
||||
margin.top: 94
|
||||
margin.left: 132
|
||||
onClick: messageBox("Error", "Not implemented yet")
|
||||
onClick: displayErrorBox("Error", "Not implemented yet")
|
||||
|
||||
%button#okButton
|
||||
text: Ok
|
||||
@@ -43,7 +43,7 @@
|
||||
anchors.bottom: parent.bottom
|
||||
margin.bottom: 10
|
||||
margin.right: 66
|
||||
onClick: enterGame_onOkClicked()
|
||||
onClick: EnterGame_connectToLoginServer()
|
||||
|
||||
%button#cancelButton
|
||||
text: Cancel
|
@@ -5,22 +5,6 @@
|
||||
anchors.top: parent.top
|
||||
anchors.bottom: parent.bottom
|
||||
|
||||
%panel#icos4d
|
||||
skin:
|
||||
image: /skins/lightness/mouse.png
|
||||
anchors.left: parent.left
|
||||
anchors.top: parent.top
|
||||
margin.left: -2
|
||||
margin.top: 70
|
||||
|
||||
%panel#mouse
|
||||
skin:
|
||||
image: /skins/lightness/icos4d.png
|
||||
anchors.right: parent.right
|
||||
anchors.bottom: parent.bottom
|
||||
margin.left: 60
|
||||
margin.top: 70
|
||||
|
||||
%panel#mainMenu
|
||||
skin: roundedGridPanel
|
||||
size: [117, 171]
|
||||
@@ -34,32 +18,32 @@
|
||||
anchors.top: parent.top
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
margin.top: 16
|
||||
onClick: UI.load("entergamewindow.yml")
|
||||
onClick: UI.load("entergamewindow")
|
||||
|
||||
%button#accessAccountButton
|
||||
text: Access Account
|
||||
anchors.top: parent.top
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
margin.top: 46
|
||||
onClick: messageBox("Error", "Not implemented yet")
|
||||
onClick: displayErrorBox("Error", "Not implemented yet")
|
||||
|
||||
%button#optionsButton
|
||||
text: Options
|
||||
anchors.top: parent.top
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
margin.top: 76
|
||||
onClick: UI.load("optionswindow.yml")
|
||||
onClick: UI.load("optionswindow")
|
||||
|
||||
%button#infoButton
|
||||
text: Info
|
||||
anchors.top: parent.top
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
margin.top: 106
|
||||
onClick: UI.load("infowindow.yml")
|
||||
onClick: UI.load("infowindow")
|
||||
|
||||
%button#exitGameButton
|
||||
text: Exit
|
||||
anchors.top: parent.top
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
margin.top: 136
|
||||
onClick: onApplicationClose()
|
||||
onClick: terminateApplication()
|
@@ -1,34 +1,86 @@
|
||||
MessageBox = {}
|
||||
MessageBox.__index = MessageBox
|
||||
|
||||
function MessageBox.create(title, text)
|
||||
local msgBox = {}
|
||||
setmetatable(msgBox, MessageBox)
|
||||
-- messagebox flags
|
||||
MessageBoxOk = 1
|
||||
MessageBoxCancel = 2
|
||||
|
||||
local window = UI.load("messagebox.yml")
|
||||
window.locked = true
|
||||
window.title = title
|
||||
window:child("textLabel").text = text
|
||||
window:child("okButton").onClick = function()
|
||||
self.parent:destroy()
|
||||
end
|
||||
window.onDestroy = function()
|
||||
if msgBox.onDestroy then
|
||||
msgBox.onDestroy()
|
||||
end
|
||||
end
|
||||
function MessageBox.create(title, text, flags)
|
||||
local box = {}
|
||||
setmetatable(box, MessageBox)
|
||||
|
||||
msgBox.window = window
|
||||
return msgBox
|
||||
-- create messagebox window
|
||||
local window = UIWindow.new("messageBoxWindow", rootUI)
|
||||
window.title = title
|
||||
window:centerIn(rootUI)
|
||||
window:setLocked()
|
||||
|
||||
-- create messagebox label
|
||||
local label = UILabel.new("messageBoxLabel", window)
|
||||
label.text = text
|
||||
label:addAnchor(AnchorHorizontalCenter, window, AnchorHorizontalCenter)
|
||||
label:addAnchor(AnchorTop, window, AnchorTop)
|
||||
label:setMargin(27, 0)
|
||||
|
||||
-- set window size based on label size
|
||||
window.width = label.width + 40
|
||||
window.height = label.height + 64
|
||||
|
||||
-- setup messagebox first button
|
||||
local buttonText
|
||||
local button1 = UIButton.new("messageBoxButton1", window)
|
||||
button1:addAnchor(AnchorBottom, window, AnchorBottom)
|
||||
button1:addAnchor(AnchorRight, window, AnchorRight)
|
||||
button1:setMargin(10)
|
||||
|
||||
if flags == MessageBoxOk then
|
||||
buttonText = "Ok"
|
||||
box.onOk = createEmptyFunction()
|
||||
button1.onClick = function()
|
||||
box.onOk()
|
||||
box:destroy()
|
||||
end
|
||||
elseif flags == MessageBoxCancel then
|
||||
buttonText = "Cancel"
|
||||
box.onCancel = createEmptyFunction()
|
||||
button1.onClick = function()
|
||||
box.onCancel()
|
||||
box:destroy()
|
||||
end
|
||||
end
|
||||
button1.text = buttonText
|
||||
|
||||
box.window = window
|
||||
return box
|
||||
end
|
||||
|
||||
function MessageBox:destroy()
|
||||
if self.window then
|
||||
self.window:destroy()
|
||||
self.window = nil
|
||||
end
|
||||
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
|
||||
|
||||
function messageBox(title, text)
|
||||
return MessageBox.create(title, text)
|
||||
-- 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
|
||||
|
||||
|
@@ -1,18 +0,0 @@
|
||||
%window#messageBoxWindow
|
||||
size: [192, 78]
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
|
||||
|
||||
%label#textLabel
|
||||
anchors.horizontalCenter: parent.horizontalCenter
|
||||
anchors.top: parent.top
|
||||
margin.top: 27
|
||||
|
||||
%button#okButton
|
||||
text: Ok
|
||||
size: [43, 20]
|
||||
anchors.right: parent.right
|
||||
anchors.bottom: parent.bottom
|
||||
margin.bottom: 10
|
||||
margin.right: 10
|
8
data/modules/messagebox/module.otml
Normal file
8
data/modules/messagebox/module.otml
Normal file
@@ -0,0 +1,8 @@
|
||||
title: Message box
|
||||
notes: Functions for creating message boxes
|
||||
author: edubart
|
||||
version: 1
|
||||
website: https://github.com/edubart/otclient
|
||||
enabled: true
|
||||
script: messagebox.lua
|
||||
|
Reference in New Issue
Block a user