Add coordinate labels to hexes.

This commit is contained in:
Dorian 2016-07-16 15:11:41 -04:00
parent 9a1fd04b75
commit 5b1161f2f3
1 changed files with 14 additions and 3 deletions

17
main.py
View File

@ -1,7 +1,7 @@
import collections import collections
from kivy import app, properties from kivy import app, properties
from kivy.uix import label from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Ellipse, Line from kivy.graphics import Color, Ellipse, Line
import kivy.utils import kivy.utils
@ -23,7 +23,7 @@ class StrategyGame(FloatLayout):
col = region % self.map_cols col = region % self.map_cols
# Add hex cells to make up the map. # Add hex cells to make up the map.
hex_cell = HexMapCell() hex_cell = HexMapCell(row, col)
self.main_map.add_widget(hex_cell) self.main_map.add_widget(hex_cell)
# Add overlay conditionally. # Add overlay conditionally.
@ -46,15 +46,23 @@ class StrategyGame(FloatLayout):
Color(*kivy.utils.get_random_color(alpha=.5)) Color(*kivy.utils.get_random_color(alpha=.5))
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)
hex_cell.coord_label = Label(
text=hex_cell.coordinate_text(), center_x=hex_cell.x, center_y=hex_cell.y)
# 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)
class HexMapCell(label.Label): class HexMapCell(Label):
def __init__(self, row=0, col=0, **kwargs): def __init__(self, row=0, col=0, **kwargs):
super(HexMapCell, self).__init__(**kwargs) super(HexMapCell, self).__init__(**kwargs)
self.coords = MapCoords(row, col) self.coords = MapCoords(row, col)
def coordinate_text(self):
return '({}, {})'.format(self.coords.row, self.coords.col)
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.
radius = 2 * self.height radius = 2 * self.height
@ -69,6 +77,9 @@ class HexMapCell(label.Label):
self.solid.pos = (solid_x, solid_y) self.solid.pos = (solid_x, solid_y)
self.solid.size = solid_size self.solid.size = solid_size
self.coord_label.center_x = self.x
self.coord_label.center_y = self.y
class StrategyGameApp(app.App): class StrategyGameApp(app.App):
def build(self): def build(self):