Blender V5.0
transform_mode_shrink_fatten.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <fmt/format.h>
11
12#include "BLI_math_vector.h"
13#include "BLI_task.hh"
14
15#include "BKE_report.hh"
16#include "BKE_unit.hh"
17
18#include "ED_screen.hh"
19
20#include "WM_api.hh"
21#include "WM_types.hh"
22
23#include "UI_interface.hh"
24
25#include "BLT_translation.hh"
26
27#include "RNA_access.hh"
28
29#include "transform.hh"
30#include "transform_convert.hh"
31#include "transform_snap.hh"
32
33#include "transform_mode.hh"
34
35namespace blender::ed::transform {
36
37/* -------------------------------------------------------------------- */
40
45
55
57 const TransDataContainer * /*tc*/,
58 TransData *td,
59 TransDataExtension *td_ext,
60 const float distance)
61{
62 ShrinkFattenCustomData *custom_data = static_cast<ShrinkFattenCustomData *>(t->custom.mode.data);
63
64 /* Get the final offset. */
65 float tdistance = distance * td->factor;
66 if (td_ext && custom_data->mode == EVEN_THICKNESS_ON) {
67 tdistance *= td_ext->iscale[0]; /* Shell factor. */
68 }
69
70 madd_v3_v3v3fl(td->loc, td->iloc, td->axismtx[2], tdistance);
71}
72
74{
76 ShrinkFattenCustomData *custom_data = static_cast<ShrinkFattenCustomData *>(t->custom.mode.data);
77 const wmKeyMapItem *kmi = custom_data->kmi;
78
80 bool use_even_thickness = custom_data->use_alt_press_to_disable != (event->val == KM_PRESS);
81 custom_data->mode = use_even_thickness ? EVEN_THICKNESS_ON : EVEN_THICKNESS_OFF;
82 return TREDRAW_HARD;
83 }
84 else if (kmi && event->type == kmi->type && event->val == kmi->val) {
85 /* Allows the "Even Thickness" effect to be enabled as a toggle. */
86 custom_data->mode = custom_data->mode == EVEN_THICKNESS_ON ? EVEN_THICKNESS_OFF :
88
89 /* Also toggle the Alt press state. */
90 custom_data->use_alt_press_to_disable = !custom_data->use_alt_press_to_disable;
91 return TREDRAW_HARD;
92 }
93 return TREDRAW_NOTHING;
94}
95
97{
98 float distance;
99 fmt::memory_buffer str;
100 const UnitSettings &unit = t->scene->unit;
101 ShrinkFattenCustomData *custom_data = static_cast<ShrinkFattenCustomData *>(t->custom.mode.data);
102
103 distance = t->values[0] + t->values_modal_offset[0];
104
106
108
109 t->values_final[0] = distance;
110
111 /* Header print for NumInput. */
112 fmt::format_to(fmt::appender(str), "{}", IFACE_("Shrink/Fatten: "));
113 if (hasNumInput(&t->num)) {
114 char c[NUM_STR_REP_LEN];
115 outputNumInput(&(t->num), c, unit);
116 fmt::format_to(fmt::appender(str), "{}", c);
117 }
118 else {
119 /* Default header print. */
120 if (unit.system != USER_UNIT_NONE) {
121 char unit_str[64];
123 unit_str, sizeof(unit_str), distance, -4, B_UNIT_LENGTH, unit, true);
124 fmt::format_to(fmt::appender(str), "{}", unit_str);
125 }
126 else {
127 fmt::format_to(fmt::appender(str), "{:.4f}", distance);
128 }
129 }
130
131 if (t->proptext[0]) {
132 fmt::format_to(fmt::appender(str), " {}", t->proptext);
133 }
134
135 /* Done with header string. */
136
138 threading::parallel_for(IndexRange(tc->data_len), 1024, [&](const IndexRange range) {
139 for (const int i : range) {
140 TransData *td = &tc->data[i];
141 TransDataExtension *td_ext = tc->data_ext ? &tc->data_ext[i] : nullptr;
142 if (td->flag & TD_SKIP) {
143 continue;
144 }
145 transdata_elem_shrink_fatten(t, tc, td, td_ext, distance);
146 }
147 });
148 }
149
150 recalc_data(t);
151
152 ED_area_status_text(t->area, fmt::to_string(str).c_str());
153
154 if (custom_data->op) {
155 WorkspaceStatus status(t->context);
156
157 status.opmodal(IFACE_("Confirm"), custom_data->op->type, TFM_MODAL_CONFIRM);
158 status.opmodal(IFACE_("Cancel"), custom_data->op->type, TFM_MODAL_CANCEL);
159 status.opmodal(
160 IFACE_("Snap"), custom_data->op->type, TFM_MODAL_SNAP_TOGGLE, t->modifiers & MOD_SNAP);
161 status.opmodal(IFACE_("Snap Invert"),
162 custom_data->op->type,
164 t->modifiers & MOD_SNAP_INVERT);
165 status.opmodal(IFACE_("Precision"),
166 custom_data->op->type,
168 t->modifiers & MOD_PRECISION);
169 status.opmodal(IFACE_("Even Thickness"),
170 custom_data->op->type,
172 custom_data->mode == EVEN_THICKNESS_ON);
173 status.item(IFACE_("Even Thickness Invert"), ICON_EVENT_ALT);
174
175 if (t->proptext[0]) {
176 status.opmodal({}, custom_data->op->type, TFM_MODAL_PROPSIZE_UP);
177 status.opmodal(IFACE_("Proportional Size"), custom_data->op->type, TFM_MODAL_PROPSIZE_DOWN);
178 }
179 }
180}
181
183{
184 if ((t->flag & T_EDIT) == 0 || (t->obedit_type != OB_MESH)) {
185 BKE_report(t->reports, RPT_ERROR, "'Shrink/Fatten' meshes is only supported in edit mode");
186 t->state = TRANS_CANCEL;
187 }
188
190
192
193 t->idx_max = 0;
194 t->num.idx_max = 0;
195 t->increment[0] = 1.0f;
196 t->increment_precision = 0.1f;
197
198 copy_v3_fl(t->num.val_inc, t->increment[0]);
199 t->num.unit_sys = t->scene->unit.system;
201
202 ShrinkFattenCustomData *custom_data = static_cast<ShrinkFattenCustomData *>(
203 MEM_callocN(sizeof(*custom_data), __func__));
204 t->custom.mode.data = custom_data;
205 t->custom.mode.free_cb = [](TransInfo *t, TransDataContainer *, TransCustomData *custom_data) {
206 ShrinkFattenCustomData *data = static_cast<ShrinkFattenCustomData *>(custom_data->data);
207
208 /* WORKAROUND: Use #T_ALT_TRANSFORM to indicate the value of the "use_even_offset" property in
209 * `saveTransform`. */
212 custom_data->data = nullptr;
213 };
214
215 if (t->keymap) {
216 /* Workaround to use the same key as the modal keymap. */
218 }
219
220 if (op) {
221 custom_data->op = op;
222 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "use_even_offset");
223 if (RNA_property_is_set(op->ptr, prop) && RNA_property_boolean_get(op->ptr, prop)) {
224 /* TODO: Check if the Alt button is already pressed. */
225 custom_data->mode = EVEN_THICKNESS_ON;
226 custom_data->use_alt_press_to_disable = true;
227 }
228 }
229}
230
232
234 /*flags*/ T_NO_CONSTRAINT,
235 /*init_fn*/ initShrinkFatten,
236 /*transform_fn*/ applyShrinkFatten,
237 /*transform_matrix_fn*/ nullptr,
238 /*handle_event_fn*/ shrinkfatten_handleEvent,
239 /*snap_distance_fn*/ nullptr,
240 /*snap_apply_fn*/ nullptr,
241 /*draw_fn*/ nullptr,
242};
243
244} // namespace blender::ed::transform
@ RPT_ERROR
Definition BKE_report.hh:39
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
@ B_UNIT_LENGTH
Definition BKE_unit.hh:137
size_t BKE_unit_value_as_string_scaled(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad)
Definition unit.cc:1895
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
#define SET_FLAG_FROM_TEST(value, test, flag)
#define ELEM(...)
#define IFACE_(msgid)
@ OB_MESH
@ USER_UNIT_NONE
#define NUM_STR_REP_LEN
bool applyNumInput(NumInput *n, float *vec)
Definition numinput.cc:190
void outputNumInput(NumInput *n, char *str, const UnitSettings &unit_settings)
Definition numinput.cc:88
bool hasNumInput(const NumInput *n)
Definition numinput.cc:171
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:851
@ KM_PRESS
Definition WM_types.hh:311
BMesh const char void * data
#define str(s)
float distance(VecOp< float, D >, VecOp< float, D >) RET
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
void recalc_data(TransInfo *t)
static void initShrinkFatten(TransInfo *t, wmOperator *op)
bool transform_snap_increment(const TransInfo *t, float *r_val)
static void applyShrinkFatten(TransInfo *t)
static void transdata_elem_shrink_fatten(const TransInfo *t, const TransDataContainer *, TransData *td, TransDataExtension *td_ext, const float distance)
static eRedrawFlag shrinkfatten_handleEvent(TransInfo *t, const wmEvent *event)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
const int status
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
short idx_max
float val_inc[NUM_MAX_ELEMENTS]
int unit_type[NUM_MAX_ELEMENTS]
struct UnitSettings unit
void(* free_cb)(TransInfo *, TransDataContainer *tc, TransCustomData *custom_data)
Definition transform.hh:633
TransCustomDataContainer custom
Definition transform.hh:974
wmEventType type
Definition WM_types.hh:757
short val
Definition WM_types.hh:759
struct PointerRNA * ptr
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:42
conversion and adaptation of different datablocks to a common struct.
transform modes used by different operators.
@ EVT_RIGHTALTKEY
@ EVT_LEFTALTKEY
const wmKeyMapItem * WM_modalkeymap_find_propvalue(const wmKeyMap *km, const int propvalue)