Blender V5.3
grease_pencil_primitive.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <fmt/format.h>
11
12#include <cstring>
13
14#include "BKE_attribute.hh"
15#include "BKE_brush.hh"
16#include "BKE_colortools.hh"
17#include "BKE_context.hh"
18#include "BKE_curves.hh"
19#include "BKE_grease_pencil.hh"
21#include "BKE_material.hh"
22#include "BKE_paint.hh"
23#include "BKE_screen.hh"
24
25#include "WM_api.hh"
26#include "WM_types.hh"
27
28#include "RNA_access.hh"
29#include "RNA_define.hh"
30#include "RNA_enum_types.hh"
31
32#include "DEG_depsgraph.hh"
33
34#include "DNA_brush_types.h"
35#include "DNA_material_types.h"
36
37#include "ED_grease_pencil.hh"
38#include "ED_screen.hh"
39#include "ED_space_api.hh"
40#include "ED_view3d.hh"
41
42#include "BLI_array_utils.hh"
43#include "BLI_math_matrix.hh"
44#include "BLI_rand.hh"
45#include "BLI_vector.hh"
46
47#include "BLT_translation.hh"
48
49#include "GPU_immediate.hh"
50#include "GPU_state.hh"
51
52#include "UI_resources.hh"
53
54namespace blender {
55
56namespace ed::greasepencil {
57
58enum class PrimitiveType : int8_t {
59 Line = 0,
61 Arc = 2,
62 Curve = 3,
63 Box = 4,
64 Circle = 5,
65};
66
67enum class OperatorMode : int8_t {
68 Idle = 0,
70 /* Set the active control point to the mouse. */
71 Grab = 2,
72 /* Move the active control point. */
73 Drag = 3,
74 /* Move all control points. */
76 /* Rotate all control points. */
78 /* Scale all control points. */
80 /* Change brush radius. */
82 /* Change brush opacity. */
84};
85
87 /* The points that are at the end of segments. */
89 /* The points inside of the segments not including the end points. */
91};
92
106
107static constexpr float ui_primary_point_draw_size_px = 8.0f;
108static constexpr float ui_secondary_point_draw_size_px = 5.0f;
109static constexpr float ui_tertiary_point_draw_size_px = 3.0f;
110static constexpr float ui_point_hit_size_px = 20.0f;
111static constexpr float ui_point_max_hit_size_px = 600.0f;
112
113/* These three points are only used for `Box` and `Circle` type. */
114static constexpr int control_point_first = 0;
115static constexpr int control_point_center = 1;
116static constexpr int control_point_last = 2;
117
120 /* For drawing preview loop. */
123
125 /* Stored in layer space. */
127 /* Store the control points temporally. */
130
134 /* Helper class to project screen space coordinates to 3D. */
136
140 std::optional<ColorGeometry4f> vertex_color;
141 std::optional<ColorGeometry4f> fill_color;
145 float softness;
149
157
161
162 /* Reference mouse position for initial radial control value. */
164 /* Initial value of radius or opacity. */
165 std::variant<int, float> initial_value;
166
168};
169
171{
172 switch (ptd.type) {
174 case PrimitiveType::Line: {
175 return 1;
176 }
179 case PrimitiveType::Arc: {
180 return 2;
181 }
183 return 3;
184 }
185 }
186
188 return 0;
189}
190
192{
193 BLI_assert(point != -1);
196 }
197
198 const int num_shared_points = control_points_per_segment(ptd);
199 if (math::mod(point, num_shared_points) == 0) {
201 }
203}
204
207 MutableSpan<float> sizes)
208{
209 ColorGeometry4f color_gizmo_primary;
210 ColorGeometry4f color_gizmo_secondary;
211 ColorGeometry4f color_gizmo_a;
212 ColorGeometry4f color_gizmo_b;
213 ui::theme::get_color_4fv(TH_GIZMO_PRIMARY, color_gizmo_primary);
214 ui::theme::get_color_4fv(TH_GIZMO_SECONDARY, color_gizmo_secondary);
215 ui::theme::get_color_4fv(TH_GIZMO_A, color_gizmo_a);
216 ui::theme::get_color_4fv(TH_GIZMO_B, color_gizmo_b);
217
218 const float size_primary = ui_primary_point_draw_size_px;
219 const float size_secondary = ui_secondary_point_draw_size_px;
220 const float size_tertiary = ui_tertiary_point_draw_size_px;
221
222 if (ptd.segments == 0) {
223 colors.fill(color_gizmo_primary);
224 sizes.fill(size_primary);
225 return;
226 }
227
229 colors.fill(color_gizmo_primary);
230 sizes.fill(size_primary);
231
232 /* Set the center point's color. */
233 colors[control_point_center] = color_gizmo_b;
234 sizes[control_point_center] = size_secondary;
235 }
236 else {
237 colors.fill(color_gizmo_secondary);
238 sizes.fill(size_secondary);
239
240 for (const int i : colors.index_range()) {
241 const ControlPointType control_point_type = get_control_point_type(ptd, i);
242
243 if (control_point_type == ControlPointType::JoinPoint) {
244 colors[i] = color_gizmo_b;
245 sizes[i] = size_tertiary;
246 }
247 }
248
249 colors.last() = color_gizmo_primary;
250 sizes.last() = size_tertiary;
251
253 colors.last(1) = color_gizmo_secondary;
254 sizes.last(1) = size_primary;
255 }
256 }
257
258 const int active_index = ptd.active_control_point_index;
259 if (active_index != -1) {
260 sizes[active_index] *= 1.5;
261 colors[active_index] = math::interpolate(colors[active_index], color_gizmo_a, 0.5f);
262 }
263}
264
266{
267 GPUVertFormat *format3d = immVertexFormat();
268 const uint pos3d = GPU_vertformat_attr_add(format3d, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
269 const uint col3d = GPU_vertformat_attr_add(
270 format3d, "color", gpu::VertAttrType::SFLOAT_32_32_32_32);
271 const uint siz3d = GPU_vertformat_attr_add(format3d, "size", gpu::VertAttrType::SFLOAT_32);
273
276
277 Array<ColorGeometry4f> colors(ptd.control_points.size());
278 Array<float> sizes(ptd.control_points.size());
279 control_point_colors_and_sizes(ptd, colors, sizes);
280
281 for (const int point : ptd.control_points.index_range()) {
283 ptd.control_points[point]);
284 const ColorGeometry4f color = colors[point];
285 const float size = sizes[point];
286
287 immAttr4f(col3d, color[0], color[1], color[2], color[3]);
288 immAttr1f(siz3d, size * 2.0f);
289 immVertex3fv(pos3d, world_pos);
290 }
291
292 immEnd();
295}
296
297static void grease_pencil_primitive_draw(const bContext * /*C*/, ARegion * /*region*/, void *arg)
298{
299 PrimitiveToolOperation &ptd = *reinterpret_cast<PrimitiveToolOperation *>(arg);
301}
302
304{
305 ptd.temp_segments = ptd.segments;
306 ptd.temp_control_points.resize(ptd.control_points.size());
307 array_utils::copy(ptd.control_points.as_span(), ptd.temp_control_points.as_mutable_span());
308}
309
311{
312 ptd.segments = ptd.temp_segments;
313 ptd.control_points.resize(ptd.temp_control_points.size());
314 array_utils::copy(ptd.temp_control_points.as_span(), ptd.control_points.as_mutable_span());
315}
316
318 Span<float2> control_points,
319 MutableSpan<float2> new_positions)
320{
321 const int subdivision = ptd.subdivision;
322 const int new_points_num = new_positions.size();
323
324 if (ptd.segments == 0) {
325 new_positions.fill(control_points.last());
326 return;
327 }
328
329 switch (ptd.type) {
332 for (const int i : new_positions.index_range().drop_back(1)) {
333 const float t = math::mod(i / float(subdivision + 1), 1.0f);
334 const int point = int(i / (subdivision + 1));
335 const int point_next = point + 1;
336 new_positions[i] = math::interpolate(control_points[point], control_points[point_next], t);
337 }
338 new_positions.last() = control_points.last();
339 return;
340 }
341 case PrimitiveType::Arc: {
342 const int num_shared_points = control_points_per_segment(ptd);
343 const int num_segments = ptd.segments;
344 for (const int segment_i : IndexRange(num_segments)) {
345 const float2 A = control_points[num_shared_points * segment_i + 0];
346 const float2 B = control_points[num_shared_points * segment_i + 1];
347 const float2 C = control_points[num_shared_points * segment_i + 2];
348 for (const int i : IndexRange(subdivision + 1)) {
349 const float t = i / float(subdivision + 1);
350 const float2 AB = math::interpolate(A, B, t);
351 const float2 BC = math::interpolate(B, C, t);
352 new_positions[i + segment_i * (subdivision + 1)] = math::interpolate(AB, BC, t);
353 }
354 }
355 new_positions.last() = control_points.last();
356 return;
357 }
359 const int num_shared_points = control_points_per_segment(ptd);
360 const int num_segments = ptd.segments;
361
362 for (const int segment_i : IndexRange(num_segments)) {
363 const float2 A = control_points[num_shared_points * segment_i + 0];
364 const float2 B = control_points[num_shared_points * segment_i + 1];
365 const float2 C = control_points[num_shared_points * segment_i + 2];
366 const float2 D = control_points[num_shared_points * segment_i + 3];
367 for (const int i : IndexRange(subdivision + 1)) {
368 const float t = i / float(subdivision + 1);
369 const float2 AB = math::interpolate(A, B, t);
370 const float2 BC = math::interpolate(B, C, t);
371 const float2 CD = math::interpolate(C, D, t);
372 const float2 ABBC = math::interpolate(AB, BC, t);
373 const float2 BCCD = math::interpolate(BC, CD, t);
374 new_positions[i + segment_i * (subdivision + 1)] = math::interpolate(ABBC, BCCD, t);
375 }
376 }
377 new_positions.last() = control_points.last();
378 return;
379 }
381 const float2 center = control_points[control_point_center];
382 const float2 offset = control_points[control_point_first] - center;
383 for (const int i : new_positions.index_range()) {
384 const float t = i / float(new_points_num);
385 const float a = t * std::numbers::pi * 2.0f;
386 new_positions[i] = offset * float2(sinf(a), cosf(a)) + center;
387 }
388 return;
389 }
390 case PrimitiveType::Box: {
391 const float2 center = control_points[control_point_center];
392 const float2 offset = control_points[control_point_first] - center;
393 /*
394 * Calculate the 4 corners of the box.
395 * Here's a diagram.
396 *
397 * +-----------+
398 * |A B|
399 * | |
400 * | center |
401 * | |
402 * |D C|
403 * +-----------+
404 *
405 */
406 const float2 A = center + offset * float2(1.0f, 1.0f);
407 const float2 B = center + offset * float2(-1.0f, 1.0f);
408 const float2 C = center + offset * float2(-1.0f, -1.0f);
409 const float2 D = center + offset * float2(1.0f, -1.0f);
410 const float2 corners[4] = {A, B, C, D};
411 for (const int i : new_positions.index_range()) {
412 const float t = math::mod(i / float(subdivision + 1), 1.0f);
413 const int point = int(i / (subdivision + 1));
414 const int point_next = math::mod(point + 1, 4);
415 new_positions[i] = math::interpolate(corners[point], corners[point_next], t);
416 }
417 return;
418 }
419 }
420}
421
427
429 MutableSpan<float2> new_positions)
430{
431 Array<float2> control_points_2d(ptd.control_points.size());
432 for (const int i : ptd.control_points.index_range()) {
433 control_points_2d[i] = primitive_local_to_screen(ptd, ptd.control_points[i]);
434 }
435
436 primitive_calculate_curve_positions(ptd, control_points_2d, new_positions);
437}
438
440{
441 const int subdivision = ptd.subdivision;
442
443 switch (ptd.type) {
448 case PrimitiveType::Arc: {
449 const int join_points = ptd.segments + 1;
450 return join_points + subdivision * ptd.segments;
451 break;
452 }
453 case PrimitiveType::Box: {
454 return 4 + subdivision * 4;
455 break;
456 }
457 }
458
460 return 0;
461}
462
464{
465 const bool on_back = ptd.on_back;
466 const int new_points_num = grease_pencil_primitive_curve_points_number(ptd);
467 const bool use_random = (ptd.settings->flag & GP_BRUSH_GROUP_RANDOM) != 0;
468
470 const int target_curve_index = on_back ? 0 : curves.curves_range().last();
471 ed::greasepencil::resize_single_curve(curves, on_back == false, new_points_num);
472
473 const IndexRange curve_points = curves.points_by_curve()[target_curve_index];
474
475 MutableSpan<float3> positions_3d = curves.positions_for_write().slice(curve_points);
476 Array<float2> positions_2d(new_points_num);
477
479 ptd.placement.project(positions_2d, positions_3d);
480
481 Set<std::string> point_attributes_to_skip;
482
483 MutableSpan<float> new_radii = ptd.drawing->radii_for_write().slice(curve_points);
484 MutableSpan<float> new_opacities = ptd.drawing->opacities_for_write().slice(curve_points);
485 MutableSpan<ColorGeometry4f> new_vertex_colors = ptd.drawing->vertex_colors_for_write().slice(
486 curve_points);
488 MutableSpan<float> new_rotations;
489 if (use_random && ptd.settings->uv_random > 0.0f) {
490 rotations = curves.attributes_for_write().lookup_or_add_for_write_span<float>(
491 "rotation", bke::AttrDomain::Point);
492 new_rotations = rotations.span.slice(curve_points);
493 }
494
495 const ToolSettings *ts = ptd.vc.scene->toolsettings;
496 const GP_Sculpt_Settings *gset = &ts->gp_sculpt;
497 const Paint &paint = ts->gp_paint->paint;
498
499 /* Screen-space length along curve used as randomization parameter. */
500 Array<float> lengths(new_points_num);
501
502 for (const int point : curve_points.index_range()) {
503 float pressure = 1.0f;
504 /* Apply pressure curve. */
506 const float t = point / float(new_points_num - 1);
507 pressure = BKE_curvemapping_evaluateF(gset->cur_primitive, 0, t);
508 }
509
510 const float radius = ed::greasepencil::radius_from_input_sample(ptd.vc.rv3d,
511 ptd.region,
512 paint,
513 ptd.brush,
514 pressure,
515 positions_3d[point],
517 ptd.settings);
519 pressure, paint, ptd.brush, ptd.settings);
520
521 if (point == 0) {
522 lengths[point] = 0.0f;
523 }
524 else {
525 const float distance_2d = math::distance(positions_2d[point - 1], positions_2d[point]);
526 lengths[point] = lengths[point - 1] + distance_2d;
527 }
528
529 new_radii[point] = ed::greasepencil::randomize_radius(
530 *ptd.settings, ptd.stroke_random_radius_factor, lengths[point], radius, pressure);
531 new_opacities[point] = ed::greasepencil::randomize_opacity(
532 *ptd.settings, ptd.stroke_random_opacity_factor, lengths[point], opacity, pressure);
533 if (ptd.vertex_color) {
534 std::optional<BrushColorJitterSettings> jitter_settings =
536 ptd.brush);
537 new_vertex_colors[point] = ed::greasepencil::randomize_color(*ptd.settings,
538 jitter_settings,
542 lengths[point],
543 *ptd.vertex_color,
544 pressure);
545 }
546 if (rotations) {
547 new_rotations[point] = ed::greasepencil::randomize_rotation(
548 *ptd.settings, ptd.stroke_random_rotation_factor, lengths[point], pressure);
549 }
550 }
551
552 point_attributes_to_skip.add_multiple({"position", "radius", "opacity"});
553 if (ptd.vertex_color) {
554 point_attributes_to_skip.add("vertex_color");
555 }
556 if (rotations) {
557 point_attributes_to_skip.add("rotation");
558 rotations.finish();
559 }
560
561 /* Initialize the rest of the attributes with default values. */
562 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
565 bke::attribute_filter_from_skip_ref(point_attributes_to_skip),
566 curve_points);
567
568 if (on_back) {
570 }
571 else {
572 ptd.drawing->tag_topology_changed(IndexRange::from_single(target_curve_index));
573 }
575 IndexRange::from_single(target_curve_index));
576}
577
579{
580 /* Resize the curves geometry so there is one more curve with a single point. */
581 const bool on_back = ptd.on_back;
582 ed::greasepencil::add_single_curve(*ptd.drawing, on_back == false);
584
585 const int target_curve_index = on_back ? 0 : (curves.curves_num() - 1);
586
587 Set<std::string> curve_attributes_to_skip;
588
589 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
591 "material_index", bke::AttrDomain::Curve);
592 materials.span[target_curve_index] = ptd.material_index;
593 materials.finish();
594 curve_attributes_to_skip.add("material_index");
595
597 "cyclic", bke::AttrDomain::Curve);
598 const bool is_cyclic = ELEM(ptd.type, PrimitiveType::Box, PrimitiveType::Circle);
599 cyclic.span[target_curve_index] = is_cyclic;
600 cyclic.finish();
601 curve_attributes_to_skip.add("cyclic");
602
603 if ((ptd.settings->flag2 & GP_BRUSH_USE_STROKE) == 0) {
605 "hide_stroke", bke::AttrDomain::Curve);
606 hide_stroke.span[target_curve_index] = true;
607 curve_attributes_to_skip.add("hide_stroke");
608 hide_stroke.finish();
609 }
610 if (ptd.use_fill) {
612 "fill_id", bke::AttrDomain::Curve);
613 /* Set new fill id to zero, because it will have uninitialized memory otherwise.
614 * Then get the #VArray of all fill ids to compute a new one. */
615 fill_id.span[target_curve_index] = 0;
616 fill_id.span[target_curve_index] = bke::greasepencil::get_next_available_fill_id(fill_id.span);
617 curve_attributes_to_skip.add("fill_id");
618 fill_id.finish();
619 }
620
621 if (bke::SpanAttributeWriter<float> softness = attributes.lookup_or_add_for_write_span<float>(
622 "softness", bke::AttrDomain::Curve))
623 {
624 softness.span[target_curve_index] = ptd.softness;
625 softness.finish();
626 curve_attributes_to_skip.add("softness");
627 }
628
629 /* Only set the attribute if the type is not the default or if it already exists. */
630 if (ptd.settings->caps_type != GP_STROKE_CAP_TYPE_ROUND || attributes.contains("start_cap")) {
631 if (bke::SpanAttributeWriter<int8_t> start_caps =
633 {
634 start_caps.span[target_curve_index] = ptd.settings->caps_type;
635 start_caps.finish();
636 curve_attributes_to_skip.add("start_cap");
637 }
638 }
639
640 if (ptd.settings->caps_type != GP_STROKE_CAP_TYPE_ROUND || attributes.contains("end_cap")) {
643 {
644 end_caps.span[target_curve_index] = ptd.settings->caps_type;
645 end_caps.finish();
646 curve_attributes_to_skip.add("end_cap");
647 }
648 }
649
650 if (ptd.use_fill && (ptd.fill_opacity < 1.0f || attributes.contains("fill_opacity"))) {
651 if (bke::SpanAttributeWriter fill_opacities = attributes.lookup_or_add_for_write_span<float>(
652 "fill_opacity", bke::AttrDomain::Curve, bke::AttributeInitValue(1.0f)))
653 {
654 fill_opacities.span[target_curve_index] = ptd.fill_opacity;
655 fill_opacities.finish();
656 curve_attributes_to_skip.add("fill_opacity");
657 }
658 }
659
660 if (ptd.fill_color) {
661 ptd.drawing->fill_colors_for_write()[target_curve_index] = *ptd.fill_color;
662 curve_attributes_to_skip.add("fill_color");
663 }
664
666 "u_scale", bke::AttrDomain::Curve))
667 {
668 u_scale.span[target_curve_index] = 1.0f;
669 u_scale.finish();
670 curve_attributes_to_skip.add("u_scale");
671 }
672
673 curves.curve_types_for_write()[target_curve_index] = CURVE_TYPE_POLY;
674 curves.update_curve_types();
675 curve_attributes_to_skip.add("curve_type");
676
677 /* Initialize the rest of the attributes with default values. */
680 bke::attribute_filter_from_skip_ref(curve_attributes_to_skip),
681 on_back ? curves.curves_range().take_front(1) :
682 curves.curves_range().take_back(1));
684}
685
687{
689 const int target_curve_index = ptd.on_back ? 0 : (curves.curves_num() - 1);
690 curves.remove_curves(IndexRange::from_single(target_curve_index), {});
692}
693
694/* Helper: Draw status message while the user is running the operator. */
696 wmOperator *op,
698{
699 WorkspaceStatus status(C);
700 status.opmodal(IFACE_("Confirm"), op->type, int(ModalKeyMode::Confirm));
701 status.opmodal(IFACE_("Cancel"), op->type, int(ModalKeyMode::Cancel));
702 status.opmodal(IFACE_("Panning"), op->type, int(ModalKeyMode::Panning));
703 status.item(IFACE_("Align"), ICON_EVENT_SHIFT);
704 status.opmodal("", op->type, int(ModalKeyMode::IncreaseSubdivision));
705 status.opmodal("", op->type, int(ModalKeyMode::DecreaseSubdivision));
706 status.item(fmt::format("{} ({})", IFACE_("Subdivisions"), ptd.subdivision), ICON_NONE);
707
708 if (ptd.segments == 1) {
709 status.item(IFACE_("Center"), ICON_EVENT_ALT);
710 }
711
712 if (ELEM(ptd.type,
717 {
718 status.opmodal(IFACE_("Extrude"), op->type, int(ModalKeyMode::Extrude));
719 }
720
721 status.opmodal(IFACE_("Grab"), op->type, int(ModalKeyMode::Grab));
722 status.opmodal(IFACE_("Rotate"), op->type, int(ModalKeyMode::Rotate));
723 status.opmodal(IFACE_("Scale"), op->type, int(ModalKeyMode::Scale));
724}
725
735
736/* Invoke handler: Initialize the operator. */
738 wmOperator *op,
739 const wmEvent *event)
740{
742 return OPERATOR_CANCELLED;
743 }
744
745 /* If in tools region, wait till we get to the main (3D-space)
746 * region before allowing drawing to take place. */
748
749 wmWindow *win = CTX_wm_window(C);
750
751 /* Set cursor to indicate modal. */
753
755
756 /* Allocate new data. */
757 PrimitiveToolOperation *ptd_pointer = MEM_new<PrimitiveToolOperation>(__func__);
758 op->customdata = ptd_pointer;
759
760 PrimitiveToolOperation &ptd = *ptd_pointer;
761
762 ptd.vc = vc;
763 ptd.region = vc.region;
765 const float2 start_coords = float2(event->mval);
766
767 GreasePencil *grease_pencil = id_cast<GreasePencil *>(vc.obact->data);
768
769 /* Initialize helper class for projecting screen space coordinates. */
771 *vc.scene, *vc.region, *view3d, *vc.obact, grease_pencil->get_active_layer());
772 if (placement.use_project_to_surface()) {
774 }
775 else if (placement.use_project_to_stroke()) {
777 }
778
779 ptd.placement = placement;
780
781 ptd.vod = ED_view3d_navigation_init(C, nullptr);
782
783 ptd.start_position_2d = start_coords;
784 ptd.subdivision = RNA_int_get(op->ptr, "subdivision");
785 ptd.type = PrimitiveType(RNA_enum_get(op->ptr, "type"));
786 const float3 pos = ptd.placement.project(ptd.start_position_2d);
787 ptd.segments = 0;
788 ptd.control_points = Vector<float3>({pos});
789
791
793 ptd.segments++;
794 ptd.control_points.append_n_times(pos, control_points_per_segment(ptd));
797
800 if (ptd.brush->gpencil_settings == nullptr) {
802 }
805
815
817 GP_Sculpt_Settings *gset = &ts->gp_sculpt;
818 /* Initialize pressure curve. */
821 }
822
824 CTX_data_main(C), vc.obact, ptd.brush);
826 ptd.use_fill = (ptd.settings->flag2 & GP_BRUSH_USE_FILL) != 0;
827
828 const bool use_vertex_color = (vc.scene->toolsettings->gp_paint->mode ==
830 if (use_vertex_color) {
831 ColorGeometry4f color_base;
832 copy_v3_v3(color_base, BKE_brush_color_get(paint, ptd.brush));
833 color_base.a = ptd.settings->vertex_factor;
834
836 std::make_optional(color_base) :
837 std::nullopt;
838 ptd.fill_color = ptd.use_fill ? std::make_optional(color_base) : std::nullopt;
839 }
840 else {
841 ptd.vertex_color = std::nullopt;
842 ptd.fill_color = std::nullopt;
843 }
844
846 ptd.softness = 1.0 - ptd.settings->hardness;
847
849 vc.scene, ptd.region, ptd.start_position_2d, ptd.placement);
850
851 const bool use_random = (ptd.settings->flag & GP_BRUSH_GROUP_RANDOM) != 0;
852 if (use_random) {
854 ptd.stroke_random_radius_factor = ptd.rng.get_float() * 2.0f - 1.0f;
855 ptd.stroke_random_opacity_factor = ptd.rng.get_float() * 2.0f - 1.0f;
856 ptd.stroke_random_rotation_factor = ptd.rng.get_float() * 2.0f - 1.0f;
857 ptd.stroke_random_hue_factor = ptd.rng.get_float() * 2.0f - 1.0f;
858 ptd.stroke_random_sat_factor = ptd.rng.get_float() * 2.0f - 1.0f;
859 ptd.stroke_random_val_factor = ptd.rng.get_float() * 2.0f - 1.0f;
860 }
861
862 BLI_assert(grease_pencil->has_active_layer());
863 ptd.local_transform = grease_pencil->get_active_layer()->local_transform();
864 ptd.drawing = grease_pencil->get_editable_drawing_at(*grease_pencil->get_active_layer(),
865 vc.scene->r.cfra);
866
869
872
873 /* Updates indicator in header. */
875
876 /* Add a modal handler for this operator. */
878
880}
881
882/* Exit and free memory. */
883static void grease_pencil_primitive_exit(bContext *C, wmOperator *op, const bool cancelled)
884{
886
887 const Scene &scene = *CTX_data_scene(C);
888 const bool do_automerge_endpoints = (scene.toolsettings->gpencil_flags &
890 const bool on_back = (scene.toolsettings->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) != 0;
891
892 if (do_automerge_endpoints && !cancelled) {
893 const Object &ob = *ptd->vc.obact;
894 const GreasePencil *grease_pencil = id_cast<GreasePencil *>(ob.data);
895 const bke::greasepencil::Layer &active_layer = *grease_pencil->get_active_layer();
896
897 constexpr float merge_distance = 30.0f;
898 const float4x4 layer_to_world = active_layer.to_world_space(ob);
899 bke::greasepencil::Drawing &drawing = *ptd->drawing;
900 const bke::CurvesGeometry &src_curves = drawing.strokes();
901 const int active_curve = on_back ? src_curves.curves_range().first() :
902 src_curves.curves_range().last();
903 const IndexMask selection = IndexRange::from_single(active_curve);
904
906 *CTX_wm_region(C), src_curves, layer_to_world, merge_distance, selection, {});
907 drawing.tag_topology_changed();
908 }
909
910 /* Clear status message area. */
911 ED_workspace_status_text(C, nullptr);
912
914
915 /* Deactivate the extra drawing stuff in 3D-View. */
917
919
921
922 MEM_delete<PrimitiveToolOperation>(ptd);
923 /* Clear pointer. */
924 op->customdata = nullptr;
925}
926
928{
929 using namespace math;
930 return sign(p) * float2(1.0f / std::numbers::sqrt2) * length(p);
931}
932
933/* Using Chebyshev distance instead of Euclidean. */
935{
936 using namespace math;
937 return sign(p) * float2(std::max(abs(p[0]), abs(p[1])));
938}
939
940/* Snaps to the closest diagonal, horizontal or vertical. */
942{
943 using namespace math;
944 /* sin(pi/8) or sin of 22.5 degrees. */
945 const float sin225 = 0.3826834323650897717284599840304f;
946 return sign(p) * length(p) * normalize(sign(normalize(abs(p)) - sin225) + 1.0f);
947}
948
950 const wmEvent *event)
951{
952 using namespace math;
953 const float2 start = ptd.start_position_2d;
954 const float2 end = float2(event->mval);
955
956 const float2 dif = end - start;
957 float2 offset = dif;
958
959 if (event->modifier & KM_SHIFT) {
960 if (ptd.type == PrimitiveType::Box) {
961 offset = snap_diagonals_box(dif);
962 }
963 else if (ptd.type == PrimitiveType::Circle) {
964 offset = snap_diagonals(dif);
965 }
966 else { /* Line, Polyline, Arc and Curve. */
967 offset = snap_8_angles(dif);
968 }
969 }
970 offset *= 0.5f;
971
972 float2 center = start + offset;
973
974 if (event->modifier & KM_ALT && ptd.segments == 1) {
975 center = start;
976 offset *= 2.0f;
977 }
978
979 const float3 start_pos = ptd.placement.project(center - offset);
980 const float3 end_pos = ptd.placement.project(center + offset);
981
982 const int number_control_points = control_points_per_segment(ptd);
983 for (const int i : IndexRange(number_control_points + 1)) {
984 ptd.control_points.last(i) = interpolate(
985 end_pos, start_pos, (i / float(number_control_points)));
986 }
987}
988
990 const wmEvent *event)
991{
992 const float2 start = ptd.start_position_2d;
993 const float2 end = float2(event->mval);
994 const float2 dif = end - start;
995
996 for (const int point_index : ptd.control_points.index_range()) {
997 const float2 start_pos2 = primitive_local_to_screen(ptd, ptd.temp_control_points[point_index]);
998
999 float3 pos = ptd.placement.project(start_pos2 + dif);
1000 ptd.control_points[point_index] = pos;
1001 }
1002}
1003
1005{
1007 const float3 pos = ptd.placement.project(float2(event->mval));
1009
1011 return;
1012 }
1013
1014 /* If the center point is been grabbed, move all points. */
1017 return;
1018 }
1019
1020 const int other_point = ptd.active_control_point_index == control_point_first ?
1023
1024 /* Get the location of the other control point. */
1025 const float2 other_point_2d = primitive_local_to_screen(ptd,
1026 ptd.temp_control_points[other_point]);
1027
1028 /* Set the center point to between the first and last point. */
1030 (other_point_2d + float2(event->mval)) / 2.0f);
1031}
1032
1034{
1036 const float2 start = ptd.start_position_2d;
1037 const float2 end = float2(event->mval);
1038 const float2 dif = end - start;
1039
1040 const float2 start_pos2 = primitive_local_to_screen(
1042
1043 const float3 pos = ptd.placement.project(start_pos2 + dif);
1045}
1046
1048{
1051 }
1052 float2 center_of_mass = float2(0.0f, 0.0f);
1053
1054 for (const int point_index : ptd.control_points.index_range()) {
1055 center_of_mass += primitive_local_to_screen(ptd, ptd.temp_control_points[point_index]);
1056 }
1057 center_of_mass /= ptd.control_points.size();
1058 return center_of_mass;
1059}
1060
1062 const wmEvent *event)
1063{
1064 const float2 start = ptd.start_position_2d;
1065 const float2 end = float2(event->mval);
1066
1067 const float2 center_of_mass = primitive_center_of_mass(ptd);
1068
1069 const float2 end_ = end - center_of_mass;
1070 const float2 start_ = start - center_of_mass;
1071 const float rotation = math::atan2(start_[0], start_[1]) - math::atan2(end_[0], end_[1]);
1072
1073 for (const int point_index : ptd.control_points.index_range()) {
1074 const float2 start_pos2 = primitive_local_to_screen(ptd, ptd.temp_control_points[point_index]);
1075
1076 const float2 dif = start_pos2 - center_of_mass;
1077 const float c = math::cos(rotation);
1078 const float s = math::sin(rotation);
1079 const float2x2 rot = float2x2(float2(c, s), float2(-s, c));
1080 const float2 pos2 = rot * dif + center_of_mass;
1081 const float3 pos = ptd.placement.project(pos2);
1082 ptd.control_points[point_index] = pos;
1083 }
1084}
1085
1087 const wmEvent *event)
1088{
1089 const float2 start = ptd.start_position_2d;
1090 const float2 end = float2(event->mval);
1091
1092 const float2 center_of_mass = primitive_center_of_mass(ptd);
1093
1094 const float scale = math::length(end - center_of_mass) / math::length(start - center_of_mass);
1095
1096 for (const int point_index : ptd.control_points.index_range()) {
1097 const float2 start_pos2 = primitive_local_to_screen(ptd, ptd.temp_control_points[point_index]);
1098
1099 const float2 pos2 = (start_pos2 - center_of_mass) * scale + center_of_mass;
1100 const float3 pos = ptd.placement.project(pos2);
1101 ptd.control_points[point_index] = pos;
1102 }
1103}
1104
1106{
1107 PointerRNA brush_ptr = RNA_id_pointer_create(&ptd.brush->id);
1108 const int value = RNA_int_get(&brush_ptr, "size");
1109
1110 ptd.initial_value.emplace<int>(value);
1111 ptd.reference_position_2d = ptd.start_position_2d - float2(value, 0.0f);
1112}
1113
1115{
1116 const float display_size = 200.0f * UI_SCALE_FAC;
1117
1118 PointerRNA brush_ptr = RNA_id_pointer_create(&ptd.brush->id);
1119 const float value = RNA_float_get(&brush_ptr, "strength");
1120
1121 ptd.initial_value.emplace<float>(value);
1122 ptd.reference_position_2d = ptd.start_position_2d - float2(value * display_size, 0.0f);
1123}
1124
1126{
1127 PointerRNA brush_ptr = RNA_id_pointer_create(&ptd.brush->id);
1128 RNA_int_set(&brush_ptr, "size", std::get<int>(ptd.initial_value));
1129}
1130
1132{
1133 PointerRNA brush_ptr = RNA_id_pointer_create(&ptd.brush->id);
1134 RNA_float_set(&brush_ptr, "strength", std::get<float>(ptd.initial_value));
1135}
1136
1138 const wmEvent *event)
1139{
1140 /* Clamp reference position if mouse moves past the limits. */
1141 const float2 mouse_co = float2(event->mval);
1142 ptd.reference_position_2d.x = std::min(ptd.reference_position_2d.x, mouse_co.x);
1143 const float2 delta = mouse_co - ptd.reference_position_2d;
1144 /* Clamp to work around brush property getting "stuck" on zero. */
1145 const int new_value = std::max(int(delta.x), 1);
1146
1147 PointerRNA brush_ptr = RNA_id_pointer_create(&ptd.brush->id);
1148 RNA_int_set(&brush_ptr, "size", new_value);
1149}
1150
1152 const wmEvent *event)
1153{
1154 const float display_size = 200.0f * UI_SCALE_FAC;
1155
1156 /* Clamp reference position if mouse moves past the limits. */
1157 const float2 mouse_co = float2(event->mval);
1158 ptd.reference_position_2d.x = std::max(std::min(ptd.reference_position_2d.x, mouse_co.x),
1159 mouse_co.x - display_size);
1160 const float2 delta = mouse_co - ptd.reference_position_2d;
1161 const float new_value = delta.x / display_size;
1162
1163 PointerRNA brush_ptr = RNA_id_pointer_create(&ptd.brush->id);
1164 RNA_float_set(&brush_ptr, "strength", new_value);
1165}
1166
1167static int primitive_check_ui_hover(const PrimitiveToolOperation &ptd, const wmEvent *event)
1168{
1169 float closest_distance_squared = std::numeric_limits<float>::max();
1170 int closest_point = -1;
1171
1172 for (const int i : ptd.control_points.index_range()) {
1173 const int point = (ptd.control_points.size() - 1) - i;
1174 const float2 pos_proj = primitive_local_to_screen(ptd, ptd.control_points[point]);
1175 const float radius_sq = ui_point_hit_size_px * ui_point_hit_size_px;
1176 const float distance_squared = math::distance_squared(pos_proj, float2(event->mval));
1177 /* If the mouse is over a control point. */
1178 if (distance_squared <= radius_sq) {
1179 return point;
1180 }
1181
1182 const ControlPointType control_point_type = get_control_point_type(ptd, point);
1183
1184 /* Save the closest handle point. */
1185 if (distance_squared < closest_distance_squared &&
1186 control_point_type == ControlPointType::HandlePoint &&
1188 {
1189 closest_point = point;
1190 closest_distance_squared = distance_squared;
1191 }
1192 }
1193
1194 if (closest_point != -1) {
1195 return closest_point;
1196 }
1197
1198 return -1;
1199}
1200
1203 const wmEvent *event)
1204{
1205 wmWindow *win = CTX_wm_window(C);
1206
1207 if (ptd.mode != OperatorMode::Idle) {
1209 return;
1210 }
1211
1212 const int ui_id = primitive_check_ui_hover(ptd, event);
1213 ptd.active_control_point_index = ui_id;
1214 if (ui_id == -1) {
1215 if (ptd.type == PrimitiveType::Polyline) {
1217 return;
1218 }
1219
1221 return;
1222 }
1223
1225}
1226
1228 wmOperator *op,
1230 const wmEvent *event)
1231{
1232 switch (event->val) {
1233 case int(ModalKeyMode::Cancel): {
1236
1237 return OPERATOR_CANCELLED;
1238 }
1239 case int(ModalKeyMode::Confirm): {
1240 grease_pencil_primitive_exit(C, op, false);
1241
1242 return OPERATOR_FINISHED;
1243 }
1244 case int(ModalKeyMode::Extrude): {
1245 if (ptd.mode == OperatorMode::Idle &&
1247 {
1250
1252 const float3 pos = ptd.placement.project(ptd.start_position_2d);
1253
1254 const int number_control_points = control_points_per_segment(ptd);
1255 ptd.control_points.append_n_times(pos, number_control_points);
1257 ptd.segments++;
1258
1260 }
1261
1262 if (ptd.type == PrimitiveType::Polyline &&
1264 {
1267
1270 const float3 pos = ptd.placement.project(float2(event->mval));
1271
1272 /* If we have only two points and they're the same then don't extrude new a point. */
1273 if (ptd.segments == 1 &&
1274 math::distance_squared(ptd.control_points.first(), ptd.control_points.last()) == 0.0f)
1275 {
1276 ptd.control_points.last() = pos;
1277 }
1278 else {
1279 ptd.control_points.append(pos);
1280 ptd.segments++;
1281 }
1282
1284 }
1285
1287 }
1288 case int(ModalKeyMode::Grab): {
1289 if (ptd.mode == OperatorMode::Idle) {
1290 ptd.start_position_2d = float2(event->mval);
1292
1294 }
1296 }
1297 case int(ModalKeyMode::Rotate): {
1298 if (ptd.mode == OperatorMode::Idle) {
1299 ptd.start_position_2d = float2(event->mval);
1301
1303 }
1305 }
1306 case int(ModalKeyMode::Scale): {
1307 if (ptd.mode == OperatorMode::Idle) {
1308 ptd.start_position_2d = float2(event->mval);
1310
1312 }
1314 }
1316 if (event->val != KM_RELEASE) {
1317 ptd.subdivision++;
1318 RNA_int_set(op->ptr, "subdivision", ptd.subdivision);
1319 }
1321 }
1323 if (event->val != KM_RELEASE) {
1324 ptd.subdivision--;
1325 ptd.subdivision = std::max(ptd.subdivision, 0);
1326 RNA_int_set(op->ptr, "subdivision", ptd.subdivision);
1327 }
1329 }
1330 case int(ModalKeyMode::ChangeRadius): {
1331 if (ptd.mode == OperatorMode::Idle) {
1332 ptd.start_position_2d = float2(event->mval);
1335
1337 }
1339 }
1340 case int(ModalKeyMode::ChangeOpacity): {
1341 if (ptd.mode == OperatorMode::Idle) {
1342 ptd.start_position_2d = float2(event->mval);
1345
1347 }
1349 }
1350 }
1351
1353}
1354
1356 const wmEvent *event)
1357{
1358 if (event->val == KM_RELEASE && ELEM(ptd.mode,
1367 {
1370 }
1371
1372 if (ptd.mode == OperatorMode::Idle && event->val == KM_PRESS) {
1373 const int ui_id = primitive_check_ui_hover(ptd, event);
1374 ptd.active_control_point_index = ui_id;
1375 if (ui_id == -1) {
1376 if (ptd.type != PrimitiveType::Polyline) {
1377 ptd.start_position_2d = float2(event->mval);
1379
1381
1383 }
1384 }
1385 else {
1386 const ControlPointType control_point_type = get_control_point_type(ptd, ui_id);
1387
1388 if (control_point_type == ControlPointType::JoinPoint) {
1392
1394 }
1395 else if (control_point_type == ControlPointType::HandlePoint) {
1396 ptd.start_position_2d = float2(event->mval);
1398
1400 }
1401
1403 }
1404 }
1405
1406 if (ptd.type == PrimitiveType::Polyline && ptd.mode == OperatorMode::Idle &&
1407 event->val == KM_PRESS)
1408 {
1411
1413 const float3 pos = ptd.placement.project(float2(event->mval));
1414
1415 /* If we have only two points and they're the same then don't extrude new a point. */
1416 if (ptd.segments == 1 &&
1417 math::distance_squared(ptd.control_points.first(), ptd.control_points.last()) == 0.0f)
1418 {
1419 ptd.control_points.last() = pos;
1420 }
1421 else {
1422 ptd.control_points.append(pos);
1423 ptd.segments++;
1424 }
1425 }
1426
1428}
1429
1431 const wmEvent *event)
1432{
1433 switch (ptd.mode) {
1436 break;
1437 }
1438 case OperatorMode::Grab: {
1440 break;
1441 }
1442 case OperatorMode::Drag: {
1444 break;
1445 }
1446 case OperatorMode::DragAll: {
1448 break;
1449 }
1452 break;
1453 }
1456 break;
1457 }
1460 break;
1461 }
1464 break;
1465 }
1466 case OperatorMode::Idle: {
1467 /* Do nothing. */
1468 break;
1469 }
1470 }
1471}
1472
1473/* Modal handler: Events handling during interactive part. */
1475 wmOperator *op,
1476 const wmEvent *event)
1477{
1478 PrimitiveToolOperation &ptd = *reinterpret_cast<PrimitiveToolOperation *>(op->customdata);
1479
1480 /* Check for confirm before navigation. */
1481 if (event->type == EVT_MODAL_MAP) {
1482 if (event->val == int(ModalKeyMode::Confirm)) {
1483 grease_pencil_primitive_exit(C, op, false);
1484
1485 return OPERATOR_FINISHED;
1486 }
1487 }
1488
1489 const float3 pos = ptd.control_points.first();
1490 if (ED_view3d_navigation_do(C, ptd.vod, event, pos)) {
1491 if (ptd.vc.rv3d->rflag & RV3D_NAVIGATING) {
1493
1496
1498 }
1499 }
1500
1503
1504 if (event->type == EVT_MODAL_MAP) {
1505 const wmOperatorStatus return_val = grease_pencil_primitive_event_modal_map(C, op, ptd, event);
1506 if (return_val != OPERATOR_RUNNING_MODAL) {
1507 return return_val;
1508 }
1509 }
1510
1511 switch (event->type) {
1512 case LEFTMOUSE: {
1513 const wmOperatorStatus return_val = grease_pencil_primitive_mouse_event(ptd, event);
1514 if (return_val != OPERATOR_RUNNING_MODAL) {
1515 return return_val;
1516 }
1517
1518 break;
1519 }
1520 case RIGHTMOUSE: {
1521 if (event->val != KM_PRESS) {
1522 break;
1523 }
1524
1525 if (ptd.mode == OperatorMode::Idle) {
1528
1529 return OPERATOR_CANCELLED;
1530 }
1531
1532 if (ptd.mode == OperatorMode::ChangeRadius) {
1534 }
1535 if (ptd.mode == OperatorMode::ChangeOpacity) {
1537 }
1538
1541 break;
1542 }
1543 default: {
1544 break;
1545 }
1546 }
1547
1548 /* Updating is done every event not just `MOUSEMOVE`. */
1551
1552 /* Updates indicator in header. */
1555
1556 /* Still running... */
1558}
1559
1560/* Cancel handler. */
1562{
1563 /* This is just a wrapper around exit() */
1565}
1566
1568 const int default_subdiv,
1569 const PrimitiveType default_type)
1570{
1571 static const EnumPropertyItem grease_pencil_primitive_type[] = {
1572 {int(PrimitiveType::Box), "BOX", 0, "Box", ""},
1573 {int(PrimitiveType::Line), "LINE", 0, "Line", ""},
1574 {int(PrimitiveType::Polyline), "POLYLINE", 0, "Polyline", ""},
1575 {int(PrimitiveType::Circle), "CIRCLE", 0, "Circle", ""},
1576 {int(PrimitiveType::Arc), "ARC", 0, "Arc", ""},
1577 {int(PrimitiveType::Curve), "CURVE", 0, "Curve", ""},
1578 {0, nullptr, 0, nullptr, nullptr},
1579 };
1580
1581 PropertyRNA *prop;
1582
1583 prop = RNA_def_int(ot->srna,
1584 "subdivision",
1585 default_subdiv,
1586 0,
1587 INT_MAX,
1588 "Subdivisions",
1589 "Number of subdivisions per segment",
1590 0,
1591 INT_MAX);
1593
1595 ot->srna, "type", grease_pencil_primitive_type, int(default_type), "Type", "Type of shape");
1596}
1597
1599{
1600 /* Identifiers. */
1601 ot->name = "Grease Pencil Line Shape";
1602 ot->idname = "GREASE_PENCIL_OT_primitive_line";
1603 ot->description = "Create predefined Grease Pencil stroke lines";
1604
1605 /* Callbacks. */
1609
1610 /* Flags. */
1612
1613 /* Properties and Flags. */
1615}
1616
1618{
1619 /* Identifiers. */
1620 ot->name = "Grease Pencil Polyline Shape";
1621 ot->idname = "GREASE_PENCIL_OT_primitive_polyline";
1622 ot->description = "Create predefined Grease Pencil stroke polylines";
1623
1624 /* Callbacks. */
1628
1629 /* Flags. */
1631
1632 /* Properties. */
1634}
1635
1637{
1638 /* Identifiers. */
1639 ot->name = "Grease Pencil Arc Shape";
1640 ot->idname = "GREASE_PENCIL_OT_primitive_arc";
1641 ot->description = "Create predefined Grease Pencil stroke arcs";
1642
1643 /* Callbacks. */
1647
1648 /* Flags. */
1650
1651 /* Properties. */
1653}
1654
1656{
1657 /* Identifiers. */
1658 ot->name = "Grease Pencil Curve Shape";
1659 ot->idname = "GREASE_PENCIL_OT_primitive_curve";
1660 ot->description = "Create predefined Grease Pencil stroke curve shapes";
1661
1662 /* Callbacks. */
1666
1667 /* Flags. */
1669
1670 /* Properties. */
1672}
1673
1675{
1676 /* Identifiers. */
1677 ot->name = "Grease Pencil Box Shape";
1678 ot->idname = "GREASE_PENCIL_OT_primitive_box";
1679 ot->description = "Create predefined Grease Pencil stroke boxes";
1680
1681 /* Callbacks. */
1685
1686 /* Flags. */
1688
1689 /* Properties. */
1691}
1692
1694{
1695 /* Identifiers. */
1696 ot->name = "Grease Pencil Circle Shape";
1697 ot->idname = "GREASE_PENCIL_OT_primitive_circle";
1698 ot->description = "Create predefined Grease Pencil stroke circles";
1699
1700 /* Callbacks. */
1704
1705 /* Flags. */
1707
1708 /* Properties. */
1710}
1711
1712} // namespace ed::greasepencil
1713
1724
1726{
1727 using namespace blender::ed::greasepencil;
1728 static const EnumPropertyItem modal_items[] = {
1729 {int(ModalKeyMode::Cancel), "CANCEL", 0, "Cancel", ""},
1730 {int(ModalKeyMode::Confirm), "CONFIRM", 0, "Confirm", ""},
1731 {int(ModalKeyMode::Panning), "PANNING", 0, "Panning", ""},
1732 {int(ModalKeyMode::Extrude), "EXTRUDE", 0, "Extrude", ""},
1733 {int(ModalKeyMode::Grab), "GRAB", 0, "Grab", ""},
1734 {int(ModalKeyMode::Rotate), "ROTATE", 0, "Rotate", ""},
1735 {int(ModalKeyMode::Scale), "SCALE", 0, "Scale", ""},
1737 "INCREASE_SUBDIVISION",
1738 0,
1739 "Increase Subdivision",
1740 ""},
1742 "DECREASE_SUBDIVISION",
1743 0,
1744 "Decrease Subdivision",
1745 ""},
1746 {int(ModalKeyMode::ChangeRadius), "CHANGE_RADIUS", 0, "Change Radius", ""},
1747 {int(ModalKeyMode::ChangeOpacity), "CHANGE_OPACITY", 0, "Change Opacity", ""},
1748 {0, nullptr, 0, nullptr, nullptr},
1749 };
1750
1751 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Primitive Tool Modal Map");
1752
1753 /* This function is called for each space-type, only needs to add map once. */
1754 if (keymap && keymap->modal_items) {
1755 return;
1756 }
1757
1758 keymap = WM_modalkeymap_ensure(keyconf, "Primitive Tool Modal Map", modal_items);
1759
1760 WM_modalkeymap_assign(keymap, "GREASE_PENCIL_OT_primitive_line");
1761 WM_modalkeymap_assign(keymap, "GREASE_PENCIL_OT_primitive_polyline");
1762 WM_modalkeymap_assign(keymap, "GREASE_PENCIL_OT_primitive_arc");
1763 WM_modalkeymap_assign(keymap, "GREASE_PENCIL_OT_primitive_curve");
1764 WM_modalkeymap_assign(keymap, "GREASE_PENCIL_OT_primitive_box");
1765 WM_modalkeymap_assign(keymap, "GREASE_PENCIL_OT_primitive_circle");
1766}
1767
1768} // namespace blender
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
#define IFACE_(msgid)
#define UI_SCALE_FAC
#define REGION_DRAW_POST_VIEW
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define BLI_assert_unreachable()
#define ELEM(...)
#define A
ChannelStorageType a
constexpr int64_t first() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t last(const int64_t n=0) const
constexpr IndexRange()=default
static constexpr IndexRange from_single(const int64_t index)
constexpr IndexRange index_range() const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
static RandomNumberGenerator from_random_seed()
Definition rand.cc:288
bool add(const Key &key)
Definition BLI_set.hh:248
void add_multiple(Span< Key > keys)
Definition BLI_set.hh:287
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
friend class Vector
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void opmodal(std::string text, const wmOperatorType *ot, int propvalue, bool inverted=false)
Definition area.cc:1107
bool contains(StringRef name) const
IndexRange curves_range() const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
MutableSpan< float > opacities_for_write()
MutableSpan< float > radii_for_write()
bke::CurvesGeometry & strokes_for_write()
const bke::CurvesGeometry & strokes() const
MutableSpan< ColorGeometry4f > fill_colors_for_write()
MutableSpan< ColorGeometry4f > vertex_colors_for_write()
void set_texture_matrices(Span< float4x2 > matrices, const IndexMask &selection)
float4x4 to_world_space(const Object &object) const
void cache_viewport_depths(Depsgraph *depsgraph, ARegion *region, View3D *view3d)
float3 project(float2 co, bool &clipped) const
#define rot(x, k)
#define INT_MAX
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T sign(T) RET
#define abs
float length(VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
Quaternion interpolate(Quaternion a, Quaternion b, float t)
float distance_squared(VecT a, VecT b)
#define B
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
int get_next_available_fill_id(Span< int > fill_ids)
auto attribute_filter_from_skip_ref(const Span< StringRef > skip)
void fill_attribute_range_default(MutableAttributeAccessor dst_attributes, AttrDomain domain, const AttributeFilter &attribute_filter, IndexRange range)
static void grease_pencil_primitive_init_opacity(PrimitiveToolOperation &ptd)
static constexpr int control_point_center
static void grease_pencil_primitive_change_opacity(PrimitiveToolOperation &ptd, const wmEvent *event)
bool grease_pencil_draw_operator_begin(bContext *C, wmOperator *op, const bool use_duplicate_previous_key)
float opacity_from_input_sample(const float pressure, const Paint &paint, const Brush *brush, const BrushGpencilSettings *settings)
static int control_points_per_segment(const PrimitiveToolOperation &ptd)
static void grease_pencil_primitive_cancel_opacity(PrimitiveToolOperation &ptd)
static void primitive_calculate_curve_positions_2d(PrimitiveToolOperation &ptd, MutableSpan< float2 > new_positions)
float randomize_rotation(const BrushGpencilSettings &settings, const float stroke_factor, const float distance, const float pressure)
static constexpr float ui_tertiary_point_draw_size_px
void resize_single_curve(bke::CurvesGeometry &curves, const bool at_end, const int new_points_num)
static void grease_pencil_primitive_cancel(bContext *C, wmOperator *op)
static void grease_pencil_primitive_grab_update(PrimitiveToolOperation &ptd, const wmEvent *event)
static void grease_pencil_primitive_operator_update(PrimitiveToolOperation &ptd, const wmEvent *event)
static void grease_pencil_primitive_cancel_radius(PrimitiveToolOperation &ptd)
float randomize_opacity(const BrushGpencilSettings &settings, const float stroke_factor, const float distance, const float opacity, const float pressure)
static void GREASE_PENCIL_OT_primitive_circle(wmOperatorType *ot)
static float2 snap_diagonals_box(float2 p)
static constexpr int control_point_first
static float2 snap_8_angles(float2 p)
ColorGeometry4f randomize_color(const BrushGpencilSettings &settings, const std::optional< BrushColorJitterSettings > &jitter, const float stroke_hue_factor, const float stroke_saturation_factor, const float stroke_value_factor, const float distance, const ColorGeometry4f color, const float pressure)
static wmOperatorStatus grease_pencil_primitive_event_modal_map(bContext *C, wmOperator *op, PrimitiveToolOperation &ptd, const wmEvent *event)
static void grease_pencil_primitive_change_radius(PrimitiveToolOperation &ptd, const wmEvent *event)
float randomize_radius(const BrushGpencilSettings &settings, const float stroke_factor, const float distance, const float radius, const float pressure)
static void grease_pencil_primitive_drag_update(PrimitiveToolOperation &ptd, const wmEvent *event)
static void grease_pencil_primitive_init_radius(PrimitiveToolOperation &ptd)
static void control_point_colors_and_sizes(const PrimitiveToolOperation &ptd, MutableSpan< ColorGeometry4f > colors, MutableSpan< float > sizes)
static void grease_pencil_primitive_extruding_update(PrimitiveToolOperation &ptd, const wmEvent *event)
static int grease_pencil_primitive_curve_points_number(PrimitiveToolOperation &ptd)
static void grease_pencil_primitive_update_view(bContext *C, PrimitiveToolOperation &ptd)
static void grease_pencil_primitive_draw(const bContext *, ARegion *, void *arg)
static void draw_control_points(PrimitiveToolOperation &ptd)
static void grease_pencil_primitive_update_curves(PrimitiveToolOperation &ptd)
static float2 primitive_center_of_mass(const PrimitiveToolOperation &ptd)
static void grease_pencil_primitive_drag_all_update(PrimitiveToolOperation &ptd, const wmEvent *event)
static constexpr float ui_secondary_point_draw_size_px
bke::CurvesGeometry curves_merge_endpoints_by_distance(const ARegion &region, const bke::CurvesGeometry &src_curves, const float4x4 &layer_to_world, const float merge_distance, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
static constexpr float ui_point_hit_size_px
static constexpr int control_point_last
static constexpr float ui_point_max_hit_size_px
static void primitive_calculate_curve_positions(PrimitiveToolOperation &ptd, Span< float2 > control_points, MutableSpan< float2 > new_positions)
static void grease_pencil_primitive_init_curves(PrimitiveToolOperation &ptd)
static void grease_pencil_primitive_undo_curves(PrimitiveToolOperation &ptd)
static wmOperatorStatus grease_pencil_primitive_modal(bContext *C, wmOperator *op, const wmEvent *event)
static void GREASE_PENCIL_OT_primitive_line(wmOperatorType *ot)
static void grease_pencil_primitive_scale_all_update(PrimitiveToolOperation &ptd, const wmEvent *event)
static ControlPointType get_control_point_type(const PrimitiveToolOperation &ptd, const int point)
static constexpr float ui_primary_point_draw_size_px
static void grease_pencil_primitive_common_props(wmOperatorType *ot, const int default_subdiv, const PrimitiveType default_type)
static void grease_pencil_primitive_rotate_all_update(PrimitiveToolOperation &ptd, const wmEvent *event)
float4x2 calculate_texture_space(const Scene *scene, const ARegion *region, const float2 &mouse, const DrawingPlacement &placement)
static void grease_pencil_primitive_exit(bContext *C, wmOperator *op, const bool cancelled)
void add_single_curve(bke::greasepencil::Drawing &drawing, const bool at_end)
static void grease_pencil_primitive_cursor_update(bContext *C, PrimitiveToolOperation &ptd, const wmEvent *event)
static void grease_pencil_primitive_load(PrimitiveToolOperation &ptd)
static void grease_pencil_primitive_status_indicators(bContext *C, wmOperator *op, PrimitiveToolOperation &ptd)
static void GREASE_PENCIL_OT_primitive_polyline(wmOperatorType *ot)
static void GREASE_PENCIL_OT_primitive_curve(wmOperatorType *ot)
static float2 primitive_local_to_screen(const PrimitiveToolOperation &ptd, const float3 &point)
static void grease_pencil_primitive_save(PrimitiveToolOperation &ptd)
static float2 snap_diagonals(float2 p)
static void GREASE_PENCIL_OT_primitive_box(wmOperatorType *ot)
static wmOperatorStatus grease_pencil_primitive_invoke(bContext *C, wmOperator *op, const wmEvent *event)
float radius_from_input_sample(const RegionView3D *rv3d, const ARegion *region, const Paint &paint, const Brush *brush, const float pressure, const float3 &location, const float4x4 &to_world, const BrushGpencilSettings *settings)
static wmOperatorStatus grease_pencil_primitive_mouse_event(PrimitiveToolOperation &ptd, const wmEvent *event)
static void GREASE_PENCIL_OT_primitive_arc(wmOperatorType *ot)
static int primitive_check_ui_hover(const PrimitiveToolOperation &ptd, const wmEvent *event)
T cos(const AngleRadianBase< T > &a)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
T interpolate(const T &a, const T &b, const FactorT &t)
T atan2(const T &y, const T &x)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T sin(const AngleRadianBase< T > &a)
T mod(const T &a, const T &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void get_color_4fv(int colorid, float col[4])
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ GP_SCULPT_SETT_FLAG_PRIMITIVE_CURVE
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
ViewOpsData * ED_view3d_navigation_init(bContext *C, const wmKeyMapItem *kmi_merge)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void immAttr4f(uint attr_id, float x, float y, float z, float w)
void immEnd()
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition voxel.cc:16
ViewContext ED_view3d_viewcontext_init(bContext *C, Depsgraph *depsgraph)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
PointerRNA RNA_id_pointer_create(ID *id)
float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
GPUVertFormat * immVertexFormat()
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
MatBase< float, 2, 2 > float2x2
void WM_cursor_modal_set(wmWindow *win, int val)
void immBegin(GPUPrimType, uint vertex_len)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
float2 ED_view3d_project_float_v2_m4(const ARegion *region, const float co[3], const float4x4 &mat)
void BKE_brush_init_gpencil_settings(Brush *brush)
Definition brush.cc:719
int RNA_int_get(PointerRNA *ptr, const char *name)
wmWindow * CTX_wm_window(const bContext *C)
@ TH_GIZMO_SECONDARY
MatBase< float, 4, 4 > float4x4
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void immUnbindProgram()
bool ED_view3d_navigation_do(bContext *C, ViewOpsData *vod, const wmEvent *event, const float depth_loc_override[3])
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void BKE_curvemapping_init(CurveMapping *cumap)
@ GP_TOOL_FLAG_PAINT_ONBACK
@ GP_TOOL_FLAG_AUTOMERGE_STROKE
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
void ED_primitivetool_modal_keymap(wmKeyConfig *keyconf)
VecBase< float, 2 > float2
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR
MatBase< float, 4, 2 > float4x2
void WM_cursor_modal_restore(wmWindow *win)
void GPU_program_point_size(bool enable)
Definition gpu_state.cc:165
int BKE_object_material_index_get(Object *ob, const Material *ma)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
void immAttr1f(uint attr_id, float x)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void immVertex3fv(uint attr_id, const float data[3])
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
@ WM_CURSOR_HAND
Definition wm_cursors.hh:24
@ WM_CURSOR_NSEW_SCROLL
Definition wm_cursors.hh:54
@ WM_CURSOR_CROSS
Definition wm_cursors.hh:28
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
void ED_operatortypes_grease_pencil_primitives()
std::optional< BrushColorJitterSettings > BKE_brush_color_jitter_get_settings(const Paint *paint, const Brush *brush)
Definition brush.cc:1262
@ KM_RELEASE
Definition WM_types.hh:325
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
Brush * BKE_paint_brush(Paint *paint)
Material * BKE_grease_pencil_object_material_ensure_from_brush(Main *bmain, Object *ob, Brush *brush)
@ GPPAINT_FLAG_USE_VERTEXCOLOR
Scene * CTX_data_scene(const bContext *C)
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
void ED_view3d_navigation_free(bContext *C, ViewOpsData *vod)
VecBase< float, 3 > float3
@ GP_BRUSH_USE_STROKE
@ GP_BRUSH_GROUP_RANDOM
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
float RNA_float_get(PointerRNA *ptr, const char *name)
View3D * CTX_wm_view3d(const bContext *C)
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
signed char int8_t
Definition BKE_text.h:76
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::ARegionRuntime * runtime
struct CurveMapping * curve_sensitivity
struct CurveMapping * curve_jitter
struct CurveMapping * curve_rand_strength
struct CurveMapping * curve_rand_uv
struct CurveMapping * curve_rand_pressure
struct CurveMapping * curve_strength
struct CurveMapping * curve_rand_value
struct CurveMapping * curve_rand_saturation
struct BrushGpencilSettings * gpencil_settings
struct CurveMapping * curve_rand_hue
eGP_Sculpt_SettingsFlag flag
struct CurveMapping * cur_primitive
eRegionView3D_Flag rflag
struct RenderData r
struct ToolSettings * toolsettings
eGPencil_Flags gpencil_flags
struct GP_Sculpt_Settings gp_sculpt
RegionView3D * rv3d
Definition ED_view3d.hh:86
struct ARegionType * type
wmEventType type
Definition WM_types.hh:817
wmEventModifierFlag modifier
Definition WM_types.hh:834
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct wmOperatorType * type