Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
17 kB
4
Indexable
Never
#include <SFML/Graphics.hpp>
#include <iostream>
#include "dirent.h"
#include <sys/stat.h>
#include <ctime>

struct fileInfo
{
    std::string name;
    std::string ext;
    std::string size;
    std::string date;
}dir1[10000], dir2[10000];

int start1 = 0, end1, start2 = 0, end2, endMax1, endMax2, emaxi;

void drawColumnHeaders(sf::RenderWindow* window, sf::Font* font);
std::string getFormattedDate(time_t timestamp);
void drawColumnFooters(sf::RenderWindow* window, sf::Font* font, float rectangleLength);
void getFiles(int& endMax1, int& endMax2);
void drawFiles(sf::RenderWindow* window, sf::Font* font, int maxFiles);
void updateAndDrawFiles(sf::RenderWindow* window, sf::Font* font, int maxFiles)
{
    window->clear(sf::Color(79, 75, 75));
    sf::RectangleShape Bar1(sf::Vector2f(window->getSize().x, 60));
    Bar1.setFillColor(sf::Color(48, 47, 47));
    Bar1.setPosition(0, 0);
    window->draw(Bar1);
    float rectangleLength = 0.2f * window->getSize().x;
    drawColumnHeaders(window, font);
    drawFiles(window, font, maxFiles);

    //Bara de jos , deasupra butoanelor
    sf::RectangleShape Bar2(sf::Vector2f(window->getSize().x, 60));
    Bar2.setFillColor(sf::Color(48, 47, 47));
    Bar2.setPosition(0, window->getSize().y - 120);
    Bar2.setOutlineColor(sf::Color::Black);
    Bar2.setOutlineThickness(3);
    window->draw(Bar2);

    // Bara de la jumatate
    sf::RectangleShape verticalBar(sf::Vector2f(3, window->getSize().y));
    verticalBar.setFillColor(sf::Color::Black);
    verticalBar.setPosition(window->getSize().x / 2.0f - 2.5f, 0);
    window->draw(verticalBar);

    drawColumnFooters(window, font, rectangleLength);

    window->display();
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(2000, 1000), "Commander");
    float rectangleLength = 0.2f * window.getSize().x;

    sf::Font font;
    font.loadFromFile("Raleway-Medium.ttf");
    
    window.clear(sf::Color(79, 75, 75));

    //Bara de sus
    sf::RectangleShape Bar1(sf::Vector2f(window.getSize().x, 60));
    Bar1.setFillColor(sf::Color(48, 47, 47));
    Bar1.setPosition(0, 0);
    window.draw(Bar1);

    drawColumnHeaders(&window, &font);
    end1 = end2 = emaxi = (window.getSize().y - 240) / 63;// setat la 12
    
    int maxFiles = window.getSize().y - 120;
    getFiles(endMax1,endMax2);// nr file din director
    if (endMax1 < end1)
        end1 = endMax1;
    if (endMax2 < end2)
        end2 = endMax2;

    drawFiles(&window, &font, maxFiles);

    //Bara de jos , deasupra butoanelor
    sf::RectangleShape Bar2(sf::Vector2f(window.getSize().x, 60));
    Bar2.setFillColor(sf::Color(48, 47, 47));
    Bar2.setPosition(0, window.getSize().y - 120);
    Bar2.setOutlineColor(sf::Color::Black);
    Bar2.setOutlineThickness(3);
    window.draw(Bar2);

    // Bara de la jumatate
    sf::RectangleShape verticalBar(sf::Vector2f(3, window.getSize().y));
    verticalBar.setFillColor(sf::Color::Black);
    verticalBar.setPosition(window.getSize().x / 2.0f - 2.5f, 0);
    window.draw(verticalBar);

    drawColumnFooters(&window, &font, rectangleLength);

    window.display();

    sf::RenderWindow secondwindow(sf::VideoMode(1000, 600), "Create a new folder");
    secondwindow.clear(sf::Color(79, 75, 75));
    secondwindow.setVisible(false);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
            {
                int col = event.mouseButton.x / (window.getSize().x / 5);

                if (event.mouseButton.y >= window.getSize().y - 60)
                {
                    switch (col)
                    {
                    case 0:
                        std::cout << "F1 Rename\n";
                        break;
                    case 1:
                        std::cout << "F2 Create\n";
                        secondwindow.setVisible(true);
                        while (secondwindow.isOpen())
                        {
                            sf::Event event;
                            while (secondwindow.pollEvent(event))
                            {
                                if (event.type == sf::Event::Closed)
                                {
                                    secondwindow.close();
                                    secondwindow.setVisible(false);
                                }
                            }
                        }
                        break;
                    case 2:
                        std::cout << "F3 Copy\n";
                        break;
                    case 3:
                        std::cout << "F4 Move\n";
                        break;
                    case 4:
                        std::cout << "F5 Delete\n";
                        break;
                    }
                }
            }

            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::F1)
                {
                    std::cout << "F1 Rename\n";
                }
                if (event.key.code == sf::Keyboard::F2)
                {
                    std::cout << "F2 Create\n";
                }
                if (event.key.code == sf::Keyboard::F3)
                {
                    std::cout << "F3 Copy\n";
                }
                if (event.key.code == sf::Keyboard::F4)
                {
                    std::cout << "F4 Move\n";
                }
                if (event.key.code == sf::Keyboard::F5)
                {
                    std::cout << "F5 Delete\n";
                }
                if (event.key.code == sf::Keyboard::S)
                {
                    if (endMax1 > emaxi)
                        if (start1 < endMax1 && end1 <= endMax1)
                            start1++, end1++;
                    updateAndDrawFiles(&window, &font, maxFiles);
                }
                if (event.key.code == sf::Keyboard::W)
                {
                    if (start1 > 0)
                        start1--, end1--;
                    updateAndDrawFiles(&window, &font, maxFiles);
                }
                if (event.key.code == sf::Keyboard::Down)
                {
                    if (endMax2 > emaxi)
                        if (start2 < endMax2 && end2 <= endMax2)
                            start2++, end2++;
                    updateAndDrawFiles(&window, &font, maxFiles);
                }
                if (event.key.code == sf::Keyboard::Up)
                {
                    if (start2 > 0)
                        start2--, end2--;
                    updateAndDrawFiles(&window, &font, maxFiles);
                }
            }
        }
    }

    return 0;
}

void drawColumnHeaders(sf::RenderWindow* window, sf::Font* font) {
    int halfLength = window->getSize().x / 2;

    char labels[4][10] = { "Name", "Ext", "Size", "Date" };
    double proportionalLength[4] = { 0.5, 0.1, 0.2, 0.2 };

    // left column headers
    int offset = 0;
    for (int i = 0; i < 4; i++)
    {
        float rectangleLength = halfLength * proportionalLength[i];
        sf::RectangleShape rectangle(sf::Vector2f(rectangleLength, 60));
        rectangle.setFillColor(sf::Color(128, 128, 128));
        rectangle.setOutlineColor(sf::Color::Black);
        rectangle.setOutlineThickness(3);

        rectangle.setPosition(offset, 63);
        window->draw(rectangle);
        offset = offset + rectangleLength;

        sf::Text text(labels[i], *font, 25);
        text.setFillColor(sf::Color::White);
        sf::FloatRect textRect = text.getLocalBounds();
        text.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);

        // Afisare text in mjl butonului
        text.setPosition(rectangle.getPosition().x + rectangle.getSize().x / 2.0f, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - textRect.height / 2.0f + textRect.height / 2.0f);

        window->draw(text);
    }

    // right column headers
    for (int i = 0; i < 4; i++)
    {
        float rectangleLength = halfLength * proportionalLength[i];
        sf::RectangleShape rectangle(sf::Vector2f(rectangleLength, 60));
        rectangle.setFillColor(sf::Color(128, 128, 128));
        rectangle.setOutlineColor(sf::Color::Black);
        rectangle.setOutlineThickness(3);

        rectangle.setPosition(offset, 63);
        window->draw(rectangle);
        offset = offset + rectangleLength;

        sf::Text text(labels[i], *font, 25);
        text.setFillColor(sf::Color::White);
        sf::FloatRect textRect = text.getLocalBounds();
        text.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);

        // Afisare text in mjl butonului
        text.setPosition(rectangle.getPosition().x + rectangle.getSize().x / 2.0f, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - textRect.height / 2.0f + textRect.height / 2.0f);

        window->draw(text);
    }
}

std::string getFormattedDate(time_t timestamp)
{
    struct tm* timeinfo = localtime(&timestamp);

    char buffer[20];
    strftime(buffer, sizeof(buffer), "%m/%d/%Y", timeinfo);

    return std::string(buffer);
}

void drawColumnFooters(sf::RenderWindow* window, sf::Font* font, float rectangleLength)
{
    char f[5][10] = { "F1 Rename", "F2 Create", "F3 Copy", "F4 Move", "F5 Delete" };
    for (int i = 0; i < 5; ++i)
    {
        sf::RectangleShape rectangle(sf::Vector2f(rectangleLength, 60));
        rectangle.setFillColor(sf::Color(79, 75, 75));
        rectangle.setOutlineColor(sf::Color::Black);
        rectangle.setOutlineThickness(3);

        rectangle.setPosition(i * rectangleLength, window->getSize().y - 60);
        window->draw(rectangle);

        sf::Text text(f[i], *font, 25);
        text.setFillColor(sf::Color::White);
        sf::FloatRect textRect = text.getLocalBounds();
        text.setOrigin(textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f);

        // Afisare text in mjl butonului
        text.setPosition(rectangle.getPosition().x + rectangle.getSize().x / 2.0f, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - textRect.height / 2.0f + textRect.height / 2.0f);

        window->draw(text);
    }
}

void getFiles(int &endMax1,int &endMax2)
{
    struct dirent* de;
    DIR* dr1 = opendir(".");
    int ind1 = 0;
    while ((de = readdir(dr1)) != NULL)
    {
        dir1[ind1].name = de->d_name;

        std::string fileName(de->d_name);
        std::size_t found = fileName.find_last_of(".");
        std::string extension = (found != std::string::npos && found != 0 && found != fileName.length() - 1) ? fileName.substr(found + 1) : "<file>";

        dir1[ind1].ext = extension;

        struct stat fileStat;
        if (stat(de->d_name, &fileStat) == 0)
        {
            dir1[ind1].size = std::to_string(fileStat.st_size) + " Bytes";
            dir1[ind1].date = getFormattedDate(fileStat.st_mtime);
        }
        ind1++;
    }
    endMax1 = ind1 - 1;
    closedir(dr1);

    DIR* dr2 = opendir(".");
    int ind2 = 0;
    while ((de = readdir(dr2)) != NULL)
    {
        dir2[ind2].name = de->d_name;

        std::string fileName(de->d_name);
        std::size_t found = fileName.find_last_of(".");
        std::string extension = (found != std::string::npos && found != 0 && found != fileName.length() - 1) ? fileName.substr(found + 1) : "<file>";

        dir2[ind2].ext = extension;

        struct stat fileStat;
        if (stat(de->d_name, &fileStat) == 0)
        {
            dir2[ind2].size = std::to_string(fileStat.st_size) + " Bytes";
            dir2[ind2].date = getFormattedDate(fileStat.st_mtime);
        }
        ind2++;
    }
    endMax2 = ind2 - 1;
}

void drawFiles(sf::RenderWindow* window, sf::Font* font, int maxFiles)
{
    int yOffset = 63 * 2;
    // left column
    int ind1 = start1;
    int halfLength = window->getSize().x / 2;
    double proportionalLength[4] = { 0.5, 0.1, 0.2, 0.2 };

    for (int l1 = 0; l1 <= end1; l1++)
    {
        int offset = 0;
        for (int i = 0; i < 4 && yOffset < maxFiles; i++)
        {
            float rectangleLength;
            if (i != 3)
                rectangleLength = halfLength * proportionalLength[i];
            else
                rectangleLength = halfLength * proportionalLength[i] - 3;
            sf::RectangleShape rectangle(sf::Vector2f(rectangleLength, 60));
            rectangle.setFillColor(sf::Color(73, 70, 70));
            rectangle.setOutlineColor(sf::Color::Black);
            rectangle.setOutlineThickness(3);

            rectangle.setPosition(offset, yOffset);
            window->draw(rectangle);
            offset = offset + rectangleLength;
            if (i == 0)
            {
                sf::Text text(dir1[ind1].name, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 20, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
            }
            else if (i == 1)
            {
                sf::Text text(dir1[ind1].ext, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 5, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
            }
            else if (i == 2)
            {
                sf::Text text(dir1[ind1].size, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 10, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
            }
            else if (i == 3)
            {
                sf::Text text(dir1[ind1].date, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 10, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
                ind1++;
            }
        }

        yOffset += 63;
    }
    yOffset = 63 * 2;
    int ind2 = start2;

    // right column
    halfLength = window->getSize().x / 2;
    for (int l2 = 0; l2 <= end2; l2++)
    {
        int offset = window->getSize().x / 2;
        for (int i = 0; i < 4 && yOffset < maxFiles; i++)
        {
            float rectangleLength = halfLength * proportionalLength[i];
            sf::RectangleShape rectangle(sf::Vector2f(rectangleLength, 60));
            rectangle.setFillColor(sf::Color(73, 70, 70));
            rectangle.setOutlineColor(sf::Color::Black);
            rectangle.setOutlineThickness(3);

            rectangle.setPosition(offset, yOffset);
            window->draw(rectangle);
            offset = offset + rectangleLength;
            if (i == 0)
            {
                sf::Text text(dir2[ind2].name, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 20, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
            }
            else if (i == 1)
            {
                sf::Text text(dir2[ind2].ext, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 5, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
            }
            else if (i == 2)
            {
                sf::Text text(dir2[ind2].size, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 10, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
            }
            else if (i == 3)
            {
                sf::Text text(dir2[ind2].date, *font, 25);
                text.setFillColor(sf::Color::White);

                text.setPosition(rectangle.getPosition().x + 10, rectangle.getPosition().y + rectangle.getSize().y / 2.0f - text.getCharacterSize() / 2.0f);
                window->draw(text);
                ind2++;
            }
        }
        yOffset += 63;
    }
}
Leave a Comment