Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[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         /* 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         ar->v2d.tot.xmin=  0.0f;
126         ar->v2d.tot.ymin=  0.0f;
127         ar->v2d.tot.xmax= 1280;
128         ar->v2d.tot.ymax= 240.0f;
129         
130         ar->v2d.cur.xmin=  0.0f;
131         ar->v2d.cur.ymin=  0.0f;
132         ar->v2d.cur.xmax= 1280.0f;
133         ar->v2d.cur.ymax= 240.0f;
134         
135         ar->v2d.min[0]= 1.0f;
136         ar->v2d.min[1]= 1.0f;
137         
138         ar->v2d.max[0]= 32000.0f;
139         ar->v2d.max[1]= 32000.0f;
140         
141         ar->v2d.minzoom= 0.5f;
142         ar->v2d.maxzoom= 1.21f;
143         
144         ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
145         ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
146         ar->v2d.keeptot= 0;
147         
148         
149         return (SpaceLink *)slogic;
150 }
151
152 /* not spacelink itself */
153 static void logic_free(SpaceLink *sl)
154 {       
155 //      Spacelogic *slogic= (SpaceLogic*) sl;
156         
157 //      if(slogic->gpd)
158 // XXX          free_gpencil_data(slogic->gpd);
159         
160 }
161
162
163 /* spacetype; init callback */
164 static void logic_init(struct wmWindowManager *wm, ScrArea *sa)
165 {
166
167 }
168
169 static SpaceLink *logic_duplicate(SpaceLink *sl)
170 {
171         SpaceLogic *slogicn= MEM_dupallocN(sl);
172                 
173         return (SpaceLink *)slogicn;
174 }
175
176 void logic_operatortypes(void)
177 {
178         WM_operatortype_append(LOGIC_OT_properties);
179
180 }
181
182 void logic_keymap(struct wmWindowManager *wm)
183 {
184         ListBase *keymap= WM_keymap_listbase(wm, "Logic Generic", SPACE_LOGIC, 0);
185         
186         WM_keymap_add_item(keymap, "LOGIC_OT_properties", NKEY, KM_PRESS, 0, 0);
187 }
188
189 static void logic_refresh(const bContext *C, ScrArea *sa)
190 {
191 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
192 //      Object *obedit= CTX_data_edit_object(C);
193
194 }
195
196 static void logic_listener(ARegion *ar, wmNotifier *wmn)
197 {
198         /* context changes */
199         switch(wmn->category) {
200                 case NC_SCENE:
201                         switch(wmn->data) {
202                                 case ND_FRAME:
203                                         ED_region_tag_redraw(ar);
204                                         break;
205                                         
206                                 case ND_OB_ACTIVE:
207                                         ED_region_tag_redraw(ar);
208                                         break;
209                                 }
210                                 break;
211                 case NC_OBJECT:
212                         break;
213         }
214 }
215
216 static int logic_context(const bContext *C, const char *member, bContextDataResult *result)
217 {
218 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
219
220
221         return 0;
222 }
223
224 /************************** main region ***************************/
225
226
227 /* add handlers, stuff you only do once or on area/region changes */
228 static void logic_main_area_init(wmWindowManager *wm, ARegion *ar)
229 {
230         ListBase *keymap;
231         
232         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
233
234         /* own keymaps */
235         keymap= WM_keymap_listbase(wm, "Logic Generic", SPACE_LOGIC, 0);
236         WM_event_add_keymap_handler(&ar->handlers, keymap);
237 }
238
239 static void logic_main_area_draw(const bContext *C, ARegion *ar)
240 {
241         /* draw entirely, view changes should be handled here */
242 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
243         View2D *v2d= &ar->v2d;
244         View2DScrollers *scrollers;
245         float col[3];
246         
247         /* clear and setup matrix */
248         UI_GetThemeColor3fv(TH_BACK, col);
249         glClearColor(col[0], col[1], col[2], 0.0);
250         glClear(GL_COLOR_BUFFER_BIT);
251         
252         UI_view2d_view_ortho(C, v2d);
253         
254         logic_buttons((bContext *)C, ar);
255         
256         /* reset view matrix */
257         UI_view2d_view_restore(C);
258         
259         /* scrollers */
260         scrollers= UI_view2d_scrollers_calc(C, v2d, 10, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
261         UI_view2d_scrollers_draw(C, v2d, scrollers);
262         UI_view2d_scrollers_free(scrollers);
263         
264 }
265
266
267 /* *********************** buttons region ************************ */
268
269 /* add handlers, stuff you only do once or on area/region changes */
270 static void logic_buttons_area_init(wmWindowManager *wm, ARegion *ar)
271 {
272         ListBase *keymap;
273
274         ED_region_panels_init(wm, ar);
275         
276         keymap= WM_keymap_listbase(wm, "Logic Generic", SPACE_LOGIC, 0);
277         WM_event_add_keymap_handler(&ar->handlers, keymap);
278 }
279
280 static void logic_buttons_area_draw(const bContext *C, ARegion *ar)
281 {
282         ED_region_panels(C, ar, 1, NULL, -1);
283 }
284
285 /************************* header region **************************/
286
287 /* add handlers, stuff you only do once or on area/region changes */
288 static void logic_header_area_init(wmWindowManager *wm, ARegion *ar)
289 {
290         ED_region_header_init(ar);
291 }
292
293 static void logic_header_area_draw(const bContext *C, ARegion *ar)
294 {
295         float col[3];
296         
297         /* clear */
298         if(ED_screen_area_active(C))
299                 UI_GetThemeColor3fv(TH_HEADER, col);
300         else
301                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
302         
303         glClearColor(col[0], col[1], col[2], 0.0);
304         glClear(GL_COLOR_BUFFER_BIT);
305         
306         /* set view2d view matrix for scrolling (without scrollers) */
307         UI_view2d_view_ortho(C, &ar->v2d);
308         
309         logic_header_buttons(C, ar);
310         
311         /* restore view matrix? */
312         UI_view2d_view_restore(C);
313 }
314
315 /**************************** spacetype *****************************/
316
317 /* only called once, from space/spacetypes.c */
318 void ED_spacetype_logic(void)
319 {
320         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype logic");
321         ARegionType *art;
322         
323         st->spaceid= SPACE_LOGIC;
324         
325         st->new= logic_new;
326         st->free= logic_free;
327         st->init= logic_init;
328         st->duplicate= logic_duplicate;
329         st->operatortypes= logic_operatortypes;
330         st->keymap= logic_keymap;
331         st->refresh= logic_refresh;
332         st->context= logic_context;
333         
334         /* regions: main window */
335         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
336         art->regionid = RGN_TYPE_WINDOW;
337         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES|ED_KEYMAP_VIEW2D;
338         art->init= logic_main_area_init;
339         art->draw= logic_main_area_draw;
340         art->listener= logic_listener;
341
342         BLI_addhead(&st->regiontypes, art);
343         
344         /* regions: listview/buttons */
345         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
346         art->regionid = RGN_TYPE_UI;
347         art->minsizex= 220; // XXX
348         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
349         art->listener= logic_listener;
350         art->init= logic_buttons_area_init;
351         art->draw= logic_buttons_area_draw;
352         BLI_addhead(&st->regiontypes, art);
353
354         logic_buttons_register(art);
355
356         /* regions: header */
357         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
358         art->regionid = RGN_TYPE_HEADER;
359         art->minsizey= HEADERY;
360         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
361         art->init= logic_header_area_init;
362         art->draw= logic_header_area_draw;
363         
364         BLI_addhead(&st->regiontypes, art);
365         
366         BKE_spacetype_register(st);
367 }
368
369