]> git.sesse.net Git - ccbs/blob - bigscreen/top5chosenscreen.cpp
Center some headings a bit better.
[ccbs] / bigscreen / top5chosenscreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3
4 #include "resolution.h"
5 #include "top5chosenscreen.h"
6 #include "fonts.h"
7
8 #define RANK_X 30
9 #define SONG_X 70
10 #define FREQUENCY_X (LOGICAL_SCREEN_WIDTH - 55)
11 #define SONG_MAX_WIDTH (LOGICAL_SCREEN_WIDTH - 190)
12
13 Top5ChosenScreen::Top5ChosenScreen(pqxx::connection &conn, unsigned tournament)
14         : conn(conn), tournament(tournament), scores_changed(conn, "scores"), valid(false)
15 {
16 }
17
18 Top5ChosenScreen::~Top5ChosenScreen()
19 {
20 }
21
22 bool Top5ChosenScreen::check_invalidated()
23 {
24         if (!valid)
25                 return true;
26         if (!scores_changed.get_flag())
27                 return false;
28         scores_changed.reset_flag();
29
30         // check that there are indeed changes, otherwise don't bother
31         std::vector<TopChosen> scores;
32         conn.perform(FetchTopChosenSongsForTournament(tournament, 5, &scores));
33
34         for (std::vector<TopChosen>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
35                 if (seen_topchosen.count(*i) == 0) {
36                         valid = false;
37                         return true;
38                 }       
39         }
40         
41         return false;
42 }
43
44 void Top5ChosenScreen::draw(unsigned char *buf, unsigned width, unsigned height)
45 {
46         scores_changed.reset_flag();
47         memset(buf, 0, width * height * 4);
48         set_screen_size(width, height);
49
50         // fetch the top 5 chosen songs
51         std::vector<TopChosen> scores;
52         conn.perform(FetchTopChosenSongsForTournament(tournament, 5, &scores));
53
54         {
55                 unsigned width = my_draw_text("Today's top 5 chosen songs", NULL, 40.0);
56                 my_draw_text("Today's top 5 chosen songs", buf, 40.0, LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
57         }
58
59         // simple headings
60         my_draw_text("Song", buf, 12.0, SONG_X, 100);
61         width = my_draw_text("Frequency", NULL, 12.0);
62         my_draw_text("Frequency", buf, 12.0, FREQUENCY_X - width/2, 100);
63         
64         unsigned row = 1, y = 140;
65         for (std::vector<TopChosen>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
66                 char str[16];
67                 unsigned r = 255, g = 255, b = 255;
68
69                 // print new entries in red
70                 if (seen_topchosen.count(*i) == 0 && seen_topchosen.size() > 0) {
71                         g = b = 0;
72                 }
73
74                 std::sprintf(str, "%u", row++);
75                 unsigned width = my_draw_text(str, NULL, 24.0);
76                 my_draw_text(str, buf, 24.0, RANK_X - width/2, y);
77
78                 if (my_draw_text(i->title, NULL, 24.0) > SONG_MAX_WIDTH) {
79                         my_draw_text(i->shorttitle, buf, 24.0, SONG_X, y, r, g, b);
80                 } else {
81                         my_draw_text(i->title, buf, 24.0, SONG_X, y, r, g, b);
82                 }
83                 
84                 std::sprintf(str, "%u", i->frequency);
85                 width = my_draw_text(str, NULL, 24.0);
86                 my_draw_text(str, buf, 24.0, FREQUENCY_X - width/2, y, r, g, b);
87
88                 y += 40;
89         }
90         
91         valid = true;
92         
93         seen_topchosen.erase(seen_topchosen.begin(), seen_topchosen.end());
94         std::copy(scores.begin(), scores.end(), std::inserter(seen_topchosen, seen_topchosen.end()));
95 }
96