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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* Copyright (c) 2020, Till Mayer <till.mayer@web.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Card.h"
#include <LibGUI/Widget.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
namespace Cards {
static const NonnullRefPtr<Gfx::CharacterBitmap> s_diamond = Gfx::CharacterBitmap::create_from_ascii(
" # "
" ### "
" ##### "
" ####### "
"#########"
" ####### "
" ##### "
" ### "
" # ",
9, 9);
static const NonnullRefPtr<Gfx::CharacterBitmap> s_heart = Gfx::CharacterBitmap::create_from_ascii(
" # # "
" ### ### "
"#########"
"#########"
"#########"
" ####### "
" ##### "
" ### "
" # ",
9, 9);
static const NonnullRefPtr<Gfx::CharacterBitmap> s_spade = Gfx::CharacterBitmap::create_from_ascii(
" # "
" ### "
" ##### "
" ####### "
"#########"
"#########"
" ## # ## "
" ### "
" ### ",
9, 9);
static const NonnullRefPtr<Gfx::CharacterBitmap> s_club = Gfx::CharacterBitmap::create_from_ascii(
" ### "
" ##### "
" ##### "
" ## ### ## "
"###########"
"###########"
"#### # ####"
" ## ### ## "
" ### ",
11, 9);
static RefPtr<Gfx::Bitmap> s_background;
static RefPtr<Gfx::Bitmap> s_background_inverted;
Card::Card(Type type, uint8_t value)
: m_rect(Gfx::IntRect({}, { width, height }))
, m_front(*Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height }))
, m_type(type)
, m_value(value)
{
VERIFY(value < card_count);
Gfx::IntRect paint_rect({ 0, 0 }, { width, height });
if (s_background.is_null()) {
s_background = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { width, height });
Gfx::Painter bg_painter(*s_background);
auto image = Gfx::Bitmap::try_load_from_file("/res/icons/cards/buggie-deck.png");
VERIFY(!image.is_null());
float aspect_ratio = image->width() / static_cast<float>(image->height());
auto target_size = Gfx::IntSize(static_cast<int>(aspect_ratio * (height - 5)), height - 5);
bg_painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, card_radius);
auto inner_paint_rect = paint_rect.shrunken(2, 2);
bg_painter.fill_rect_with_rounded_corners(inner_paint_rect, Color::White, card_radius - 1);
bg_painter.draw_scaled_bitmap(
{ { (width - target_size.width()) / 2, (height - target_size.height()) / 2 }, target_size },
*image, image->rect());
s_background_inverted = invert_bitmap(*s_background);
}
Gfx::Painter painter(m_front);
auto& font = Gfx::FontDatabase::default_font().bold_variant();
auto label = labels[value];
painter.fill_rect_with_rounded_corners(paint_rect, Color::Black, card_radius);
paint_rect.shrink(2, 2);
painter.fill_rect_with_rounded_corners(paint_rect, Color::White, card_radius - 1);
paint_rect.set_height(paint_rect.height() / 2);
paint_rect.shrink(10, 6);
auto text_rect = Gfx::IntRect { 4, 6, font.width("10"), font.glyph_height() };
painter.draw_text(text_rect, label, font, Gfx::TextAlignment::Center, color());
NonnullRefPtr<Gfx::CharacterBitmap> symbol = s_diamond;
switch (m_type) {
case Diamonds:
symbol = s_diamond;
break;
case Clubs:
symbol = s_club;
break;
case Spades:
symbol = s_spade;
break;
case Hearts:
symbol = s_heart;
break;
default:
VERIFY_NOT_REACHED();
}
painter.draw_bitmap(
{ text_rect.x() + (text_rect.width() - symbol->size().width()) / 2, text_rect.bottom() + 5 },
symbol, color());
for (int y = height / 2; y < height; ++y) {
for (int x = 0; x < width; ++x) {
m_front->set_pixel(x, y, m_front->get_pixel(width - x - 1, height - y - 1));
}
}
m_front_inverted = invert_bitmap(*m_front);
}
Card::~Card()
{
}
void Card::draw(GUI::Painter& painter) const
{
VERIFY(!s_background.is_null());
if (m_inverted)
painter.blit(position(), m_upside_down ? *s_background_inverted : *m_front_inverted, m_front_inverted->rect());
else
painter.blit(position(), m_upside_down ? *s_background : *m_front, m_front->rect());
}
void Card::clear(GUI::Painter& painter, const Color& 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, const Color& background_color)
{
if (is_old_position_valid())
clear(painter, background_color);
draw(painter);
save_old_position();
}
NonnullRefPtr<Gfx::Bitmap> Card::invert_bitmap(Gfx::Bitmap& bitmap)
{
auto inverted_bitmap = bitmap.clone();
VERIFY(inverted_bitmap);
for (int y = 0; y < inverted_bitmap->height(); y++) {
for (int x = 0; x < inverted_bitmap->width(); x++) {
inverted_bitmap->set_pixel(x, y, inverted_bitmap->get_pixel(x, y).inverted());
}
}
return *inverted_bitmap;
}
}
|