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.



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

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.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def color
  @color
end

#dxObject

Returns the value of attribute dx.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def dx
  @dx
end

#dyObject

Returns the value of attribute dy.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def dy
  @dy
end

#file_pathObject

Returns the value of attribute file_path.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def file_path
  @file_path
end

#fixedObject

Returns the value of attribute fixed.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def fixed
  @fixed
end

#icon_pathObject

Returns the value of attribute icon_path.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def icon_path
  @icon_path
end

#labelObject

Returns the value of attribute label.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def label
  @label
end

#linkedObject

Returns the value of attribute linked.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def linked
  @linked
end

#nameObject

Returns the value of attribute name.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def name
  @name
end

#typeObject

Returns the value of attribute type.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def type
  @type
end

#xObject

Returns the value of attribute x.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def x
  @x
end

#yObject

Returns the value of attribute y.



135
136
137
# File 'lib/mk_semi_lattice/sl_components.rb', line 135

def y
  @y
end

Instance Method Details

#create_graphicsObject



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

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-10, size: 18, color: 'black', font: font_path, z: 11)
  else
    @text = Text.new(label, x: x-28, y: y-10, size: 18, color: 'black', z: 11)
  end

  # アイコン生成
  @circle = case @type
    when 'dir_icon'
      FolderIcon.new(x: x, y: y, color: NODE_COLOR, z: 10)
    when 'dir'
      Folder.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



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

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 - 28
  @text.y = @y
end

#japanese_fontObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/mk_semi_lattice/sl_components.rb', line 194

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



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

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



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mk_semi_lattice/sl_components.rb', line 155

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