/
CartaAltaFrame.cpp
112 lines (105 loc) · 4.83 KB
/
CartaAltaFrame.cpp
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
/**********************************************************************************
* Copyright (C) 2022 by Giulio Sorrentino *
* gsorre84@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
**********************************************************************************/
#include "CartaAltaFrame.h"
BEGIN_EVENT_TABLE(CartaAltaFrame, wxDialog)
EVT_BUTTON(ID_BUTTON_OK, CartaAltaFrame::onOk)
EVT_PAINT(CartaAltaFrame::onPaint)
EVT_TEXT_ENTER(ID_TEXTFIELD_CARTA, CartaAltaFrame::onOk)
END_EVENT_TABLE()
CartaAltaFrame::CartaAltaFrame(wxWindow *parent, wxString nomeMazzo, wxFont *f) : wxDialog(parent, wxID_ANY, _("Gioco della carta alta"), wxDefaultPosition, wxSize(500,300), wxCAPTION | wxSYSTEM_MENU) , primaUtente(true) {
wxPoint dimStringa;
wxString str(_("Scrivere un numero da 1 a 40 che identifica la carta del mazzo da scegliere."));
SetFont(*f);
GetTextExtent(str, &dimStringa.x, &dimStringa.y);
SetClientSize(dimStringa.x, dimStringa.y*15);
valore=wxEmptyString;
s=new wxBoxSizer(wxHORIZONTAL);
box=new wxBoxSizer(wxVERTICAL);
boxPulsanti=new wxBoxSizer(wxHORIZONTAL);
IntValidator v=IntValidator(&valore, 1, 40); //inizializziamo il validatore indicando che deve prendere un numero da 1 a 40
cartaUtente=new wxTextCtrl(this, ID_TEXTFIELD_CARTA, "", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER, v);
cartaUtente->SetMaxLength(2);
s->Add(new wxStaticText(this, wxID_ANY, _("Numero della carta da prendere: ")), 0, wxALL, 4);
s->Add(cartaUtente, 0, wxALL, 4);
ok=new wxButton(this, ID_BUTTON_OK, _("OK"));
boxPulsanti->Add(ok, 0, wxALL, 4);
cancella=new wxButton(this, wxID_CANCEL, _("Annulla"));
boxPulsanti->Add(cancella, 0, wxALL, 4);
inizio=new wxStaticText(this, wxID_ANY, wxString(_("Il gioco della carta alta permette di stabilire chi gioca per primo."))+"\n"+str+"\n"+_("Il computer ne scegliera' un'altra e chi avra' il valore maggiore comincera'.")+"\n\n\n");
box->Add(inizio,0,wxALL,4);
box->Add(s);
msg=new wxStaticText(this, wxID_ANY, "\n\n\n");
box->Add(msg, 0, wxALL, 4);
box->Add(boxPulsanti);
SetSizer(box);
m=new mazzo(new elaboratoreCarteBriscola());
c=c1=NULL;
srand(time(NULL));
ok->SetFocus();
}
void CartaAltaFrame::onOk(wxCommandEvent &evt) {
wxString st;
if (c!=NULL) { //se la carta e' gia' stata presa
Close();
return;
}
long l;
valore=cartaUtente->GetValue();
if (valore=="") //se non e' stato indicato nessun valore
return;
valore.ToLong(&l);
c=carta::getCarta(m->getCarta(static_cast<size_t>(--l))); //prendiamo l'immagine della carta indicata dall'utente
l=rand()%39; //selezioniamo una carta casuale
c1=carta::getCarta(m->getCarta(l)); //ne prendiamo l'immagine
primaUtente=c->getValore()>=c1->getValore(); //confrontiamo i valori
if (!primaUtente)
st=_("Comincia prima il pc");
else
st=_("Cominci prima tu");
s->Show(false);
cancella->Show(false);
inizio->SetLabel(st);
Refresh();
ok->SetFocus();
}
void CartaAltaFrame::onPaint(wxPaintEvent &event) {
wxPaintDC dc(this);
dc.SetFont(GetFont());
wxCoord x, y, x1, y1;
wxString msg=_("Carta tua"), msg1=_("Carta del pc");
GetTextExtent(msg, &x, &y);
GetTextExtent(msg1, &x1, &y1);
if (c==NULL && c1==NULL) {
event.Skip();
return;
}
if (c!=NULL) {
dc.DrawText(msg, 0,y*2);
dc.DrawBitmap(wxBitmap(*c->getImmagine()), 0, y*3);
x = c->getLarghezzaImmagine();
}
if (c1!=NULL) {
dc.DrawText(msg1, x, y1*2);
dc.DrawBitmap(wxBitmap(*c1->getImmagine()), x, y1*3);
}
}
CartaAltaFrame::~CartaAltaFrame() {
delete m;
}