gaming-pads/skilltree.py

97 lines
2.8 KiB
Python

import pyray as RL
from pyray import (Rectangle as Rect, Vector2 as Vec2)
import math
import pdb
import random
from typing import Optional, Tuple, List
from dataclasses import dataclass, field
screen_width = 1280
screen_height = 1024
node_width = 200
node_height = 60
node_font_height = 24
default_font = RL.get_font_default()
@dataclass
class Node:
text: str
rect: Rect
@dataclass
class World:
nodes: List[Node]
dragging_node: Optional[Tuple[Node, Tuple[float, float]]] = None
global w
def init():
global w
nodes = [Node("Node1",Rect(50,50,node_width,node_height)),Node("Node2",Rect(150,150,node_width,node_height))]
w = World(nodes)
def player_input():
if RL.is_mouse_button_pressed(0):
w.dragging_node = None
for node in w.nodes:
mouse_pos = RL.get_mouse_position()
if RL.check_collision_point_rec(mouse_pos, node.rect):
w.dragging_node = node, (node.rect.x - mouse_pos.x, node.rect.y - mouse_pos.y)
break
if RL.is_mouse_button_released(0):
w.dragging_node = None
if w.dragging_node:
node, offset_x, offset_y = w.dragging_node[0], *w.dragging_node[1]
mouse_pos = RL.get_mouse_position()
node.rect.x, node.rect.y = mouse_pos.x + offset_x, mouse_pos.y + offset_y
def update():
pass
def draw():
RL.begin_drawing()
RL.clear_background(RL.RAYWHITE)
for node in w.nodes:
rect_offset = Rect(node.rect.x + 2, node.rect.y + 2, node.rect.width, node.rect.height)
RL.draw_rectangle_lines_ex(rect_offset, 2, RL.BLACK)
RL.draw_rectangle_rec(node.rect, RL.WHITE)
RL.draw_rectangle_lines_ex(node.rect, 1, RL.BLACK)
text_width = RL.measure_text(node.text, node_font_height)
pos = Vec2(
node.rect.x + node.rect.width / 2 - text_width / 2,
node.rect.y + node.rect.height / 2 - node_font_height / 2,
)
RL.draw_text_ex(default_font, node.text, pos, node_font_height, 1, RL.BLACK)
node1 = w.nodes[0]
node2 = w.nodes[1]
start_pos = Vec2(node1.rect.x + node1.rect.width // 2, node1.rect.y + node1.rect.height)
end_pos = Vec2(node2.rect.x + node2.rect.width // 2, node2.rect.y)
# RL.draw_line_bezier(start_pos, end_pos, 3, RL.BLUE)
# c1 = start_pos
# c1.x += 50
# c1.y += 50
# c2 = end_pos
# c2.y -= 50
mid_point = RL.vector2_subtract(end_pos, start_pos)
mid_control = RL.vector2_add(mid_point, Vec2(50,50))
points = [start_pos, start_pos, mid_point, mid_control, end_pos, end_pos]
# points = [start_pos, c1, c2, mid_point, end_pos]
RL.draw_spline_bezier_cubic(points, 6, 3, RL.BLUE)
RL.end_drawing()
RL.init_window(screen_width, screen_height, "Skilltree");
RL.set_target_fps(60)
init()
while not RL.window_should_close():
player_input()
update()
draw()