merge with/from trunk at r35190
[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 #include <string.h>
27 #include <stdio.h>
28
29 #include "DNA_meshdata_types.h"
30
31 #include "BLI_blenlib.h"
32 #include "BLI_math.h"
33 #include "BLI_utildefines.h"
34
35 #include "BKE_context.h"
36 #include "BKE_screen.h"
37
38 #include "ED_screen.h"
39
40 #include "RNA_access.h"
41 #include "RNA_define.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "UI_interface.h"
47 #include "UI_view2d.h"
48
49 #include "interface_intern.h"
50 #include "logic_intern.h"
51
52 #if 0
53 static void do_logic_panel_events(bContext *C, void *arg, int event)
54 {
55         
56         switch(event) {
57                 
58         }
59 }
60
61
62 /* *** */
63
64 static void logic_panel_properties(const bContext *C, Panel *pa)
65 {
66 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
67         uiBlock *block;
68         
69         block= uiLayoutAbsoluteBlock(pa->layout);
70         uiBlockSetHandleFunc(block, do_logic_panel_events, NULL);
71
72 }       
73
74 static void logic_panel_view_properties(const bContext *C, Panel *pa)
75 {
76         //      SpaceLogic *slogic= CTX_wm_space_logic(C);
77         uiBlock *block;
78         
79         block= uiLayoutAbsoluteBlock(pa->layout);
80         uiBlockSetHandleFunc(block, do_logic_panel_events, NULL);
81         
82 }       
83 #endif
84
85 void logic_buttons_register(ARegionType *UNUSED(art))
86 {
87 #if 0
88         PanelType *pt;
89
90         pt= MEM_callocN(sizeof(PanelType), "spacetype logic panel properties");
91         strcpy(pt->idname, "LOGIC_PT_properties");
92         strcpy(pt->label, "Logic Properties");
93         pt->draw= logic_panel_properties;
94         BLI_addtail(&art->paneltypes, pt);
95
96         pt= MEM_callocN(sizeof(PanelType), "spacetype logic view properties");
97         strcpy(pt->idname, "LOGIC_PT_view_properties");
98         strcpy(pt->label, "View Properties");
99         pt->draw= logic_panel_view_properties;
100         BLI_addtail(&art->paneltypes, pt);
101 #endif
102
103 }
104
105 static int logic_properties(bContext *C, wmOperator *UNUSED(op))
106 {
107         ScrArea *sa= CTX_wm_area(C);
108         ARegion *ar= logic_has_buttons_region(sa);
109         
110         if(ar)
111                 ED_region_toggle_hidden(C, ar);
112
113         return OPERATOR_FINISHED;
114 }
115
116 void LOGIC_OT_properties(wmOperatorType *ot)
117 {
118         ot->name= "Properties";
119         ot->description= "Toggle display properties panel";
120         ot->idname= "LOGIC_OT_properties";
121         
122         ot->exec= logic_properties;
123         ot->poll= ED_operator_logic_active;
124         
125         /* flags */
126         ot->flag= 0;
127 }
128
129 /* Remove Logic Bricks Connections */
130 /* ********************** Cut Link operator ***************** */
131
132 #define LINK_RESOL 12
133 static int cut_links_intersect(uiLinkLine *line, float mcoords[][2], int tot)
134 {
135         float coord_array[LINK_RESOL+1][2];
136         int i, b;
137         rcti rectlink;
138
139         rectlink.xmin= (int) (line->from->x1 + line->from->x2) / 2;
140         rectlink.ymin= (int) (line->from->y1 + line->from->y2) / 2;
141         rectlink.xmax= (int) (line->to->x1 + line->to->x2) / 2;
142         rectlink.ymax= (int) (line->to->y1 + line->to->y2) / 2;
143
144         if(ui_link_bezier_points(&rectlink, coord_array, LINK_RESOL)){
145                 for(i=0; i<tot-1; i++)
146                         for(b=0; b<LINK_RESOL-1; b++)
147                                 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
148                                         return 1;
149         }
150         return 0;
151 }
152
153 static int cut_links_exec(bContext *C, wmOperator *op)
154 {
155         ARegion *ar= CTX_wm_region(C);
156         float mcoords[256][2];
157         int i= 0;
158         
159         RNA_BEGIN(op->ptr, itemptr, "path") {
160                 float loc[2];
161                 
162                 RNA_float_get_array(&itemptr, "loc", loc);
163                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
164                                                                  &mcoords[i][0], &mcoords[i][1]);
165                 i++;
166                 if(i>= 256) break;
167         }
168         RNA_END;
169
170         if (i>1) {
171                 uiBlock *block;
172                 uiLinkLine *line, *nline;
173                 uiBut *but;
174                 for(block= ar->uiblocks.first; block; block= block->next)
175                 {
176                         but= block->buttons.first;
177                         while(but) {
178                                 if(but->type==LINK && but->link) {
179                                         for(line= but->link->lines.first; line; line= nline) {
180                                                 nline= line->next;
181
182                                                 if(cut_links_intersect(line, mcoords, i)) {
183                                                         ui_delete_linkline(line, but);
184                                                 }
185                                         }
186                                 }
187                                 but= but->next;
188                         }
189                 }
190                 return OPERATOR_FINISHED;
191         }       
192         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
193 }
194
195 void LOGIC_OT_links_cut(wmOperatorType *ot)
196 {
197         PropertyRNA *prop;
198         
199         ot->name= "Cut links";
200         ot->idname= "LOGIC_OT_links_cut";
201         ot->description= "Remove logic brick connections";
202         
203         ot->invoke= WM_gesture_lines_invoke;
204         ot->modal= WM_gesture_lines_modal;
205         ot->exec= cut_links_exec;
206         
207         ot->poll= ED_operator_logic_active;
208         
209         /* flags */
210         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
211         
212         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
213         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
214         /* internal */
215         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
216 }
217