/usr/share/pyshared/gnome_sudoku/dancer.py is in gnome-sudoku 1:3.4.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
from gi.repository import Gtk,GObject
import colors
class GridDancer:
DANCE_COLORS = [colors.color_hex_to_float(hx) for hx in
[
'#cc0000', # red
'#ef2929',
'#f57900', # orange
'#fcaf3e',
'#fce94f',
'#8ae234', # green
'#73d216',
'#729fcf', # blue
'#3465a4',
'#ad7fa8', # violet
'#75507b', ]
]
STEPS_PER_ANIMATION = 10
def __init__ (self, grid):
self.animations = [self.value_dance,
self.box_dance,
self.col_dance,
self.row_dance,]
self.current_animation = self.value_dance
self.step = 0
self.grid = grid
self.dancing = False
self.adjustment = 0
def start_dancing (self):
for box in self.grid.__entries__.values():
box.props.can_focus = False
if box.read_only:
box.read_only = False
box.need_restore = True
else:
box.need_restore = False
self.grid.get_toplevel().child_focus(Gtk.DirectionType.TAB_BACKWARD)
self.dancing = True
GObject.timeout_add(350, self.dance_grid)
def stop_dancing (self):
self.dancing = False
for box in self.grid.__entries__.values():
box.props.can_focus = True
if box.need_restore:
box.read_only = True
self.grid.unhighlight_cells()
def dance_grid (self):
if not self.dancing:
return
if self.step > self.STEPS_PER_ANIMATION:
self.rotate_animation()
self.adjustment = (self.adjustment + 1) % 9
try:
self.current_animation()
except AttributeError:
return True
self.step += 1
if self.dancing:
return True
def rotate_animation (self):
current_index = self.animations.index(self.current_animation)
next_index = (current_index + 1) % len(self.animations)
self.current_animation = self.animations[next_index]
self.step = 0
def next_color (self, current):
result = (current + self.adjustment) % len(self.DANCE_COLORS)
return self.DANCE_COLORS[result]
def col_dance (self):
for x in range(9):
color = self.next_color(x)
for y in range(9):
self.grid.__entries__[(x, y)].set_background_color(color)
def row_dance (self):
for y in range(9):
color = self.next_color(y)
for x in range(9):
self.grid.__entries__[(x, y)].set_background_color(color)
def box_dance (self):
for box in range(9):
color = self.next_color(box)
for x, y in self.grid.grid.box_coords[box]:
self.grid.__entries__[(x, y)].set_background_color(color)
def value_dance (self):
for value in range(10):
color = self.next_color(value)
for x in range(9):
for y in range(9):
box = self.grid.__entries__[(x, y)]
if box.get_value() == value:
box.set_background_color(color)
if __name__ == '__main__':
def test_dance_grid ():
import gsudoku
window = Gtk.Window()
game = '''9 1 6 3 2 8 4 5 7
5 7 4 6 1 9 2 8 3
8 3 2 5 7 4 9 6 1
6 8 7 2 4 1 3 9 5
2 9 5 7 3 6 1 4 8
3 4 1 8 9 5 7 2 6
4 6 9 1 8 7 5 3 2
1 2 8 9 5 3 6 7 4
7 5 3 4 6 2 8 1 9'''
gsd = gsudoku.SudokuGameDisplay(game)
dancer = GridDancer(gsd)
button = Gtk.Button('toggle')
button.connect('clicked',
lambda *args: dancer.stop_dancing() if dancer.dancing
else dancer.start_dancing())
vbox = Gtk.VBox()
vbox.pack_start(gsd, True, True, 0)
vbox.pack_end(button, True, True, 0)
vbox.set_focus_child(button)
window.add(vbox)
window.show_all()
window.connect('delete-event', Gtk.main_quit)
Gtk.main()
test_dance_grid()
|