From 8d837d6a9049bc72364918ff10a45d660c896543 Mon Sep 17 00:00:00 2001 From: pomyk Date: Thu, 8 Feb 2007 18:29:50 +0000 Subject: [PATCH] Ruby: key_frames function, improvements in scripts Originally committed to SVN as r934. --- aegisub/auto4_ruby.cpp | 27 +++++++++++++++- aegisub/auto4_ruby.h | 1 + automation/demos/k-replacer.rb | 15 ++++----- automation/include/utils.rb | 56 +++++++++++++++++++++------------- 4 files changed, 69 insertions(+), 30 deletions(-) diff --git a/aegisub/auto4_ruby.cpp b/aegisub/auto4_ruby.cpp index d1cad5cfb..358dd7dac 100644 --- a/aegisub/auto4_ruby.cpp +++ b/aegisub/auto4_ruby.cpp @@ -42,6 +42,7 @@ #include "text_file_reader.h" #include "options.h" #include "vfr.h" +#include "video_context.h" #include "main.h" #include "frame_main.h" #include "subs_grid.h" @@ -115,6 +116,7 @@ namespace Automation4 { rb_define_module_function(RubyAegisub, "text_extents",reinterpret_cast(&RubyTextExtents), 2); rb_define_module_function(RubyAegisub, "frame_to_time",reinterpret_cast(&RubyFrameToTime), 1); rb_define_module_function(RubyAegisub, "time_to_frame",reinterpret_cast(&RubyTimeToFrame), 1); + rb_define_module_function(RubyAegisub, "key_frames",reinterpret_cast(&RubyKeyFrames), 0); rb_define_module_function(rb_eException, "set_backtrace",reinterpret_cast(&backtrace_hook), 1); rb_define_module_function(RubyScript::RubyAegisub, "progress_set",reinterpret_cast(&RubyProgressSink::RubySetProgress), 1); rb_define_module_function(RubyScript::RubyAegisub, "progress_task",reinterpret_cast(&RubyProgressSink::RubySetTask), 1); @@ -164,7 +166,7 @@ namespace Automation4 { void RubyScript::Destroy() { if(loaded) { - ruby_finalize(); // broken in 1.9 ?_? +// ruby_finalize(); // broken in 1.9 ?_? } // remove features @@ -232,6 +234,29 @@ namespace Automation4 { return Qnil; } + ////////////////////////////////////////////////////////////////////////// + // output: [[keyframe indices], [keyframe times in ms]] + VALUE RubyScript::RubyKeyFrames(VALUE self) + { + if(!VideoContext::Get()->KeyFramesLoaded()) + return Qnil; + + wxArrayInt key_frames = VideoContext::Get()->GetKeyFrames(); + + VALUE frames = rb_ary_new(); + VALUE times = rb_ary_new(); + + for(int i = 0; i < key_frames.size(); ++i) + { + rb_ary_push(frames, INT2FIX(key_frames[i])); + rb_ary_push(times, INT2FIX(VFR_Output.GetTimeAtFrame(key_frames[i], true))); + } + VALUE res = rb_ary_new(); + rb_ary_push(res, frames); + rb_ary_push(res, times); + return res; + } + wxString RubyScript::GetError() { return wxString(error + _T("\n") + backtrace); diff --git a/aegisub/auto4_ruby.h b/aegisub/auto4_ruby.h index d064b24a9..2ab02add3 100644 --- a/aegisub/auto4_ruby.h +++ b/aegisub/auto4_ruby.h @@ -168,6 +168,7 @@ namespace Automation4 { static VALUE RubyTextExtents(VALUE self, VALUE style, VALUE text); static VALUE RubyFrameToTime(VALUE self, VALUE frame); static VALUE RubyTimeToFrame(VALUE self, VALUE time); + static VALUE RubyKeyFrames(VALUE self); static VALUE backtrace_hook(VALUE self, VALUE backtr); public: diff --git a/automation/demos/k-replacer.rb b/automation/demos/k-replacer.rb index a80a69fcf..4fe9e7460 100644 --- a/automation/demos/k-replacer.rb +++ b/automation/demos/k-replacer.rb @@ -13,14 +13,13 @@ register_filter("Simple k-replacer", "k-replacer filter", 100, :k_replace_filter def k_replace_macro(subs, sel, act) - + cfg = k_replace_cfg(subs, nil) ok, opt = display_dialog(cfg, nil) return if not ok # cancelled -# write_options(subs, $script_name, opt) - i = 0 + + write_options(subs, {$script_name => opt}) subs.each do |l| - i += 1 k_replace(l, opt[:templ], opt[:strip]) if l[:class] == :dialogue && # replace if its dialogue (opt[:style] =="" || l[:style] == opt[:style]) # and has the right style end @@ -32,6 +31,7 @@ def k_replace_filter(subs, opt) k_replace(l, opt[:templ], opt[:strip]) if l[:class] == :dialogue && # replace if its dialogue opt[:style] =="" || l[:style] == opt[:style] # and has the right style end + write_options(subs, {$script_name => opt}) return subs end @@ -53,11 +53,12 @@ Calculation example: \\t(%$start+$dur*2%,$end,\\fscx90) head opt = read_options(subs, $script_name) + s_name = $script_name.to_sym cfg = ScriptCfg.new # helper class for building dialogs cfg.header header_text, :x => 1, :width => 1 - cfg.edit :templ, "template", :text => opt[:templ] - cfg.dropdown :style, "Style", :items => styles, :value => opt[:style] - cfg.checkbox :strip, "", :label => "Strip tags?", :value => (opt[:strip] == "true" ? true : false) + cfg.edit :templ, "template", :text => opt[s_name][:templ] + cfg.dropdown :style, "Style", :items => styles, :value => opt[s_name][:style] + cfg.checkbox :strip, "", :label => "Strip tags?", :value => (opt[s_name][:strip] == "true" ? true : false) cfg.to_ary # convert to array end diff --git a/automation/include/utils.rb b/automation/include/utils.rb index da39d5206..da9510a6a 100644 --- a/automation/include/utils.rb +++ b/automation/include/utils.rb @@ -61,31 +61,43 @@ module Aegisub end - # inserts line with options into the file - def write_options(subs, name, opt, sep = "~~") - i = 0 - while subs[i][:class] != :info do - i += 1 - end - while subs[i][:class] == :info do - i += 1 - end - l = {:class => :info, :key => name, :value => opt.to_a.flatten!.join(sep), :section => "[Script Info]"} - subs = subs.insert(i, l) + # inserts lines with options into [Script Info] section + def write_options(subs, opt, sep = "~~") + subs.collect! do |l| + if l[:class] == :info + info = true + value = opt.delete(l[:key]) + l[:value] = value.instance_of?(Hash) ? value.to_a.flatten!.join(sep) : value.to_s if value + l + else + if info + r = [l] + opt.each do |key, val| + r << {:class => :info, :key => key, + :value => value.instance_of?(Hash) ? value.to_a.flatten!.join(sep) : value.to_s, + :section => "[Script Info]"} + end + info = false + r + else + l + end + end + end end - # returns hash with options loaded from the subs - def read_options(subs, name, sep = "~~") - i = 0 - i += 1 while subs[i][:class] != :info - i += 1 while subs[i][:class] == :info && subs[i][:key] != name - return {} if subs[i][:class] != :info - a = subs[i][:value].split(sep) - h = {} - (a.size/2).times do |j| - h[a[2*j].to_sym] = a[2*j+1] + # returns a hash with options from [Script Info] section + def read_options(subs, name, sep = "~~") + opt = {} + subs.each { |l| opt[l[:key].to_sym] = l[:value] if l[:class] == :info } + n_sym = name.to_sym + if opt[n_sym] # parsing of script specific options + a = opt[n_sym].split(sep) + h = {} + (a.size/2).times { |j| h[a[2*j].to_sym] = a[2*j+1] } + opt[n_sym] = h end - return h + return opt end end