Hallo Community,
aus Langeweile hab ich mich heute mal an eine DX-Liste gesetzt. Ich hab versucht es in OOP aufzubauen. Die Scrollbar funktioniert noch nicht ganz. Ich war zu faul die richtige Berechnung rauszufinden. Hover- und Klickfunktion sind bereits eingebaut
Vorschau
Externer Inhalt
www.picapple.de
Inhalte von externen Seiten werden ohne Ihre Zustimmung nicht automatisch geladen und angezeigt.
Spoiler anzeigen
Lua
cDx = {}
cDx.__index = cDx
local x,y = guiGetScreenSize()
function cDx:new(title, width, height, background, fontcolor, font)
--Checking the datas for the right type--
assert((type(title) == 'string'), 'Bad Argument @cDx:new [Expected stringt at argument 1 got '..type(title)..']')
assert((type(width) == 'number'), 'Bad Argument @cDx:new [Expected number at argument 2 got '..type(width)..']')
assert((type(height) == 'number'), 'Bad Argument @cDx:new [Expected number at argument 3 got '..type(height)..']')
assert((type(background) == 'function'), 'Bad Argument @cDx:new [Expected function at argument 4 got '..type(background)..']')
assert((type(fontcolor) == 'function'), 'Bad Argument @cDx:new [Expected function at argument 5 got '..type(fontcolor)..']')
assert((type(font) == 'string'), 'Bad Argument @cDx:new [Expected stringt at argument 6 got '..type(font)..']')
--creating the instance--
local instance = {
title = tostring(title),
width = tonumber(width),
height = tonumber(height),
background = background,
fontcolor = fontcolor,
font = tostring(font),
show = false,
list = {},
scroll = 0,
di = 0
}
--Setting the metatable--
setmetatable(instance, self)
--return the instance--
return instance
end
function cDx:changeTitle(string)
self.title = string
end
function cDx:remove(i)
if self.list[i] then
table.remove(self.list, i)
end
end
function cDx:renameItem(i,string)
for num, p in ipairs(self.list)
if num == i then
p = string
return true
end
end
return false
end
function cDx:add(string)
table.insert(self.list, string)
outputChatBox(string)
end
function cDx:scrollUp()
if self.scroll < #self.list then
self.scroll = self.scroll + 1
outputChatBox(self.scroll)
else
self.scroll = #self.list
end
end
function cDx:scrollDown()
if self.scroll > 0 then
self.scroll = self.scroll - 1
outputChatBox(self.scroll)
else
self.scroll = 0
end
end
function cDx:click(btn,state,cx,cy)
if btn == "left" and state == "up" then
if isCursorShowing() then
local di = 0
for i = 1+self.scroll, 17+self.scroll do
if self.list[i] then
if cx >= x/2-self.width/2+50 and cx <= x/2-self.width/2+50+412 and cy >= y/2-self.height/2+50+((di)*20) and cy <= y/2-self.height/2+50+((di)*20)+15 then
--Was soll beim Klicken passieren!
end
end
di = di + 1
end
end
end
end
function cDx:enable(value)
if self.show == value then outputDebugString("Self.show is alread the value!") return end
if self.show == false then
showCursor(true)
addEventHandler("onClientRender", root, function() self:render() end)
addEventHandler("onClientClick", root, function(btn,state,cx,cy) self:click(btn,state,cx,cy) end)
bindKey("mouse_wheel_down","down",function() self:scrollUp() end)
bindKey("mouse_wheel_up","down",function() self:scrollDown() end)
self.show = value
else
self.show = value
removeEventHandler("onClientRender", root, function() self:render() end)
removeEventHandler("onClientClick", root, function() self:click() end)
unbindKey("mouse_wheel_down","down",function() self:scrollUp() end)
unbindKey("mouse_wheel_up","down",function() self:scrollDown() end)
end
end
function cDx:render()
local di = 0
--Drawing the window--
dxDrawRectangle(x/2-self.width/2, y/2-self.height/2, self.width, self.height, self.background, false)
--Drawing the title
dxDrawText(self.title, x/2-dxGetTextWidth(self.title,1.5,"default-bold")/2, y/2-self.height/2+5, self.width, self.height, tocolor(255,255,255,255), 1.5, "default-bold", "left", "top", false, false, true, true)
--Background for the dxlist--
dxDrawRectangle(x/2-self.width/2+45, y/2-self.height/2+47, 422, 340, tocolor(0, 0, 0, 150), false)
--Drawing the list
for i = 1+self.scroll, 17+self.scroll do
if self.list[i] then
dxDrawText(self.list[i], x/2-self.width/2+50, y/2-self.height/2+50+((di)*20), 412, 16, self.fontcolor, 1, self.font, "left", "top", false, false, true, true)
--Adding the count--
di = di + 1
end
end
--Drawing the list beneath--
dxDrawRectangle(x/2-self.width/2+45+422, y/2-self.height/2+47+((self.scroll*(#self.list/0.4)/340)), 10, 20, tocolor(155, 155, 155, 155), true)
--Hover-Animation--
local ki = 0
if isCursorShowing() then
local cx, cy = getCursorPosition()
cx, cy = cx * x, cy * y
for i = 1+self.scroll, 17+self.scroll do
if self.list[i] then
if cx >= x/2-self.width/2+50 and cx <= x/2-self.width/2+50+422 and cy >= y/2-self.height/2+50+((ki)*20) and cy <= y/2-self.height/2+50+(ki*20)+15 then
outputChatBox("x")
dxDrawRectangle(x/2-self.width/2+45, y/2-self.height/2+50+((ki)*20)-3, 422, 20, tocolor(255, 150, 0, 50), true)
end
ki = ki + 1
end
end
end
end
Alles anzeigen
Hier ein Beispielcode:
Spoiler anzeigen
Viel Spaß beim Benutzen
Grüße