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