small merge needed for testing new animsys in 2.5 BGE, mixing own changes with merge...
[blender-staging.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         /* header */
106         ar= MEM_callocN(sizeof(ARegion), "header for logic");
107         
108         BLI_addtail(&slogic->regionbase, ar);
109         ar->regiontype= RGN_TYPE_HEADER;
110         ar->alignment= RGN_ALIGN_BOTTOM;
111         
112         /* buttons/list view */
113         ar= MEM_callocN(sizeof(ARegion), "buttons for logic");
114         
115         BLI_addtail(&slogic->regionbase, ar);
116         ar->regiontype= RGN_TYPE_UI;
117         ar->alignment= RGN_ALIGN_LEFT;
118         
119         /* main area */
120         ar= MEM_callocN(sizeof(ARegion), "main area for logic");
121         
122         BLI_addtail(&slogic->regionbase, ar);
123         ar->regiontype= RGN_TYPE_WINDOW;
124         
125         return (SpaceLink *)slogic;
126 }
127
128 /* not spacelink itself */
129 static void logic_free(SpaceLink *sl)
130 {       
131 //      Spacelogic *slogic= (SpaceLogic*) sl;
132         
133 //      if(slogic->gpd)
134 // XXX          free_gpencil_data(slogic->gpd);
135         
136 }
137
138
139 /* spacetype; init callback */
140 static void logic_init(struct wmWindowManager *wm, ScrArea *sa)
141 {
142
143 }
144
145 static SpaceLink *logic_duplicate(SpaceLink *sl)
146 {
147         SpaceLogic *slogicn= MEM_dupallocN(sl);
148                 
149         return (SpaceLink *)slogicn;
150 }
151
152 void logic_operatortypes(void)
153 {
154         WM_operatortype_append(LOGIC_OT_properties);
155
156 }
157
158 void logic_keymap(struct wmWindowManager *wm)
159 {
160         ListBase *keymap= WM_keymap_listbase(wm, "Logic Generic", SPACE_LOGIC, 0);
161         
162         WM_keymap_add_item(keymap, "LOGIC_OT_properties", NKEY, KM_PRESS, 0, 0);
163 }
164
165 static void logic_refresh(const bContext *C, ScrArea *sa)
166 {
167 //      SpaceLogic *slogic= (SpaceImage*)CTX_wm_space_data(C);
168 //      Object *obedit= CTX_data_edit_object(C);
169
170 }
171
172 static void logic_listener(ARegion *ar, wmNotifier *wmn)
173 {
174         /* context changes */
175         switch(wmn->category) {
176                 case NC_SCENE:
177                         switch(wmn->data) {
178                                 case ND_FRAME:
179                                         ED_region_tag_redraw(ar);
180                                         break;
181                                         
182                                 case ND_OB_ACTIVE:
183                                         ED_region_tag_redraw(ar);
184                                         break;
185                                 }
186                                 break;
187                 case NC_OBJECT:
188                         break;
189         }
190 }
191
192 static int logic_context(const bContext *C, const char *member, bContextDataResult *result)
193 {
194 //      SpaceLogic *slogic= (SpaceLogic*)CTX_wm_space_data(C);
195
196
197         return 0;
198 }
199
200 /************************** main region ***************************/
201
202
203 /* add handlers, stuff you only do once or on area/region changes */
204 static void logic_main_area_init(wmWindowManager *wm, ARegion *ar)
205 {
206         ListBase *keymap;
207         
208         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
209
210         /* own keymaps */
211         keymap= WM_keymap_listbase(wm, "Logic Generic", SPACE_LOGIC, 0);
212         WM_event_add_keymap_handler(&ar->handlers, keymap);
213 }
214
215 static void logic_main_area_draw(const bContext *C, ARegion *ar)
216 {
217         /* draw entirely, view changes should be handled here */
218 //      SpaceLogic *slogic= (SpaceLogic*)CTX_wm_space_data(C);
219         View2D *v2d= &ar->v2d;
220         float col[3];
221         
222         /* clear and setup matrix */
223         UI_GetThemeColor3fv(TH_BACK, col);
224         glClearColor(col[0], col[1], col[2], 0.0);
225         glClear(GL_COLOR_BUFFER_BIT);
226         
227         /* we set view2d from own zoom and offset each time */
228 //      image_main_area_set_view2d(sima, ar, scene);
229                 
230         UI_view2d_view_ortho(C, v2d);
231         
232         /* scrollers? */
233         /*scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_UNIT_VALUES, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
234         UI_view2d_scrollers_draw(C, v2d, scrollers);
235         UI_view2d_scrollers_free(scrollers);*/
236 }
237
238
239 /* *********************** buttons region ************************ */
240
241 /* add handlers, stuff you only do once or on area/region changes */
242 static void logic_buttons_area_init(wmWindowManager *wm, ARegion *ar)
243 {
244         ListBase *keymap;
245
246         ED_region_panels_init(wm, ar);
247         
248         keymap= WM_keymap_listbase(wm, "Logic Generic", SPACE_LOGIC, 0);
249         WM_event_add_keymap_handler(&ar->handlers, keymap);
250 }
251
252 static void logic_buttons_area_draw(const bContext *C, ARegion *ar)
253 {
254         ED_region_panels(C, ar, 1, NULL);
255 }
256
257 /************************* header region **************************/
258
259 /* add handlers, stuff you only do once or on area/region changes */
260 static void logic_header_area_init(wmWindowManager *wm, ARegion *ar)
261 {
262         ED_region_header_init(ar);
263 }
264
265 static void logic_header_area_draw(const bContext *C, ARegion *ar)
266 {
267         float col[3];
268         
269         /* clear */
270         if(ED_screen_area_active(C))
271                 UI_GetThemeColor3fv(TH_HEADER, col);
272         else
273                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
274         
275         glClearColor(col[0], col[1], col[2], 0.0);
276         glClear(GL_COLOR_BUFFER_BIT);
277         
278         /* set view2d view matrix for scrolling (without scrollers) */
279         UI_view2d_view_ortho(C, &ar->v2d);
280         
281         logic_header_buttons(C, ar);
282         
283         /* restore view matrix? */
284         UI_view2d_view_restore(C);
285 }
286
287 /**************************** spacetype *****************************/
288
289 /* only called once, from space/spacetypes.c */
290 void ED_spacetype_logic(void)
291 {
292         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype logic");
293         ARegionType *art;
294         
295         st->spaceid= SPACE_LOGIC;
296         
297         st->new= logic_new;
298         st->free= logic_free;
299         st->init= logic_init;
300         st->duplicate= logic_duplicate;
301         st->operatortypes= logic_operatortypes;
302         st->keymap= logic_keymap;
303         st->refresh= logic_refresh;
304         st->context= logic_context;
305         
306         /* regions: main window */
307         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
308         art->regionid = RGN_TYPE_WINDOW;
309         art->keymapflag= ED_KEYMAP_FRAMES;
310         art->init= logic_main_area_init;
311         art->draw= logic_main_area_draw;
312         art->listener= logic_listener;
313         art->keymapflag= 0;
314
315         BLI_addhead(&st->regiontypes, art);
316         
317         /* regions: listview/buttons */
318         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
319         art->regionid = RGN_TYPE_UI;
320         art->minsizex= 220; // XXX
321         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
322         art->listener= logic_listener;
323         art->init= logic_buttons_area_init;
324         art->draw= logic_buttons_area_draw;
325         BLI_addhead(&st->regiontypes, art);
326
327         logic_buttons_register(art);
328
329         /* regions: header */
330         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
331         art->regionid = RGN_TYPE_HEADER;
332         art->minsizey= HEADERY;
333         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
334         art->init= logic_header_area_init;
335         art->draw= logic_header_area_draw;
336         
337         BLI_addhead(&st->regiontypes, art);
338         
339         BKE_spacetype_register(st);
340 }
341
342