[Tutorial] dxFunctions
Hi everybody!
Today, I will make a tutorial for this community...
This tutorial will teach you to click dxElements (dxDrawRectangle, dxDrawText etc.)
For the beginning, create variables
--For all screen sizes
local screenW, screenH = guiGetScreenSize()
local sW = (screenW / 640)
local sH = (screenH / 480)
local tS = (screenW / 640) * 0.5 --Text Size
--dx Button variable
local dx_button = {
--{x, y, width, height, color, text, output text after clicking}
{x = sW * 70, y = sH * 200, w = sW * 75, h = sH * 10, c = tocolor(0, 0, 0, 0), t = 'Login', o = 'Login button works is fine!'},
{x = sW * 70, y = sH * 225, w = sW * 75, h = sH * 10, c = tocolor(0, 0, 0, 0), t = 'Register', o = 'Register button works is fine!'},
{x = sW * 70, y = sH * 250, w = sW * 75, h = sH * 10, c = tocolor(0, 0, 0, 0), t = 'Exit', o = 'Exit button works is fine!'}
}
local isVisible = false
Created? great!
Now we create function
--We need a (onClientRender, onClientCursorMove, onClientClick) functions
--Create Menu and deMenu functions for rendering dxElements
function Menu()
addEventHandler('onClientRender', root, Render)
addEventHandler('onClientCursorMove', root, Move)
addEventHandler('onClientClick', root, Click)
end
function deMenu()
removeEventHandler('onClientRender', root, Render)
removeEventHandler('onClientCursorMove', root, Move)
removeEventHandler('onClientClick', root, Click)
end
function Bind()
isVisible = (not isVisible)
showCursor(isVisible)
loadstring(('%s()'):format((isVisible and 'Menu' or 'deMenu')))()
end
bindKey('f', 'down', Bind)
After creating the necessary functions
function Render()
for i = 1, #dx_button do
dxDrawRectangle(dx_button[i].x, dx_button[i].y, dx_button[i].w, dx_button[i].h, dx_button[i].c)
dxDrawText(dx_button[i].t, dx_button[i].x, dx_button[i].y, dx_button[i].x + dx_button[i].w, dx_button[i].y + dx_button[i].h, tocolor(255, 255, 255, 255), tS * 1, 'default', 'center', 'center')
end
end
function Move(_, _, x, y)
--Cursor x, y position
for i = 1, #dx_button do
--If the cursor position is equal to dx position, then change the color
if (x >= dx_button[i].x and x <= dx_button[i].x + dx_button[i].w) and (y > dx_button[i].y and y < dx_button[i].y + dx_button[i].h) then
dx_button[i].c = tocolor(150, 0, 0, 200)
else
dx_button[i].c = tocolor(0, 0, 0, 0)
end
end
end
function Click(b, s, x, y)
if (b == 'left' and s == 'up') then
for i = 1, #dx_button do
--If the dx position is equal to cursor click position, then output message
if (x >= dx_button[i].x and x <= dx_button[i].x + dx_button[i].w) and (y > dx_button[i].y and y < dx_button[i].y + dx_button[i].h) then
if (dx_button[i].o) then
outputChatBox(dx_button[i].o)
end
end
end
end
end
All this it turns out so
local screenW, screenH = guiGetScreenSize()
local sW = (screenW / 640)
local sH = (screenH / 480)
local tS = (screenW / 640) * 0.5
local dx_button = {
{x = sW * 70, y = sH * 200, w = sW * 75, h = sH * 10, c = tocolor(0, 0, 0, 0), t = 'Login', o = 'Login button works is fine!'},
{x = sW * 70, y = sH * 225, w = sW * 75, h = sH * 10, c = tocolor(0, 0, 0, 0), t = 'Register', o = 'Register button works is fine!'},
{x = sW * 70, y = sH * 250, w = sW * 75, h = sH * 10, c = tocolor(0, 0, 0, 0), t = 'Exit', o = 'Exit button works is fine!'}
}
local isVisible = false
function Menu()
addEventHandler('onClientRender', root, Render)
addEventHandler('onClientCursorMove', root, Move)
addEventHandler('onClientClick', root, Click)
end
function deMenu()
removeEventHandler('onClientRender', root, Render)
removeEventHandler('onClientCursorMove', root, Move)
removeEventHandler('onClientClick', root, Click)
end
function Bind()
isVisible = (not isVisible)
showCursor(isVisible)
loadstring(('%s()'):format((isVisible and 'Menu' or 'deMenu')))()
end
bindKey('f', 'down', Bind)
function Render()
for i = 1, #dx_button do
dxDrawRectangle(dx_button[i].x, dx_button[i].y, dx_button[i].w, dx_button[i].h, dx_button[i].c)
dxDrawText(dx_button[i].t, dx_button[i].x, dx_button[i].y, dx_button[i].x + dx_button[i].w, dx_button[i].y + dx_button[i].h, tocolor(255, 255, 255, 255), tS * 1, 'default', 'center', 'center')
end
end
function Move(_, _, x, y)
for i = 1, #dx_button do
if (x >= dx_button[i].x and x <= dx_button[i].x + dx_button[i].w) and (y > dx_button[i].y and y < dx_button[i].y + dx_button[i].h) then
dx_button[i].c = tocolor(150, 0, 0, 200)
else
dx_button[i].c = tocolor(0, 0, 0, 0)
end
end
end
function Click(b, s, x, y)
if (b == 'left' and s == 'up') then
for i = 1, #dx_button do
if (x >= dx_button[i].x and x <= dx_button[i].x + dx_button[i].w) and (y > dx_button[i].y and y < dx_button[i].y + dx_button[i].h) then
if (dx_button[i].o) then
outputChatBox(dx_button[i].o)
end
end
end
end
end
This way we are able to click dxElement
That's all!
p.s Do not judge strictly, this is my first tutorial