card_painter_default.vala 4.18 KB
Newer Older
1
2
// tarot implements the rules of the tarot game
// Copyright (C) 2017, 2018, 2019  Vivien Kraus
3
//
4
5
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, version 3 of the License.
7
8
9
10
11
12
13
//
// 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
14
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16
17

using Cairo;

Vivien Kraus's avatar
Vivien Kraus committed
18
19
namespace TarotCairo {
	public class DefaultCardPainter: GLib.Object, TarotCairo.CardPainter {
20
21
22
23
24
25
26
27
28
		private double _card_width = 32;
		private double _card_height = 48;
		public double card_width {
			get { return _card_width; }
		}
		public double card_height {
			get { return _card_height; }
		}
		public double border_width = 1;
29
		private void paint_border (bool selected, bool selectable, Cairo.Context context) {
30
31
32
			context.translate (- card_width / 2, - card_height / 2);
			context.rectangle (0, 0, card_width, card_height);
			context.set_line_width (border_width);
Vivien Kraus's avatar
Vivien Kraus committed
33
34
			if (selected) {
				context.set_source_rgb (0.988, 0.914, 0.31);
35
			} else if (selectable) {
Vivien Kraus's avatar
Vivien Kraus committed
36
				context.set_source_rgb (0.933, 0.933, 0.925);
37
38
			} else {
				context.set_source_rgb (0.827, 0.843, 0.812);
Vivien Kraus's avatar
Vivien Kraus committed
39
			}
40
41
42
43
44
45
			context.fill ();
			context.move_to (0, 0);
			context.line_to (0, card_height);
			context.line_to (card_width, card_height);
			context.line_to (card_width, 0);
			context.line_to (0, 0);
Vivien Kraus's avatar
Vivien Kraus committed
46
			context.set_source_rgb (0.18, 0.204, 0.212);
47
48
49
			context.stroke ();
		}
		private void paint_back (Cairo.Context context) {
50
			paint_border (false, false, context);
51
			context.rectangle (0, 0, card_width, card_height);
Vivien Kraus's avatar
Vivien Kraus committed
52
			context.set_source_rgb (0.333, 0.341, 0.325);
53
54
			context.fill ();
		}
Vivien Kraus's avatar
Vivien Kraus committed
55
56
57
		private void paint_number_and_suit (Cairo.Context context, string number, string suit, int color) {
			switch (color) {
			case 0:
Vivien Kraus's avatar
Vivien Kraus committed
58
				context.set_source_rgb (0.18, 0.204, 0.212);
Vivien Kraus's avatar
Vivien Kraus committed
59
60
				break;
			case 1:
Vivien Kraus's avatar
Vivien Kraus committed
61
				context.set_source_rgb (0.643, 0, 0);
Vivien Kraus's avatar
Vivien Kraus committed
62
63
				break;
			case 2:
Vivien Kraus's avatar
Vivien Kraus committed
64
				context.set_source_rgb (0.125, 0.29, 0.529);
Vivien Kraus's avatar
Vivien Kraus committed
65
66
67
68
69
				break;
			}
			var font_size = 4;
			context.select_font_face ("Sans", Cairo.FontSlant.NORMAL, Cairo.FontWeight.BOLD);
			context.set_font_size (font_size);
Vivien Kraus's avatar
Vivien Kraus committed
70
			context.translate (1, font_size);
Vivien Kraus's avatar
Vivien Kraus committed
71
72
73
74
75
76
77
78
79
			context.show_text (number);
			context.move_to (0, font_size);
			context.show_text (suit);
			context.move_to (6, 12);
			context.set_font_size (14);
			context.show_text (number);
			context.move_to (6, 32);
			context.show_text (suit);
		}
80
		public void paint_card (Tarot.Card card, bool selected, bool selectable, Cairo.Context context) {
81
82
83
			// This is debugging information in case you want to debug
			// an empty deck; it prints the location of the card
			// centers in pixels on the device.
84

85
86
87
88
			// double x = 0, y = 0;
			// context.user_to_device (ref x, ref y);
			// stderr.printf ("Printing card %s at (%f, %f)\n", card.to_string (), x, y);
			if (card < 78) {
89
				paint_border (selected, selectable, context);
Vivien Kraus's avatar
Vivien Kraus committed
90
91
				Tarot.Number n;
				Tarot.Suit s;
Vivien Kraus's avatar
Vivien Kraus committed
92
				string number = _ ("EXC");
Vivien Kraus's avatar
Vivien Kraus committed
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
				string suit = "*";
				int color = 2;
				if (Tarot.decompose (card, out n, out s) == 0) {
					if (s == Tarot.Suit.TRUMPS) {
						suit = "#";
						color = 2;
						number = n.to_string ();
					} else {
						switch (s) {
						case Tarot.Suit.HEARTS:
							suit = "♥";
							color = 1;
							break;
						case Tarot.Suit.CLUBS:
							suit = "♣";
							color = 0;
							break;
						case Tarot.Suit.DIAMONDS:
							suit = "♦";
							color = 1;
							break;
						case Tarot.Suit.SPADES:
							suit = "♠";
							color = 0;
							break;
						}
						number = n.to_string ();
						switch (n) {
						case 11:
Vivien Kraus's avatar
Vivien Kraus committed
122
							number = _ ("J");
Vivien Kraus's avatar
Vivien Kraus committed
123
124
							break;
						case 12:
Vivien Kraus's avatar
Vivien Kraus committed
125
							number = _ ("C");
Vivien Kraus's avatar
Vivien Kraus committed
126
127
							break;
						case 13:
Vivien Kraus's avatar
Vivien Kraus committed
128
							number = _ ("Q");
Vivien Kraus's avatar
Vivien Kraus committed
129
130
							break;
						case 14:
Vivien Kraus's avatar
Vivien Kraus committed
131
							number = _ ("K");
Vivien Kraus's avatar
Vivien Kraus committed
132
133
134
135
136
							break;
						}
					}
				}
				paint_number_and_suit (context, number, suit, color);
137
138
139
140
141
			} else if (card == 78) {
				paint_back (context);
			}
		}
	}
142
}