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