forked from leonardcj/pointillism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.py
executable file
·235 lines (194 loc) · 7.58 KB
/
activity.py
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Pointillism
# Copyright (C) 2008, Nirav Patel
# Copyright (C) 2011, 2012, Alan Aguiar
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact information:
# Alan Aguiar <[email protected]>
# Nirav Patel <[email protected]>
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
import puntillism
from gettext import gettext as _
from sugar3.datastore import datastore
from sugar3.graphics.objectchooser import ObjectChooser
from sugar3.graphics.toolbutton import ToolButton
from sugar3.activity.widgets import StopButton
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.activity import activity
from sugar3 import mime
import sugargame.canvas
import sugargame
import pygame
from gi.repository import Gtk
from gi.repository import Gdk
import os
class Activity(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
self.max_participants = 1
self.file_path_temp = None
self.radio_uno = 2
self.radio_dos = 12
self.paused = False
self.actividad = puntillism.Puntillism(self)
self.build_toolbar()
self._pygamecanvas = \
sugargame.canvas.PygameCanvas(
self,
main=self.actividad.run,
modules=[pygame.display])
self.set_canvas(self._pygamecanvas)
self._pygamecanvas.grab_focus()
def build_toolbar(self):
toolbar_box = ToolbarBox()
activity_button = ActivityToolbarButton(self)
toolbar_box.toolbar.insert(activity_button, -1)
activity_button.show()
barra = toolbar_box.toolbar
separador1 = Gtk.SeparatorToolItem()
separador1.props.draw = True
barra.insert(separador1, 1)
item1 = Gtk.ToolItem()
self.label_radio1 = Gtk.Label()
self.label_radio1.set_text(_('Circles between') + ' ')
item1.add(self.label_radio1)
barra.insert(item1, 2)
grayed = Gdk.Color(20000.0, 20000.0, 20000.0)
item2 = Gtk.ToolItem()
self.cradio1 = Gtk.SpinButton()
self.cradio1.modify_bg(Gtk.StateType.NORMAL, grayed)
self.cradio1.set_range(1, 20)
self.cradio1.set_increments(1, 2)
self.cradio1.props.value = self.radio_uno
self.cradio1_handler = self.cradio1.connect(
'notify::value', self.cradio1_valor)
item2.add(self.cradio1)
barra.insert(item2, 3)
item3 = Gtk.ToolItem()
self.label_and = Gtk.Label()
self.label_and.set_text(' ' + _('and') + ' ')
item3.add(self.label_and)
barra.insert(item3, 4)
item4 = Gtk.ToolItem()
self.cradio2 = Gtk.SpinButton()
self.cradio2.modify_bg(Gtk.StateType.NORMAL, grayed)
self.cradio2.set_range(1, 20)
self.cradio2.set_increments(1, 2)
self.cradio2.props.value = self.radio_dos
self.cradio2_handler = self.cradio2.connect(
'notify::value', self.cradio2_valor)
item4.add(self.cradio2)
barra.insert(item4, 5)
separator1 = Gtk.SeparatorToolItem()
separator1.props.draw = True
separator1.set_expand(False)
barra.insert(separator1, 6)
save_button = ToolButton('filesave')
save_button.set_tooltip(_('Save Image'))
save_button.connect('clicked', self._savebutton_cb)
barra.insert(save_button, 7)
save_button.show()
open_button = ToolButton('fileopen')
open_button.set_tooltip(_('Open Image'))
open_button.connect('clicked', self.open_image)
barra.insert(open_button, 8)
open_button.show()
# Pause/Play button:
pause_play = ToolButton('media-playback-pause')
pause_play.set_tooltip(_("Pause"))
pause_play.set_accelerator(_('<ctrl>space'))
pause_play.connect('clicked', self._pause_play_cb)
pause_play.show()
toolbar_box.toolbar.insert(pause_play, 9)
separator2 = Gtk.SeparatorToolItem()
separator2.props.draw = False
separator2.set_expand(True)
barra.insert(separator2, 10)
stop_button = StopButton(self)
stop_button.props.accelerator = '<Ctrl>q'
barra.insert(stop_button, -1)
stop_button.show()
self.set_toolbar_box(toolbar_box)
toolbar_box.show_all()
def cradio1_valor(self, radio, value):
self.radio_uno = int(radio.props.value)
self.actividad.poner_radio1(self.radio_uno)
def cradio2_valor(self, radio, value):
self.radio_dos = int(radio.props.value)
self.actividad.poner_radio2(self.radio_dos)
def _savebutton_cb(self, button):
pygame.event.post(pygame.event.Event(
pygame.USEREVENT, action='savebutton'))
def save_image(self, image):
journalobj = datastore.create()
journalobj.metadata['title'] = _('Pointillism')
journalobj.metadata['mime_type'] = 'image/jpeg'
file_path = os.path.join(
os.environ['SUGAR_ACTIVITY_ROOT'], 'data', 'pointillism.jpg')
pygame.image.save(image, file_path)
journalobj.set_file_path(file_path)
datastore.write(journalobj)
journalobj.destroy()
def open_image(self, button):
pygame.event.post(pygame.event.Event(
pygame.USEREVENT, action='openbutton'))
def choose_image_from_journal_cb(self):
''' Create a chooser for image objects '''
self.image_id = None
##
self.chooser = None
try:
self.chooser = ObjectChooser(
parent=self, what_filter=mime.GENERIC_TYPE_IMAGE)
except TypeError:
self.chooser = ObjectChooser(
None, self,
Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT)
if self.chooser is not None:
try:
result = self.chooser.run()
if result == Gtk.ResponseType.ACCEPT:
dsobject = self.chooser.get_selected_object()
self.file_path_temp = str(dsobject.get_file_path())[:]
finally:
self.chooser.destroy()
del self.chooser
return self.file_path_temp
def _pause_play_cb(self, button):
# Pause or unpause the game.
self.paused = not self.paused
self.actividad.set_paused(self.paused)
# Update the button to show the next action.
if self.paused:
button.set_icon_name('media-playback-start')
button.set_tooltip(_("Start"))
else:
button.set_icon_name('media-playback-pause')
button.set_tooltip(_("Pause"))
def return_image_to_pygame(self):
self.file_path_temp = self.choose_image_from_journal_cb()
return self.file_path_temp
def get_preview(self):
return self._pygamecanvas.get_preview()
def can_close(self):
self.actividad.load_image_loop = False
self.actividad.running = False
return True