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