merge with trunk at r31523
[blender.git] / source / blender / editors / space_logic / logic_buttons.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2009 by Blender Foundation
21  * All rights reserved.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "DNA_meshdata_types.h"
31
32
33 #include "BLI_blenlib.h"
34 #include "BLI_math.h"
35
36 #include "BKE_context.h"
37 #include "BKE_screen.h"
38
39 #include "ED_screen.h"
40
41
42 #include "RNA_access.h"
43 #include "RNA_define.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "UI_interface.h"
49 #include "UI_view2d.h"
50
51 #include "interface_intern.h"
52 #include "logic_intern.h"
53
54 #if 0
55 static void do_logic_panel_events(bContext *C, void *arg, int event)
56 {
57         
58         switch(event) {
59                 
60         }
61 }
62
63
64 /* *** */
65
66 static void logic_panel_properties(const bContext *C, Panel *pa)
67 {
68 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
69         uiBlock *block;
70         
71         block= uiLayoutAbsoluteBlock(pa->layout);
72         uiBlockSetHandleFunc(block, do_logic_panel_events, NULL);
73
74 }       
75
76 static void logic_panel_view_properties(const bContext *C, Panel *pa)
77 {
78         //      SpaceLogic *slogic= CTX_wm_space_logic(C);
79         uiBlock *block;
80         
81         block= uiLayoutAbsoluteBlock(pa->layout);
82         uiBlockSetHandleFunc(block, do_logic_panel_events, NULL);
83         
84 }       
85 #endif
86
87 void logic_buttons_register(ARegionType *art)
88 {
89 #if 0
90         PanelType *pt;
91
92         pt= MEM_callocN(sizeof(PanelType), "spacetype logic panel properties");
93         strcpy(pt->idname, "LOGIC_PT_properties");
94         strcpy(pt->label, "Logic Properties");
95         pt->draw= logic_panel_properties;
96         BLI_addtail(&art->paneltypes, pt);
97
98         pt= MEM_callocN(sizeof(PanelType), "spacetype logic view properties");
99         strcpy(pt->idname, "LOGIC_PT_view_properties");
100         strcpy(pt->label, "View Properties");
101         pt->draw= logic_panel_view_properties;
102         BLI_addtail(&art->paneltypes, pt);
103 #endif
104
105 }
106
107 static int logic_properties(bContext *C, wmOperator *op)
108 {
109         ScrArea *sa= CTX_wm_area(C);
110         ARegion *ar= logic_has_buttons_region(sa);
111         
112         if(ar)
113                 ED_region_toggle_hidden(C, ar);
114
115         return OPERATOR_FINISHED;
116 }
117
118 void LOGIC_OT_properties(wmOperatorType *ot)
119 {
120         ot->name= "Properties";
121         ot->description= "Toggle display properties panel";
122         ot->idname= "LOGIC_OT_properties";
123         
124         ot->exec= logic_properties;
125         ot->poll= ED_operator_logic_active;
126         
127         /* flags */
128         ot->flag= 0;
129 }
130
131 /* Remove Logic Bricks Connections */
132 /* ********************** Cut Link operator ***************** */
133
134 #define LINK_RESOL 12
135 static int cut_links_intersect(uiLinkLine *line, float mcoords[][2], int tot)
136 {
137         float coord_array[LINK_RESOL+1][2];
138         int i, b;
139         rcti rectlink;
140
141         rectlink.xmin= (int) (line->from->x1 + line->from->x2) / 2;
142         rectlink.ymin= (int) (line->from->y1 + line->from->y2) / 2;
143         rectlink.xmax= (int) (line->to->x1 + line->to->x2) / 2;
144         rectlink.ymax= (int) (line->to->y1 + line->to->y2) / 2;
145
146         if(ui_link_bezier_points(&rectlink, coord_array, LINK_RESOL)){
147                 for(i=0; i<tot-1; i++)
148                         for(b=0; b<LINK_RESOL-1; b++)
149                                 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
150                                         return 1;
151         }
152         return 0;
153 }
154
155 static int cut_links_exec(bContext *C, wmOperator *op)
156 {
157         ARegion *ar= CTX_wm_region(C);
158         float mcoords[256][2];
159         int i= 0;
160         
161         RNA_BEGIN(op->ptr, itemptr, "path") {
162                 float loc[2];
163                 
164                 RNA_float_get_array(&itemptr, "loc", loc);
165                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
166                                                                  &mcoords[i][0], &mcoords[i][1]);
167                 i++;
168                 if(i>= 256) break;
169         }
170         RNA_END;
171
172         if (i>1) {
173                 uiBlock *block;
174                 uiLinkLine *line, *nline;
175                 uiBut *but;
176                 for(block= ar->uiblocks.first; block; block= block->next)
177                 {
178                         but= block->buttons.first;
179                         while(but) {
180                                 if(but->type==LINK && but->link) {
181                                         for(line= but->link->lines.first; line; line= nline) {
182                                                 nline= line->next;
183
184                                                 if(cut_links_intersect(line, mcoords, i)) {
185                                                         ui_delete_linkline(line, but);
186                                                 }
187                                         }
188                                 }
189                                 but= but->next;
190                         }
191                 }
192                 return OPERATOR_FINISHED;
193         }       
194         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
195 }
196
197 void LOGIC_OT_links_cut(wmOperatorType *ot)
198 {
199         PropertyRNA *prop;
200         
201         ot->name= "Cut links";
202         ot->idname= "LOGIC_OT_links_cut";
203         
204         ot->invoke= WM_gesture_lines_invoke;
205         ot->modal= WM_gesture_lines_modal;
206         ot->exec= cut_links_exec;
207         
208         ot->poll= ED_operator_logic_active;
209         
210         /* flags */
211         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
212         
213         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
214         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
215         /* internal */
216         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
217 }
218