| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import cairo, math |
| 19 |
from .abstract_classic_tool import AbstractClassicTool |
| 20 |
from .utilities_paths import utilities_smooth_path |
| 21 |
|
| 22 |
class ToolShape(AbstractClassicTool): |
| 23 |
__gtype_name__ = 'ToolShape' |
| 24 |
|
| 25 |
def __init__(self, window, **kwargs): |
| 26 |
super().__init__('shape', _("Shape"), 'tool-freeshape-symbolic', window) |
| 27 |
self.use_operator = True |
| 28 |
|
| 29 |
self._reset_temp_points() |
| 30 |
|
| 31 |
self.add_tool_action_simple('shape_close', self._force_close_shape) |
| 32 |
self.set_action_sensitivity('shape_close', False) |
| 33 |
|
| 34 |
self.load_tool_action_enum('shape_type', 'last-active-shape') |
| 35 |
self._set_active_shape() |
| 36 |
|
| 37 |
self._filling_id = self.load_tool_action_enum('shape_filling', \ |
| 38 |
'last-shape-filling') |
| 39 |
|
| 40 |
self._outline_id = 'solid' |
| 41 |
self._outline_label = _("Solid outline") |
| 42 |
self.add_tool_action_enum('shape_outline', self._outline_id) |
| 43 |
|
| 44 |
def _reset_temp_points(self): |
| 45 |
self._path = None |
| 46 |
self.x_press = None |
| 47 |
self.y_press = None |
| 48 |
self.initial_x = None |
| 49 |
self.initial_y = None |
| 50 |
|
| 51 |
def get_tooltip(self, event_x, event_y, motion_behavior): |
| 52 |
if motion_behavior != 1: |
| 53 |
return None |
| 54 |
if self._shape_id in ['polygon', 'freeshpae']: |
| 55 |
return None |
| 56 |
|
| 57 |
delta_x = abs(self.x_press - event_x) |
| 58 |
delta_y = abs(self.y_press - event_y) |
| 59 |
|
| 60 |
if self._shape_id == 'circle': |
| 61 |
length = round(math.sqrt(delta_x * delta_x + delta_y * delta_y), 2) |
| 62 |
return _("Radius: %spx") % length |
| 63 |
|
| 64 |
line1 = _("Width: %spx") % str(delta_x) |
| 65 |
line2 = _("Height: %spx") % str(delta_y) |
| 66 |
return line1 + "\n" + line2 |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
def _set_filling_style(self): |
| 71 |
self._filling_id = self.get_option_value('shape_filling') |
| 72 |
|
| 73 |
def _set_outline_style(self): |
| 74 |
self._outline_id = self.get_option_value('shape_outline') |
| 75 |
|
| 76 |
def _set_active_shape(self, *args): |
| 77 |
self._shape_id = self.get_option_value('shape_type') |
| 78 |
if self._shape_id == 'rectangle' or self._shape_id == 'polygon': |
| 79 |
self._join_id = cairo.LineJoin.MITER |
| 80 |
|
| 81 |
else: |
| 82 |
self._join_id = cairo.LineJoin.ROUND |
| 83 |
|
| 84 |
def get_options_label(self): |
| 85 |
return _("Shape options") |
| 86 |
|
| 87 |
def get_editing_tips(self): |
| 88 |
self._set_active_shape() |
| 89 |
shape_name = { |
| 90 |
'rectangle': _("Rectangle"), |
| 91 |
'roundedrect': _("Rounded rectangle"), |
| 92 |
'oval': _("Oval"), |
| 93 |
'circle': _("Circle"), |
| 94 |
'polygon': _("Polygon"), |
| 95 |
'freeshape': _("Free shape"), |
| 96 |
}[self._shape_id] |
| 97 |
|
| 98 |
label_options = shape_name |
| 99 |
self._set_filling_style() |
| 100 |
self._set_outline_style() |
| 101 |
if self._outline_id != 'solid': |
| 102 |
label_options += " - " + { |
| 103 |
'solid': _("Solid outline"), |
| 104 |
'dashed': _("Dashed outline"), |
| 105 |
'none': _("No outline"), |
| 106 |
}[self._outline_id] |
| 107 |
if self._filling_id != 'empty': |
| 108 |
label_options += " - " + { |
| 109 |
'empty': _("Empty shape"), |
| 110 |
|
| 111 |
'filled': _("Filled with main color"), |
| 112 |
|
| 113 |
'secondary': _("Filled with secondary color"), |
| 114 |
'h-gradient': _("Horizontal gradient"), |
| 115 |
'v-gradient': _("Vertical gradient"), |
| 116 |
'r-gradient': _("Radial gradient"), |
| 117 |
}[self._filling_id] |
| 118 |
|
| 119 |
if (self._shape_id == 'polygon' or self._shape_id == 'freeshape') and \ |
| 120 |
self._path is not None: |
| 121 |
label_instruction = shape_name + " - " + \ |
| 122 |
_("Click on the shape's first point to close it.") |
| 123 |
else: |
| 124 |
label_instruction = None |
| 125 |
self.set_action_sensitivity('shape_close', False) |
| 126 |
|
| 127 |
if self.get_image().get_mouse_is_pressed(): |
| 128 |
label_modifiers = None |
| 129 |
else: |
| 130 |
label_modifiers = _("Press <Alt>, <Shift>, or both, to quickly " + \ |
| 131 |
"change the 'filling' option") |
| 132 |
|
| 133 |
full_list = [label_options, label_instruction, label_modifiers] |
| 134 |
return list(filter(None, full_list)) |
| 135 |
|
| 136 |
def give_back_control(self, preserve_selection): |
| 137 |
self.restore_pixbuf() |
| 138 |
self._reset_temp_points() |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
def on_press_on_area(self, event, surface, event_x, event_y): |
| 143 |
self.last_mouse_btn = event.button |
| 144 |
self.set_common_values(self.last_mouse_btn, event_x, event_y) |
| 145 |
|
| 146 |
self.update_modifier_state(event.state) |
| 147 |
if 'SHIFT' in self._modifier_keys and 'ALT' in self._modifier_keys: |
| 148 |
self._filling_id = 'secondary' |
| 149 |
elif 'SHIFT' in self._modifier_keys: |
| 150 |
self._filling_id = 'empty' |
| 151 |
if self._outline_id == 'none': |
| 152 |
self._outline_id = 'solid' |
| 153 |
elif 'ALT' in self._modifier_keys: |
| 154 |
self._filling_id = 'filled' |
| 155 |
self._outline_id = 'none' |
| 156 |
|
| 157 |
def on_motion_on_area(self, event, surface, event_x, event_y, render=True): |
| 158 |
if self._shape_id == 'freeshape': |
| 159 |
operation = self._add_point(event_x, event_y, True) |
| 160 |
elif self._shape_id == 'polygon': |
| 161 |
operation = self._add_point(event_x, event_y, False) |
| 162 |
else: |
| 163 |
if self._shape_id == 'rectangle': |
| 164 |
self._draw_rectangle(event_x, event_y) |
| 165 |
elif self._shape_id == 'roundedrect': |
| 166 |
self._draw_roundedrect(event_x, event_y) |
| 167 |
elif self._shape_id == 'oval': |
| 168 |
self._draw_ellipse(event_x, event_y) |
| 169 |
elif self._shape_id == 'circle': |
| 170 |
self._draw_circle(event_x, event_y) |
| 171 |
operation = self.build_operation(self._path) |
| 172 |
if render: |
| 173 |
self.do_tool_operation(operation) |
| 174 |
|
| 175 |
def on_release_on_area(self, event, surface, event_x, event_y): |
| 176 |
if self._shape_id == 'freeshape' or self._shape_id == 'polygon': |
| 177 |
operation = self._add_point(event_x, event_y, True) |
| 178 |
self.set_action_sensitivity('shape_close', not operation['closed']) |
| 179 |
if operation['closed']: |
| 180 |
self.apply_operation(operation) |
| 181 |
self._reset_temp_points() |
| 182 |
else: |
| 183 |
self.do_tool_operation(operation) |
| 184 |
self.non_destructive_show_modif() |
| 185 |
else: |
| 186 |
if self._shape_id == 'rectangle': |
| 187 |
self._draw_rectangle(event_x, event_y) |
| 188 |
elif self._shape_id == 'roundedrect': |
| 189 |
self._draw_roundedrect(event_x, event_y) |
| 190 |
elif self._shape_id == 'oval': |
| 191 |
self._draw_ellipse(event_x, event_y) |
| 192 |
elif self._shape_id == 'circle': |
| 193 |
self._draw_circle(event_x, event_y) |
| 194 |
operation = self.build_operation(self._path) |
| 195 |
self.apply_operation(operation) |
| 196 |
self._reset_temp_points() |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
def _force_close_shape(self, *args): |
| 201 |
self.set_common_values(self.last_mouse_btn, self.x_press, self.y_press) |
| 202 |
self.on_release_on_area(None, None, self.initial_x, self.initial_y) |
| 203 |
|
| 204 |
def _add_point(self, event_x, event_y, memorize): |
| 205 |
"""Add a point to a shape (used by both freeshape and polygon).""" |
| 206 |
cairo_context = self.get_context() |
| 207 |
if self.initial_x is None: |
| 208 |
|
| 209 |
(self.initial_x, self.initial_y) = (self.x_press, self.y_press) |
| 210 |
cairo_context.move_to(self.x_press, self.y_press) |
| 211 |
self._path = cairo_context.copy_path() |
| 212 |
else: |
| 213 |
cairo_context.append_path(self._path) |
| 214 |
should_close = self._should_close_shape(event_x, event_y) |
| 215 |
if not should_close: |
| 216 |
|
| 217 |
cairo_context.line_to(event_x, event_y) |
| 218 |
if memorize: |
| 219 |
|
| 220 |
self._path = cairo_context.copy_path() |
| 221 |
operation = self.build_operation(cairo_context.copy_path()) |
| 222 |
operation['closed'] = should_close |
| 223 |
return operation |
| 224 |
|
| 225 |
def _should_close_shape(self, event_x, event_y): |
| 226 |
if self.initial_x is None or self.initial_y is None: |
| 227 |
return False |
| 228 |
delta_x = max(event_x, self.initial_x) - min(event_x, self.initial_x) |
| 229 |
delta_y = max(event_y, self.initial_y) - min(event_y, self.initial_y) |
| 230 |
closing_limit = max(2, self.tool_width) |
| 231 |
return (delta_x < closing_limit and delta_y < closing_limit) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
def _draw_rectangle(self, event_x, event_y): |
| 236 |
cairo_context = self.get_context() |
| 237 |
cairo_context.move_to(self.x_press, self.y_press) |
| 238 |
cairo_context.line_to(self.x_press, event_y) |
| 239 |
cairo_context.line_to(event_x, event_y) |
| 240 |
cairo_context.line_to(event_x, self.y_press) |
| 241 |
cairo_context.close_path() |
| 242 |
self._path = cairo_context.copy_path() |
| 243 |
|
| 244 |
def _draw_roundedrect(self, event_x, event_y): |
| 245 |
cairo_context = self.get_context() |
| 246 |
a = min(self.x_press, event_x) |
| 247 |
b = max(self.x_press, event_x) |
| 248 |
c = min(self.y_press, event_y) |
| 249 |
d = max(self.y_press, event_y) |
| 250 |
radius = min(d - c, b - a) / 6 |
| 251 |
pi2 = math.pi / 2 |
| 252 |
cairo_context.arc(a + radius, c + radius, radius, 2 * pi2, 3 * pi2) |
| 253 |
cairo_context.arc(b - radius, c + radius, radius, 3 * pi2, 4 * pi2) |
| 254 |
cairo_context.arc(b - radius, d - radius, radius, 0 * pi2, 1 * pi2) |
| 255 |
cairo_context.arc(a + radius, d - radius, radius, 1 * pi2, 2 * pi2) |
| 256 |
cairo_context.close_path() |
| 257 |
self._path = cairo_context.copy_path() |
| 258 |
|
| 259 |
def _draw_ellipse(self, event_x, event_y): |
| 260 |
cairo_context = self.get_context() |
| 261 |
saved_matrix = cairo_context.get_matrix() |
| 262 |
|
| 263 |
halfw = int((self.x_press - event_x) / 2) |
| 264 |
halfh = int((self.y_press - event_y) / 2) |
| 265 |
|
| 266 |
if halfw == 0: |
| 267 |
halfw = 1 |
| 268 |
if halfh == 0: |
| 269 |
halfh = 1 |
| 270 |
cairo_context.translate(event_x + halfw, event_y + halfh) |
| 271 |
cairo_context.scale(halfw, halfh) |
| 272 |
cairo_context.arc(0, 0, 1, 0, 2 * math.pi) |
| 273 |
|
| 274 |
cairo_context.set_matrix(saved_matrix) |
| 275 |
cairo_context.close_path() |
| 276 |
self._path = cairo_context.copy_path() |
| 277 |
|
| 278 |
def _draw_circle(self, event_x, event_y): |
| 279 |
cairo_context = self.get_context() |
| 280 |
delta_x2 = (self.x_press - event_x) * (self.x_press - event_x) |
| 281 |
delta_y2 = (self.y_press - event_y) * (self.y_press - event_y) |
| 282 |
rayon = math.sqrt(delta_x2 + delta_y2) |
| 283 |
cairo_context.arc(self.x_press, self.y_press, rayon, 0.0, 2 * math.pi) |
| 284 |
self._path = cairo_context.copy_path() |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
def build_operation(self, cairo_path): |
| 289 |
pixelart_mode = self.get_image().is_zoomed_surface_sharp() |
| 290 |
operation = { |
| 291 |
'tool_id': self.id, |
| 292 |
'rgba_main': self.main_color, |
| 293 |
'rgba_secd': self.secondary_color, |
| 294 |
'antialias': self._use_antialias, |
| 295 |
'operator': self._operator, |
| 296 |
'line_join': self._join_id, |
| 297 |
'line_width': self.tool_width, |
| 298 |
'filling': self._filling_id, |
| 299 |
'outline': self._outline_id, |
| 300 |
'smooth': (self._shape_id == 'freeshape') and not pixelart_mode, |
| 301 |
'closed': True, |
| 302 |
'path': cairo_path |
| 303 |
} |
| 304 |
return operation |
| 305 |
|
| 306 |
def get_pattern_h(self, xmin, xmax): |
| 307 |
pattern = cairo.LinearGradient(xmin, 0.0, xmax, 0.0) |
| 308 |
return pattern |
| 309 |
|
| 310 |
def get_pattern_v(self, ymin, ymax): |
| 311 |
pattern = cairo.LinearGradient(0.0, ymin, 0.0, ymax) |
| 312 |
return pattern |
| 313 |
|
| 314 |
def get_pattern_r(self, center_x, center_y, rad): |
| 315 |
pattern = cairo.RadialGradient(center_x, center_y, 0.1 * rad, \ |
| 316 |
center_x, center_y, 0.9 * rad) |
| 317 |
|
| 318 |
return pattern |
| 319 |
|
| 320 |
def _fill_pattern(self, cairo_context, pattern, c1, c2): |
| 321 |
"""Fill the shape defined in the context with a gradient from c1 to c2 |
| 322 |
according to the given pattern. The colors are normalized arrays.""" |
| 323 |
pattern.add_color_stop_rgba(0.1, *c1) |
| 324 |
pattern.add_color_stop_rgba(0.9, *c2) |
| 325 |
cairo_context.set_source(pattern) |
| 326 |
cairo_context.fill_preserve() |
| 327 |
|
| 328 |
def _fill_plain(self, cairo_context, color): |
| 329 |
"""Fill the shape defined in the context with the color c.""" |
| 330 |
cairo_context.set_source_rgba(*color) |
| 331 |
cairo_context.fill_preserve() |
| 332 |
|
| 333 |
def do_tool_operation(self, operation): |
| 334 |
cairo_context = self.start_tool_operation(operation) |
| 335 |
|
| 336 |
line_width = operation['line_width'] |
| 337 |
cairo_context.set_line_width(line_width) |
| 338 |
cairo_context.set_line_join(operation['line_join']) |
| 339 |
|
| 340 |
if operation['smooth']: |
| 341 |
utilities_smooth_path(cairo_context, operation['path']) |
| 342 |
else: |
| 343 |
cairo_context.append_path(operation['path']) |
| 344 |
if operation['closed']: |
| 345 |
cairo_context.close_path() |
| 346 |
|
| 347 |
cairo_context.set_operator(operation['operator']) |
| 348 |
color_main = operation['rgba_main'] |
| 349 |
color_secd = operation['rgba_secd'] |
| 350 |
|
| 351 |
filling = operation['filling'] |
| 352 |
if filling == 'secondary': |
| 353 |
self._fill_plain(cairo_context, color_secd) |
| 354 |
elif filling == 'filled': |
| 355 |
self._fill_plain(cairo_context, color_main) |
| 356 |
elif filling == 'h-gradient': |
| 357 |
x1, y1, x2, y2 = cairo_context.path_extents() |
| 358 |
pattern = self.get_pattern_h(x1, x2) |
| 359 |
self._fill_pattern(cairo_context, pattern, color_main, color_secd) |
| 360 |
elif filling == 'v-gradient': |
| 361 |
x1, y1, x2, y2 = cairo_context.path_extents() |
| 362 |
pattern = self.get_pattern_v(y1, y2) |
| 363 |
self._fill_pattern(cairo_context, pattern, color_main, color_secd) |
| 364 |
elif filling == 'r-gradient': |
| 365 |
x1, y1, x2, y2 = cairo_context.path_extents() |
| 366 |
ddx = abs(x1 - x2) / 2 |
| 367 |
ddy = abs(y1 - y2) / 2 |
| 368 |
center_x = min(x1, x2) + ddx |
| 369 |
center_y = min(y1, y2) + ddy |
| 370 |
rad = max(ddx, ddy) |
| 371 |
pattern = self.get_pattern_r(center_x, center_y, rad) |
| 372 |
self._fill_pattern(cairo_context, pattern, color_main, color_secd) |
| 373 |
else: |
| 374 |
pass |
| 375 |
|
| 376 |
outline = operation['outline'] |
| 377 |
cairo_context.set_source_rgba(*color_main) |
| 378 |
if outline == 'dashed': |
| 379 |
cairo_context.set_dash([2 * line_width, 2 * line_width]) |
| 380 |
if outline != 'none': |
| 381 |
cairo_context.stroke() |
| 382 |
|
| 383 |
|
| 384 |
cairo_context.new_path() |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|