1- import { CannotFindCompositionError , CannotSetTimeRemapError , CannotTuningError , HasNoVideoError , MyError , NoLayerSelectedError , NoMidiError , NoOptionsCheckedError , NotOneTrackForApplyEffectsOnlyError } from "../errors" ;
1+ import { CannotFindCompositionError , CannotSetTimeRemapError , CannotTuningError , EmptySubtitlesError , HasNoVideoError , InvalidDurationError , MyError , NoLayerSelectedError , NoMidiError , NoOptionsCheckedError , NotOneTrackForApplyEffectsOnlyError } from "../errors" ;
22import Portal from "../ui/Portal" ;
33import getComp from "../modules/getComp" ;
44import Setting from "../settings/Setting" ;
@@ -46,6 +46,8 @@ export default class Core {
4646 this . applyMarkerConductor ( comp ) ;
4747 else if ( tool === this . portal . toolsTab . ease )
4848 this . applyEase100Percent ( comp ) ;
49+ else if ( tool === this . portal . toolsTab . subtitle )
50+ this . applyGenerateSubtitles ( comp ) ;
4951 }
5052 } catch ( error ) {
5153 throw new MyError ( error as Error ) ;
@@ -544,7 +546,8 @@ export default class Core {
544546 const layers = comp . selectedLayers ;
545547 for ( const layer of layers ) {
546548 if ( layer === undefined ) continue ;
547- for ( const property of layer . selectedProperties as Property [ ] ) {
549+ const selectedProperties = Core . getSelectedProperties ( layer ) ;
550+ for ( const property of selectedProperties ) {
548551 if ( property === undefined ) continue ;
549552 for ( const keyIndex of property . selectedKeys ) {
550553 if ( keyIndex === undefined ) continue ;
@@ -554,6 +557,23 @@ export default class Core {
554557 }
555558 }
556559
560+ applyGenerateSubtitles ( comp : CompItem ) {
561+ const { subtitle } = this . portal . toolsTab ;
562+ const duration = parseFloat ( subtitle . durationTxt . text ) ;
563+ const subtitlesText = subtitle . subtitlesText . text ;
564+ if ( ! isFinite ( duration ) || duration <= 0 ) throw new InvalidDurationError ( ) ;
565+ if ( ! subtitlesText . trim ( ) . length ) throw new EmptySubtitlesError ( ) ;
566+ const subtitles = subtitlesText . replace ( / \r \n | \n \r | \r | \n / g, "\n" ) . split ( "\n" ) ;
567+
568+ app . beginUndoGroup ( "om midi - Apply Batch Subtitle Generation" ) ;
569+ const layer = comp . layers . addText ( ) ;
570+ const startTime = layer . startTime = this . getStartTime ( comp ) ;
571+ subtitles . forEach ( ( line , index ) => {
572+ if ( ! line . trim ( ) . length ) return ;
573+ layer . sourceText . setValueAtTime ( startTime + index * duration , new TextDocument ( line ) ) ;
574+ } ) ;
575+ }
576+
557577 //#region 辅助方法
558578 /**
559579 * 创建一个空对象图层。
@@ -604,6 +624,28 @@ export default class Core {
604624 return layer ( "Effects" ) as PropertyGroup ;
605625 }
606626
627+ /**
628+ * 获取选中的参数,用以获取选中的关键帧,但是要避免选中参数组,如果是参数组则通过递归来获取真正的参数。
629+ * @param layer - 图层或参数类。
630+ * @returns 选中的参数。
631+ */
632+ private static getSelectedProperties ( layer : Layer | _PropertyClasses [ ] ) : Property [ ] {
633+ const properties : Property [ ] = [ ] ;
634+ const propertyClasses = layer instanceof Array ? layer : layer . selectedProperties ;
635+ for ( const property of propertyClasses ) {
636+ if ( property === undefined ) continue ;
637+ else if ( property instanceof Property )
638+ properties . push ( property ) ;
639+ else {
640+ const subProperties : _PropertyClasses [ ] = [ ] ;
641+ for ( let i = 1 ; i <= property . numProperties ; i ++ )
642+ subProperties . push ( property . property ( i ) ) ;
643+ properties . push ( ...this . getSelectedProperties ( subProperties ) ) ;
644+ }
645+ }
646+ return properties ;
647+ }
648+
607649 /**
608650 * 为指定图层添加一个表达式控制 - 滑块控制的效果。
609651 * @param layer - 图层。
@@ -719,10 +761,20 @@ export default class Core {
719761 if ( ease . length !== 0 )
720762 property . setTemporalEaseAtKey ( keyIndex , ease as [ KeyframeEase ] ) ;
721763 const anotherSide = isHold ? KeyframeInterpolationType . HOLD : KeyframeInterpolationType . LINEAR ;
764+ const original : [ KeyframeInterpolationType , KeyframeInterpolationType ] =
765+ [ property . keyInInterpolationType ( keyIndex ) , property . keyOutInterpolationType ( keyIndex ) ] ;
722766 if ( easeType === EaseType . EASE_IN )
723767 property . setInterpolationTypeAtKey ( keyIndex , KeyframeInterpolationType . BEZIER , anotherSide ) ;
724768 else if ( easeType === EaseType . EASE_OUT )
725769 property . setInterpolationTypeAtKey ( keyIndex , anotherSide , KeyframeInterpolationType . BEZIER ) ;
770+ else if ( easeType === EaseType . LINEAR )
771+ property . setInterpolationTypeAtKey ( keyIndex , KeyframeInterpolationType . LINEAR ) ;
772+ else if ( easeType === EaseType . HOLD )
773+ property . setInterpolationTypeAtKey ( keyIndex , KeyframeInterpolationType . HOLD ) ;
774+ else if ( easeType === EaseType . HOLD_IN )
775+ property . setInterpolationTypeAtKey ( keyIndex , KeyframeInterpolationType . HOLD , original [ 1 ] ) ;
776+ else if ( easeType === EaseType . HOLD_OUT )
777+ property . setInterpolationTypeAtKey ( keyIndex , original [ 0 ] , KeyframeInterpolationType . HOLD ) ;
726778 }
727779 //#endregion
728780
0 commit comments