| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from gi.repository import Gdk |
| 19 |
from .abstract_classic_tool import AbstractClassicTool |
| 20 |
|
| 21 |
from .brush_simple import BrushSimple |
| 22 |
from .brush_airbrush import BrushAirbrush |
| 23 |
from .brush_nib import BrushNib |
| 24 |
from .brush_hairy import BrushHairy |
| 25 |
|
| 26 |
class ToolBrush(AbstractClassicTool): |
| 27 |
__gtype_name__ = 'ToolBrush' |
| 28 |
|
| 29 |
def __init__(self, window, **kwargs): |
| 30 |
super().__init__('brush', _("Brush"), 'tool-brush-symbolic', window) |
| 31 |
self.use_operator = True |
| 32 |
self._used_pressure = False |
| 33 |
|
| 34 |
self._brushes_dict = { |
| 35 |
'simple': BrushSimple('simple', self), |
| 36 |
'airbrush': BrushAirbrush('airbrush', self), |
| 37 |
'calligraphic': BrushNib('calligraphic', self), |
| 38 |
'hairy': BrushHairy('hairy', self), |
| 39 |
} |
| 40 |
|
| 41 |
self._brush_type = 'simple' |
| 42 |
self._brush_dir = 'right' |
| 43 |
self.add_tool_action_enum('brush-type', self._brush_type) |
| 44 |
self.add_tool_action_enum('brush-dir', self._brush_dir) |
| 45 |
|
| 46 |
def get_options_label(self): |
| 47 |
return _("Brush options") |
| 48 |
|
| 49 |
def get_editing_tips(self): |
| 50 |
self._brush_type = self.get_option_value('brush-type') |
| 51 |
self._brush_dir = self.get_option_value('brush-dir') |
| 52 |
|
| 53 |
enable_direction = self._brush_type == 'calligraphic' |
| 54 |
self.set_action_sensitivity('brush-dir', enable_direction) |
| 55 |
|
| 56 |
active_brush = self._brushes_dict[self._brush_type] |
| 57 |
return active_brush._get_tips(self._used_pressure, self._brush_dir) |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
def on_press_on_area(self, event, surface, event_x, event_y): |
| 62 |
self.set_common_values(event.button, event_x, event_y) |
| 63 |
self._manual_path = [] |
| 64 |
self._add_pressured_point(event_x, event_y, event) |
| 65 |
self._used_pressure = self._manual_path[0]['p'] is not None |
| 66 |
|
| 67 |
def on_motion_on_area(self, event, surface, event_x, event_y, render=True): |
| 68 |
self._add_pressured_point(event_x, event_y, event) |
| 69 |
if render: |
| 70 |
operation = self.build_operation() |
| 71 |
self.do_tool_operation(operation) |
| 72 |
|
| 73 |
def on_release_on_area(self, event, surface, event_x, event_y): |
| 74 |
self._add_pressured_point(event_x, event_y, event) |
| 75 |
operation = self.build_operation() |
| 76 |
operation['is_preview'] = False |
| 77 |
self.apply_operation(operation) |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
def _add_pressured_point(self, event_x, event_y, event): |
| 82 |
new_point = { |
| 83 |
'x': event_x, |
| 84 |
'y': event_y, |
| 85 |
'p': self._get_pressure(event) |
| 86 |
} |
| 87 |
self._manual_path.append(new_point) |
| 88 |
|
| 89 |
def _get_pressure(self, event): |
| 90 |
device = event.get_source_device() |
| 91 |
|
| 92 |
if device is None: |
| 93 |
return None |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
tool = event.get_device_tool() |
| 98 |
aut |
| 99 |
|
| 100 |
|
| 101 |
et |
| 102 |
) |
| 103 |
r. |
| 104 |
|
| 105 |
pressure = event.get_axis(Gdk.AxisUse.PRESSURE) |
| 106 |
|
| 107 |
if pressure is None: |
| 108 |
return None |
| 109 |
return pressure |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
def build_operation(self): |
| 114 |
operation = { |
| 115 |
'tool_id': self.id, |
| 116 |
'brush_id': self._brush_type, |
| 117 |
'nib_dir': self._brush_dir, |
| 118 |
'rgba': self.main_color, |
| 119 |
'operator': self._operator, |
| 120 |
'line_width': self.tool_width, |
| 121 |
'antialias': self._use_antialias, |
| 122 |
'is_preview': True, |
| 123 |
'smooth': not self.get_image().is_zoomed_surface_sharp(), |
| 124 |
'path': self._manual_path |
| 125 |
} |
| 126 |
return operation |
| 127 |
|
| 128 |
def do_tool_operation(self, operation): |
| 129 |
if operation['path'] is None or len(operation['path']) < 1: |
| 130 |
return |
| 131 |
cairo_context = self.start_tool_operation(operation) |
| 132 |
|
| 133 |
active_brush = self._brushes_dict[operation['brush_id']] |
| 134 |
active_brush.do_brush_operation(cairo_context, operation) |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|