class Text2048::CursesView::Tile

Shows tiles in curses. rubocop:disable ClassLength

Constants

DEFAULT_HEIGHT
DEFAULT_WIDTH

Attributes

color[R]
height[R]
width[R]

Public Class Methods

height(scale) click to toggle source
# File lib/text2048/curses_view/tile.rb, line 26
def self.height(scale)
  (DEFAULT_HEIGHT * scale).to_i
end
new(tile, row, col, color, scale = 1) click to toggle source
# File lib/text2048/curses_view/tile.rb, line 30
def initialize(tile, row, col, color, scale = 1)
  klass = self.class
  @value = tile.value
  @height = klass.height(scale)
  @width = klass.width(scale)
  @row = (@height + 1) * row + 2
  @col = (@width + 1) * col + 1
  @color = color
  @scale = scale
end
width(scale) click to toggle source
# File lib/text2048/curses_view/tile.rb, line 22
def self.width(scale)
  @width = (DEFAULT_WIDTH * scale).to_i
end

Public Instance Methods

draw_box() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 54
def draw_box
  draw_square
  [box_upper_left, box_upper_right,
   box_lower_left, box_lower_right].each do |row, col|
    setpos(row, col)
    addstr('+')
  end
end
draw_number() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 72
def draw_number
  return unless @value
  if @scale >= 1
    draw_lcd_number
  else
    setpos(@row + @height / 2, @col)
    colorize(@color) do
      addstr @value.to_s.center(@width)
    end
  end
end
fill_black() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 67
def fill_black
  colorize(COLOR_BLACK + 100) { fill }
  refresh
end
fill_tile_color() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 63
def fill_tile_color
  colorize(@color + 100) { fill }
end
pop() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 48
def pop
  colorize(@color + 100) do
    draw_box
  end
end
show() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 41
def show
  draw_box
  fill_tile_color
  draw_number
  self
end

Private Instance Methods

box_height() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 124
def box_height
  @height + 2
end
box_lower_left() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 116
def box_lower_left
  [@row + @height, @col - 1]
end
box_lower_right() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 120
def box_lower_right
  [@row + @height, @col + @width]
end
box_upper_left() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 108
def box_upper_left
  [@row - 1, @col - 1]
end
box_upper_right() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 112
def box_upper_right
  [@row - 1, @col + @width]
end
box_width() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 128
def box_width
  @width + 2
end
col_padded() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 93
def col_padded
  num_length = @value.to_i.to_s.length
  @col + (@width - num_length * 4 + 1) / 2
end
draw_horizonal_line(row, col, length) click to toggle source
# File lib/text2048/curses_view/tile.rb, line 139
def draw_horizonal_line(row, col, length)
  setpos(row, col)
  addstr('-' * length)
end
draw_lcd_line(line, row) click to toggle source

@todo This method smells of :reek:NestedIterators @todo This method smells of :reek:TooManyStatements

# File lib/text2048/curses_view/tile.rb, line 100
def draw_lcd_line(line, row)
  line.split(//).each_with_index do |each, index|
    setpos(row, col_padded + index)
    color = each == '*' ? COLOR_BLACK + 100 : @color
    colorize(color) { addstr each }
  end
end
draw_lcd_number() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 86
def draw_lcd_number
  num_lcd = LCD.new(@value).render
  num_lcd.split("\n").each_with_index do |each, index|
    draw_lcd_line(each, @row + index + (@height - 5) / 2)
  end
end
draw_square() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 132
def draw_square
  draw_horizonal_line(*box_upper_left, box_width)
  draw_vertical_line(*box_upper_left, box_height)
  draw_vertical_line(*box_upper_right, box_height)
  draw_horizonal_line(*box_lower_left, box_width)
end
draw_vertical_line(row, col, length) click to toggle source
# File lib/text2048/curses_view/tile.rb, line 144
def draw_vertical_line(row, col, length)
  (0..(length - 1)).each do |each|
    setpos(row + each, col)
    addstr('|')
  end
end
fill() click to toggle source
# File lib/text2048/curses_view/tile.rb, line 151
def fill
  (0..(@height - 1)).each do |each|
    setpos(@row + each, @col)
    if @value && each == @height / 2
      addstr @value.to_s.center(@width)
    else
      addstr('.' * @width)
    end
  end
end