Class: Dcr::Program

Inherits:
Object
  • Object
show all
Includes:
Glimmer::DataBinding::ObservableModel
Defined in:
app/models/dcr/program.rb

Overview

A DCR program that takes text (representing commands) and board width/height for drawing polygons It has a current location (x/y) and angle. Angle is clockwise, with 0 being upward (north).

Constant Summary collapse

STICK_FIGURE_SIZE =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: '', canvas_width: 800, canvas_height: 600) ⇒ Program

Returns a new instance of Program.



38
39
40
41
42
43
44
# File 'app/models/dcr/program.rb', line 38

def initialize(text: '', canvas_width: 800, canvas_height: 600)
  @commands = []
  @canvas_width = canvas_width
  @canvas_height = canvas_height
  reset!
  self.text = text
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def angle
  @angle
end

#canvas_heightObject

Returns the value of attribute canvas_height.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def canvas_height
  @canvas_height
end

#canvas_widthObject

Returns the value of attribute canvas_width.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def canvas_width
  @canvas_width
end

#commandsObject

Returns the value of attribute commands.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def commands
  @commands
end

#expanded_commandsObject

Returns the value of attribute expanded_commands.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def expanded_commands
  @expanded_commands
end

#location_xObject

Returns the value of attribute location_x.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def location_x
  @location_x
end

#location_yObject

Returns the value of attribute location_y.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def location_y
  @location_y
end

#polygonsObject

array of polygon objects including array of point arrays and color to be drawn/filled in GUI



36
37
38
# File 'app/models/dcr/program.rb', line 36

def polygons
  @polygons
end

#textObject

Returns the value of attribute text.



33
34
35
# File 'app/models/dcr/program.rb', line 33

def text
  @text
end

Instance Method Details

#new_polygon!Object



94
95
96
# File 'app/models/dcr/program.rb', line 94

def new_polygon!
  @polygons << Polygon.new(location_x, location_y)
end

#reset!Object



71
72
73
74
75
76
# File 'app/models/dcr/program.rb', line 71

def reset!
  reset_location!
  reset_angle!
  reset_next_color_index!
  reset_polygons!
end

#reset_angle!Object

Resets angle (0 means upward / north). Angle value is clockwise.



85
86
87
# File 'app/models/dcr/program.rb', line 85

def reset_angle!
  self.angle = 0 # means pointing upward (north)
end

#reset_location!Object



78
79
80
81
82
# File 'app/models/dcr/program.rb', line 78

def reset_location!
  # also set stick_figure_location_x and stick_figure_location_y, which is slightly different
  self.location_x = (canvas_width - STICK_FIGURE_SIZE) / 2.0
  self.location_y = (canvas_height - STICK_FIGURE_SIZE) / 2.0
end

#reset_next_color_index!Object



98
99
100
# File 'app/models/dcr/program.rb', line 98

def reset_next_color_index!
  Command::Color.reset_next_color_index!
end

#reset_polygons!Object



89
90
91
92
# File 'app/models/dcr/program.rb', line 89

def reset_polygons!
  # reset quietly via instance variable without alerting observers with attribute writer method
  @polygons = [Polygon.new(location_x, location_y)]
end