Class: OR2D::Console

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

Overview

The Console class is used to construct Console objects that allow execution of Ruby code at runtime.

Since:

  • 2023-04-26

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Console

Constructs a new Console object.

Parameters:

  • options (Hash) (defaults to: {})

    the options to create the console with

Since:

  • 2023-04-26



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/or2d/console.rb', line 15

def initialize(options = {})
  @boundary = Ruby2D::Rectangle.new(x: 0, y: -(OR2D.game.window.get(:height) / 2), z: 0,
                                    width: OR2D.game.window.get(:width),
                                    height: (OR2D.game.window.get(:height) / 2),
                                    color: 'green', opacity: 0.25)
  OR2D.game.window.add(@boundary)
  @properties = { input_size: 8.scale, output_size: 4.scale, max_length: options[:max_length] || 24 }
  @properties[:max_lines] = (@boundary.height / @properties[:output_size].scale).to_i - @properties[:output_size]

  @prompt = options[:prompt] || '>> '
  @buffer = String.new
  @display = OR2D::Composites::Text.new
  @display.add_line(text: 'OR2D Console', color: 'yellow', size: @properties[:output_size],
                    x: @boundary.x + 16, y: @boundary.y + 16.scale, z: @boundary.z + 1)
  @input = OR2D::Entity.new(:text, { text: @prompt, color: 'white',
                                     x: @boundary.x + 16, y: (@boundary.y + @boundary.height) - 16.scale,
                                     z: @boundary.z + 1, size: @properties[:input_size],
                                     show: true })
  @state = State.new(:RETRACTED, false, false)

  setup_events
end

Instance Attribute Details

#boundaryRuby2D::Rectangle (readonly)

Returns the boundary of the Console object.

Returns:

  • (Ruby2D::Rectangle)

    the boundary of the Console object

Since:

  • 2023-04-26



11
12
13
# File 'lib/or2d/console.rb', line 11

def boundary
  @boundary
end

#displayOR2D::Composites::Text (readonly)

Returns the display of the Console object.

Returns:

Since:

  • 2023-04-26



7
8
9
# File 'lib/or2d/console.rb', line 7

def display
  @display
end

Instance Method Details

#add_line(options) ⇒ Object

Add a line of text to the console.

Since:

  • 2023-04-26



39
40
41
# File 'lib/or2d/console.rb', line 39

def add_line(options)
  @display.add_line(options.merge(size: @properties[:output_size]))
end

#pull_downObject

Pulls the console into view.

Since:

  • 2023-04-26



86
87
88
# File 'lib/or2d/console.rb', line 86

def pull_down
  @state.animation = :PULL_DOWN
end

#removeObject

Remove the console text displays from the game instance window.

Since:

  • 2023-04-26



44
45
46
47
48
49
# File 'lib/or2d/console.rb', line 44

def remove
  @input.destroy
  @display.destroy
  OR2D.game.window.remove(@boundary)
  @descriptors.each_value { |descriptor| OR2D.game.window.off(descriptor) }
end

#retractObject

Retracts the console from view.

Since:

  • 2023-04-26



81
82
83
# File 'lib/or2d/console.rb', line 81

def retract
  @state.animation = :RETRACTED
end

#updateObject

Update the console.

Since:

  • 2023-04-26



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/or2d/console.rb', line 52

def update
  case @state.animation
  when :RETRACTED
    if @boundary.y >= -@boundary.height
      @boundary.y -= 16
      @input.y -= 16
      @display.y -= 16 unless @display.empty?
    else
      @state.animation = :DRAWN
      @state.drawn = false
    end
  when :PULL_DOWN
    if @boundary.y <= -16.scale
      @boundary.y += 16
      @input.y += 16
      @display.y += 16 unless @display.empty?
    else
      @state.animation = :DRAWN
      @state.drawn = true
    end
  end

  @display.update
  @display.remove_layer(@display.layers.keys.first) if @display.layers.length > @properties[:max_lines]
  @input.resource.text = @prompt + @buffer if @state.drawn
  OR2D.game.window.add(@boundary)
end