2015-07-09 15:34:02 +02:00
|
|
|
|
|
|
|
class SpaceView < PIXI::Graphics
|
2015-07-13 18:08:35 +02:00
|
|
|
include Sof::Util
|
2015-07-09 15:34:02 +02:00
|
|
|
|
2015-07-14 18:59:21 +02:00
|
|
|
def initialize max
|
2015-07-09 15:34:02 +02:00
|
|
|
super()
|
2015-07-14 18:59:21 +02:00
|
|
|
@max = max
|
|
|
|
@max.x -= 50
|
|
|
|
@max.y -= 20
|
2015-07-13 17:49:46 +02:00
|
|
|
space = Virtual.machine.space
|
|
|
|
# just a way to get the space into a list. objects is an id => occurence mapping.
|
|
|
|
# occurence.object is the object
|
|
|
|
objects = Sof::Members.new(space).objects
|
2015-07-09 15:34:02 +02:00
|
|
|
@objects = objects
|
2015-07-14 12:07:28 +02:00
|
|
|
puts "Objects #{objects.length}"
|
2015-07-13 17:49:46 +02:00
|
|
|
# create a mapping from id to volt models
|
|
|
|
@view_objects = {}
|
2015-07-09 15:34:02 +02:00
|
|
|
|
2015-07-13 17:49:46 +02:00
|
|
|
@objects.each do |i , o|
|
2015-07-14 15:57:44 +02:00
|
|
|
ob = o.object
|
2015-07-14 18:59:21 +02:00
|
|
|
next unless ob
|
2015-07-14 15:57:44 +02:00
|
|
|
next if basic?(ob)
|
2015-07-14 18:59:21 +02:00
|
|
|
next if ob.class.name.include? "Binary"
|
|
|
|
next if ob.class.name.include? "Array"
|
|
|
|
puts "object #{ob.class.name}"
|
2015-07-14 15:57:44 +02:00
|
|
|
|
|
|
|
view = ObjectView.new ob
|
2015-07-13 17:49:46 +02:00
|
|
|
@view_objects[i] = view
|
2015-07-13 22:11:00 +02:00
|
|
|
add_child view.text
|
2015-07-13 17:49:46 +02:00
|
|
|
end
|
2015-07-13 22:11:00 +02:00
|
|
|
fill_attributes
|
2015-07-09 15:34:02 +02:00
|
|
|
end
|
|
|
|
|
2015-07-13 17:49:46 +02:00
|
|
|
# should almost be called draw by now
|
2015-07-13 22:11:00 +02:00
|
|
|
def draw_me
|
2015-07-13 17:49:46 +02:00
|
|
|
update_positions
|
2015-07-09 15:34:02 +02:00
|
|
|
self.clear
|
2015-07-13 17:49:46 +02:00
|
|
|
@view_objects.each do |i , view|
|
2015-07-14 12:07:28 +02:00
|
|
|
self.lineStyle(4, 0xffd900, 2)
|
|
|
|
puts "v" if view.nil?
|
|
|
|
view.attributes.each do |n , v |
|
|
|
|
next if n == :id
|
|
|
|
next unless v.is_a? ObjectView
|
|
|
|
next unless v.is_parfait
|
|
|
|
puts "v2" if view.nil?
|
|
|
|
puts "0" if v.nil?
|
|
|
|
self.moveTo( view.position.x , view.position.y )
|
2015-07-14 15:57:44 +02:00
|
|
|
# self.lineTo( v.position.x , v.position.y )
|
2015-07-13 17:49:46 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-14 18:59:21 +02:00
|
|
|
def force from , to , offset = 0
|
2015-07-14 15:57:44 +02:00
|
|
|
dir_x = from.x - to.x
|
2015-07-14 18:59:21 +02:00
|
|
|
dir_x2 = (dir_x - offset ) * (dir_x - offset )
|
2015-07-14 15:57:44 +02:00
|
|
|
dir_y = from.y - to.y
|
2015-07-14 18:59:21 +02:00
|
|
|
dir_y2 =( dir_y - offset) * (dir_y - offset)
|
2015-07-13 22:11:00 +02:00
|
|
|
if( dir_x2 < 0.1 and dir_y2 < 0.1 )
|
|
|
|
puts "Were close"
|
2015-07-14 18:59:21 +02:00
|
|
|
dir_x = rand(3)
|
|
|
|
dir_y = rand(3)
|
2015-07-13 22:11:00 +02:00
|
|
|
end
|
|
|
|
f = dir_x * dir_x + dir_y * dir_y
|
2015-07-14 18:59:21 +02:00
|
|
|
f = f / 200
|
2015-07-14 12:07:28 +02:00
|
|
|
f = 0.01 if f < 0.01
|
|
|
|
#puts "force #{f}"
|
2015-07-13 22:11:00 +02:00
|
|
|
PIXI::Point.new( dir_x / f , dir_y / f)
|
|
|
|
end
|
|
|
|
|
2015-07-13 17:49:46 +02:00
|
|
|
def update_positions
|
|
|
|
@view_objects.each do |i , view|
|
2015-07-14 12:07:28 +02:00
|
|
|
view.attributes.each do |n , v |
|
2015-07-13 22:11:00 +02:00
|
|
|
next if n == :id
|
2015-07-14 12:07:28 +02:00
|
|
|
next unless v.is_a? ObjectView
|
|
|
|
next unless v.is_parfait
|
2015-07-14 18:59:21 +02:00
|
|
|
view.position.add_and_bounce force( view.position , v.position , 20 ).scale_by(-1.5) , @max
|
|
|
|
end
|
|
|
|
@view_objects.each do |ii , vv |
|
|
|
|
next if i == ii
|
|
|
|
view.position.add_and_bounce force( view.position , vv.position ) , @max
|
2015-07-13 22:11:00 +02:00
|
|
|
end
|
2015-07-14 18:59:21 +02:00
|
|
|
offset = 100
|
|
|
|
view.position.add_and_bounce force( view.position , view.position.at_x(-offset) ) , @max
|
|
|
|
view.position.add_and_bounce force( view.position , view.position.at_y(-offset) ) , @max
|
|
|
|
view.position.add_and_bounce force( view.position , view.position.at_y(@max.y + offset) ) , @max
|
|
|
|
view.position.add_and_bounce force( view.position , view.position.at_x(@max.x + offset) ) , @max
|
2015-07-13 17:49:46 +02:00
|
|
|
end
|
2015-07-09 15:34:02 +02:00
|
|
|
end
|
2015-07-13 18:08:35 +02:00
|
|
|
|
|
|
|
def fill_attributes
|
|
|
|
@view_objects.each do |i , view|
|
2015-07-14 12:07:28 +02:00
|
|
|
ob = view.object
|
2015-07-13 22:11:00 +02:00
|
|
|
next if is_value?(ob)
|
|
|
|
case ob.class.name
|
2015-07-13 18:08:35 +02:00
|
|
|
when "Array" , "Parfait::List"
|
|
|
|
fill_array view
|
|
|
|
when "Hash" , "Parfait::Dictionary"
|
|
|
|
fill_hash view
|
|
|
|
else
|
2015-07-14 15:57:44 +02:00
|
|
|
# next if basic?(ob)
|
2015-07-13 22:11:00 +02:00
|
|
|
|
|
|
|
attributes = attributes_for(ob)
|
2015-07-13 18:08:35 +02:00
|
|
|
attributes.each do |a|
|
2015-07-13 22:11:00 +02:00
|
|
|
next if a == "html_safe"
|
|
|
|
next if a == "constructor"
|
|
|
|
next if a == "toString"
|
|
|
|
next if a == "position"
|
|
|
|
val = get_value( ob , a)
|
|
|
|
if( @view_objects[val.object_id])
|
2015-07-14 12:07:28 +02:00
|
|
|
val = @view_objects[val.object_id]
|
2015-07-13 18:08:35 +02:00
|
|
|
end
|
2015-07-13 22:11:00 +02:00
|
|
|
#puts "set #{a}"
|
2015-07-13 18:08:35 +02:00
|
|
|
view.set(a , val )
|
|
|
|
end
|
2015-07-13 22:11:00 +02:00
|
|
|
superclasses = [ob.class.superclass.name]
|
2015-07-13 18:08:35 +02:00
|
|
|
if superclasses.include?( "Array") or superclasses.include?( "Parfait::List")
|
|
|
|
fill_array view
|
|
|
|
end
|
|
|
|
if superclasses.include?( "Hash") or superclasses.include?( "Parfait::Dictionary")
|
|
|
|
fill_hash view
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-14 15:57:44 +02:00
|
|
|
def basic? ob
|
|
|
|
return true if ob.class.name.include?("::") and !ob.class.name.include?("Parfait")
|
|
|
|
return true if ob.class.name == "Proc"
|
|
|
|
return true if ob.class.name == "String"
|
|
|
|
return true if ob.class.name == "Numeric"
|
|
|
|
return true if ob.class.name == "Class"
|
|
|
|
false
|
|
|
|
end
|
2015-07-13 18:08:35 +02:00
|
|
|
# and hash keys/values
|
2015-07-14 15:57:44 +02:00
|
|
|
def fill_hash view_hash
|
|
|
|
view_hash.object.each do |k , val|
|
|
|
|
if( @view_objects[val.object_id])
|
|
|
|
val = @view_objects[val.object_id]
|
|
|
|
end
|
|
|
|
view_hash.set(k , val )
|
2015-07-13 18:08:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# and array values
|
2015-07-14 15:57:44 +02:00
|
|
|
def fill_array view_array
|
|
|
|
index = 0
|
|
|
|
view_array.object.each do |val|
|
|
|
|
if( @view_objects[val.object_id])
|
|
|
|
val = @view_objects[val.object_id]
|
|
|
|
end
|
|
|
|
view_array.set("#{index}" , val )
|
|
|
|
index += 1
|
2015-07-13 18:08:35 +02:00
|
|
|
end
|
2015-07-14 15:57:44 +02:00
|
|
|
#puts "set #{a}"
|
2015-07-13 18:08:35 +02:00
|
|
|
end
|
|
|
|
|
2015-07-09 15:34:02 +02:00
|
|
|
end
|