Add ability to select and deselect a hex.

This commit is contained in:
Dorian 2016-07-16 17:52:11 -04:00
parent f806453bf6
commit 1ab95bbf6a
2 changed files with 26 additions and 13 deletions

29
main.py
View File

@ -28,7 +28,8 @@ class StrategyGame(FloatLayout):
# Add overlay conditionally.
if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0) and (col > 0):
print('({}, {})'.format(row, col))
hex_cell.visible_on_map = True
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
radius = 2 * hex_cell.height
@ -39,7 +40,7 @@ class StrategyGame(FloatLayout):
with hex_cell.canvas.after:
# Create the outline of hexagon, based off the centre of the hex.
Color(1, 0, 1, 1)
Color(*kivy.utils.get_color_from_hex('#A1A5AA'))
hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y, radius, 0, 360, 6), width=2)
# Create the solid background of the hexagon, from the bottom left coordinate of the hex.
@ -60,6 +61,8 @@ class HexMapCell(Label):
def __init__(self, row=0, col=0, **kwargs):
super(HexMapCell, self).__init__(**kwargs)
self.coords = MapCoords(row, col)
self.selected = False
self.visible_on_map = False
def coordinate_text(self):
return '({}, {})'.format(self.coords.row, self.coords.col)
@ -80,13 +83,33 @@ class HexMapCell(Label):
# Resize the outline of the cell.
self.ell.circle = (self.x, self.y, radius, 0, 360, 6)
# Resize the actual cell.
# Resize the actual cell.f
self.solid.pos = (solid_x, solid_y)
self.solid.size = solid_size
self.coord_label.center_x = self.x
self.coord_label.center_y = self.y
def on_touch_down(self, touch):
if super(HexMapCell, self).on_touch_down(touch):
return True
if not self.collide_point(touch.x, touch.y):
return False
if not self.visible_on_map:
return True
self.selected = not self.selected
with self.canvas.after:
if self.selected:
Color(*kivy.utils.get_color_from_hex('#00FF00'))
else:
Color(*kivy.utils.get_color_from_hex('#A1A5AA'))
radius = 2 * self.height
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2)
return True
class StrategyGameApp(app.App):
def build(self):

View File

@ -31,16 +31,6 @@
size_hint: 1, .33
background_color: .75, .71, .99, 1
<Hex@Label>:
pos_hint: {'center_x':.5, 'center_y':.5}
canvas.after:
Color:
rgba: 1,1,1,1
Ellipse:
segments: 6
pos: self.pos
size: min(self.width, self.height), min(self.width, self.height)
<HexMapCell>:
size_hint: 1, None
height: self.width / math.sqrt(3)