Class: CyberarmEngine::Element::EditLine

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

Direct Known Subclasses

EditBox

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, #tip, #x, #y, #z

Instance Method Summary collapse

Methods inherited from Button

#clicked_left_mouse_button, #draw_image, #released_left_mouse_button, #value=

Methods inherited from Label

#clicked_left_mouse_button, #handle_text_wrapping, #line_width, #value=

Methods inherited from CyberarmEngine::Element

#background=, #button_up, #content_height, #content_width, #default_events, #dimensional_size, #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, #set_static_position, #show, #stylize, #to_s, #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
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 4

def initialize(text, options = {}, block = nil)
  @filter = options.delete(:filter)
  super(text, options, block)

  @type = default(:type)

  @caret_width = default(:caret_width)
  @caret_height = @text.textobject.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
  @last_text_value = text

  if @filter && @filter.respond_to?(:call)
    @text_input.instance_variable_set(:@filter, @filter)

    def @text_input.filter(text_in)
      @filter.call(text_in)
    end
  end

  @offset_x = 0
  @offset_y = 0

  event(:begin_drag)
  event(:drag_update)
  event(:end_drag)
end

Instance Method Details

#begin_drag(_sender, x, _y, _button) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 226

def begin_drag(_sender, x, _y, _button)
  @drag_start = x
  @offset_drag_start = @offset_x
  @drag_caret_position = @text_input.caret_pos

  :handled
end

#blur(_sender) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 213

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

  :handled
end

#button_down(id) ⇒ Object



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

def button_down(id)
  handle_keyboard_shortcuts(id)
end

#caret_positionObject



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

def caret_position
  text_input_position_for(:caret_pos)
end

#caret_position_under_mouse(mouse_x) ⇒ Object



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

def caret_position_under_mouse(mouse_x)
  1.upto(@text.text.length) do |i|
    return i - 1 if mouse_x < @text.x - @offset_x + @text.width(@text.text[0...i])
  end

  @text_input.text.length
end

#drag_update(_sender, x, _y, _button) ⇒ Object



234
235
236
237
238
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 234

def drag_update(_sender, x, _y, _button)
  @text_input.caret_pos = caret_position_under_mouse(x)

  :handled
end

#draggable?(button) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 222

def draggable?(button)
  button == :left
end

#draw_caretObject



51
52
53
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 51

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

#draw_selectionObject



55
56
57
58
59
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 55

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

#draw_textObject



47
48
49
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 47

def draw_text
  @text.draw(:draw_text)
end

#end_drag(_sender, _x, _y, _button) ⇒ Object



240
241
242
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 240

def end_drag(_sender, _x, _y, _button)
  :handled
end

#enter(_sender) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 195

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

  :handled
end

#handle_keyboard_shortcuts(id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 87

def handle_keyboard_shortcuts(id)
  return unless @focus && @enabled

  if Gosu.button_down?(Gosu::KB_LEFT_CONTROL) || Gosu.button_down?(Gosu::KB_RIGHT_CONTROL)
    case id
    when Gosu::KB_A
      @text_input.selection_start = 0
      @text_input.caret_pos = @text_input.text.length

    when Gosu::KB_C
      if @text_input.selection_start < @text_input.caret_pos
        Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos])
      else
        Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start])
      end

    when Gosu::KB_X
      chars = @text_input.text.chars

      if @text_input.selection_start < @text_input.caret_pos
        Clipboard.copy(@text_input.text[@text_input.selection_start...@text_input.caret_pos])
        chars.slice!(@text_input.selection_start, @text_input.caret_pos)
      else
        Clipboard.copy(@text_input.text[@text_input.caret_pos...@text_input.selection_start])
        chars.slice!(@text_input.caret_pos, @text_input.selection_start)
      end

      @text_input.text = chars.join

    when Gosu::KB_V
      if instance_of?(EditLine) # EditLine assumes a single line of text
        @text_input.text = @text_input.text.insert(@text_input.caret_pos,
                                                   Clipboard.paste.encode("UTF-8").gsub("\n", ""))
      else
        @text_input.text = @text_input.text.insert(@text_input.caret_pos, Clipboard.paste.encode("UTF-8"))
      end
    end
  end
end

#keep_caret_visibleObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 139

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

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

  @last_text = @text.text
  @last_pos = caret_pos

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

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

  elsif caret_pos > @width
    @offset_x = caret_pos - @width

  else
    # Reset to Zero
    @offset_x = 0
  end
end

#leave(sender) ⇒ Object



207
208
209
210
211
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 207

def leave(sender)
  super unless @focus

  :handled
end

#left_mouse_button(sender, x, y) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 183

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, y)

  :handled
end

#move_caret_to_mouse(mouse_x, _mouse_y) ⇒ Object



135
136
137
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 135

def move_caret_to_mouse(mouse_x, _mouse_y)
  @text_input.caret_pos = @text_input.selection_start = caret_position_under_mouse(mouse_x)
end

#recalculateObject



244
245
246
247
248
249
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 244

def recalculate
  super

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

#renderObject



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

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

#selection_start_positionObject



171
172
173
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 171

def selection_start_position
  text_input_position_for(:selection_start)
end

#text_input_position_for(method) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 175

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

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 61

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

  if @last_text_value != value
    @last_text_value = value

    publish(:changed, value)
  end

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

    @show_caret = !@show_caret
  end

  keep_caret_visible
end

#valueObject



251
252
253
# File 'lib/cyberarm_engine/ui/elements/edit_line.rb', line 251

def value
  @text_input.text
end