black
This commit is contained in:
1 parent
76c982536c
commit
5f25b04bc5
7 files changed
+86
-60
No files matched your search
@@ -10,7 +10,16 @@ from kivymd.app import MDApp
|
||||
|
||||
from katrain.core.game_node import GameNode
|
||||
from katrain.gui.kivyutils import BackgroundMixin, draw_circle, draw_text
|
||||
from katrain.gui.style import WHITE, STONE_COLORS, LIGHTGREY, BACKGROUND_COLOR, OUTLINE_COLORS, LIGHTER_BACKGROUND_COLOR, YELLOW, STONE_TEXT_COLORS
|
||||
from katrain.gui.style import (
|
||||
WHITE,
|
||||
STONE_COLORS,
|
||||
LIGHTGREY,
|
||||
BACKGROUND_COLOR,
|
||||
OUTLINE_COLORS,
|
||||
LIGHTER_BACKGROUND_COLOR,
|
||||
YELLOW,
|
||||
STONE_TEXT_COLORS,
|
||||
)
|
||||
|
||||
|
||||
class MoveTreeCanvas(Widget):
|
||||
@@ -19,9 +28,11 @@ class MoveTreeCanvas(Widget):
|
||||
move_tree = ObjectProperty(None)
|
||||
|
||||
def on_touch_up(self, touch):
|
||||
if touch.button=='left':
|
||||
node, (x,y) = min(self.move_xy_pos.items(),key=lambda n_xy: abs(n_xy[1][0]-touch.x)+abs(n_xy[1][1]-touch.y) )
|
||||
if max(abs(x-touch.x),abs(y-touch.y)) <= (self.move_size/2):
|
||||
if touch.button == "left":
|
||||
node, (x, y) = min(
|
||||
self.move_xy_pos.items(), key=lambda n_xy: abs(n_xy[1][0] - touch.x) + abs(n_xy[1][1] - touch.y)
|
||||
)
|
||||
if max(abs(x - touch.x), abs(y - touch.y)) <= (self.move_size / 2):
|
||||
katrain = MDApp.get_running_app().gui
|
||||
katrain.game.set_current_node(node)
|
||||
katrain.update_state()
|
||||
@@ -31,75 +42,81 @@ class MoveTreeCanvas(Widget):
|
||||
return
|
||||
spacing = 5
|
||||
moves_vert = 3
|
||||
self.move_size = (self.move_tree.height - (moves_vert+1) * spacing) / moves_vert
|
||||
self.move_size = (self.move_tree.height - (moves_vert + 1) * spacing) / moves_vert
|
||||
|
||||
root = current_node.root
|
||||
|
||||
move_pos = {root: (0,0)}
|
||||
stack = GameNode.order_children( root.children )[::-1]
|
||||
next_y_pos = defaultdict(int) # x pos -> max y pos
|
||||
move_pos = {root: (0, 0)}
|
||||
stack = GameNode.order_children(root.children)[::-1]
|
||||
next_y_pos = defaultdict(int) # x pos -> max y pos
|
||||
|
||||
while stack:
|
||||
move = stack.pop()
|
||||
x = move.depth
|
||||
y = max(next_y_pos[x], move_pos[move.parent][1])
|
||||
next_y_pos[x] = y+1
|
||||
next_y_pos[x-1] = max(next_y_pos[x],next_y_pos[x-1])
|
||||
move_pos[move] = (x,y)
|
||||
for c in GameNode.order_children( move.children )[::-1]: # stack, so push top child last to process first
|
||||
next_y_pos[x] = y + 1
|
||||
next_y_pos[x - 1] = max(next_y_pos[x], next_y_pos[x - 1])
|
||||
move_pos[move] = (x, y)
|
||||
for c in GameNode.order_children(move.children)[::-1]: # stack, so push top child last to process first
|
||||
stack.append(c)
|
||||
|
||||
def draw_stone(pos, player):
|
||||
draw_circle(pos,self.move_size/2-0.5,STONE_COLORS[player])
|
||||
draw_circle(pos, self.move_size / 2 - 0.5, STONE_COLORS[player])
|
||||
Color(*STONE_TEXT_COLORS[player])
|
||||
Line(circle=(*pos, self.move_size/2), width=1)
|
||||
Line(circle=(*pos, self.move_size / 2), width=1)
|
||||
|
||||
def coord_pos(coord):
|
||||
return (coord + 0.5) * (spacing + self.move_size) + spacing / 2
|
||||
|
||||
self.width = coord_pos(max(x+0.5 for x, y in move_pos.values()))
|
||||
self.height = coord_pos(max(y+0.5 for x, y in move_pos.values()))
|
||||
self.width = coord_pos(max(x + 0.5 for x, y in move_pos.values()))
|
||||
self.height = coord_pos(max(y + 0.5 for x, y in move_pos.values()))
|
||||
|
||||
def xy_pos(x,y):
|
||||
def xy_pos(x, y):
|
||||
return coord_pos(x), self.height - coord_pos(y)
|
||||
|
||||
self.move_xy_pos = {n: xy_pos(x,y) for n,(x,y) in move_pos.items() }
|
||||
self.move_xy_pos = {n: xy_pos(x, y) for n, (x, y) in move_pos.items()}
|
||||
|
||||
self.canvas.clear()
|
||||
with self.canvas:
|
||||
Color(*YELLOW)
|
||||
Rectangle(pos=[c-self.move_size/2-spacing/2 for c in self.move_xy_pos[current_node]],size=(self.move_size+spacing,self.move_size+spacing))
|
||||
Rectangle(
|
||||
pos=[c - self.move_size / 2 - spacing / 2 for c in self.move_xy_pos[current_node]],
|
||||
size=(self.move_size + spacing, self.move_size + spacing),
|
||||
)
|
||||
Color(*LIGHTGREY)
|
||||
for node, (x,y) in self.move_xy_pos.items():
|
||||
for node, (x, y) in self.move_xy_pos.items():
|
||||
for ci, c in enumerate(GameNode.order_children(node.children)):
|
||||
cx,cy = self.move_xy_pos[c]
|
||||
Line(points=[x,y,x,cy,cx,cy],width=1)
|
||||
cx, cy = self.move_xy_pos[c]
|
||||
Line(points=[x, y, x, cy, cx, cy], width=1)
|
||||
|
||||
for node, pos in self.move_xy_pos.items():
|
||||
draw_stone(pos, node.player)
|
||||
text = str(node.depth)
|
||||
Color(*STONE_COLORS['W' if node.player=='B' else 'B'])
|
||||
Color(*STONE_COLORS["W" if node.player == "B" else "B"])
|
||||
draw_text(pos=pos, text=text, font_size=self.move_size * 1.75 / (1 + 1 * len(text)), font_name="Roboto")
|
||||
|
||||
self.move_tree.scroll_to_pixel(*self.move_xy_pos[current_node])
|
||||
|
||||
|
||||
class MoveTree(ScrollView,BackgroundMixin):
|
||||
class MoveTree(ScrollView, BackgroundMixin):
|
||||
current_node = ObjectProperty(None)
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.redraw_tree_trigger = Clock.create_trigger(lambda _dt: self.move_tree_canvas.draw_move_tree(self.current_node))
|
||||
self.bind(current_node=self.redraw_tree_trigger,size=self.redraw_tree_trigger)
|
||||
self.redraw_tree_trigger = Clock.create_trigger(
|
||||
lambda _dt: self.move_tree_canvas.draw_move_tree(self.current_node)
|
||||
)
|
||||
self.bind(current_node=self.redraw_tree_trigger, size=self.redraw_tree_trigger)
|
||||
|
||||
def scroll_to_pixel(self, x, y):
|
||||
if not self._viewport:
|
||||
return
|
||||
vp = self._viewport
|
||||
if vp.width > self.width:
|
||||
sx = (x - self.width/2) / (vp.width - self.width)
|
||||
self.scroll_x =max(0, min(1, sx))
|
||||
sx = (x - self.width / 2) / (vp.width - self.width)
|
||||
self.scroll_x = max(0, min(1, sx))
|
||||
if vp.height > self.height:
|
||||
sy = (y - self.height/2) / (vp.height - self.height)
|
||||
sy = (y - self.height / 2) / (vp.height - self.height)
|
||||
self.scroll_y = max(0, min(1, sy))
|
||||
|
||||
|
||||
@@ -112,4 +129,5 @@ Builder.load_string(
|
||||
move_tree: root
|
||||
id: move_tree_canvas
|
||||
size_hint: None, None
|
||||
""")
|
||||
"""
|
||||
)
|
||||
Reference in new issue
Block a user