Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Simple code for drawing rounded rect with shadow — Gideros Forum

Simple code for drawing rounded rect with shadow

RickyngkRickyngk Member
edited March 2014 in Code snippets
Hi,
I've coded a simple code for rounded rect. However, still occur alias at the conner
function CSprite.CreateRoundRect(alpha, rgb, w, h, 
		border_apha, border_color, border_width, border_radius, 
		shadow_offsetx, shadow_offsety, shadow_alpha, shadow_color, 
		filtering)
 
	shadow_offsetx = shadow_offsetx or 0
	shadow_offsety = shadow_offsety or 0
	shadow_alpha = shadow_alpha or 0
	border_apha = border_apha or 0
	border_width = border_width or 0
	border_radius = border_radius or 0
 
	-- create rtt
	local rtt = RenderTarget.new(w + shadow_offsetx, h + shadow_offsety, filtering)
	rtt:clear(0, 0)
	bitmap = Bitmap.new(rtt)
 
	-- create shape
	local shape = Shape.new()
	shape:setFillStyle(Shape.SOLID, rgb, alpha)
	if border_apha > 0 and border_width > 0 then
		shape:setLineStyle(border_width, border_color, border_apha)
	end 
	shape:beginPath()
 
 
	local x, y = nil,nil
 
	if border_radius > 0 then
		local min_r = math.min(math.max(border_radius*0.01, 1), 10)
		local plot = math.min(floor(math.asin(min_r/border_radius)*180/pi + 0.5), 15)
 
 
		shape:moveTo(0, border_radius)
		for alpha = 180, 270, plot do
			x = border_radius*_cos(alpha) + border_radius
			y = border_radius*_sin(alpha) + border_radius
			shape:lineTo(x, y)
		end
		shape:lineTo(border_radius, 0)
		shape:lineTo(w - border_width - border_radius - 1, 0)
 
		for alpha = 270, 360, plot do
			x = border_radius*_cos(alpha) + (w - border_width - border_radius - 1)
			y = border_radius*_sin(alpha) + border_radius
			shape:lineTo(x, y)
		end
		shape:lineTo(w, border_radius)
		shape:lineTo(w, h-border_radius)
 
		for alpha = 0, 90, plot do
			x = border_radius*_cos(alpha) + (w - border_width - border_radius - 1)
			y = border_radius*_sin(alpha) + (h-border_radius)
			shape:lineTo(x, y)
		end
		shape:lineTo(w - border_width - border_radius - 1, h)
		shape:lineTo(border_radius, h)
 
		for alpha = 90, 180, plot do
			x = border_radius*_cos(alpha) + border_radius
			y = border_radius*_sin(alpha) + (h-border_radius)
			shape:lineTo(x, y)
		end
 
		shape:lineTo(0, h-border_radius)
		shape:lineTo(0, border_radius)
	else
		shape:moveTo(0, 0)
		shape:lineTo(w, 0)
		shape:lineTo(w, h)
		shape:lineTo(0, h)
	end
	shape:endPath()
 
	-- draw shadow
	if ((shadow_offsetx > 0 or shadow_offsety > 0) and shadow_alpha > 0) then
		local red = ((floor(rgb/65536) % 256)/256)
		local shadow_red = ((floor(shadow_color/65536) % 256)/256)
		red = red * shadow_red
 
		local green = ((floor(rgb/256) % 256)/256)
		local shadow_green = ((floor(shadow_color/256) % 256)/256)
		green = green * shadow_green
 
		local blue = ((rgb % 256)/256)
		local shadow_blue = (shadow_color % 256)/256
		blue = blue * shadow_blue
 
		shape:setColorTransform(red, green, blue, shadow_alpha)
		shape:setPosition(shadow_offsetx, shadow_offsety)
		rtt:draw(shape)
	end
 
	-- draw shape
	shape:setColorTransform(1, 1, 1, 1)
	shape:setPosition(0, 0)
	rtt:draw(shape)
 
	return bitmap
end
--example
CreateRoundRect(
    1, 0xffffff,         -- alpha, color
    0, 0, 0,             -- border line
    20,                    --border radius
    100, 100,         -- size
    0, 3, 0.5, 0x0,   --shadow
    false                --filtering
)
+1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.