refactor out the last two decisions in the directional navigation so instead of being test code that can only move left, they now move in all four directions

This commit is contained in:
Chris Jones 2010-01-20 13:04:14 +00:00
parent ddce3a862d
commit ee72ae478f
2 changed files with 26 additions and 4 deletions

View File

@ -124,7 +124,6 @@ class Terminator(Borg):
if next < 0: if next < 0:
next = length - 1 next = length - 1
elif direction in ['left', 'right', 'up', 'down']: elif direction in ['left', 'right', 'up', 'down']:
print direction
window = get_top_window(terminal) window = get_top_window(terminal)
layout = window.get_visible_terminals() layout = window.get_visible_terminals()
@ -147,8 +146,7 @@ class Terminator(Borg):
offsets = {} offsets = {}
for term in possibles: for term in possibles:
rect = layout[term] rect = layout[term]
offset = edge - (rect.x + rect.width) offsets[term] = util.get_nav_offset(edge, rect, direction)
offsets[term] = offset
keys = offsets.values() keys = offsets.values()
keys.sort() keys.sort()
winners = [k for k, v in offsets.iteritems() if v == keys[0]] winners = [k for k, v in offsets.iteritems() if v == keys[0]]
@ -160,7 +158,7 @@ class Terminator(Borg):
cursor_y = cursor_y + allocation.y cursor_y = cursor_y + allocation.y
for term in winners: for term in winners:
rect = layout[term] rect = layout[term]
if cursor_y >= rect.y and cursor_y <= (rect.y + rect.height): if util.get_nav_tiebreak(direction, cursor_x, cursor_y, rect):
next = self.terminals.index(term) next = self.terminals.index(term)
break; break;
else: else:

View File

@ -202,3 +202,27 @@ def get_nav_possible(edge, allocation, direction):
else: else:
raise ValueError('Unknown direction: %s' % direction) raise ValueError('Unknown direction: %s' % direction)
def get_nav_offset(edge, allocation, direction):
"""Work out how far edge is from a particular point on the allocation
rectangle, in the given direction"""
if direction == 'left':
return(edge - (allocation.x + allocation.width))
elif direction == 'right':
return(edge + allocation.x)
elif direction == 'up':
return(edge - (allocation.y - allocation.height))
elif direction == 'down':
return(edge + allocation.y)
else:
raise ValueError('Unknown direction: %s' % direction)
def get_nav_tiebreak(direction, cursor_x, cursor_y, rect):
"""We have multiple candidate terminals. Pick the closest by cursor
position"""
if direction in ['left', 'right']:
return(cursor_y >= rect.y and cursor_y <= (rect.y + rect.height))
elif direction in ['up', 'down']:
return(cursor_x >= rect.x and cursor_x <= (rect.x + rect.width))
else:
raise ValueError('Unknown direction: %s' % direction)