Class: RubyLabs::Canvas::TkObject
- Inherits:
-
Object
- Object
- RubyLabs::Canvas::TkObject
- Defined in:
- lib/rubylabs.rb
Overview
TkObject
Base class of all objects defined for RubyLabs::Canvas. Objects derived from this class are proxies for Tk objects in a wish shell, opened when the canvas is initialized.
Applications should not try to instantiate a TkObject directly, but instead should call a constructor of one of the derived classes. For example, to draw a circle on the canvas:
c = Canvas::Circle.new(x, y, r)
Public instance methods defined here are inherited by all objects derived from TkObject, and can be used to manipulate the object. For example, to change the color of the circle:
c.fill = 'green'
The current implementation is very sparse: the only operations defined are the ones needs for the visualizations described in the textbook, which are just the basic methods that create an object or move it around on the screen.
In addition to the usual attributes, these objects have a “pen point”, which is the location of an imaginary pen relative to the object’s base coordinate. The pen point is used by methods that draw a track as an object is moved (e.g. see the methods that control the motion of the robot in SphereLab)
Instance Attribute Summary collapse
-
#coords ⇒ Object
Returns the value of attribute coords.
-
#name ⇒ Object
Returns the value of attribute name.
-
#penpoint ⇒ Object
Returns the value of attribute penpoint.
Class Method Summary collapse
-
.nextId ⇒ Object
Return a unique ID number for a new proxy object.
-
.options(h) ⇒ Object
Translate a Ruby hash
h
into a Tk option string. -
.reset(p) ⇒ Object
Initialization: set the object ID to 0 and save the file descriptor for the connection to the wish shell.
Instance Method Summary collapse
-
#erase ⇒ Object
Remove an object from the canvas.
-
#fill=(x) ⇒ Object
Set the fill color for an object.
-
#lower ⇒ Object
Put an object in the background (below all other objects on the canvas).
-
#raise ⇒ Object
Bring an object to the foreground (on top of all other objects on the canvas).
Instance Attribute Details
#coords ⇒ Object
Returns the value of attribute coords.
756 757 758 |
# File 'lib/rubylabs.rb', line 756 def coords @coords end |
#name ⇒ Object
Returns the value of attribute name.
756 757 758 |
# File 'lib/rubylabs.rb', line 756 def name @name end |
#penpoint ⇒ Object
Returns the value of attribute penpoint.
756 757 758 |
# File 'lib/rubylabs.rb', line 756 def penpoint @penpoint end |
Class Method Details
.nextId ⇒ Object
776 777 778 779 |
# File 'lib/rubylabs.rb', line 776 def TkObject.nextId @@id += 1 return "obj" + @@id.to_s end |
.options(h) ⇒ Object
Translate a Ruby hash h
into a Tk option string. Example:
>> Canvas::TkObject.options( { :outline => :black, :fill => :red } )
=> "-fill red -outline black"
Note the ordering of the options in the Tk string may not be the same as the order they are listed when the hash object is created.
:call-seq:
TkObject.(h) => String
791 792 793 794 795 796 797 |
# File 'lib/rubylabs.rb', line 791 def TkObject.(h) a = [] h.each do |k,v| a << "-#{k} #{v}" end return a.join(" ") end |
Instance Method Details
#erase ⇒ Object
Remove an object from the canvas.
:call-seq:
x.erase()
825 826 827 |
# File 'lib/rubylabs.rb', line 825 def erase @@pipe.puts ".canvas delete $#{@name}" end |
#fill=(x) ⇒ Object
Set the fill color for an object. Example:
>> x.fill = "green"
=> "green"
:call-seq:
x.fill = String
857 858 859 |
# File 'lib/rubylabs.rb', line 857 def fill=(x) @@pipe.puts ".canvas itemconfigure $#{name} -fill #{x}" end |
#lower ⇒ Object
Put an object in the background (below all other objects on the canvas).
:call-seq:
x.lower()
815 816 817 |
# File 'lib/rubylabs.rb', line 815 def lower @@pipe.puts ".canvas lower $#{@name}" end |
#raise ⇒ Object
Bring an object to the foreground (on top of all other objects on the canvas).
:call-seq:
x.raise()
805 806 807 |
# File 'lib/rubylabs.rb', line 805 def raise @@pipe.puts ".canvas raise $#{@name}" end |