int values were not drawing curve handles, yet the handles could still be selected...
[blender-staging.git] / source / blender / editors / space_graph / graph_draw.c
index 19288bae2a2696c38a713e577d5a113febcf860e..382bb71a5926060e1050dddd0c0b61fd03076158 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/editors/space_graph/graph_draw.c
+ *  \ingroup spgraph
+ */
+
+
 #include <stdio.h>
 #include <math.h>
 #include <string.h>
 #include <float.h>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #ifndef _WIN32
 #include <unistd.h>
 #else
 #include <io.h>
 #endif
 
-#include "MEM_guardedalloc.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
+#include "BLI_utildefines.h"
 
 #include "DNA_anim_types.h"
-#include "DNA_action_types.h"
-#include "DNA_camera_types.h"
-#include "DNA_curve_types.h"
-#include "DNA_key_types.h"
-#include "DNA_lamp_types.h"
-#include "DNA_material_types.h"
-#include "DNA_meta_types.h"
 #include "DNA_object_types.h"
-#include "DNA_particle_types.h"
-#include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_space_types.h"
-#include "DNA_sequence_types.h"
-#include "DNA_userdef_types.h"
-#include "DNA_view2d_types.h"
 #include "DNA_windowmanager_types.h"
-#include "DNA_world_types.h"
 
-#include "BKE_animsys.h"
 #include "BKE_context.h"
 #include "BKE_curve.h"
-#include "BKE_depsgraph.h"
 #include "BKE_fcurve.h"
-#include "BKE_global.h"
-#include "BKE_key.h"
-#include "BKE_object.h"
-#include "BKE_screen.h"
-#include "BKE_utildefines.h"
+
 
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
 
 #include "ED_anim_api.h"
-#include "ED_util.h"
 
 #include "graph_intern.h"
 
 #include "UI_interface.h"
-#include "UI_interface_icons.h"
 #include "UI_resources.h"
 #include "UI_view2d.h"
 
@@ -96,7 +76,7 @@
  */
 #define drawFCurveFade(fcu) ( ((fcu)->flag & FCURVE_SELECTED)? 1.0f : 0.25f )
 
-/* set the colour for some point from some value given packed into an int 
+/* set the color for some point from some value given packed into an int 
  *     - intV: integer value containing color info packed into an int
  *     - alpha: float value describing the 
  */
 /* Envelope -------------- */
 
 // TODO: draw a shaded poly showing the region of influence too!!!
-static void draw_fcurve_modifier_controls_envelope (FCurve *fcu, FModifier *fcm, View2D *v2d)
+static void draw_fcurve_modifier_controls_envelope (FModifier *fcm, View2D *v2d)
 {
        FMod_Envelope *env= (FMod_Envelope *)fcm->data;
        FCM_EnvelopeData *fed;
@@ -163,7 +143,7 @@ static void draw_fcurve_modifier_controls_envelope (FCurve *fcu, FModifier *fcm,
 /* Points ---------------- */
 
 /* helper func - draw keyframe vertices only for an F-Curve */
-static void draw_fcurve_vertices_keyframes (bAnimContext *ac, FCurve *fcu, View2D *v2d, short edit, short sel)
+static void draw_fcurve_vertices_keyframes (FCurve *fcu, SpaceIpo *UNUSED(sipo), View2D *v2d, short edit, short sel)
 {
        BezTriple *bezt= fcu->bezt;
        const float fac= 0.05f * (v2d->cur.xmax - v2d->cur.xmin);
@@ -235,7 +215,7 @@ static void draw_fcurve_handle_control (float x, float y, float xscale, float ys
 }
 
 /* helper func - draw handle vertices only for an F-Curve (if it is not protected) */
-static void draw_fcurve_vertices_handles (bAnimContext *ac, SpaceIpo *sipo, FCurve *fcu, View2D *v2d, short sel)
+static void draw_fcurve_vertices_handles (FCurve *fcu, SpaceIpo *sipo, View2D *v2d, short sel, short sel_handle_only)
 {
        BezTriple *bezt= fcu->bezt;
        BezTriple *prevbezt = NULL;
@@ -251,7 +231,7 @@ static void draw_fcurve_vertices_handles (bAnimContext *ac, SpaceIpo *sipo, FCur
        else UI_ThemeColor(TH_HANDLE_VERTEX);
        
        /* anti-aliased lines for more consistent appearance */
-       glEnable(GL_LINE_SMOOTH);
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
        glEnable(GL_BLEND);
        
        for (i=0; i < fcu->totvert; i++, prevbezt=bezt, bezt++) {
@@ -263,7 +243,7 @@ static void draw_fcurve_vertices_handles (bAnimContext *ac, SpaceIpo *sipo, FCur
                 * Also, need to take into account whether the keyframe was selected
                 * if a Graph Editor option to only show handles of selected keys is on.
                 */
-               if ( !(sipo->flag & SIPO_SELVHANDLESONLY) || BEZSELECTED(bezt) ) {
+               if ( !sel_handle_only || BEZSELECTED(bezt) ) {
                        if ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) {
                                if ((bezt->f1 & SELECT) == sel)/* && v2d->cur.xmin < bezt->vec[0][0] < v2d->cur.xmax)*/
                                        draw_fcurve_handle_control(bezt->vec[0][0], bezt->vec[0][1], xscale, yscale, hsize);
@@ -276,12 +256,12 @@ static void draw_fcurve_vertices_handles (bAnimContext *ac, SpaceIpo *sipo, FCur
                }
        }
        
-       glDisable(GL_LINE_SMOOTH);
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
        glDisable(GL_BLEND);
 }
 
 /* helper func - set color to draw F-Curve data with */
-static void set_fcurve_vertex_color (SpaceIpo *sipo, FCurve *fcu, short sel)
+static void set_fcurve_vertex_color (FCurve *fcu, short sel)
 {
        /* Fade the 'intensity' of the vertices based on the selection of the curves too */
        int alphaOffset= (int)((drawFCurveFade(fcu) - 1.0f) * 255);
@@ -300,7 +280,7 @@ static void set_fcurve_vertex_color (SpaceIpo *sipo, FCurve *fcu, short sel)
 }
 
 
-static void draw_fcurve_vertices (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, FCurve *fcu, int do_handles)
+static void draw_fcurve_vertices (SpaceIpo *sipo, ARegion *ar, FCurve *fcu, short do_handles, short sel_handle_only)
 {
        View2D *v2d= &ar->v2d;
        
@@ -316,19 +296,19 @@ static void draw_fcurve_vertices (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar,
        /* draw the two handles first (if they're shown, the curve doesn't have just a single keyframe, and the curve is being edited) */
        if (do_handles)
        {
-               set_fcurve_vertex_color(sipo, fcu, 0);
-               draw_fcurve_vertices_handles(ac, sipo, fcu, v2d, 0);
+               set_fcurve_vertex_color(fcu, 0);
+               draw_fcurve_vertices_handles(fcu, sipo, v2d, 0, sel_handle_only);
                
-               set_fcurve_vertex_color(sipo, fcu, 1);
-               draw_fcurve_vertices_handles(ac, sipo, fcu, v2d, 1);
+               set_fcurve_vertex_color(fcu, 1);
+               draw_fcurve_vertices_handles(fcu, sipo, v2d, 1, sel_handle_only);
        }
                
        /* draw keyframes over the handles */
-       set_fcurve_vertex_color(sipo, fcu, 0);
-       draw_fcurve_vertices_keyframes(ac, fcu, v2d, !(fcu->flag & FCURVE_PROTECTED), 0);
+       set_fcurve_vertex_color(fcu, 0);
+       draw_fcurve_vertices_keyframes(fcu, sipo, v2d, !(fcu->flag & FCURVE_PROTECTED), 0);
        
-       set_fcurve_vertex_color(sipo, fcu, 1);
-       draw_fcurve_vertices_keyframes(ac, fcu, v2d, !(fcu->flag & FCURVE_PROTECTED), 1);
+       set_fcurve_vertex_color(fcu, 1);
+       draw_fcurve_vertices_keyframes(fcu, sipo, v2d, !(fcu->flag & FCURVE_PROTECTED), 1);
        
        glPointSize(1.0f);
 }
@@ -340,7 +320,9 @@ static int draw_fcurve_handles_check(SpaceIpo *sipo, FCurve *fcu)
        /* don't draw handle lines if handles are not to be shown */
        if (    (sipo->flag & SIPO_NOHANDLES) || /* handles shouldn't be shown anywhere */
                        (fcu->flag & FCURVE_PROTECTED) || /* keyframes aren't editable */
+#if 0          /* handles can still be selected and handle types set, better draw - campbell */
                        (fcu->flag & FCURVE_INT_VALUES) || /* editing the handles here will cause weird/incorrect interpolation issues */
+#endif
                        ((fcu->grp) && (fcu->grp->flag & AGRP_PROTECTED)) || /* group that curve belongs to is not editable */
                        (fcu->totvert <= 1) /* do not show handles if there is only 1 keyframe, otherwise they all clump together in an ugly ball */
                ) 
@@ -355,9 +337,8 @@ static int draw_fcurve_handles_check(SpaceIpo *sipo, FCurve *fcu)
 
 /* draw lines for F-Curve handles only (this is only done in EditMode)
  * note: draw_fcurve_handles_check must be checked before running this. */
-static void draw_fcurve_handles (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
+static void draw_fcurve_handles (SpaceIpo *sipo, FCurve *fcu)
 {
-       extern unsigned int nurbcol[];
        int sel, b;
        
        /* a single call to GL_LINES here around these calls should be sufficient to still
@@ -371,8 +352,9 @@ static void draw_fcurve_handles (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar,
         */
        for (sel= 0; sel < 2; sel++) {
                BezTriple *bezt=fcu->bezt, *prevbezt=NULL;
-               unsigned int *col= (sel)? (nurbcol+4) : (nurbcol);
+               int basecol= (sel)? TH_HANDLE_SEL_FREE : TH_HANDLE_FREE;
                float *fp;
+               unsigned char col[4];
                
                /* if only selected keyframes have handles shown, skip the first round */
                if ((sel == 0) && (sipo->flag & SIPO_SELVHANDLESONLY))
@@ -390,19 +372,24 @@ static void draw_fcurve_handles (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar,
                        /* draw handle with appropriate set of colors if selection is ok */
                        if ((bezt->f2 & SELECT)==sel) {
                                fp= bezt->vec[0];
-                               
+
                                /* only draw first handle if previous segment had handles */
                                if ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) 
                                {
-                                       cpackA(col[(unsigned char)bezt->h1], drawFCurveFade(fcu));
+                                       UI_GetThemeColor3ubv(basecol + bezt->h1, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
                                        
                                        glVertex2fv(fp); glVertex2fv(fp+3); 
                                }
-                               
+
                                /* only draw second handle if this segment is bezier */
                                if (bezt->ipo == BEZT_IPO_BEZ) 
                                {
-                                       cpackA(col[(unsigned char)bezt->h2], drawFCurveFade(fcu));
+                                       UI_GetThemeColor3ubv(basecol + bezt->h2, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
+
                                        glVertex2fv(fp+3); glVertex2fv(fp+6); 
                                }
                        }
@@ -412,8 +399,10 @@ static void draw_fcurve_handles (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar,
                                         ( (!prevbezt && (bezt->ipo==BEZT_IPO_BEZ)) || (prevbezt && (prevbezt->ipo==BEZT_IPO_BEZ)) ) ) 
                                {
                                        fp= bezt->vec[0];
-                                       cpackA(col[(unsigned char)bezt->h1], drawFCurveFade(fcu));
-                                       
+                                       UI_GetThemeColor3ubv(basecol + bezt->h1, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
+
                                        glVertex2fv(fp); glVertex2fv(fp+3); 
                                }
                                
@@ -422,7 +411,9 @@ static void draw_fcurve_handles (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar,
                                         (bezt->ipo == BEZT_IPO_BEZ) )
                                {
                                        fp= bezt->vec[1];
-                                       cpackA(col[(unsigned char)bezt->h2], drawFCurveFade(fcu));
+                                       UI_GetThemeColor3ubv(basecol + bezt->h2, col);
+                                       col[3]= drawFCurveFade(fcu) * 255;
+                                       glColor4ubv((GLubyte *)col);
                                        
                                        glVertex2fv(fp); glVertex2fv(fp+3); 
                                }
@@ -472,7 +463,7 @@ static void draw_fcurve_sample_control (float x, float y, float xscale, float ys
 }
 
 /* helper func - draw keyframe vertices only for an F-Curve */
-static void draw_fcurve_samples (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
+static void draw_fcurve_samples (SpaceIpo *sipo, ARegion *ar, FCurve *fcu)
 {
        FPoint *first, *last;
        float hsize, xscale, yscale;
@@ -492,27 +483,35 @@ static void draw_fcurve_samples (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar,
        /* draw */
        if (first && last) {
                /* anti-aliased lines for more consistent appearance */
-               glEnable(GL_LINE_SMOOTH);
+               if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
                glEnable(GL_BLEND);
                
                draw_fcurve_sample_control(first->vec[0], first->vec[1], xscale, yscale, hsize);
                draw_fcurve_sample_control(last->vec[0], last->vec[1], xscale, yscale, hsize);
                
                glDisable(GL_BLEND);
-               glDisable(GL_LINE_SMOOTH);
+               if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
        }
 }
 
 /* Curve ---------------- */
 
 /* helper func - just draw the F-Curve by sampling the visible region (for drawing curves with modifiers) */
-static void draw_fcurve_curve (bAnimContext *ac, ID *id, FCurve *fcu, SpaceIpo *sipo, View2D *v2d, View2DGrid *grid)
+static void draw_fcurve_curve (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d, View2DGrid *grid)
 {
        ChannelDriver *driver;
        float samplefreq, ctime;
        float stime, etime;
        float unitFac;
-       
+       float dx, dy;
+
+       /* when opening a blend file on a different sized screen or while dragging the toolbar this can happen
+        * best just bail out in this case */
+       UI_view2d_grid_size(grid, &dx, &dy);
+       if(dx <= 0.0f)
+               return;
+
+
        /* disable any drivers temporarily */
        driver= fcu->driver;
        fcu->driver= NULL;
@@ -529,19 +528,18 @@ static void draw_fcurve_curve (bAnimContext *ac, ID *id, FCurve *fcu, SpaceIpo *
         *      though it is impossible to predict this from the modifiers!
         *
         *      If the automatically determined sampling frequency is likely to cause an infinite
-        *      loop (i.e. too close to FLT_EPSILON), fall back to default of 0.001
+        *      loop (i.e. too close to 0), then clamp it to a determined "safe" value. The value
+        *      chosen here is just the coarsest value which still looks reasonable...
         */
-               /* grid->dx is the first float in View2DGrid struct, so just cast to float pointer, and use it
-                * It represents the number of 'frames' between gridlines, but we divide by U.v2d_min_gridsize to get pixels-steps
-                */
+               /* grid->dx represents the number of 'frames' between gridlines, but we divide by U.v2d_min_gridsize to get pixels-steps */
                // TODO: perhaps we should have 1.0 frames as upper limit so that curves don't get too distorted?
-       samplefreq= *((float *)grid) / U.v2d_min_gridsize;
-       if (IS_EQ(samplefreq, 0)) samplefreq= 0.001f;
+       samplefreq= dx / U.v2d_min_gridsize;
+       if (samplefreq < 0.00001f) samplefreq= 0.00001f;
        
        
        /* the start/end times are simply the horizontal extents of the 'cur' rect */
        stime= v2d->cur.xmin;
-       etime= v2d->cur.xmax;
+       etime= v2d->cur.xmax + samplefreq; /* + samplefreq here so that last item gets included... */
        
        
        /* at each sampling interval, add a new vertex 
@@ -637,7 +635,7 @@ static void draw_fcurve_curve_samples (bAnimContext *ac, ID *id, FCurve *fcu, Vi
 }
 
 /* helper func - draw one repeat of an F-Curve */
-static void draw_fcurve_curve_bezts (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d, View2DGrid *grid)
+static void draw_fcurve_curve_bezts (bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d)
 {
        BezTriple *prevbezt= fcu->bezt;
        BezTriple *bezt= prevbezt+1;
@@ -794,7 +792,7 @@ static void draw_fcurve_curve_bezts (bAnimContext *ac, ID *id, FCurve *fcu, View
 /* Draw the 'ghost' F-Curves (i.e. snapshots of the curve) 
  * NOTE: unit mapping has already been applied to the values, so do not try and apply again
  */
-void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGrid *grid)
+void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar)
 {
        FCurve *fcu;
        
@@ -803,7 +801,7 @@ void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, Vie
        glLineWidth(3.0f);
        
        /* anti-aliased lines for less jagged appearance */
-       glEnable(GL_LINE_SMOOTH);
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
        glEnable(GL_BLEND);
        
        /* the ghost curves are simply sampled F-Curves stored in sipo->ghostCurves */
@@ -822,7 +820,7 @@ void graph_draw_ghost_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, Vie
        setlinestyle(0);
        glLineWidth(1.0f);
        
-       glDisable(GL_LINE_SMOOTH);
+       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
        glDisable(GL_BLEND);
 }
 
@@ -880,7 +878,7 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                        }
                        
                        /* anti-aliased lines for less jagged appearance */
-                       glEnable(GL_LINE_SMOOTH);
+                       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glEnable(GL_LINE_SMOOTH);
                        glEnable(GL_BLEND);
                        
                        /* draw F-Curve */
@@ -888,12 +886,12 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                                /* draw a curve affected by modifiers or only allowed to have integer values 
                                 * by sampling it at various small-intervals over the visible region 
                                 */
-                               draw_fcurve_curve(ac, ale->id, fcu, sipo, &ar->v2d, grid);
+                               draw_fcurve_curve(ac, ale->id, fcu, &ar->v2d, grid);
                        }
                        else if ( ((fcu->bezt) || (fcu->fpt)) && (fcu->totvert) ) { 
                                /* just draw curve based on defined data (i.e. no modifiers) */
                                if (fcu->bezt)
-                                       draw_fcurve_curve_bezts(ac, ale->id, fcu, &ar->v2d, grid);
+                                       draw_fcurve_curve_bezts(ac, ale->id, fcu, &ar->v2d);
                                else if (fcu->fpt)
                                        draw_fcurve_curve_samples(ac, ale->id, fcu, &ar->v2d);
                        }
@@ -901,7 +899,7 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                        /* restore settings */
                        setlinestyle(0);
                        
-                       glDisable(GL_LINE_SMOOTH);
+                       if ((sipo->flag & SIPO_BEAUTYDRAW_OFF)==0) glDisable(GL_LINE_SMOOTH);
                        glDisable(GL_BLEND);
                }
                
@@ -909,12 +907,12 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                 *      - if the option to only show controls if the F-Curve is selected is enabled, we must obey this
                 */
                if (!(sipo->flag & SIPO_SELCUVERTSONLY) || (fcu->flag & FCURVE_SELECTED)) {
-                       if (fcurve_needs_draw_fmodifier_controls(fcu, fcm)) {
+                       if (fcurve_are_keyframes_usable(fcu) == 0) {
                                /* only draw controls if this is the active modifier */
                                if ((fcu->flag & FCURVE_ACTIVE) && (fcm)) {
                                        switch (fcm->type) {
                                                case FMODIFIER_TYPE_ENVELOPE: /* envelope */
-                                                       draw_fcurve_modifier_controls_envelope(fcu, fcm, &ar->v2d);
+                                                       draw_fcurve_modifier_controls_envelope(fcm, &ar->v2d);
                                                        break;
                                        }
                                }
@@ -929,15 +927,15 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
                                        if (do_handles) {
                                                /* only draw handles/vertices on keyframes */
                                                glEnable(GL_BLEND);
-                                               draw_fcurve_handles(ac, sipo, ar, fcu);
+                                               draw_fcurve_handles(sipo, fcu);
                                                glDisable(GL_BLEND);
                                        }
                                        
-                                       draw_fcurve_vertices(ac, sipo, ar, fcu, do_handles);
+                                       draw_fcurve_vertices(sipo, ar, fcu, do_handles, (sipo->flag & SIPO_SELVHANDLESONLY));
                                }
                                else {
                                        /* samples: only draw two indicators at either end as indicators */
-                                       draw_fcurve_samples(ac, sipo, ar, fcu);
+                                       draw_fcurve_samples(sipo, ar, fcu);
                                }
                                
                                /* unapply unit mapping */
@@ -958,7 +956,7 @@ void graph_draw_curves (bAnimContext *ac, SpaceIpo *sipo, ARegion *ar, View2DGri
 /* Channel List */
 
 /* left hand part */
-void graph_draw_channel_names(bContext *C, bAnimContext *ac, SpaceIpo *sipo, ARegion *ar) 
+void graph_draw_channel_names(bContext *C, bAnimContext *ac, ARegion *ar) 
 {
        ListBase anim_data = {NULL, NULL};
        bAnimListElem *ale;