more lint includes
[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 #include "BKE_utildefines.h"
41
42 #include "ED_screen.h"
43
44 #include "BIF_gl.h"
45 #include "BIF_glutil.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 *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 *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 *wm, ScrArea *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 void logic_operatortypes(void)
174 {
175         WM_operatortype_append(LOGIC_OT_properties);
176         WM_operatortype_append(LOGIC_OT_links_cut);
177 }
178
179 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 }
186
187 static void logic_refresh(const bContext *C, ScrArea *sa)
188 {
189 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
190 //      Object *obedit= CTX_data_edit_object(C);
191
192 }
193
194 static void logic_listener(ARegion *ar, wmNotifier *wmn)
195 {
196         /* context changes */
197         switch(wmn->category) {
198                 case NC_SCENE:
199                         switch(wmn->data) {
200                                 case ND_FRAME:
201                                         ED_region_tag_redraw(ar);
202                                         break;
203                                         
204                                 case ND_OB_ACTIVE:
205                                         ED_region_tag_redraw(ar);
206                                         break;
207                                 }
208                                 break;
209                 case NC_OBJECT:
210                         break;
211                 case NC_ID:
212                         if(wmn->action == NA_RENAME)
213                                 ED_region_tag_redraw(ar);
214                         break;
215         }
216 }
217
218 static int logic_context(const bContext *C, const char *member, bContextDataResult *result)
219 {
220 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
221
222
223         return 0;
224 }
225
226 /************************** main region ***************************/
227
228
229 /* add handlers, stuff you only do once or on area/region changes */
230 static void logic_main_area_init(wmWindowManager *wm, ARegion *ar)
231 {
232         wmKeyMap *keymap;
233         
234         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
235
236         /* own keymaps */
237         keymap= WM_keymap_find(wm->defaultconf, "Logic Editor", SPACE_LOGIC, 0);
238         WM_event_add_keymap_handler(&ar->handlers, keymap);
239 }
240
241 static void logic_main_area_draw(const bContext *C, ARegion *ar)
242 {
243         /* draw entirely, view changes should be handled here */
244 //      SpaceLogic *slogic= CTX_wm_space_logic(C);
245         View2D *v2d= &ar->v2d;
246         View2DScrollers *scrollers;
247         float col[3];
248         
249         /* clear and setup matrix */
250         UI_GetThemeColor3fv(TH_BACK, col);
251         glClearColor(col[0], col[1], col[2], 0.0);
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         float col[3];
298         
299         /* clear */
300         if(ED_screen_area_active(C))
301                 UI_GetThemeColor3fv(TH_HEADER, col);
302         else
303                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
304         
305         glClearColor(col[0], col[1], col[2], 0.0);
306         glClear(GL_COLOR_BUFFER_BIT);
307         
308         /* set view2d view matrix for scrolling (without scrollers) */
309         UI_view2d_view_ortho(C, &ar->v2d);
310         
311         logic_header_buttons(C, ar);
312         
313         /* restore view matrix? */
314         UI_view2d_view_restore(C);
315 }
316
317 /**************************** spacetype *****************************/
318
319 /* only called once, from space/spacetypes.c */
320 void ED_spacetype_logic(void)
321 {
322         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype logic");
323         ARegionType *art;
324         
325         st->spaceid= SPACE_LOGIC;
326         strncpy(st->name, "Logic", BKE_ST_MAXNAME);
327         
328         st->new= logic_new;
329         st->free= logic_free;
330         st->init= logic_init;
331         st->duplicate= logic_duplicate;
332         st->operatortypes= logic_operatortypes;
333         st->keymap= logic_keymap;
334         st->refresh= logic_refresh;
335         st->context= logic_context;
336         
337         /* regions: main window */
338         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
339         art->regionid = RGN_TYPE_WINDOW;
340         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES|ED_KEYMAP_VIEW2D;
341         art->init= logic_main_area_init;
342         art->draw= logic_main_area_draw;
343         art->listener= logic_listener;
344
345         BLI_addhead(&st->regiontypes, art);
346         
347         /* regions: listview/buttons */
348         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
349         art->regionid = RGN_TYPE_UI;
350         art->prefsizex= 220; // XXX
351         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
352         art->listener= logic_listener;
353         art->init= logic_buttons_area_init;
354         art->draw= logic_buttons_area_draw;
355         BLI_addhead(&st->regiontypes, art);
356
357         logic_buttons_register(art);
358
359         /* regions: header */
360         art= MEM_callocN(sizeof(ARegionType), "spacetype logic region");
361         art->regionid = RGN_TYPE_HEADER;
362         art->prefsizey= HEADERY;
363         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
364         art->init= logic_header_area_init;
365         art->draw= logic_header_area_draw;
366         
367         BLI_addhead(&st->regiontypes, art);
368         
369         BKE_spacetype_register(st);
370 }
371
372