From 1ab95bbf6a9414673d6441a3a4270f0ef5342396 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Sat, 16 Jul 2016 17:52:11 -0400 Subject: [PATCH 1/3] Add ability to select and deselect a hex. --- main.py | 29 ++++++++++++++++++++++++++--- strategygame.kv | 10 ---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 4f66fca..52fb444 100644 --- a/main.py +++ b/main.py @@ -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): diff --git a/strategygame.kv b/strategygame.kv index 14d7ff8..b43eefe 100644 --- a/strategygame.kv +++ b/strategygame.kv @@ -31,16 +31,6 @@ size_hint: 1, .33 background_color: .75, .71, .99, 1 -: - 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) - : size_hint: 1, None height: self.width / math.sqrt(3) \ No newline at end of file From d28e9b33addafb8e0873094c0639d1931a971718 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Sat, 16 Jul 2016 18:16:08 -0400 Subject: [PATCH 2/3] Add ability to detect various button presses. --- main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 52fb444..65f17ad 100644 --- a/main.py +++ b/main.py @@ -28,7 +28,6 @@ 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): - hex_cell.visible_on_map = True # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. @@ -102,12 +101,14 @@ class HexMapCell(Label): self.selected = not self.selected with self.canvas.after: - if self.selected: + if self.selected and touch.button == 'left': Color(*kivy.utils.get_color_from_hex('#00FF00')) + elif self.selected and touch.button == 'right': + Color(*kivy.utils.get_color_from_hex('#FF0000')) 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) + self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=4) return True From 6ba611e5397e19c0c37524c82a34787282692a4e Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Sat, 16 Jul 2016 18:54:27 -0400 Subject: [PATCH 3/3] Clean up left and right selection of hexes. --- main.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 65f17ad..e03b5dc 100644 --- a/main.py +++ b/main.py @@ -91,24 +91,27 @@ class HexMapCell(Label): 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 + return False - self.selected = not self.selected - with self.canvas.after: - if self.selected and touch.button == 'left': - Color(*kivy.utils.get_color_from_hex('#00FF00')) - elif self.selected and touch.button == 'right': - Color(*kivy.utils.get_color_from_hex('#FF0000')) - else: + if not self.collide_point(touch.x, touch.y): + with self.canvas.after: 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 False + + with self.canvas.after: + if 'button' in touch.profile and touch.button == 'left': + Color(*kivy.utils.get_color_from_hex('#00FF00')) + + if 'button' in touch.profile and touch.button == 'right': + # TODO Will refactor to have separate on_touch_up for selected target hex instead. + Color(*kivy.utils.get_color_from_hex('#FF0000')) radius = 2 * self.height - self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=4) + self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2) return True