penguins  1.0.0
player_info_box.cc
Go to the documentation of this file.
1 #include "gui/player_info_box.hh"
2 #include "game.h"
3 #include "gui/main.hh"
4 #include "gui/tileset.hh"
5 #include "movement.h"
6 #include <wx/bitmap.h>
7 #include <wx/dcclient.h>
8 #include <wx/defs.h>
9 #include <wx/event.h>
10 #include <wx/font.h>
11 #include <wx/gdicmn.h>
12 #include <wx/pen.h>
13 #include <wx/stattext.h>
14 #include <wx/string.h>
15 #include <wx/window.h>
16 
17 PlayerInfoBox::PlayerInfoBox(wxWindow* parent, wxWindowID id, int max_points)
18 : SimpleStaticBox(parent, id) {
19  this->root_hbox = new wxStaticBoxSizer(this, wxHORIZONTAL);
20 
21  this->penguin_window = new PlayerPenguinWindow(this, wxID_ANY);
22  this->root_hbox->Add(this->penguin_window, wxSizerFlags().Border());
23 
24  this->info_vbox = new wxBoxSizer(wxVERTICAL);
25 
26  this->name_text = new wxStaticText(this, wxID_ANY, wxEmptyString);
27  this->name_text->SetFont(this->name_text->GetFont().MakeBold());
28  this->info_vbox->Add(this->name_text, wxSizerFlags().Border(wxRIGHT));
29 
30  wxString score_test_str;
31  if (max_points < 0) max_points = -max_points;
32  do {
33  score_test_str << '0';
34  max_points /= 10;
35  } while (max_points != 0);
36  score_test_str << " points";
37 
38  this->score_text = new wxStaticText(
40  );
41  this->info_vbox->Add(this->score_text, wxSizerFlags().Border(wxRIGHT));
42 
43  this->root_hbox->Add(this->info_vbox, wxSizerFlags().Border(wxALL & ~wxLEFT));
44 }
45 
46 void PlayerInfoBox::update_data(Game* game, int idx, const wxString& name) {
47  Player* player = game_get_player(game, idx);
48 
49  this->is_current = idx == game->current_player_index;
50  this->is_blocked =
52 
53  this->name_text->SetLabel(name);
54 
55  wxString score_str;
56  score_str << player->points << " point";
57  if (player->points != 1) score_str << "s";
58  this->score_text->SetLabel(score_str);
59 
60  auto& tileset = wxGetApp().tileset;
61  this->penguin_sprite = tileset.penguin_sprites[idx % WXSIZEOF(tileset.penguin_sprites)];
62 
63  this->Refresh();
64 }
65 
67  auto& tileset = wxGetApp().tileset;
68  const wxRect rect(wxPoint(0, 0), dc.GetSize());
69  dc.DrawBitmap(tileset.ice_tiles[0], rect.GetPosition());
70  if (this->penguin_sprite.IsOk()) {
71  dc.DrawBitmap(this->penguin_sprite, rect.GetPosition());
72  }
73  if (this->is_blocked) {
74  dc.SetPen(wxPen(*wxRED, 3));
75  wxRect cross_rect = rect;
76  cross_rect.Deflate(rect.GetSize() / 10);
77  dc.DrawLine(cross_rect.GetTopLeft(), cross_rect.GetBottomRight());
78  dc.DrawLine(cross_rect.GetTopRight(), cross_rect.GetBottomLeft());
79  }
80  if (this->is_current) {
81  dc.DrawBitmap(tileset.current_penguin_overlay, rect.GetPosition());
82  }
83 }
84 
86 : wxWindow(parent, id), info_box(parent) {
88 }
89 
91  const wxBitmap& bitmap = this->info_box->get_penguin_sprite();
92  return this->FromPhys(bitmap.IsOk() ? bitmap.GetSize() : wxSize(0, 0));
93 }
94 
96  wxPaintDC dc(this);
97  this->info_box->paint_penguin_window(dc);
98 }
void paint_penguin_window(wxDC &dc)
wxBitmap penguin_sprite
wxStaticBoxSizer * root_hbox
wxBoxSizer * info_vbox
const wxBitmap & get_penguin_sprite() const
PlayerInfoBox(wxWindow *parent, wxWindowID id, int max_points)
void update_data(Game *game, int idx, const wxString &name)
PlayerPenguinWindow * penguin_window
wxStaticText * name_text
wxStaticText * score_text
PlayerInfoBox * info_box
virtual wxSize DoGetBestSize() const override
PlayerPenguinWindow(PlayerInfoBox *parent, wxWindowID id)
void on_paint(wxPaintEvent &event)
A wxStaticBox with just the border and no label.
virtual bool IsOk() const
wxSize GetSize() const
void SetPen(const wxPen &pen)
void DrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
void GetSize(wxCoord *width, wxCoord *height) const
void DrawBitmap(const wxBitmap &bitmap, wxCoord x, wxCoord y, bool useMask=false)
void Bind(const EventTag &eventType, Functor functor, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL)
wxFont & MakeBold()
wxPoint GetBottomLeft() const
wxPoint GetPosition() const
wxSize GetSize() const
wxPoint GetBottomRight() const
wxPoint GetTopRight() const
wxPoint GetTopLeft() const
wxRect & Deflate(wxCoord dx, wxCoord dy)
wxSizerItem * Add(wxWindow *window, const wxSizerFlags &flags)
virtual void SetLabel(const wxString &label)
wxSize FromPhys(const wxSize &sz) const
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL)
virtual bool SetFont(const wxFont &font)
wxFont GetFont() const
wxColour * wxRED
wxVERTICAL
wxHORIZONTAL
wxALL
wxRIGHT
wxLEFT
wxID_ANY
Player * game_get_player(const Game *self, int idx)
Returns a pointer to the player at the given index. Fails if the index isn't within the bounds of the...
Definition: game.h:394
The core of the unified game logic library, contains the Game struct.
@ GAME_PHASE_MOVEMENT
Set by movement_begin.
Definition: game.h:60
const wxSize wxDefaultSize
const wxPoint wxDefaultPosition
wxAppDerivedClass & wxGetApp()
wxEventType wxEVT_PAINT
wxString wxEmptyString
bool any_valid_player_move_exists(const Game *game, int player_idx)
Definition: movement.c:47
Movement phase functions.
#define wxST_NO_AUTORESIZE
The central struct of the application, holds the game data and settings.
Definition: game.h:237
GamePhase phase
The current state of the state machine, initially set to GAME_PHASE_NONE. Use game_set_phase for sett...
Definition: game.h:242
int current_player_index
A negative value means that there is no current player selected. Use game_set_current_player for sett...
Definition: game.h:256
Holds the data of the players of the Game.
Definition: game.h:68
int points
The score of the player, i.e. the number of collected fish.
Definition: game.h:74
int wxWindowID