~ubuntu-branches/ubuntu/trusty/aegisub/trusty

« back to all changes in this revision

Viewing changes to automation/include/utils.rb

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel
  • Date: 2012-03-16 22:58:00 UTC
  • Revision ID: package-import@ubuntu.com-20120316225800-yfb8h9e5n04rk46a
Tags: upstream-2.1.9
ImportĀ upstreamĀ versionĀ 2.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include Aegisub
 
2
 
 
3
class Object
 
4
    def deep_clone
 
5
        Marshal.load(Marshal.dump(self))
 
6
    end
 
7
end
 
8
 
 
9
 
 
10
 
 
11
module Aegisub
 
12
 
 
13
        class ScriptCfg
 
14
                def initialize  # constructor
 
15
                        @opt = []
 
16
                        @x = 0
 
17
                        @y = 0
 
18
                        @labels = true
 
19
                        @width = 1 # TODO
 
20
                        @height = 1
 
21
                end
 
22
                
 
23
        private
 
24
                def control(type, name, opt = {})
 
25
                        @opt << {:class => type, :name => name, :x => @x, :y => @y, 
 
26
                                :width => 1, :height => 1}.merge!(opt)
 
27
                end
 
28
 
 
29
                # some meta-programming :]              
 
30
                def self.create_functions(*arr)
 
31
                        arr.each do |a|
 
32
                                class_eval(%Q[
 
33
                                        def #{a.to_s}(name, text, opt = {})
 
34
                                                if @labels; label text, opt; @x += 1; end
 
35
                                                control "#{a.to_s}", name, opt
 
36
                                                @y += 1
 
37
                                                @x = 0
 
38
                                        end
 
39
                                ])
 
40
                        end
 
41
                end
 
42
 
 
43
        public
 
44
                create_functions *[:edit, :intedit, :floatedit, :textbox, 
 
45
                        :dropdown, :checkbox, :color, :coloralpha, :alpha ]
 
46
 
 
47
                def no_labels; @labels = false; end
 
48
        
 
49
                def label(text, opt = {})
 
50
                        control :label, text, opt.merge({:label => text})
 
51
                end
 
52
                
 
53
                def header(text, opt = {})
 
54
                        label text, opt.merge!({:width => 2})
 
55
                        @y += 1
 
56
                end
 
57
 
 
58
                def to_ary      # conversion to array
 
59
                        @opt
 
60
                end
 
61
 
 
62
        end
 
63
 
 
64
        # inserts lines with options into [Script Info] section
 
65
        def write_options(subs, opt, sep = "~~")
 
66
                subs.collect! do |l|
 
67
                        if l[:class] == :info
 
68
                                info = true
 
69
                                value = opt.delete(l[:key])
 
70
                                l[:value] = value.instance_of?(Hash) ? value.to_a.flatten!.join(sep) : value.to_s if value
 
71
                                l
 
72
                        elsif info
 
73
                                r = [l]
 
74
                                opt.each do |key, val|
 
75
                                        r << {:class => :info, :key => key, 
 
76
                                        :value => value.instance_of?(Hash) ? value.to_a.flatten!.join(sep) : value.to_s,
 
77
                                        :section => "[Script Info]"}
 
78
                                end
 
79
                                info = false
 
80
                                r
 
81
                        else
 
82
                                l                       
 
83
                        end
 
84
                end     
 
85
        end
 
86
 
 
87
        # returns a hash with options from [Script Info] section        
 
88
        def read_options(subs, name, sep = "~~")                
 
89
                opt = {}
 
90
                subs.each { |l| opt[l[:key].to_sym] = l[:value] if l[:class] == :info }
 
91
                n_sym = name.to_sym
 
92
                if opt[n_sym]   # parsing of script specific options
 
93
                        a = opt[n_sym].split(sep)
 
94
                        h = {}
 
95
                        (a.size/2).times { |j|  h[a[2*j].to_sym] = a[2*j+1] }
 
96
                        opt[n_sym] = h
 
97
                end
 
98
                return opt
 
99
        end
 
100
 
 
101
        def rgb_to_ssa(*c)
 
102
                res = "&H"
 
103
                c.reverse_each {|v| res << "%02X" % v}
 
104
                res << "&"
 
105
                return res
 
106
        end
 
107
 
 
108
        def ssa_to_rgb(col)
 
109
                res = []
 
110
                col.scan(/[0-9a-fA-F]{2}/) { res.unshift $1.to_i(16) }
 
111
                res
 
112
        end
 
113
end
 
114