Hallo. Ich möchte hiermit ein kleines Projekt, oder wie man es nennen will, vorstellen. Die dx-Klasse wird immer wieder weiterentwickelt. (Sofern ich Zeit habe).
Wer keine Lust hat es auszuprobieren:
Externer Inhalt
178.254.38.191
Inhalte von externen Seiten werden ohne Ihre Zustimmung nicht automatisch geladen und angezeigt.
"dx-Klasse"
Lua
dxElements = {
available = {
["window"] = true,
["gridlist"] = true,
["button"] = true,
["edit"] = true,
["text"] = true
}
}
local validKeys = {
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'l',
'k',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'backspace',
'space',
'lshift',
'-',
'.',
',',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'<'
}
local specialKeys = {
["q"] = "@"
}
setmetatable(dxElements, {
__call = function(_, properties)
if dxElements.available[properties[1]] == true then
local element = setmetatable({
typ = properties[1],
x = properties[2],
y = properties[3],
w = properties[4],
h = properties[5],
text = properties[6],
bgColor = properties[7] or {255, 255, 255, 255},
buttonColor = properties[8] or {255, 255, 255, 255},
textColor = properties[9] or {255, 255, 255, 255},
postGUI = properties[10] or false,
buttonFunction = properties[11],
visible = false,
textScale = 1,
textFont = "default"
}, {__index = dxElements})
element.renderFunc = function() element:render() end
element.clickFunc = function(button, state) element:onClick(button, state) end
element.charFunc = function(c, press) element:onChar(c, press) end
addEventHandler("onClientRender", getRootElement(), element.renderFunc)
addEventHandler("onClientClick", getRootElement(), element.clickFunc)
addEventHandler("onClientKey", getRootElement(), element.charFunc)
print(("[dxElements] New %s has been registered."):format(element.typ))
return element
end
return print("false")
end
})
function dxElements:unregister()
removeEventHandler("onClientRender", getRootElement(), self.renderFunc)
removeEventHandler("onClientClick", getRootElement(), self.clickFunc)
removeEventHandler("onClientKey", getRootElement(), self.charFunc)
self = nil
end
function dxElements:render()
if self.visible == true then
if self.typ == "window" then
local textpart_h = dxGetFontHeight(1.3, "default-bold")
local r, g, b, a = unpack(self.bgColor)
dxDrawRectangle(self.x, self.y, self.w, self.h, tocolor(r, g, b, a), self.postGUI) -- window background
dxDrawRectangle(self.x, self.y, self.w, textpart_h, tocolor(r+20, g+20, b+20, a), self.postGUI) -- topic(rectangle)
dxDrawText(self.text, self.x+self.w/2-(dxGetTextWidth(self.text, 1.3, "default-bold")/2), self.y, self.w, textpart_h, tocolor(unpack(self.textColor)), 1.3, "default-bold", "left", "top", false, false, self.postGUI, true) -- topic(text)
elseif self.typ == "gridlist" then
elseif self.typ == "button" then
local r, g, b, a = unpack(self.buttonColor)
if isMouseOverElement(self.x, self.y, self.w, self.h) then
dxDrawRectangle(self.x, self.y, self.w, self.h, tocolor(r+20, g+20, b+20, a), self.postGUI)
else
dxDrawRectangle(self.x, self.y, self.w, self.h, tocolor(r, g, b, a), self.postGUI)
end
local textpart_h = dxGetFontHeight(1, "default-bold")
dxDrawText(self.text, self.x+self.w/2-(dxGetTextWidth(self.text, 1, "default-bold")/2), self.y+(textpart_h/2), self.w, textpart_h, tocolor(unpack(self.textColor)), 1, "default-bold", "left", "top", false, false, self.postGUI, true) -- topic(text)
elseif self.typ == "edit" then
dxDrawRectangle(self.x, self.y, self.x, self.h, tocolor(unpack(self.bgColor)), self.postGUI) -- edit background
dxDrawText(self.text, self.x, self.y+(self.h/2), self.x, self.h, tocolor(unpack(self.textColor)), 1, "default-bold", "left", "top", false, true, self.postGUI)
elseif self.typ == "text" then
dxDrawText(self.text, self.x, self.y, self.w, self.h, tocolor(unpack(self.textColor)), self.textScale, self.textFont, "left", "top", false, false, self.postGUI)
end
end
end
function dxElements:onClick(button, state)
if state == "up" or button == "right" then return end
if self.typ == "window" then
return true
elseif self.typ == "gridlist" then
-- TODO
elseif self.typ == "button" then
if isMouseOverElement(self.x, self.y, self.w, self.h) then
return self.buttonFunction() and true or false
end
elseif self.typ == "edit" then
if isMouseOverElement(self.x, self.y, self.w, self.h) then
self.active = true
else
self.active = false
end
end
end
function dxElements:isValidKey (char)
for _, key in ipairs(validKeys) do
if key:lower() == char:lower() then
return true
end
end
return false
end
function dxElements:onChar(c, p)
if self.active == true then
if p then
if c == "lshift" then
self.shift = true
return
elseif c == "ralt" then
self.ralt = true
return
end
if dxElements:isValidKey(c) and (not isChatBoxInputActive()) and (not isConsoleActive()) and (not isMainMenuActive()) then
if c == "space" then
self.text = self.text.." "
elseif c == "backspace" then
self.text = string.sub(self.text, 1, #self.text-1)
elseif self.ralt then
self.text = self.text..specialKeys[c:lower()]
else
if self.shift then c = c:upper() end
self.text = self.text..c
end
end
else
if c == "lshift" then
self.shift = false
return
elseif c == "ralt" then
self.ralt = false
return
end
end
end
end
function dxElements:toggleVisibility()
self.visible = not self.visible
end
function dxElements:textSetScale(int)
self.textScale = int
end
function dxElements:textSetFont(font)
self.textFont = font
end
function isMouseOverElement(psx,psy,pssx,pssy)
if isCursorShowing() == false then
return false
end
local x,y = guiGetScreenSize()
local cx,cy = getCursorPosition()
cx,cy = cx*x,cy*y
if cx >= psx and cx <= psx+pssx and cy >= psy and cy <= psy+pssy then
return true,cx,cy
else
return false
end
end
Alles anzeigen
Kleines Beispiel wie es zu nutzen ist:
Lua
local t = {}
t.window = dxElements({"window", 400, 251, 1121, 578, "Test-Fenster", {0, 0, 0, 255/2}, {0, 0, 0, 255/2}, {255, 255, 255, 255}, true})
t.username = dxElements({"edit", 550, 306, 250, 37, "", {0, 0, 0, 255/2}, nil, {255, 255, 255, 255}, true })
t.text_username = dxElements({"text", 408, 308, 527, 340, "Username: ", nil, nil, {255, 255, 255, 255}, true })
t.text_username:textSetScale(2)
t.text_username:textSetFont("default-bold")
t.button = dxElements({"button", 1340, 775, 156, 40, "Close", nil, {0, 0, 0, 255/2}, {255, 255, 255, 255}, true,
function()
outputChatBox("Hi")
end
})
-- destroy all
for index, value in pairs(t) do
if value.typ ~= nil then
value:unregister()
end
end
-- if you dont wanna destroy them, use value:toggleVisibility()
Alles anzeigen