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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
|
/**
* $Id:
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2007 Blender Foundation.
* All rights reserved.
*
*
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <float.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <string.h>
#include "DNA_ID.h"
#include "DNA_screen_types.h"
#include "DNA_userdef_types.h"
#include "DNA_windowmanager_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_dynstr.h" /*for WM_operator_pystring */
#include "BKE_blender.h"
#include "BKE_context.h"
#include "BKE_idprop.h"
#include "BKE_library.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_utildefines.h"
#include "BIF_gl.h"
#include "BIF_glutil.h" /* for paint cursor */
#include "IMB_imbuf_types.h"
#include "ED_fileselect.h"
#include "ED_screen.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "UI_interface.h"
#include "UI_resources.h"
#include "WM_api.h"
#include "WM_types.h"
#include "wm.h"
#include "wm_window.h"
#include "wm_subwindow.h"
#include "wm_event_system.h"
static ListBase global_ops= {NULL, NULL};
/* ************ operator API, exported ********** */
wmOperatorType *WM_operatortype_find(const char *idname)
{
wmOperatorType *ot;
for(ot= global_ops.first; ot; ot= ot->next) {
if(strncmp(ot->idname, idname, OP_MAX_TYPENAME)==0)
return ot;
}
printf("search for unknown operator %s\n", idname);
return NULL;
}
wmOperatorType *WM_operatortype_first(void)
{
return global_ops.first;
}
/* all ops in 1 list (for time being... needs evaluation later) */
void WM_operatortype_append(void (*opfunc)(wmOperatorType*))
{
wmOperatorType *ot;
ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
opfunc(ot);
RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
RNA_def_struct_identifier(ot->srna, ot->idname);
BLI_addtail(&global_ops, ot);
}
void WM_operatortype_append_ptr(void (*opfunc)(wmOperatorType*, void*), void *userdata)
{
wmOperatorType *ot;
ot= MEM_callocN(sizeof(wmOperatorType), "operatortype");
ot->srna= RNA_def_struct(&BLENDER_RNA, "", "OperatorProperties");
opfunc(ot, userdata);
RNA_def_struct_ui_text(ot->srna, ot->name, "DOC_BROKEN"); /* TODO - add a discription to wmOperatorType? */
RNA_def_struct_identifier(ot->srna, ot->idname);
BLI_addtail(&global_ops, ot);
}
int WM_operatortype_remove(const char *idname)
{
wmOperatorType *ot = WM_operatortype_find(idname);
if (ot==NULL)
return 0;
BLI_remlink(&global_ops, ot);
RNA_struct_free(&BLENDER_RNA, ot->srna);
MEM_freeN(ot);
return 1;
}
/* print a string representation of the operator, with the args that it runs
* so python can run it again */
char *WM_operator_pystring(wmOperator *op)
{
const char *arg_name= NULL;
PropertyRNA *prop, *iterprop;
CollectionPropertyIterator iter;
/* for building the string */
DynStr *dynstr= BLI_dynstr_new();
char *cstring, *buf;
int first_iter=1;
BLI_dynstr_appendf(dynstr, "%s(", op->idname);
iterprop= RNA_struct_iterator_property(op->ptr);
RNA_property_collection_begin(op->ptr, iterprop, &iter);
for(; iter.valid; RNA_property_collection_next(&iter)) {
prop= iter.ptr.data;
arg_name= RNA_property_identifier(&iter.ptr, prop);
if (strcmp(arg_name, "rna_type")==0) continue;
buf= RNA_property_as_string(op->ptr, prop);
BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
MEM_freeN(buf);
first_iter = 0;
}
RNA_property_collection_end(&iter);
BLI_dynstr_append(dynstr, ")");
cstring = BLI_dynstr_get_cstring(dynstr);
BLI_dynstr_free(dynstr);
return cstring;
}
void WM_operator_properties_create(PointerRNA *ptr, const char *opstring)
{
wmOperatorType *ot= WM_operatortype_find(opstring);
if(ot)
RNA_pointer_create(NULL, ot->srna, NULL, ptr);
else
RNA_pointer_create(NULL, &RNA_OperatorProperties, NULL, ptr);
}
void WM_operator_properties_free(PointerRNA *ptr)
{
IDProperty *properties= ptr->data;
if(properties) {
IDP_FreeProperty(properties);
MEM_freeN(properties);
}
}
/* ************ default op callbacks, exported *********** */
/* invoke callback, uses enum property named "type" */
/* only weak thing is the fixed property name... */
int WM_menu_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
PropertyRNA *prop= RNA_struct_find_property(op->ptr, "type");
const EnumPropertyItem *item;
int totitem, i, len= strlen(op->type->name) + 8;
char *menu, *p;
if(prop==NULL) {
printf("WM_menu_invoke: %s has no \"type\" enum property\n", op->type->idname);
}
else if (RNA_property_type(op->ptr, prop) != PROP_ENUM) {
printf("WM_menu_invoke: %s \"type\" is not an enum property\n", op->type->idname);
}
else {
RNA_property_enum_items(op->ptr, prop, &item, &totitem);
for (i=0; i<totitem; i++)
len+= strlen(item[i].name) + 8;
menu= MEM_callocN(len, "string");
p= menu + sprintf(menu, "%s %%t", op->type->name);
for (i=0; i<totitem; i++)
p+= sprintf(p, "|%s %%x%d", item[i].name, item[i].value);
uiPupMenuOperator(C, totitem/30, op, "type", menu);
MEM_freeN(menu);
return OPERATOR_RUNNING_MODAL;
}
return OPERATOR_CANCELLED;
}
/* op->invoke */
int WM_operator_confirm(bContext *C, wmOperator *op, wmEvent *event)
{
char buf[512];
sprintf(buf, "OK? %%i%d%%t|%s", ICON_HELP, op->type->name);
uiPupMenuOperator(C, 0, op, NULL, buf);
return OPERATOR_RUNNING_MODAL;
}
/* op->invoke, opens fileselect if filename property not set, otherwise executes */
int WM_operator_filesel(bContext *C, wmOperator *op, wmEvent *event)
{
if (RNA_property_is_set(op->ptr, "filename")) {
return WM_operator_call(C, op);
} else {
SpaceFile *sfile;
ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
/* settings for filebrowser */
sfile= (SpaceFile*)CTX_wm_space_data(C);
sfile->op = op;
ED_fileselect_set_params(sfile, FILE_BLENDER, op->type->name, "", 0, 0, 0);
/* screen and area have been reset already in ED_screen_full_newspace */
return OPERATOR_RUNNING_MODAL;
}
}
/* op->poll */
int WM_operator_winactive(bContext *C)
{
if(CTX_wm_window(C)==NULL) return 0;
return 1;
}
/* ************ window / screen operator definitions ************** */
static void WM_OT_window_duplicate(wmOperatorType *ot)
{
ot->name= "Duplicate Window";
ot->idname= "WM_OT_window_duplicate";
ot->invoke= WM_operator_confirm;
ot->exec= wm_window_duplicate_op;
ot->poll= WM_operator_winactive;
}
static void WM_OT_save_homefile(wmOperatorType *ot)
{
ot->name= "Save User Settings";
ot->idname= "WM_OT_save_homefile";
ot->invoke= WM_operator_confirm;
ot->exec= WM_write_homefile;
ot->poll= WM_operator_winactive;
}
/* ********* recent file *********** */
static void recent_filelist(char *pup)
{
struct RecentFile *recent;
int i, ofs= 0;
char *p;
p= pup + sprintf(pup, "Open Recent%%t");
if (G.sce[0]) {
p+= sprintf(p, "|%s %%x%d", G.sce, 1);
ofs = 1;
}
for (recent = G.recent_files.first, i=0; (i<U.recent_files) && (recent); recent = recent->next, i++) {
if (strcmp(recent->filename, G.sce)) {
p+= sprintf(p, "|%s %%x%d", recent->filename, i+ofs+1);
}
}
}
static int recentfile_exec(bContext *C, wmOperator *op)
{
int event= RNA_enum_get(op->ptr, "nr");
// XXX wm in context is not set correctly after WM_read_file -> crash
// do it before for now, but is this correct with multiple windows?
if(event>0) {
if (G.sce[0] && (event==1)) {
WM_event_add_notifier(C, NC_WINDOW, NULL);
WM_read_file(C, G.sce, op->reports);
}
else {
struct RecentFile *recent = BLI_findlink(&(G.recent_files), event-2);
if(recent) {
WM_event_add_notifier(C, NC_WINDOW, NULL);
WM_read_file(C, recent->filename, op->reports);
}
}
}
return 0;
}
static int wm_recentfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
char pup[2048];
recent_filelist(pup);
uiPupMenuOperator(C, 0, op, "nr", pup);
return OPERATOR_RUNNING_MODAL;
}
static void WM_OT_open_recentfile(wmOperatorType *ot)
{
ot->name= "Open Recent File";
ot->idname= "WM_OT_open_recentfile";
ot->invoke= wm_recentfile_invoke;
ot->exec= recentfile_exec;
ot->poll= WM_operator_winactive;
RNA_def_property(ot->srna, "nr", PROP_ENUM, PROP_NONE);
}
/* ********* main file *********** */
static int wm_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceFile *sfile;
ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
/* settings for filebrowser */
sfile= (SpaceFile*)CTX_wm_space_data(C);
sfile->op = op;
// XXX replace G.sce
ED_fileselect_set_params(sfile, FILE_BLENDER, "Load", G.sce, 0, 0, 0);
/* screen and area have been reset already in ED_screen_full_newspace */
return OPERATOR_RUNNING_MODAL;
}
static int wm_mainfile_exec(bContext *C, wmOperator *op)
{
char filename[FILE_MAX];
RNA_string_get(op->ptr, "filename", filename);
// XXX wm in context is not set correctly after WM_read_file -> crash
// do it before for now, but is this correct with multiple windows?
WM_event_add_notifier(C, NC_WINDOW, NULL);
WM_read_file(C, filename, op->reports);
return 0;
}
static void WM_OT_open_mainfile(wmOperatorType *ot)
{
ot->name= "Open Blender File";
ot->idname= "WM_OT_open_mainfile";
ot->invoke= wm_mainfile_invoke;
ot->exec= wm_mainfile_exec;
ot->poll= WM_operator_winactive;
ot->flag= 0;
RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
}
static int wm_save_as_mainfile_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
SpaceFile *sfile;
ED_screen_full_newspace(C, CTX_wm_area(C), SPACE_FILE);
/* settings for filebrowser */
sfile= (SpaceFile*)CTX_wm_space_data(C);
sfile->op = op;
// XXX replace G.sce
ED_fileselect_set_params(sfile, FILE_BLENDER, "Save As", G.sce, 0, 0, 0);
/* screen and area have been reset already in ED_screen_full_newspace */
return OPERATOR_RUNNING_MODAL;
}
static int wm_save_as_mainfile_exec(bContext *C, wmOperator *op)
{
char filename[FILE_MAX];
RNA_string_get(op->ptr, "filename", filename);
WM_write_file(C, filename, op->reports);
WM_event_add_notifier(C, NC_WINDOW, NULL);
return 0;
}
static void WM_OT_save_as_mainfile(wmOperatorType *ot)
{
ot->name= "Open Blender File";
ot->idname= "WM_OT_save_as_mainfile";
ot->invoke= wm_save_as_mainfile_invoke;
ot->exec= wm_save_as_mainfile_exec;
ot->poll= WM_operator_winactive;
ot->flag= 0;
RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
}
/* *********************** */
static void WM_OT_window_fullscreen_toggle(wmOperatorType *ot)
{
ot->name= "Toggle Fullscreen";
ot->idname= "WM_OT_window_fullscreen_toggle";
ot->invoke= WM_operator_confirm;
ot->exec= wm_window_fullscreen_toggle_op;
ot->poll= WM_operator_winactive;
}
static int wm_exit_blender_op(bContext *C, wmOperator *op)
{
WM_operator_free(op);
WM_exit(C);
return OPERATOR_FINISHED;
}
static void WM_OT_exit_blender(wmOperatorType *ot)
{
ot->name= "Exit Blender";
ot->idname= "WM_OT_exit_blender";
ot->invoke= WM_operator_confirm;
ot->exec= wm_exit_blender_op;
ot->poll= WM_operator_winactive;
}
/* ************ default paint cursors, draw always around cursor *********** */
/*
- returns handler to free
- poll(bContext): returns 1 if draw should happen
- draw(bContext): drawing callback for paint cursor
*/
void *WM_paint_cursor_activate(wmWindowManager *wm, int (*poll)(bContext *C),
void (*draw)(bContext *C, int, int, void *customdata), void *customdata)
{
wmPaintCursor *pc= MEM_callocN(sizeof(wmPaintCursor), "paint cursor");
BLI_addtail(&wm->paintcursors, pc);
pc->customdata = customdata;
pc->poll= poll;
pc->draw= draw;
return pc;
}
void WM_paint_cursor_end(wmWindowManager *wm, void *handle)
{
wmPaintCursor *pc;
for(pc= wm->paintcursors.first; pc; pc= pc->next) {
if(pc == (wmPaintCursor *)handle) {
BLI_remlink(&wm->paintcursors, pc);
MEM_freeN(pc);
return;
}
}
}
/* ************ window gesture operator-callback definitions ************** */
/*
* These are default callbacks for use in operators requiring gesture input
*/
/* **************** Border gesture *************** */
/* Border gesture has two types:
1) WM_GESTURE_CROSS_RECT: starts a cross, on mouse click it changes to border
2) WM_GESTURE_RECT: starts immediate as a border, on mouse click or release it ends
It stores 4 values (xmin, xmax, ymin, ymax) and event it ended with (event_type)
*/
static int border_apply(bContext *C, wmOperator *op, int event_type)
{
wmGesture *gesture= op->customdata;
rcti *rect= gesture->customdata;
if(rect->xmin > rect->xmax)
SWAP(int, rect->xmin, rect->xmax);
if(rect->ymin > rect->ymax)
SWAP(int, rect->ymin, rect->ymax);
if(rect->xmin==rect->xmax || rect->ymin==rect->ymax)
return 0;
/* operator arguments and storage. */
RNA_int_set(op->ptr, "xmin", rect->xmin);
RNA_int_set(op->ptr, "ymin", rect->ymin);
RNA_int_set(op->ptr, "xmax", rect->xmax);
RNA_int_set(op->ptr, "ymax", rect->ymax);
if( RNA_struct_find_property(op->ptr, "event_type") )
RNA_int_set(op->ptr, "event_type", event_type);
op->type->exec(C, op);
return 1;
}
static void wm_gesture_end(bContext *C, wmOperator *op)
{
wmGesture *gesture= op->customdata;
WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
op->customdata= NULL;
ED_area_tag_redraw(CTX_wm_area(C));
}
int WM_border_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
op->customdata= WM_gesture_new(C, event, WM_GESTURE_CROSS_RECT);
/* add modal handler */
WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
wm_gesture_tag_redraw(C);
return OPERATOR_RUNNING_MODAL;
}
int WM_border_select_modal(bContext *C, wmOperator *op, wmEvent *event)
{
wmGesture *gesture= op->customdata;
rcti *rect= gesture->customdata;
int sx, sy;
switch(event->type) {
case MOUSEMOVE:
wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
rect->xmin= rect->xmax= event->x - sx;
rect->ymin= rect->ymax= event->y - sy;
}
else {
rect->xmax= event->x - sx;
rect->ymax= event->y - sy;
}
wm_gesture_tag_redraw(C);
break;
case LEFTMOUSE:
case MIDDLEMOUSE:
case RIGHTMOUSE:
if(event->val==1) {
if(gesture->type==WM_GESTURE_CROSS_RECT && gesture->mode==0) {
gesture->mode= 1;
wm_gesture_tag_redraw(C);
}
}
else {
if(border_apply(C, op, event->type)) {
wm_gesture_end(C, op);
return OPERATOR_FINISHED;
}
wm_gesture_end(C, op);
return OPERATOR_CANCELLED;
}
break;
case ESCKEY:
wm_gesture_end(C, op);
return OPERATOR_CANCELLED;
}
return OPERATOR_RUNNING_MODAL;
}
/* **************** circle gesture *************** */
/* works now only for selection or modal paint stuff, calls exec while hold mouse, exit on release */
int WM_gesture_circle_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
op->customdata= WM_gesture_new(C, event, WM_GESTURE_CIRCLE);
/* add modal handler */
WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
wm_gesture_tag_redraw(C);
return OPERATOR_RUNNING_MODAL;
}
static void gesture_circle_apply(bContext *C, wmOperator *op)
{
wmGesture *gesture= op->customdata;
rcti *rect= gesture->customdata;
/* operator arguments and storage. */
RNA_int_set(op->ptr, "x", rect->xmin);
RNA_int_set(op->ptr, "y", rect->ymin);
RNA_int_set(op->ptr, "radius", rect->xmax);
if(op->type->exec)
op->type->exec(C, op);
}
int WM_gesture_circle_modal(bContext *C, wmOperator *op, wmEvent *event)
{
wmGesture *gesture= op->customdata;
rcti *rect= gesture->customdata;
int sx, sy;
switch(event->type) {
case MOUSEMOVE:
wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
rect->xmin= event->x - sx;
rect->ymin= event->y - sy;
wm_gesture_tag_redraw(C);
if(gesture->mode)
gesture_circle_apply(C, op);
break;
case WHEELUPMOUSE:
rect->xmax += 2 + rect->xmax/10;
wm_gesture_tag_redraw(C);
break;
case WHEELDOWNMOUSE:
rect->xmax -= 2 + rect->xmax/10;
if(rect->xmax < 1) rect->xmax= 1;
wm_gesture_tag_redraw(C);
break;
case LEFTMOUSE:
case MIDDLEMOUSE:
case RIGHTMOUSE:
if(event->val==0) { /* key release */
wm_gesture_end(C, op);
return OPERATOR_FINISHED;
}
else {
if( RNA_struct_find_property(op->ptr, "event_type") )
RNA_int_set(op->ptr, "event_type", event->type);
/* apply first click */
gesture_circle_apply(C, op);
gesture->mode= 1;
}
break;
case ESCKEY:
wm_gesture_end(C, op);
return OPERATOR_CANCELLED;
}
return OPERATOR_RUNNING_MODAL;
}
#if 0
/* template to copy from */
void WM_OT_circle_gesture(wmOperatorType *ot)
{
ot->name= "Circle Gesture";
ot->idname= "WM_OT_circle_gesture";
ot->invoke= WM_gesture_circle_invoke;
ot->modal= WM_gesture_circle_modal;
ot->poll= WM_operator_winactive;
RNA_def_property(ot->srna, "x", PROP_INT, PROP_NONE);
RNA_def_property(ot->srna, "y", PROP_INT, PROP_NONE);
RNA_def_property(ot->srna, "radius", PROP_INT, PROP_NONE);
}
#endif
/* **************** Tweak gesture *************** */
static void tweak_gesture_modal(bContext *C, wmEvent *event)
{
wmWindow *window= CTX_wm_window(C);
wmGesture *gesture= window->tweak;
rcti *rect= gesture->customdata;
int sx, sy, val;
switch(event->type) {
case MOUSEMOVE:
wm_subwindow_getorigin(window, gesture->swinid, &sx, &sy);
rect->xmax= event->x - sx;
rect->ymax= event->y - sy;
if((val= wm_gesture_evaluate(C, gesture))) {
wmEvent event;
event= *(window->eventstate);
if(gesture->event_type==LEFTMOUSE)
event.type= EVT_TWEAK_L;
else if(gesture->event_type==RIGHTMOUSE)
event.type= EVT_TWEAK_R;
else
event.type= EVT_TWEAK_M;
event.val= val;
/* mouse coords! */
wm_event_add(window, &event);
WM_gesture_end(C, gesture); /* frees gesture itself, and unregisters from window */
window->tweak= NULL;
}
break;
case LEFTMOUSE:
case RIGHTMOUSE:
case MIDDLEMOUSE:
if(gesture->event_type==event->type) {
WM_gesture_end(C, gesture);
window->tweak= NULL;
/* when tweak fails we should give the other keymap entries a chance
* those then won't react to km_press, but km_release
* it sets hidden event value where tweak maps fail on, to prevent loops */
//event->val= 1;
//event->no_tweak= 1;
}
break;
default:
WM_gesture_end(C, gesture);
window->tweak= NULL;
}
}
/* standard tweak, called after window handlers passed on event */
void wm_tweakevent_test(bContext *C, wmEvent *event, int action)
{
wmWindow *win= CTX_wm_window(C);
if(win->tweak==NULL) {
if(CTX_wm_region(C)) {
if(event->val) { // pressed
if( ELEM3(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE) )
win->tweak= WM_gesture_new(C, event, WM_GESTURE_TWEAK);
}
}
}
else {
if(action==WM_HANDLER_BREAK) {
WM_gesture_end(C, win->tweak);
win->tweak= NULL;
}
else
tweak_gesture_modal(C, event);
}
}
/* *********************** lasso gesture ****************** */
int WM_gesture_lasso_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
op->customdata= WM_gesture_new(C, event, WM_GESTURE_LASSO);
/* add modal handler */
WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
wm_gesture_tag_redraw(C);
return OPERATOR_RUNNING_MODAL;
}
static void gesture_lasso_apply(bContext *C, wmOperator *op, int event_type)
{
wmGesture *gesture= op->customdata;
PointerRNA itemptr;
float loc[2];
int i;
short *lasso= gesture->customdata;
/* operator storage as path. */
for(i=0; i<gesture->points; i++, lasso+=2) {
loc[0]= lasso[0];
loc[1]= lasso[1];
RNA_collection_add(op->ptr, "path", &itemptr);
RNA_float_set_array(&itemptr, "loc", loc);
}
wm_gesture_end(C, op);
if(op->type->exec)
op->type->exec(C, op);
}
int WM_gesture_lasso_modal(bContext *C, wmOperator *op, wmEvent *event)
{
wmGesture *gesture= op->customdata;
int sx, sy;
switch(event->type) {
case MOUSEMOVE:
wm_gesture_tag_redraw(C);
wm_subwindow_getorigin(CTX_wm_window(C), gesture->swinid, &sx, &sy);
if(gesture->points < WM_LASSO_MAX_POINTS) {
short *lasso= gesture->customdata;
lasso += 2 * gesture->points;
lasso[0] = event->x - sx;
lasso[1] = event->y - sy;
gesture->points++;
}
else {
gesture_lasso_apply(C, op, event->type);
return OPERATOR_FINISHED;
}
break;
case LEFTMOUSE:
case MIDDLEMOUSE:
case RIGHTMOUSE:
if(event->val==0) { /* key release */
gesture_lasso_apply(C, op, event->type);
return OPERATOR_FINISHED;
}
break;
case ESCKEY:
wm_gesture_end(C, op);
return OPERATOR_CANCELLED;
}
return OPERATOR_RUNNING_MODAL;
}
#if 0
/* template to copy from */
static int gesture_lasso_exec(bContext *C, wmOperator *op)
{
RNA_BEGIN(op->ptr, itemptr, "path") {
float loc[2];
RNA_float_get_array(&itemptr, "loc", loc);
printf("Location: %f %f\n", loc[0], loc[1]);
}
RNA_END;
return OPERATOR_FINISHED;
}
void WM_OT_lasso_gesture(wmOperatorType *ot)
{
PropertyRNA *prop;
ot->name= "Lasso Gesture";
ot->idname= "WM_OT_lasso_gesture";
ot->invoke= WM_gesture_lasso_invoke;
ot->modal= WM_gesture_lasso_modal;
ot->exec= gesture_lasso_exec;
ot->poll= WM_operator_winactive;
prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
}
#endif
/* *********************** radial control ****************** */
const int WM_RADIAL_CONTROL_DISPLAY_SIZE = 200;
typedef struct wmRadialControl {
int mode;
float initial_value, value, max_value;
int initial_mouse[2];
void *cursor;
GLuint tex;
} wmRadialControl;
static void wm_radial_control_paint(bContext *C, int x, int y, void *customdata)
{
wmRadialControl *rc = (wmRadialControl*)customdata;
ARegion *ar = CTX_wm_region(C);
float r1, r2, r3, angle;
/* Keep cursor in the original place */
x = rc->initial_mouse[0] - ar->winrct.xmin;
y = rc->initial_mouse[1] - ar->winrct.ymin;
glPushMatrix();
glTranslatef((float)x, (float)y, 0.0f);
if(rc->mode == WM_RADIALCONTROL_SIZE) {
r1= rc->value;
r2= rc->initial_value;
r3= r1;
} else if(rc->mode == WM_RADIALCONTROL_STRENGTH) {
r1= (1 - rc->value) * WM_RADIAL_CONTROL_DISPLAY_SIZE;
r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
} else if(rc->mode == WM_RADIALCONTROL_ANGLE) {
r1= r2= WM_RADIAL_CONTROL_DISPLAY_SIZE;
r3= WM_RADIAL_CONTROL_DISPLAY_SIZE;
angle = rc->value;
}
glColor4ub(255, 255, 255, 128);
glEnable( GL_LINE_SMOOTH );
glEnable(GL_BLEND);
if(rc->mode == WM_RADIALCONTROL_ANGLE)
fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
if(rc->tex) {
const float str = rc->mode == WM_RADIALCONTROL_STRENGTH ? (rc->value + 0.5) : 1;
if(rc->mode == WM_RADIALCONTROL_ANGLE) {
glRotatef(angle, 0, 0, 1);
fdrawline(0, 0, WM_RADIAL_CONTROL_DISPLAY_SIZE, 0);
}
glBindTexture(GL_TEXTURE_2D, rc->tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor4f(0,0,0, str);
glTexCoord2f(0,0);
glVertex2f(-r3, -r3);
glTexCoord2f(1,0);
glVertex2f(r3, -r3);
glTexCoord2f(1,1);
glVertex2f(r3, r3);
glTexCoord2f(0,1);
glVertex2f(-r3, r3);
glEnd();
glDisable(GL_TEXTURE_2D);
}
glColor4ub(255, 255, 255, 128);
glutil_draw_lined_arc(0.0, M_PI*2.0, r1, 40);
glutil_draw_lined_arc(0.0, M_PI*2.0, r2, 40);
glDisable(GL_BLEND);
glDisable( GL_LINE_SMOOTH );
glPopMatrix();
}
int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
{
wmRadialControl *rc = (wmRadialControl*)op->customdata;
int mode, initial_mouse[2], delta[2];
float dist;
double new_value = RNA_float_get(op->ptr, "new_value");
int ret = OPERATOR_RUNNING_MODAL;
mode = RNA_int_get(op->ptr, "mode");
RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);
switch(event->type) {
case MOUSEMOVE:
delta[0]= initial_mouse[0] - event->x;
delta[1]= initial_mouse[1] - event->y;
dist= sqrt(delta[0]*delta[0]+delta[1]*delta[1]);
if(mode == WM_RADIALCONTROL_SIZE)
new_value = dist;
else if(mode == WM_RADIALCONTROL_STRENGTH) {
new_value = 1 - dist / WM_RADIAL_CONTROL_DISPLAY_SIZE;
} else if(mode == WM_RADIALCONTROL_ANGLE)
new_value = ((int)(atan2(delta[1], delta[0]) * (180.0 / M_PI)) + 180);
if(event->ctrl) {
if(mode == WM_RADIALCONTROL_STRENGTH)
new_value = ((int)(new_value * 100) / 10*10) / 100.0f;
else
new_value = ((int)new_value + 5) / 10*10;
}
break;
case ESCKEY:
case RIGHTMOUSE:
ret = OPERATOR_CANCELLED;
break;
case LEFTMOUSE:
case PADENTER:
op->type->exec(C, op);
ret = OPERATOR_FINISHED;
break;
}
/* Clamp */
if(new_value > rc->max_value)
new_value = rc->max_value;
else if(new_value < 0)
new_value = 0;
/* Update paint data */
rc->value = new_value;
RNA_float_set(op->ptr, "new_value", new_value);
if(ret != OPERATOR_RUNNING_MODAL) {
WM_paint_cursor_end(CTX_wm_manager(C), rc->cursor);
MEM_freeN(rc);
}
ED_region_tag_redraw(CTX_wm_region(C));
return ret;
}
/* Expects the operator customdata to be an ImBuf (or NULL) */
int WM_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
wmRadialControl *rc = MEM_callocN(sizeof(wmRadialControl), "radial control");
int mode = RNA_int_get(op->ptr, "mode");
float initial_value = RNA_float_get(op->ptr, "initial_value");
int mouse[2] = {event->x, event->y};
if(mode == WM_RADIALCONTROL_SIZE) {
rc->max_value = 200;
mouse[0]-= initial_value;
}
else if(mode == WM_RADIALCONTROL_STRENGTH) {
rc->max_value = 1;
mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * (1 - initial_value);
}
else if(mode == WM_RADIALCONTROL_ANGLE) {
rc->max_value = 360;
mouse[0]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * cos(initial_value);
mouse[1]-= WM_RADIAL_CONTROL_DISPLAY_SIZE * sin(initial_value);
initial_value *= 180.0f/M_PI;
}
if(op->customdata) {
ImBuf *im = (ImBuf*)op->customdata;
/* Build GL texture */
glGenTextures(1, &rc->tex);
glBindTexture(GL_TEXTURE_2D, rc->tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, im->x, im->y, 0, GL_ALPHA, GL_FLOAT, im->rect_float);
MEM_freeN(im->rect_float);
MEM_freeN(im);
}
RNA_int_set_array(op->ptr, "initial_mouse", mouse);
RNA_float_set(op->ptr, "new_value", initial_value);
op->customdata = rc;
rc->mode = mode;
rc->initial_value = initial_value;
rc->initial_mouse[0] = mouse[0];
rc->initial_mouse[1] = mouse[1];
rc->cursor = WM_paint_cursor_activate(CTX_wm_manager(C), op->type->poll,
wm_radial_control_paint, op->customdata);
/* add modal handler */
WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
WM_radial_control_modal(C, op, event);
return OPERATOR_RUNNING_MODAL;
}
/* Gets a descriptive string of the operation */
void WM_radial_control_string(wmOperator *op, char str[], int maxlen)
{
int mode = RNA_int_get(op->ptr, "mode");
float v = RNA_float_get(op->ptr, "new_value");
if(mode == WM_RADIALCONTROL_SIZE)
sprintf(str, "Size: %d", (int)v);
else if(mode == WM_RADIALCONTROL_STRENGTH)
sprintf(str, "Strength: %d", (int)v);
else if(mode == WM_RADIALCONTROL_ANGLE)
sprintf(str, "Angle: %d", (int)(v * 180.0f/M_PI));
}
/** Important: this doesn't define an actual operator, it
just sets up the common parts of the radial control op. **/
void WM_OT_radial_control_partial(wmOperatorType *ot)
{
static EnumPropertyItem prop_mode_items[] = {
{WM_RADIALCONTROL_SIZE, "SIZE", "Size", ""},
{WM_RADIALCONTROL_STRENGTH, "STRENGTH", "Strength", ""},
{WM_RADIALCONTROL_ANGLE, "ANGLE", "Angle", ""},
{0, NULL, NULL, NULL}};
/* Should be set in custom invoke() */
RNA_def_float(ot->srna, "initial_value", 0, 0, FLT_MAX, "Initial Value", "", 0, FLT_MAX);
/* Set internally, should be used in custom exec() to get final value */
RNA_def_float(ot->srna, "new_value", 0, 0, FLT_MAX, "New Value", "", 0, FLT_MAX);
/* Should be set before calling operator */
RNA_def_enum(ot->srna, "mode", prop_mode_items, 0, "Mode", "");
/* Internal */
RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
}
/* ******************************************************* */
/* called on initialize WM_exit() */
void wm_operatortype_free(void)
{
BLI_freelistN(&global_ops);
}
/* called on initialize WM_init() */
void wm_operatortype_init(void)
{
WM_operatortype_append(WM_OT_window_duplicate);
WM_operatortype_append(WM_OT_save_homefile);
WM_operatortype_append(WM_OT_window_fullscreen_toggle);
WM_operatortype_append(WM_OT_exit_blender);
WM_operatortype_append(WM_OT_open_recentfile);
WM_operatortype_append(WM_OT_open_mainfile);
WM_operatortype_append(WM_OT_jobs_timer);
WM_operatortype_append(WM_OT_save_as_mainfile);
}
/* default keymap for windows and screens, only call once per WM */
void wm_window_keymap(wmWindowManager *wm)
{
ListBase *keymap= WM_keymap_listbase(wm, "Window", 0, 0);
/* items to make WM work */
WM_keymap_verify_item(keymap, "WM_OT_jobs_timer", TIMERJOBS, KM_ANY, KM_ANY, 0);
/* note, this doesn't replace existing keymap items */
WM_keymap_verify_item(keymap, "WM_OT_window_duplicate", AKEY, KM_PRESS, KM_CTRL|KM_ALT, 0);
WM_keymap_verify_item(keymap, "WM_OT_save_homefile", UKEY, KM_PRESS, KM_CTRL, 0);
WM_keymap_verify_item(keymap, "WM_OT_open_recentfile", OKEY, KM_PRESS, KM_CTRL, 0);
WM_keymap_verify_item(keymap, "WM_OT_open_mainfile", F1KEY, KM_PRESS, 0, 0);
WM_keymap_verify_item(keymap, "WM_OT_save_as_mainfile", F2KEY, KM_PRESS, 0, 0);
WM_keymap_verify_item(keymap, "WM_OT_window_fullscreen_toggle", F11KEY, KM_PRESS, 0, 0);
WM_keymap_verify_item(keymap, "WM_OT_exit_blender", QKEY, KM_PRESS, KM_CTRL, 0);
}
|