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.



763
764
765
# File 'lib/rubylabs.rb', line 763

def coords
  @coords
end

#nameObject

Returns the value of attribute name.



763
764
765
# File 'lib/rubylabs.rb', line 763

def name
  @name
end

#penpointObject

Returns the value of attribute penpoint.



763
764
765
# File 'lib/rubylabs.rb', line 763

def penpoint
  @penpoint
end

Class Method Details

.nextIdObject

Return a unique ID number for a new proxy object.

:call-seq:

TkObject.nextId()


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


798
799
800
801
802
803
804
# File 'lib/rubylabs.rb', line 798

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)


772
773
774
775
# File 'lib/rubylabs.rb', line 772

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

Instance Method Details

#eraseObject

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

#lowerObject

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

#raiseObject

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