Dispatch event from a selected HexMapCell up to the parent game to update the status.

This commit is contained in:
Dorian 2016-07-16 19:35:29 -04:00
parent 6ba611e539
commit fb829f28e4
2 changed files with 17 additions and 7 deletions

23
main.py
View File

@ -11,10 +11,12 @@ MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
class StrategyGame(FloatLayout): class StrategyGame(FloatLayout):
main_map = properties.ObjectProperty(None) main_map = properties.ObjectProperty(None)
status = properties.ObjectProperty(None)
map_rows = properties.NumericProperty(0) map_rows = properties.NumericProperty(0)
map_cols = properties.NumericProperty(0) map_cols = properties.NumericProperty(0)
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.register_event_type('on_cell_selection')
super(StrategyGame, self).__init__(**kwargs) super(StrategyGame, self).__init__(**kwargs)
number_of_regions = self.map_rows * self.map_cols number_of_regions = self.map_rows * self.map_cols
@ -43,7 +45,8 @@ class StrategyGame(FloatLayout):
hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y, radius, 0, 360, 6), width=2) 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. # Create the solid background of the hexagon, from the bottom left coordinate of the hex.
Color(*kivy.utils.get_random_color(alpha=.5)) hex_cell.terrain_colour = kivy.utils.get_random_color(alpha=.5)
Color(*hex_cell.terrain_colour)
hex_cell.solid = Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6) hex_cell.solid = Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6)
Color(1, 1, 1, 1) Color(1, 1, 1, 1)
@ -55,6 +58,11 @@ class StrategyGame(FloatLayout):
# Bind the cell code so as to update its position and size when the parent widget resizes. # Bind the cell code so as to update its position and size when the parent widget resizes.
hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos) hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos)
def on_cell_selection(self, coords, terrain_colour, *args):
self.status.text = 'Coords: ({}, {})'.format(coords[0], coords[1])
self.status.background_color = Color(*terrain_colour)
return True
class HexMapCell(Label): class HexMapCell(Label):
def __init__(self, row=0, col=0, **kwargs): def __init__(self, row=0, col=0, **kwargs):
@ -62,15 +70,14 @@ class HexMapCell(Label):
self.coords = MapCoords(row, col) self.coords = MapCoords(row, col)
self.selected = False self.selected = False
self.visible_on_map = False self.visible_on_map = False
self.terrain_colour = Color(0, 0, 0, 1)
def coordinate_text(self): def map_coordinates(self):
return '({}, {})'.format(self.coords.row, self.coords.col) return self.coords.row / 3, self.coords.col / 2
def map_coordinate_text(self):
return '[{}, {}]'.format(self.coords.row / 3, self.coords.col / 2)
def map_display_text(self): def map_display_text(self):
return "{}\n{}".format(self.coordinate_text(), self.map_coordinate_text()) map_x, map_y = self.map_coordinates()
return "({}, {})".format(map_x, map_y)
def update_pos(self, instance, value): def update_pos(self, instance, value):
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
@ -112,6 +119,8 @@ class HexMapCell(Label):
Color(*kivy.utils.get_color_from_hex('#FF0000')) Color(*kivy.utils.get_color_from_hex('#FF0000'))
radius = 2 * self.height radius = 2 * self.height
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2) self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2)
self.parent.parent.parent.dispatch('on_cell_selection', self.map_coordinates(), self.terrain_colour)
return True return True

View File

@ -4,6 +4,7 @@
<StrategyGame>: <StrategyGame>:
id: _game id: _game
main_map: _main_map main_map: _main_map
status: _stats
map_rows: 15 map_rows: 15
map_cols: 10 map_cols: 10
BoxLayout: BoxLayout: