Something went wrong. Try again.
Something went wrong. Try again.
ners-roller tui.rb
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120# frozen_string_literal: true
require 'tty-box'require 'tty-cursor'require 'tty-table'
# Class for rendering the Character class to a Text UIclass Tui attr_reader :attrs, :skills, :cursor
def initialize(char) @width = TTY::Screen.width @character = char @basics = TTY::Table.new(@character.basics_table) @attrs = TTY::Table.new(@character.attr_names, [@character.attr_values]) @skills = TTY::Table.new(@character.skills_table) @ratings = TTY::Table.new(@character.ratings_table) @defences = TTY::Table.new(@character.defences_table) @cursor = TTY::Cursor clear end
def name width = [@character.basics_table.first.length, @character.attr_names.length].max * 15 + 1 @name ||= TTY::Box.frame(width: width, align: :center, style: { border: { fg: :red } }) do @character.name end end
def basics_table col_count = @character.basics_table.first.length @basics_table ||= @basics.render(:unicode, alignments: [:center] * col_count, column_widths: [14] * col_count) do |r| r.border.style = :blue r.border.separator = [0] end end
def attribute_table col_count = @character.attr_names.length @attrs.render(:unicode, alignments: [:center] * col_count, column_widths: [14] * col_count) do |r| r.border.style = :red end end
def skills_table @skills_table ||= TTY::Box.frame(top: 13, left: 0, title: { top_left: 'SKILLS' }, style: { border: { fg: :blue } }) do @skills.render(:unicode, alignments: %i[left center], column_widths: [16, 8]) do |r| r.border.separator = [4, 8] end end end
def ratings_table @ratings_table ||= TTY::Box.frame(top: 13, left: 32, title: { top_left: 'RATINGS' }, style: { border: { fg: :blue } }) do @ratings.render(:unicode, alignments: %i[left center], column_widths: [16, 8]) end end
def defences_table return if @character.defences_table.empty?
@defences_table ||= TTY::Box.frame(top: 24, left: 32, title: { top_left: 'DEFENCES' }, style: { border: { fg: :blue } }) do @defences.render(:unicode, alignment: %i[left center], column_widths: [16, 8]) end end
def format_test(test) puts "#{formatter.underline(test.check)}:" print " Roll: #{test.rolls.join(' + ')} = #{decorated_roll_total(test)}; " puts "Bonus: #{decorated_result(test)}" puts " Final Result: #{formatter.bold(test.rating + test.result.to_i)}" if test.roll_total > 1 puts '' end
def reset print @cursor.move_to(0, 0) print name puts basics_table print attribute_table print skills_table print @cursor.column(0) print @cursor.down(2) print @cursor.save print ratings_table print defences_table print @cursor.restore print @cursor.clear_screen_down end
def clear print @cursor.move_to(0, 0) print @cursor.clear_screen_down end
private
def formatter @formatter ||= Pastel.new end
def decorated_roll_total(test) if test.roll_total >= 60 formatter.bold.on_red(" #{test.roll_total} ") else formatter.white(test.roll_total) end end
def decorated_result(test) colour = if test.roll_total == 1 :red elsif test.roll_total <= 10 :yellow else :blue end formatter.bold.send(colour, test.result) endend