Debian Codegraph @

src/tools/classic_tools/tool_experiment.py

raw symbols SCIP
1 # tool_experiment.py
2
3 import cairo, math, random
4 from gi.repository import Gdk
5 from .abstract_classic_tool import AbstractClassicTool
6 from .utilities_paths import utilities_smooth_path
7
8 class ToolExperiment(AbstractClassicTool):
9 __gtype_name__ = 'ToolExperiment'
10
11 def __init__(self, window, **kwargs):
12 super().__init__('experiment', _("Experiment"), 'applications-utilities-symbolic', window)
13
14 # In order to draw pressure-sensitive lines, the path is collected as
15 # an array whose elements are dicts (keys are 'x', 'y', 'p'). An actual
16 # cairo path will be built from this data when necessary.
17 self._manual_path = []
18
19 self._use_antialias = True
20 self._operators_dict = {
21 'CLEAR': cairo.Operator.CLEAR,
22 'SOURCE': cairo.Operator.SOURCE,
23 'OVER': cairo.Operator.OVER,
24 'IN': cairo.Operator.IN,
25 'OUT': cairo.Operator.OUT,
26 'ATOP': cairo.Operator.ATOP,
27 'DEST': cairo.Operator.DEST,
28 'DEST_OVER': cairo.Operator.DEST_OVER,
29 'DEST_IN': cairo.Operator.DEST_IN,
30 'DEST_OUT': cairo.Operator.DEST_OUT,
31 'DEST_ATOP': cairo.Operator.DEST_ATOP,
32 'XOR': cairo.Operator.XOR,
33 'ADD': cairo.Operator.ADD,
34 'SATURATE': cairo.Operator.SATURATE,
35 'MULTIPLY': cairo.Operator.MULTIPLY,
36 'SCREEN': cairo.Operator.SCREEN,
37 'OVERLAY': cairo.Operator.OVERLAY,
38 'DARKEN': cairo.Operator.DARKEN,
39 'LIGHTEN': cairo.Operator.LIGHTEN,
40 'COLOR_DODGE': cairo.Operator.COLOR_DODGE,
41 'COLOR_BURN': cairo.Operator.COLOR_BURN,
42 'HARD_LIGHT': cairo.Operator.HARD_LIGHT,
43 'SOFT_LIGHT': cairo.Operator.SOFT_LIGHT,
44 'DIFFERENCE': cairo.Operator.DIFFERENCE,
45 'EXCLUSION': cairo.Operator.EXCLUSION,
46 'HSL_HUE': cairo.Operator.HSL_HUE,
47 'HSL_SATURATION': cairo.Operator.HSL_SATURATION,
48 'HSL_COLOR': cairo.Operator.HSL_COLOR,
49 'HSL_LUMINOSITY': cairo.Operator.HSL_LUMINOSITY
50 }
51 self._operator_label = "OVER"
52 self.operator2 = cairo.Operator.OVER
53 self._selected_mode = 'feather'
54 self._feather_dir = 'up'
55
56 self.add_tool_action_enum('experiment_operator', self._operator_label)
57 self.add_tool_action_enum('experiment_mode', self._selected_mode)
58
59 def build_flowbox_child(self, flowbox):
60 super().build_flowbox_child(flowbox)
61 self._label_box.get_style_context().add_class('dim-label')
62
63 def get_editing_tips(self):
64 return [
65 self.label + " - " + self.get_options_label(),
66 "You're not supposed to use this tool (development only)."
67 ]
68
69 def get_options_label(self):
70 self._set_active_operator()
71 self._set_active_mode()
72 if self._selected_mode == 'simple':
73 return self._operator_label
74 else:
75 return self._selected_mode
76
77 ############################################################################
78
79 def _set_active_mode(self, *args):
80 state_as_string = self.get_option_value('experiment_mode')
81 self._selected_mode = state_as_string
82
83 def _set_active_operator(self, *args):
84 state_as_string = self.get_option_value('experiment_operator')
85 self.operator2 = self._operators_dict[state_as_string]
86 self._operator_label = state_as_string
87
88 ############################################################################
89
90 def on_press_on_area(self, event, surface, event_x, event_y):
91 self.get_options_label()
92 self.x_press = event_x
93 self.y_press = event_y
94 self.set_common_values(event.button, event_x, event_y)
95 self._manual_path = []
96 self._add_pressured_point(event_x, event_y, event)
97
98 def on_motion_on_area(self, event, surface, event_x, event_y, render=True):
99 self._add_pressured_point(event_x, event_y, event)
100 if render:
101 operation = self.build_operation()
102 self.do_tool_operation(operation)
103
104 def on_release_on_area(self, event, surface, event_x, event_y):
105 self._add_pressured_point(event_x, event_y, event)
106 operation = self.build_operation()
107 operation['is_preview'] = False
108 self.apply_operation(operation)
109
110 ############################################################################
111
112 def _add_pressured_point(self, event_x, event_y, event):
113 new_point = {
114 'x': event_x,
115 'y': event_y,
116 'p': self._get_pressure(event)
117 }
118 self._manual_path.append(new_point)
119
120 def _get_pressure(self, event):
121 device = event.get_source_device()
122 # print(device)
123 if device is None:
124 return None
125 source = device.get_source()
126 # print(source) # TODO ça peut être Gdk.InputSource.MOUSE, ou .TOUCHPAD,
127 # ou bien des trucs pertinents comme Gdk.InputSource.ERASER ou .PEN
128 # J'ignore s'il faut faire quelque chose de cette information ? Il y a
129 # ptêt des touchpads ou des touchscreens sensibles à la pression non ?
130
131 tool = event.get_device_tool()
132 # print(tool) # TODO ça indique qu'on a ici un appareil dédié au dessin
133 # (vaut `None` si c'est pas le cas). Autrement on peut avoir des valeurs
134 # comme Gdk.DeviceToolType.PEN, .ERASER, .BRUSH, .PENCIL, ou .AIRBRUSH,
135 # et aussi (même si jsuis pas sûr ce soit pertinent) .UNKNOWN, .MOUSE et
136 # .LENS (fuck ces bourgeois avec des tablettes haut de gamme au pire).
137 # On pourrait adapter le comportement (couleur/opérateur/aliasing/etc.)
138 # à cette information à l'avenir.
139
140 pressure = event.get_axis(Gdk.AxisUse.PRESSURE)
141 # print(pressure)
142 if pressure is None:
143 return None
144 return pressure
145
146 ############################################################################
147
148 def build_operation(self):
149 operation = {
150 'tool_id': self.id,
151 'rgba': self.main_color,
152 'operator': self.operator2,
153 'mode': self._selected_mode,
154 'line_width': self.tool_width,
155 'line_cap': cairo.LineCap.ROUND,
156 'line_join': cairo.LineJoin.ROUND,
157 'antialias': self._use_antialias,
158 'is_preview': True,
159 'path': self._manual_path
160 }
161 return operation
162
163 def do_tool_operation(self, operation):
164 if operation['path'] is None or len(operation['path']) < 1:
165 return
166 cairo_context = self.start_tool_operation(operation)
167 cairo_context.set_line_cap(operation['line_cap'])
168 cairo_context.set_line_join(operation['line_join'])
169 cairo_context.set_source_rgba(*operation['rgba'])
170
171 if operation['mode'] == 'pressure':
172 if operation['is_preview']: # Previewing helps performance & debug
173 operation['line_width'] = int(operation['line_width'] / 2)
174 return self.op_simple(operation, cairo_context)
175 self.op_pressure(operation, cairo_context)
176 elif operation['mode'] == 'smooth':
177 if operation['is_preview']: # Previewing helps performance & debug
178 return self.op_simple(operation, cairo_context)
179 self.op_smooth(operation, cairo_context)
180 elif operation['mode'] == 'macro-w':
181 self.op_macro_w(operation, cairo_context)
182 else:
183 self.op_simple(operation, cairo_context)
184
185 ############################################################################
186
187 def op_macro_w(self, operation, cairo_context):
188 """Trying to study whatever tf is the rendering issue #337"""
189 cairo_context.set_antialias(cairo.Antialias.DEFAULT)
190 cairo_context.set_operator(cairo.Operator.HSL_HUE)
191 cairo_context.set_line_width(operation['line_width'])
192 cairo_context.new_path()
193 cairo_context.line_to(50, 70)
194 cairo_context.line_to(70, 50)
195 cairo_context.line_to(90, 60)
196 cairo_context.line_to(110, 85)
197 cairo_context.line_to(130, 120)
198 cairo_context.line_to(150, 110)
199 cairo_context.line_to(170, 80)
200 cairo_context.line_to(190, 50)
201 cairo_context.line_to(210, 75)
202 cairo_context.line_to(230, 90)
203 cairo_context.line_to(250, 110)
204 cairo_context.line_to(270, 70)
205 cairo_context.line_to(290, 50)
206 cairo_context.line_to(310, 75)
207 cairo_context.line_to(330, 90)
208 cairo_context.stroke()
209
210 ############################################################################
211
212 def op_simple(self, operation, cairo_context):
213 cairo_context.set_operator(operation['operator'])
214 cairo_context.set_line_width(operation['line_width'])
215 cairo_context.new_path()
216 for pt in operation['path']:
217 cairo_context.line_to(pt['x'], pt['y'])
218 cairo_context.stroke()
219
220 def op_smooth(self, operation, cairo_context):
221 cairo_context.set_operator(cairo.Operator.OVER)
222 cairo_context.set_line_width(operation['line_width'])
223
224 # Build a cairo path from the raw data
225 cairo_context.new_path()
226 for pt in operation['path']:
227 cairo_context.line_to(pt['x'], pt['y'])
228 path = cairo_context.copy_path()
229
230 # Smooth it
231 cairo_context.new_path()
232 utilities_smooth_path(cairo_context, path)
233
234 # Draw it
235 cairo_context.stroke()
236
237 ############################################################################
238 ################################################################################
239