synched with trunk at revision 34793
[blender.git] / source / blender / editors / space_logic / space_logic.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 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_math.h"
37 #include "BLI_utildefines.h"
38
39 #include "BKE_context.h"
40 #include "BKE_screen.h"
41
42 #include "ED_screen.h"
43
44 #include "BIF_gl.h"
45
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "UI_resources.h"
51 #include "UI_view2d.h"
52
53 #include "logic_intern.h"
54
55 /* ******************** manage regions ********************* */
56
57 ARegion *logic_has_buttons_region(ScrArea *sa)
58 {
59         ARegion *ar, *arnew;
60         
61         for(ar= sa->regionbase.first; ar; ar= ar->next)
62                 if(ar->regiontype==RGN_TYPE_UI)
63                         return ar;
64         
65         /* add subdiv level; after header */
66         for(ar= sa->regionbase.first; ar; ar= ar->next)
67                 if(ar->regiontype==RGN_TYPE_HEADER)
68                         break;
69         
70         /* is error! */
71         if(ar==NULL) return NULL;
72         
73         arnew= MEM_callocN(sizeof(ARegion), "buttons for image");
74         
75         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
76         arnew->regiontype= RGN_TYPE_UI;
77         arnew->alignment= RGN_ALIGN_LEFT;
78         
79         arnew->flag = RGN_FLAG_HIDDEN;
80         
81         return arnew;
82 }
83
84 /* ******************** default callbacks for image space ***************** */
85
86 static SpaceLink *logic_new(const bContext *UNUSED(C))
87 {
88         ARegion *ar;
89         SpaceLogic *slogic;
90         
91         slogic= MEM_callocN(sizeof(SpaceLogic), "initlogic");
92         slogic->spacetype= SPACE_LOGIC;
93         
94         /* default options */
95         slogic->scaflag =       (BUTS_SENS_SEL|BUTS_SENS_ACT|BUTS_SENS_LINK)    |
96                                                 (BUTS_CONT_SEL|BUTS_CONT_ACT|BUTS_CONT_LINK)    |
97                                                 (BUTS_ACT_SEL|BUTS_ACT_ACT|BUTS_ACT_LINK)               |
98                                                 (BUTS_SENS_STATE|BUTS_ACT_STATE);
99         
100         
101         /* header */
102         ar= MEM_callocN(sizeof(ARegion), "header for logic");
103         
104         BLI_addtail(&slogic->regionbase, ar);
105         ar->regiontype= RGN_TYPE_HEADER;
106         ar->alignment= RGN_ALIGN_BOTTOM;
107         
108         /* buttons/list view */
109         ar= MEM_callocN(sizeof(ARegion), "buttons for logic");
110         
111         BLI_addtail(&slogic->regionbase, ar);
112         ar->regiontype= RGN_TYPE_UI;
113         ar->alignment= RGN_ALIGN_LEFT;
114         
115         /* main area */
116         ar= MEM_callocN(sizeof(ARegion), "main area for logic");
117         
118         BLI_addtail(&slogic->regionbase, ar);
119         ar->regiontype= RGN_TYPE_WINDOW;
120
121         ar->v2d.tot.xmin=  0.0f;
122         ar->v2d.tot.ymin=  0.0f;
123         ar->v2d.tot.xmax= 1280;
124         ar->v2d.tot.ymax= 240.0f;
125         
126         ar->v2d.cur.xmin=  0.0f;
127         ar->v2d.cur.ymin=  0.0f;
128         ar->v2d.cur.xmax= 1280.0f;
129         ar->v2d.cur.ymax= 240.0f;
130         
131         ar->v2d.min[0]= 1.0f;
132         ar->v2d.min[1]= 1.0f;
133         
134         ar->v2d.max[0]= 32000.0f;
135         ar->v2d.max[1]= 32000.0f;
136         
137         ar->v2d.minzoom= 0.5f;
138         ar->v2d.maxzoom= 1.21f;
139         
140         ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
141         ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
142         ar->v2d.keeptot= 0;
143         
144         
145         return (SpaceLink *)slogic;
146 }
147
148 /* not spacelink itself */
149 static void logic_free(SpaceLink *UNUSED(sl))
150 {       
151 //      Spacelogic *slogic= (SpaceLogic*) sl;
152         
153 //      if(slogic->gpd)
154 // XXX          free_gpencil_data(slogic->gpd);
155         
156 }
157
158
159 /* spacetype; init callback */
160 static void logic_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
161 {
162
163 }
164
165 static SpaceLink *logic_duplicate(SpaceLink *sl)
166 {
167         SpaceLogic *slogicn= MEM_dupallocN(sl);
168                 
169         return (SpaceLink *)slogicn;
170 }
171
172 void logic_operatortypes(void)
173 {
174         WM_operatortype_append(LOGIC_OT_properties);
175         WM_operatortype_append(LOGIC_OT_links_cut);
176 }
177
178 void logic_keymap(struct wmKeyConfig *keyconf)
179 {
180         wmKeyMap *keymap= WM_keymap_find(keyconf, "Logic Editor", SPACE_LOGIC, 0);
181         
182         WM_keymap_add_item(keymap, "LOGIC_OT_properties", NKEY, KM_PRESS, 0, 0);
183         WM_keymap_add_item(keymap, "LOGIC_OT_links_cut", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
184         WM_keymap_add_menu(keymap, "LOGIC_MT_logicbricks_add", AKEY, KM_PRESS, KM_SHIFT, 0);
185 }
186
187 static void logic_refresh(const bContext *UNUSED(C), ScrArea *UNUSED(sa))
188 {
189 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
190 //      Object *obedit= CTX_data_edit_object(C);
191
192 }
193
194 static void logic_listener(ARegion *ar, wmNotifier *wmn)
195 {
196         /* context changes */
197         switch(wmn->category) {
198                 case NC_LOGIC:
199                         ED_region_tag_redraw(ar);
200                         break;
201                 case NC_SCENE:
202                         switch(wmn->data) {
203                                 case ND_FRAME:
204                                         ED_region_tag_redraw(ar);
205                                         break;
206                                         
207                                 case ND_OB_ACTIVE:
208                                         ED_region_tag_redraw(ar);
209                                         break;
210                                 }
211                                 break;
212                 case NC_OBJECT:
213                         break;
214                 case NC_ID:
215                         if(wmn->action == NA_RENAME)
216                                 ED_region_tag_redraw(ar);
217                         break;
218         }
219 }
220
221 static int logic_context(const bContext *UNUSED(C), const char *UNUSED(member), bContextDataResult *UNUSED(result))
222 {
223 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
224         return 0;
225 }
226
227 /************************** main region ***************************/
228
229
230 /* add handlers, stuff you only do once or on area/region changes */
231 static void logic_main_area_init(wmWindowManager *wm, ARegion *ar)
232 {
233         wmKeyMap *keymap;
234         
235         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
236
237         /* own keymaps */
238         keymap= WM_keymap_find(wm->defaultconf, "Logic Editor", SPACE_LOGIC, 0);
239         WM_event_add_keymap_handler(&ar->handlers, keymap);
240 }
241
242 static void logic_main_area_draw(const bContext *C, ARegion *ar)
243 {
244         /* draw entirely, view changes should be handled here */
245 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
246         View2D *v2d= &ar->v2d;
247         View2DScrollers *scrollers;
248         
249         /* clear and setup matrix */
250         UI_ThemeClearColor(TH_BACK);
251         glClear(GL_COLOR_BUFFER_BIT);
252         
253         UI_view2d_view_ortho(v2d);
254         
255         logic_buttons((bContext *)C, ar);
256         
257         /* reset view matrix */
258         UI_view2d_view_restore(C);
259         
260         /* scrollers */
261         scrollers= UI_view2d_scrollers_calc(C, v2d, 10, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
262         UI_view2d_scrollers_draw(C, v2d, scrollers);
263         UI_view2d_scrollers_free(scrollers);
264         
265 }
266
267
268 /* *********************** buttons region ************************ */
269
270 /* add handlers, stuff you only do once or on area/region changes */
271 static void logic_buttons_area_init(wmWindowManager *wm, ARegion *ar)
272 {
273         wmKeyMap *keymap;
274
275         ED_region_panels_init(wm, ar);
276         
277         keymap= WM_keymap_find(wm->defaultconf, "Logic Editor", SPACE_LOGIC, 0);
278         WM_event_add_keymap_handler(&ar->handlers, keymap);
279 }
280
281 static void logic_buttons_area_draw(const bContext *C, ARegion *ar)
282 {
283         ED_region_panels(C, ar, 1, NULL, -1);
284 }
285
286 /************************* header region **************************/
287
288 /* add handlers, stuff you only do once or on area/region changes */
289 static void logic_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
290 {
291         ED_region_header_init(ar);
292 }
293
294 static void logic_header_area_draw(const bContext *C, ARegion *ar)
295 {
296         ED_region_header(C, ar);
297 }
298
299 /**************************** spacetype *****************************/
300
301 /* only called once, from space/spacetypes.c */
302 void ED_spacetype_logic(void)
303 {
304         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype logic");
305         ARegionType *art;
306         
307         st->spaceid= SPACE_LOGIC;
308         strncpy(st->name, "Logic", BKE_ST_MAXNAME);
309         
310         st->new= logic_new;
311         st->free= logic_free;
312         st->init= logic_init;
313         st->duplicate= logic_duplicate;
314         st->operatortypes= logic_operatortypes;
315         st->keymap= logic_keymap;
316         st->refresh= logic_refresh;
317         st->context= logic_context;
318         
319         /* regions: main window */
320         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
321         art->regionid = RGN_TYPE_WINDOW;
322         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES|ED_KEYMAP_VIEW2D;
323         art->init= logic_main_area_init;
324         art->draw= logic_main_area_draw;
325         art->listener= logic_listener;
326
327         BLI_addhead(&st->regiontypes, art);
328         
329         /* regions: listview/buttons */
330         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
331         art->regionid = RGN_TYPE_UI;
332         art->prefsizex= 220; // XXX
333         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
334         art->listener= logic_listener;
335         art->init= logic_buttons_area_init;
336         art->draw= logic_buttons_area_draw;
337         BLI_addhead(&st->regiontypes, art);
338
339         logic_buttons_register(art);
340
341         /* regions: header */
342         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
343         art->regionid = RGN_TYPE_HEADER;
344         art->prefsizey= HEADERY;
345         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
346         art->init= logic_header_area_init;
347         art->draw= logic_header_area_draw;
348         
349         BLI_addhead(&st->regiontypes, art);
350         
351         BKE_spacetype_register(st);
352 }
353
354