Class: PREP::Core::Group

Inherits:
Drawable show all
Defined in:
lib/core/group.rb

Overview

呼び出し可能グループの定義

Constant Summary collapse

@@allow_all =
false

Instance Attribute Summary collapse

Attributes inherited from Drawable

#identifier, #layer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Drawable

#<=>, #calculate_pos, #key_string_to_symbol, #rewind_current_page, #visible?

Constructor Details

#initialize(identifier = "main", values = { }) ⇒ Group

初期化



32
33
34
35
36
37
38
39
40
41
# File 'lib/core/group.rb', line 32

def initialize(identifier = "main", values = { })
  super(identifier)

  @drawables = { }
  values.keys.each do |key|
    unless key == "type" # type は除外
      add_drawable(key, values[key])
    end
  end
end

Instance Attribute Details

#drawablesObject (readonly)

Returns the value of attribute drawables.



21
22
23
# File 'lib/core/group.rb', line 21

def drawables
  @drawables
end

Class Method Details

.allow_allObject



23
24
25
# File 'lib/core/group.rb', line 23

def self.allow_all
  return @@allow_all
end

.allow_all=(v) ⇒ Object



27
28
29
# File 'lib/core/group.rb', line 27

def self.allow_all=(v)
  @@allow_all = !!v
end

Instance Method Details

#[](drawable_key) ⇒ Object

指定された識別子を持つ定義情報を返却



44
45
46
47
48
49
50
# File 'lib/core/group.rb', line 44

def [](drawable_key)
  if @drawables.has_key?(drawable_key.to_sym)
    return @drawables[drawable_key.to_sym]
  else
    raise "Unknown drawable key \"#{drawable_key}\"."
  end
end

#add_drawable(identifier, config, global = false) ⇒ Object

構成要素の追加

引数に渡されるのは単一の構成要素



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/core/group.rb', line 60

def add_drawable(identifier, config, global = false)
  # 事前にキー重複をチェック
  if @drawables.has_key?(identifier.to_sym)
    raise "Duplicated ID \"#{identifier}\"."
  end
  # キー重複が無ければ設定情報を読み込む
  # 構成要素種別の指定を確認
  case config["type"]
  when "label"
    klass = Label
    unless @@allow_all
      @drawables.values.each do |drawable|
        if Loop === drawable
          raise "Group already has Loop!!"
        end
      end
    end
  when "line"
    klass = Line
    unless @@allow_all
      @drawables.values.each do |drawable|
        if Loop === drawable
          raise "Group already has Loop!!"
        end
      end
    end
  when "rectangle"
    klass = Rectangle
    unless @@allow_all
      @drawables.values.each do |drawable|
        if Loop === drawable
          raise "Group already has Loop!!"
        end
      end
    end
  when "arc_rectangle"
    klass = ArcRectangle
    unless @@allow_all
      @drawables.values.each do |drawable|
        if Loop === drawable
          raise "Group already has Loop!!"
        end
      end
    end
  when "image"
    klass = Image
    unless @@allow_all
      @drawables.values.each do |drawable|
        if Loop === drawable
          raise "Group already has Loop!!"
        end
      end
    end
  when "group"
    # global でのみグループ定義を許可
    unless global
      raise "Group definition allowed at global level only for \"#{identifier}\"."
    end
    klass = Group
  when "loop"
    klass = Loop
    unless @@allow_all
      unless @drawables.size.zero?
        raise "Group has only one loop!!"
      end
    end
  else
    raise "Unknown type expression \"#{config["type"]}\"."
  end

  @drawables[identifier.to_sym] = klass.new(identifier, config)
end

#calculate_region(prep, region, values, stop_on_drawable = nil) ⇒ Object

グループを構成する各要素が全体で占有する領域サイズを返却



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/core/group.rb', line 149

def calculate_region(prep, region, values, stop_on_drawable = nil)
  if self === stop_on_drawable
    raise ReRenderJump.new(region)
  end
  puts "Calculate region for #{self.class}: #{self.identifier} region: #{region}" if ENV["DEBUG"]
  values ||= { }

  # 各構成要素の描画領域を計算して最大の領域を計算、width, height のみを利用
  group_region_size = drawable_items.inject({ }) do |size, drawable|
    if values.has_key?(drawable.identifier.to_sym)
      drawable_values = values[drawable.identifier.to_sym]
    else
      drawable_values = { }
    end

    width, height = drawable.calculate_region(prep, region, drawable_values, stop_on_drawable)

    size[:width] ||= width
    size[:height] ||= height
    size[:width] = width if size[:width] < width
    size[:height] = height if size[:height] < height

    next size
  end

  group_region_size[:width] ||= 0
  group_region_size[:height] ||= 0

  ret_region = Region.new(0, 0,
                          region.width - group_region_size[:width],
                          region.height - group_region_size[:height])
  return group_region_size[:width], group_region_size[:height]
end

#draw(prep, region, values, stop_on_drawable = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/core/group.rb', line 183

def draw(prep, region, values, stop_on_drawable = nil)
  if self === stop_on_drawable
    raise ReRenderJump.new(region)
  end
  STDERR.puts("Draw on #{self.class} #{self.identifier}") if ENV['DEBUG']
  values ||= { }
  # 管理対象の各オブジェクトに対して描画を開始
  drawable_items.each do |drawable|
    if values.has_key?(drawable.identifier.to_sym)
      drawable_values = values[drawable.identifier.to_sym]
    else
      drawable_values = { }
    end

    drawable.draw(prep, region, drawable_values, stop_on_drawable)
  end
end

#drawable_itemsObject

描画対象となる構成要素の一覧を返却



202
203
204
205
206
207
208
209
210
211
# File 'lib/core/group.rb', line 202

def drawable_items
  return @drawables.values.map { |d|
    case d
    when Group
      next nil
    else
      next d
    end
  }.compact.sort
end

#generate_sample_dataset(prep) ⇒ Object

データセット雛形を生成



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/core/group.rb', line 134

def generate_sample_dataset(prep)
  dataset = { }
  drawable_items.each do |drawable|
    case drawable
    when Loop, Group
      dataset[drawable.identifier.to_sym] = drawable.generate_sample_dataset(prep)
    when Label
      dataset[drawable.identifier.to_sym] = drawable.label
    end
  end

  return dataset
end

#has_identifier?(key) ⇒ Boolean

指定された識別子を持つ定義情報の有無を返却

Returns:

  • (Boolean)


53
54
55
# File 'lib/core/group.rb', line 53

def has_identifier?(key)
  return @drawables.has_key?(key)
end