forked from inclement/noGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetcache.py
169 lines (159 loc) · 6.26 KB
/
widgetcache.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
# Copyright 2013 Alexander Taylor
# This file is part of noGo.
# noGo 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.
# noGo 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 noGo. If not, see http://www.gnu.org/licenses/gpl-3.0.txt
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty, ListProperty, AliasProperty, StringProperty, DictProperty, BooleanProperty, StringProperty, OptionProperty
from boardwidgets import Stone, TextMarker, TriangleMarker, SquareMarker, CircleMarker, CrossMarker, VarStone, get_stone_image_location, BlackStoneSimple, WhiteStoneSimple, BlackStoneShell, WhiteStoneShell, BlackStoneDrawn, WhiteStoneDrawn, WhiteStoneBorderedShell, BlackStoneBorderedShell, WhiteStoneStylised, BlackStoneStylised
from kivy.app import App
def get_stone(colour):
stone_type = App.get_running_app().stone_type
#print 'What kind of stone to get?',stone_type
if colour == 'white':
if stone_type == 'slate and shell':
return WhiteStoneShell()
elif stone_type == 'bordered slate and shell':
return WhiteStoneBorderedShell()
elif stone_type == 'stylised':
return WhiteStoneStylised()
elif stone_type == 'simple':
return WhiteStoneSimple()
else:
return WhiteStoneDrawn()
else:
if stone_type == 'slate and shell':
return BlackStoneShell()
elif stone_type == 'bordered slate and shell':
return BlackStoneBorderedShell()
elif stone_type == 'stylised':
return BlackStoneStylised()
elif stone_type == 'simple':
return BlackStoneSimple()
else:
return BlackStoneDrawn()
class WidgetCache(object):
# Cached board widgets
blackstonecache = []
whitestonecache = []
labelcache = {}
varstonecache = []
shapecache = {}
def get_black_stone(self,size=(0,0),pos=(0,0)):
bsc = self.blackstonecache
if len(bsc) > 0:
stone = bsc.pop(0)
else:
stone = get_stone('black')
#stone.set_colour('black')
return stone
def cache_black_stone(self,stone):
self.blackstonecache.append(stone)
def get_white_stone(self,size=(0,0),pos=(0,0)):
wsc = self.whitestonecache
if len(wsc) > 0:
stone = wsc.pop(0)
else:
stone = get_stone('white')
#stone.set_colour('white')
return stone
def cache_white_stone(self,stone):
self.whitestonecache.append(stone)
def get_stone(self,colour='b'):
if colour == 'b':
return self.get_black_stone()
elif colour == 'w':
return self.get_white_stone()
else:
print 'asked for stone colour that doesn\'t exist'
def cache_stone(self,stone,colour):
if colour in ['b','black']:
self.cache_black_stone(stone)
elif colour in ['w','white']:
self.cache_white_stone(stone)
else:
print 'asked to cache stone colour that doesn\'t exist', stone, colour
def purge_stone_cache(self):
self.blackstonecache = []
self.whitestonecache = []
def get_label(self,text):
print 'Asked for cached label with text',text
print self.labelcache
lc = self.labelcache
if lc.has_key(text):
#print 'Already have such a label!'
labels = lc[text]
label = labels.pop(0)
#print 'got',label,'from',labels
if len(labels) == 0:
lc.pop(text)
return label
# if len(lc) > 0:
# alttext = lc.keys()[0]
# labels = lc[alttext]
# label = labels.pop(0)
# if len(labels)==0:
# lc.pop(alttext)
# label.text = text
# return label
label = TextMarker(text=text)
return label
def cache_label(self,label):
text = label.text
lc = self.labelcache
if not lc.has_key(text):
lc[text] = []
lc[text].append(label)
def get_var_stone(self):
vsc = self.varstonecache
if len(vsc)>0:
varstone = vsc.pop(0)
else:
varstone = VarStone()
return varstone
def cache_var_stone(self,varstone):
self.varstonecache.append(varstone)
def get_shape_marker(self, shape):
sc = self.shapecache
print 'asked for shape marker',shape,sc
if sc.has_key(shape):
markers = sc[shape]
marker = markers.pop(0)
if len(markers) == 0:
sc.pop(shape)
return marker
if shape == 'triangle':
return TriangleMarker()
elif shape == 'square':
return SquareMarker()
elif shape == 'circle':
return CircleMarker()
elif shape == 'cross':
return CrossMarker()
def cache_shape_marker(self,marker):
sc = self.shapecache
print 'asked to cache shape marker',marker,sc
if isinstance(marker,TriangleMarker):
try:
sc['triangle'].append(marker)
except KeyError:
sc['triangle'] = [marker]
elif isinstance(marker,SquareMarker):
try:
sc['square'].append(marker)
except KeyError:
sc['square'] = [marker]
elif isinstance(marker,CrossMarker):
try:
sc['cross'].append(marker)
except KeyError:
sc['cross'] = [marker]
elif isinstance(marker,CircleMarker):
try:
sc['circle'].append(marker)
except KeyError:
sc['circle'] = [marker]
def cache_marker(self,marker):
if isinstance(marker,TextMarker):
self.cache_label(marker)
else:
self.cache_shape_marker(marker)