aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-06-12 02:12:22 +0200
committerdec05eba <dec05eba@protonmail.com>2021-06-12 02:12:22 +0200
commitaea2950aab53fa171d073d5e001e0419098ab82d (patch)
treee8a83866477ebcbcedf2565ee72ff92077876a9c /src
parent0d4b8100cb67eb4d4e3396be20743bd0d4e2a59a (diff)
Add drop shadow
Diffstat (limited to 'src')
-rw-r--r--src/RoundedRectangle.cpp62
1 files changed, 56 insertions, 6 deletions
diff --git a/src/RoundedRectangle.cpp b/src/RoundedRectangle.cpp
index 4aad699..596d980 100644
--- a/src/RoundedRectangle.cpp
+++ b/src/RoundedRectangle.cpp
@@ -8,17 +8,38 @@ namespace QuickMedia {
radius(radius), pos(0.0f, 0.0f), size(size), rounded_rectangle_shader(rounded_rectangle_shader), band_color(sf::Color::Transparent)
{
assert(rounded_rectangle_shader);
- vertices[0].color = color;
- vertices[1].color = color;
- vertices[2].color = color;
- vertices[3].color = color;
-
vertices[0].texCoords = sf::Vector2f(0.0f, 0.0f);
vertices[1].texCoords = sf::Vector2f(1.0f, 0.0f);
vertices[2].texCoords = sf::Vector2f(1.0f, 1.0f);
vertices[3].texCoords = sf::Vector2f(0.0f, 1.0f);
-
+ set_color(color);
set_size(size);
+
+ const sf::Color shadow_color(0, 0, 0, 50);
+
+ // Top
+ drop_shadow_vertices[0].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+ drop_shadow_vertices[1].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+ drop_shadow_vertices[2].color = shadow_color;
+ drop_shadow_vertices[3].color = shadow_color;
+
+ // Bottom
+ drop_shadow_vertices[4].color = shadow_color;
+ drop_shadow_vertices[5].color = shadow_color;
+ drop_shadow_vertices[6].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+ drop_shadow_vertices[7].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+
+ // Left
+ drop_shadow_vertices[8].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+ drop_shadow_vertices[9].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+ drop_shadow_vertices[10].color = shadow_color;
+ drop_shadow_vertices[11].color = shadow_color;
+
+ // Right
+ drop_shadow_vertices[12].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+ drop_shadow_vertices[13].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
+ drop_shadow_vertices[14].color = shadow_color;
+ drop_shadow_vertices[15].color = shadow_color;
}
void RoundedRectangle::set_position(sf::Vector2f pos) {
@@ -27,6 +48,33 @@ namespace QuickMedia {
vertices[1].position = pos + sf::Vector2f(size.x, 0.0f);
vertices[2].position = pos + sf::Vector2f(size.x, size.y);
vertices[3].position = pos + sf::Vector2f(0.0f, size.y);
+
+ const sf::Color shadow_color(0, 0, 0, 50);
+ const float shadow_radius = 5.0f;
+
+ // Top
+ drop_shadow_vertices[0].position = pos + sf::Vector2f(radius*0.5f, 0.0f) - sf::Vector2f(0.0f, shadow_radius);
+ drop_shadow_vertices[1].position = pos + sf::Vector2f(size.x - radius*0.5f, 0.0f) - sf::Vector2f(0.0f, shadow_radius);
+ drop_shadow_vertices[2].position = pos + sf::Vector2f(size.x - radius*0.5f, 0.0f);
+ drop_shadow_vertices[3].position = pos + sf::Vector2f(radius*0.5f, 0.0f);
+
+ // Bottom
+ drop_shadow_vertices[4].position = pos + sf::Vector2f(radius*0.5f, size.y);
+ drop_shadow_vertices[5].position = pos + size - sf::Vector2f(radius*0.5f, 0.0f);
+ drop_shadow_vertices[6].position = pos + size - sf::Vector2f(radius*0.5f, 0.0f) + sf::Vector2f(0.0f, shadow_radius);
+ drop_shadow_vertices[7].position = pos + sf::Vector2f(radius*0.5f, size.y) + sf::Vector2f(0.0f, shadow_radius);
+
+ // Left
+ drop_shadow_vertices[8].position = pos + sf::Vector2f(-shadow_radius, size.y - radius*0.5f);
+ drop_shadow_vertices[9].position = pos + sf::Vector2f(-shadow_radius, radius*0.5f);
+ drop_shadow_vertices[10].position = pos + sf::Vector2f(0.0f, radius*0.5f);
+ drop_shadow_vertices[11].position = pos + sf::Vector2f(0.0f, size.y - radius*0.5f);
+
+ // Right
+ drop_shadow_vertices[12].position = pos + sf::Vector2f(size.x + shadow_radius, radius*0.5f);
+ drop_shadow_vertices[13].position = pos + sf::Vector2f(size.x + shadow_radius, size.y - radius*0.5f);
+ drop_shadow_vertices[14].position = pos + sf::Vector2f(size.x, size.y - radius*0.5f);
+ drop_shadow_vertices[15].position = pos + sf::Vector2f(size.x, radius*0.5f);
}
void RoundedRectangle::set_size(sf::Vector2f size) {
@@ -59,6 +107,8 @@ namespace QuickMedia {
}
void RoundedRectangle::draw(sf::RenderTarget &target) {
+ target.draw(drop_shadow_vertices, 16, sf::Quads);
+
// TODO: Remove these for optimizations. Also do the same in other places where setUniform is called
rounded_rectangle_shader->setUniform("radius", radius);
rounded_rectangle_shader->setUniform("band_pos", band_pos);