Class: Zif::UI::Input

Inherits:
Label
  • Object
show all
Includes:
KeyPressable, Serializable
Defined in:
lib/zif/ui/input.rb

Overview

A basic input field that can accept keystrokes and record and display changes

to have it receive key strokes it needs to be registered

$game.services[:input_service].register_key_pressable(@input)

and added to the static_labels so that it draws on the screen.

$gtk.args.outputs.static_labels << [@input]

Constant Summary collapse

FILTER_NUMERIC =

convience values for @filter_keys

('0'..'9').to_a.freeze
FILTER_ALPHA_LOWERCASE =
('a'..'z').to_a.freeze
FILTER_ALPHA =
(FILTER_ALPHA_LOWERCASE + FILTER_ALPHA_LOWERCASE.map(&:upcase)).freeze
FILTER_ALPHA_NUMERIC =
(FILTER_NUMERIC + FILTER_ALPHA).freeze
FILTER_ALPHA_NUMERIC_UPPERCASE =
(FILTER_NUMERIC + FILTER_ALPHA).map(&:upcase).freeze

Constants inherited from Label

Label::ALIGNMENT, Label::BLENDMODE, Label::MAGIC_NEWLINE_DELIMITER, Label::VERTICAL_ALIGNMENT

Instance Attribute Summary collapse

Attributes included from KeyPressable

#on_key_down

Attributes inherited from Label

#a, #anchor_x, #anchor_y, #b, #ellipsis, #font, #full_text, #g, #max_width, #min_height, #min_width, #r, #size, #size_px, #text, #x, #y

Attributes included from Actions::Actionable

#actions, #dirty

Instance Method Summary collapse

Methods included from Serializable

#exclude_from_serialize, #inspect, #serialize, #to_s

Methods included from KeyPressable

#handle_key

Methods inherited from Label

#align, #align=, #blend, #blend=, #color, #color=, #full_size_rect, #primitive_marker, #recalculate_minimums, #recenter_in, #rect, #retruncate, #right, #split_labels, #truncate, #vertical_align, #vertical_align=, #wrap

Methods included from Actions::Actionable

#bounce_forever_around, #delayed_action, #fade_in, #fade_out, #fade_out_and_in_forever, #new_action, #perform_actions, #run_action, #running_actions?, #stop_action

Constructor Details

#initialize(text = '', size: -1,, alignment: :left, vertical_alignment: :top, font: 'font.tff', ellipsis: '…', r: 51, g: 51, b: 51, a: 255) ⇒ Input

Returns a new instance of Input.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zif/ui/input.rb', line 36

def initialize(
  text='',
  size:               -1,
  alignment:          :left,
  vertical_alignment: :top,
  font:               'font.tff',
  ellipsis:           '',
  r:                  51,
  g:                  51,
  b:                  51,
  a:                  255
)
  # super # calling super without args should have worked and passed the args up, but apparently only text??
  # https://github.com/DragonRuby/dragonruby-game-toolkit-contrib/issues/78
  super(
    text,
    size:               size,
    alignment:          alignment,
    vertical_alignment: vertical_alignment,
    font:               font,
    ellipsis:           ellipsis,
    r:                  r,
    g:                  g,
    b:                  b,
    a:                  a
  )

  @on_key_down = ->(text_key, all_keys) { handle_input(text_key, all_keys) }
  @filter_keys = nil
end

Instance Attribute Details

#filter_keysArray<String>

Returns List of characters to accept as valid input. Defaults to nil to allow all characters.

Returns:

  • (Array<String>)

    List of characters to accept as valid input. Defaults to nil to allow all characters.



21
22
23
# File 'lib/zif/ui/input.rb', line 21

def filter_keys
  @filter_keys
end

#has_focusBoolean

Returns Input fields only record key strokes when it has focus, set to true when you want to capture keys. Defaults to false.

Returns:

  • (Boolean)

    Input fields only record key strokes when it has focus, set to true when you want to capture keys. Defaults to false



27
28
29
# File 'lib/zif/ui/input.rb', line 27

def has_focus
  @has_focus
end

#max_lengthInteger

Returns Constrain input to max_length number of characters. Defaults to zero to allow any number of characters.

Returns:

  • (Integer)

    Constrain input to max_length number of characters. Defaults to zero to allow any number of characters.



18
19
20
# File 'lib/zif/ui/input.rb', line 18

def max_length
  @max_length
end

#special_keysArray<Symbol>

Returns List of additional symbols to process, but not to append to the field. Defaults to [:delete, :backspace] and handles those.

Returns:

  • (Array<Symbol>)

    List of additional symbols to process, but not to append to the field. Defaults to [:delete, :backspace] and handles those.



24
25
26
# File 'lib/zif/ui/input.rb', line 24

def special_keys
  @special_keys
end

Instance Method Details

#focused?Boolean

Returns Is the text field focused?.

Returns:

  • (Boolean)

    Is the text field focused?



68
69
70
# File 'lib/zif/ui/input.rb', line 68

def focused?
  @has_focus
end

#handle_input(text_key, all_keys) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/zif/ui/input.rb', line 73

def handle_input(text_key, all_keys)
  return false unless has_focus

  return false if @filter_keys&.include?(text_key)

  delete = (all_keys & %i[delete backspace]).any?
  return false if max_length.positive? && !delete && text.length >= max_length

  if delete
    self.text = text.chop
  else
    text.concat(text_key) unless text_key.nil?
  end
  true
end