Class: PeterPan

Inherits:
Object
  • Object
show all
Defined in:
lib/peter_pan.rb

Constant Summary collapse

VERSION =
"1.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PeterPan

Possible Options:

  • :viewport_width - Viewport width, integer, default 21

  • :viewport_height - Viewport height, integer, default 7

  • :empty_point_character - the char of an empty cell (default ‘ ’)

  • :buffer_width - Buffer width, integer, default 0

  • :buffer_height - Buffer height, integer, default 0

NOTE: The buffer will automatically expand dimensionally to hold any point that is #plot()‘ed or text written with #write().



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/peter_pan.rb', line 26

def initialize(opts={})
  @viewport_width = (opts[:viewport_width] || 21).to_i # x
  @viewport_height = (opts[:viewport_height] || 7).to_i # y
  @font_name = 'transpo' # only font for now
  @empty_point_character = (opts[:empty_point_character] || ' ')
  buffer_changed!(false)
  clear_buffer!(
    :width => (opts[:buffer_width] || 0),
    :height => (opts[:buffer_height] || 0)
  )
end

Instance Attribute Details

#empty_point_characterObject (readonly)

Returns the value of attribute empty_point_character.



12
13
14
# File 'lib/peter_pan.rb', line 12

def empty_point_character
  @empty_point_character
end

#viewport_heightObject (readonly)

Returns the value of attribute viewport_height.



12
13
14
# File 'lib/peter_pan.rb', line 12

def viewport_height
  @viewport_height
end

#viewport_widthObject (readonly)

Returns the value of attribute viewport_width.



12
13
14
# File 'lib/peter_pan.rb', line 12

def viewport_width
  @viewport_width
end

Instance Method Details

#buffer_heightObject

Return an integer of the height of the buffer at it tallest point



77
78
79
# File 'lib/peter_pan.rb', line 77

def buffer_height
  @buffer.size
end

#buffer_widthObject

Return an integer of the width of the buffer at it’s widest point.



66
67
68
69
70
71
72
73
74
# File 'lib/peter_pan.rb', line 66

def buffer_width
  if !@buffer_width || buffer_changed?
    @buffer_width = 0
    @buffer.each do |by|
      @buffer_width = by.size if by.size > @buffer_width
    end
  end
  @buffer_width
end

#clear_buffer!(opts = {}) ⇒ Object

clears everything out of the buffer. By default, sets the buffer dimensions to 0x0. Optionally, you can pass :width and :height args and the buffer dimentions will be set accordingly. By default the buffer will be filled with space character, but you can set the char to be used by passing :clear_with



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/peter_pan.rb', line 160

def clear_buffer!(opts={})
  opts = { :width => 0, :height => 0, :clear_with => @empty_point_character }.merge(opts)
  @buffer = [[]]
  opts[:height].times do |y|
    @buffer[y] = []
    opts[:width].times do |x|
      @buffer[y][x] = opts[:clear_with].to_s.slice(0,1)
    end
  end
  buffer_changed!
end

#fontObject

returns a data structure representing the current font used by #write.



173
174
175
# File 'lib/peter_pan.rb', line 173

def font
  @font ||= YAML.load(File.new("#{Gem.loaded_specs['peter_pan'].full_gem_path}/fonts/#{@font_name}.yml").read)
end

#pan_viewport(x1, y1, x2, y2) ⇒ Object

Move the viewport over the buffer from x1/y1 to x2/y2. Returns an array of strings. Each string is a frame of the pan path of the kind returned by #show_viewport.



102
103
104
105
106
# File 'lib/peter_pan.rb', line 102

def pan_viewport(x1, y1, x2, y2)
  calculate_integral_points(x1, y1, x2, y2).map do |px, py|
    show_viewport(px, py)
  end
end

#path_viewport(*coordinates) ⇒ Object

Like pan_viewport, but multiple pairs of coordinates can be passed. The first pair will be used as the start and the viewport will be panned from coordinate-pair to coordinate-pair. It expects to be passed an array-like list of coordinate pairs. It returns an array of string representing the frames of the pathing.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/peter_pan.rb', line 118

def path_viewport(*coordinates)
  coordinates.flatten!
  start_x = coordinates.shift
  start_y = coordinates.shift
  coordinates.flatten.each_slice(2).map do |x,y|
    pan = pan_viewport(start_x, start_y, x, y)
    start_x = x
    start_y = y
    pan
  end.flatten
end

#plot(x, y, value = '*') ⇒ Object

Draw a point in the virtual buffer. The virtual buffer will be enlarged automatically.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/peter_pan.rb', line 40

def plot(x, y, value='*')
  1.upto(y+1) do |i|
    @buffer[i-1] ||= []
    1.upto(x+1) do |ii|
      @buffer[i-1][ii-1] ||= @empty_point_character
    end
  end

  @buffer[y][x] = value.to_s.slice(0,1)

  buffer_changed!
end

#plot_sprite(sprite, x, y) ⇒ Object

Draw a text sprint to the buffer at given coordinates.

  • sprite is an ARRAY of string representing the image.



137
138
139
140
141
142
143
# File 'lib/peter_pan.rb', line 137

def plot_sprite(sprite, x, y)
  sprite.each_with_index do |line, line_y|
    line.split('').each_with_index do |c, char_x|
      plot(char_x + x, line_y + y, c)
    end
  end
end

#pretty_pan_viewport(*coordinates) ⇒ Object

Same as #path_viewport, but with an ascii-art border around each frame.



109
110
111
# File 'lib/peter_pan.rb', line 109

def pretty_pan_viewport(x1, y1, x2, y2)
  pan_viewport(x1, y1, x2, y2).map{|vp| wrap_frame_with_border(vp) }
end

#pretty_print_bufferObject

Same as #show_buffer but with an ascii-art border around it.



54
55
56
# File 'lib/peter_pan.rb', line 54

def pretty_print_buffer
  wrap_frame_with_border(show_buffer)
end

#pretty_print_viewport(x, y, x2 = @viewport_width, y2 = @viewport_height) ⇒ Object

Same as #show_viewort, but with an ascii-art border around it.



95
96
97
# File 'lib/peter_pan.rb', line 95

def pretty_print_viewport(x,y,x2=@viewport_width,y2=@viewport_height)
  wrap_frame_with_border(show_viewport(x,y,x2,y2))
end

#show_bufferObject

Return the current buffer as a string delimited by “n” characters



59
60
61
62
63
# File 'lib/peter_pan.rb', line 59

def show_buffer
  normalize_buffer_width

  @buffer.map{|bx| "#{bx.join}\n" }.join
end

#show_viewport(x, y, x2 = @viewport_width, y2 = @viewport_height) ⇒ Object

Show a viewport area of the larger buffer. width and height of the viewport can be set in the object initialization for defaults, or manually here. Returns a string delimited by “n” characters.



85
86
87
88
89
90
91
92
# File 'lib/peter_pan.rb', line 85

def show_viewport(x,y,x2=@viewport_width,y2=@viewport_height)
  normalize_buffer_width

  y.upto((y2-1)+y).map do |i|
    buffer_row = @buffer[i] || @viewport_width.times.map{@empty_point_character}
    sprintf("%-#{x2}s", buffer_row[x..((x2-1)+x)].join) + "\n"
  end.join
end

#write(x, y, message) ⇒ Object

Write a string to the buffer at the given coordinates.



146
147
148
149
150
151
152
153
# File 'lib/peter_pan.rb', line 146

def write(x, y, message)
  letter_x = x
  message.split('').each do |c|
    char = font['characters'][c].map{|l|l.gsub('.', @empty_point_character)}
    plot_sprite(char, letter_x, y)
    letter_x = letter_x + font['width'] + 1
  end
end