aboutsummaryrefslogtreecommitdiff
path: root/src/RoomSidePanel.cpp
blob: a7baf8ae3b5b448d8075aaa91cbebe7928811458 (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
82
83
84
85
86
87
88
#include "../include/RoomSidePanel.hpp"
#include "../include/RoomTopPanel.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/Settings.hpp"
#include "../include/ColorScheme.hpp"
#include "../include/Room.hpp"
#include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Window/Mouse.hpp>
#include <dchat/Room.hpp>
#include <vector>
#include <cmath>

using namespace std;

namespace dchat
{
    static sf::Vector2f position;
    const float WIDTH = 300.0f;
    const unsigned int FONT_SIZE = 20;
    const float PADDING_BOTTOM = 10.0f;
    const float CHANNEL_NAME_BOX_HEIGHT_RATIO = 1.5f;

    void RoomSidePanel::draw(sf::RenderWindow &window)
    {
        std::shared_ptr<Rooms> rooms = getRooms();
        if(!rooms) return;

        float posY = RoomTopPanel::getHeight();
        auto windowSize = window.getSize();
        sf::RectangleShape rect(sf::Vector2f(getWidth(), windowSize.y));
        rect.setPosition(0.0f, 0.0f);
        rect.setFillColor(ColorScheme::getPanelColor());
        window.draw(rect);
        
        const sf::Font *font = ResourceCache::getFont("fonts/Nunito-Regular.ttf");
        const float fontSize = FONT_SIZE * Settings::getScaling();
        const float fontHeight = font->getLineSpacing(fontSize);
        const float channelBoxHeight = floor(fontHeight * CHANNEL_NAME_BOX_HEIGHT_RATIO);
        
        auto mousePos = sf::Mouse::getPosition(window);
        
        position.y = posY;
        for(auto &it : rooms->getRooms())
        {
            const std::shared_ptr<Room> room = it.second;
            sf::FloatRect box(0.0f, position.y, getWidth(), channelBoxHeight);
            if(room == getCurrentRoom())
            {
                rect.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(15, 15, 15));
                rect.setSize(sf::Vector2f(box.width, box.height));
                rect.setPosition(sf::Vector2f(0.0f, position.y));
                window.draw(rect);
            }
            else if(box.contains(mousePos.x, mousePos.y))
            {
                rect.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(5, 5, 5));
                rect.setSize(sf::Vector2f(box.width, box.height));
                rect.setPosition(sf::Vector2f(0.0f, position.y));
                window.draw(rect);
                
                if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
                {
                    setCurrentRoom(room);
                }
            }
            
            // TODO: Remove this shit
            sf::String str = "# ";
            str += sf::String::fromUtf8(room->name.begin(), room->name.end());
            sf::Text text(str, *font, fontSize);
            text.setPosition(sf::Vector2f(position.x, floor(position.y + channelBoxHeight * 0.5f - fontHeight * 0.5f)));
            text.setFillColor(ColorScheme::getTextRegularColor());
            window.draw(text);
            position.y += floor(fontHeight + PADDING_BOTTOM);
        }
    }
    
    float RoomSidePanel::getWidth()
    {
        return floor(WIDTH * Settings::getScaling());
    }

    float RoomSidePanel::getHeight()
    {
        return position.y;
    }
}