2014-09-26 08:57:56 +03:00
|
|
|
-- Buildat: extension/__menu/init.lua
|
|
|
|
-- http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
-- Copyright 2014 Perttu Ahola <celeron55@gmail.com>
|
|
|
|
local log = buildat.Logger("extension/__menu")
|
2014-10-05 11:14:55 +03:00
|
|
|
local dump = buildat.dump
|
2014-09-28 12:56:14 +03:00
|
|
|
local magic = require("buildat/extension/urho3d").safe
|
2014-09-26 13:25:26 +03:00
|
|
|
local uistack = require("buildat/extension/uistack")
|
2014-10-05 11:14:55 +03:00
|
|
|
local ui_utils = require("buildat/extension/ui_utils").safe
|
2014-09-26 08:57:56 +03:00
|
|
|
local M = {safe = nil}
|
2014-09-26 12:23:53 +03:00
|
|
|
|
2014-09-26 13:41:16 +03:00
|
|
|
local function show_error(message)
|
2014-10-05 11:14:55 +03:00
|
|
|
ui_utils.show_message_dialog(message)
|
2014-09-26 13:41:16 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local function show_connect_to_server()
|
2014-10-05 11:14:55 +03:00
|
|
|
local root = uistack.main:push({desc="connect_to_server"})
|
2014-09-26 13:41:16 +03:00
|
|
|
|
2014-09-27 14:26:26 +03:00
|
|
|
local style = magic.cache:GetResource("XMLFile", "__menu/res/main_style.xml")
|
2014-09-26 13:41:16 +03:00
|
|
|
root.defaultStyle = style
|
|
|
|
|
|
|
|
local window = root:CreateChild("Window")
|
|
|
|
window:SetStyleAuto()
|
|
|
|
window:SetName("connect_to_server window")
|
2014-09-27 14:26:26 +03:00
|
|
|
window:SetLayout(LM_VERTICAL, 10, magic.IntRect(10, 10, 10, 10))
|
2014-09-26 13:41:16 +03:00
|
|
|
window:SetAlignment(HA_LEFT, VA_CENTER)
|
|
|
|
|
|
|
|
local line_edit = window:CreateChild("LineEdit")
|
|
|
|
line_edit:SetStyleAuto()
|
|
|
|
line_edit:SetName("connect_to_server line_edit")
|
|
|
|
line_edit.minHeight = 24
|
|
|
|
line_edit.minWidth = 300
|
|
|
|
line_edit:SetText("localhost:20000")
|
|
|
|
line_edit:SetFocus(true)
|
|
|
|
|
|
|
|
local connect_button = window:CreateChild("Button")
|
|
|
|
connect_button:SetStyleAuto()
|
|
|
|
connect_button:SetName("Button")
|
2014-09-27 14:26:26 +03:00
|
|
|
connect_button:SetLayout(LM_VERTICAL, 10, magic.IntRect(0, 0, 0, 0))
|
2014-09-26 13:41:16 +03:00
|
|
|
connect_button.minHeight = 20
|
|
|
|
local connect_button_text = connect_button:CreateChild("Text")
|
|
|
|
connect_button_text:SetName("ButtonText")
|
|
|
|
connect_button_text:SetStyleAuto()
|
|
|
|
connect_button_text.text = "Connect"
|
|
|
|
connect_button_text:SetTextAlignment(HA_CENTER)
|
|
|
|
|
|
|
|
function connect_or_show_error(address)
|
|
|
|
local ok, err = buildat.connect_server(line_edit:GetText())
|
|
|
|
if ok then
|
|
|
|
log:info("buildat.connect_server() returned true")
|
2014-10-05 11:14:55 +03:00
|
|
|
local root = uistack.main:push({desc="empty (game is running)"})
|
2014-09-26 13:41:16 +03:00
|
|
|
magic.ui:SetFocusElement(nil)
|
|
|
|
else
|
|
|
|
log:info("buildat.connect_server() returned false")
|
|
|
|
show_error(err)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
magic.SubscribeToEvent(connect_button, "Released",
|
|
|
|
function(self, event_type, event_data)
|
|
|
|
log:info("connect_button: Released")
|
|
|
|
connect_or_show_error(line_edit:GetText())
|
|
|
|
end)
|
|
|
|
|
|
|
|
magic.SubscribeToEvent(line_edit, "TextFinished",
|
|
|
|
function(self, event_type, event_data)
|
|
|
|
log:info("line_edit: TextFinished")
|
|
|
|
connect_or_show_error(line_edit:GetText())
|
|
|
|
end)
|
|
|
|
|
|
|
|
root:SubscribeToStackEvent("KeyDown", function(event_type, event_data)
|
|
|
|
local key = event_data:GetInt("Key")
|
|
|
|
if key == KEY_ESC then
|
|
|
|
log:info("KEY_ESC pressed at connect_to_server level")
|
2014-10-05 11:14:55 +03:00
|
|
|
uistack.main:pop(root)
|
2014-09-26 13:25:26 +03:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2014-09-26 12:23:53 +03:00
|
|
|
|
2014-09-26 13:25:26 +03:00
|
|
|
function M.boot()
|
2014-10-05 11:14:55 +03:00
|
|
|
local root = uistack.main:push("boot")
|
2014-09-26 12:23:53 +03:00
|
|
|
|
2014-09-27 14:26:26 +03:00
|
|
|
local style = magic.cache:GetResource("XMLFile", "__menu/res/boot_style.xml")
|
2014-09-26 13:25:26 +03:00
|
|
|
root.defaultStyle = style
|
|
|
|
|
2014-09-26 12:23:53 +03:00
|
|
|
local layout = root:CreateChild("Window")
|
|
|
|
layout:SetStyleAuto()
|
2014-09-26 13:41:16 +03:00
|
|
|
layout:SetName("Layout")
|
2014-09-27 14:26:26 +03:00
|
|
|
layout:SetLayout(LM_HORIZONTAL, 20, magic.IntRect(0, 0, 0, 0))
|
2014-09-28 01:05:04 +03:00
|
|
|
layout:SetAlignment(HA_LEFT, VA_CENTER)
|
2014-09-26 12:23:53 +03:00
|
|
|
|
|
|
|
local button = layout:CreateChild("Button")
|
2014-09-26 08:57:56 +03:00
|
|
|
button:SetStyleAuto()
|
|
|
|
button:SetName("Button")
|
2014-09-27 14:26:26 +03:00
|
|
|
button:SetLayout(LM_VERTICAL, 10, magic.IntRect(0, 0, 0, 0))
|
2014-09-26 13:41:16 +03:00
|
|
|
local button_image = button:CreateChild("Sprite")
|
|
|
|
button_image:SetName("ButtonImage")
|
2014-09-26 12:23:53 +03:00
|
|
|
button_image:SetTexture(
|
2014-09-27 14:26:26 +03:00
|
|
|
magic.cache:GetResource("Texture2D", "__menu/res/icon_network.png"))
|
|
|
|
button_image.color = magic.Color(.3, .3, .3)
|
2014-09-26 12:23:53 +03:00
|
|
|
button_image:SetFixedSize(200, 200)
|
2014-09-26 13:41:16 +03:00
|
|
|
local button_text = button:CreateChild("Text")
|
|
|
|
button_text:SetName("ButtonText")
|
2014-09-26 12:23:53 +03:00
|
|
|
button_text:SetStyleAuto()
|
2014-09-26 13:41:16 +03:00
|
|
|
button_text.text = "Connect to server"
|
2014-09-27 14:26:26 +03:00
|
|
|
button_text.color = magic.Color(.3, .3, .3)
|
2014-09-28 01:05:04 +03:00
|
|
|
button_text:SetAlignment(HA_CENTER, VA_TOP)
|
2014-09-26 13:41:16 +03:00
|
|
|
button_text:SetTextAlignment(HA_CENTER)
|
2014-09-26 12:23:53 +03:00
|
|
|
|
|
|
|
magic.SubscribeToEvent(button, "HoverBegin",
|
|
|
|
function(self, event_type, event_data)
|
2014-09-27 14:26:26 +03:00
|
|
|
self:GetChild("ButtonImage").color = magic.Color(1, 1, 1)
|
|
|
|
self:GetChild("ButtonText").color = magic.Color(1, 1, 1)
|
2014-09-26 12:23:53 +03:00
|
|
|
end)
|
|
|
|
magic.SubscribeToEvent(button, "HoverEnd",
|
|
|
|
function(self, event_type, event_data)
|
2014-09-27 14:26:26 +03:00
|
|
|
self:GetChild("ButtonImage").color = magic.Color(.3, .3, .3)
|
|
|
|
self:GetChild("ButtonText").color = magic.Color(.3, .3, .3)
|
2014-09-26 12:23:53 +03:00
|
|
|
end)
|
|
|
|
magic.SubscribeToEvent(button, "Released",
|
|
|
|
function(self, event_type, event_data)
|
|
|
|
log:info("Button clicked: \"Connect to server\"")
|
2014-09-26 13:41:16 +03:00
|
|
|
show_connect_to_server()
|
2014-09-26 12:23:53 +03:00
|
|
|
end)
|
2014-09-26 08:57:56 +03:00
|
|
|
|
2014-09-26 13:41:16 +03:00
|
|
|
root:SubscribeToStackEvent("KeyDown", function(event_type, event_data)
|
|
|
|
local key = event_data:GetInt("Key")
|
|
|
|
if key == KEY_ESC then
|
|
|
|
log:info("KEY_ESC pressed at top level")
|
|
|
|
engine:Exit()
|
|
|
|
end
|
|
|
|
if key == KEY_RETURN then
|
|
|
|
log:info("RETURN pressed at top level")
|
|
|
|
show_connect_to_server()
|
2014-09-26 08:57:56 +03:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|
|
|
|
-- vim: set noet ts=4 sw=4:
|