1 // Copyright (C) 2017, 2020 Ben Asselstine
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17
18 #include <config.h>
19
20 #include <gtkmm.h>
21
22 #include "tartan-progress-bar.h"
23
24 #include "input-helpers.h"
25 #include "ucompose.hpp"
26 #include "ImageCache.h"
27 #include "GameMap.h"
28 #include "player.h"
29 #include "stacklist.h"
30 #include "shieldset.h"
31 #include "PixMask.h"
32 #include "playerlist.h"
33 #include "font-size.h"
34
TartanProgressBar(Player * p)35 TartanProgressBar::TartanProgressBar(Player *p)
36 {
37 player = p;
38 percent = 0;
39 }
40
~TartanProgressBar()41 TartanProgressBar::~TartanProgressBar()
42 {
43 }
44
calculate_percentage() const45 double TartanProgressBar::calculate_percentage () const
46 {
47 static int c = 0;
48 c++;
49 guint32 count = player->getStacklist()->size() -
50 player->getStacklist()->countMovableStacks();
51 return (double) count / (double)player->getStacklist()->size();
52 }
53
pulse(Player * p)54 void TartanProgressBar::pulse (Player *p)
55 {
56 player = p;
57 double new_percent = calculate_percentage ();
58 if (new_percent == percent)
59 return;
60 percent = new_percent;
61 queue_draw();
62 while (g_main_context_iteration(NULL, FALSE)); //doEvents
63 }
64
on_draw(const Cairo::RefPtr<Cairo::Context> & cr)65 bool TartanProgressBar::on_draw (const Cairo::RefPtr<Cairo::Context> &cr)
66 {
67 // This is where we draw on the window
68 Glib::RefPtr<Gdk::Window> window = get_window();
69 if(window && get_parent() && player)
70 {
71 PixMask *p =
72 ImageCache::getInstance()->getTartanPic (player, get_width() *
73 TARTAN_PERCENT_WIDTH,
74 GameMap::getShieldset(),
75 FontSize::getInstance()->get_height ());
76
77 set_size_request(-1, p->get_height());
78 cr->set_source(p->get_pixmap(), 0, 0);
79 if (percent < MIN_PERCENT)
80 percent = MIN_PERCENT;
81 guint32 limit = (double)p->get_width() * percent;
82 cr->rectangle(0, 0, limit, p->get_height());
83 cr->fill();
84 p =
85 ImageCache::getInstance()->getEmptyTartanPic (player, get_width() *
86 TARTAN_PERCENT_WIDTH,
87 GameMap::getShieldset(),
88 FontSize::getInstance()->get_height ());
89 cr->set_source(p->get_pixmap(), 0, 0);
90 cr->rectangle(limit, 0, p->get_width() - limit, p->get_height());
91 cr->fill();
92 }
93
94 return true;
95 }
96