diff options
author | Yash Dabhade <yashdabhade5@gmail.com> | 2022-08-18 09:32:18 +0300 |
---|---|---|
committer | Yash Dabhade <yashdabhade5@gmail.com> | 2022-08-18 09:32:18 +0300 |
commit | 8cd76bff475c5f6bc95e4df28c8003907eb0af56 (patch) | |
tree | 290e39825df9f2937b55732ef08905ec613c846d | |
parent | 9d1cfd9dde3d23d39ba869fada295ea579231490 (diff) |
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.
-rw-r--r-- | release/scripts/presets/keyconfig/keymap_data/blender_default.py | 2 | ||||
-rw-r--r-- | release/scripts/startup/bl_ui/properties_data_curve.py | 17 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/vfont.c | 55 | ||||
-rw-r--r-- | source/blender/editors/curve/curve_intern.h | 3 | ||||
-rw-r--r-- | source/blender/editors/curve/curve_ops.c | 3 | ||||
-rw-r--r-- | source/blender/editors/curve/editfont.c | 111 | ||||
-rw-r--r-- | source/blender/makesrna/intern/rna_curve.c | 79 |
7 files changed, 262 insertions, 8 deletions
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" @@ -1756,6 +1757,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/makesrna/intern/rna_curve.c index fff3f479a3f..ac3b8c7b8cd 100644 --- a/source/blender/makesrna/intern/rna_curve.c +++ b/source/blender/makesrna/intern/rna_curve.c @@ -837,6 +837,50 @@ static bool rna_Curve_is_editmode_get(PointerRNA *ptr) } } +static bool rna_Curve_is_select_underline_get(PointerRNA *ptr) +{ + Curve *cu= (Curve *)ptr->owner_id; + if(cu->editfont != NULL) + return cu->editfont->select_is_underline; + else + return false; +} + +static bool rna_Curve_is_select_bold_get(PointerRNA *ptr) +{ + Curve *cu= (Curve *)ptr->owner_id; + if(cu->editfont != NULL) + return cu->editfont->select_is_bold; + else + return false; +} + +static bool rna_Curve_is_select_italics_get(PointerRNA *ptr) +{ + Curve *cu= (Curve *)ptr->owner_id; + if(cu->editfont != NULL) + return cu->editfont->select_is_italics; + else + return false; +} + +static bool rna_Curve_is_select_smallcaps_get(PointerRNA *ptr) +{ + Curve *cu= (Curve *)ptr->owner_id; + if(cu->editfont != NULL) + return cu->editfont->select_is_smallcaps; + else + return false; +} +static bool rna_Curve_is_selected(PointerRNA *ptr) +{ + Curve *cu= (Curve *)ptr->owner_id; + if(cu->editfont != NULL) + return cu->editfont->is_selected; + else + return false; +} + #else static const float tilt_limit = DEG2RADF(21600.0f); @@ -1271,6 +1315,41 @@ static void rna_def_font(BlenderRNA *UNUSED(brna), StructRNA *srna) RNA_def_property_boolean_sdna(prop, NULL, "flag", CU_FAST); RNA_def_property_ui_text(prop, "Fast Editing", "Don't fill polygons while editing"); RNA_def_property_update(prop, 0, "rna_Curve_update_data"); + + prop = RNA_def_property(srna, "select_is_underline", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_funcs(prop, "rna_Curve_is_select_underline_get", NULL); + RNA_def_property_ui_text(prop, "bold sig", "Checks for bold"); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY); + RNA_def_property_update(prop, 0, "rna_Curve_update_data"); + + prop = RNA_def_property(srna, "select_is_bold", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_funcs(prop, "rna_Curve_is_select_bold_get", NULL); + RNA_def_property_ui_text(prop, "bold sig", "Checks for bold"); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY); + RNA_def_property_update(prop, 0, "rna_Curve_update_data"); + + prop = RNA_def_property(srna, "select_is_italics", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_funcs(prop, "rna_Curve_is_select_italics_get", NULL); + RNA_def_property_ui_text(prop, "bold sig", "Checks for bold"); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY); + RNA_def_property_update(prop, 0, "rna_Curve_update_data"); + + prop = RNA_def_property(srna, "select_is_smallcaps", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_funcs(prop, "rna_Curve_is_select_smallcaps_get", NULL); + RNA_def_property_ui_text(prop, "bold sig", "Checks for bold"); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY); + RNA_def_property_update(prop, 0, "rna_Curve_update_data"); + + prop = RNA_def_property(srna, "is_selected", PROP_BOOLEAN, PROP_NONE); + RNA_def_property_boolean_funcs(prop, "rna_Curve_is_selected", NULL); + RNA_def_property_ui_text(prop, "bold sig", "Checks for bold"); + RNA_def_property_clear_flag(prop, PROP_EDITABLE); + RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY); + RNA_def_property_update(prop, 0, "rna_Curve_update_data"); } static void rna_def_textbox(BlenderRNA *brna) |