Code cleanup:
[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(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;
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) (line->from->x1 + line->from->x2) / 2;
88         rectlink.ymin = (int) (line->from->y1 + line->from->y2) / 2;
89         rectlink.xmax = (int) (line->to->x1 + line->to->x2) / 2;
90         rectlink.ymax = (int) (line->to->y1 + line->to->y2) / 2;
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                 float loc[2];
109                 
110                 RNA_float_get_array(&itemptr, "loc", loc);
111                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
112                                                                  &mcoords[i][0], &mcoords[i][1]);
113                 i++;
114                 if (i>= 256) break;
115         }
116         RNA_END;
117
118         if (i>1) {
119                 uiBlock *block;
120                 uiLinkLine *line, *nline;
121                 uiBut *but;
122                 for (block= ar->uiblocks.first; block; block= block->next)
123                 {
124                         but= block->buttons.first;
125                         while (but) {
126                                 if (but->type==LINK && but->link) {
127                                         for (line= but->link->lines.first; line; line= nline) {
128                                                 nline= line->next;
129
130                                                 if (cut_links_intersect(line, mcoords, i)) {
131                                                         ui_delete_linkline(line, but);
132                                                 }
133                                         }
134                                 }
135                                 but= but->next;
136                         }
137                 }
138                 return OPERATOR_FINISHED;
139         }       
140         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
141 }
142
143 void LOGIC_OT_links_cut(wmOperatorType *ot)
144 {
145         PropertyRNA *prop;
146         
147         ot->name = "Cut links";
148         ot->idname = "LOGIC_OT_links_cut";
149         ot->description = "Remove logic brick connections";
150         
151         ot->invoke = WM_gesture_lines_invoke;
152         ot->modal = WM_gesture_lines_modal;
153         ot->exec = cut_links_exec;
154         ot->cancel = WM_gesture_lines_cancel;
155         
156         ot->poll = ED_operator_logic_active;
157         
158         /* flags */
159         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
160         
161         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
162         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
163         /* internal */
164         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
165 }
166