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, #content_height, #content_width, #default_events, #draw, #enabled?, #height, #hide, #hit?, #inner_height, #inner_width, #is_root?, #noncontent_height, #noncontent_width, #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
21
22
# 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

  @offset_x = 0

  return self
end

Instance Method Details

#blur(sender) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 135

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

  return :handled
end

#caret_positionObject



144
145
146
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 144

def caret_position
  text_input_position_for(:caret_pos)
end

#draw_caretObject



34
35
36
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 34

def draw_caret
  Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z)
end

#draw_selectionObject



38
39
40
41
42
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 38

def draw_selection
  selection_width = caret_position - selection_start_position

  Gosu.draw_rect(selection_start_position, @text.y, selection_width, @text.height, default(:selection_color), @z)
end

#enter(sender) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 115

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

#keep_caret_visibleObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 71

def keep_caret_visible
  caret_pos = (caret_position - @text.x) + @caret_width

  @last_text ||= "/\\"
  @last_pos ||= -1

  puts "caret pos: #{caret_pos}, width: #{@width}, offset: #{@offset_x}" if (@last_text != @text.text) || (@last_pos != caret_pos)

  @last_text = @text.text
  @last_pos = caret_pos


  if caret_pos.between?(@offset_x, @width + @offset_x)
    # Do nothing


  elsif caret_pos < @offset_x
    if caret_pos > @width
      @offset_x = caret_pos + @width
    else
      @offset_x = 0
    end

  elsif caret_pos > @width
    @offset_x = caret_pos - @width
    puts "triggered"

  else
    # Reset to Zero

    @offset_x = 0
  end
end

#leave(sender) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 127

def leave(sender)
  unless @focus
    super
  end

  return :handled
end

#left_mouse_button(sender, x, y) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 103

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

  @caret_last_interval = Gosu.milliseconds
  @show_caret = true

  move_caret_to_mouse(x)

  return :handled
end

#move_caret_to_mouse(mouse_x) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 60

def move_caret_to_mouse(mouse_x)
  1.upto(@text.text.length) do |i|
    if mouse_x < @text.x + @text.textobject.text_width(@text.text[0...i])
      @text_input.caret_pos = @text_input.selection_start = i - 1;
      return
    end
  end

  @text_input.caret_pos = @text_input.selection_start = @text_input.text.length
end

#recalculateObject



160
161
162
163
164
165
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 160

def recalculate
  super

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

#renderObject



24
25
26
27
28
29
30
31
32
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 24

def render
  Gosu.clip_to(@text.x, @text.y, @style.width, @text.height) do
    Gosu.translate(-@offset_x, 0) do
      draw_selection
      draw_caret if @focus && @show_caret
      draw_text
    end
  end
end

#selection_start_positionObject



148
149
150
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 148

def selection_start_position
  text_input_position_for(:selection_start)
end

#text_input_position_for(method) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 152

def text_input_position_for(method)
  if @type == :password
    @text.x + @text.textobject.text_width(default(:password_character) * @text_input.text[0..@text_input.send(method)].length)
  else
    @text.x + @text.textobject.text_width(@text_input.text[0..@text_input.send(method)])
  end
end

#updateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 44

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

  keep_caret_visible
end

#valueObject



167
168
169
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 167

def value
  @text_input.text
end