![]() |
|
Emitter Touch Example/Function - Printable Version +- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk) +-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html) +--- Forum: Starfall (https://teamscm.co.uk/forum-7.html) +--- Thread: Emitter Touch Example/Function (/thread-1383.html) |
Emitter Touch Example/Function - TheOneAndOnlyTee - 05-10-2015 Got a bit caught up coding my emitter so i didn't get around to releasing this. just for clarification, the button functions are coded by Link320 not LocInt and the touch function is coded by Garbius. I only put my name in it because i ported it to Link's Code: --@name Garbius/Link320/T-17 Emitter Buttons
--@author Garbius, Link320, T-17
--@shared
----IMPORTANT NOTE
--[[
my button function doesn't work like Link320's original, since i prefer the render.drawRect() instead of drawCRect()
i made it so i can plug a rects() arguments into the button function with no problems.
if you want it the other way simply replace the Button:pressed() function with one that works for you
]]--
if SERVER then
function getEnt()
if net.start() then
net.writeEntity( ents.self():isWeldedTo() )
net.broadcast()
end
end
function netRead()
if net.readString() == "RequestEnt" then
getEnt()
end
end
hook("net","requestEnt",netRead)
else --- CLIENT
if net.start() then
net.writeString("RequestEnt")
net.sendToServer()
end
-------TOUCH AND BUTTON STUFF
function Touch( ply, bind, pressed )
if bind == "+use" and pressed then
if not pos then return end
local aimvec = ply:aimVector()
local normal = Base:forward()
if aimvec:Dot( normal ) >= 0 then return end
local length = normal:Dot( pos - ply:eyePos()) / normal:Dot( aimvec )
if length > 100 then return end
local point = ( ply:eyePos() + aimvec * length - pos )/.11
point:Rotate( -normal:Angle() )
--dot.x = point.y
--dot.y = -point.z
--print(math.floor(point.y)..","..math.floor(-point.z))
Button( point.y, -point.z ,ply )
end
end
hook.add( "playerbindpress", "touch", Touch )
button = {}
button.__index = button
buttons = {}
function addButton(index,x,y,w,h)
if index ~= nil then
buttons[index] = setmetatable({x,y,w,h}, button)
end
end
function button:pressed(X,Y)
if X == nil or Y == nil then return false end
A = self[1]
B = self[2]
W = self[3]
H = self[4]
if H == nil then --Circular
return ((X-A)^2 + (Y-B)^2 < W^2)
else --Rectangular
return (X > A) and (X < A+W) and (Y > B) and (Y < B+H)
end
end
function Button( x, y, ply )
for k,v in pairs( buttons ) do
if v:pressed(x,y) then
if k == "Example" and ply == ents.owner() then
print("Voila")
end
end
end
end
function renderEmitter()
if Base~=nil and Base:isValid() then
ang = Base:ang()
ang:RotateAroundAxis( Base:right(), 0 )
ang:RotateAroundAxis( Base:forward(), 90 )
ang:RotateAroundAxis( Base:up(), 90 )
pos = Base:toWorld( Vector( -55, 0, 0 ) )
render.start3D2D( pos, ang, .11 )
--START 3D2D
render.setColor(0,164,255,255)
render.drawRect(-200,-200,400,400) --draws a 200x200 rect in the center
render.setColor(41,41,41,255)
render.drawRect(-190,-190,380,380)
render.setColor(255,255,255)
render.drawText("Default",0,0,"Button",render.TEXT_ALIGN_CENTER)
addButton("Example",-190,-190,380,380)
--END 3D2D
render.end3D2D()
end
end
function netRead()
if Base==nil then
Base = net.readEntity()
hook.add("render","emitter",renderEmitter)
end
end
hook("net","getEnt",netRead)
endYou need to spawn it on something to see it work/open the code without error Also if you find the button dimensions are incorrect on your screen it's because I dislike how Link320 coded his button functions originally since i could just plug a rect's pos/size into them, so i fixed that in my Button:pressed(). |