aboutsummaryrefslogtreecommitdiff
path: root/src/ChannelTopPanel.cpp
blob: 44ce007b0d363a451db6180777405d058defbbd5 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "../include/ChannelTopPanel.hpp"
#include "../include/Settings.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/Channel.hpp"
#include "../include/ColorScheme.hpp"
#include "../include/ChannelSidePanel.hpp"
#include "../include/UsersSidePanel.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
#include <cmath>

namespace dchat
{
    struct LineColor
    {
        sf::Color sideColor, centerColor;
    };
    
    const float FONT_SIZE = 40.0f;
    const float BOTTOM_LINE_HEIGHT = 2.0f;
    const float PADDING_TOP = 20.0f;
    const float PADDING_BOTTOM = PADDING_TOP;
    const float PADDING_SIDE = 20.0f;
    
    static void drawGradientLine(const sf::Vector2f &position, const sf::Vector2f &size, const LineColor &color, sf::RenderWindow &window)
    {
        sf::Vertex rectangle[] = 
        {
            sf::Vertex(position, color.sideColor),
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y), color.centerColor),
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y + size.y), color.centerColor),
            sf::Vertex(sf::Vector2f(position.x, position.y + size.y), color.sideColor),
            
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y), color.centerColor),
            sf::Vertex(sf::Vector2f(position.x + size.x, position.y), color.sideColor),
            sf::Vertex(sf::Vector2f(position.x + size.x, position.y + size.y), color.sideColor),
            sf::Vertex(sf::Vector2f(position.x + size.x * 0.5f, position.y + size.y), color.centerColor)
        };
        window.draw(rectangle, 8, sf::Quads);
    }
    
    void ChannelTopPanel::draw(sf::RenderWindow &window)
    {
        const sf::Color lineSideColor = ColorScheme::getBackgroundColor() + sf::Color(20, 0, 0);
        const sf::Color lineCenterColor = lineSideColor + sf::Color(40, 0, 0);
        
        auto windowSize = window.getSize();
        sf::RectangleShape rect(sf::Vector2f(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth()), getHeight() - BOTTOM_LINE_HEIGHT));
        rect.setPosition(ChannelSidePanel::getWidth(), 0.0f);
        rect.setFillColor(ColorScheme::getBackgroundColor());
        window.draw(rect);
        
        sf::Vector2f bottomLinePos(floor(ChannelSidePanel::getWidth() + PADDING_SIDE * Settings::getScaling()), getHeight() - BOTTOM_LINE_HEIGHT);
        sf::Vector2f bottomLineSize(floor(windowSize.x - ChannelSidePanel::getWidth() - UsersSidePanel::getWidth() - PADDING_SIDE * Settings::getScaling() * 2.0f), BOTTOM_LINE_HEIGHT);
        
        LineColor lineColor
        {
            .sideColor = lineSideColor,
            .centerColor = lineCenterColor
        };
        drawGradientLine(bottomLinePos, bottomLineSize, lineColor, window);
        
        Channel *currentChannel = Channel::getCurrent();
        if(!currentChannel) return;
        
        sf::String str = "# ";
        str += sf::String::fromUtf8(currentChannel->getName().begin(), currentChannel->getName().end());
        float fontSize = FONT_SIZE * Settings::getScaling();
        sf::Text text(str, *ResourceCache::getFont("fonts/Roboto-Regular.ttf"), fontSize);
        auto textBounds = text.getLocalBounds();
        text.setPosition(floor(ChannelSidePanel::getWidth() + PADDING_SIDE * Settings::getScaling()), PADDING_TOP);
        text.setFillColor(ColorScheme::getTextRegularColor());
        window.draw(text);
    }
    
    float ChannelTopPanel::getHeight()
    {
        float fontSize = FONT_SIZE * Settings::getScaling();
        return floor(ResourceCache::getFont("fonts/Roboto-Regular.ttf")->getLineSpacing(fontSize) + PADDING_TOP + PADDING_BOTTOM + BOTTOM_LINE_HEIGHT);
    }
}