Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if CLIENT then
- local GShare = { }
- GShare.STRING = 'GShare.stream'
- GShare.Crop = false
- GShare.Bounds = { }
- GShare.Container = { }
- GShare.RT = GetRenderTarget("GShareRenderer", 300, 300, false)
- GShare.Key = 'wC2Gs@cHG_artybyte_gBLcW7@d$s^'
- GShare.RTMAT = CreateMaterial("GShareMat","UnlitGeneric",{
- ['$basetexture'] = GShare.RT:GetName(),
- });
- GShare.Net = function(t)
- net.Start(GShare.STRING)
- net.WriteTable(t)
- net.SendToServer()
- end
- GShare.DisplayScreenshot = function(url)
- local f = vgui.Create('DFrame')
- f:SetSize(500, 500)
- f:Center()
- f:SetSizable(true)
- f:MakePopup()
- local h = vgui.Create('DHTML', f)
- h:Dock(FILL)
- h:OpenURL(url)
- end
- GShare.InitContainer = function()
- for i = 1, GShare.Bounds[3] do
- GShare.Container[i] = { }
- for j = 1, GShare.Bounds[4] do
- GShare.Container[i][j] = { }
- end
- end
- end
- GShare.ShowShareWindow = function(url)
- local f = vgui.Create('DFrame')
- f:SetSize(100,200)
- f:Center()
- f:MakePopup()
- local ds = vgui.Create('DScrollPanel', f)
- ds:SetSize(100,180)
- ds:SetPos(0, 20)
- for i, kv in pairs(player.GetHumans()) do
- local b = vgui.Create('DButton', f)
- b:SetPos(10, 20+i*15)
- b:SetSize(80, 14)
- b:SetText(kv:Nick())
- b.DoClick = function()
- GShare.Net({'share_screenshot', LocalPlayer():SteamID(), kv:SteamID(), url})
- f:Close()
- end
- end
- end
- GShare.PostPixelData = function(tbl)
- local function post(descr)
- local readyJSON = util.TableToJSON(tbl)
- local readyData = util.Base64Encode(readyJSON)
- local sid = LocalPlayer():SteamID()
- local nick = LocalPlayer():Nick()
- http.Post( "http://91.122.14.185:11111/saveimage",
- {
- key = GShare.Key,
- pixeldata = readyData,
- width = tostring(GShare.Bounds[3]),
- height = tostring(GShare.Bounds[4]),
- sid = sid,
- username = nick,
- description = descr
- },
- -- onSuccess function
- function( req, length, headers, code )
- GShare.ShowShareWindow(req)
- end,
- -- onFailure function
- function( message )
- chat.AddText(Color(255,0,255), 'GShare: ' .. tostring(message))
- end
- )
- end
- local f = vgui.Create('DFrame')
- f:SetSize(350,50)
- f:Center()
- f:SetTitle('Enter description')
- f:MakePopup()
- local t = vgui.Create('DTextEntry', f)
- t:SetPos(10, 24)
- t:SetSize(f:GetWide()-80, f:GetTall()*0.4)
- t:RequestFocus()
- t.OnEnter = function() f:Close(); post(t:GetValue()) end
- local b = vgui.Create('DButton', f)
- b:SetPos(f:GetWide() - 70, 24)
- b:SetSize(60, f:GetTall()*0.4)
- b:SetText('Send')
- b.DoClick = t.OnEnter
- end
- GShare.DataMapping = function()
- -- first: convert RGB to 0xRRGGBBAA
- -- init temporary table
- local temp = { }
- for i = 1, GShare.Bounds[3] do
- temp[i] = { }
- for j = 1, GShare.Bounds[4] do
- temp[i][j] = { }
- end
- end
- for i = 1, GShare.Bounds[3] do
- for j = 1, GShare.Bounds[4] do
- local c = GShare.Container[i][j]
- local _r = string.format('%02x', c[1])
- local _g = string.format('%02x', c[2])
- local _b = string.format('%02x', c[3])
- temp[i][j] = '0x' .. tostring(_r) .. tostring(_g) .. tostring(_b) .. 'FF'
- end
- end
- -- send pixeldata to server
- GShare.PostPixelData(temp)
- end
- GShare.ReadPixels = function()
- -- re-create table of container with new given size
- GShare.InitContainer()
- -- call hook to scan pixels
- hook.Add('PostRenderVGUI', 'GShare.ReadPixels', function( )
- render.CapturePixels()
- for w = 1, GShare.Bounds[3] do
- for h = 1, GShare.Bounds[4] do
- local r, g, b = render.ReadPixel( GShare.Bounds[1] + w, GShare.Bounds[2] + h )
- GShare.Container[w][h] = { r, g, b }
- end
- end
- GShare.DataMapping()
- hook.Remove('PostRenderVGUI', 'GShare.ReadPixels')
- end )
- end
- GShare.FlipCorners = function(px, py, ppx, ppy)
- local lux, luy, rlx, rly = 0, 0, 0, 0
- local rux, ruy, llx, lly = 0, 0, 0, 0
- -- 1st quarter
- if ppx > px and ppy < py then
- -- left upper
- local lux, luy = px, ppy
- -- right lower
- local rlx, rly = ppx, py
- px = lux
- py = luy
- ppx = rlx
- ppy = rly
- end
- -- 2st quarter
- if ppx < px and ppy < py then
- -- left lower
- local llx, lly = ppx, py
- -- right upper
- local rux, ruy = px, ppy
- px = llx
- py = ruy
- ppx = rux
- ppy = lly
- end
- -- 3st quarter
- if ppx < px and ppy > py then
- -- left upper
- local lux, luy = ppx, py
- -- right lower
- local rlx, rly = px, ppy
- px = lux
- py = luy
- ppx = rlx
- ppy = rly
- end
- -- 4th quarter
- if ppx > px and ppy > py then
- -- right upper
- local rux, ruy = ppx, py
- -- left lower
- local llx, lly = px, ppy
- px = llx
- py = ruy
- ppx = rux
- ppy = lly
- end
- return px, py, ppx, ppy
- end
- GShare.OpenCropWindow = function()
- if GShare.Crop then return end
- GShare.Crop = true
- local pnl = vgui.Create('DPanel')
- pnl:SetPos(0, 0)
- pnl:SetSize(ScrW(), ScrH())
- pnl:MakePopup()
- local pressed = false
- local prsd = false
- local capturing = false
- local px, py, ppx, ppy = 0, 0, 0, 0
- pnl.Think = function()
- if input.IsMouseDown(107) and not pressed then
- prsd = false
- pressed = true
- capturing = true
- px, py = input.GetCursorPos()
- end
- if not input.IsMouseDown(107) and prsd != pressed then
- pressed = false
- capturing = false
- ppx, ppy = input.GetCursorPos()
- -- 4th quarters --
- -- simple algorythm to fix coords (make: LEFT_UPPER-RIGHT_LOWER vector in all of 4 cases)
- px, py, ppx, ppy = GShare.FlipCorners(px, py, ppx, ppy)
- local sizeW, sizeH = ppx - px, ppy - py
- GShare.Bounds = { px, py, sizeW, sizeH }
- GShare.Crop = false
- pnl:Remove()
- GShare.ReadPixels()
- end
- end
- pnl.Paint = function(self, w, h)
- draw.NoTexture()
- surface.SetDrawColor(255,0,0,140)
- local ppx, ppy = input.GetCursorPos()
- local PX, PY, PPX, PPY = GShare.FlipCorners(px, py, ppx, ppy)
- local _w, _h = PPX-PX, PPY-PY
- if pressed then
- -- horizontal lines
- surface.DrawRect(PX, PY, _w-1, 1)
- surface.DrawRect(PX, PY+_h-2, _w-1, 1)
- -- vertical lines
- surface.DrawRect(PX, PY, 1, _h-1)
- surface.DrawRect(PX+_w-2, PY, 1, _h-1)
- -- bottom size panel
- surface.SetDrawColor(0, 0, 0, 180)
- surface.DrawRect(PX, PY + _h - 20, _w, 20)
- draw.SimpleText(string.format('start [%d,%d], end [%d, %d]', PX, PY, PPX, PPY), 'DermaDefault', PX + _w/2, PY + _h - 10, Color(255,255,255), 1, 1)
- end
- end
- chat.AddText(Color(200,50,170), 'Select rectangle to make screenshot')
- end
- GShare.Receive = function()
- local t = net.ReadTable()
- local cmd = t[1]
- if cmd == 'open_crop_window' then
- GShare.OpenCropWindow()
- elseif cmd == 'open_screenshot_by_share' then
- local caller = t[2]
- local url = t[3]
- local caller_ply = player.GetBySteamID(caller)
- local f = vgui.Create('DFrame')
- f:SetSize(400, 80)
- f:Center()
- f:MakePopup()
- local l = vgui.Create('DLabel', f)
- l:SetPos(25, 30)
- l:SetText('Player ' .. tostring(caller_ply:Nick()) .. ' wanna show you screenshot. Accept? ')
- l:SetSize(350, 30)
- local b = vgui.Create('DButton', f)
- b:SetSize(50, 18)
- b:Center()
- b:SetPos(select(1, b:GetPos()), 55)
- b:SetText('Show')
- b.DoClick = function()
- GShare.DisplayScreenshot(url)
- f:Close()
- end
- end
- end
- -- init script
- hook.Add('InitPostEntity', 'GShare.init', GShare.Init)
- -- receive networks
- net.Receive(GShare.STRING, GShare.Receive)
- concommand.Add('gshare', GShare.OpenCropWindow)
- end
- if SERVER then
- local GShare = { }
- GShare.STRING = 'GShare.stream'
- GShare.cmd = '!gs'
- GShare.Receive = function()
- local t = net.ReadTable()
- local cmd = t[1]
- if cmd == 'share_screenshot' then
- local caller = t[2]
- local tar = t[3]
- local url = t[4]
- local sendTo = player.GetBySteamID(tar)
- GShare.Net({'open_screenshot_by_share', caller, url}, sendTo)
- end
- end
- GShare.Net = function(t, ply)
- net.Start(GShare.STRING)
- net.WriteTable(t)
- if ply == nil then
- net.Broadcast()
- else
- net.Send(ply)
- end
- end
- GShare.Chat = function(ply, text)
- if string.Trim(text, ' ') == GShare.cmd then
- print(ply, ' used a GShare')
- GShare.Net({'open_crop_window'}, ply)
- return ''
- end
- end
- -- init network
- util.AddNetworkString(GShare.STRING)
- -- start receiving networks
- net.Receive('GShare.stream', GShare.Receive)
- -- catch player messages
- hook.Add('PlayerSay', 'GShare.psay', GShare.Chat)
- end
Add Comment
Please, Sign In to add comment