]> git.sesse.net Git - ccbs/blob - bigscreen/top10scorescreen.cpp
Shape text using Pango and HarfBuzz; gives us nice ligatures and exotic scripts.
[ccbs] / bigscreen / top10scorescreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3
4 #include "resolution.h"
5 #include "top10scorescreen.h"
6 #include "fonts.h"
7 #include "theme.h"
8
9 #define RANK_X 30
10 #define PLAYER_X 70
11 #define SONG_X 370
12 #define SCORE_X (LOGICAL_SCREEN_WIDTH - 72)
13 #define SONG_MAX_WIDTH (LOGICAL_SCREEN_WIDTH - 480)
14
15 Top10ScoreScreen::Top10ScoreScreen(pqxx::connection &conn, unsigned tournament)
16         : conn(conn), tournament(tournament), scores_changed(conn, "scores"), valid(false)
17 {
18 }
19
20 Top10ScoreScreen::~Top10ScoreScreen()
21 {
22 }
23
24 bool Top10ScoreScreen::check_invalidated()
25 {
26         if (!valid)
27                 return true;
28         if (!scores_changed.get_flag())
29                 return false;
30         scores_changed.reset_flag();
31
32         // check that there are indeed changes, otherwise don't bother
33         std::vector<TopScore> scores;
34         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
35         
36         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
37                 if (seen_topscore.count(*i) == 0) {
38                         valid = false;
39                         return true;
40                 }
41         }
42
43         return false;
44 }
45
46 void Top10ScoreScreen::draw(unsigned char *buf, unsigned width, unsigned height)
47 {
48         scores_changed.reset_flag();
49         fill_background(buf, "top10scorescreen", width, height);
50         set_screen_size(width, height);
51
52         // fetch the top 10 scores
53         std::vector<TopScore> scores;
54         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
55
56         {
57                 unsigned width = my_draw_text("Today's top 10 scores", NULL, 40.0, "mainheading");
58                 my_draw_text("Today's top 10 scores", buf, 40.0, "mainheading", LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
59         }
60
61         // simple headings
62         my_draw_text("Player", buf, 12.0, "columnheading", PLAYER_X, 100);
63         my_draw_text("Song", buf, 12.0, "columnheading", SONG_X, 100);
64         width = my_draw_text("Score", NULL, 12.0, "columnheading");
65         my_draw_text("Score", buf, 12.0, "columnheading", SCORE_X - width/2, 100);
66         
67         unsigned row = 1, y = 140;
68         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
69                 std::string suffix;
70                 if (row % 2 == 1) {
71                         suffix = ".odd";
72                 } else {
73                         suffix = ".even";
74                 }
75
76                 char str[16];
77                 std::string heading_theme_element = "top10scores.rowheading" + suffix;
78                 std::string nick_theme_element = "top10scores.nick" + suffix;
79                 std::string songname_theme_element = "top10scores.songname" + suffix;
80                 std::string frequency_theme_element = "top10scores.frequency" + suffix;
81
82                 // print new entries in red
83                 if (seen_topscore.count(*i) == 0 && seen_topscore.size() > 0) {
84                         heading_theme_element = "top10scores.freshrowheading" + suffix;
85                         nick_theme_element = "top10scores.freshnick" + suffix;
86                         songname_theme_element = "top10scores.freshsongname" + suffix;
87                         frequency_theme_element = "top10scores.freshfrequency" + suffix;
88                 }
89
90                 std::sprintf(str, "%u", row++);
91                 unsigned width = my_draw_text(str, NULL, 24.0, heading_theme_element);
92                 my_draw_text(str, buf, 24.0, heading_theme_element, RANK_X - width/2, y);
93
94                 my_draw_text(i->nick, buf, 24.0, nick_theme_element, PLAYER_X, y);
95
96                 if (my_draw_text(i->title, NULL, 24.0, songname_theme_element) > SONG_MAX_WIDTH &&
97                     !i->shorttitle.empty()) {
98                         my_draw_text(i->shorttitle, buf, 24.0, songname_theme_element, SONG_X, y);
99                 } else {
100                         my_draw_text(i->title, buf, 24.0, songname_theme_element, SONG_X, y);
101                 }
102                 
103                 std::sprintf(str, "%u", i->score);
104                 width = my_draw_text(str, NULL, 24.0, frequency_theme_element);
105                 my_draw_text(str, buf, 24.0, frequency_theme_element, SCORE_X - width/2, y);
106
107                 y += 40;
108         }
109         
110         valid = true;
111         
112         seen_topscore.erase(seen_topscore.begin(), seen_topscore.end());
113         std::copy(scores.begin(), scores.end(), std::inserter(seen_topscore, seen_topscore.end()));
114 }
115
116 int Top10ScoreScreen::get_priority()
117 {
118         return 5;
119 }