I am making a 2d shape editor with ruby2d. It's very simple, but there are some things it needs to support. Now I'm stuck with creating a border(I made the same object slightly bigger than the main and put it behind). The main issue is when I change the shape of the main object, its border acts like its shadow and changes proportionally.
This is what it looks like in the default state:
And this is what it looks like in the changed state:
def check_point(event)
$array_of_figures.each do |element|
element.hash_of_cp.each{|name,cp| @element=element,cp if cp.contains?event.x,event.y}
end
end
update do
on :mouse_down do |event|
check_point(event)
on :mouse_move do |smth|
@element[0].change(smth,@element[1]) if @element!=0
end
on :mouse_up do|this|
if @element!=0
@element[0].change(this,@element[1])
@element=0
end
end
end
end
show
class N3 < Figure
def initialize
super
@premitive=Triangle.new(
x1: 60, y1: 10,
x2: 110, y2: 100,
x3: 10, y3: 100,
color: 'yellow',
z: 100
)
@arc=Triangle.new(
x1: @premitive.x1, y1: @premitive.y1.to_i-2,
x2: @premitive.x2.to_i+2, y2: @premitive.y2.to_i+2,
x3: @premitive.x3.to_i-2, y3: @premitive.y3.to_i+2,
color: 'white',
z: 99
)
@hash_of_cp[:x1_cp]=Circle.new(
x: @premitive.x1, y: @premitive.y1,
radius: 3,
sectors: 32,
color: 'lime',
z: 101
)
@hash_of_cp[:x2_cp]=Circle.new(
x: @premitive.x2, y: @premitive.y2,
radius: 3,
sectors: 32,
color: 'lime',
z: 101
)
@hash_of_cp[:x3_cp]=Circle.new(
x: @premitive.x3, y: @premitive.y3,
radius: 3,
sectors: 32,
color: 'lime',
z: 101
)
@center=Circle.new(
x: (@premitive.x1+@premitive.x2+@premitive.x3)/3 , y: (@premitive.y1+@premitive.y2+@premitive.y3)/3,
radius: 3,
sectors: 32,
color: 'lime',
z: 101
)
end
def change(smth,cp)
if @premitive.x1==cp.x && @premitive.y1==cp.y
@premitive.x1=smth.x
@premitive.y1=smth.y
@arc.x1=smth.x
@arc.y1=smth.y
elsif @premitive.x2==cp.x && @premitive.y2==cp.y
@premitive.x2=smth.x
@premitive.y2=smth.y
@arc.x2=@premitive.x2
@arc.y2=@premitive.y2
elsif @premitive.x3==cp.x && @premitive.y3==cp.y
@premitive.x3=smth.x
@premitive.y3=smth.y
@arc.x3=@premitive.x3
@arc.y3=@premitive.y3
end
@center.x=(@premitive.x1+@premitive.x2+@premitive.x3)/3
@center.y=(@premitive.y1+@premitive.y2+@premitive.y3)/3
cp.x =smth.x
cp.y =smth.y
end
end