【发布时间】:2014-08-14 21:06:32
【问题描述】:
我还在为这些 Kivy 的东西热身,希望你不要介意我的另一个问题!
我正在尝试了解我的一些小部件的预定事件。为了上下文,我希望能够将节点移动到边上,并让边“捕捉”到节点。
这是我的 .kv 文件:
<GraphInterface>:
node: graph_node
edge: graph_edge
GraphNode:
id: graph_node
center: self.parent.center
GraphEdge:
id: graph_edge
center: 200,200
<GraphNode>:
size: 50, 50
canvas:
Color:
rgba: (root.r,1,1,1)
Ellipse:
pos: self.pos
size: self.size
<GraphEdge>:
size: self.size
canvas:
Color:
rgba: (root.r,1,1,1)
Line:
width: 2.0
close: True
我在节点和边之间有一个碰撞检测事件,我正在我的程序中动态创建节点和边。这是相关的python代码:
class GraphInterface(Widget):
node = ObjectProperty(None)
edge = ObjectProperty(None)
def update(self, dt):
# detect node collision
self.edge.snap_to_node(self.node)
return True
class GraphEdge(Widget):
r = NumericProperty(1.0)
def __init__(self, **kwargs):
super(GraphEdge, self).__init__(**kwargs)
with self.canvas:
self.line = Line(points=[100, 200, 200, 200], width = 2.0, close = True)
def snap_to_node(self, node):
if self.collide_widget(node):
print "collision detected"
del self.line.points[-2:]
self.line.points+=node.center
self.size = [math.sqrt(((self.line.points[0]-self.line.points[2])**2 + (self.line.points[1]-self.line.points[3])**2))]*2
self.center = ((self.line.points[0]+self.line.points[2])/2,(self.line.points[1]+self.line.points[3])/2)
pass
class GraphApp(App):
def build(self):
node = GraphNode()
game = GraphInterface()
createNodeButton = Button(text = 'CreateNode', pos=(100,0))
createEdgeButton = Button(text = 'CreateEdge')
game.add_widget(createNodeButton)
game.add_widget(createEdgeButton)
#scatter = Scatter(do_rotation=False, do_scale=False)
#scatter.add_widget(node)
def createNode(instance):
newNode = GraphNode()
game.add_widget(newNode)
#scatter.add_widget(newNode)
print "Node Created"
def createEdge(instance):
newEdge = GraphEdge()
game.add_widget(newEdge)
#scatter.add_widget(newEdge)
print "Edge Created"
createNodeButton.bind(on_press=createNode)
createEdgeButton.bind(on_press=createEdge)
Clock.schedule_interval(game.update, 1.0/60.0)
return game
好的,有很多东西要读(抱歉)!
基本上,我的碰撞只检测到我在 .kv 文件中创建的初始边和节点。新创建的边和节点不能通过碰撞机制进行交互。
如果人们愿意,我可以提供更多代码,但我认为这应该是所有相关部分。谢谢!
编辑:
新方法:
def on_touch_move(self, touch):
if touch.grab_current is self:
self.pos=[touch.x-25,touch.y-25]
for widget in self.parent.children:
if isinstance(widget, GraphEdge) and widget.collide_widget(self):
print "collision detected"
widget.snap_to_node(self)
return True
return super(GraphNode, self).on_touch_move(touch)
使用此代码,我仍然可以通过快速移动节点来断开连接。
EDIT2:
我可能给出的答案有点过早。我只能与我生成的第一条边进行交互。而且我还有一个奇怪的错误,第一条边和第一个节点似乎具有相同的颜色属性。虽然也许这应该在它自己的问题中提出。
【问题讨论】:
标签: python python-2.7 drawing kivy