Class: UI::InputNumber

Inherits:
SpriteStack show all
Defined in:
scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb

Overview

UI element showing input number (for number choice & selling)

Constant Summary collapse

IMAGE_SOURCE =

Image used as source for the UI build

'numin_bg'
IMAGE_COMPOSITION =

List of coordinates used to know how to build the UI from the image

{
  left: [0, 0, 33, 44],
  number: [33, 0, 24, 44],
  separator: [57, 0, 6, 44],
  right: [83, 0, 10, 44],
  money_add: [94, 0, 68, 44]
}
MONEY_PADDING =

Padding for money text

2

Constants inherited from SpriteStack

SpriteStack::NO_INITIAL_IMAGE

Instance Attribute Summary collapse

Attributes inherited from SpriteStack

#animated, #data, #moving, #stack, #viewport, #x, #y

Instance Method Summary collapse

Methods inherited from SpriteStack

#[], #add_background, #add_line, #add_text, #anime, #anime_delta_set, #dispose, #each, #execute_anime, #move, #move_to, #opacity, #opacity=, #push, #push_sprite, #set_origin, #set_position, #simple_mouse_in?, #size, #stop_animation, #translate_mouse_coords, #update_animation, #update_position, #visible, #visible=, #with_cache, #with_font, #with_surface, #z, #z=

Constructor Details

#initialize(viewport, max_digits, default_number = 0, accept_negatives = false) ⇒ InputNumber

Create a new Input number

Parameters:

  • viewport (Viewport)
  • max_digits (Integer)

    maximum number of digit

  • default_number (Integer) (defaults to: 0)

    default number

  • accept_negatives (Boolean) (defaults to: false)

    if we can provide negative values



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb', line 31

def initialize(viewport, max_digits, default_number = 0, accept_negatives = false)
  super(viewport)
  @max_digits = max_digits
  @accept_negatives = accept_negatives
  @digit_index = 0
  @max = (10**@max_digits) - 1
  @min = accept_negatives ? -@max : 0
  # @type [Array<Sprite>]
  @width_accounting_sprites = []
  # @type [Array<Text>]
  @texts = []
  create_sprites
  self.number = default_number
  @default_number = @number
end

Instance Attribute Details

#maxInteger

Maximum value

Returns:



21
22
23
# File 'scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb', line 21

def max
  @max
end

#minInteger

Minimum value

Returns:



18
19
20
# File 'scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb', line 18

def min
  @min
end

#numberInteger

Currently inputed number

Returns:



24
25
26
# File 'scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb', line 24

def number
  @number
end

Instance Method Details

#heightInteger

Get the height of the UI

Returns:



80
81
82
# File 'scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb', line 80

def height
  @stack.first&.height || 0
end

#update

Update the UI element



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb', line 48

def update
  if Input.repeat?(:DOWN)
    self.number -= 10**@digit_index
  elsif Input.repeat?(:UP)
    self.number += 10**@digit_index
  elsif Input.trigger?(:LEFT)
    @digit_index = (@digit_index + 1).clamp(0, @max_digits - 1)
  elsif Input.trigger?(:RIGHT)
    @digit_index = (@digit_index - 1).clamp(0, @max_digits - 1)
  elsif Input.trigger?(:B)
    self.number = @default_number
  else
    return # Prevent useless drawing
  end
  draw_digits
end

#widthInteger

Get the width of the UI

Returns:



74
75
76
# File 'scripts/01450 Systems/00205 Input/00002 UI/00100 NumberInput.rb', line 74

def width
  @width_accounting_sprites.map(&:width).reduce(0, :+)
end