Keyed physics refresh:
[blender-staging.git] / source / blender / editors / space_buttons / space_buttons.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) 2008 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_object_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_userdef_types.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_arithb.h"
42 #include "BLI_rand.h"
43
44 #include "BKE_colortools.h"
45 #include "BKE_context.h"
46 #include "BKE_screen.h"
47
48 #include "ED_space_api.h"
49 #include "ED_screen.h"
50
51 #include "BIF_gl.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58 #include "UI_view2d.h"
59
60 #include "ED_previewrender.h"
61
62 #include "buttons_intern.h"     // own include
63
64 /* ******************** default callbacks for buttons space ***************** */
65
66 static SpaceLink *buttons_new(const bContext *C)
67 {
68         ARegion *ar;
69         SpaceButs *sbuts;
70         
71         sbuts= MEM_callocN(sizeof(SpaceButs), "initbuts");
72         sbuts->spacetype= SPACE_BUTS;
73         sbuts->scaflag= BUTS_SENS_LINK|BUTS_SENS_ACT|BUTS_CONT_ACT|BUTS_ACT_ACT|BUTS_ACT_LINK;
74         sbuts->align= BUT_AUTO;
75
76         /* header */
77         ar= MEM_callocN(sizeof(ARegion), "header for buts");
78         
79         BLI_addtail(&sbuts->regionbase, ar);
80         ar->regiontype= RGN_TYPE_HEADER;
81         ar->alignment= RGN_ALIGN_BOTTOM;
82         
83 #if 0
84         /* context area */
85         ar= MEM_callocN(sizeof(ARegion), "context area for buts");
86         BLI_addtail(&sbuts->regionbase, ar);
87         ar->regiontype= RGN_TYPE_CHANNELS;
88         ar->alignment= RGN_ALIGN_TOP;
89 #endif
90
91         /* main area */
92         ar= MEM_callocN(sizeof(ARegion), "main area for buts");
93         
94         BLI_addtail(&sbuts->regionbase, ar);
95         ar->regiontype= RGN_TYPE_WINDOW;
96         
97 #if 0 // disabled, as this currently draws badly in new system
98         /* buts space goes from (0,0) to (1280, 228) */
99         ar->v2d.tot.xmin= 0.0f;
100         ar->v2d.tot.ymin= 0.0f;
101         ar->v2d.tot.xmax= 1279.0f;
102         ar->v2d.tot.ymax= 228.0f;
103         
104         ar->v2d.cur= sbuts->v2d.tot;
105         
106         ar->v2d.min[0]= 256.0f;
107         ar->v2d.min[1]= 42.0f;
108         
109         ar->v2d.max[0]= 2048.0f;
110         ar->v2d.max[1]= 450.0f;
111         
112         ar->v2d.minzoom= 0.5f;
113         ar->v2d.maxzoom= 1.21f;
114         
115         ar->v2d.scroll= 0;  // TODO: will we need scrollbars?
116         ar->v2d.align= V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y;
117         ar->v2d.keepzoom= V2D_KEEPZOOM|V2D_KEEPASPECT;
118         ar->v2d.keeptot= V2D_KEEPTOT_BOUNDS;
119 #endif  
120         
121         
122         return (SpaceLink *)sbuts;
123 }
124
125 /* not spacelink itself */
126 static void buttons_free(SpaceLink *sl)
127 {       
128         SpaceButs *sbuts= (SpaceButs*) sl;
129         
130         if(sbuts->ri) { 
131                 if (sbuts->ri->rect) MEM_freeN(sbuts->ri->rect);
132                 MEM_freeN(sbuts->ri);
133         }
134
135         if(sbuts->path)
136                 MEM_freeN(sbuts->path);
137 }
138
139 /* spacetype; init callback */
140 static void buttons_init(struct wmWindowManager *wm, ScrArea *sa)
141 {
142         SpaceButs *sbuts= sa->spacedata.first;
143
144         /* auto-align based on size */
145         if(sbuts->align == BUT_AUTO || !sbuts->align) {
146                 if(sa->winx > sa->winy)
147                         sbuts->align= BUT_HORIZONTAL;
148                 else
149                         sbuts->align= BUT_VERTICAL;
150         }
151 }
152
153 static SpaceLink *buttons_duplicate(SpaceLink *sl)
154 {
155         SpaceButs *sbutsn= MEM_dupallocN(sl);
156         
157         /* clear or remove stuff from old */
158         sbutsn->ri= NULL;
159         sbutsn->path= NULL;
160         
161         return (SpaceLink *)sbutsn;
162 }
163
164 /* add handlers, stuff you only do once or on area/region changes */
165 static void buttons_main_area_init(wmWindowManager *wm, ARegion *ar)
166 {
167         ListBase *keymap;
168
169         ED_region_panels_init(wm, ar);
170         
171         /* own keymap */
172         keymap= WM_keymap_listbase(wm, "Buttons", SPACE_BUTS, 0);       /* XXX weak? */
173         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
174 }
175
176 static void buttons_main_area_draw(const bContext *C, ARegion *ar)
177 {
178         /* draw entirely, view changes should be handled here */
179         SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
180         int vertical= (sbuts->align == BUT_VERTICAL);
181
182         buttons_context_compute(C, sbuts);
183
184         if(sbuts->mainb == BCONTEXT_SCENE)
185                 ED_region_panels(C, ar, vertical, "scene");
186         else if(sbuts->mainb == BCONTEXT_WORLD)
187                 ED_region_panels(C, ar, vertical, "world");
188         else if(sbuts->mainb == BCONTEXT_OBJECT)
189                 ED_region_panels(C, ar, vertical, "object");
190         else if(sbuts->mainb == BCONTEXT_DATA)
191                 ED_region_panels(C, ar, vertical, "data");
192         else if(sbuts->mainb == BCONTEXT_MATERIAL)
193                 ED_region_panels(C, ar, vertical, "material");
194         else if(sbuts->mainb == BCONTEXT_TEXTURE)
195                 ED_region_panels(C, ar, vertical, "texture");
196         else if(sbuts->mainb == BCONTEXT_PARTICLE)
197                 ED_region_panels(C, ar, vertical, "particle");
198         else if(sbuts->mainb == BCONTEXT_PHYSICS)
199                 ED_region_panels(C, ar, vertical, "physics");
200         else if(sbuts->mainb == BCONTEXT_BONE)
201                 ED_region_panels(C, ar, vertical, "bone");
202         else if(sbuts->mainb == BCONTEXT_MODIFIER)
203                 ED_region_panels(C, ar, vertical, "modifier");
204         else if (sbuts->mainb == BCONTEXT_CONSTRAINT)
205                 ED_region_panels(C, ar, vertical, "constraint");
206
207     sbuts->re_align= 0;
208         sbuts->mainbo= sbuts->mainb;
209 }
210
211 void buttons_operatortypes(void)
212 {
213         WM_operatortype_append(OBJECT_OT_material_slot_add);
214         WM_operatortype_append(OBJECT_OT_material_slot_remove);
215         WM_operatortype_append(OBJECT_OT_material_slot_assign);
216         WM_operatortype_append(OBJECT_OT_material_slot_select);
217         WM_operatortype_append(OBJECT_OT_material_slot_deselect);
218
219         WM_operatortype_append(MATERIAL_OT_new);
220         WM_operatortype_append(TEXTURE_OT_new);
221         WM_operatortype_append(WORLD_OT_new);
222
223         WM_operatortype_append(OBJECT_OT_particle_system_add);
224         WM_operatortype_append(OBJECT_OT_particle_system_remove);
225
226         WM_operatortype_append(PARTICLE_OT_new);
227         WM_operatortype_append(PARTICLE_OT_new_keyed_target);
228         WM_operatortype_append(PARTICLE_OT_remove_keyed_target);
229         WM_operatortype_append(PARTICLE_OT_keyed_target_move_up);
230         WM_operatortype_append(PARTICLE_OT_keyed_target_move_down);
231 }
232
233 void buttons_keymap(struct wmWindowManager *wm)
234 {
235         
236 }
237
238 //#define PY_HEADER
239 /* add handlers, stuff you only do once or on area/region changes */
240 static void buttons_header_area_init(wmWindowManager *wm, ARegion *ar)
241 {
242 #ifdef PY_HEADER
243         ED_region_header_init(ar);
244 #else
245         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
246 #endif
247 }
248
249 static void buttons_header_area_draw(const bContext *C, ARegion *ar)
250 {
251 #ifdef PY_HEADER
252         ED_region_header(C, ar);
253 #else
254
255         float col[3];
256         
257         /* clear */
258         if(ED_screen_area_active(C))
259                 UI_GetThemeColor3fv(TH_HEADER, col);
260         else
261                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
262         
263         glClearColor(col[0], col[1], col[2], 0.0);
264         glClear(GL_COLOR_BUFFER_BIT);
265         
266         /* set view2d view matrix for scrolling (without scrollers) */
267         UI_view2d_view_ortho(C, &ar->v2d);
268         
269         buttons_header_buttons(C, ar);
270 #endif  
271
272         /* restore view matrix? */
273         UI_view2d_view_restore(C);
274 }
275
276 #if 0
277 /* add handlers, stuff you only do once or on area/region changes */
278 static void buttons_context_area_init(wmWindowManager *wm, ARegion *ar)
279 {
280         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
281 }
282
283 #define CONTEXTY        30
284
285 static void buttons_context_area_draw(const bContext *C, ARegion *ar)
286 {
287         SpaceButs *sbuts= (SpaceButs*)CTX_wm_space_data(C);
288         uiStyle *style= U.uistyles.first;
289         uiBlock *block;
290         uiLayout *layout;
291         View2D *v2d= &ar->v2d;
292         float col[3];
293         int x, y, w, h;
294
295         buttons_context_compute(C, sbuts);
296
297         w= v2d->cur.xmax - v2d->cur.xmin;
298         h= v2d->cur.ymax - v2d->cur.ymin;
299         UI_view2d_view_ortho(C, v2d);
300
301         /* create UI */
302         block= uiBeginBlock(C, ar, "buttons_context", UI_EMBOSS);
303         layout= uiBlockLayout(block, UI_LAYOUT_HORIZONTAL, UI_LAYOUT_PANEL,
304                 style->panelspace, h - (h-UI_UNIT_Y)/2, w, 20, style);
305
306         buttons_context_draw(C, layout);
307
308         uiBlockLayoutResolve(C, block, &x, &y);
309         uiEndBlock(C, block);
310
311         /* draw */
312         UI_SetTheme(SPACE_BUTS, RGN_TYPE_WINDOW); /* XXX */
313
314         UI_GetThemeColor3fv(TH_BACK, col);
315         glClearColor(col[0], col[1], col[2], 0.0);
316         glClear(GL_COLOR_BUFFER_BIT);
317
318         UI_view2d_totRect_set(v2d, x, -y);
319         UI_view2d_view_ortho(C, v2d);
320
321         uiDrawBlock(C, block);
322
323         /* restore view matrix */
324         UI_view2d_view_restore(C);
325 }
326 #endif
327
328 /* reused! */
329 static void buttons_area_listener(ScrArea *sa, wmNotifier *wmn)
330 {
331         SpaceButs *sbuts= sa->spacedata.first;
332
333         /* context changes */
334         switch(wmn->category) {
335                 case NC_SCENE:
336                         switch(wmn->data) {
337                                 case ND_FRAME:
338                                 case ND_MODE:
339                                         ED_area_tag_redraw(sa);
340                                         break;
341                                         
342                                 case ND_OB_ACTIVE:
343                                         ED_area_tag_redraw(sa);
344                                         sbuts->preview= 1;
345                                         break;
346                         }
347                         break;
348                 case NC_OBJECT:
349                         switch(wmn->data) {
350                                 case ND_TRANSFORM:
351                                 case ND_BONE_ACTIVE:
352                                 case ND_BONE_SELECT:
353                                 case ND_GEOM_SELECT:
354                                         ED_area_tag_redraw(sa);
355                                         break;
356                                 case ND_SHADING:
357                                 case ND_SHADING_DRAW:
358                                         /* currently works by redraws... if preview is set, it (re)starts job */
359                                         sbuts->preview= 1;
360                                         break;
361                         }
362                         break;
363                 case NC_MATERIAL:
364                         ED_area_tag_redraw(sa);
365                         
366                         switch(wmn->data) {
367                                 case ND_SHADING:
368                                 case ND_SHADING_DRAW:
369                                         /* currently works by redraws... if preview is set, it (re)starts job */
370                                         sbuts->preview= 1;
371                                         break;
372                         }                                       
373                         break;
374                 case NC_WORLD:
375                         ED_area_tag_redraw(sa);
376                         sbuts->preview= 1;
377                 case NC_LAMP:
378                         ED_area_tag_redraw(sa);
379                         sbuts->preview= 1;
380                 case NC_TEXTURE:
381                         ED_area_tag_redraw(sa);
382                         sbuts->preview= 1;
383         }
384
385         if(wmn->data == ND_KEYS)
386                 ED_area_tag_redraw(sa);
387 }
388
389 /* only called once, from space/spacetypes.c */
390 void ED_spacetype_buttons(void)
391 {
392         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype buttons");
393         ARegionType *art;
394         
395         st->spaceid= SPACE_BUTS;
396         
397         st->new= buttons_new;
398         st->free= buttons_free;
399         st->init= buttons_init;
400         st->duplicate= buttons_duplicate;
401         st->operatortypes= buttons_operatortypes;
402         st->keymap= buttons_keymap;
403         st->listener= buttons_area_listener;
404         st->context= buttons_context;
405         
406         /* regions: main window */
407         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
408         art->regionid = RGN_TYPE_WINDOW;
409         art->init= buttons_main_area_init;
410         art->draw= buttons_main_area_draw;
411         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
412         BLI_addhead(&st->regiontypes, art);
413
414         buttons_context_register(art);
415         
416         /* regions: header */
417         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
418         art->regionid = RGN_TYPE_HEADER;
419         art->minsizey= HEADERY;
420         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES;
421         
422         art->init= buttons_header_area_init;
423         art->draw= buttons_header_area_draw;
424         BLI_addhead(&st->regiontypes, art);
425
426 #if 0
427         /* regions: context */
428         art= MEM_callocN(sizeof(ARegionType), "spacetype buttons region");
429         art->regionid = RGN_TYPE_CHANNELS;
430         art->minsizey= CONTEXTY;
431         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
432         art->init= buttons_context_area_init;
433         art->draw= buttons_context_area_draw;;
434         art->listener= buttons_area_listener;
435         
436         BLI_addhead(&st->regiontypes, art);
437 #endif
438         
439         BKE_spacetype_register(st);
440 }
441