def on_mouse(event, x, y, flags, param):
layout, downpos, ismove = param
# record downpos
if event == cv2.EVENT_LBUTTONDOWN:
print 'click at', x*2, y*2 # picture is half-sized.
param[1] = (x, y)
param[2] = False
return
# check if is moving
if event == cv2.EVENT_MOUSEMOVE:
if ismove: return
if downpos is None:
param[2] = False
return
_x, _y = downpos
if (_x-x)**2 + (_y-y)**2 > 64:
param[2] = True
return
if event != cv2.EVENT_LBUTTONUP:
return
# update layout.highlight
b = layout.tree.bounds
l, t = b.left, b.top
w, h = b.right - b.left, b.bottom - b.top
highlight = np.zeros((h, w, 3), np.uint8)
if downpos and ismove: # drag
node = layout.find_scrollable_node(x*2+l, y*2+t)
print 'scroll to', x*2, y*2
if node:
b = node.bounds
print 'scrollable node', b, node.index, node.className,
print 'resource_id:', node.resourceId,
print 'text:', node.text.encode(encoding, 'ignore'),
print 'desc:', node.description.encode(encoding, 'ignore')
cv2.rectangle(highlight, (b.left-l, b.top-t), (b.right-l, b.bottom-t), (0,255,255), 4)
else:
node = layout.find_clickable_node(x*2+l, y*2+t)
if node:
b = node.bounds
print 'clickable node', b, node.index, node.className,
print 'resource_id:', node.resourceId,
print 'text:', node.text.encode(encoding, 'ignore'),
print 'desc:', node.description.encode(encoding, 'ignore')
print device(className=node.className, index=node.index).info
cv2.rectangle(highlight, (b.left-l, b.top-t), (b.right-l, b.bottom-t), (0,0,255), 4)
cond, order = layout.find_selector(node)
if cond:
print 'selector', cond, order
subnode = layout._filter_nodes(cond)[order or 0]
b = subnode.bounds
cv2.rectangle(highlight, (b.left-l, b.top-t), (b.right-l, b.bottom-t), (0,180,255), 4)
param[0].highlight = highlight
param[1], param[2] = None, False
评论列表
文章目录