Skip to content

Commit

Permalink
Cleaned up the selection box code
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlegiantJGC committed Jun 12, 2020
1 parent 61ed067 commit b67424f
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self,
self._editable = True
self._active_box: Optional[RenderSelectionEditable] = None
self._active_box_index: Optional[int] = None
self._last_active_box_index: Optional[int] = None
self._last_active_box_index: Optional[int] = None # the last active box for use when deselecting or escaping a non-committed box
self._hover_box_index: Optional[int] = None
self._boxes: List[RenderSelectionHighlightable] = []
self._last_highlighted_box_index: Optional[int] = None
Expand Down Expand Up @@ -52,17 +52,24 @@ def _new_render_selection(self):
def _new_editable_render_selection(self):
return RenderSelectionEditable(self._context_identifier, self._texture_bounds, self._texture)

def _unload_active_box(self):
if self._active_box is not None:
self._active_box.unload()
self._active_box = None

def _create_active_box_from_cursor(self):
# create from the cursor position
self._unload_active_box()
self._active_box = self._new_editable_render_selection() # create a new render selection
self._last_active_box_index = self._active_box_index
self._active_box.point1, self._active_box.point2 = self._cursor_position, self._cursor_position
self._active_box_index = None
self._post_box_disable_inputs_event()

def _create_active_box_from_existing(self):
self._unload_active_box()
self._active_box = self._new_editable_render_selection() # create a new render selection
self._last_active_box_index = self._active_box_index
self._last_active_box_index = None
active_box = self._boxes[self._active_box_index]
self._active_box.point1, self._active_box.point2 = active_box.point1, active_box.point2
self._active_box.lock()
Expand Down Expand Up @@ -103,10 +110,11 @@ def escape(self):
self._create_active_box_from_existing()
elif self._last_active_box_index is not None:
self._active_box_index = self._last_active_box_index
self._last_active_box_index = None
self._create_active_box_from_existing()
else:
# if the box hasn't been committed yet
self._active_box = None
self._unload_active_box()
self._active_box_index = self._last_active_box_index = None
self._post_box_disable_inputs_event()

Expand All @@ -124,23 +132,22 @@ def deselect_active(self):
box = self._boxes.pop(self._active_box_index)
box.unload()
if self._boxes:
if self._last_active_box_index is not None and self._last_active_box_index > self._active_box_index:
self._last_active_box_index -= 1
if self._active_box_index >= 1:
self._active_box_index -= 1
if self._last_highlighted_box_index is not None and self._last_highlighted_box_index >= 1:
self._last_highlighted_box_index -= 1
self._create_active_box_from_existing()
else:
self._active_box = None
self._unload_active_box()
self._active_box_index = self._last_active_box_index = None
self._post_box_disable_inputs_event()
elif self._last_active_box_index is not None:
self._active_box_index = self._last_active_box_index
self._last_active_box_index = None
self._create_active_box_from_existing()
else:
# if the box hasn't been committed yet
self._active_box = None
self._unload_active_box()
self._active_box_index = self._last_active_box_index = None
self._post_box_disable_inputs_event()

Expand Down

0 comments on commit b67424f

Please sign in to comment.