Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/buddiespanel.py
blob: b7d821608d4ad5196b7f5abe44603a48ae0a92f2 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import gtk
import hippo
import math

from sugar.graphics.canvasicon import CanvasIcon
from sugar.graphics.xocolor import XoColor
from sugar.graphics import color
from sugar.graphics import font
from sugar.graphics import units


class BuddyPlayer(hippo.CanvasBox, hippo.CanvasItem):
    __gtype_name__ = 'BuddyPlayer'
    _BORDER_DEFAULT = units.points_to_pixels(1.0)
 
    def __init__(self, buddy, **kargs):
        hippo.CanvasBox.__init__(self, **kargs)
       
        self._radius = units.points_to_pixels(5)
        self.props.border_color = 0
        self.props.background_color = 0
        self.props.orientation = hippo.ORIENTATION_VERTICAL
        self.props.border = self._BORDER_DEFAULT
        self.props.border_left = self._radius
        self.props.border_right = self._radius
        
        buddy_color = buddy.props.color
        if not buddy_color:
            buddy_color = "#000000,#ffffff"

        self.icon = CanvasIcon(
            icon_name='theme:stock-buddy',
            xo_color=XoColor(buddy_color))

        nick = buddy.props.nick
        if not nick:
            nick = ""
        self.name = hippo.CanvasText(text=nick, color=color.WHITE.get_int())

        self.append(self.icon)
        self.append(self.name)

        
    def do_paint_background(self, cr, damaged_box):
        [width, height] = self.get_allocation()     

        x = self._BORDER_DEFAULT / 2
        y = self._BORDER_DEFAULT / 2
        width -= self._BORDER_DEFAULT
        height -= self._BORDER_DEFAULT

        cr.move_to(x + self._radius, y);
        cr.arc(x + width - self._radius, y + self._radius,
               self._radius, math.pi * 1.5, math.pi * 2);
        cr.arc(x + width - self._radius, x + height - self._radius,
               self._radius, 0, math.pi * 0.5);
        cr.arc(x + self._radius, y + height - self._radius,
               self._radius, math.pi * 0.5, math.pi);
        cr.arc(x + self._radius, y + self._radius, self._radius,
               math.pi, math.pi * 1.5);

        hippo.cairo_set_source_rgba32(cr, self.props.background_color)
        cr.fill()


class BuddiesPanel(hippo.CanvasBox):
    _COLOR_ACTIVE = 50
    _COLOR_INACTIVE = 0
    
    def __init__(self):
        hippo.CanvasBox.__init__(self, spacing=4, padding=5,
                orientation=hippo.ORIENTATION_VERTICAL)

        self.players_box = hippo.CanvasBox(spacing=4, padding=5,
                orientation=hippo.ORIENTATION_VERTICAL)

        self.watchers_box = hippo.CanvasBox(spacing=4, padding=5,
                orientation=hippo.ORIENTATION_VERTICAL)

        self.append(self.players_box)
        self.append(hippo.CanvasWidget(widget=gtk.HSeparator()))
        self.append(self.watchers_box, hippo.PACK_EXPAND)

        self.players = {}
        self.watchers = {}
        self.last_active = None
        
    def _create_buddy_vbox (self, buddy):
        buddy_color = buddy.props.color
        if not buddy_color:
            buddy_color = "#000000,#ffffff"

        icon = CanvasIcon(
            icon_name='theme:stock-buddy',
            xo_color=XoColor(buddy_color))

        nick = buddy.props.nick
        if not nick:
            nick = ""
        name = hippo.CanvasText(text=nick, color=color.WHITE.get_int())

        vbox = hippo.CanvasBox(padding=5)
        vbox._radius = units.points_to_pixels(5)
        vbox.props.border_color = 100
        vbox.props.background_color = 200
        vbox.props.orientation = hippo.ORIENTATION_VERTICAL
        vbox.props.border = units.points_to_pixels(1.0)
        vbox.props.border_left = vbox._radius
        vbox.props.border_right = vbox._radius

        vbox.append(icon)
        vbox.append(name)

        return vbox

    def add_watcher(self, buddy):
        op = buddy.object_path()
        if self.watchers.get(op) is not None:
            return
        # if the watcher is also a player, don't add them
        if self.players.get(op) is not None:
            return

        vbox = self._create_buddy_vbox (buddy)

        self.watchers_box.append(vbox)

        self.watchers[op] = vbox

    def add_player(self, buddy):
        op = buddy.object_path()
        if self.players.get(op) is not None:
            return
        # if the player is also a watcher, drop them from the watchers
        widget = self.watchers.pop(op, None)
        if widget is not None:
            self.watchers_box.remove(widget)

        assert len(self.players) < 2

        hbox = hippo.CanvasBox(spacing=4, padding=5,
                orientation=hippo.ORIENTATION_HORIZONTAL)
        hbox.append(BuddyPlayer(buddy))

        count_font = font.DEFAULT_BOLD.get_pango_desc()
        count_font.set_size(30000)
        count = hippo.CanvasText(text="0", color=color.WHITE.get_int(),
                font_desc=count_font)
        hbox.append(count)

        self.players_box.append(hbox)

        self.players[op] = hbox
        
    def set_is_playing(self, buddy):
        hbox = self.players.get(buddy.object_path())
        bp = hbox.get_children()[0]
        bp.props.background_color = self._COLOR_ACTIVE
        bp.emit_paint_needed(0, 0, -1, -1)
        if self.last_active is not None:
            hbox = self.players.get(self.last_active.object_path())
            lbp = hbox.get_children()[0]
            lbp.props.background_color = self._COLOR_INACTIVE
            lbp.emit_paint_needed(0, 0, -1, -1)
        self.last_active = buddy
    
    def set_count(self, buddy, val):
        hbox = self.players.get(buddy.object_path())
        if hbox is None:
            return

        count = hbox.get_children()[1]
        count.props.text = str(val)

    def remove_watcher(self, buddy):
        op = buddy.object_path()
        widget = self.watchers[op]
        if widget is None:
            return

        self.watchers_box.remove(widget)
        del self.watchers[op]

        # removing someone from the game entirely should also remove them
        # from the players
        self.remove_player(buddy)

    def remove_player(self, buddy):
        op = buddy.object_path()
        widget = self.players.get(op)
        if widget is None:
            return

        self.players_box.remove(widget)
        del self.players[op]

        self.add_watcher(buddy)