Class: SLComponents::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/mk_semi_lattice/sl_components.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Node

Returns a new instance of Node.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mk_semi_lattice/sl_components.rb', line 140

def initialize(attrs = {})
  @name      = attrs[:name]
  @label     = attrs[:label]
  @x         = attrs[:x] || rand(80..520)
  @y         = attrs[:y] || rand(80..520)
  @fixed     = attrs[:fixed] || false
  @linked    = attrs[:linked] || false
  @color     = attrs[:color] || NODE_COLOR
  @dx        = attrs[:dx] || 0.0
  @dy        = attrs[:dy] || 0.0
  @type      = attrs[:type]
  @icon_path = attrs[:icon_path] || nil
  @file_path = attrs[:file_path]
  @circle = nil
  @text = nil
  @created = false
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def color
  @color
end

#dxObject

Returns the value of attribute dx.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def dy
  @dy
end

#file_pathObject

Returns the value of attribute file_path.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def file_path
  @file_path
end

#fixedObject

Returns the value of attribute fixed.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def fixed
  @fixed
end

#icon_pathObject

Returns the value of attribute icon_path.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def icon_path
  @icon_path
end

#labelObject

Returns the value of attribute label.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def label
  @label
end

#linkedObject

Returns the value of attribute linked.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def linked
  @linked
end

#nameObject

Returns the value of attribute name.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def name
  @name
end

#typeObject

Returns the value of attribute type.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def type
  @type
end

#xObject

Returns the value of attribute x.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def x
  @x
end

#yObject

Returns the value of attribute y.



138
139
140
# File 'lib/mk_semi_lattice/sl_components.rb', line 138

def y
  @y
end

Instance Method Details

#create_graphicsObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/mk_semi_lattice/sl_components.rb', line 212

def create_graphics
  return if @created # すでに作成済みなら何もしない
  font_path = japanese_font

  # テキスト生成(共通化)
  if font_path && File.exist?(font_path)
    @text = Text.new(label, x: x-28, y: y+35, size: 14, color: 'black', font: font_path, z: 11)
  else
    @text = Text.new(label, x: x-28, y: y+35, size: 14, color: 'black', z: 11)
  end

  # アイコン生成
  @circle = case @type
    when 'dir_icon'
      FolderIcon.new(x: x, y: y, color: NODE_COLOR, z: 10)
    when 'dir'
      FolderIcon.new(x: x, y: y, color: NODE_COLOR, z: 10)
    when 'document'
      Document.new(x: x, y: y, color: NODE_COLOR, z: 10)
    when 'icon'
      p @type
      Icon.new(x: x, y: y, color: NODE_COLOR, z: 10, icon_path: @icon_path)
    else #when 'file'
      Circle.new(x: x, y: y, radius: 30, color: NODE_COLOR, z: 10)
    end
  @created = true
end

#draw(selected) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/mk_semi_lattice/sl_components.rb', line 240

def draw(selected)
  create_graphics # すでに作成済みなら何もしない
  
  c = if selected
        SELECT_COLOR
      elsif @fixed
        FIXED_COLOR
      elsif @linked
        LINKED_COLOR
      else
        NODE_COLOR
      end
  @circle.color = c # これ以降でcomponentをupdate
  @circle.x = @x
  @circle.y = @y
  @text.x = @x - @text.width / 2
  @text.y = @y + 35
end

#japanese_fontObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/mk_semi_lattice/sl_components.rb', line 197

def japanese_font
  fonts = [
    '/usr/share/fonts/truetype/takao-gothic/TakaoGothic.ttf',
    '/usr/share/fonts/truetype/vlgothic/VL-Gothic-Regular.ttf',
    '/usr/share/fonts/truetype/fonts-japanese-gothic.ttf',
    '/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc',
    '/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',
    '/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
    '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
  ]
  selected_font = fonts.find { |font| File.exist?(font) }
  # puts "Selected font: #{selected_font}" if selected_font
  selected_font || fonts.last
end

#relax(nodes) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mk_semi_lattice/sl_components.rb', line 170

def relax(nodes)
  ddx = 0
  ddy = 0

  nodes.each do |n|
    next if n == self
    
    vx = x - n.x
    vy = y - n.y
    lensq = vx * vx + vy * vy
    
    if lensq == 0
      ddx += rand(-1.0..1.0)
      ddy += rand(-1.0..1.0)
    elsif lensq < 100 * 100
      ddx += vx / lensq
      ddy += vy / lensq
    end
  end
  
  dlen = Math.sqrt(ddx * ddx + ddy * ddy) / 2
  if dlen > 0
    @dx += ddx / dlen
    @dy += ddy / dlen
  end
end

#updateObject



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/mk_semi_lattice/sl_components.rb', line 158

def update
  unless fixed
    @x += [@dx, -5, 5].sort[1]
    @y += [@dy, -5, 5].sort[1]
    
    @x = [[@x, 0].max, 600].min
    @y = [[@y, 0].max, 600].min
  end
  @dx /= 2.0
  @dy /= 2.0
end