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.
763 764 765 |
# File 'lib/rubylabs.rb', line 763 def coords @coords end |
#name ⇒ Object
Returns the value of attribute name.
763 764 765 |
# File 'lib/rubylabs.rb', line 763 def name @name end |
#penpoint ⇒ Object
Returns the value of attribute penpoint.
763 764 765 |
# File 'lib/rubylabs.rb', line 763 def penpoint @penpoint end |
Class Method Details
.nextId ⇒ Object
783 784 785 786 |
# File 'lib/rubylabs.rb', line 783 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
798 799 800 801 802 803 804 |
# File 'lib/rubylabs.rb', line 798 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()
832 833 834 |
# File 'lib/rubylabs.rb', line 832 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
864 865 866 |
# File 'lib/rubylabs.rb', line 864 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()
822 823 824 |
# File 'lib/rubylabs.rb', line 822 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()
812 813 814 |
# File 'lib/rubylabs.rb', line 812 def raise @@pipe.puts ".canvas raise $#{@name}" end |