69ac68f6f8237cf57eec7ed34da3eb30c3cb7b09
[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 #include "BLI_utildefines.h"
38
39 #include "BKE_context.h"
40 #include "BKE_screen.h"
41
42 #include "ED_space_api.h"
43 #include "ED_screen.h"
44
45 #include "BIF_gl.h"
46
47
48 #include "WM_api.h"
49 #include "WM_types.h"
50
51 #include "UI_resources.h"
52 #include "UI_view2d.h"
53
54 #include "logic_intern.h"
55
56 /* ******************** manage regions ********************* */
57
58 ARegion *logic_has_buttons_region(ScrArea *sa)
59 {
60         ARegion *ar, *arnew;
61         
62         for(ar= sa->regionbase.first; ar; ar= ar->next)
63                 if(ar->regiontype==RGN_TYPE_UI)
64                         return ar;
65         
66         /* add subdiv level; after header */
67         for(ar= sa->regionbase.first; ar; ar= ar->next)
68                 if(ar->regiontype==RGN_TYPE_HEADER)
69                         break;
70         
71         /* is error! */
72         if(ar==NULL) return NULL;
73         
74         arnew= MEM_callocN(sizeof(ARegion), "buttons for image");
75         
76         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
77         arnew->regiontype= RGN_TYPE_UI;
78         arnew->alignment= RGN_ALIGN_LEFT;
79         
80         arnew->flag = RGN_FLAG_HIDDEN;
81         
82         return arnew;
83 }
84
85 /* ******************** default callbacks for image space ***************** */
86
87 static SpaceLink *logic_new(const bContext *UNUSED(C))
88 {
89         ARegion *ar;
90         SpaceLogic *slogic;
91         
92         slogic= MEM_callocN(sizeof(SpaceLogic), "initlogic");
93         slogic->spacetype= SPACE_LOGIC;
94         
95         /* default options */
96         slogic->scaflag =       (BUTS_SENS_SEL|BUTS_SENS_ACT|BUTS_SENS_LINK)    |
97                                                 (BUTS_CONT_SEL|BUTS_CONT_ACT|BUTS_CONT_LINK)    |
98                                                 (BUTS_ACT_SEL|BUTS_ACT_ACT|BUTS_ACT_LINK)               |
99                                                 (BUTS_SENS_STATE|BUTS_ACT_STATE);
100         
101         
102         /* header */
103         ar= MEM_callocN(sizeof(ARegion), "header for logic");
104         
105         BLI_addtail(&slogic->regionbase, ar);
106         ar->regiontype= RGN_TYPE_HEADER;
107         ar->alignment= RGN_ALIGN_BOTTOM;
108         
109         /* buttons/list view */
110         ar= MEM_callocN(sizeof(ARegion), "buttons for logic");
111         
112         BLI_addtail(&slogic->regionbase, ar);
113         ar->regiontype= RGN_TYPE_UI;
114         ar->alignment= RGN_ALIGN_LEFT;
115         
116         /* main area */
117         ar= MEM_callocN(sizeof(ARegion), "main area for logic");
118         
119         BLI_addtail(&slogic->regionbase, ar);
120         ar->regiontype= RGN_TYPE_WINDOW;
121
122         ar->v2d.tot.xmin=  0.0f;
123         ar->v2d.tot.ymin=  0.0f;
124         ar->v2d.tot.xmax= 1280;
125         ar->v2d.tot.ymax= 240.0f;
126         
127         ar->v2d.cur.xmin=  0.0f;
128         ar->v2d.cur.ymin=  0.0f;
129         ar->v2d.cur.xmax= 1280.0f;
130         ar->v2d.cur.ymax= 240.0f;
131         
132         ar->v2d.min[0]= 1.0f;
133         ar->v2d.min[1]= 1.0f;
134         
135         ar->v2d.max[0]= 32000.0f;
136         ar->v2d.max[1]= 32000.0f;
137         
138         ar->v2d.minzoom= 0.5f;
139         ar->v2d.maxzoom= 1.21f;
140         
141         ar->v2d.scroll= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
142         ar->v2d.keepzoom= V2D_LIMITZOOM|V2D_KEEPASPECT;
143         ar->v2d.keeptot= 0;
144         
145         
146         return (SpaceLink *)slogic;
147 }
148
149 /* not spacelink itself */
150 static void logic_free(SpaceLink *UNUSED(sl))
151 {       
152 //      Spacelogic *slogic= (SpaceLogic*) sl;
153         
154 //      if(slogic->gpd)
155 // XXX          free_gpencil_data(slogic->gpd);
156         
157 }
158
159
160 /* spacetype; init callback */
161 static void logic_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
162 {
163
164 }
165
166 static SpaceLink *logic_duplicate(SpaceLink *sl)
167 {
168         SpaceLogic *slogicn= MEM_dupallocN(sl);
169                 
170         return (SpaceLink *)slogicn;
171 }
172
173 static void logic_operatortypes(void)
174 {
175         WM_operatortype_append(LOGIC_OT_properties);
176         WM_operatortype_append(LOGIC_OT_links_cut);
177 }
178
179 static void logic_keymap(struct wmKeyConfig *keyconf)
180 {
181         wmKeyMap *keymap= WM_keymap_find(keyconf, "Logic Editor", SPACE_LOGIC, 0);
182         
183         WM_keymap_add_item(keymap, "LOGIC_OT_properties", NKEY, KM_PRESS, 0, 0);
184         WM_keymap_add_item(keymap, "LOGIC_OT_links_cut", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
185         WM_keymap_add_menu(keymap, "LOGIC_MT_logicbricks_add", AKEY, KM_PRESS, KM_SHIFT, 0);
186 }
187
188 static void logic_refresh(const bContext *UNUSED(C), ScrArea *UNUSED(sa))
189 {
190 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
191 //      Object *obedit= CTX_data_edit_object(C);
192
193 }
194
195 static void logic_listener(ARegion *ar, wmNotifier *wmn)
196 {
197         /* context changes */
198         switch(wmn->category) {
199                 case NC_LOGIC:
200                         ED_region_tag_redraw(ar);
201                         break;
202                 case NC_SCENE:
203                         switch(wmn->data) {
204                                 case ND_FRAME:
205                                         ED_region_tag_redraw(ar);
206                                         break;
207                                         
208                                 case ND_OB_ACTIVE:
209                                         ED_region_tag_redraw(ar);
210                                         break;
211                                 }
212                                 break;
213                 case NC_OBJECT:
214                         break;
215                 case NC_ID:
216                         if(wmn->action == NA_RENAME)
217                                 ED_region_tag_redraw(ar);
218                         break;
219         }
220 }
221
222 static int logic_context(const bContext *UNUSED(C), const char *UNUSED(member), bContextDataResult *UNUSED(result))
223 {
224 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
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(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 *UNUSED(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