-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzleview.py
104 lines (85 loc) · 3.87 KB
/
puzzleview.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
"""
Sturddlefish Chess App (c) 2021, 2022, 2023 Cristian Vlasceanu
-------------------------------------------------------------------------
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/>.
-------------------------------------------------------------------------
"""
from kivy.metrics import sp
from kivy.properties import *
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from boardwidget import BoardWidget
from engine import BoardModel
from puzzlelib import *
class PuzzleView(GridLayout):
_container = ObjectProperty(None)
_page_size = NumericProperty(10)
_board_size = NumericProperty(sp(250))
selection = ObjectProperty(None, allownone=True)
prev_page_size = NumericProperty(0)
next_page_size = NumericProperty(0)
play = ObjectProperty(lambda *_:None)
def __init__(self, index=0, **kwargs):
super().__init__(**kwargs)
self._collection = PuzzleCollection()
self._num_pages = (self._collection.count + self._page_size - 1) // self._page_size
self._page = []
self._offset = 0
if index:
self._offset = ((index-1) // self._page_size) * self._page_size
self._show_page(self._offset, index)
def _show_page(self, offset, selection_index=0):
self.selection = None
self._page = self._collection.get(offset, self._page_size)
self.next_page_size = min(self._collection.count - offset - len(self._page), self._page_size)
self.prev_page_size = min(self._page_size, offset)
self._offset = offset
if selection_index == 0:
selection_index = offset + 1
self._container.clear_widgets()
for puzzle in self._page:
board_view = BoardWidget(board_image='images/greyboard', grid_color=(0,0,0,0))
board_view.set_model(BoardModel(fen=puzzle[1]))
board_view.highlight_area = lambda *_:None # disable highlights
board_view.on_touch_move = lambda *_:None # disable piece dragging
if not board_view.model.turn:
board_view.rotate()
selection = Selection(size_hint=(None, None), size=(self._board_size, self._board_size))
selection.add_widget(board_view)
selection.puzzle = puzzle
selection.bind(on_touch_down=self.on_select)
if selection_index == puzzle[3]:
selection.selected = True
self.selection = selection
self._container.add_widget(selection)
page_num = offset // self._page_size + 1
self._info.text = f'Page {page_num:2d} / {self._num_pages:2d}'
if self.selection:
self._scroll.scroll_to(self.selection, animate=False)
def next_page(self):
self._show_page(self._offset + self._page_size)
def prev_page(self):
self._show_page(self._offset - self._page_size)
def on_select(self, w, touch):
if w.collide_point(*touch.pos):
prev = self.selection
self.selection = None
w.selected ^= True
if w.selected:
self.selection = w
if prev and prev != w:
prev.selected = False
class Selection(RelativeLayout):
selected: BooleanProperty(False)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.puzzle = None