1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include "../include/RoundedRectangle.hpp"
#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <assert.h>
namespace QuickMedia {
static const float shadow_radius = 20.0f; // Has to match the shadow offset in rounded_rectangle.glsl
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)
{
assert(rounded_rectangle_shader);
set_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);
}
void RoundedRectangle::set_position(sf::Vector2f pos) {
this->pos = pos;
const float shadow_radius_ins = rounded_rectangle_shader->getNativeHandle() == 0 ? 0.0f : shadow_radius;
vertices[0].position = pos + sf::Vector2f(-shadow_radius_ins, -shadow_radius_ins);
vertices[1].position = pos + sf::Vector2f(shadow_radius_ins, -shadow_radius_ins) + sf::Vector2f(size.x, 0.0f);
vertices[2].position = pos + sf::Vector2f(shadow_radius_ins, shadow_radius_ins) + sf::Vector2f(size.x, size.y);
vertices[3].position = pos + sf::Vector2f(-shadow_radius_ins, shadow_radius_ins) + sf::Vector2f(0.0f, size.y);
}
void RoundedRectangle::set_size(sf::Vector2f size) {
this->size = size;
set_position(pos);
}
void RoundedRectangle::set_color(sf::Color color) {
for(size_t i = 0; i < 4; ++i)
vertices[i].color = color;
}
sf::Vector2f RoundedRectangle::get_position() const {
return pos;
}
sf::Vector2f RoundedRectangle::get_size() const {
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) {
// 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));
target.draw(vertices, 4, sf::Quads, rounded_rectangle_shader);
}
}
|