Перейти к содержимому
Gothic Multiplayer
Gothic MP Wiki

Browser

A web page drawn over the game: events between the page and Lua, showing, moving, resizing, focus.

since 0.1.36-alpha

Pages are opened with createBrowser. The page's side of the events is window.gmp. In the examples page is any Browser.

Arguments between Lua and the page are converted like JSON: numbers, strings, booleans and tables go both ways (a table with keys 1..n becomes an array, any other table an object); nil / null is nil; functions and other Lua values cannot be sent.

local page = createBrowser("login/index.html", 0, 0, getScreenSize())
page:on("ready", function()                 -- the page loaded and registered its gmp.on handlers
  page:trigger("setNickname", getLocalPlayer():getName())
end)
page:on("login", function(data)             -- gmp.trigger("login", { user: "...", password: "..." })
  triggerServerEvent("login", data.user, data.password)
end)

Methods

trigger

since 0.1.36-alpha
page:trigger(name, ...)

Calls the page's gmp.on(name, fn) handlers with the arguments. Handlers the page registers later do not get it - send data after the page said it is ready (gmp.trigger("ready") -> browser:on("ready")).

hud:trigger("stats", { health = 120, maxHealth = 200, gold = 35 })
ParameterTypeDescription
namestringEvent name (1-64 characters).
...anyArguments (converted to JSON, at most 4 MB together).

Returns: boolean - false when the browser was destroyed.

on

since 0.1.36-alpha
page:on(name, handler)

Registers a function called when the page calls gmp.trigger(name, ...); it gets the page's arguments. Several functions for one name are called in the order of registration.

page:on("buy", function(item, amount)       -- gmp.trigger("buy", "ItMi_Gold", 5)
  triggerServerEvent("shopBuy", item, amount)
end)
ParameterTypeDescription
namestringEvent name (1-64 characters).
handlerfunctionFunction called with the page's arguments.

Returns: boolean - false when the browser was destroyed.

off

since 0.1.36-alpha
page:off(name, handler)

Removes a function registered with browser:on.

ParameterTypeDescription
namestringEvent name.
handlerfunctionThe registered function.

Returns: boolean - false when it was not registered.

execute

since 0.1.36-alpha
page:execute(code)

Runs JavaScript code in the page (in its global scope, without a result). Usually browser:trigger is the cleaner way; this is for quick changes.

page:execute("document.body.style.opacity = '0.5'")
ParameterTypeDescription
codestringJavaScript code (at most 4 MB).

Returns: boolean - false when the browser was destroyed.

setVisible

since 0.1.36-alpha
page:setVisible(visible)

Shows or hides the page. A hidden page does not render (no CPU time) and gets no clicks or keys; it keeps its state (form contents, JavaScript variables).

ParameterTypeDescription
visibleboolean

Returns: boolean - false when the browser was destroyed.

isVisible

since 0.1.36-alpha
page:isVisible()

Whether the page is shown (see browser:setVisible).

Returns: boolean - false also when the browser was destroyed.

setPosition

since 0.1.36-alpha
page:setPosition(x, y)

Moves the page; CSS pixels of a 1920x1080 screen.

ParameterTypeDescription
xintegerLeft edge.
yintegerTop edge.

Returns: boolean - false when the browser was destroyed.

getPosition

since 0.1.36-alpha
page:getPosition()

Position of the page (CSS px).

Returns:

ValueTypeDescription
xinteger | nilnil when the browser was destroyed.
yinteger

setSize

since 0.1.36-alpha
page:setSize(width, height)

Changes the size of the page (CSS px, 1-8192); the page lays itself out again (CSS media queries, resize events).

ParameterTypeDescription
widthinteger
heightinteger

Returns: boolean - false when the browser was destroyed.

getSize

since 0.1.36-alpha
page:getSize()

Size of the page (CSS px).

Returns:

ValueTypeDescription
widthinteger | nilnil when the browser was destroyed.
heightinteger

setOrder

since 0.1.36-alpha
page:setOrder(order)

Sets which page is on top where pages overlap: higher numbers are drawn over lower ones and get the clicks first. Default 100; the chat is 50 (below the pages of scripts).

ParameterTypeDescription
orderinteger0-1000.

Returns: boolean - false when the browser was destroyed.

loadPage

since 0.1.36-alpha
page:loadPage(path)

Loads another page of the server's ui folder into this browser (its browser:on handlers stay).

ParameterTypeDescription
pathstringPath of the page file in the server's ui folder.

Returns: boolean - false when the browser was destroyed.

focus

since 0.1.36-alpha
page:focus()

Gives the keyboard to this page while the cursor is shown (showCursor(true)) - like a click on it, so the player can type into a focused text field at once.

showCursor(true)
login:focus()   -- the page's <input autofocus> gets the keys without a click

Returns: boolean - false when the browser was destroyed.

destroy

since 0.1.36-alpha
page:destroy()

Closes the page. The scripts' pages are also closed when the scripts stop (leaving the server).

Returns: boolean - false when it was already destroyed.

isValid

since 0.1.36-alpha
page:isValid()

Whether the browser still exists.

Returns: boolean