Class: RubyLabs::Canvas::TkObject

Inherits:
Object
  • Object
show all
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)

Direct Known Subclasses

Circle, Font, Line, Polygon, Rectangle, Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#coordsObject

Returns the value of attribute coords.



756
757
758
# File 'lib/rubylabs.rb', line 756

def coords
  @coords
end

#nameObject

Returns the value of attribute name.



756
757
758
# File 'lib/rubylabs.rb', line 756

def name
  @name
end

#penpointObject

Returns the value of attribute penpoint.



756
757
758
# File 'lib/rubylabs.rb', line 756

def penpoint
  @penpoint
end

Class Method Details

.nextIdObject

Return a unique ID number for a new proxy object.

:call-seq:

TkObject.nextId()


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.options(h) => String


791
792
793
794
795
796
797
# File 'lib/rubylabs.rb', line 791

def TkObject.options(h)
  a = []
  h.each do |k,v| 
    a << "-#{k} #{v}"
  end
  return a.join(" ")
end

.reset(p) ⇒ Object

Initialization: set the object ID to 0 and save the file descriptor for the connection to the wish shell.

:call-seq:

TkObject.reset(p)


765
766
767
768
# File 'lib/rubylabs.rb', line 765

def TkObject.reset(p)
  @@pipe = p
  @@id = 0
end

Instance Method Details

#eraseObject

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

#lowerObject

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

#raiseObject

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