Skip to content

Commit 2014ffd

Browse files
committed
Merge branch 'master' of github.com:TomDotBat/pixel-ui
2 parents 62f7e0b + 2f199b4 commit 2014ffd

File tree

3 files changed

+459
-196
lines changed

3 files changed

+459
-196
lines changed

lua/pixelui/drawing/cl_outlined_box.lua

Lines changed: 22 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -15,97 +15,36 @@
1515
along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
--]]
1717

18-
local setDrawColor = surface.SetDrawColor
19-
local drawOutlinedRect = surface.DrawOutlinedRect
18+
local RNDX_SHAPE_CIRCLE, RNDX_NO_TL, RNDX_NO_TR, RNDX_NO_BL, RNDX_NO_BR, RNDX_DRAW_OUTLINED
2019

2120
function PIXEL.DrawOutlinedBox(x, y, w, h, thickness, col)
22-
setDrawColor(col.r, col.g, col.b, col.a)
23-
for i = 0, thickness - 1 do
24-
drawOutlinedRect(x + i, y + i, w - i * 2, h - i * 2)
25-
end
21+
PIXEL.RNDX.DrawOutlined(0, x, y, w, h, col, thickness)
2622
end
2723

28-
local ipairs = ipairs
29-
local setTexture = surface.SetTexture
30-
local drawPoly = surface.DrawPoly
31-
local drawRect = surface.DrawRect
24+
local function DrawOutlinedRoundedBoxEx(borderSize, x, y, w, h, col, thickness, tl, tr, bl, br)
25+
if not RDNX_DRAW_OUTLINED then
26+
if not PIXEL.RNDX then return end
3227

33-
local roundedBoxCache = {}
34-
local whiteTexture = surface.GetTextureID("vgui/white")
28+
RNDX_SHAPE_CIRCLE = PIXEL.RNDX.SHAPE_CIRCLE
29+
RNDX_NO_TL = PIXEL.RNDX.NO_TL
30+
RNDX_NO_TR = PIXEL.RNDX.NO_TR
31+
RNDX_NO_BL = PIXEL.RNDX.NO_BL
32+
RNDX_NO_BR = PIXEL.RNDX.NO_BR
33+
RNDX_DRAW_OUTLINED = PIXEL.RNDX.DrawOutlined
34+
end
3535

36-
function PIXEL.DrawOutlinedRoundedBox(borderSize, x, y, w, h, col, thickness)
37-
thickness = thickness or 1
38-
39-
setDrawColor(col.r, col.g, col.b, col.a)
40-
41-
if borderSize <= 0 then
42-
PIXEL.DrawOutlinedBox(x, y, w, h, thickness, col)
43-
return
44-
end
45-
46-
local fullRight = x + w
47-
local fullBottom = y + h
48-
49-
local left, right = x + borderSize, fullRight - borderSize
50-
local top, bottom = y + borderSize, fullBottom - borderSize
51-
52-
local halfBorder = borderSize * .6
53-
54-
local width, height = w - borderSize * 2, h - borderSize * 2
36+
local flags = RNDX_SHAPE_CIRCLE
5537

56-
drawRect(x, top, thickness, height) --Left
57-
drawRect(x + w - thickness, top, thickness, height) --Right
58-
drawRect(left, y, width, thickness) --Top
59-
drawRect(left, y + h - thickness, width, thickness) --Bottom
38+
if tl == false then flags = flags + RNDX_NO_TL end
39+
if tr == false then flags = flags + RNDX_NO_TR end
40+
if bl == false then flags = flags + RNDX_NO_BL end
41+
if br == false then flags = flags + RNDX_NO_BR end
6042

61-
local cacheName = borderSize .. x .. y .. w .. h .. thickness
62-
local cache = roundedBoxCache[cacheName]
63-
if not cache then
64-
cache = {
65-
{ --Top Right
66-
{x = right, y = y}, --Outer
67-
{x = right + halfBorder, y = top - halfBorder},
68-
{x = fullRight, y = top},
69-
70-
{x = fullRight - thickness, y = top}, --Inner
71-
{x = right + halfBorder - thickness, y = top - halfBorder + thickness},
72-
{x = right, y = y + thickness}
73-
},
74-
{ --Bottom Right
75-
{x = fullRight, y = bottom}, --Outer
76-
{x = right + halfBorder, y = bottom + halfBorder},
77-
{x = right, y = fullBottom},
78-
79-
{x = right, y = fullBottom - thickness}, --Inner
80-
{x = right + halfBorder - thickness, y = bottom + halfBorder - thickness},
81-
{x = fullRight - thickness, y = bottom}
82-
},
83-
{ --Bottom Left
84-
{x = left, y = fullBottom}, --Outer
85-
{x = left - halfBorder, y = bottom + halfBorder},
86-
{x = x, y = bottom},
87-
88-
{x = x + thickness, y = bottom}, --Inner
89-
{x = left - halfBorder + thickness, y = bottom + halfBorder - thickness},
90-
{x = left, y = fullBottom - thickness},
91-
},
92-
{ --Top Left
93-
{x = x, y = top}, --Outer
94-
{x = left - halfBorder, y = top - halfBorder},
95-
{x = left, y = y},
96-
97-
{x = left, y = y + thickness}, --Inner
98-
{x = left - halfBorder + thickness, y = top - halfBorder + thickness},
99-
{x = x + thickness, y = top}
100-
}
101-
}
102-
103-
roundedBoxCache[cacheName] = cache
104-
end
43+
RNDX_DRAW_OUTLINED(borderSize, x, y, w, h, col, thickness, flags)
44+
end
10545

106-
setTexture(whiteTexture)
46+
PIXEL.DrawOutlinedRoundedBoxEx = DrawOutlinedRoundedBoxEx
10747

108-
for k,v in ipairs(cache) do
109-
drawPoly(v)
110-
end
48+
function PIXEL.DrawOutlinedRoundedBox(borderSize, x, y, w, h, col, thickness)
49+
return DrawOutlinedRoundedBoxEx(borderSize, x, y, w, h, col, thickness, true, true, true, true)
11150
end

lua/pixelui/drawing/cl_rounded_box.lua

Lines changed: 24 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -15,131 +15,42 @@
1515
along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
--]]
1717

18-
local cornerTex8 = surface.GetTextureID("gui/corner8")
19-
local cornerTex16 = surface.GetTextureID("gui/corner16")
20-
local cornerTex32 = surface.GetTextureID("gui/corner32")
21-
local cornerTex64 = surface.GetTextureID("gui/corner64")
22-
local cornerTex512 = surface.GetTextureID("gui/corner512")
18+
local RNDX_DRAW, RNDX_FLAG_TL, RNDX_FLAG_TR, RNDX_FLAG_BL, RNDX_FLAG_BR, RNDX_SHAPE_CIRCLE
2319

24-
local round = math.Round
25-
local min = math.min
26-
local floor = math.floor
20+
local function DrawFullRoundedBoxEx(borderSize, x, y, w, h, col, tl, tr, bl, br)
21+
if not RNDX_DRAW then
22+
-- just in case this frame we still dont have it
23+
if not PIXEL.RNDX then return end
2724

28-
local setDrawColor = surface.SetDrawColor
29-
local drawRect = surface.DrawRect
30-
local drawTexturedRectUV = surface.DrawTexturedRectUV
31-
local setTexture = surface.SetTexture
25+
RNDX_DRAW = PIXEL.RNDX.Draw
26+
RNDX_FLAG_TL = PIXEL.RNDX.NO_TL
27+
RNDX_FLAG_TR = PIXEL.RNDX.NO_TR
28+
RNDX_FLAG_BL = PIXEL.RNDX.NO_BL
29+
RNDX_FLAG_BR = PIXEL.RNDX.NO_BR
30+
RNDX_SHAPE_CIRCLE = PIXEL.RNDX.SHAPE_CIRCLE
31+
end
3232

33-
function PIXEL.DrawRoundedBoxEx(borderSize, x, y, w, h, col, topLeft, topRight, bottomLeft, bottomRight)
34-
setDrawColor(col.r, col.g, col.b, col.a)
35-
36-
if borderSize <= 0 then
37-
drawRect(x, y, w, h)
38-
return
39-
end
40-
41-
x = round(x)
42-
y = round(y)
43-
w = round(w)
44-
h = round(h)
45-
borderSize = min(round(borderSize), floor(w / 2))
33+
local flags = RNDX_SHAPE_CIRCLE
4634

47-
drawRect(x + borderSize, y, w - borderSize * 2, h)
48-
drawRect(x, y + borderSize, borderSize, h - borderSize * 2)
49-
drawRect(x + w - borderSize, y + borderSize, borderSize, h - borderSize * 2)
35+
if tl == false then flags = flags + RNDX_FLAG_TL end
36+
if tr == false then flags = flags + RNDX_FLAG_TR end
37+
if bl == false then flags = flags + RNDX_FLAG_BL end
38+
if br == false then flags = flags + RNDX_FLAG_BR end
5039

51-
local tex = cornerTex8
52-
if borderSize > 8 then tex = cornerTex16 end
53-
if borderSize > 16 then tex = cornerTex32 end
54-
if borderSize > 32 then tex = cornerTex64 end
55-
if borderSize > 64 then tex = cornerTex512 end
56-
57-
setTexture(tex)
58-
59-
if topLeft then
60-
drawTexturedRectUV(x, y, borderSize, borderSize, 0, 0, 1, 1)
61-
else
62-
drawRect(x, y, borderSize, borderSize)
63-
end
40+
RNDX_DRAW(borderSize, x, y, w, h, col, flags)
41+
end
6442

65-
if topRight then
66-
drawTexturedRectUV(x + w - borderSize, y, borderSize, borderSize, 1, 0, 0, 1)
67-
else
68-
drawRect(x + w - borderSize, y, borderSize, borderSize)
69-
end
43+
PIXEL.DrawFullRoundedBoxEx = DrawFullRoundedBoxEx
7044

71-
if bottomLeft then
72-
drawTexturedRectUV(x, y + h -borderSize, borderSize, borderSize, 0, 1, 1, 0)
73-
else
74-
drawRect(x, y + h - borderSize, borderSize, borderSize)
75-
end
7645

77-
if bottomRight then
78-
drawTexturedRectUV(x + w - borderSize, y + h - borderSize, borderSize, borderSize, 1, 1, 0, 0)
79-
else
80-
drawRect(x + w - borderSize, y + h - borderSize, borderSize, borderSize)
81-
end
46+
function PIXEL.DrawRoundedBoxEx(borderSize, x, y, w, h, col, topLeft, topRight, bottomLeft, bottomRight)
47+
return DrawFullRoundedBoxEx(borderSize, x, y, w, h, col, topLeft, topRight, bottomLeft, bottomRight)
8248
end
8349

84-
local drawRoundedBoxEx = PIXEL.DrawRoundedBoxEx
8550
function PIXEL.DrawRoundedBox(borderSize, x, y, w, h, col)
86-
return drawRoundedBoxEx(borderSize, x, y, w, h, col, true, true, true, true)
87-
end
88-
89-
local roundedBoxCache = {}
90-
local whiteTexture = surface.GetTextureID("vgui/white")
91-
92-
local drawPoly = surface.DrawPoly
93-
94-
function PIXEL.DrawFullRoundedBoxEx(borderSize, x, y, w, h, col, tl, tr, bl, br)
95-
setDrawColor(col.r, col.g, col.b, col.a)
96-
97-
if borderSize <= 0 then
98-
drawRect(x, y, w, h)
99-
return
100-
end
101-
102-
local fullRight = x + w
103-
local fullBottom = y + h
104-
105-
local left, right = x + borderSize, fullRight - borderSize
106-
local top, bottom = y + borderSize, fullBottom - borderSize
107-
108-
local halfBorder = borderSize * .7
109-
110-
local cacheName = borderSize .. x .. y .. w .. h
111-
local cache = roundedBoxCache[cacheName]
112-
if not cache then
113-
cache = {
114-
{x = right, y = y}, --Top Right
115-
{x = right + halfBorder, y = top - halfBorder},
116-
{x = fullRight, y = top},
117-
118-
{x = fullRight, y = bottom}, --Bottom Right
119-
{x = right + halfBorder, y = bottom + halfBorder},
120-
{x = right, y = fullBottom},
121-
122-
{x = left, y = fullBottom}, --Bottom Left
123-
{x = left - halfBorder, y = bottom + halfBorder},
124-
{x = x, y = bottom},
125-
126-
{x = x, y = top}, --Top Left
127-
{x = left - halfBorder, y = top - halfBorder},
128-
{x = left, y = y}
129-
}
130-
131-
roundedBoxCache[cacheName] = cache
132-
end
133-
134-
setTexture(whiteTexture)
135-
drawPoly(cache)
136-
137-
if not tl then drawRect(x, y, borderSize, borderSize) end
138-
if not tr then drawRect(x + w - borderSize, y, borderSize, borderSize) end
139-
if not bl then drawRect(x, y + h - borderSize, borderSize, borderSize) end
140-
if not br then drawRect(x + w - borderSize, y + h - borderSize, borderSize, borderSize) end
51+
return DrawFullRoundedBoxEx(borderSize, x, y, w, h, col, true, true, true, true)
14152
end
14253

14354
function PIXEL.DrawFullRoundedBox(borderSize, x, y, w, h, col)
144-
return PIXEL.DrawFullRoundedBoxEx(borderSize, x, y, w, h, col, true, true, true, true)
55+
return DrawFullRoundedBoxEx(borderSize, x, y, w, h, col, true, true, true, true)
14556
end

0 commit comments

Comments
 (0)