[Bf-blender-cvs] [8cd76bff475] soc-2022-text-usability: UI: Select regions of text and toggle its style

Yash Dabhade noreply at git.blender.org
Thu Aug 18 08:33:01 CEST 2022


Commit: 8cd76bff475c5f6bc95e4df28c8003907eb0af56
Author: Yash Dabhade
Date:   Thu Aug 18 12:02:18 2022 +0530
Branches: soc-2022-text-usability
https://developer.blender.org/rB8cd76bff475c5f6bc95e4df28c8003907eb0af56

UI: Select regions of text and toggle its style

1)Text can be selected along the mouse pointer by dragging the cursor .
2) After the selection we can toggle the style of the selected region.

===================================================================

M	release/scripts/presets/keyconfig/keymap_data/blender_default.py
M	release/scripts/startup/bl_ui/properties_data_curve.py
M	source/blender/blenkernel/intern/vfont.c
M	source/blender/editors/curve/curve_intern.h
M	source/blender/editors/curve/curve_ops.c
M	source/blender/editors/curve/editfont.c
M	source/blender/makesrna/intern/rna_curve.c

===================================================================

diff --git a/release/scripts/presets/keyconfig/keymap_data/blender_default.py b/release/scripts/presets/keyconfig/keymap_data/blender_default.py
index 7d0929ef28f..519f6b4dac4 100644
--- a/release/scripts/presets/keyconfig/keymap_data/blender_default.py
+++ b/release/scripts/presets/keyconfig/keymap_data/blender_default.py
@@ -5576,6 +5576,8 @@ def km_font(params):
          {"properties": [("delta", 1)]}),
         ("font.change_character", {"type": 'DOWN_ARROW', "value": 'PRESS', "alt": True, "repeat": True},
          {"properties": [("delta", -1)]}),
+        ("font.selection_set", {"type": 'LEFTMOUSE', "value": 'PRESS',"ctrl":True,"alt":True}, None),
+        ("font.select_word", {"type": 'LEFTMOUSE', "value": 'DOUBLE_CLICK',"ctrl":True,"alt":True}, None),
         ("font.select_all", {"type": 'A', "value": 'PRESS', "ctrl": True}, None),
         ("font.text_copy", {"type": 'C', "value": 'PRESS', "ctrl": True}, None),
         ("font.text_cut", {"type": 'X', "value": 'PRESS', "ctrl": True}, None),
diff --git a/release/scripts/startup/bl_ui/properties_data_curve.py b/release/scripts/startup/bl_ui/properties_data_curve.py
index 4da7cd0283b..e986afd2f3d 100644
--- a/release/scripts/startup/bl_ui/properties_data_curve.py
+++ b/release/scripts/startup/bl_ui/properties_data_curve.py
@@ -357,12 +357,17 @@ class DATA_PT_font(CurveButtonsPanelText, Panel):
 
         if mode == 'EDIT_TEXT':
             layout.separator()
-
-            row = layout.row(align=True)
-            row.prop(char, "use_bold", toggle=True)
-            row.prop(char, "use_italic", toggle=True)
-            row.prop(char, "use_underline", toggle=True)
-            row.prop(char, "use_small_caps", toggle=True)
+            if text.is_selected == False :
+                row = layout.row(align=True)
+                row.prop(char, "use_bold", toggle=True)
+                row.prop(char, "use_italic", toggle=True)
+                row.prop(char, "use_underline", toggle=True)
+                row.prop(char, "use_small_caps", toggle=True)
+            else:
+                layout.operator("font.style_toggle", text="Bold", icon='BOLD' , depress = text.select_is_bold).style = 'BOLD'
+                layout.operator("font.style_toggle", text="Italic", icon='ITALIC' , depress = text.select_is_italics).style = 'ITALIC'
+                layout.operator("font.style_toggle", text="Underline", icon='UNDERLINE' , depress = text.select_is_underline).style = 'UNDERLINE'
+                layout.operator("font.style_toggle", text="Small Caps", icon='SMALL_CAPS' , depress = text.select_is_smallcaps).style = 'SMALL_CAPS'
 
 
 class DATA_PT_font_transform(CurveButtonsPanelText, Panel):
diff --git a/source/blender/blenkernel/intern/vfont.c b/source/blender/blenkernel/intern/vfont.c
index 9a6f861eae8..b1fd3d3c0e8 100644
--- a/source/blender/blenkernel/intern/vfont.c
+++ b/source/blender/blenkernel/intern/vfont.c
@@ -873,8 +873,10 @@ static bool vfont_to_curve(Object *ob,
     if (BKE_vfont_select_get(ob, &selstart, &selend)) {
       ef->selboxes_len = (selend - selstart) + 1;
       ef->selboxes = MEM_calloc_arrayN(ef->selboxes_len, sizeof(EditFontSelBox), "font selboxes");
+      ef->is_selected= true;
     }
     else {
+      ef->is_selected=false;
       ef->selboxes_len = 0;
       ef->selboxes = NULL;
     }
@@ -906,6 +908,8 @@ static bool vfont_to_curve(Object *ob,
   }
 
   i = 0;
+  int ret=0;
+  float min_dist=0.0f;
   while (i <= slen) {
     /* Characters in the list */
     info = &custrinfo[i];
@@ -1120,9 +1124,36 @@ static bool vfont_to_curve(Object *ob,
       }
     }
     ct++;
+    if (ef != NULL && ob != NULL){
+      float fx=ef->m_loc[0];
+      float fy=ef->m_loc[1];
+      float di= (fx-xof)*(fx-xof)+(fy-yof)*(fy-yof);
+      if(i==0)
+      {
+        min_dist=di;
+        ret=i;
+      }
+      if(di<min_dist)
+      {
+        min_dist=di;
+        ret=i;
+      } 
+    }
     i++;
   }
-
+  if (ef != NULL && ob != NULL)
+  {
+    if(ret==0)
+    {
+      ret++;
+      ef->m_pos=ret;
+    }
+    else if(ret>0 && ret<=ef->len-1)
+    {
+      ret++;
+      ef->m_pos=ret;
+    }
+  }
   current_line_length += xof + twidth - MARGIN_X_MIN;
   longest_line_length = MAX2(current_line_length, longest_line_length);
 
@@ -1135,7 +1166,27 @@ static bool vfont_to_curve(Object *ob,
     }
   }
 
-  /* Line-data is now: width of line. */
+  bool is_bold=true;
+  bool is_italics=true;
+  bool is_underline=true;
+  bool is_smallcaps=true;
+  for(int k=selstart;k<=selend;k++)
+  {
+
+    info = &custrinfo[k];
+    is_bold = info->flag & CU_CHINFO_BOLD ? is_bold : false ;
+    is_italics = info->flag & CU_CHINFO_ITALIC ? is_italics : false ; 
+    is_underline = info->flag & CU_CHINFO_UNDERLINE ? is_underline : false ;
+    is_smallcaps = info->flag & CU_CHINFO_SMALLCAPS ? is_smallcaps : false ;  
+  }
+
+    if (ef != NULL && ob != NULL) {
+      ef->select_is_underline=is_underline;
+      ef->select_is_bold=is_bold;
+      ef->select_is_italics=is_italics;
+      ef->select_is_smallcaps=is_smallcaps;
+    }
+  /*TODO: Store the state of the text in one variable using flags*/
 
   if (cu->spacemode != CU_ALIGN_X_LEFT) {
     ct = chartransdata;
diff --git a/source/blender/editors/curve/curve_intern.h b/source/blender/editors/curve/curve_intern.h
index 38fe2d92772..fe456515625 100644
--- a/source/blender/editors/curve/curve_intern.h
+++ b/source/blender/editors/curve/curve_intern.h
@@ -68,6 +68,9 @@ bool select_bpoint(BPoint *bp, bool selstatus, uint8_t flag, bool hidden);
 void FONT_OT_text_insert(struct wmOperatorType *ot);
 void FONT_OT_line_break(struct wmOperatorType *ot);
 
+void FONT_OT_selection_set(struct wmOperatorType *ot);
+void FONT_OT_select_word(struct wmOperatorType *ot);
+
 void FONT_OT_case_toggle(struct wmOperatorType *ot);
 void FONT_OT_case_set(struct wmOperatorType *ot);
 void FONT_OT_style_toggle(struct wmOperatorType *ot);
diff --git a/source/blender/editors/curve/curve_ops.c b/source/blender/editors/curve/curve_ops.c
index 2417947a25e..6209bfa93c6 100644
--- a/source/blender/editors/curve/curve_ops.c
+++ b/source/blender/editors/curve/curve_ops.c
@@ -28,6 +28,9 @@ void ED_operatortypes_curve(void)
   WM_operatortype_append(FONT_OT_text_insert);
   WM_operatortype_append(FONT_OT_line_break);
 
+  WM_operatortype_append(FONT_OT_selection_set);
+  WM_operatortype_append(FONT_OT_select_word);
+
   WM_operatortype_append(FONT_OT_case_toggle);
   WM_operatortype_append(FONT_OT_case_set);
   WM_operatortype_append(FONT_OT_style_toggle);
diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c
index 611dbb2e80c..c45c6887279 100644
--- a/source/blender/editors/curve/editfont.c
+++ b/source/blender/editors/curve/editfont.c
@@ -17,6 +17,7 @@
 #include "BLI_math.h"
 #include "BLI_string_cursor_utf8.h"
 #include "BLI_utildefines.h"
+#include "BLI_math_geom.h"
 
 #include "DNA_curve_types.h"
 #include "DNA_object_types.h"
@@ -1755,6 +1756,116 @@ void FONT_OT_text_insert(wmOperatorType *ot)
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Font Select Operator
+ * \{ */
+static void font_cursor_set_apply(bContext *C, wmOperator *op, const wmEvent *event)
+{
+  Object *obedit = CTX_data_active_object(C);
+  Curve *cu = obedit->data;
+  EditFont *ef = cu->editfont;
+  ARegion *region = CTX_wm_region(C);
+  float rout[3];
+  float mal[2];
+  mal[0]=event->mval[0];
+  mal[1]=event->mval[1];
+  const float *co = obedit->obmat[3];
+  const float *no = obedit->obmat[2]; /* Z axis. */
+  float plane[4];
+  plane_from_point_normal_v3(plane, co, no);
+  ED_view3d_win_to_3d_on_plane(region,plane,mal,true,rout);
+  mul_m4_v3(obedit->imat, rout);
+  ef->m_loc[0]=rout[0];
+  ef->m_loc[1]=rout[1];
+  ef->pos=ef->m_pos;
+    if ((select) && (ef->selstart==0)) {
+      if(ef->pos==0)
+      ef->selstart = ef->selend =1;
+      else
+      ef->selstart = ef->selend = ef->pos+1;
+  }
+  ef->selend=ef->pos;
+  text_update_edited(C, obedit, true);
+
+}
+
+static int font_selection_set_invoke(bContext *C, wmOperator *op, const wmEvent *event)
+{ 
+  Object *obedit = CTX_data_active_object(C);
+  Curve *cu = obedit->data;
+  EditFont *ef = cu->editfont;
+
+  WM_event_add_modal_handler(C, op);
+
+  font_cursor_set_apply(C, op, event);
+  ef->selstart=0;
+  ef->selend=0;
+  
+  text_update_edited(C, obedit, true);
+
+  return OPERATOR_RUNNING_MODAL; 
+}
+
+static int font_selection_set_modal(bContext *C, wmOperator *op, const wmEvent *event)
+{
+  Object *obedit = CTX_data_active_object(C);
+  Curve *cu = obedit->data;
+  EditFont *ef = cu->editfont;
+  switch (event->type) {
+    case LEFTMOUSE:
+      font_cursor_set_apply(C, op, event);
+      return OPERATOR_FINISHED;
+    case MIDDLEMOUSE:
+    case RIGHTMOUSE:
+      return OPERATOR_FINISHED; 
+    case TIMER:
+    case MOUSEMOVE:
+      font_cursor_set_apply(C, op, event);
+      break;
+  }
+  
+  return OPERATOR_RUNNING_MODAL;
+}
+
+void FONT_OT_selection_set(struct wmOperatorType *ot)
+{
+  /* identifiers */
+  ot->name = "Set Selection";
+  ot->idname = "FONT_OT_selection_set";
+  ot->description = "Set cursor selection";
+  /* api callbacks */
+  ot->invoke = font_selection_set_invoke;
+  ot->modal = font_selection_set_modal;
+  ot->poll = ED_operator_editfont;
+}
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Select Word Operator
+ * \{ */
+static  font_select_word_exec(bContext *C, wmOperator *op)
+{
+  Object *obedit = CTX_data_active_object(C);
+  Curve *cu = obedit->data;
+  EditFont *ef = cu->editfont;
+
+  move_cursor(C,PREV_WORD,false)+move_cursor(C,NEXT_WORD,true);
+  return OPERATOR_FINISHED; 
+}
+
+void FONT_OT_select_word(wmOperatorType *ot)
+{
+  /* identifiers */
+  ot->name = "Select Word";
+  ot->idname = "FONT_OT_select_word";
+  ot->description = "Select word under cursor";
+
+  /* api callbacks */
+  ot->exec = font_select_word_exec;
+  ot->poll = ED_operator_editfont;
+}
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Text-Box Add Operator
  * \{ */
diff --git a/source/blender/makesrna/intern/rna_curve.c b/source/blender/mak

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list