Blender  V2.59
time_ops.c
Go to the documentation of this file.
00001 /*
00002  * $Id: time_ops.c 36380 2011-04-29 06:59:18Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2008 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * 
00024  * Contributor(s): Blender Foundation
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <stdlib.h>
00035 #include <math.h>
00036 
00037 #include "DNA_scene_types.h"
00038 
00039 #include "BLI_blenlib.h"
00040 #include "BLI_utildefines.h"
00041 
00042 #include "BKE_context.h"
00043 
00044 #include "ED_screen.h"
00045 
00046 #include "WM_api.h"
00047 #include "WM_types.h"
00048 
00049 #include "time_intern.h"
00050 
00051 /* ****************** Start/End Frame Operators *******************************/
00052 static int time_set_sfra_exec (bContext *C, wmOperator *UNUSED(op))
00053 {
00054         Scene *scene= CTX_data_scene(C);
00055         int frame;
00056 
00057         if (scene == NULL)
00058                 return OPERATOR_CANCELLED;
00059 
00060         frame= CFRA;
00061         /* if 'end frame' (Preview Range or Actual) is less than 'frame', 
00062          * clamp 'frame' to 'end frame'
00063          */
00064         if (PEFRA < frame) frame= PEFRA;
00065                 
00066         /* if Preview Range is defined, set the 'start' frame for that */
00067         if (PRVRANGEON)
00068                 scene->r.psfra= frame;
00069         else
00070                 scene->r.sfra= frame;
00071         
00072         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
00073         
00074         return OPERATOR_FINISHED;
00075 }
00076 
00077 static void TIME_OT_start_frame_set (wmOperatorType *ot)
00078 {
00079         /* identifiers */
00080         ot->name= "Set Start Frame";
00081         ot->idname= "TIME_OT_start_frame_set";
00082         ot->description="Set the start frame";
00083         
00084         /* api callbacks */
00085         ot->exec= time_set_sfra_exec;
00086         ot->poll= ED_operator_timeline_active;
00087         
00088         /* flags */
00089         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00090 }       
00091 
00092 
00093 static int time_set_efra_exec (bContext *C, wmOperator *UNUSED(op))
00094 {
00095         Scene *scene= CTX_data_scene(C);
00096         int frame;
00097 
00098         if (scene == NULL)
00099                 return OPERATOR_CANCELLED;
00100 
00101         frame= CFRA;
00102 
00103         /* if 'start frame' (Preview Range or Actual) is greater than 'frame', 
00104          * clamp 'frame' to 'end frame'
00105          */
00106         if (PSFRA > frame) frame= PSFRA;
00107                 
00108         /* if Preview Range is defined, set the 'end' frame for that */
00109         if (PRVRANGEON)
00110                 scene->r.pefra= frame;
00111         else
00112                 scene->r.efra= frame;
00113         
00114         WM_event_add_notifier(C, NC_SCENE|ND_FRAME, scene);
00115         
00116         return OPERATOR_FINISHED;
00117 }
00118 
00119 static void TIME_OT_end_frame_set (wmOperatorType *ot)
00120 {
00121         /* identifiers */
00122         ot->name= "Set End Frame";
00123         ot->idname= "TIME_OT_end_frame_set";
00124         ot->description="Set the end frame";
00125         
00126         /* api callbacks */
00127         ot->exec= time_set_efra_exec;
00128         ot->poll= ED_operator_timeline_active;
00129         
00130         /* flags */
00131         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00132 }
00133 
00134 /* ************************ View All Operator *******************************/
00135 
00136 static int time_view_all_exec (bContext *C, wmOperator *UNUSED(op))
00137 {
00138         Scene *scene= CTX_data_scene(C);
00139         ARegion *ar= CTX_wm_region(C);
00140         View2D *v2d= (ar) ? &ar->v2d : NULL;
00141         float extra;
00142         
00143         if ELEM(NULL, scene, ar)
00144                 return OPERATOR_CANCELLED;
00145                 
00146         /* set extents of view to start/end frames (Preview Range too) */
00147         v2d->cur.xmin= (float)PSFRA;
00148         v2d->cur.xmax= (float)PEFRA;
00149         
00150         /* we need an extra "buffer" factor on either side so that the endpoints are visible */
00151         extra= 0.01f * (v2d->cur.xmax - v2d->cur.xmin);
00152         v2d->cur.xmin -= extra;
00153         v2d->cur.xmax += extra;
00154         
00155         /* this only affects this TimeLine instance, so just force redraw of this region */
00156         ED_region_tag_redraw(ar);
00157         
00158         return OPERATOR_FINISHED;
00159 }
00160 
00161 static void TIME_OT_view_all (wmOperatorType *ot)
00162 {
00163         /* identifiers */
00164         ot->name= "View All";
00165         ot->idname= "TIME_OT_view_all";
00166         ot->description= "Show the entire playable frame range";
00167         
00168         /* api callbacks */
00169         ot->exec= time_view_all_exec;
00170         ot->poll= ED_operator_timeline_active;
00171         
00172         /* flags */
00173         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00174 }
00175 
00176 /* ************************** registration **********************************/
00177 
00178 void time_operatortypes(void)
00179 {
00180         WM_operatortype_append(TIME_OT_start_frame_set);
00181         WM_operatortype_append(TIME_OT_end_frame_set);
00182         WM_operatortype_append(TIME_OT_view_all);
00183 }
00184 
00185 void time_keymap(wmKeyConfig *keyconf)
00186 {
00187         wmKeyMap *keymap= WM_keymap_find(keyconf, "Timeline", SPACE_TIME, 0);
00188         
00189         WM_keymap_add_item(keymap, "TIME_OT_start_frame_set", SKEY, KM_PRESS, 0, 0);
00190         WM_keymap_add_item(keymap, "TIME_OT_end_frame_set", EKEY, KM_PRESS, 0, 0);
00191         WM_keymap_add_item(keymap, "TIME_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
00192 }
00193