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