~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/blender/editors/interface/interface_ops.c

  • Committer: Reinhard Tartler
  • Date: 2014-05-31 01:50:05 UTC
  • mfrom: (14.2.27 sid)
  • Revision ID: siretart@tauware.de-20140531015005-ml6druahuj82nsav
mergeĀ fromĀ debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 *  \ingroup edinterface
28
28
 */
29
29
 
30
 
#include <stdio.h>
31
 
#include <math.h>
32
30
#include <string.h>
33
31
 
34
32
#include "MEM_guardedalloc.h"
35
33
 
36
 
#include "DNA_scene_types.h"
37
34
#include "DNA_screen_types.h"
38
35
#include "DNA_text_types.h" /* for UI_OT_reports_to_text */
39
36
 
40
37
#include "BLI_blenlib.h"
41
 
#include "BLI_math_color.h"
42
 
#include "BLI_math_vector.h"
43
 
#include "BLI_utildefines.h"
44
38
 
45
39
#include "BLF_api.h"
46
40
#include "BLF_translation.h"
54
48
#include "RNA_access.h"
55
49
#include "RNA_define.h"
56
50
 
57
 
#include "BIF_gl.h"
58
 
 
59
51
#include "UI_interface.h"
60
52
 
61
 
#include "IMB_colormanagement.h"
62
 
 
63
53
#include "interface_intern.h"
64
54
 
65
55
#include "WM_api.h"
70
60
#include "BKE_main.h"
71
61
#include "BLI_ghash.h"
72
62
 
73
 
#include "ED_image.h"  /* for HDR color sampling */
74
 
#include "ED_node.h"   /* for HDR color sampling */
75
 
#include "ED_clip.h"   /* for HDR color sampling */
76
 
 
77
 
/* ********************************************************** */
78
 
 
79
 
typedef struct Eyedropper {
80
 
        struct ColorManagedDisplay *display;
81
 
 
82
 
        PointerRNA ptr;
83
 
        PropertyRNA *prop;
84
 
        int index;
85
 
 
86
 
        int   accum_start; /* has mouse been presed */
87
 
        float accum_col[3];
88
 
        int   accum_tot;
89
 
} Eyedropper;
90
 
 
91
 
static int eyedropper_init(bContext *C, wmOperator *op)
92
 
{
93
 
        Scene *scene = CTX_data_scene(C);
94
 
        Eyedropper *eye;
95
 
        
96
 
        op->customdata = eye = MEM_callocN(sizeof(Eyedropper), "Eyedropper");
97
 
        
98
 
        uiContextActiveProperty(C, &eye->ptr, &eye->prop, &eye->index);
99
 
 
100
 
        if ((eye->ptr.data == NULL) ||
101
 
            (eye->prop == NULL) ||
102
 
            (RNA_property_editable(&eye->ptr, eye->prop) == FALSE) ||
103
 
            (RNA_property_array_length(&eye->ptr, eye->prop) < 3) ||
104
 
            (RNA_property_type(eye->prop) != PROP_FLOAT))
105
 
        {
106
 
                return FALSE;
107
 
        }
108
 
 
109
 
        if (RNA_property_subtype(eye->prop) == PROP_COLOR) {
110
 
                const char *display_device;
111
 
 
112
 
                display_device = scene->display_settings.display_device;
113
 
                eye->display = IMB_colormanagement_display_get_named(display_device);
114
 
        }
115
 
 
116
 
        return TRUE;
117
 
}
118
 
 
119
 
static void eyedropper_exit(bContext *C, wmOperator *op)
120
 
{
121
 
        WM_cursor_modal_restore(CTX_wm_window(C));
122
 
        
123
 
        if (op->customdata)
124
 
                MEM_freeN(op->customdata);
125
 
        op->customdata = NULL;
126
 
}
127
 
 
128
 
static int eyedropper_cancel(bContext *C, wmOperator *op)
129
 
{
130
 
        eyedropper_exit(C, op);
131
 
        return OPERATOR_CANCELLED;
132
 
}
133
 
 
134
 
/* *** eyedropper_color_ helper functions *** */
135
 
 
136
 
/**
137
 
 * \brief get the color from the screen.
138
 
 *
139
 
 * Special check for image or nodes where we MAY have HDR pixels which don't display.
140
 
 */
141
 
static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int mx, int my, float r_col[3])
142
 
{
143
 
 
144
 
        /* we could use some clever */
145
 
        wmWindow *win = CTX_wm_window(C);
146
 
        ScrArea *sa;
147
 
        for (sa = win->screen->areabase.first; sa; sa = sa->next) {
148
 
                if (BLI_rcti_isect_pt(&sa->totrct, mx, my)) {
149
 
                        if (sa->spacetype == SPACE_IMAGE) {
150
 
                                ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
151
 
                                if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
152
 
                                        SpaceImage *sima = sa->spacedata.first;
153
 
                                        int mval[2] = {mx - ar->winrct.xmin,
154
 
                                                       my - ar->winrct.ymin};
155
 
 
156
 
                                        if (ED_space_image_color_sample(sima, ar, mval, r_col)) {
157
 
                                                return;
158
 
                                        }
159
 
                                }
160
 
                        }
161
 
                        else if (sa->spacetype == SPACE_NODE) {
162
 
                                ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
163
 
                                if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
164
 
                                        SpaceNode *snode = sa->spacedata.first;
165
 
                                        int mval[2] = {mx - ar->winrct.xmin,
166
 
                                                       my - ar->winrct.ymin};
167
 
 
168
 
                                        if (ED_space_node_color_sample(snode, ar, mval, r_col)) {
169
 
                                                return;
170
 
                                        }
171
 
                                }
172
 
                        }
173
 
                        else if (sa->spacetype == SPACE_CLIP) {
174
 
                                ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
175
 
                                if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
176
 
                                        SpaceClip *sc = sa->spacedata.first;
177
 
                                        int mval[2] = {mx - ar->winrct.xmin,
178
 
                                                       my - ar->winrct.ymin};
179
 
 
180
 
                                        if (ED_space_clip_color_sample(sc, ar, mval, r_col)) {
181
 
                                                return;
182
 
                                        }
183
 
                                }
184
 
                        }
185
 
                }
186
 
        }
187
 
 
188
 
        /* fallback to simple opengl picker */
189
 
        glReadBuffer(GL_FRONT);
190
 
        glReadPixels(mx, my, 1, 1, GL_RGB, GL_FLOAT, r_col);
191
 
        glReadBuffer(GL_BACK);
192
 
}
193
 
 
194
 
/* sets the sample color RGB, maintaining A */
195
 
static void eyedropper_color_set(bContext *C, Eyedropper *eye, const float col[3])
196
 
{
197
 
        float col_conv[4];
198
 
 
199
 
        /* to maintain alpha */
200
 
        RNA_property_float_get_array(&eye->ptr, eye->prop, col_conv);
201
 
 
202
 
        /* convert from display space to linear rgb space */
203
 
        if (eye->display) {
204
 
                copy_v3_v3(col_conv, col);
205
 
                IMB_colormanagement_display_to_scene_linear_v3(col_conv, eye->display);
206
 
        }
207
 
        else {
208
 
                copy_v3_v3(col_conv, col);
209
 
        }
210
 
 
211
 
        RNA_property_float_set_array(&eye->ptr, eye->prop, col_conv);
212
 
 
213
 
        RNA_property_update(C, &eye->ptr, eye->prop);
214
 
}
215
 
 
216
 
/* set sample from accumulated values */
217
 
static void eyedropper_color_set_accum(bContext *C, Eyedropper *eye)
218
 
{
219
 
        float col[3];
220
 
        mul_v3_v3fl(col, eye->accum_col, 1.0f / (float)eye->accum_tot);
221
 
        eyedropper_color_set(C, eye, col);
222
 
}
223
 
 
224
 
/* single point sample & set */
225
 
static void eyedropper_color_sample(bContext *C, Eyedropper *eye, int mx, int my)
226
 
{
227
 
        float col[3];
228
 
        eyedropper_color_sample_fl(C, eye, mx, my, col);
229
 
        eyedropper_color_set(C, eye, col);
230
 
}
231
 
 
232
 
static void eyedropper_color_sample_accum(bContext *C, Eyedropper *eye, int mx, int my)
233
 
{
234
 
        float col[3];
235
 
        eyedropper_color_sample_fl(C, eye, mx, my, col);
236
 
        /* delay linear conversion */
237
 
        add_v3_v3(eye->accum_col, col);
238
 
        eye->accum_tot++;
239
 
}
240
 
 
241
 
/* main modal status check */
242
 
static int eyedropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
243
 
{
244
 
        Eyedropper *eye = (Eyedropper *)op->customdata;
245
 
        
246
 
        switch (event->type) {
247
 
                case ESCKEY:
248
 
                case RIGHTMOUSE:
249
 
                        return eyedropper_cancel(C, op);
250
 
                case LEFTMOUSE:
251
 
                        if (event->val == KM_RELEASE) {
252
 
                                if (eye->accum_tot == 0) {
253
 
                                        eyedropper_color_sample(C, eye, event->x, event->y);
254
 
                                }
255
 
                                else {
256
 
                                        eyedropper_color_set_accum(C, eye);
257
 
                                }
258
 
                                eyedropper_exit(C, op);
259
 
                                return OPERATOR_FINISHED;
260
 
                        }
261
 
                        else if (event->val == KM_PRESS) {
262
 
                                /* enable accum and make first sample */
263
 
                                eye->accum_start = TRUE;
264
 
                                eyedropper_color_sample_accum(C, eye, event->x, event->y);
265
 
                        }
266
 
                        break;
267
 
                case MOUSEMOVE:
268
 
                        if (eye->accum_start) {
269
 
                                /* button is pressed so keep sampling */
270
 
                                eyedropper_color_sample_accum(C, eye, event->x, event->y);
271
 
                                eyedropper_color_set_accum(C, eye);
272
 
                        }
273
 
                        break;
274
 
                case SPACEKEY:
275
 
                        if (event->val == KM_RELEASE) {
276
 
                                eye->accum_tot = 0;
277
 
                                zero_v3(eye->accum_col);
278
 
                                eyedropper_color_sample_accum(C, eye, event->x, event->y);
279
 
                                eyedropper_color_set_accum(C, eye);
280
 
                        }
281
 
                        break;
282
 
        }
283
 
        
284
 
        return OPERATOR_RUNNING_MODAL;
285
 
}
286
 
 
287
 
/* Modal Operator init */
288
 
static int eyedropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
289
 
{
290
 
        /* init */
291
 
        if (eyedropper_init(C, op)) {
292
 
                WM_cursor_modal_set(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
293
 
 
294
 
                /* add temp handler */
295
 
                WM_event_add_modal_handler(C, op);
296
 
                
297
 
                return OPERATOR_RUNNING_MODAL;
298
 
        }
299
 
        else {
300
 
                eyedropper_exit(C, op);
301
 
                return OPERATOR_CANCELLED;
302
 
        }
303
 
}
304
 
 
305
 
/* Repeat operator */
306
 
static int eyedropper_exec(bContext *C, wmOperator *op)
307
 
{
308
 
        /* init */
309
 
        if (eyedropper_init(C, op)) {
310
 
                
311
 
                /* do something */
312
 
                
313
 
                /* cleanup */
314
 
                eyedropper_exit(C, op);
315
 
                
316
 
                return OPERATOR_FINISHED;
317
 
        }
318
 
        else {
319
 
                return OPERATOR_CANCELLED;
320
 
        }
321
 
}
322
 
 
323
 
static int eyedropper_poll(bContext *C)
324
 
{
325
 
        if (!CTX_wm_window(C)) return 0;
326
 
        else return 1;
327
 
}
328
 
 
329
 
static void UI_OT_eyedropper(wmOperatorType *ot)
330
 
{
331
 
        /* identifiers */
332
 
        ot->name = "Eyedropper";
333
 
        ot->idname = "UI_OT_eyedropper";
334
 
        ot->description = "Sample a color from the Blender Window to store in a property";
335
 
        
336
 
        /* api callbacks */
337
 
        ot->invoke = eyedropper_invoke;
338
 
        ot->modal = eyedropper_modal;
339
 
        ot->cancel = eyedropper_cancel;
340
 
        ot->exec = eyedropper_exec;
341
 
        ot->poll = eyedropper_poll;
342
 
        
343
 
        /* flags */
344
 
        ot->flag = OPTYPE_BLOCKING;
345
 
        
346
 
        /* properties */
347
 
}
348
 
 
349
63
/* Reset Default Theme ------------------------ */
350
64
 
351
65
static int reset_default_theme_exec(bContext *C, wmOperator *UNUSED(op))
408
122
                path = RNA_path_from_ID_to_property(&ptr, prop);
409
123
                
410
124
                if (path) {
411
 
                        WM_clipboard_text_set(path, FALSE);
 
125
                        WM_clipboard_text_set(path, false);
412
126
                        MEM_freeN(path);
413
127
                        return OPERATOR_FINISHED;
414
128
                }
471
185
{
472
186
        PointerRNA ptr;
473
187
        PropertyRNA *prop;
474
 
        int index, all = RNA_boolean_get(op->ptr, "all");
 
188
        int index;
 
189
        const bool all = RNA_boolean_get(op->ptr, "all");
475
190
 
476
191
        /* try to reset the nominated setting to its default value */
477
192
        uiContextActiveProperty(C, &ptr, &prop, &index);
515
230
        uiContextActiveProperty(C, &ptr, &prop, &index);
516
231
 
517
232
        /* if there is a valid property that is editable... */
518
 
        if (ptr.data && prop && RNA_property_editable(&ptr, prop)
519
 
            /*&& RNA_property_is_idprop(prop)*/ && RNA_property_is_set(&ptr, prop))
 
233
        if (ptr.data && prop && RNA_property_editable(&ptr, prop) &&
 
234
            /* RNA_property_is_idprop(prop) && */
 
235
            RNA_property_is_set(&ptr, prop))
520
236
        {
521
237
                RNA_property_unset(&ptr, prop);
522
238
                return operator_button_property_finish(C, &ptr, prop);
542
258
 
543
259
/* Copy To Selected Operator ------------------------ */
544
260
 
545
 
static int copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb, int *use_path)
 
261
static bool copy_to_selected_list(bContext *C, PointerRNA *ptr, ListBase *lb, bool *use_path)
546
262
{
547
 
        *use_path = FALSE;
 
263
        *use_path = false;
548
264
 
549
265
        if (RNA_struct_is_a(ptr->type, &RNA_EditBone))
550
266
                *lb = CTX_data_collection_get(C, "selected_editable_bones");
557
273
 
558
274
                if (id && GS(id->name) == ID_OB) {
559
275
                        *lb = CTX_data_collection_get(C, "selected_editable_objects");
560
 
                        *use_path = TRUE;
561
 
                }
562
 
                else
563
 
                        return 0;
 
276
                        *use_path = true;
 
277
                }
 
278
                else {
 
279
                        return false;
 
280
                }
564
281
        }
565
282
        
566
 
        return 1;
 
283
        return true;
567
284
}
568
285
 
569
286
static int copy_to_selected_button_poll(bContext *C)
576
293
 
577
294
        if (ptr.data && prop) {
578
295
                char *path = NULL;
579
 
                int use_path;
 
296
                bool use_path;
580
297
                CollectionPointerLink *link;
581
298
                ListBase lb;
582
299
 
617
334
{
618
335
        PointerRNA ptr, lptr, idptr;
619
336
        PropertyRNA *prop, *lprop;
620
 
        int success = 0;
621
 
        int index, all = RNA_boolean_get(op->ptr, "all");
 
337
        bool success = false;
 
338
        int index;
 
339
        const bool all = RNA_boolean_get(op->ptr, "all");
622
340
 
623
341
        /* try to reset the nominated setting to its default value */
624
342
        uiContextActiveProperty(C, &ptr, &prop, &index);
626
344
        /* if there is a valid property that is editable... */
627
345
        if (ptr.data && prop) {
628
346
                char *path = NULL;
629
 
                int use_path;
 
347
                bool use_path;
630
348
                CollectionPointerLink *link;
631
349
                ListBase lb;
632
350
 
633
351
                if (!copy_to_selected_list(C, &ptr, &lb, &use_path))
634
 
                        return success;
 
352
                        return OPERATOR_CANCELLED;
635
353
 
636
354
                if (!use_path || (path = RNA_path_from_ID_to_property(&ptr, prop))) {
637
355
                        for (link = lb.first; link; link = link->next) {
650
368
                                                if (RNA_property_editable(&lptr, lprop)) {
651
369
                                                        if (RNA_property_copy(&lptr, &ptr, prop, (all) ? -1 : index)) {
652
370
                                                                RNA_property_update(C, &lptr, prop);
653
 
                                                                success = 1;
 
371
                                                                success = true;
654
372
                                                        }
655
373
                                                }
656
374
                                        }
776
494
        ui_editsource_info = NULL;
777
495
}
778
496
 
779
 
static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
 
497
static bool ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
780
498
{
781
499
#if 0
782
500
        printf("matching buttons: '%s' == '%s'\n",
793
511
            (but_a->unit_type == but_b->unit_type) &&
794
512
            (strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0))
795
513
        {
796
 
                return TRUE;
 
514
                return true;
797
515
        }
798
516
        else {
799
 
                return FALSE;
 
517
                return false;
800
518
        }
801
519
}
802
520
 
861
579
                        BKE_reportf(op->reports, RPT_INFO, "See '%s' in the text editor", text->id.name + 2);
862
580
                }
863
581
 
864
 
                txt_move_toline(text, line - 1, FALSE);
 
582
                txt_move_toline(text, line - 1, false);
865
583
                WM_event_add_notifier(C, NC_TEXT | ND_CURSOR, text);
866
584
        }
867
585
 
1114
832
 
1115
833
void UI_buttons_operatortypes(void)
1116
834
{
1117
 
        WM_operatortype_append(UI_OT_eyedropper);
1118
835
        WM_operatortype_append(UI_OT_reset_default_theme);
1119
836
        WM_operatortype_append(UI_OT_copy_data_path_button);
1120
837
        WM_operatortype_append(UI_OT_reset_default_button);
1127
844
        WM_operatortype_append(UI_OT_edittranslation_init);
1128
845
#endif
1129
846
        WM_operatortype_append(UI_OT_reloadtranslation);
 
847
 
 
848
        /* external */
 
849
        WM_operatortype_append(UI_OT_eyedropper_color);
 
850
        WM_operatortype_append(UI_OT_eyedropper_id);
1130
851
}