Commit Graph

37 Commits

Author SHA1 Message Date
Sam Atkins
5186e617bd LibCards+Games: Remove concept of a CardStack being focused
This was only used for asking the stack if it is the one we are moving
cards from. We now have a better way to do that, by comparing against
`CardGame::moving_cards_source_stack()`, which doesn't require manually
telling a stack that it is/isn't focused.
2022-10-10 16:16:01 +01:00
Sam Atkins
ef8b1e25aa LibCards: If dropping cards over multiple valid stacks, pick the closest
Previously, dropping a card that overlapped multiple stacks that could
accept it, would always choose the stack that came first in the stacks
list, usually the leftmost one. This would feel very odd if the card
was only slightly overlapping the left stack, and 90% over the right
one. So now, we keep looking for closer stacks even once we've found a
valid one.

There may be an option that feels even better, based on the position of
the card being dragged and the card on top of the stack we're dropping
onto, but this already fixes the issue and feels very nice. :^)
2022-10-10 16:16:01 +01:00
Sam Atkins
a2f0b67aea LibCards: Add support for card dragging
Solitaire and Spider have almost identical code for dragging cards from
one stack to another, so it makes sense to move that here. But using
the term "moving" for them, instead of "focused" which (to me at least)
was not clear what it meant.
2022-10-10 16:16:01 +01:00
Sam Atkins
5960c0556f LibCards+Games: Move mark_intersecting_stacks_dirty() to CardGame
As part of this, made a const overload for `Card::rect()`. We need the
non-const one too as it's used for modifying the position of a card
that's being dragged. I plan on changing that soon but we'll see.
2022-10-10 16:16:01 +01:00
Sam Atkins
12612703f9 LibCards+Spider: Move ensure_top_card_is_visible() logic to CardStack 2022-10-10 16:16:01 +01:00
Sam Atkins
b26383bc6c LibCards: Fix some minor clang-tidy issues 2022-10-10 16:16:01 +01:00
Sam Atkins
21a818ab50 LibCards: Combine CardStack constructors
And while I'm at it, clarify the name of `associated_stack` to
`covered_stack`. It's used in exactly one way, so we can make the code
clearer by giving it a less generic name.
2022-10-10 16:16:01 +01:00
Sam Atkins
bfa9ae809f LibCards+Games: Rename "draw" methods to "paint" for clarity
"Draw" is already a card-game term so using it for graphics was
 confusing me a lot!
2022-10-10 16:16:01 +01:00
Sam Atkins
6ef0504a42 LibCards+Games: Make CardGame responsible for managing CardStacks
Just moving some code around really.
2022-10-10 16:16:01 +01:00
Sam Atkins
46299f3853 LibCards+Games: Move "create a deck" logic to LibCards
`create_standard_deck()` is the usual 52-card deck, but more custom
setups (such as Spider's multiples-of-one-suit) can be created by
passing suit counts to `create_deck()`.
2022-10-10 16:16:01 +01:00
Sam Atkins
435e53fcfe LibCards: Remove card-back-image scaling
This was giving wonky results with images that do not fill the entire
card - and it's also unnecessary, since the Buggie image we have been
using, and the two I will be adding, are all small enough to not need
scaling anyway. :^)
2022-08-22 12:50:41 +02:00
Sam Atkins
40b1428194 LibCards: Make the card back image configurable
`CardPainter::set_background_image_path()` immediately repaints the card
back and inverted card back bitmaps if they exist, so users just need
to `update()` that part of the screen. This is handled automatically by
`CardGame`, but other users will have to do this manually.
2022-08-22 12:50:41 +02:00
Sam Atkins
7f46d31849 LibCards: Centralise card bitmap creation
Instead of each card being responsible for painting its own bitmaps, we
now have a CardPainter which is responsible for this. It paints a given
card the first time it is requested, and then re-uses that bitmap when
requested in the future. This saves memory for duplicate cards (such as
in Spider where several sets of the same suit are used) or unused ones
(for example, the inverted cards which are only used by Hearts). It
also means we don't throw away bitmaps and then re-create identical
ones when starting a new game.

We get some nice memory savings from this:

|           | Before   | After    | Before (Virtual) | After (Virtual) |
|:----------|---------:|---------:|-----------------:|----------------:|
| Hearts    | 12.2 MiB |  9.3 MiB |         25.1 MiB |        22.2 MiB |
| Spider    | 12.1 MiB | 10.1 MiB |         29.2 MiB |        22.9 MiB |
| Solitaire | 16.4 MiB |  9.0 MiB |         25.0 MiB |        21.9 MiB |

All these measurements taken from x86_64 build, from a fresh launch of
each game after the animation has finished, but without making any
moves. The Hearts value will go up once inverted cards start being
requested.
2022-08-22 12:50:41 +02:00
Sam Atkins
aac2488d5c LibCards+Games: Replace card "value" int with a Rank enum
Because `card->value() == 11` is a lot less clear than `card->rank() ==
Cards::Rank::Queen`, and also safer.

Put this, along with the `Suit` enum, in the `Cards` namespace directly
instead of inside `Cards::Card`. Slightly less typing that way.
2022-08-22 12:50:41 +02:00
Sam Atkins
c5b7ad6004 LibCards: Add a CardGame base class
For now, the only feature of this is that it sets the background colour
from the `Games::Cards::BackgroundColor` config value. Later, other
card game configuration and shared behaviour can go here, to save
duplicating it in each game.
2022-08-22 12:50:41 +02:00
sin-ack
3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00
Simon Wanner
206d6ece55 LibGfx: Move other font-related files to LibGfx/Font/ 2022-04-09 23:48:18 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Lenny Maiorani
a51fce6c0f LibCards+Games: Change name of card type to card suit
Playing cards have a `suit` such as `hearts`/`diamonds`, not a
`type`. Make the internal naming consistent with the way playing cards
are typically named.
2022-03-18 23:49:34 +00:00
Lenny Maiorani
56046d3f9b Libraries: Change enums to enum classes in LibCards 2022-03-18 19:59:43 +01:00
Lenny Maiorani
11b28c88fc Libraries: Use default constructors/destructors in LibCards
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-10 18:04:26 -08:00
Lenny Maiorani
d5fdc6096c Libraries: Make CharacterBitmap instances at compile-time
`CharacterBitmap` instances are generated at run-time and put on the
heap, but they can be created in a `constexpr` context and stored in
static memory.

Also, remove additional `width` and `height` `static` values in favor
of using the `constexpr` member functions of `CharacterBitmap`.

These changes also include the removal of some initialization code
which tests if the `CharacterBitmap` is created since it is always
created and removes function-local `static` values which cause
run-time branches to ensure it is initialized each time the function
is called.
2022-03-04 17:41:08 +01:00
Andreas Kling
216e21a1fa AK: Convert AK::Format formatting helpers to returning ErrorOr<void>
This isn't a complete conversion to ErrorOr<void>, but a good chunk.
The end goal here is to propagate buffer allocation failures to the
caller, and allow the use of TRY() with formatting functions.
2021-11-17 00:21:13 +01:00
Andreas Kling
0de33b3d6c LibGfx: Use ErrorOr<T> for Bitmap::try_create()
Another one that was used in a fajillion places.
2021-11-08 00:35:27 +01:00
Andreas Kling
235f39e449 LibGfx: Use ErrorOr<T> for Bitmap::try_load_from_file()
This was used in a lot of places, so this patch makes liberal use of
ErrorOr<T>::release_value_but_fixme_should_propagate_errors().
2021-11-08 00:35:27 +01:00
Andreas Kling
2da4cfcc80 LibGfx: Use ErrorOr<T> for Bitmap::clone() 2021-11-08 00:35:27 +01:00
Andreas Kling
c7d891765c LibGfx: Use "try_" prefix for static factory functions
Also mark them as [[nodiscard]].
2021-07-21 18:02:15 +02:00
Jamie Mansfield
b7e806e15e LibCards: Support non-alternating colour patience games
This introduces a new MovementType concept to LibCards, starting the
process to allow other patience games to be implemented using it - that
differ more substantially from Klondike in logic.

This is currently used for two purposes: 1. to verify that the
'grabbed' stack of cards is valid* (sequential and correct colours) and
2. to allow 'grabbed' stacks to be pushed onto same-colour,
either-colour, or alternating-colour stacks

* Klondike doesn't need this logic, as per how the game works any
  'grabbed' selection is guaranteed to be valid.
2021-06-24 10:32:53 +02:00
Timothy Flynn
f73bb164ad LibCards: Draw card stack background when the entire stack is moving
The stack background should be painted when the entire stack is being
dragged, rather than just the top card.

Fixes #7786. Regressed in 2b762ef940.
2021-06-04 23:48:25 +02:00
Timothy Flynn
2b762ef940 Solitaire+LibCards: Draw card stacks with rounded corners
Now that the cards have rounded corners, draw the stack box behind the
cards with rounded corners as well. This way, the corner of the stack
box doesn't peek out from behind the cards.

The caveat here is that the "play" card stack now needs to hold a
reference to the "waste" stack beneath it so it knows when not to draw
its background on top of the waste stack. To faciliate that, the array
of card stacks is now a NonnullRefPtrVector so the play stack can store
a smart pointer to the waste stack (instead of a raw pointer or
reference).
2021-06-04 19:11:45 +02:00
Gunnar Beutner
6093236424 LibCards: Don't draw the first card smaller than the others 2021-06-04 09:20:21 +02:00
David Isaksson
fe03630716 LibCards: Draw cards with rounded card corners
closes #7412
2021-06-03 22:58:14 +02:00
Gunnar Beutner
971f4ca71c Hearts: Highlight cards when an invalid play is attempted
This briefly inverts the selected card when the user attempts to make
an invalid play.
2021-05-26 19:57:08 +02:00
Gunnar Beutner
661a8707f0 LibCards: Fix alignment for the card labels
Previously the code didn't take into account that the label for the
"10" card is larger than for the other cards.
2021-05-22 13:15:59 +01:00
Gunnar Beutner
630430379e LibCards: Correct a spelling mistake 2021-05-22 12:03:31 +01:00
Gunnar Beutner
c3efae85f2 Games: Add Hearts 2021-05-21 23:38:18 +02:00
Gunnar Beutner
3e47eec862 Solitaire: Move cards functionality into LibCards 2021-05-21 23:38:18 +02:00