Jump to content

Help dx list


kikos500

Recommended Posts

i made the same topic some months ago but still i wasn't able to finish my dx list scroll system i would really be grateful if someone can help me finish it

function createList(x,y,w,h,parent)
	local id = #elements_Table.list + 1
	elements_Table.list[id] = {}
	if not x or not y or not w or not h then
		outputDebugString("Error: Missing some arguments (x,y,w,h)")
	end
	elements_Table.list[id].x = x
	elements_Table.list[id].y = y
	elements_Table.list[id].w = w
	elements_Table.list[id].h = h
	elements_Table.list[id].parent = parent or nil
	elements_Table.list[id].visible = false
	elements_Table.list[id].items = {}
	elements_Table.list[id].selected = 0
	elements_Table.list[id].scroll = 0
	if parent then
		table.insert(parent.children,elements_Table.list[id])
		elements_Table.list[id].x = elements_Table.list[id].x + parent.px
		elements_Table.list[id].y = elements_Table.list[id].y + parent.py
	end
	return elements_Table.list[id]
end

function addItem(grid,text)
	if grid then
		table.insert(grid.items,{text = text, alpha = 0})
		return #grid.items
	end
	return false
end

function onHover()
	for _,list in pairs(elements_Table.list) do
		local num = list.h/20
		for i = 1+list.scroll, num+list.scroll do
			local posrow = i - list.scroll
			if isCursorHover(list.x,list.y+(posrow*20)-20,list.w,20) then
				list.items[i].alpha = 150
			else
				list.items[i].alpha = 0
			end
		end
	end
end
addEventHandler( "onClientRender", root, onHover)

function onClick()
	for i,list in pairs(elements_Table.list) do
		local num = list.h/20
		for i = 1+list.scroll, num+list.scroll do
			local posrow = i - list.scroll
			if isCursorHover(list.x,list.y+(posrow*20)-20,list.w,20) then
				list.selected = i
			end
		end
	end
end
addEventHandler( "onClientClick", root, onClick )

function scrollDown()
	for i,v in pairs(elements_Table.list) do
		if isCursorHover(v.x,v.y,v.w,v.h) then
			if v.scroll+(v.h/20) < #v.items then
				v.scroll = v.scroll+1
			end
		end
	end
end
bindKey("mouse_wheel_down", "down", scrollDown)
function scrollUp()
	for i,v in pairs(elements_Table.list) do
		if isCursorHover(v.x,v.y,v.w,v.h) then
			if v.scroll > 0 then
				v.scroll = v.scroll-1
			end
		end
	end
end
bindKey("mouse_wheel_up", "down", scrollUp)

function renderList()
	for i,v in pairs(elements_Table.list) do
		if v.visible then
			dxDrawRectangle(v.x,v.y,v.w,v.h,tocolor(0,0,0,150))
			local num = v.h/20
			for i = 1+v.scroll, num+v.scroll do
				local posrow = i - v.scroll
				if v.items[i] then
					if v.selected ~= i then
						dxDrawRectangle( v.x, v.y+(posrow*20)-20, v.w, 20,tocolor(0,0,0,v.items[i].alpha))
					else
						dxDrawRectangle( v.x, v.y+(posrow*20)-20, v.w, 20,tocolor(0,0,0,255))
					end
					dxDrawText(v.items[i].text,v.x + 1,v.y+(posrow*20)-20,v.w+v.x,v.y,tocolor(255,255,255,255),1,"default-bold","left","top")
				end
			end
			local endX = v.scroll * 20
			dxDrawRectangle( v.x+v.w, v.y+endX, 10, num*20)
		end
	end
end
addEventHandler( "onClientRender", root, renderList)

 

Link to comment
  • Discord Moderators

What you exactly want from us?We can help you, but we will not write a script for you.

Btw, user assert instead of that

	if not x or not y or not w or not h then
		outputDebugString("Error: Missing some arguments (x,y,w,h)")
	end

So, you assert function will look like this:

for k,v in ipairs({x,y,w,h}) do
	assert(type(v) == "number" or not v, "Expected number at argument #" .. k .. " @ createList [got " .. type(v) .. "]")
end

 

Edited by Pirulax
added some text
Link to comment
function createList(x,y,w,h,parent)
	local id = #elements_Table.list + 1
	elements_Table.list[id] = {}
	if not x or not y or not w or not h then
		outputDebugString("Error: Missing some arguments (x,y,w,h)")
	end
	elements_Table.list[id].type = "list"
	elements_Table.list[id].x = x
	elements_Table.list[id].y = y
	elements_Table.list[id].px = x
	elements_Table.list[id].py = y
	elements_Table.list[id].w = w
	elements_Table.list[id].h = h
	elements_Table.list[id].parent = parent or nil
	elements_Table.list[id].visible = false
	elements_Table.list[id].items = {}
	elements_Table.list[id].selected = 0
	elements_Table.list[id].scroll = 0
	elements_Table.list[id].postgui = false
	elements_Table.list[id].itc = {50,255,50}
	elements_Table.list[id].scc = {50,255,50}
	elements_Table.list[id].rTarget = dxCreateRenderTarget(w, h, true)
	if parent then
		table.insert(parent.children,elements_Table.list[id])
		elements_Table.list[id].x = elements_Table.list[id].x + parent.px
		elements_Table.list[id].y = elements_Table.list[id].y + parent.py
	end
	return elements_Table.list[id]
end

function addItem(grid,text,data)
	if grid then
		table.insert(grid.items,{text = text, alpha = 0, data = data or nil})
		return #grid.items
	end
	return false
end

function getData(grid)
	return grid.items[selected].data
end

function resetGrid(grid)
	grid.items = {}
end

function onHover()
	for _,list in pairs(elements_Table.list) do
		local num = list.h/20
		for i = 1+list.scroll, num+list.scroll do
			local posrow = i - list.scroll
			if not list.items[i] then return end
			if isCursorHover(list.x,list.y+(posrow*20)-20,list.w,20) then
				list.items[i].alpha = 150
			else
				list.items[i].alpha = 0
			end
		end
	end
end
addEventHandler( "onClientRender", root, onHover)

addEvent("onGridListSelected", true)
function onClick(button,state)
	if button == "left" and state == "down" then
		for i,list in pairs(elements_Table.list) do
			local num = list.h/20
			for i = 1+list.scroll, num+list.scroll do
				local posrow = i - list.scroll
				if isCursorHover(list.x,list.y+(posrow*20)-20,list.w,20) then
					list.selected = i
					triggerEvent("onGridListSelected", root, i)
				end
			end
		end
	end
end
addEventHandler( "onClientClick", root, onClick )

function getData(grid)
	return grid.items[grid.selected]
end


function scrollDown()
	for i,v in pairs(elements_Table.list) do
		if isCursorHover(v.x,v.y,v.w,v.h) then
			if v.scroll+(v.h/20) < #v.items then
				v.scroll = v.scroll+1
			end
		end
	end
end
bindKey("mouse_wheel_down", "down", scrollDown)

function scrollUp()
	for i,v in pairs(elements_Table.list) do
		if isCursorHover(v.x,v.y,v.w,v.h) then
			if v.scroll > 0 then
				v.scroll = v.scroll-1
			end
		end
	end
end
bindKey("mouse_wheel_up", "down", scrollUp)

function renderList()
	for i,v in pairs(elements_Table.list) do
		if v.visible then
			dxDrawRectangle(v.x,v.y,v.w,v.h,tocolor(0,0,0,200))
			local num = v.h/20
			for i = 1+v.scroll, num+v.scroll do
				local posrow = i - v.scroll
				if v.items[i] then
					if v.selected ~= i then
						dxDrawRectangle( v.x, v.y+(posrow*20)-20, v.w, 20,tocolor(v.itc[1],v.itc[2],v.itc[3],v.items[i].alpha))
					else
						dxDrawRectangle( v.x, v.y+(posrow*20)-20, v.w, 20,tocolor(50,255,50,255,255))
					end
					dxDrawText(v.items[i].text,v.x + 1,v.y+(posrow*20)-20,v.w+v.x,v.y,tocolor(255,255,255,255),1,"default-bold","left","top",false,false)
				end
			end
			dxSetRenderTarget(v.rTarget,true)
			dxDrawImage(v.x, v.y, v.w, v.h, v.rTarget,0,0,0,tocolor(255,255,255,155))
			dxSetRenderTarget()
			if num < #v.items and #v.items*20 > v.h then
				local size = (v.h-(v.h/3))/(#v.items - num) 
				local endX = (((v.h - size) / (#v.items - num)) * v.scroll)
				dxDrawRectangle( v.x+v.w, v.y, 10, v.h, tocolor(0,0,0,100))
				dxDrawRectangle( v.x+v.w, v.y+endX, 10, size, tocolor(v.scc[1],v.scc[2],v.scc[3], 255))
			end
		end
	end
end
addEventHandler( "onClientRender", root, renderList)

so this is the new code i tried doing it but now the scroll goes out of bounds like https://i.imgur.com/rktYddV.png

 

Edited by kikos500
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...