Nikita Kruglickiy 1a0785862d Initial commit
2020-12-25 22:18:23 +06:00

81 lines
2.1 KiB
Plaintext

--@include utils.txt
--@include render_devices/render_device.txt
--@include elements/root.txt
--@include skins/default.txt
require("utils.txt")
require("render_devices/render_device.txt")
require("elements/root.txt")
local defaultSkin = require("skins/default.txt")
GUI = class("GUI")
function GUI:initialize(renderDevice, skin)
checkVarClass(renderDevice, RenderDevice)
self.renderDevice = renderDevice
self._root = ERoot:new("root")
self._root:setSize(renderDevice:getSize())
self._root:_setSkin(skin or defaultSkin)
hook.add("inputPressed", "gui_inputPressed", function(key)
local keyName = input.getKeyName(key)
if key >= 107 and key <= 111 then
local x, y = input.getCursorPos()
self._root:_postEvent("MOUSE_PRESSED", x, y, key, keyName)
else
self._root:_postEvent("BUTTON_PRESSED", x, y, key, keyName)
end
end)
hook.add("inputReleased", "gui_inputReleased", function(key)
local keyName = input.getKeyName(key)
if key >= 107 and key <= 111 then
local x, y = input.getCursorPos()
self._root:_postEvent("MOUSE_RELEASED", x, y, key, keyName)
else
self._root:_postEvent("BUTTON_RELEASED", x, y, key, keyName)
end
end)
self._lastMouseX = 0
self._lastMouseY = 0
hook.add("think", "gui_think", function()
if input.getCursorVisible then
local x, y = input.getCursorPos()
if x ~= self._lastMouseX or y ~= self._lastMouseY then
self._lastMouseX = x
self._lastMouseY = y
self._root:_postEvent("MOUSE_MOVED", x, y)
end
end
self._root:_postEvent("THINK")
end)
renderDevice.render = function()
self._root:_postEvent("PAINT")
end
end
function GUI:getRoot()
return self._root
end
function GUI:add(element)
this._root:addChild(element)
end
function GUI:setVisible(state)
self._root:setVisible(state)
end
function GUI:isVisible()
self._root:isVisible()
end