Class: ExampleApp::FormField

Inherits:
Zif::CompoundSprite show all
Defined in:
app/ui/labels/form_field.rb

Overview

This is an example of bundling Zif::UI::Input together with a descriptive label and a background sprite

Constant Summary

Constants inherited from Zif::Sprite

Zif::Sprite::BLENDMODE

Instance Attribute Summary collapse

Attributes inherited from Zif::CompoundSprite

#labels, #sprites

Attributes inherited from Zif::Sprite

#a, #angle, #b, #g, #h, #logical_x, #logical_y, #name, #path, #r, #render_target, #source_h, #source_w, #source_x, #source_y, #w, #x, #y, #z_index

Attributes included from Zif::Clickable

#on_mouse_changed, #on_mouse_down, #on_mouse_up

Attributes included from Zif::Actions::Animatable

#animation_sequences, #cur_animation

Attributes included from Zif::Actions::Actionable

#actions, #dirty

Instance Method Summary collapse

Methods inherited from Zif::CompoundSprite

#draw_override, #source_rect

Methods inherited from Zif::Sprite

#blend, #blend=, #center, #center_x, #center_y, #clicked?, #color, #color=, #dup_and_assign, #exclude_from_serialize, #hide, #rect, rect_array_to_hash, rect_array_to_source_hash, #rect_hash, rect_hash_to_source_hash, #show, #source_as_rect_hash, #source_center, #source_is_set?, #source_rect, #source_rect_hash, #source_wh, #source_xy, #to_h, #view_actual_size!, #wh, #xy, #zoom_factor

Methods included from Zif::Clickable

#absorb_click?, #clicked?

Methods included from Zif::Actions::Animatable

#new_basic_animation, #new_tiled_animation, #register_animation_sequence, #run_animation_sequence, #stop_animating

Methods included from Zif::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

Methods included from Zif::Serializable

#exclude_from_serialize, #inspect, #serialize, #to_s

Methods included from Zif::Assignable

#assign

Constructor Details

#initialize(name: Zif.unique_name('form_field'), x: 0, y: 0, label: 'Input: ', char_width: 10) ⇒ FormField

Returns a new instance of FormField.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/ui/labels/form_field.rb', line 6

def initialize(name: Zif.unique_name('form_field'), x: 0, y: 0, label: 'Input: ', char_width: 10)
  super(name)
  @x = x
  @y = y

  @label = Zif::UI::Label.new(label, size: 0, r: 255, g: 255, b: 255)
  @label.x = 0
  @label.y = 25

  @input = Zif::UI::Input.new('Input Placeholder', size: 0).tap do |l|
    l.x = @label.max_width + 5
    l.y = 25
    l.color = [0, 0, 0].freeze
    l.max_length = char_width
    l.has_focus = false
    # l.filter_keys = Zif::UI::Input::FILTER_ALPHA_NUMERIC_UPPERCASE
  end

  @background = Zif::Sprite.new.tap do |bg|
    bg.x = @input.x - 5
    bg.y = 0
    bg.w = 20 + (char_width * 10)
    bg.h = 30
    bg.path = 'sprites/white_1.png'
  end

  lose_focus

  @w = @label.max_width + @background.w
  @h = @background.h
  @on_mouse_up = lambda do |_sprite, _point|
    gain_focus
  end

  @labels = [@label, @input]
  @sprites = [@background]
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



4
5
6
# File 'app/ui/labels/form_field.rb', line 4

def background
  @background
end

#inputObject

Returns the value of attribute input.



4
5
6
# File 'app/ui/labels/form_field.rb', line 4

def input
  @input
end

#labelObject

Returns the value of attribute label.



4
5
6
# File 'app/ui/labels/form_field.rb', line 4

def label
  @label
end

Instance Method Details

#gain_focusObject



44
45
46
47
48
49
# File 'app/ui/labels/form_field.rb', line 44

def gain_focus
  @input.has_focus = true
  @background.r = 255
  @background.b = 255
  @background.g = 255
end

#lose_focusObject



51
52
53
54
55
56
# File 'app/ui/labels/form_field.rb', line 51

def lose_focus
  @input.has_focus = false
  @background.r = 55
  @background.b = 55
  @background.g = 55
end