ed8c7f2e4e764ba206638300097071c5845221e2
[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_math.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                 case NC_ID:
221                         switch(wmn->data) {
222                                 case ND_ID_RENAME:
223                                         ED_region_tag_redraw(ar);
224                                         break;
225                         }
226                         break;
227         }
228 }
229
230 static int logic_context(const bContext *C, const char *member, bContextDataResult *result)
231 {
232 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
233
234
235         return 0;
236 }
237
238 /************************** main region ***************************/
239
240
241 /* add handlers, stuff you only do once or on area/region changes */
242 static void logic_main_area_init(wmWindowManager *wm, ARegion *ar)
243 {
244         wmKeyMap *keymap;
245         
246         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
247
248         /* own keymaps */
249         keymap= WM_keymap_find(wm->defaultconf, "Logic Generic", SPACE_LOGIC, 0);
250         WM_event_add_keymap_handler(&ar->handlers, keymap);
251 }
252
253 static void logic_main_area_draw(const bContext *C, ARegion *ar)
254 {
255         /* draw entirely, view changes should be handled here */
256 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
257         View2D *v2d= &ar->v2d;
258         View2DScrollers *scrollers;
259         float col[3];
260         
261         /* clear and setup matrix */
262         UI_GetThemeColor3fv(TH_BACK, col);
263         glClearColor(col[0], col[1], col[2], 0.0);
264         glClear(GL_COLOR_BUFFER_BIT);
265         
266         UI_view2d_view_ortho(C, v2d);
267         
268         logic_buttons((bContext *)C, ar);
269         
270         /* reset view matrix */
271         UI_view2d_view_restore(C);
272         
273         /* scrollers */
274         scrollers= UI_view2d_scrollers_calc(C, v2d, 10, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
275         UI_view2d_scrollers_draw(C, v2d, scrollers);
276         UI_view2d_scrollers_free(scrollers);
277         
278 }
279
280
281 /* *********************** buttons region ************************ */
282
283 /* add handlers, stuff you only do once or on area/region changes */
284 static void logic_buttons_area_init(wmWindowManager *wm, ARegion *ar)
285 {
286         wmKeyMap *keymap;
287
288         ED_region_panels_init(wm, ar);
289         
290         keymap= WM_keymap_find(wm->defaultconf, "Logic Generic", SPACE_LOGIC, 0);
291         WM_event_add_keymap_handler(&ar->handlers, keymap);
292 }
293
294 static void logic_buttons_area_draw(const bContext *C, ARegion *ar)
295 {
296         ED_region_panels(C, ar, 1, NULL, -1);
297 }
298
299 /************************* header region **************************/
300
301 /* add handlers, stuff you only do once or on area/region changes */
302 static void logic_header_area_init(wmWindowManager *wm, ARegion *ar)
303 {
304         ED_region_header_init(ar);
305 }
306
307 static void logic_header_area_draw(const bContext *C, ARegion *ar)
308 {
309         float col[3];
310         
311         /* clear */
312         if(ED_screen_area_active(C))
313                 UI_GetThemeColor3fv(TH_HEADER, col);
314         else
315                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
316         
317         glClearColor(col[0], col[1], col[2], 0.0);
318         glClear(GL_COLOR_BUFFER_BIT);
319         
320         /* set view2d view matrix for scrolling (without scrollers) */
321         UI_view2d_view_ortho(C, &ar->v2d);
322         
323         logic_header_buttons(C, ar);
324         
325         /* restore view matrix? */
326         UI_view2d_view_restore(C);
327 }
328
329 /**************************** spacetype *****************************/
330
331 /* only called once, from space/spacetypes.c */
332 void ED_spacetype_logic(void)
333 {
334         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype logic");
335         ARegionType *art;
336         
337         st->spaceid= SPACE_LOGIC;
338         
339         st->new= logic_new;
340         st->free= logic_free;
341         st->init= logic_init;
342         st->duplicate= logic_duplicate;
343         st->operatortypes= logic_operatortypes;
344         st->keymap= logic_keymap;
345         st->refresh= logic_refresh;
346         st->context= logic_context;
347         
348         /* regions: main window */
349         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
350         art->regionid = RGN_TYPE_WINDOW;
351         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES|ED_KEYMAP_VIEW2D;
352         art->init= logic_main_area_init;
353         art->draw= logic_main_area_draw;
354         art->listener= logic_listener;
355
356         BLI_addhead(&st->regiontypes, art);
357         
358         /* regions: listview/buttons */
359         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
360         art->regionid = RGN_TYPE_UI;
361         art->minsizex= 220; // XXX
362         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
363         art->listener= logic_listener;
364         art->init= logic_buttons_area_init;
365         art->draw= logic_buttons_area_draw;
366         BLI_addhead(&st->regiontypes, art);
367
368         logic_buttons_register(art);
369
370         /* regions: header */
371         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
372         art->regionid = RGN_TYPE_HEADER;
373         art->minsizey= HEADERY;
374         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
375         art->init= logic_header_area_init;
376         art->draw= logic_header_area_draw;
377         
378         BLI_addhead(&st->regiontypes, art);
379         
380         BKE_spacetype_register(st);
381 }
382
383