2020-03-09 12:09:22 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
|
2022-02-28 23:21:12 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-03-09 12:09:22 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-09 12:09:22 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Card.h"
|
2022-08-21 08:53:20 -03:00
|
|
|
#include <LibCards/CardPainter.h>
|
2020-03-09 12:09:22 -03:00
|
|
|
|
2021-05-20 08:01:14 -03:00
|
|
|
namespace Cards {
|
2021-05-05 10:42:59 -03:00
|
|
|
|
2022-08-20 16:21:33 -03:00
|
|
|
Card::Card(Suit suit, Rank rank)
|
2020-06-10 05:57:59 -03:00
|
|
|
: m_rect(Gfx::IntRect({}, { width, height }))
|
2022-03-18 16:53:23 -03:00
|
|
|
, m_suit(suit)
|
2022-08-20 16:21:33 -03:00
|
|
|
, m_rank(rank)
|
2020-03-09 12:09:22 -03:00
|
|
|
{
|
2022-08-20 16:21:33 -03:00
|
|
|
VERIFY(to_underlying(rank) < card_count);
|
2020-03-09 12:09:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Card::draw(GUI::Painter& painter) const
|
|
|
|
|
{
|
2022-08-21 08:53:20 -03:00
|
|
|
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());
|
2020-03-09 12:09:22 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
void Card::clear(GUI::Painter& painter, Color const& background_color) const
|
2020-03-09 12:09:22 -03:00
|
|
|
{
|
2021-05-22 02:13:31 -03:00
|
|
|
painter.fill_rect({ old_position(), { width, height } }, background_color);
|
2020-03-09 12:09:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Card::save_old_position()
|
|
|
|
|
{
|
|
|
|
|
m_old_position = m_rect.location();
|
|
|
|
|
m_old_position_valid = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
void Card::clear_and_draw(GUI::Painter& painter, Color const& background_color)
|
2020-03-09 12:09:22 -03:00
|
|
|
{
|
|
|
|
|
if (is_old_position_valid())
|
|
|
|
|
clear(painter, background_color);
|
|
|
|
|
|
|
|
|
|
draw(painter);
|
|
|
|
|
save_old_position();
|
|
|
|
|
}
|
2021-05-05 10:42:59 -03:00
|
|
|
|
|
|
|
|
}
|