Blender V5.3
sequencer_text_edit.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
8
9#include <cstddef>
10
11#include "DNA_sequence_types.h"
12
13#include "BLF_api.hh"
14
15#include "BLI_math_matrix.hh"
16#include "BLI_math_vector.hh"
17#include "BLI_string.hh"
18#include "BLI_string_utf8.hh"
19
20#include "BKE_context.hh"
21#include "BKE_scene.hh"
22
23#include "SEQ_effects.hh"
24#include "SEQ_relations.hh"
25#include "SEQ_select.hh"
26#include "SEQ_transform.hh"
27
28#include "WM_api.hh"
29
30#include "RNA_define.hh"
31
32#include "UI_view2d.hh"
33
34#include "ED_screen.hh"
35
36#include "sequencer_intern.hh"
37
38namespace blender::ed::vse {
39
40static bool text_insert(TextVars *data, const char *buf, size_t buf_len);
41
42/* -------------------------------------------------------------------- */
45
47{
49 return false;
50 }
51
53 return false;
54 }
55
56 const Scene *scene = CTX_data_sequencer_scene(C);
57 const Strip *strip = seq::select_active_get(scene);
58 if (strip == nullptr || strip->type != STRIP_TYPE_TEXT ||
59 !strip->intersects_frame(scene, BKE_scene_frame_get(scene)))
60 {
61 return false;
62 }
63
64 const TextVars *data = static_cast<TextVars *>(strip->effectdata);
65 if (data == nullptr || data->runtime == nullptr || !seq::effects_can_render_text(strip)) {
66 return false;
67 }
68
69 return true;
70}
71
73{
75 return false;
76 }
77
78 const Scene *scene = CTX_data_sequencer_scene(C);
79 const Strip *strip = seq::select_active_get(scene);
80
81 return (strip->flag & SEQ_FLAG_TEXT_EDITING_ACTIVE) != 0;
82}
83
85{
86 Strip *strip = seq::select_active_get(scene);
87 if (strip && (strip->type == STRIP_TYPE_TEXT) && (strip->flag & SEQ_FLAG_TEXT_EDITING_ACTIVE)) {
88 return strip;
89 }
90 return nullptr;
91}
92std::optional<int2> sequencer_text_editing_cursor_region_xy_get(const Scene *scene,
93 const ARegion *region,
94 const Strip *strip)
95{
97 const TextVars *data = static_cast<const TextVars *>(strip->effectdata);
98 if (strip->intersects_frame(scene, BKE_scene_frame_get(scene)) && data && data->runtime &&
99 !data->runtime->lines.is_empty())
100 {
101 const seq::TextVarsRuntime *runtime = data->runtime;
102 const int2 cursor_pos = strip_text_cursor_offset_to_position(runtime, data->cursor_offset);
103 float2 co = runtime->lines[cursor_pos.y].characters[cursor_pos.x].position;
104 co += float2(0.0f, float(runtime->font_descender));
105 co += float2(-scene->r.xsch / 2.0f, -scene->r.ysch / 2.0f);
107
108 int2 r;
109 ui::view2d_view_to_region(&region->v2d, co.x, co.y, &r.x, &r.y);
110 return r;
111 }
112 return std::nullopt;
113}
114
116
117/* -------------------------------------------------------------------- */
120
122{
123 cursor_offset = std::clamp(cursor_offset, 0, runtime->character_count);
124
125 int2 cursor_position{0, 0};
126 for (const seq::LineInfo &line : runtime->lines) {
127 if (cursor_offset < line.characters.size()) {
128 cursor_position.x = cursor_offset;
129 break;
130 }
131 cursor_offset -= line.characters.size();
132 cursor_position.y += 1;
133 }
134
135 cursor_position.y = std::clamp(cursor_position.y, 0, int(runtime->lines.size() - 1));
136 cursor_position.x = std::clamp(
137 cursor_position.x, 0, int(runtime->lines[cursor_position.y].characters.size() - 1));
138
139 return cursor_position;
140}
141
143 const int2 cursor_pos)
144{
145 return runtime->lines[cursor_pos.y].characters[cursor_pos.x];
146}
147
149 const int cursor_offset)
150{
151 const int2 cursor_pos = strip_text_cursor_offset_to_position(runtime, cursor_offset);
152 return character_at_cursor_pos_get(runtime, cursor_pos);
153}
154
155static int cursor_position_to_offset(const seq::TextVarsRuntime *runtime, int2 cursor_position)
156{
157 return character_at_cursor_pos_get(runtime, cursor_position).index;
158}
159
161{
162 data->selection_start_offset = 0;
163 data->selection_end_offset = 0;
164}
165
167{
168 /* Ensure, that selection start < selection end. */
169 int sel_start_offset = data->selection_start_offset;
170 int sel_end_offset = data->selection_end_offset;
171 if (sel_start_offset > sel_end_offset) {
172 std::swap(sel_start_offset, sel_end_offset);
173 }
174
175 return IndexRange(sel_start_offset, sel_end_offset - sel_start_offset);
176}
177
179{
181}
182
184{
185 std::scoped_lock runtime_lock(seq::text_runtime_mutex_get());
186 seq::text_effect_update_runtime(nullptr, text, text.runtime->image_size);
187 BLF_disable(text.runtime->font, BLF_BOLD | BLF_ITALIC);
188}
189
191{
192 if (!text_has_selection(data)) {
193 return;
194 }
195
196 seq::TextVarsRuntime *runtime = data->runtime;
198
199 seq::CharInfo char_start = character_at_cursor_offset_get(runtime, sel_range.first());
200 seq::CharInfo char_end = character_at_cursor_offset_get(runtime, sel_range.last());
201
202 const int offset_start = char_start.offset;
203 const int offset_end = char_end.offset + char_end.byte_length;
204 BLI_assert(offset_start >= 0 && offset_end <= data->text_len_bytes);
205 BLI_assert(offset_end >= 0 && offset_end <= data->text_len_bytes);
206 BLI_assert(offset_start <= offset_end);
207 const int remaining = data->text_len_bytes - offset_end;
208
209 std::memmove(data->text_ptr + offset_start, data->text_ptr + offset_end, remaining + 1);
210 data->text_len_bytes = offset_start + remaining;
211
212 const int2 sel_start = strip_text_cursor_offset_to_position(runtime, sel_range.first());
213 data->cursor_offset = cursor_position_to_offset(runtime, sel_start);
215
217}
218
225
227
228/* -------------------------------------------------------------------- */
231
233{
235 TextVars *data = static_cast<TextVars *>(strip->effectdata);
236 data->selection_start_offset = 0;
237 data->selection_end_offset = data->runtime->character_count;
239 return OPERATOR_FINISHED;
240}
241
243{
244 /* identifiers */
245 ot->name = "Select All";
246 ot->description = "Select all characters";
247 ot->idname = "SEQUENCER_OT_text_select_all";
248
249 /* API callbacks. */
252
253 /* flags */
254 ot->flag = OPTYPE_UNDO;
255}
256
258{
260 TextVars *data = static_cast<TextVars *>(strip->effectdata);
261
262 if (!text_has_selection(data)) {
263 /* Exit edit mode, so text can be translated by mouse. */
264 strip->flag &= ~SEQ_FLAG_TEXT_EDITING_ACTIVE;
265 }
266 else {
268 }
269
271 return OPERATOR_FINISHED;
272}
273
275{
276 /* identifiers */
277 ot->name = "Deselect All";
278 ot->description = "Deselect all characters";
279 ot->idname = "SEQUENCER_OT_text_deselect_all";
280
281 /* API callbacks. */
284
285 /* flags */
286 ot->flag = OPTYPE_UNDO;
287}
288
290
291/* -------------------------------------------------------------------- */
294
296{
297 const seq::TextVarsRuntime *runtime = data->runtime;
298 const IndexRange selection_range = strip_text_selection_range_get(data);
299 const seq::CharInfo start = character_at_cursor_offset_get(runtime, selection_range.first());
300 const seq::CharInfo end = character_at_cursor_offset_get(runtime, selection_range.last());
301
302 const int offset_start = start.offset;
303 const int offset_end = end.offset + end.byte_length;
304 BLI_assert(offset_start >= 0 && offset_start <= data->text_len_bytes);
305 BLI_assert(offset_end >= 0 && offset_end <= data->text_len_bytes);
306 BLI_assert(offset_start <= offset_end);
307
308 const size_t len = offset_end - offset_start;
309 char *buf = MEM_new_array_uninitialized<char>(len + 1, "text clipboard");
310 memcpy(buf, data->text_ptr + offset_start, len);
311 buf[len] = 0;
312 WM_clipboard_text_set(buf, false);
313 MEM_delete(buf);
314
316}
317
319{
321 TextVars *data = static_cast<TextVars *>(strip->effectdata);
322
323 if (!text_has_selection(data)) {
324 return OPERATOR_CANCELLED;
325 }
326
328
329 return OPERATOR_FINISHED;
330}
331
333{
334 /* identifiers */
335 ot->name = "Copy Text";
336 ot->description = "Copy text to clipboard";
337 ot->idname = "SEQUENCER_OT_text_edit_copy";
338
339 /* API callbacks. */
342
343 /* flags */
344 ot->flag = OPTYPE_UNDO;
345}
346
348
349/* -------------------------------------------------------------------- */
352
354{
356 TextVars *data = static_cast<TextVars *>(strip->effectdata);
357
358 if (!text_has_selection(data)) {
359 return OPERATOR_CANCELLED;
360 }
361
364
366 return OPERATOR_FINISHED;
367}
368
370{
371 /* identifiers */
372 ot->name = "Cut Text";
373 ot->description = "Cut text to clipboard";
374 ot->idname = "SEQUENCER_OT_text_edit_cut";
375
376 /* API callbacks. */
379
380 /* flags */
381 ot->flag = OPTYPE_UNDO;
382}
383
385
386/* -------------------------------------------------------------------- */
389
391{
393 TextVars *data = static_cast<TextVars *>(strip->effectdata);
394
395 int buf_len;
396 char *buf = WM_clipboard_text_get(false, true, &buf_len);
397
398 if (buf_len == 0) {
399 return OPERATOR_CANCELLED;
400 }
401
402 text_insert(data, buf, buf_len);
403
404 MEM_delete(buf);
405
407 return OPERATOR_FINISHED;
408}
409
411{
412 /* identifiers */
413 ot->name = "Paste Text";
414 ot->description = "Paste text from clipboard";
415 ot->idname = "SEQUENCER_OT_text_edit_paste";
416
417 /* API callbacks. */
420
421 /* flags */
422 ot->flag = OPTYPE_UNDO;
423}
424
426
427/* -------------------------------------------------------------------- */
430
431enum {
442};
443
445 {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
446 {LINE_END, "LINE_END", 0, "Line End", ""},
447 {TEXT_BEGIN, "TEXT_BEGIN", 0, "Text Begin", ""},
448 {TEXT_END, "TEXT_END", 0, "Text End", ""},
449 {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
450 {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
451 {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
452 {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
453 {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""},
454 {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""},
455 {0, nullptr, 0, nullptr, nullptr},
456};
457
458static int2 cursor_move_by_character(int2 cursor_position,
459 const seq::TextVarsRuntime *runtime,
460 int offset)
461{
462 const seq::LineInfo &cur_line = runtime->lines[cursor_position.y];
463 /* Move to next line. */
464 if (cursor_position.x + offset > cur_line.characters.size() - 1 &&
465 cursor_position.y < runtime->lines.size() - 1)
466 {
467 cursor_position.x = 0;
468 cursor_position.y++;
469 }
470 /* Move to previous line. */
471 else if (cursor_position.x + offset < 0 && cursor_position.y > 0) {
472 cursor_position.y--;
473 cursor_position.x = runtime->lines[cursor_position.y].characters.size() - 1;
474 }
475 else {
476 cursor_position.x += offset;
477 const int position_max = runtime->lines[cursor_position.y].characters.size() - 1;
478 cursor_position.x = std::clamp(cursor_position.x, 0, position_max);
479 }
480 return cursor_position;
481}
482
483static int2 cursor_move_by_line(int2 cursor_position,
484 const seq::TextVarsRuntime *runtime,
485 int offset)
486{
487 const seq::LineInfo &cur_line = runtime->lines[cursor_position.y];
488 const int cur_pos_x = cur_line.characters[cursor_position.x].position.x;
489
490 const int line_max = runtime->lines.size() - 1;
491 const int new_line_index = std::clamp(cursor_position.y + offset, 0, line_max);
492 const seq::LineInfo &new_line = runtime->lines[new_line_index];
493
494 if (cursor_position.y == new_line_index) {
495 return cursor_position;
496 }
497
498 /* Find character in another line closest to current position. */
499 int best_distance = std::numeric_limits<int>::max();
500 int best_character_index = 0;
501
502 for (int i : new_line.characters.index_range()) {
503 seq::CharInfo character = new_line.characters[i];
504 const int distance = std::abs(character.position.x - cur_pos_x);
505 if (distance < best_distance) {
506 best_distance = distance;
507 best_character_index = i;
508 }
509 }
510
511 cursor_position.x = best_character_index;
512 cursor_position.y = new_line_index;
513 return cursor_position;
514}
515
516static int2 cursor_move_line_end(int2 cursor_position, const seq::TextVarsRuntime *runtime)
517{
518 const seq::LineInfo &cur_line = runtime->lines[cursor_position.y];
519 cursor_position.x = cur_line.characters.size() - 1;
520 return cursor_position;
521}
522
523static bool is_whitespace_transition(char chr1, char chr2)
524{
525 return ELEM(chr1, ' ', '\t', '\n') && !ELEM(chr2, ' ', '\t', '\n');
526}
527
528static int2 cursor_move_prev_word(int2 cursor_position,
529 const seq::TextVarsRuntime *runtime,
530 const char *text_ptr)
531{
532 cursor_position = cursor_move_by_character(cursor_position, runtime, -1);
533
534 while (cursor_position.x > 0 || cursor_position.y > 0) {
535 const seq::CharInfo character = character_at_cursor_pos_get(runtime, cursor_position);
536 const int2 prev_cursor_pos = cursor_move_by_character(cursor_position, runtime, -1);
537 const seq::CharInfo prev_character = character_at_cursor_pos_get(runtime, prev_cursor_pos);
538
539 if (is_whitespace_transition(text_ptr[prev_character.offset], text_ptr[character.offset])) {
540 break;
541 }
542 cursor_position = prev_cursor_pos;
543 }
544 return cursor_position;
545}
546
547static int2 cursor_move_next_word(int2 cursor_position,
548 const seq::TextVarsRuntime *runtime,
549 const char *text_ptr)
550{
551 const int maxline = runtime->lines.size() - 1;
552 const int maxchar = runtime->lines.last().characters.size() - 1;
553
554 while ((cursor_position.x < maxchar) || (cursor_position.y < maxline)) {
555 const seq::CharInfo character = character_at_cursor_pos_get(runtime, cursor_position);
556 cursor_position = cursor_move_by_character(cursor_position, runtime, 1);
557 const seq::CharInfo next_character = character_at_cursor_pos_get(runtime, cursor_position);
558
559 if (is_whitespace_transition(text_ptr[next_character.offset], text_ptr[character.offset])) {
560 break;
561 }
562 }
563 return cursor_position;
564}
565
567{
569 TextVars *data = static_cast<TextVars *>(strip->effectdata);
570 const seq::TextVarsRuntime *runtime = data->runtime;
571
572 if (RNA_boolean_get(op->ptr, "select_text") && !text_has_selection(data)) {
573 data->selection_start_offset = data->cursor_offset;
574 }
575
576 int2 cursor_position = strip_text_cursor_offset_to_position(runtime, data->cursor_offset);
577
578 switch (RNA_enum_get(op->ptr, "type")) {
579 case PREV_CHAR:
580 cursor_position = cursor_move_by_character(cursor_position, runtime, -1);
581 break;
582 case NEXT_CHAR:
583 cursor_position = cursor_move_by_character(cursor_position, runtime, 1);
584 break;
585 case PREV_LINE:
586 cursor_position = cursor_move_by_line(cursor_position, runtime, -1);
587 break;
588 case NEXT_LINE:
589 cursor_position = cursor_move_by_line(cursor_position, runtime, 1);
590 break;
591 case LINE_BEGIN:
592 cursor_position.x = 0;
593 break;
594 case LINE_END:
595 cursor_position = cursor_move_line_end(cursor_position, runtime);
596 break;
597 case TEXT_BEGIN:
598 cursor_position = {0, 0};
599 break;
600 case TEXT_END:
601 cursor_position.y = runtime->lines.size() - 1;
602 cursor_position = cursor_move_line_end(cursor_position, runtime);
603 break;
604 case PREV_WORD:
605 cursor_position = cursor_move_prev_word(cursor_position, runtime, data->text_ptr);
606 break;
607 case NEXT_WORD:
608 cursor_position = cursor_move_next_word(cursor_position, runtime, data->text_ptr);
609 break;
610 }
611
612 data->cursor_offset = cursor_position_to_offset(runtime, cursor_position);
613 if (RNA_boolean_get(op->ptr, "select_text")) {
614 data->selection_end_offset = data->cursor_offset;
615 }
616
617 if (!RNA_boolean_get(op->ptr, "select_text") ||
618 data->cursor_offset == data->selection_start_offset)
619 {
621 }
622
624 return OPERATOR_FINISHED;
625}
626
628{
629 /* identifiers */
630 ot->name = "Move Cursor";
631 ot->description = "Move cursor in text";
632 ot->idname = "SEQUENCER_OT_text_cursor_move";
633
634 /* API callbacks. */
637
638 /* flags */
640
641 /* properties */
642 RNA_def_enum(ot->srna,
643 "type",
646 "Type",
647 "Where to move cursor to, to make a selection");
648
650 ot->srna, "select_text", false, "Select Text", "Select text while moving cursor");
652}
653
655
656/* -------------------------------------------------------------------- */
659
662 {DEL_NEXT_SEL, "NEXT_OR_SELECTION", 0, "Next or Selection", ""},
663 {DEL_PREV_SEL, "PREVIOUS_OR_SELECTION", 0, "Previous or Selection", ""},
664 {0, nullptr, 0, nullptr, nullptr},
665};
666
667static void delete_character(const seq::CharInfo character, TextVars *data)
668{
669 const int offset_start = character.offset;
670 const int offset_end = character.offset + character.byte_length;
671 BLI_assert(offset_start >= 0 && offset_start <= data->text_len_bytes);
672 BLI_assert(offset_end >= 0 && offset_end <= data->text_len_bytes);
673 const int remaining = data->text_len_bytes - offset_end + 1;
674 std::memmove(data->text_ptr + offset_start, data->text_ptr + offset_end, remaining);
675 data->text_len_bytes -= character.byte_length;
676 BLI_assert(data->text_len_bytes >= 0);
677}
678
680{
681#ifdef WITH_INPUT_IME
682 if (const std::optional<wmOperatorStatus> status = WM_operator_IME_edit_maybe(C)) {
683 return *status;
684 }
685#endif
686
688 TextVars *data = static_cast<TextVars *>(strip->effectdata);
689 const seq::TextVarsRuntime *runtime = data->runtime;
690 const int type = RNA_enum_get(op->ptr, "type");
691
695 return OPERATOR_FINISHED;
696 }
697
698 if (type == DEL_NEXT_SEL) {
699 if (data->cursor_offset >= runtime->character_count) {
700 return OPERATOR_CANCELLED;
701 }
702
703 delete_character(character_at_cursor_offset_get(runtime, data->cursor_offset), data);
704 }
705 if (type == DEL_PREV_SEL) {
706 if (data->cursor_offset == 0) {
707 return OPERATOR_CANCELLED;
708 }
709
710 delete_character(character_at_cursor_offset_get(runtime, data->cursor_offset - 1), data);
711 data->cursor_offset -= 1;
712 }
713
716 return OPERATOR_FINISHED;
717}
718
720{
721 /* identifiers */
722 ot->name = "Delete Text";
723 ot->description = "Delete text at cursor position";
724 ot->idname = "SEQUENCER_OT_text_delete";
725
726 /* API callbacks. */
729
730 /* flags */
731 ot->flag = OPTYPE_UNDO;
732
733 /* properties */
734 RNA_def_enum(ot->srna,
735 "type",
738 "Type",
739 "Which part of the text to delete");
740}
741
743
744/* -------------------------------------------------------------------- */
747
748static bool text_insert(TextVars *data, const char *buf, const size_t buf_len)
749{
750 BLI_assert(strlen(buf) == buf_len);
751
753
754 size_t needed_size = data->text_len_bytes + buf_len + 1;
755 char *new_text = MEM_new_array_uninitialized<char>(needed_size, "text");
756
757 const seq::CharInfo cur_char = character_at_cursor_offset_get(data->runtime,
758 data->cursor_offset);
759 BLI_assert(cur_char.offset >= 0 && cur_char.offset <= data->text_len_bytes);
760 std::memcpy(new_text, data->text_ptr, cur_char.offset);
761 std::memcpy(new_text + cur_char.offset, buf, buf_len);
762 std::memcpy(new_text + cur_char.offset + buf_len,
763 data->text_ptr + cur_char.offset,
764 data->text_len_bytes - cur_char.offset + 1);
765 data->text_len_bytes += buf_len;
766 MEM_delete(data->text_ptr);
767 data->text_ptr = new_text;
768
769 data->cursor_offset += BLI_strlen_utf8(buf);
770
772 return true;
773}
774
776{
778 TextVars *data = static_cast<TextVars *>(strip->effectdata);
779
780 char str[512];
781 RNA_string_get(op->ptr, "string", str);
782
783 const size_t in_buf_len = STRNLEN(str);
784 if (in_buf_len == 0) {
786 }
787
788 if (!text_insert(data, str, in_buf_len)) {
789 return OPERATOR_CANCELLED;
790 }
791
793 return OPERATOR_FINISHED;
794}
795
797 wmOperator *op,
798 const wmEvent *event)
799{
800#ifdef WITH_INPUT_IME
801 if (const std::optional<wmOperatorStatus> status = WM_operator_IME_insert_maybe(
802 C, op, event, "string"))
803 {
804 return *status;
805 }
806#endif
807
808 char str[6];
810 RNA_string_set(op->ptr, "string", str);
811 return sequencer_text_insert_exec(C, op);
812}
813
815{
816 /* identifiers */
817 ot->name = "Insert Text";
818 ot->description = "Insert text at cursor position";
819 ot->idname = "SEQUENCER_OT_text_insert";
820
821 /* API callbacks. */
825
826 /* flags */
827 ot->flag = OPTYPE_UNDO;
828
829 /* properties */
831 ot->srna, "string", nullptr, 512, "String", "String to be inserted at cursor position");
832}
833
835
836/* -------------------------------------------------------------------- */
839
841{
843 TextVars *data = static_cast<TextVars *>(strip->effectdata);
844
845 if (!text_insert(data, "\n", 1)) {
846 return OPERATOR_CANCELLED;
847 }
848
850 return OPERATOR_FINISHED;
851}
852
854{
855 /* identifiers */
856 ot->name = "Insert Line Break";
857 ot->description = "Insert line break at cursor position";
858 ot->idname = "SEQUENCER_OT_text_line_break";
859
860 /* API callbacks. */
863
864 /* flags */
865 ot->flag = OPTYPE_UNDO;
866}
867
869
870/* -------------------------------------------------------------------- */
873
874static int find_closest_cursor_offset(const TextVars *data, float2 mouse_loc)
875{
876 const seq::TextVarsRuntime *runtime = data->runtime;
877 int best_cursor_offset = 0;
878 float best_distance = std::numeric_limits<float>::max();
879
880 for (const seq::LineInfo &line : runtime->lines) {
881 for (const seq::CharInfo &character : line.characters) {
882 const float distance = math::distance(mouse_loc, character.position);
883 if (distance < best_distance) {
884 best_distance = distance;
885 best_cursor_offset = character.index;
886 }
887 }
888 }
889
890 return best_cursor_offset;
891}
892
893static void cursor_set_by_mouse_position(const bContext *C, const wmEvent *event)
894{
895 const Scene *scene = CTX_data_sequencer_scene(C);
896 const Strip *strip = seq::select_active_get(scene);
897 TextVars *data = static_cast<TextVars *>(strip->effectdata);
898 const View2D *v2d = ui::view2d_fromcontext(C);
899
900 int2 mval_region;
901 WM_event_drag_start_mval(event, CTX_wm_region(C), mval_region);
902 float2 mouse_loc;
903 ui::view2d_region_to_view(v2d, mval_region.x, mval_region.y, &mouse_loc.x, &mouse_loc.y);
904
905 /* Convert cursor coordinates to domain of CharInfo::position. */
906 const float2 view_offs{-scene->r.xsch / 2.0f, -scene->r.ysch / 2.0f};
908
909 mouse_loc = math::transform_point(math::invert(transform_mat), mouse_loc);
910 mouse_loc -= view_offs;
911 data->cursor_offset = find_closest_cursor_offset(data, float2(mouse_loc));
912}
913
915 wmOperator * /*op*/,
916 const wmEvent *event)
917{
918 const Scene *scene = CTX_data_sequencer_scene(C);
919 const Strip *strip = seq::select_active_get(scene);
920 TextVars *data = static_cast<TextVars *>(strip->effectdata);
921 bool make_selection = false;
922
923 switch (event->type) {
924 case LEFTMOUSE:
925 if (event->val == KM_RELEASE) {
927 if (make_selection) {
928 data->selection_end_offset = data->cursor_offset;
929 }
930 return OPERATOR_FINISHED;
931 }
932 break;
933 case MIDDLEMOUSE:
934 case RIGHTMOUSE:
935 return OPERATOR_FINISHED;
936 case MOUSEMOVE:
937 make_selection = true;
938 if (!text_has_selection(data)) {
939 data->selection_start_offset = data->cursor_offset;
940 }
942 data->selection_end_offset = data->cursor_offset;
943 break;
944 default: {
945 break;
946 }
947 }
948
951}
952
954 wmOperator *op,
955 const wmEvent *event)
956{
957 const Scene *scene = CTX_data_sequencer_scene(C);
958 Strip *strip = seq::select_active_get(scene);
959 TextVars *data = static_cast<TextVars *>(strip->effectdata);
960 const View2D *v2d = ui::view2d_fromcontext(C);
961
962 int2 mval_region;
963 WM_event_drag_start_mval(event, CTX_wm_region(C), mval_region);
964 float2 mouse_loc;
965 ui::view2d_region_to_view(v2d, mval_region.x, mval_region.y, &mouse_loc.x, &mouse_loc.y);
966
967 if (!strip_point_image_isect(scene, strip, mouse_loc)) {
968 strip->flag &= ~SEQ_FLAG_TEXT_EDITING_ACTIVE;
970 }
971
974
978}
979
981{
982 /* identifiers */
983 ot->name = "Set Cursor";
984 ot->description = "Set cursor position in text";
985 ot->idname = "SEQUENCER_OT_text_cursor_set";
986
987 /* API callbacks. */
991
992 /* flags */
994
995 /* properties */
996
998 ot->srna, "select_text", false, "Select Text", "Select text while moving cursor");
1000}
1001
1003
1004/* -------------------------------------------------------------------- */
1007
1009{
1011 if ((strip->flag & SEQ_FLAG_TEXT_EDITING_ACTIVE) != 0) {
1012 strip->flag &= ~SEQ_FLAG_TEXT_EDITING_ACTIVE;
1013 }
1014 else {
1015 strip->flag |= SEQ_FLAG_TEXT_EDITING_ACTIVE;
1016 }
1017
1019 return OPERATOR_FINISHED;
1020}
1021
1023{
1024 /* identifiers */
1025 ot->name = "Edit Text";
1026 ot->description = "Toggle text editing";
1027 ot->idname = "SEQUENCER_OT_text_edit_mode_toggle";
1028
1029 /* API callbacks. */
1032
1033 /* flags */
1034 ot->flag = OPTYPE_UNDO;
1035}
1036
1038
1039} // namespace blender::ed::vse
#define STRNLEN(str)
#define C
Definition RandGen.cpp:29
#define ND_SEQUENCER
Definition WM_types.hh:456
#define NC_SCENE
Definition WM_types.hh:391
#define ELEM(...)
constexpr int64_t first() const
constexpr int64_t last(const int64_t n=0) const
constexpr bool is_empty() const
constexpr IndexRange()=default
#define str(s)
float distance(float, float) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
IndexRange strip_text_selection_range_get(const TextVars *data)
static void delete_character(const seq::CharInfo character, TextVars *data)
void SEQUENCER_OT_text_edit_copy(wmOperatorType *ot)
void SEQUENCER_OT_text_line_break(wmOperatorType *ot)
static void text_runtime_update(TextVars &text)
static void cursor_set_by_mouse_position(const bContext *C, const wmEvent *event)
static wmOperatorStatus sequencer_text_insert_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void SEQUENCER_OT_text_delete(wmOperatorType *ot)
void SEQUENCER_OT_text_edit_cut(wmOperatorType *ot)
static const EnumPropertyItem move_type_items[]
bool sequencer_text_editing_active_poll(bContext *C)
static wmOperatorStatus sequencer_text_edit_mode_toggle_exec(bContext *C, wmOperator *)
static int2 cursor_move_by_line(int2 cursor_position, const seq::TextVarsRuntime *runtime, int offset)
static const EnumPropertyItem delete_type_items[]
static wmOperatorStatus sequencer_text_edit_paste_exec(bContext *C, wmOperator *)
void SEQUENCER_OT_text_insert(wmOperatorType *ot)
static int cursor_position_to_offset(const seq::TextVarsRuntime *runtime, int2 cursor_position)
void SEQUENCER_OT_text_deselect_all(wmOperatorType *ot)
bool sequencer_editing_initialized_and_active(bContext *C)
static const seq::CharInfo & character_at_cursor_pos_get(const seq::TextVarsRuntime *runtime, const int2 cursor_pos)
static wmOperatorStatus sequencer_text_edit_copy_exec(bContext *C, wmOperator *)
int2 strip_text_cursor_offset_to_position(const seq::TextVarsRuntime *runtime, int cursor_offset)
static void text_editing_update(const bContext *C)
static const seq::CharInfo & character_at_cursor_offset_get(const seq::TextVarsRuntime *runtime, const int cursor_offset)
void SEQUENCER_OT_text_select_all(wmOperatorType *ot)
static wmOperatorStatus sequencer_text_deselect_all_exec(bContext *C, wmOperator *)
static bool text_insert(TextVars *data, const char *buf, size_t buf_len)
static bool sequencer_text_editing_poll(bContext *C)
static wmOperatorStatus sequencer_text_cursor_move_exec(bContext *C, wmOperator *op)
static wmOperatorStatus sequencer_text_cursor_set_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus sequencer_text_delete_exec(bContext *C, wmOperator *op)
static int2 cursor_move_by_character(int2 cursor_position, const seq::TextVarsRuntime *runtime, int offset)
static void text_selection_cancel(TextVars *data)
static bool is_whitespace_transition(char chr1, char chr2)
static wmOperatorStatus sequencer_text_insert_exec(bContext *C, wmOperator *op)
void SEQUENCER_OT_text_edit_paste(wmOperatorType *ot)
static void delete_selected_text(TextVars *data)
static int2 cursor_move_next_word(int2 cursor_position, const seq::TextVarsRuntime *runtime, const char *text_ptr)
static int2 cursor_move_line_end(int2 cursor_position, const seq::TextVarsRuntime *runtime)
bool strip_point_image_isect(const Scene *scene, const Strip *strip, float point_view[2])
static bool text_has_selection(const TextVars *data)
void SEQUENCER_OT_text_cursor_move(wmOperatorType *ot)
static int find_closest_cursor_offset(const TextVars *data, float2 mouse_loc)
static wmOperatorStatus sequencer_text_edit_cut_exec(bContext *C, wmOperator *)
static int2 cursor_move_prev_word(int2 cursor_position, const seq::TextVarsRuntime *runtime, const char *text_ptr)
static wmOperatorStatus sequencer_text_select_all_exec(bContext *C, wmOperator *)
const Strip * sequencer_text_editing_cursor_strip_get(const Scene *scene)
static wmOperatorStatus sequencer_text_line_break_exec(bContext *C, wmOperator *)
std::optional< int2 > sequencer_text_editing_cursor_region_xy_get(const Scene *scene, const ARegion *region, const Strip *strip)
void SEQUENCER_OT_text_cursor_set(wmOperatorType *ot)
static void text_edit_copy(TextVars *data)
void SEQUENCER_OT_text_edit_mode_toggle(wmOperatorType *ot)
static wmOperatorStatus sequencer_text_cursor_set_modal(bContext *C, wmOperator *, const wmEvent *event)
T distance(const T &a, const T &b)
CartesianBasis invert(const CartesianBasis &basis)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
std::recursive_mutex & text_runtime_mutex_get()
void relations_invalidate_cache_raw(Scene *scene, Strip *strip)
void text_effect_update_runtime(const RenderData *context, TextVars &text, const int2 image_size)
float3x3 image_transform_matrix_get(const Scene *scene, const Strip *strip)
Strip * select_active_get(const Scene *scene)
bool effects_can_render_text(const Strip *strip)
void view2d_view_to_region(const View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL()
Definition view2d.cc:1705
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
View2D * view2d_fromcontext(const bContext *C)
Definition view2d.cc:1836
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
Scene * CTX_data_sequencer_scene(const bContext *C)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void BLF_disable(int fontid, FontFlags flag)
Definition blf.cc:331
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
float BKE_scene_frame_get(const Scene *scene)
Definition scene.cc:2595
BMesh const char void * data
wmWindowManager * CTX_wm_manager(const bContext *C)
@ SEQ_FLAG_TEXT_EDITING_ACTIVE
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
MatBase< float, 3, 3 > float3x3
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void WM_event_drag_start_mval(const wmEvent *event, const ARegion *region, int r_mval[2])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
bScreen * ED_screen_animation_no_scrub(const wmWindowManager *wm)
char * WM_clipboard_text_get(bool selection, bool ensure_utf8, int *r_len)
void WM_clipboard_text_set(const char *buf, bool selection)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ KM_RELEASE
Definition WM_types.hh:325
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
size_t BLI_strlen_utf8(const char *strc) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
ARegion * CTX_wm_region(const bContext *C)
@ BLF_ITALIC
Definition BLF_enums.hh:49
struct RenderData r
Vector< CharInfo > characters
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
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