Advertisement
adamnejm

Untitled

Nov 27th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | None | 0 0
  1. -- vgui.Create( "HTML", Base )
  2. -- HTML:SetSize( 1024, 1024 )
  3. local canvas = render.createHTMLCanvas(1024, 1024)
  4.  
  5. -- sf-canvas min level; required (?)
  6. -- No access to body or lower
  7. canvas:setHTML([[
  8.     <sf-canvas>
  9.         <div id="titles">
  10.             <h1></h1>
  11.             <h2></h2>
  12.             <h3></h3>
  13.         </div>
  14.        
  15.         <br>  <!-- Invalid -->
  16.         <br/> <!-- Allowed -->
  17.        
  18.         <div>
  19.             <button id="increase" type="button" clickable>Increase Counter</button>
  20.             <p>0</p>
  21.         </div>
  22.  
  23.         <div>
  24.             <button id="increase2" type="button">Increase Counter 2</button>
  25.             <p>0</p>
  26.         </div>
  27.        
  28.         <div
  29.        
  30.     </sf-canvas>
  31. ]])
  32.  
  33.  
  34. -- separating CSS from HTML for safetly reasons
  35. canvas:setCSS([[
  36.     p{
  37.         background-color: #FF0000;
  38.     }
  39.    
  40.     title{
  41.         position: relative;
  42.         color: #FFFFFF;
  43.     }
  44. ]])
  45.  
  46.  
  47. local titles = canvas:getElementByID("titles")
  48. local titlesChildren = titles:getChildren()
  49.  
  50. for k, v in pairs(titlesChildren) do
  51.     -- jQuery would make these really easy to implement
  52.     v:addClass("title")
  53.     v:setText("Title "..k)
  54.     v:setCSS("left", k.."px")
  55. end
  56.  
  57. titles:insertElement("<p>Hi!</p>", nil=LAST)
  58.  
  59.  
  60. local function increaseCounter(element)
  61.     local sibling = element:getNext()
  62.     local count   = tonumber(sibling:getText())
  63.     sibling:setText(tostring(count + 1))
  64. end
  65.  
  66. -- Interaction method: clickable attribute
  67. hook.add("htmlInputHandler", "", function(element, id)
  68.     if id == "increase" then
  69.         increaseCounter(element)
  70.     end
  71. end)
  72.  
  73.  
  74. -- Interaction method: callback
  75. local button = canvas:getAllElements()[4]
  76. button:setCallback("OnClick", increaseCounter)
  77.  
  78. button:setCallback("MouseEnter", function(element)
  79.     el:setCSS("color", "#00FF00")
  80. end)
  81.  
  82.  
  83. hook.add("render", "", function()
  84.    
  85.     -- https://wiki.garrysmod.com/page/Panel/GetHTMLMaterial
  86.     render.setMaterial("!"..canvas:getMaterial())
  87.     render.drawTexturedRect(0,0,1024,1024)
  88.    
  89. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement