aboutsummaryrefslogtreecommitdiff
path: root/src/RoomTopPanel.cpp
blob: f46bc339e553097d62264ea3253db91b66632be5 (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
#include "../include/RoomTopPanel.hpp"
#include "../include/Settings.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/ColorScheme.hpp"
#include "../include//RoomSidePanel.hpp"
#include "../include/UsersSidePanel.hpp"
#include "../include/Room.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
#include <dchat/Room.hpp>
#include <cmath>

namespace dchat
{
    struct LineColor
    {
        sf::Color sideColor, centerColor;
    };
    
    const float FONT_SIZE = 30.0f;
    const float BOTTOM_LINE_HEIGHT = 2.0f;
    const float PADDING_TOP = 15.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 RoomTopPanel::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 - RoomSidePanel::getWidth()), getHeight() - BOTTOM_LINE_HEIGHT));
        rect.setPosition(RoomSidePanel::getWidth(), 0.0f);
        rect.setFillColor(ColorScheme::getBackgroundColor());
        window.draw(rect);
        
        sf::Vector2f bottomLinePos(floor(RoomSidePanel::getWidth() + PADDING_SIDE * Settings::getScaling()), getHeight() - BOTTOM_LINE_HEIGHT);
        sf::Vector2f bottomLineSize(floor(windowSize.x - RoomSidePanel::getWidth() - PADDING_SIDE * Settings::getScaling() * 2.0f), BOTTOM_LINE_HEIGHT);
        
        LineColor lineColor;
        lineColor.sideColor = lineSideColor;
        lineColor.centerColor = lineCenterColor;
        drawGradientLine(bottomLinePos, bottomLineSize, lineColor, window);
        
        std::shared_ptr<Room> currentRoom = getCurrentRoom();
        if(!currentRoom) return;
        
        sf::String str = "# ";
        str += sf::String::fromUtf8(currentRoom->name.begin(), currentRoom->name.end());
        float fontSize = FONT_SIZE * Settings::getScaling();
        sf::Text text(str, *ResourceCache::getFont("fonts/Nunito-Regular.ttf"), fontSize);
        text.setPosition(floor(RoomSidePanel::getWidth() + PADDING_SIDE * Settings::getScaling()), PADDING_TOP);
        text.setFillColor(ColorScheme::getTextRegularColor());
        window.draw(text);
    }
    
    float RoomTopPanel::getHeight()
    {
        float fontSize = FONT_SIZE * Settings::getScaling();
        return floor(ResourceCache::getFont("fonts/Nunito-Regular.ttf")->getLineSpacing(fontSize) + PADDING_TOP + PADDING_BOTTOM + BOTTOM_LINE_HEIGHT);
    }
}