• surface.DrawRectUV (custom lua for those who need it)
    10 replies, posted
Hey Facepunch, So I was thinking since nobody has made this before and I just thought of a quick way on how to do it I made it using the given resources I had. I kept it as simple as lua as I wasn't sure how surface.DrawRect in itself worked. If anyone wants they are free to optimize the code as it does seem quite messy and probably could be optimized or fixed for certain things as surface.DrawPoly can cause a crash if done incorrectly. [B][U]Description[/U]:[/B] surface.DrawRectUV allows you to draw a rectange with a texture / material applied to it and scale it's uv's so you can have a bar or material scale it. You can now 'clip' a material along a rectangle. For more understanding, view example below. [B][U]Usage[/U]:[/B] surface.DrawRectUV( x, y, width, height, max_width, max_height, color Color, texture Texture, Flip_X Bool, Flip_Y Bool ) [B][U]Code[/U]:[/B] [lua]function surface.DrawRectUV( xpos, ypos, w, h, maxw, maxh, color, texture, flipx, flipy ) local draw_data = {} local xpos_a,xpos_b,ypos_a,ypos_b,uv_h_a,uv_h_b,uv_w_a,uv_w_b if flipx then xpos_a = xpos-w xpos_b = xpos uv_w_a = 1+((w/maxw)-1) uv_w_b = 0 else xpos_a = xpos xpos_b = xpos+w uv_w_a = 0 uv_w_b = 1+((w/maxw)-1) end if flipy then ypos_a = ypos-h ypos_b = ypos uv_h_a = 1+((h/maxh)-1) uv_h_b = 0 else ypos_a = ypos ypos_b = ypos+h uv_h_a = 0 uv_h_b = 1+((h/maxh)-1) end draw_data[1] = {x=math.Round(xpos_a),y=math.Round(ypos_a),u=uv_h_a,v=uv_w_a} draw_data[2] = {x=math.Round(xpos_b),y=math.Round(ypos_a),u=uv_h_a,v=uv_w_b} draw_data[3] = {x=math.Round(xpos_b),y=math.Round(ypos_b),u=uv_h_b,v=uv_w_b} draw_data[4] = {x=math.Round(xpos_a),y=math.Round(ypos_b),u=uv_h_b,v=uv_w_a} surface.SetDrawColor(color) if texture then surface.SetTexture(surface.GetTextureID(texture)) end surface.DrawPoly(draw_data) end[/lua] [B][U]Example[/U]:[/B] [lua]// Example use of surface.DrawRectUV \\ local size = 128 // Using 128 because it is the size of the material (pixels). timer.Create("setSize",0.05,0,function() size = size - 1 if size <= 0 then size = 128 end end) function testDrawRectUV() surface.DrawRectUV( 10, 10, size, size, 128, 128, Color(255,255,255), "test/health", false, false ) surface.DrawRectUV( 300, 10, size, size, 128, 128, Color(255,255,255), "test/health", true, false ) surface.DrawRectUV( 320, 138, size, size, 128, 128, Color(255,255,255), "test/health", false, true ) surface.DrawRectUV( 610, 138, size, size, 128, 128, Color(255,255,255), "test/health", true, true ) end hook.Add("HUDPaint","TestDrawRectUVPaint",testDrawRectUV)[/lua] [B][U]Example Output[/U]:[/B] [IMG]http://www.bastian-de-byl.de/surface_drawrectuv.gif[/IMG] [B]*Note[/B]: [I]If this has already been made, please inform me. I was unable to find such a function on the Wiki as well as Facepunch since the search feature is still not working.[/I]
lua script yourself a girlfriend
This looks very nice good job
nice! though i dont have a use for it, it still looks nice
Cool, this should make sprite sheets a possibility. I have a suggestion, though. Don't create the tables inside of the function. Instead, make four local tables outside of the function whose contents are replaced. This is faster since it's not creating tables each time you draw.
Like this? [lua]local xPos_a,xPos_b,yPos_a,yPos_b,uv_h_a,uv_h_b,uv_w_a,uv_w_b local draw_data = {} draw_data[1] = {x=0,y=0,u=0,v=0} draw_data[2] = {x=0,y=0,u=0,v=0} draw_data[3] = {x=0,y=0,u=0,v=0} draw_data[4] = {x=0,y=0,u=0,v=0} function surface.DrawRectUV( xPos, yPos, w, h, maxW, maxH, color, texture, flipX, flipY ) if flipX then xPos_a = xPos-w xPos_b = xPos uv_w_a = 1+((w/maxW)-1) uv_w_b = 0 else xPos_a = xPos xPos_b = xPos+w uv_w_a = 0 uv_w_b = 1+((w/maxW)-1) end if flipY then yPos_a = yPos-h yPos_b = yPos uv_h_a = 1+((h/maxH)-1) uv_h_b = 0 else yPos_a = yPos yPos_b = yPos+h uv_h_a = 0 uv_h_b = 1+((h/maxH)-1) end draw_data[1]["x"] = math.Round(xPos_a) draw_data[1]["y"] = math.Round(yPos_a) draw_data[1]["u"] = uv_h_a draw_data[1]["v"] = uv_w_a draw_data[2]["x"] = math.Round(xPos_b) draw_data[2]["y"] = math.Round(yPos_a) draw_data[2]["u"] = uv_h_a draw_data[2]["v"] = uv_w_b draw_data[3]["x"] = math.Round(xPos_b) draw_data[3]["y"] = math.Round(yPos_b) draw_data[3]["u"] = uv_h_b draw_data[3]["v"] = uv_w_b draw_data[4]["x"] = math.Round(xPos_a) draw_data[4]["y"] = math.Round(yPos_b) draw_data[4]["u"] = uv_h_b draw_data[4]["v"] = uv_w_a surface.SetDrawColor(color) if texture then surface.SetTexture(surface.GetTextureID(texture)) end surface.DrawPoly(draw_data) end[/lua] Correct me if I'm wrong, but that is my interpretation of what you requested.
yep, looks good to me
[lua]1+((w/maxW)-1)[/lua] What's that?
[QUOTE=Nevec;23007261][lua]1+((w/maxW)-1)[/lua] What's that?[/QUOTE] Isn't that equal to (w/maxW)? :raise:
Whoops, guess I forgot to fix that. Thanks for pointing it out. [lua]local xPos_a,xPos_b,yPos_a,yPos_b,uv_h_a,uv_h_b,uv_w_a,uv_w_b local draw_data = {} draw_data[1] = {x=0,y=0,u=0,v=0} draw_data[2] = {x=0,y=0,u=0,v=0} draw_data[3] = {x=0,y=0,u=0,v=0} draw_data[4] = {x=0,y=0,u=0,v=0} function surface.DrawRectUV( xPos, yPos, w, h, maxW, maxH, color, texture, flipX, flipY ) if flipX then xPos_a = xPos-w xPos_b = xPos uv_w_a = w/maxW uv_w_b = 0 else xPos_a = xPos xPos_b = xPos+w uv_w_a = 0 uv_w_b = w/maxW end if flipY then yPos_a = yPos-h yPos_b = yPos uv_h_a = h/maxH uv_h_b = 0 else yPos_a = yPos yPos_b = yPos+h uv_h_a = 0 uv_h_b = h/maxH end draw_data[1]["x"] = math.Round(xPos_a) draw_data[1]["y"] = math.Round(yPos_a) draw_data[1]["u"] = uv_h_a draw_data[1]["v"] = uv_w_a draw_data[2]["x"] = math.Round(xPos_b) draw_data[2]["y"] = math.Round(yPos_a) draw_data[2]["u"] = uv_h_a draw_data[2]["v"] = uv_w_b draw_data[3]["x"] = math.Round(xPos_b) draw_data[3]["y"] = math.Round(yPos_b) draw_data[3]["u"] = uv_h_b draw_data[3]["v"] = uv_w_b draw_data[4]["x"] = math.Round(xPos_a) draw_data[4]["y"] = math.Round(yPos_b) draw_data[4]["u"] = uv_h_b draw_data[4]["v"] = uv_w_a surface.SetDrawColor(color) if texture then surface.SetTexture(surface.GetTextureID(texture)) end surface.DrawPoly(draw_data) end[/lua]
[QUOTE=theJ89;23004290]Cool, this should make sprite sheets a possibility.[/QUOTE] They have always been a possibility this just makes it a practical possibility :D
Sorry, you need to Log In to post a reply to this thread.