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