Jump to content

dx List


Snow-Man

Recommended Posts

i have started with new script, i have got some problems on it, it's about weapon shops with dx functions

how can i make weapon selected, i have made table of weapons using one dxDrawText

here's my code

offs = 45
valid_key = {["mouse1"] = true}
opened = false
event = addEventHandler
remove = removeEventHandler
smX, smY = 1280, 1024
sX, sY = guiGetScreenSize( )
local ammunationWeapons = {
	{16,3000, 5},
	{22,500, 30},
	{23,600, 30},
	{24,1200, 15},
	{25,600, 15},
	{26,800, 12},
	{27,1000, 10},
	{28,500, 60},
	{29,2000, 90},
	{30,3500, 120},
	{31,4500, 150},
	{32,300, 60},
	{33,600, 20},
	{34,5000, 10},
	{46,800, 1},	
}

setposition = 250
function dxDrawAmmuShop()
	if opened == true then return false end

        DxDrawBorderedRectangle(64, 186, 313, 411, tocolor(0, 0, 0, 150), false)
        dxDrawBorderedText("USC ~ Weapons Shop", 66, 185, 377, 212, tocolor(255, 255, 255, 255), 0.80, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawBorderedText("Weapons", 66, 212, 269, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
        
		for i, v in ipairs ( ammunationWeapons )do
		name = i
		r2, g2, b2, size2 = ItemsHower()
		dxDrawBorderedText("Price", 269, 212, 377, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawText(getWeaponNameFromID(v[1]), 66,  (210+(i*offs)), 269, 270, tocolor(r2, g2, b2, 255),size2, "bankgothic", "left", "center", false, false, false, false, false)
		dxDrawText(tocomma(v[2]).."$", 269, 210+(i*offs), 377, 270, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
	   end
		dxDrawFixedLine(64, 240, 377, 240, tocolor(255, 255, 255, 255), 3, false)
        dxDrawFixedLine(269, 212, 269, 597, tocolor(255, 255, 255, 255), 3, false)
		dxDrawFixedLine(64, 212, 377, 212, tocolor(255, 255, 255, 255), 3, false)
		
        r, g, b, size = buttonHover()
		r1, g1, b1, size1= closeHover()
        DxDrawBorderedRectangle(64, 603, 313, 32, tocolor(0, 0, 0, 200), false)
        dxDrawBorderedText("Purchase", 64, 602, 221, 635, tocolor(r, g, b, 255), size, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawBorderedText("Close", 221, 602, 378, 635, tocolor(r1, g1, b1, 255), size1, "bankgothic", "center", "center", false, false, false, false, false)
		if #ammunationWeapons ~= i then
			
			dxDrawFixedRectangle(64, setposition, 312, 23, tocolor(0, 0, 0, 100), false)
		end
end

i have posted half of it but it's working fine just need to know how to get item selected and select it

Link to comment
  • Moderators

By only giving a part of your script you are forcing us to write code from the top of our head and by doing assumptions of what your code might do behind the scene, which is not cool because making dx user interfaces is already a not so easy task to do.

That's why you only get me to help you ;)

That is what I managed to do. I couldn't test it of course but it seems correct by the look of it and I hope it will work at the 1st try but if not, give me as much details as you can about the problem you have:

offs = 45
valid_key = {["mouse1"] = true}
opened = false
event = addEventHandler
remove = removeEventHandler
smX, smY = 1280, 1024
sX, sY = guiGetScreenSize( )
local ammunationWeapons = {
	{16,3000, 5},
	{22,500, 30},
	{23,600, 30},
	{24,1200, 15},
	{25,600, 15},
	{26,800, 12},
	{27,1000, 10},
	{28,500, 60},
	{29,2000, 90},
	{30,3500, 120},
	{31,4500, 150},
	{32,300, 60},
	{33,600, 20},
	{34,5000, 10},
	{46,800, 1},	
}

local selectedRow = nil -- Used to remember the selection
local _renderedRowCache = {} -- Used to keep in memory the exact rendered position and size of each weapon row
addEventHandler("onClientClick", root, function (button, state, cX, cY)
	if button ~= "left" and state ~= "up" then return end -- accepting only left mouse button and only when it's released
	for k, r in ipairs(_renderedRowCache) do -- looping through the cache to see in which row we clicked
		local x1, x2, y1, y2 = r.x1, r.x2, r.y1, r.y2 -- getting the boundaries of the row from the cache
		if cx > x1 and cx < c2 and cy > y1 and cy < y2 then -- if the click is inside the row boundaries
			selectedRow = k -- 'k' is here the index in the cache but it should match the index in ammunationWeapons list
			return -- no need to test the other rows
		end
	end
end)

setposition = 250
function dxDrawAmmuShop()
	if opened == true then return false end

	DxDrawBorderedRectangle(64, 186, 313, 411, tocolor(0, 0, 0, 150), false)
	dxDrawBorderedText("USC ~ Weapons Shop", 66, 185, 377, 212, tocolor(255, 255, 255, 255), 0.80, "bankgothic", "center", "center", false, false, false, false, false)
	dxDrawBorderedText("Weapons", 66, 212, 269, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
	dxDrawBorderedText("Prices", 269, 212, 377, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false) -- don't render it for every weapon there is
	
	_renderedRowCache = {} -- clear the cache
	for i, v in ipairs ( ammunationWeapons )do
		name = i
		
		if selectedRow == i then -- if the selected row is that current row then
			r2, g2, b2, size2 = 255, 255, 255, 1 -- force the selected style
		else
			r2, g2, b2, size2 = ItemsHower() -- do the math like before
		end
		
		local posY = 210+(i*offs) -- optimisation by calculating it once per row instead of 2
		dxDrawText(getWeaponNameFromID(v[1]), 66, posY, 269, 270, tocolor(r2, g2, b2, 255),size2, "bankgothic", "left", "center", false, false, false, false, false)
		dxDrawText(tocomma(v[2]).."$", 269, posY, 377, 270, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
		
		-- calculating the boundaries of the entire row (weapon name + price)
		local rX1, rY1 = 66, posY -- top left corner of the row
		local rX2, rY2 = 377, rY1+offs -- bottom right corner of the row
		table.insert(_renderedRowCache, {x1=rX1, y1=rY1, x2=rX2, y2=rY2}) -- cache it in memory
	end
	
	dxDrawFixedLine(64, 240, 377, 240, tocolor(255, 255, 255, 255), 3, false)
	dxDrawFixedLine(269, 212, 269, 597, tocolor(255, 255, 255, 255), 3, false)
	dxDrawFixedLine(64, 212, 377, 212, tocolor(255, 255, 255, 255), 3, false)
	
	r, g, b, size = buttonHover()
	r1, g1, b1, size1= closeHover()
	DxDrawBorderedRectangle(64, 603, 313, 32, tocolor(0, 0, 0, 200), false)
	dxDrawBorderedText("Purchase", 64, 602, 221, 635, tocolor(r, g, b, 255), size, "bankgothic", "center", "center", false, false, false, false, false)
	dxDrawBorderedText("Close", 221, 602, 378, 635, tocolor(r1, g1, b1, 255), size1, "bankgothic", "center", "center", false, false, false, false, false)
	if #ammunationWeapons ~= i then
		dxDrawFixedRectangle(64, setposition, 312, 23, tocolor(0, 0, 0, 100), false)
	end
end

You have to modify line 53 for the values r, g, b, size you want for the weapon name text if selected (probably the same style than when the cursor is over it but heh, you didn't give us the content of ItemsHower function. Oh and you should rename ItemsHower by ItemsHover to be clean).

I also extracted the following line from the loop (now at line 46) as there is no reason for that line to be in that loop:

dxDrawBorderedText("Prices", 269, 212, 377, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)

Waiting for your test feedbacks.

Best regards,

Citizen

Link to comment
offs = 45
opened = false
event = addEventHandler
remove = removeEventHandler
smX, smY = 1280, 1024
sX, sY = guiGetScreenSize( )
selected = 1
move = 0
moved_up = 0
local ammunationWeapons = {
	{16,3000, 5},
	{22,500, 30},
	{23,600, 30},
	{24,1200, 15},
	{25,600, 15},
	{26,800, 12},
	{27,1000, 10},
	{28,500, 60},
	{29,2000, 90},
	{30,3500, 120},
	{31,4500, 150},
	{32,300, 60},
	{33,600, 20},
	{34,5000, 10},
	{46,800, 1},	
}

setposition = 250
function dxDrawAmmuShop()
	if opened == true then return false end

        DxDrawBorderedRectangle(64, 186, 313, 411, tocolor(0, 0, 0, 150), false)
        dxDrawBorderedText("USC ~ Weapons Shop", 66, 185, 377, 212, tocolor(255, 255, 255, 255), 0.80, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawBorderedText("Weapons", 66, 212, 269, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
        
		for i, v in ipairs ( ammunationWeapons )do
		name = i
		r2, g2, b2, size2 = ItemsHower()
		dxDrawBorderedText("Price", 269, 212, 377, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawText(getWeaponNameFromID(v[1]), 66,  (210+(i*offs)), 269, 270, tocolor(r2, g2, b2, 255),size2, "bankgothic", "left", "center", false, false, false, false, false)
		dxDrawText(tocomma(v[2]).."$", 269, 210+(i*offs), 377, 270, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
	   end
		dxDrawFixedLine(64, 240, 377, 240, tocolor(255, 255, 255, 255), 3, false)
        dxDrawFixedLine(269, 212, 269, 597, tocolor(255, 255, 255, 255), 3, false)
		dxDrawFixedLine(64, 212, 377, 212, tocolor(255, 255, 255, 255), 3, false)
		
        r, g, b, size = buttonHover()
		r1, g1, b1, size1= closeHover()
        DxDrawBorderedRectangle(64, 603, 313, 32, tocolor(0, 0, 0, 200), false)
        dxDrawBorderedText("Purchase", 64, 602, 221, 635, tocolor(r, g, b, 255), size, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawBorderedText("Close", 221, 602, 378, 635, tocolor(r1, g1, b1, 255), size1, "bankgothic", "center", "center", false, false, false, false, false)
		if #ammunationWeapons ~= i then
			
			dxDrawFixedRectangle(64, setposition, 312, 23, tocolor(0, 0, 0, 100), false)
		end
end

function buttonHover()
	if isMouseInPosition(64, 602, 221, 635) then
		r,g, b, size= 200, 200, 0, 0.9
	else
		r,g, b, size = 255, 255, 255, 0.8
	end
		return r,g, b, size
end
		
function closeHover()
	if isMouseInPosition(221, 602, 378, 635) then
		r1, g1, b1, size1= 200, 200, 0, 0.9
	else
		r1, g1, b1, size1= 255, 255, 255, 0.8
	end
		return r1, g1, b1, size1
end

function ItemsHower()
	if isMouseInPosition(66,  (210+(name*offs)), 269, 270) then
		r2, g2, b2, size2= 200, 200, 0, 0.7
	else
		r2, g2, b2, size2= 255, 255, 255, 0.6
	end
		return r2, g2, b2, size2
end

		
addEvent("setAmmuGUIVisible", true)
function setAmmuGUIVisible()
	if opened ==  false then
	toggleAllControls( false)
		event("onClientRender", root,dxDrawAmmuShop)
		showCursor(true)
		event("onClientKey", root, onPlayerDxHit)
		event("onClientKey", root, onPlayerChooseWep)
	end
end
event("setAmmuGUIVisible", root, setAmmuGUIVisible)

function onPlayerDxHit(key, pressed)
	if not key == "mouse1" then return false end
	if isMouseInPosition(sX*(221/smX), sY*(602/smY), sX*(378/smX), sY*(635/smY)) then
		close()
	end
end

function close()
	opened = false
	showCursor(false)
	--toggleAllControls( true)
	setTimer(toggleAllControls, 50, 1, true)
	remove("onClientRender", root, dxDrawAmmuShop)
		remove("onClientKey", root, onPlayerDxHit)
		remove("onClientKey", root, onPlayerChooseWep)
	end
	

function isMouseInPosition(x1,y1,x2,y2, type)
	if not isCursorShowing() then
		return false
	end
	local cx,cy = getCursorPosition()
	cx,cy = cx*sX,cy*sY
	if cx >= sX*(x1/smX) and cx <= (type == true and sX*(x2/smX) + sX*(x1/smX) or sX*(x2/smX)) and cy >= sY*(y1/smY) and cy <= (type == true and sY*(y2/smY) + sY*(y1/smY) or sY*(y2/smY)) then
		return true,cx,cy
	else
		return false
	end
end



function onPlayerChooseWep(key, press)
	if not key or not key=="mouse1" and not press then return false end

		if not (name) then return false end
				cx, cy = getCursorPosition( )
				cy = (cy*sY)-21
				cx = cx*sX
				if cy >= sY*(560/smY) or cy <= sY*(235/smY) then return false end
				if cx <= sX*(66/smX) or cx >= sX*(377/smX) then return false end
					setposition = cy+(name)
end
addEventHandler( "onClientPlayerWasted", localPlayer,
	function()
		close()
	end
)

	

 

Link to comment
  • 2 weeks later...

I keep track of the element that has focus on a variable declared at the top of the document.
I created a few functions that create elements so I have a reference for every object, in the onClientRender function I get the mouse position on screen and before drawing the element I check if the mouse is on top of it, if so I save that element in the variable. I also trigger custom events when the mouse enters or leaves an element and with onClientClick and onClientDoubleClick you can also check if the player tries to click the currently focused element.

Link to comment
Quote

 

By only giving a part of your script you are forcing us to write code from the top of our head and by doing assumptions of what your code might do behind the scene, which is not cool because making dx user interfaces is already a not so easy task to do.

 

 

If you can post full your script, maybe more people can help you.

Link to comment
  • 4 weeks later...

i have posted full script

again.

offs = 45
opened = false
event = addEventHandler
remove = removeEventHandler
smX, smY = 1280, 1024
sX, sY = guiGetScreenSize( )
selected = 1
move = 0
moved_up = 0
local ammunationWeapons = {
	{16,3000, 5},
	{22,500, 30},
	{23,600, 30},
	{24,1200, 15},
	{25,600, 15},
	{26,800, 12},
	{27,1000, 10},
	{28,500, 60},
	{29,2000, 90},
	{30,3500, 120},
	{31,4500, 150},
	{32,300, 60},
	{33,600, 20},
	{34,5000, 10},
	{46,800, 1},	
}
setposition = 250
function dxDrawAmmuShop()
	if opened == true then return false end
        DxDrawBorderedRectangle(64, 186, 313, 411, tocolor(0, 0, 0, 150), false)
        dxDrawBorderedText("USC ~ Weapons Shop", 66, 185, 377, 212, tocolor(255, 255, 255, 255), 0.80, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawBorderedText("Weapons", 66, 212, 269, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
        
		for i, v in ipairs ( ammunationWeapons )do
		name = i
		r2, g2, b2, size2 = ItemsHower()
		dxDrawBorderedText("Price", 269, 212, 377, 236, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawText(getWeaponNameFromID(v[1]), 66,  (210+(i*offs)), 269, 270, tocolor(r2, g2, b2, 255),size2, "bankgothic", "left", "center", false, false, false, false, false)
		dxDrawText(tocomma(v[2]).."$", 269, 210+(i*offs), 377, 270, tocolor(255, 255, 255, 255), 0.70, "bankgothic", "center", "center", false, false, false, false, false)
	   end
		dxDrawFixedLine(64, 240, 377, 240, tocolor(255, 255, 255, 255), 3, false)
        dxDrawFixedLine(269, 212, 269, 597, tocolor(255, 255, 255, 255), 3, false)
		dxDrawFixedLine(64, 212, 377, 212, tocolor(255, 255, 255, 255), 3, false)
		
        r, g, b, size = buttonHover()
		r1, g1, b1, size1= closeHover()
        DxDrawBorderedRectangle(64, 603, 313, 32, tocolor(0, 0, 0, 200), false)
        dxDrawBorderedText("Purchase", 64, 602, 221, 635, tocolor(r, g, b, 255), size, "bankgothic", "center", "center", false, false, false, false, false)
        dxDrawBorderedText("Close", 221, 602, 378, 635, tocolor(r1, g1, b1, 255), size1, "bankgothic", "center", "center", false, false, false, false, false)
		if #ammunationWeapons ~= i then
			
			dxDrawFixedRectangle(64, setposition, 312, 23, tocolor(0, 0, 0, 100), false)
		end
end
function buttonHover()
	if isMouseInPosition(64, 602, 221, 635) then
		r,g, b, size= 200, 200, 0, 0.9
	else
		r,g, b, size = 255, 255, 255, 0.8
	end
		return r,g, b, size
end
		
function closeHover()
	if isMouseInPosition(221, 602, 378, 635) then
		r1, g1, b1, size1= 200, 200, 0, 0.9
	else
		r1, g1, b1, size1= 255, 255, 255, 0.8
	end
		return r1, g1, b1, size1
end
function ItemsHower()
	if isMouseInPosition(66,  (210+(name*offs)), 269, 270) then
		r2, g2, b2, size2= 200, 200, 0, 0.7
	else
		r2, g2, b2, size2= 255, 255, 255, 0.6
	end
		return r2, g2, b2, size2
end
		
addEvent("setAmmuGUIVisible", true)
function setAmmuGUIVisible()
	if opened ==  false then
	toggleAllControls( false)
		event("onClientRender", root,dxDrawAmmuShop)
		showCursor(true)
		event("onClientKey", root, onPlayerDxHit)
		event("onClientKey", root, onPlayerChooseWep)
	end
end
event("setAmmuGUIVisible", root, setAmmuGUIVisible)
function onPlayerDxHit(key, pressed)
	if not key == "mouse1" then return false end
	if isMouseInPosition(sX*(221/smX), sY*(602/smY), sX*(378/smX), sY*(635/smY)) then
		close()
	end
end
function close()
	opened = false
	showCursor(false)
	--toggleAllControls( true)
	setTimer(toggleAllControls, 50, 1, true)
	remove("onClientRender", root, dxDrawAmmuShop)
		remove("onClientKey", root, onPlayerDxHit)
		remove("onClientKey", root, onPlayerChooseWep)
	end
	
function isMouseInPosition(x1,y1,x2,y2, type)
	if not isCursorShowing() then
		return false
	end
	local cx,cy = getCursorPosition()
	cx,cy = cx*sX,cy*sY
	if cx >= sX*(x1/smX) and cx <= (type == true and sX*(x2/smX) + sX*(x1/smX) or sX*(x2/smX)) and cy >= sY*(y1/smY) and cy <= (type == true and sY*(y2/smY) + sY*(y1/smY) or sY*(y2/smY)) then
		return true,cx,cy
	else
		return false
	end
end
function onPlayerChooseWep(key, press)
	if not key or not key=="mouse1" and not press then return false end
		if not (name) then return false end
				cx, cy = getCursorPosition( )
				cy = (cy*sY)-21
				cx = cx*sX
				if cy >= sY*(560/smY) or cy <= sY*(235/smY) then return false end
				if cx <= sX*(66/smX) or cx >= sX*(377/smX) then return false end
					setposition = cy+(name)
end
addEventHandler( "onClientPlayerWasted", localPlayer,
	function()
		close()
	end
)
	

 

Link to comment
  • 4 weeks later...

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...