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
198
199
200
201
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from gettext import gettext as _
import gtk
from sugar.activity import activity
from sugar.graphics.toolbarbox import ToolbarBox
from sugar.activity.widgets import ActivityToolbarButton
from sugar.activity.widgets import StopButton
from sugar.graphics.colorbutton import ColorToolButton
from sugar.graphics.toolbarbox import ToolbarButton
import sugargame.canvas
import reversi
class ReversiActivity(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
self.game = reversi.ReversiController()
self.build_toolbar()
self._pygamecanvas = sugargame.canvas.PygameCanvas(self)
self.set_canvas(self._pygamecanvas)
self._pygamecanvas.grab_focus()
self._pygamecanvas.run_pygame(self.game.run)
def build_toolbar(self):
toolbar_box = ToolbarBox()
self.set_toolbar_box(toolbar_box)
toolbar_box.show()
activity_button = ActivityToolbarButton(self)
toolbar_box.toolbar.insert(activity_button, -1)
activity_button.show()
self.build_colors_toolbar(toolbar_box)
separator = gtk.SeparatorToolItem()
separator.props.draw = False
separator.set_expand(True)
toolbar_box.toolbar.insert(separator, -1)
separator.show()
stop_button = StopButton(self)
toolbar_box.toolbar.insert(stop_button, -1)
stop_button.show()
self.show_all()
def build_colors_toolbar(self, toolbox):
colors_bar = gtk.Toolbar()
########################################################################
# Point color
item = gtk.ToolItem()
label = gtk.Label()
label.set_text('%s ' % _('Player 1'))
item.add(label)
colors_bar.insert(item, -1)
# select color
item = gtk.ToolItem()
_fill_color = ColorToolButton()
c = gtk.gdk.Color()
c.red = 65535
c.green = 65535
c.blue = 65535
_fill_color.set_color(c)
_fill_color.connect('notify::color', self.color_player1_change)
item.add(_fill_color)
colors_bar.insert(item, -1)
# Separator
separator = gtk.SeparatorToolItem()
colors_bar.insert(separator, -1)
separator.show()
########################################################################
# Back color
item = gtk.ToolItem()
label = gtk.Label()
label.set_text('%s ' % _('Player 2'))
item.add(label)
colors_bar.insert(item, -1)
# select color
item = gtk.ToolItem()
_fill_color = ColorToolButton()
c = gtk.gdk.Color()
c.red = 0
c.green = 0
c.blue = 0
_fill_color.set_color(c)
_fill_color.connect('notify::color', self.color_player2_change)
item.add(_fill_color)
colors_bar.insert(item, -1)
# Separator
separator = gtk.SeparatorToolItem()
colors_bar.insert(separator, -1)
separator.show()
########################################################################
# Line color
item = gtk.ToolItem()
label = gtk.Label()
label.set_text('%s ' % _('Lines'))
item.add(label)
colors_bar.insert(item, -1)
# select color
item = gtk.ToolItem()
_fill_color = ColorToolButton()
_fill_color.connect('notify::color', self.color_line_change)
item.add(_fill_color)
colors_bar.insert(item, -1)
# Separator
separator = gtk.SeparatorToolItem()
colors_bar.insert(separator, -1)
separator.show()
########################################################################
# Line color
item = gtk.ToolItem()
label = gtk.Label()
label.set_text('%s ' % _('Background'))
item.add(label)
colors_bar.insert(item, -1)
# select color
item = gtk.ToolItem()
_fill_color = ColorToolButton()
_fill_color.connect('notify::color', self.color_back_change)
item.add(_fill_color)
colors_bar.insert(item, -1)
# Separator
separator = gtk.SeparatorToolItem()
colors_bar.insert(separator, -1)
separator.show()
########################################################################
# Line color
item = gtk.ToolItem()
label = gtk.Label()
label.set_text('%s ' % _('Board'))
item.add(label)
colors_bar.insert(item, -1)
# select color
item = gtk.ToolItem()
_fill_color = ColorToolButton()
_fill_color.connect('notify::color', self.color_board_change)
item.add(_fill_color)
colors_bar.insert(item, -1)
########################################################################
colors_bar.show_all()
colors_button = ToolbarButton(label=_('Colors'),
page=colors_bar,
icon_name='toolbar-colors')
toolbox.toolbar.insert(colors_button, -1)
colors_button.show()
def color_player1_change(self, widget, pspec):
color = widget.get_color()
new_color = self.color_to_rgb(color)
self.game.set_player1_color(new_color)
def color_player2_change(self, widget, pspec):
color = widget.get_color()
new_color = self.color_to_rgb(color)
self.game.set_player2_color(new_color)
def color_line_change(self, widget, pspec):
color = widget.get_color()
new_color = self.color_to_rgb(color)
self.game.set_line_color(new_color)
def color_back_change(self, widget, pspec):
color = widget.get_color()
new_color = self.color_to_rgb(color)
self.game.set_back_color(new_color)
def color_board_change(self, widget, pspec):
color = widget.get_color()
new_color = self.color_to_rgb(color)
self.game.set_board_color(new_color)
def color_to_rgb(self, color):
r = color.red *255 / 65535
g = color.green *255 / 65535
b = color.blue *255 / 65535
return (r, g, b)
|