GigaPasjans
Loading...
Searching...
No Matches
CardComponent.h
1// Wykonane przez Piotra ChudziƄskiego w dniu 13.04.2025
2#pragma once
3
4#include <andromenda/Component.h>
5#include <foundation/Collections.h>
6#include <game/InteractionManager.h>
7#include <phosphorus/Languages.h>
8#include "Card.h"
9#include "CardConstants.h"
10
11namespace Game
12{
17 class CardComponent : public Andromenda::Component
18 {
20 Int2 getSize() override { return CardConstants::CardSize; }
21
26 Color getTint() const
27 {
28 if (m_IsActive)
29 return CardConstants::ActiveColor;
30
31 if (CardInteraction::Grabbed == Card)
32 return CardConstants::GrabbedColor;
33
35 return CardConstants::GrabbedColor;
36
37 return Color::White;
38 }
39
40 public:
41 Card *Card;
42
43 CardComponent(::Game::Card *card) : Card(card) {}
44
46 bool IsSelectable() const override { return Card->Flipped.GetValue(); }
47
50 {
52 return;
53
54 bool isLastCard = Card->GetStack()->FindIndex(Card) == Card->GetStack()->GetSize() - 1;
55
56 if (isLastCard)
57 {
58 registry.Register(
59 Keycode::Q, _TW("card.grab"),
60 [this]
61 {
64 },
65 MouseButton::Middle);
66 } else
67 {
68 std::vector<::Game::Card *> lowerCards;
69 int32 cardIndex = Card->GetStack()->FindIndex(Card);
70
71 for (int32 i = cardIndex; i < Card->GetStack()->GetSize(); i++)
72 lowerCards.push_back(Card->GetStack()->Get(i));
73
74 registry.Register(
75 Keycode::Q, _TW("card.grab.plural"),
76 [this, lowerCards]
77 {
80 },
81 MouseButton::Middle);
82 }
83
85 registry.Register(
86 Keycode::D, _TW("card.drop.plural"),
88 MouseButton::Right);
89
91 registry.Register(
92 Keycode::D, _TW("card.drop"),
93 [this] { InteractionManager::MoveCard(CardInteraction::Grabbed, Card->GetStack()); },
94 MouseButton::Right);
95 }
96
98 void Draw(Andromenda::DrawContext &context) override
99 {
100 Int2 size = {GetBounds().Width, GetBounds().Height};
101
102 context.StyleStack.Push({Color::Black, Color::Black});
103 context.Fill({m_Position, size});
104 context.StyleStack.Pop();
105
106 context.StyleStack.Push({getTint(), Color::Black});
107 context.Border({m_Position, size});
108 context.StyleStack.Pop();
109
110 if (!Card || !Card->Flipped.GetValue())
111 return;
112
113 Andromenda::ScopeStyle scope({Card->GetTint(), Color::Black}, context.StyleStack);
114
115 WideString numberString;
116 if (Card->Type.GetEnum() == CardType::N10)
117 numberString = L"10";
118 else
119 numberString = WideString(Card->Type.GetValue());
120
121 wchar_t colorChar = Card->Color.GetValue();
122
123 context.Put(numberString, m_Position + Int2{2, 1}, true);
124
125 int xOffset = Card->Type.GetEnum() == CardType::N10 ? 4 : 3;
126 context.Put(numberString, m_Position + Int2{size.X - xOffset, size.Y - 2}, true);
127 context.Put(WideString(colorChar), m_Position + Int2{size.X - 3, 1}, true);
128 context.Put(WideString(colorChar), m_Position + Int2{2, size.Y - 2}, true);
129 context.Put(WideString(colorChar), m_Position + size / 2, true);
130 }
131 };
132} // namespace Game
Represent registry of Action objects.
Definition ActionRegistry.h:74
void Register(const Keycode &keycode, const WideString &actionText, const Callback &callback, MouseButton button=MouseButton::None)
Registers given text and callback to given key.
Definition ActionRegistry.cpp:18
Represents a renderable identified object.
Definition Component.h:23
bool m_IsActive
A value indicating, whether object is being pointed by pointer.
Definition Component.h:32
Int2 m_Position
Component position.
Definition Component.h:61
Box GetBounds()
Returns Component bounds. Position is already aligned.
Definition Components.cpp:27
Applies given style to the stack when constructed and pops when destroyed.
Definition Terminal.h:185
void Pop()
Pops style at the top.
Definition TerminalBase.cpp:45
void Push(const TerminalStyle &style)
Pushes given style to the top.
Definition TerminalBase.cpp:43
bool IsSelectable() const override
Returns value indicating, whether component is selectable.
Definition CardComponent.h:46
void ProvideActions(Andromenda::ActionRegistry &registry) override
Provides own actions to the ActionRegistry.
Definition CardComponent.h:49
void Draw(Andromenda::DrawContext &context) override
Draws self.
Definition CardComponent.h:98
Represents a playing card in a game. This class defines a card with properties such as color,...
Definition Card.h:61
static bool ProvideActions(Andromenda::ActionRegistry &registry)
Registers actions in the provided action registry.
Definition InteractionManager.cpp:286
static void MoveCard(Card *card, CardStack *targetStack, bool authorizedMove=false)
Moves a single card to the given target stack.
Definition InteractionManager.cpp:197
static void MoveCards(const std::vector< Card * > &cards, CardStack *targetStack)
Moves a group of cards to the given target stack.
Definition InteractionManager.cpp:213
static FORCE_INLINE bool Contains(const std::vector< T > &cards, const T &element)
Checks whether given value is present in a list.
Definition Collections.h:132
Represents a mutable sequence of utf-16 characters, providing various member functions for string man...
Definition WideString.h:14
A utility which contains all essential class instances and functions for rendering objects.
Definition DrawContext.h:14
void Border(const Box &box)
Draws an rounded border at given position.
Definition DrawContext.cpp:58
void Put(const String &text, Int2 position)
Puts a UTF-8 text at given position and with given text.
Definition DrawContext.cpp:7
StyleStack StyleStack
Style stack.
Definition DrawContext.h:31
void Fill(const Box &box)
Draws an rectangle at given position.
Definition DrawContext.cpp:43
static std::vector< Card * > GrabbedCards
Cards currently being moved (global state).
Definition InteractionManager.h:184
static Card * Grabbed
Currently selected card (global state).
Definition InteractionManager.h:242
Represents a two component vector. Components are 32-bit and they are signed.
Definition Int2.h:11