Debian Codegraph @

src/tools/classic_tools/tool_brush.py

raw symbols SCIP
1 # tool_brush.py
2 #
3 # Copyright 2018-2023 Romain F. T.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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 # print(device)
92 if device is None:
93 return None
94 # source = device.get_source()
95 # print(source) # J'ignore s'il faut faire quelque chose de cette info
96
97 tool = event.get_device_tool()
98 # print(tool) # ça indique qu'on a ici un appareil dédié au dessin (vaut
99 # `None` si c'est pas le cas). Autrement on peut avoir des valeurs comme
100 # Gdk.DeviceToolType.PEN, .ERASER, .BRUSH, .PENCIL, ou .AIRBRUSH, et
101 # aussi (même si jsuis pas sûr ce soit pertinent) .UNKNOWN, .MOUSE et
102 # .LENS, on pourrait adapter le comportement (couleur/opérateur/etc.)
103 # à cette information à l'avenir.
104
105 pressure = event.get_axis(Gdk.AxisUse.PRESSURE)
106 # print(pressure)
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