aboutsummaryrefslogtreecommitdiff
path: root/src/RoundedRectangle.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-09-18 17:03:11 +0200
committerdec05eba <dec05eba@protonmail.com>2021-09-18 17:03:11 +0200
commit63774155016ad581dcf418c94cd2ec84fcf86445 (patch)
treece4a9939d2555dce80792a06d1361b9f5d8b8ec0 /src/RoundedRectangle.cpp
parent86d23983d8c9cd75af9c40f038f6b9a1f0b5fbb5 (diff)
Render selected item background as a rectangle on top instead of banding
Limit selected item background position to body content position and size
Diffstat (limited to 'src/RoundedRectangle.cpp')
-rw-r--r--src/RoundedRectangle.cpp33
1 files changed, 18 insertions, 15 deletions
diff --git a/src/RoundedRectangle.cpp b/src/RoundedRectangle.cpp
index 4776618..fc56823 100644
--- a/src/RoundedRectangle.cpp
+++ b/src/RoundedRectangle.cpp
@@ -1,13 +1,17 @@
#include "../include/RoundedRectangle.hpp"
#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
+#include <atomic>
#include <assert.h>
namespace QuickMedia {
static const float shadow_radius = 20.0f; // Has to match the shadow offset in rounded_rectangle.glsl
+ static std::atomic<float> cached_radius = 0.0f;
+ static std::atomic<float> cached_resolution_x = 0.0f;
+ static std::atomic<float> cached_resolution_y = 0.0f;
RoundedRectangle::RoundedRectangle(sf::Vector2f size, float radius, sf::Color color, sf::Shader *rounded_rectangle_shader) :
- radius(radius), pos(0.0f, 0.0f), size(size), rounded_rectangle_shader(rounded_rectangle_shader), band_color(sf::Color::Transparent)
+ radius(radius), pos(0.0f, 0.0f), size(size), rounded_rectangle_shader(rounded_rectangle_shader)
{
assert(rounded_rectangle_shader);
set_color(color);
@@ -44,22 +48,21 @@ namespace QuickMedia {
return size;
}
- void RoundedRectangle::set_band(sf::Vector2f pos, sf::Vector2f size) {
- band_pos = pos;
- band_size = size;
- }
-
- void RoundedRectangle::set_band_color(sf::Color color) {
- band_color = color;
- }
-
void RoundedRectangle::draw(sf::RenderTarget &target) {
+ const sf::Vector2f resolution = size + sf::Vector2f(shadow_radius*2.0f, shadow_radius*2.0f);
+
// 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 + sf::Vector2f(shadow_radius, shadow_radius));
- rounded_rectangle_shader->setUniform("band_size", band_size);
- rounded_rectangle_shader->setUniform("band_color", sf::Glsl::Vec4(band_color.r/255.0f, band_color.g/255.0f, band_color.b/255.0f, band_color.a/255.0f));
- rounded_rectangle_shader->setUniform("resolution", size + sf::Vector2f(shadow_radius*2.0f, shadow_radius*2.0f));
+ if(std::abs(cached_radius - radius) > 0.01f) {
+ rounded_rectangle_shader->setUniform("radius", radius);
+ cached_radius = radius;
+ }
+
+ if(std::abs(cached_resolution_x - resolution.x) > 0.01f || std::abs(cached_resolution_y - resolution.y) > 0.01f) {
+ rounded_rectangle_shader->setUniform("resolution", size + sf::Vector2f(shadow_radius*2.0f, shadow_radius*2.0f));
+ cached_resolution_x = resolution.x;
+ cached_resolution_y = resolution.y;
+ }
+
target.draw(vertices, 4, sf::Quads, rounded_rectangle_shader);
}
} \ No newline at end of file