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