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
37
# 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 * OR2D.scale), output_size: (4 * OR2D.scale), max_length: options[:max_length] || 24 }
  @properties[:max_lines] = (@boundary.height / (@properties[:output_size] * OR2D.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 * OR2D.scale), z: @boundary.z + 1)
  @input = OR2D::Entity.new(:text, { text: @prompt, color: 'white',
                                     x: @boundary.x + 16, y: (@boundary.y + @boundary.height) - (16 * OR2D.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



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

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



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

def pull_down
  @state.animation = :PULL_DOWN
end

#removeObject

Remove the console text displays from the game instance window.

Since:

  • 2023-04-26



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

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



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

def retract
  @state.animation = :RETRACTED
end

#updateObject

Update the console.

Since:

  • 2023-04-26



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
79
# File 'lib/or2d/console.rb', line 53

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 * OR2D.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