Welcome to mirror list, hosted at ThFree Co, Russian Federation.

interface_eyedropper_driver.c « interface « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0cacb55c60cbbd7a00ccd36b29eea2d13f0bc39a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2009 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup edinterface
 *
 * Eyedropper (Animation Driver Targets).
 *
 * Defines:
 * - #UI_OT_eyedropper_driver
 */

#include "MEM_guardedalloc.h"

#include "DNA_anim_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"

#include "BKE_animsys.h"
#include "BKE_context.h"

#include "DEG_depsgraph.h"
#include "DEG_depsgraph_build.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "UI_interface.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_keyframing.h"

#include "interface_eyedropper_intern.h"
#include "interface_intern.h"

typedef struct DriverDropper {
  /* Destination property (i.e. where we'll add a driver) */
  PointerRNA ptr;
  PropertyRNA *prop;
  int index;
  bool is_undo;

  /* TODO: new target? */
} DriverDropper;

static bool driverdropper_init(bContext *C, wmOperator *op)
{
  DriverDropper *ddr = MEM_callocN(sizeof(DriverDropper), __func__);

  uiBut *but = UI_context_active_but_prop_get(C, &ddr->ptr, &ddr->prop, &ddr->index);

  if ((ddr->ptr.data == NULL) || (ddr->prop == NULL) ||
      (RNA_property_editable(&ddr->ptr, ddr->prop) == false) ||
      (RNA_property_animateable(&ddr->ptr, ddr->prop) == false) || (but->flag & UI_BUT_DRIVEN)) {
    MEM_freeN(ddr);
    return false;
  }
  op->customdata = ddr;

  ddr->is_undo = UI_but_flag_is_set(but, UI_BUT_UNDO);

  return true;
}

static void driverdropper_exit(bContext *C, wmOperator *op)
{
  WM_cursor_modal_restore(CTX_wm_window(C));

  MEM_SAFE_FREE(op->customdata);
}

static void driverdropper_sample(bContext *C, wmOperator *op, const wmEvent *event)
{
  DriverDropper *ddr = (DriverDropper *)op->customdata;
  uiBut *but = eyedropper_get_property_button_under_mouse(C, event);

  const short mapping_type = RNA_enum_get(op->ptr, "mapping_type");
  const short flag = 0;

  /* we can only add a driver if we know what RNA property it corresponds to */
  if (but == NULL) {
    return;
  }
  /* Get paths for src... */
  PointerRNA *target_ptr = &but->rnapoin;
  PropertyRNA *target_prop = but->rnaprop;
  const int target_index = but->rnaindex;

  char *target_path = RNA_path_from_ID_to_property(target_ptr, target_prop);

  /* ... and destination */
  char *dst_path = RNA_path_from_ID_to_property(&ddr->ptr, ddr->prop);

  /* Now create driver(s) */
  if (target_path && dst_path) {
    int success = ANIM_add_driver_with_target(op->reports,
                                              ddr->ptr.owner_id,
                                              dst_path,
                                              ddr->index,
                                              target_ptr->owner_id,
                                              target_path,
                                              target_index,
                                              flag,
                                              DRIVER_TYPE_PYTHON,
                                              mapping_type);

    if (success) {
      /* send updates */
      UI_context_update_anim_flag(C);
      DEG_relations_tag_update(CTX_data_main(C));
      DEG_id_tag_update(ddr->ptr.owner_id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
      WM_event_add_notifier(C, NC_ANIMATION | ND_FCURVES_ORDER, NULL); /* XXX */
    }
  }

  /* cleanup */
  if (target_path) {
    MEM_freeN(target_path);
  }
  if (dst_path) {
    MEM_freeN(dst_path);
  }
}

static void driverdropper_cancel(bContext *C, wmOperator *op)
{
  driverdropper_exit(C, op);
}

/* main modal status check */
static int driverdropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
  DriverDropper *ddr = op->customdata;

  /* handle modal keymap */
  if (event->type == EVT_MODAL_MAP) {
    switch (event->val) {
      case EYE_MODAL_CANCEL: {
        driverdropper_cancel(C, op);
        return OPERATOR_CANCELLED;
      }
      case EYE_MODAL_SAMPLE_CONFIRM: {
        const bool is_undo = ddr->is_undo;
        driverdropper_sample(C, op, event);
        driverdropper_exit(C, op);
        /* Could support finished & undo-skip. */
        return is_undo ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
      }
    }
  }

  return OPERATOR_RUNNING_MODAL;
}

/* Modal Operator init */
static int driverdropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
  /* init */
  if (driverdropper_init(C, op)) {
    wmWindow *win = CTX_wm_window(C);
    /* Workaround for de-activating the button clearing the cursor, see T76794 */
    UI_context_active_but_clear(C, win, CTX_wm_region(C));
    WM_cursor_modal_set(win, WM_CURSOR_EYEDROPPER);

    /* add temp handler */
    WM_event_add_modal_handler(C, op);

    return OPERATOR_RUNNING_MODAL;
  }
  return OPERATOR_CANCELLED;
}

/* Repeat operator */
static int driverdropper_exec(bContext *C, wmOperator *op)
{
  /* init */
  if (driverdropper_init(C, op)) {
    /* cleanup */
    driverdropper_exit(C, op);

    return OPERATOR_FINISHED;
  }
  return OPERATOR_CANCELLED;
}

static bool driverdropper_poll(bContext *C)
{
  if (!CTX_wm_window(C)) {
    return false;
  }
  return true;
}

void UI_OT_eyedropper_driver(wmOperatorType *ot)
{
  /* identifiers */
  ot->name = "Eyedropper Driver";
  ot->idname = "UI_OT_eyedropper_driver";
  ot->description = "Pick a property to use as a driver target";

  /* api callbacks */
  ot->invoke = driverdropper_invoke;
  ot->modal = driverdropper_modal;
  ot->cancel = driverdropper_cancel;
  ot->exec = driverdropper_exec;
  ot->poll = driverdropper_poll;

  /* flags */
  ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING | OPTYPE_INTERNAL;

  /* properties */
  RNA_def_enum(ot->srna,
               "mapping_type",
               prop_driver_create_mapping_types,
               0,
               "Mapping Type",
               "Method used to match target and driven properties");
}