Class: AdventureRL::Textbox

Inherits:
Rectangle show all
Defined in:
lib/AdventureRL/Textbox.rb

Overview

This is similar to a Rectangle, but it can display text with basic formatting.

Direct Known Subclasses

Button

Constant Summary collapse

DEFAULT_SETTINGS =
Settings.new(
  text:       '',
  font_size:  24,
  font_name:  'MonoSpace',
  font_color: 0xff_ffffff,
  text_alignment: {
    x: :center,
    y: :center
  },
  border_padding: {
    x: 16,
    y: 8
  },
  border_color: 0xff_000000,
  border_size: {
    width:  0,
    height: 0
  },
  background_color: 0xff_000000,
  z_index: 0,
  position: {
    x: 0,
    y: 0
  },
  size: {
    width:  256,
    height: 64
  },
  origin: {
    x: :left,
    y: :top
  }
)

Constants included from Helpers::Error

Helpers::Error::PADDING, Helpers::Error::STACK_TRACE_PADDING, Helpers::Error::STACK_TRACE_SIZE

Constants inherited from Mask

Mask::MASKS

Constants inherited from Point

Point::POINTS

Instance Method Summary collapse

Methods inherited from Rectangle

#get_color, #reset_color, #set_color, #set_temporary_color

Methods included from Helpers::Error

directory_exists?, error, error_no_directory, error_no_file, file_exists?

Methods inherited from Mask

#assign_to, #assigned_to?, #collides_with?, #collides_with_hash?, #collides_with_mask?, #collides_with_point?, #get_assigned, #get_center, #get_corner, #get_layer, #get_mask, #get_origin, #get_real_center, #get_real_corner, #get_real_side, #get_real_sides, #get_side, #get_sides, #get_size, #has_layer?, #has_mask?, #set_layer, #set_size

Methods inherited from Point

#assign_to, #assigned_to?, #collides_with?, #collides_with_hash?, #collides_with_mask?, #collides_with_point?, #get_assigned, #get_layer, #get_point, #get_position, #get_real_point, #get_real_position, #has_layer?, #has_point?, #keys, #move_by, #set_layer, #set_position, #values, #x, #y

Constructor Details

#initialize(settings = {}) ⇒ Textbox

Returns a new instance of Textbox.



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

def initialize settings = {}
  @settings = DEFAULT_SETTINGS.merge settings
  @font_cache = {}  # This Hash will be filled with any loaded Gosu::Font (see #set_font_size)
  set_font_size @settings.get(:font_size)
  @text           = @settings.get :text
  @font_color     = @settings.get :font_color
  @text_alignment = @settings.get :text_alignment
  @border_padding = @settings.get :border_padding
  @border_color   = @settings.get :border_color
  @border_size    = @settings.get :border_size
  super @settings
  @color_original = @settings.get :background_color
end

Instance Method Details

#drawObject



92
93
94
95
96
97
# File 'lib/AdventureRL/Textbox.rb', line 92

def draw
  draw_border
  draw_background
  draw_text
  @color_temporary = nil
end

#get_textObject



53
54
55
# File 'lib/AdventureRL/Textbox.rb', line 53

def get_text
  return @text
end

#preload_font_sizes(*sizes) ⇒ Object

TODO: This doesn’t really work, it still takes a while to draw a Font the first time; look into this. Pass any amount of integers, which will each preload a new Gosu::Font, with the size of the integer.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/AdventureRL/Textbox.rb', line 79

def preload_font_sizes *sizes
  sizes.flatten.each do |size|
    Helpers::Error.error(
      "Expected size to be an Integer, but got",
      "`#{size.inspect}:#{size.class.name}'."
    )  unless (size.is_a? Integer)
    @font_cache[size] ||= Gosu::Font.new(
      size,
      name: @settings.get(:font_name)
    )
  end
end

#set_font_size(size) ⇒ Object

NOTE: This method is expensive, because it loads a new Gosu::Font. Call this sparingly. Once a new Gosu::Font is created, it is cached, wo when you resize to a previously used font it will not need to load a new Gosu::Font.



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

def set_font_size size
  @font = @font_cache[size]
  return  if (@font)
  @font = Gosu::Font.new(
    size,
    name: @settings.get(:font_name)
  )
  @font_cache[size] = @font
end

#set_text(text) ⇒ Object



57
58
59
# File 'lib/AdventureRL/Textbox.rb', line 57

def set_text text
  @text = text.to_s
end