Class: CyberarmEngine::Element::EditLine

Inherits:
Button show all
Defined in:
lib/cyberarm_engine/ui/elements/edit_line.rb

Constant Summary

Constants included from Theme

Theme::THEME

Instance Attribute Summary

Attributes inherited from CyberarmEngine::Element

#background_canvas, #border_canvas, #enabled, #event_handler, #options, #parent, #style, #x, #y, #z

Instance Method Summary collapse

Methods inherited from Button

#clicked_left_mouse_button, #draw_text, #released_left_mouse_button

Methods inherited from Label

#clicked_left_mouse_button, #value=

Methods inherited from CyberarmEngine::Element

#background=, #button_down, #button_up, #default_events, #draw, #enabled?, #height, #hide, #hit?, #inner_height, #inner_width, #is_root?, #outer_height, #outer_width, #reposition, #root, #set_background, #set_border_color, #set_border_thickness, #set_margin, #set_padding, #show, #stylize, #toggle, #update_background, #value=, #visible?, #width

Methods included from Common

#current_state, #darken, #draw_rect, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #show_cursor, #show_cursor=, #window

Methods included from CyberarmEngine::Event

#event, #publish, #subscribe, #unsubscribe

Methods included from Theme

#deep_merge, #default, #theme_defaults

Constructor Details

#initialize(text, options = {}, block = nil) ⇒ EditLine

Returns a new instance of EditLine.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 4

def initialize(text, options = {}, block = nil)
  super(text, options, block)

  @type = default(:type)

  @caret_width = default(:caret_width)
  @caret_height= @text.height
  @caret_color = default(:caret_color)
  @caret_interval = default(:caret_interval)
  @caret_last_interval = Gosu.milliseconds
  @show_caret  = true

  @text_input = Gosu::TextInput.new
  @text_input.text = text

  return self
end

Instance Method Details

#blur(sender) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 73

def blur(sender)
  @focus = false
  @style.background_canvas.background = default(:background)
  @text.color = default(:color)
  window.text_input = nil

  return :handled
end

#caret_positionObject

TODO: Fix caret rendering in wrong position unless caret_pos is at end of text



83
84
85
86
87
88
89
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 83

def caret_position
  if @type == :password
    @text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.caret_pos-1].length)
  else
    @text.x + @text.textobject.text_width(@text_input.text[0..@text_input.caret_pos-1])
  end
end

#enter(sender) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 53

def enter(sender)
  if @focus
    @style.background_canvas.background = default(:active, :background)
    @text.color = default(:active, :color)
  else
    @style.background_canvas.background = default(:hover, :background)
    @text.color = default(:hover, :color)
  end

  return :handled
end

#leave(sender) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 65

def leave(sender)
  unless @focus
    super
  end

  return :handled
end

#left_mouse_button(sender, x, y) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 43

def left_mouse_button(sender, x, y)
  super
  window.text_input = @text_input

  @caret_last_interval = Gosu.milliseconds
  @show_caret = true

  return :handled
end

#recalculateObject



91
92
93
94
95
96
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 91

def recalculate
  super

  @width = dimensional_size(@style.width, :width) || default(:width)
  update_background
end

#renderObject



22
23
24
25
26
27
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 22

def render
  Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
    draw_text
    Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z + 40) if @focus && @show_caret
  end
end

#updateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 29

def update
  if @type == :password
    @text.text = default(:password_character) * @text_input.text.length
  else
    @text.text = @text_input.text
  end

  if Gosu.milliseconds >= @caret_last_interval + @caret_interval
    @caret_last_interval = Gosu.milliseconds

    @show_caret = !@show_caret
  end
end

#valueObject



98
99
100
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 98

def value
  @text_input.text
end