Merging r58475 through r58700 from trunk into soc-2013-depsgraph_mt
[blender.git] / source / blender / editors / space_logic / logic_buttons.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 by Blender Foundation
19  * All rights reserved.
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/editors/space_logic/logic_buttons.c
25  *  \ingroup splogic
26  */
27
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "BLI_blenlib.h"
33 #include "BLI_math.h"
34 #include "BLI_utildefines.h"
35
36 #include "BKE_context.h"
37 #include "BKE_screen.h"
38
39 #include "ED_screen.h"
40
41 #include "RNA_access.h"
42 #include "RNA_define.h"
43
44 #include "WM_api.h"
45 #include "WM_types.h"
46
47 #include "UI_interface.h"
48 #include "UI_view2d.h"
49
50 #include "interface_intern.h"
51 #include "logic_intern.h"
52
53 static int logic_properties_toggle_exec(bContext *C, wmOperator *UNUSED(op))
54 {
55         ScrArea *sa = CTX_wm_area(C);
56         ARegion *ar = logic_has_buttons_region(sa);
57         
58         if (ar)
59                 ED_region_toggle_hidden(C, ar);
60
61         return OPERATOR_FINISHED;
62 }
63
64 void LOGIC_OT_properties(wmOperatorType *ot)
65 {
66         ot->name = "Properties";
67         ot->description = "Toggle display properties panel";
68         ot->idname = "LOGIC_OT_properties";
69         
70         ot->exec = logic_properties_toggle_exec;
71         ot->poll = ED_operator_logic_active;
72         
73         /* flags */
74         ot->flag = 0;
75 }
76
77 /* Remove Logic Bricks Connections */
78 /* ********************** Cut Link operator ***************** */
79
80 #define LINK_RESOL 12
81 static int cut_links_intersect(uiLinkLine *line, float mcoords[][2], int tot)
82 {
83         float coord_array[LINK_RESOL+1][2];
84         int i, b;
85         rcti rectlink;
86
87         rectlink.xmin = (int)BLI_rctf_cent_x(&line->from->rect);
88         rectlink.ymin = (int)BLI_rctf_cent_y(&line->from->rect);
89         rectlink.xmax = (int)BLI_rctf_cent_x(&line->to->rect);
90         rectlink.ymax = (int)BLI_rctf_cent_y(&line->to->rect);
91
92         if (ui_link_bezier_points(&rectlink, coord_array, LINK_RESOL)) {
93                 for (i=0; i<tot-1; i++)
94                         for (b=0; b<LINK_RESOL-1; b++)
95                                 if (isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
96                                         return 1;
97         }
98         return 0;
99 }
100
101 static int cut_links_exec(bContext *C, wmOperator *op)
102 {
103         ARegion *ar = CTX_wm_region(C);
104         float mcoords[256][2];
105         int i = 0;
106         
107         RNA_BEGIN (op->ptr, itemptr, "path")
108         {
109                 float loc[2];
110                 
111                 RNA_float_get_array(&itemptr, "loc", loc);
112                 UI_view2d_region_to_view(&ar->v2d,
113                                          (int)loc[0], (int)loc[1],
114                                          &mcoords[i][0], &mcoords[i][1]);
115                 i++;
116                 if (i >= 256) break;
117         }
118         RNA_END;
119
120         if (i>1) {
121                 uiBlock *block;
122                 uiLinkLine *line, *nline;
123                 uiBut *but;
124                 for (block = ar->uiblocks.first; block; block = block->next) {
125                         but = block->buttons.first;
126                         while (but) {
127                                 if (but->type==LINK && but->link) {
128                                         for (line = but->link->lines.first; line; line = nline) {
129                                                 nline = line->next;
130
131                                                 if (cut_links_intersect(line, mcoords, i)) {
132                                                         ui_delete_linkline(line, but);
133                                                 }
134                                         }
135                                 }
136                                 but = but->next;
137                         }
138                 }
139                 return OPERATOR_FINISHED;
140         }
141         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
142 }
143
144 void LOGIC_OT_links_cut(wmOperatorType *ot)
145 {
146         PropertyRNA *prop;
147         
148         ot->name = "Cut Links";
149         ot->idname = "LOGIC_OT_links_cut";
150         ot->description = "Remove logic brick connections";
151         
152         ot->invoke = WM_gesture_lines_invoke;
153         ot->modal = WM_gesture_lines_modal;
154         ot->exec = cut_links_exec;
155         ot->cancel = WM_gesture_lines_cancel;
156         
157         ot->poll = ED_operator_logic_active;
158         
159         /* flags */
160         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
161         
162         prop = RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
163         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
164         /* internal */
165         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
166 }
167