Fix [#21516] UI artifacts in array modifier
[blender.git] / source / blender / editors / space_nla / space_nla.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) 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_anim_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_math.h"
39 #include "BLI_rand.h"
40
41 #include "BKE_animsys.h"
42 #include "BKE_action.h"
43 #include "BKE_nla.h"
44 #include "BKE_colortools.h"
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_main.h"
48 #include "BKE_screen.h"
49 #include "BKE_utildefines.h"
50
51 #include "ED_anim_api.h"
52 #include "ED_markers.h"
53 #include "ED_screen.h"
54
55 #include "BIF_gl.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "UI_resources.h"
61 #include "UI_view2d.h"
62
63 #include "nla_intern.h" // own include
64
65 /* ******************** manage regions ********************* */
66
67 ARegion *nla_has_buttons_region(ScrArea *sa)
68 {
69         ARegion *ar, *arnew;
70         
71         for (ar= sa->regionbase.first; ar; ar= ar->next) {
72                 if (ar->regiontype==RGN_TYPE_UI)
73                         return ar;
74         }
75         
76         /* add subdiv level; after main */
77         for (ar= sa->regionbase.first; ar; ar= ar->next) {
78                 if (ar->regiontype==RGN_TYPE_WINDOW)
79                         break;
80         }
81         
82         /* is error! */
83         if (ar==NULL) return NULL;
84         
85         arnew= MEM_callocN(sizeof(ARegion), "buttons for nla");
86         
87         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
88         arnew->regiontype= RGN_TYPE_UI;
89         arnew->alignment= RGN_ALIGN_RIGHT;
90         
91         arnew->flag = RGN_FLAG_HIDDEN;
92         
93         return arnew;
94 }
95
96
97
98 /* ******************** default callbacks for nla space ***************** */
99
100 static SpaceLink *nla_new(const bContext *C)
101 {
102         Scene *scene= CTX_data_scene(C);
103         ScrArea *sa= CTX_wm_area(C);
104         ARegion *ar;
105         SpaceNla *snla;
106         
107         snla= MEM_callocN(sizeof(SpaceNla), "initnla");
108         snla->spacetype= SPACE_NLA;
109         
110         /* allocate DopeSheet data for NLA Editor */
111         snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
112         snla->ads->source= (ID *)scene;
113         
114         /* set auto-snapping settings */
115         snla->autosnap = SACTSNAP_FRAME;
116         
117         /* header */
118         ar= MEM_callocN(sizeof(ARegion), "header for nla");
119         
120         BLI_addtail(&snla->regionbase, ar);
121         ar->regiontype= RGN_TYPE_HEADER;
122         ar->alignment= RGN_ALIGN_BOTTOM;
123         
124         /* channel list region */
125         ar= MEM_callocN(sizeof(ARegion), "channel list for nla");
126         BLI_addtail(&snla->regionbase, ar);
127         ar->regiontype= RGN_TYPE_CHANNELS;
128         ar->alignment= RGN_ALIGN_LEFT;
129         
130                 /* only need to set these settings since this will use the 'stack' configuration */
131         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
132         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
133         
134         /* ui buttons */
135         ar= MEM_callocN(sizeof(ARegion), "buttons area for nla");
136         
137         BLI_addtail(&snla->regionbase, ar);
138         ar->regiontype= RGN_TYPE_UI;
139         ar->alignment= RGN_ALIGN_RIGHT;
140         ar->flag = RGN_FLAG_HIDDEN;
141         
142         /* main area */
143         ar= MEM_callocN(sizeof(ARegion), "main area for nla");
144         
145         BLI_addtail(&snla->regionbase, ar);
146         ar->regiontype= RGN_TYPE_WINDOW;
147         
148         ar->v2d.tot.xmin= (float)(SFRA-10);
149         ar->v2d.tot.ymin= (float)(-sa->winy)/3.0f;
150         ar->v2d.tot.xmax= (float)(EFRA+10);
151         ar->v2d.tot.ymax= 0.0f;
152         
153         ar->v2d.cur = ar->v2d.tot;
154         
155         ar->v2d.min[0]= 0.0f;
156          ar->v2d.min[1]= 0.0f;
157         
158         ar->v2d.max[0]= MAXFRAMEF;
159          ar->v2d.max[1]= 10000.0f;
160         
161         ar->v2d.minzoom= 0.01f;
162         ar->v2d.maxzoom= 50;
163         ar->v2d.scroll = (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
164         ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
165         ar->v2d.keepzoom= V2D_LOCKZOOM_Y;
166         ar->v2d.align= V2D_ALIGN_NO_POS_Y;
167         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
168         
169         return (SpaceLink *)snla;
170 }
171
172 /* not spacelink itself */
173 static void nla_free(SpaceLink *sl)
174 {       
175         SpaceNla *snla= (SpaceNla*) sl;
176         
177         if (snla->ads) {
178                 BLI_freelistN(&snla->ads->chanbase);
179                 MEM_freeN(snla->ads);
180         }
181 }
182
183
184 /* spacetype; init callback */
185 static void nla_init(struct wmWindowManager *wm, ScrArea *sa)
186 {
187         SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
188         
189         /* init dopesheet data if non-existant (i.e. for old files) */
190         if (snla->ads == NULL) {
191                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
192                 snla->ads->source= (ID *)G.main->scene.first; // XXX this is bad, but we need this to be set correct
193         }
194
195         ED_area_tag_refresh(sa);
196 }
197
198 static SpaceLink *nla_duplicate(SpaceLink *sl)
199 {
200         SpaceNla *snlan= MEM_dupallocN(sl);
201         
202         /* clear or remove stuff from old */
203         snlan->ads= MEM_dupallocN(snlan->ads);
204         
205         return (SpaceLink *)snlan;
206 }
207
208 /* add handlers, stuff you only do once or on area/region changes */
209 static void nla_channel_area_init(wmWindowManager *wm, ARegion *ar)
210 {
211         wmKeyMap *keymap;
212         
213         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
214         
215         /* own keymap */
216         // TODO: cannot use generic copy, need special NLA version
217         keymap= WM_keymap_find(wm->defaultconf, "NLA Channels", SPACE_NLA, 0);
218         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
219         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
220         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
221 }
222
223 /* draw entirely, view changes should be handled here */
224 static void nla_channel_area_draw(const bContext *C, ARegion *ar)
225 {
226         SpaceNla *snla= CTX_wm_space_nla(C);
227         bAnimContext ac;
228         View2D *v2d= &ar->v2d;
229         View2DScrollers *scrollers;
230         
231         /* clear and setup matrix */
232         UI_ThemeClearColor(TH_BACK);
233         glClear(GL_COLOR_BUFFER_BIT);
234         
235         UI_view2d_view_ortho(C, v2d);
236         
237         /* data */
238         if (ANIM_animdata_get_context(C, &ac)) {
239                 draw_nla_channel_list((bContext *)C, &ac, snla, ar);
240         }
241         
242         /* reset view matrix */
243         UI_view2d_view_restore(C);
244         
245         /* scrollers */
246         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
247         UI_view2d_scrollers_draw(C, v2d, scrollers);
248         UI_view2d_scrollers_free(scrollers);
249 }
250
251
252 /* add handlers, stuff you only do once or on area/region changes */
253 static void nla_main_area_init(wmWindowManager *wm, ARegion *ar)
254 {
255         wmKeyMap *keymap;
256         
257         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
258         
259         /* own keymap */
260         keymap= WM_keymap_find(wm->defaultconf, "NLA Editor", SPACE_NLA, 0);
261         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
262         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
263         WM_event_add_keymap_handler(&ar->handlers, keymap);
264 }
265
266 static void nla_main_area_draw(const bContext *C, ARegion *ar)
267 {
268         /* draw entirely, view changes should be handled here */
269         SpaceNla *snla= CTX_wm_space_nla(C);
270         bAnimContext ac;
271         View2D *v2d= &ar->v2d;
272         View2DGrid *grid;
273         View2DScrollers *scrollers;
274         short unit=0, flag=0;
275         
276         /* clear and setup matrix */
277         UI_ThemeClearColor(TH_BACK);
278         glClear(GL_COLOR_BUFFER_BIT);
279         
280         UI_view2d_view_ortho(C, v2d);
281         
282         /* time grid */
283         unit= (snla->flag & SNLA_DRAWTIME)? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
284         grid= UI_view2d_grid_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
285         UI_view2d_grid_draw(C, v2d, grid, V2D_GRIDLINES_ALL);
286         UI_view2d_grid_free(grid);
287         
288         /* data */
289         if (ANIM_animdata_get_context(C, &ac)) {
290                 /* strips and backdrops */
291                 draw_nla_main_data(&ac, snla, ar);
292                 
293                 /* text draw cached, in pixelspace now */
294                 UI_view2d_text_cache_draw(ar);
295         }
296         
297         UI_view2d_view_ortho(C, v2d);
298         
299         /* current frame */
300         if (snla->flag & SNLA_DRAWTIME)         flag |= DRAWCFRA_UNIT_SECONDS;
301         if ((snla->flag & SNLA_NODRAWCFRANUM)==0)  flag |= DRAWCFRA_SHOW_NUMBOX;
302         ANIM_draw_cfra(C, v2d, flag);
303         
304         /* markers */
305         UI_view2d_view_orthoSpecial(C, v2d, 1);
306         draw_markers_time(C, 0);
307         
308         /* preview range */
309         UI_view2d_view_ortho(C, v2d);
310         ANIM_draw_previewrange(C, v2d);
311         
312         /* reset view matrix */
313         UI_view2d_view_restore(C);
314         
315         /* scrollers */
316         scrollers= UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
317         UI_view2d_scrollers_draw(C, v2d, scrollers);
318         UI_view2d_scrollers_free(scrollers);
319 }
320
321
322 /* add handlers, stuff you only do once or on area/region changes */
323 static void nla_header_area_init(wmWindowManager *wm, ARegion *ar)
324 {
325         ED_region_header_init(ar);
326 }
327
328 static void nla_header_area_draw(const bContext *C, ARegion *ar)
329 {
330         ED_region_header(C, ar);
331 }
332
333 /* add handlers, stuff you only do once or on area/region changes */
334 static void nla_buttons_area_init(wmWindowManager *wm, ARegion *ar)
335 {
336         wmKeyMap *keymap;
337         
338         ED_region_panels_init(wm, ar);
339         
340         keymap= WM_keymap_find(wm->defaultconf, "NLA Generic", SPACE_NLA, 0);
341         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
342 }
343
344 static void nla_buttons_area_draw(const bContext *C, ARegion *ar)
345 {
346         ED_region_panels(C, ar, 1, NULL, -1);
347 }
348
349 static void nla_region_listener(ARegion *ar, wmNotifier *wmn)
350 {
351         /* context changes */
352         switch(wmn->category) {
353                 case NC_ANIMATION:
354                         ED_region_tag_redraw(ar);
355                         break;
356                 case NC_SCENE:
357                         switch(wmn->data) {
358                                 case ND_OB_ACTIVE:
359                                 case ND_FRAME:
360                                 case ND_MARKERS:
361                                         ED_region_tag_redraw(ar);
362                                         break;
363                         }
364                         break;
365                 case NC_OBJECT:
366                         switch(wmn->data) {
367                                 case ND_BONE_ACTIVE:
368                                 case ND_BONE_SELECT:
369                                 case ND_KEYS:
370                                         ED_region_tag_redraw(ar);
371                                         break;
372                         }
373                         break;
374                 default:
375                         if(wmn->data==ND_KEYS)
376                                 ED_region_tag_redraw(ar);
377                         break;
378         }
379 }
380
381
382 static void nla_main_area_listener(ARegion *ar, wmNotifier *wmn)
383 {
384         /* context changes */
385         switch(wmn->category) {
386                 case NC_ANIMATION:
387                         ED_region_tag_redraw(ar);
388                         break;
389                 case NC_SCENE:
390                         switch(wmn->data) {
391                                 case ND_RENDER_OPTIONS:
392                                 case ND_OB_ACTIVE:
393                                 case ND_FRAME:
394                                 case ND_MARKERS:
395                                         ED_region_tag_redraw(ar);
396                                         break;
397                         }
398                         break;
399                 case NC_OBJECT:
400                         switch(wmn->data) {
401                                 case ND_BONE_ACTIVE:
402                                 case ND_BONE_SELECT:
403                                 case ND_KEYS:
404                                 case ND_TRANSFORM:
405                                         ED_region_tag_redraw(ar);
406                                         break;
407                         }
408                         break;
409                 case NC_NODE:
410                         switch(wmn->action) {
411                                 case NA_EDITED:
412                                         ED_region_tag_redraw(ar);
413                                         break;
414                         }
415                         break;
416                 case NC_ID:
417                         if(wmn->action == NA_RENAME)
418                                 ED_region_tag_redraw(ar);
419                         break;
420                 default:
421                         if(wmn->data==ND_KEYS)
422                                 ED_region_tag_redraw(ar);
423         }
424 }
425
426 static void nla_channel_area_listener(ARegion *ar, wmNotifier *wmn)
427 {
428         /* context changes */
429         switch(wmn->category) {
430                 case NC_ANIMATION:
431                         ED_region_tag_redraw(ar);
432                         break;
433                 case NC_SCENE:
434                         switch(wmn->data) {
435                                 case ND_OB_ACTIVE:
436                                         ED_region_tag_redraw(ar);
437                                         break;
438                         }
439                         break;
440                 case NC_OBJECT:
441                         switch(wmn->data) {
442                                 case ND_BONE_ACTIVE:
443                                 case ND_BONE_SELECT:
444                                 case ND_KEYS:
445                                         ED_region_tag_redraw(ar);
446                                         break;
447                         }
448                         break;
449                 case NC_ID:
450                         if(wmn->action == NA_RENAME)
451                                 ED_region_tag_redraw(ar);
452                         break;
453
454                 default:
455                         if(wmn->data==ND_KEYS)
456                                 ED_region_tag_redraw(ar);
457         }
458 }
459
460 /* editor level listener */
461 static void nla_listener(ScrArea *sa, wmNotifier *wmn)
462 {
463         /* context changes */
464         switch (wmn->category) {
465                 case NC_ANIMATION:
466                         // TODO: filter specific types of changes?
467                         ED_area_tag_refresh(sa);
468                         break;
469                 case NC_SCENE:
470                         /*switch (wmn->data) {
471                                 case ND_OB_ACTIVE:
472                                 case ND_OB_SELECT:
473                                         ED_area_tag_refresh(sa);
474                                         break;
475                         }*/
476                         ED_area_tag_refresh(sa);
477                         break;
478                 case NC_OBJECT:
479                         switch (wmn->data) {
480                                 case ND_TRANSFORM:
481                                         /* do nothing */
482                                         break;
483                                 default:
484                                         ED_area_tag_refresh(sa);
485                                         break;
486                         }
487                         break;
488                 case NC_SPACE:
489                         if(wmn->data == ND_SPACE_NLA)
490                                 ED_area_tag_redraw(sa);
491                         break;
492         }
493 }
494
495 /* only called once, from space/spacetypes.c */
496 void ED_spacetype_nla(void)
497 {
498         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype nla");
499         ARegionType *art;
500         
501         st->spaceid= SPACE_NLA;
502         strncpy(st->name, "NLA", BKE_ST_MAXNAME);
503         
504         st->new= nla_new;
505         st->free= nla_free;
506         st->init= nla_init;
507         st->duplicate= nla_duplicate;
508         st->operatortypes= nla_operatortypes;
509         st->listener= nla_listener;
510         st->keymap= nla_keymap;
511         
512         /* regions: main window */
513         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
514         art->regionid = RGN_TYPE_WINDOW;
515         art->init= nla_main_area_init;
516         art->draw= nla_main_area_draw;
517         art->listener= nla_main_area_listener;
518         art->keymapflag= ED_KEYMAP_VIEW2D/*|ED_KEYMAP_MARKERS*/|ED_KEYMAP_ANIMATION|ED_KEYMAP_FRAMES;
519
520         BLI_addhead(&st->regiontypes, art);
521         
522         /* regions: header */
523         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
524         art->regionid = RGN_TYPE_HEADER;
525         art->prefsizey= HEADERY;
526         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
527         
528         art->init= nla_header_area_init;
529         art->draw= nla_header_area_draw;
530         
531         BLI_addhead(&st->regiontypes, art);
532         
533         /* regions: channels */
534         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
535         art->regionid = RGN_TYPE_CHANNELS;
536         art->prefsizex= 200;
537         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
538         
539         art->init= nla_channel_area_init;
540         art->draw= nla_channel_area_draw;
541         art->listener= nla_channel_area_listener;
542         
543         BLI_addhead(&st->regiontypes, art);
544         
545         /* regions: UI buttons */
546         art= MEM_callocN(sizeof(ARegionType), "spacetype nla region");
547         art->regionid = RGN_TYPE_UI;
548         art->prefsizex= 200;
549         art->keymapflag= ED_KEYMAP_UI;
550         art->listener= nla_region_listener;
551         art->init= nla_buttons_area_init;
552         art->draw= nla_buttons_area_draw;
553         
554         BLI_addhead(&st->regiontypes, art);
555
556         nla_buttons_register(art);
557         
558         
559         BKE_spacetype_register(st);
560 }
561