Skip to content

Commit

Permalink
Option to lock the line tool at multiples of 45° (#369, but not #46 yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
maoschanz committed Jun 15, 2021
1 parent 2737f4f commit 6987b89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/tools/classic_tools/tool_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ def __init__(self, window, **kwargs):
self._dashes_type = 'none'
self._arrow_type = 'none'
self._use_gradient = False
# Lock the tool to only draw orthogonal lines (multiples of 45°)
self._ortholock = False # Unrelated to Maïté's ortolan.

self.add_tool_action_enum('line_shape', 'round')
self.add_tool_action_enum('dashes-type', self._dashes_type)
self.add_tool_action_enum('arrow-type', self._arrow_type)
self.add_tool_action_boolean('use_gradient', self._use_gradient)
self.add_tool_action_boolean('pencil-outline', self._use_outline)
self.add_tool_action_boolean('line-ortholock', self._ortholock)
self._set_options_attributes() # Not optimal but more readable

def _set_active_shape(self):
Expand All @@ -53,6 +56,7 @@ def _set_options_attributes(self):
self._dashes_type = self.get_option_value('dashes-type')
self._arrow_type = self.get_option_value('arrow-type')
self._use_gradient = self.get_option_value('use_gradient')
self._ortholock = self.get_option_value('line-ortholock')
self._set_active_shape()

def get_edition_status(self):
Expand Down Expand Up @@ -96,6 +100,7 @@ def build_operation(self, event_x, event_y):
'arrow': self._arrow_type,
'gradient': self._use_gradient,
'outline': self._use_outline,
'ortholock': self._ortholock,
'x_release': event_x,
'y_release': event_y,
'x_press': self.x_press,
Expand All @@ -115,6 +120,23 @@ def do_tool_operation(self, operation):
x2 = operation['x_release']
y2 = operation['y_release']

if operation['ortholock']:
x1, y1 = int(x1), int(y1)
x2, y2 = int(x2), int(y2)
delta_x = abs(x1 - x2)
delta_y = abs(y1 - y2)
if delta_x > 2 * delta_y:
# Strictly horizontal line
y2 = y1
elif delta_x * 2 < delta_y:
# Strictly vertical line
x2 = x1
else:
# 45° line
delta45 = min(delta_x, delta_y)
x2 = x1 + delta45 if (x1 - x2 < 0) else x1 - delta45
y2 = y1 + delta45 if (y1 - y2 < 0) else y1 - delta45

self.set_dashes_and_cap(cairo_context, line_width, \
operation['dashes'], operation['line_cap'])

Expand Down
4 changes: 4 additions & 0 deletions src/tools/ui/tool-line.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<attribute name="label" translatable="yes">Outline</attribute>
<attribute name="action">win.pencil-outline</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Locked direction</attribute>
<attribute name="action">win.line-ortholock</attribute>
</item>
</section>
<section>
<attribute name="display-hint">horizontal-buttons</attribute>
Expand Down

0 comments on commit 6987b89

Please sign in to comment.