summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCards/Card.cpp
blob: a42d0279e6e442c545ad503fe1c828f12c9cea38 (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
/*
 * Copyright (c) 2020, Till Mayer <till.mayer@web.de>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "Card.h"
#include <LibCards/CardPainter.h>

namespace Cards {

Card::Card(Suit suit, Rank rank)
    : m_rect(Gfx::IntRect({}, { width, height }))
    , m_suit(suit)
    , m_rank(rank)
{
    VERIFY(to_underlying(rank) < card_count);
}

void Card::draw(GUI::Painter& painter) const
{
    auto& card_painter = CardPainter::the();
    auto bitmap = [&]() {
        if (m_inverted)
            return m_upside_down ? card_painter.card_back_inverted() : card_painter.card_front_inverted(m_suit, m_rank);

        return m_upside_down ? card_painter.card_back() : card_painter.card_front(m_suit, m_rank);
    }();
    painter.blit(position(), bitmap, bitmap->rect());
}

void Card::clear(GUI::Painter& painter, Color const& background_color) const
{
    painter.fill_rect({ old_position(), { width, height } }, background_color);
}

void Card::save_old_position()
{
    m_old_position = m_rect.location();
    m_old_position_valid = true;
}

void Card::clear_and_draw(GUI::Painter& painter, Color const& background_color)
{
    if (is_old_position_valid())
        clear(painter, background_color);

    draw(painter);
    save_old_position();
}

}