Blender  V2.59
logic_buttons.c
Go to the documentation of this file.
00001 /*
00002  * $Id: logic_buttons.c 37246 2011-06-06 11:04:54Z nazgul $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2009 by Blender Foundation
00021  * All rights reserved.
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include <string.h>
00032 #include <stdio.h>
00033 
00034 #include "BLI_blenlib.h"
00035 #include "BLI_math.h"
00036 #include "BLI_utildefines.h"
00037 
00038 #include "BKE_context.h"
00039 #include "BKE_screen.h"
00040 
00041 #include "ED_screen.h"
00042 
00043 #include "RNA_access.h"
00044 #include "RNA_define.h"
00045 
00046 #include "WM_api.h"
00047 #include "WM_types.h"
00048 
00049 #include "UI_interface.h"
00050 #include "UI_view2d.h"
00051 
00052 #include "interface_intern.h"
00053 #include "logic_intern.h"
00054 
00055 #if 0
00056 static void do_logic_panel_events(bContext *C, void *arg, int event)
00057 {
00058         
00059         switch(event) {
00060                 
00061         }
00062 }
00063 
00064 
00065 /* *** */
00066 
00067 static void logic_panel_properties(const bContext *C, Panel *pa)
00068 {
00069 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
00070         uiBlock *block;
00071         
00072         block= uiLayoutAbsoluteBlock(pa->layout);
00073         uiBlockSetHandleFunc(block, do_logic_panel_events, NULL);
00074 
00075 }       
00076 
00077 static void logic_panel_view_properties(const bContext *C, Panel *pa)
00078 {
00079         //      SpaceLogic *slogic= CTX_wm_space_logic(C);
00080         uiBlock *block;
00081         
00082         block= uiLayoutAbsoluteBlock(pa->layout);
00083         uiBlockSetHandleFunc(block, do_logic_panel_events, NULL);
00084         
00085 }       
00086 #endif
00087 
00088 void logic_buttons_register(ARegionType *UNUSED(art))
00089 {
00090 #if 0
00091         PanelType *pt;
00092 
00093         pt= MEM_callocN(sizeof(PanelType), "spacetype logic panel properties");
00094         strcpy(pt->idname, "LOGIC_PT_properties");
00095         strcpy(pt->label, "Logic Properties");
00096         pt->draw= logic_panel_properties;
00097         BLI_addtail(&art->paneltypes, pt);
00098 
00099         pt= MEM_callocN(sizeof(PanelType), "spacetype logic view properties");
00100         strcpy(pt->idname, "LOGIC_PT_view_properties");
00101         strcpy(pt->label, "View Properties");
00102         pt->draw= logic_panel_view_properties;
00103         BLI_addtail(&art->paneltypes, pt);
00104 #endif
00105 
00106 }
00107 
00108 static int logic_properties(bContext *C, wmOperator *UNUSED(op))
00109 {
00110         ScrArea *sa= CTX_wm_area(C);
00111         ARegion *ar= logic_has_buttons_region(sa);
00112         
00113         if(ar)
00114                 ED_region_toggle_hidden(C, ar);
00115 
00116         return OPERATOR_FINISHED;
00117 }
00118 
00119 void LOGIC_OT_properties(wmOperatorType *ot)
00120 {
00121         ot->name= "Properties";
00122         ot->description= "Toggle display properties panel";
00123         ot->idname= "LOGIC_OT_properties";
00124         
00125         ot->exec= logic_properties;
00126         ot->poll= ED_operator_logic_active;
00127         
00128         /* flags */
00129         ot->flag= 0;
00130 }
00131 
00132 /* Remove Logic Bricks Connections */
00133 /* ********************** Cut Link operator ***************** */
00134 
00135 #define LINK_RESOL 12
00136 static int cut_links_intersect(uiLinkLine *line, float mcoords[][2], int tot)
00137 {
00138         float coord_array[LINK_RESOL+1][2];
00139         int i, b;
00140         rcti rectlink;
00141 
00142         rectlink.xmin= (int) (line->from->x1 + line->from->x2) / 2;
00143         rectlink.ymin= (int) (line->from->y1 + line->from->y2) / 2;
00144         rectlink.xmax= (int) (line->to->x1 + line->to->x2) / 2;
00145         rectlink.ymax= (int) (line->to->y1 + line->to->y2) / 2;
00146 
00147         if(ui_link_bezier_points(&rectlink, coord_array, LINK_RESOL)){
00148                 for(i=0; i<tot-1; i++)
00149                         for(b=0; b<LINK_RESOL-1; b++)
00150                                 if(isect_line_line_v2(mcoords[i], mcoords[i+1], coord_array[b], coord_array[b+1]) > 0)
00151                                         return 1;
00152         }
00153         return 0;
00154 }
00155 
00156 static int cut_links_exec(bContext *C, wmOperator *op)
00157 {
00158         ARegion *ar= CTX_wm_region(C);
00159         float mcoords[256][2];
00160         int i= 0;
00161         
00162         RNA_BEGIN(op->ptr, itemptr, "path") {
00163                 float loc[2];
00164                 
00165                 RNA_float_get_array(&itemptr, "loc", loc);
00166                 UI_view2d_region_to_view(&ar->v2d, (short)loc[0], (short)loc[1], 
00167                                                                  &mcoords[i][0], &mcoords[i][1]);
00168                 i++;
00169                 if(i>= 256) break;
00170         }
00171         RNA_END;
00172 
00173         if (i>1) {
00174                 uiBlock *block;
00175                 uiLinkLine *line, *nline;
00176                 uiBut *but;
00177                 for(block= ar->uiblocks.first; block; block= block->next)
00178                 {
00179                         but= block->buttons.first;
00180                         while(but) {
00181                                 if(but->type==LINK && but->link) {
00182                                         for(line= but->link->lines.first; line; line= nline) {
00183                                                 nline= line->next;
00184 
00185                                                 if(cut_links_intersect(line, mcoords, i)) {
00186                                                         ui_delete_linkline(line, but);
00187                                                 }
00188                                         }
00189                                 }
00190                                 but= but->next;
00191                         }
00192                 }
00193                 return OPERATOR_FINISHED;
00194         }       
00195         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
00196 }
00197 
00198 void LOGIC_OT_links_cut(wmOperatorType *ot)
00199 {
00200         PropertyRNA *prop;
00201         
00202         ot->name= "Cut links";
00203         ot->idname= "LOGIC_OT_links_cut";
00204         ot->description= "Remove logic brick connections";
00205         
00206         ot->invoke= WM_gesture_lines_invoke;
00207         ot->modal= WM_gesture_lines_modal;
00208         ot->exec= cut_links_exec;
00209         ot->cancel= WM_gesture_lines_cancel;
00210         
00211         ot->poll= ED_operator_logic_active;
00212         
00213         /* flags */
00214         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00215         
00216         prop= RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE);
00217         RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath);
00218         /* internal */
00219         RNA_def_int(ot->srna, "cursor", BC_KNIFECURSOR, 0, INT_MAX, "Cursor", "", 0, INT_MAX);
00220 }
00221