doric-engine/render_test/main.py

74 lines
2.3 KiB
Python
Raw Permalink Normal View History

from kivy.app import App
2017-07-27 08:28:46 -04:00
from kivy.graphics import *
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.utils import get_color_from_hex
2017-07-27 08:28:46 -04:00
HEX_SIZE = 100
HORIZONTAL_BUFFER = 0.9
VERTICAL_BUFFER = 0.8
class HexMap(Widget):
2017-07-27 18:03:18 -04:00
# TODO: Incorporate design into main application.
coords_display = ObjectProperty(None)
2017-07-27 08:28:46 -04:00
def __init__(self, **kwargs):
super(HexMap, self).__init__(**kwargs)
self.redraw_scene()
# It is crucial to redraw the scene whenever the scene or position changes.
self.bind(size=self.redraw_scene, pos=self.redraw_scene)
self.updates = 0
self.on_touch_down = self.update_coords
2017-07-27 08:28:46 -04:00
def redraw_scene(self, *args):
with self.canvas:
# Clear scene
Color(*get_color_from_hex("#000000ff"))
2017-07-27 08:28:46 -04:00
Rectangle(
pos=(0, 0),
size=(self.width, self.height)
)
2017-07-27 08:28:46 -04:00
# Draw a hexagon in a decent spot.
2017-07-27 08:50:19 -04:00
tiles_in_even_row = int(self.width / (HEX_SIZE * HORIZONTAL_BUFFER))
tiles_in_odd_row = int((self.width - HEX_SIZE / 2) / (HEX_SIZE * HORIZONTAL_BUFFER))
tiles_in_col = int(self.height / (HEX_SIZE * VERTICAL_BUFFER))
for y in range(tiles_in_col):
2017-07-27 17:28:51 -04:00
even_row = y % 2 == 0
hex_color = "#0000dd55" if even_row else "#dd000055"
tiles_in_row = tiles_in_even_row if even_row else tiles_in_odd_row
y_position = y * HEX_SIZE * VERTICAL_BUFFER
for x in range(tiles_in_row):
Color(*get_color_from_hex(hex_color))
2017-07-27 17:28:51 -04:00
x_position = x * HEX_SIZE * HORIZONTAL_BUFFER
if not even_row:
x_position += 0.5 * HEX_SIZE * HORIZONTAL_BUFFER
Ellipse(
pos=(x_position, y_position),
segments=6,
size=(HEX_SIZE, HEX_SIZE),
)
def update_coords(self, touch_event):
self.coords_display.text = "Touched at {}".format(touch_event.spos)
2017-07-27 18:03:18 -04:00
# TODO: Add in finding of hexes by what position found at touch event.
# TODO: May need a buffer around "untouchable" regions"
return True
class HexMapApp(App):
2017-07-27 17:21:35 -04:00
pass
if __name__ == '__main__':
HexMapApp().run()