Class: Input::Text

Inherits:
Base
  • Object
show all
Defined in:
mygame/lib/sprite_kit/vendor/input.rb

Direct Known Subclasses

Prompt

Constant Summary

Constants inherited from Base

Base::CURSOR_FLASH_TICKS, Base::CURSOR_FULL_TICKS, Base::CURSOR_HOT_TICKS

Constants included from Keyboard

Keyboard::ALT_KEYS, Keyboard::CTRL_KEYS, Keyboard::IGNORE_KEYS, Keyboard::META_KEYS, Keyboard::SHIFT_KEYS

Instance Attribute Summary

Attributes inherited from Base

#content_h, #content_w, #cursor_x, #cursor_y, #font_height, #readonly, #scroll_h, #scroll_w, #scroll_x, #scroll_y, #selection_end, #selection_start, #value, #value_changed

Attributes included from Keyboard

#shift_lock

Instance Method Summary collapse

Methods inherited from Base

#append, #blur, #content_rect, #copy, #current_selection, #current_word, #cut, #delete_back, #delete_forward, #draw_cursor, #find, #find_next, #find_prev, #find_word_break_left, #find_word_break_right, #focus, #focussed?, #insert, #insert_at, #move_char_left, #move_char_right, #move_to_end, #move_to_start, #move_word_left, #move_word_right, #paste, #rect, #scroll_rect, #select_all, #select_char_left, #select_char_right, #select_to_end, #select_to_start, #select_word_left, #select_word_right, #size_enum, #size_px, #tick, #value_changed?

Methods included from Keyboard

#initialize_keyboard, #prepare_special_keys

Methods included from Util

#parse_color, #parse_color_nilable, #position_in_window

Constructor Details

#initialize(args, **params) ⇒ Text

Returns a new instance of Text.



1031
1032
1033
1034
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1031

def initialize(args, **params)
  @value = TextValue.new(params[:value].to_s)
  super(args, **params)
end

Instance Method Details

#draw_override(ffi) ⇒ Object



1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1036

def draw_override(ffi)
  # The argument order for ffi_draw.draw_sprite_3 is:
  # x, y, w, h,
  # path,
  # angle,
  # alpha, red_saturation, green_saturation, blue_saturation
  # tile_x, tile_y, tile_w, tile_h,
  # flip_horizontally, flip_vertically,
  # angle_anchor_x, angle_anchor_y,
  # source_x, source_y, source_w, source_h
  ffi.draw_sprite_3(
    @x, @y, @w, @h,
    @path,
    0, 255, 255, 255, 255,
    nil, nil, nil, nil,
    false, false,
    0, 0,
    0, 0, @w, @h
  )

  super # handles focus
end

#find_index_at_x(x, str = @value) ⇒ Object

rubocop:disable Metrics/MethodLength



1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1155

def find_index_at_x(x, str = @value) # rubocop:disable Metrics/MethodLength
  return 0 if x < @padding

  l = 0
  r = @value.length - 1
  loop do
    return l if l > r

    m = ((l + r) / 2).floor
    px = @font_style.string_width(str[0, m].to_s)
    if px == x
      return m
    elsif px < x
      l = m + 1
    else
      r = m - 1
    end
  end
end

#handle_keyboard ⇒ Object



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1059

def handle_keyboard
  if @meta || @ctrl
    # TODO: undo/redo
    if @down_keys.include?(:a)
      select_all
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:c)
      copy
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:x)
      @readonly ? copy : cut
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:v)
      paste unless @readonly
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:left)
      @shift ? select_to_start : move_to_start
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:right)
      @shift ? select_to_end : move_to_end
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:g)
      @shift ? find_prev : find_next
      @ensure_cursor_visible = true
    else
      @on_unhandled_key.call(@down_keys.first, self)
    end
  elsif @text_keys.empty?
    if @down_keys.include?(:delete)
      delete_forward unless @readonly
    elsif @down_keys.include?(:backspace)
      delete_back unless @readonly
    elsif @down_keys.include?(:left) || @down_keys.include?(:left_arrow)
      if @shift
        @alt ? select_word_left : select_char_left
        @ensure_cursor_visible = true
      else
        @alt ? move_word_left : move_char_left
        @ensure_cursor_visible = true
      end
    elsif @down_keys.include?(:right) || @down_keys.include?(:right_arrow)
      if @shift
        @alt ? select_word_right : select_char_right
        @ensure_cursor_visible = true
      else
        @alt ? move_word_right : move_char_right
        @ensure_cursor_visible = true
      end
    elsif @down_keys.include?(:home)
      @shift ? select_to_start : move_to_start
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:end)
      @shift ? select_to_end : move_to_end
      @ensure_cursor_visible = true
    else
      @on_unhandled_key.call(@down_keys.first, self)
    end
  else
    insert(@text_keys.join('')) unless @readonly
    @ensure_cursor_visible = true
  end
end

#handle_mouse ⇒ Object

TODO: Word selection (double click), All selection (triple click)



1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1123

def handle_mouse # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  mouse = @args.inputs.mouse

  if mouse.wheel && mouse.inside_rect?(self)
    d = mouse.wheel.x == 0 ? mouse.wheel.y : mouse.wheel.x
    @scroll_x += d * @mouse_wheel_speed
    @ensure_cursor_visible = false
  end

  return unless @mouse_down || (mouse.down && mouse.inside_rect?(self))

  if @mouse_down # dragging
    index = find_index_at_x(mouse.x - @x + @scroll_x)
    @selection_end = index
    @mouse_down = false if mouse.up
  else
    @on_click.call(mouse, self)
    return unless @focussed || @will_focus

    @mouse_down = true

    index = find_index_at_x(mouse.x - @x + @scroll_x)
    if @shift
      @selection_end = index
    else
      @selection_start = @selection_end = index
    end
  end

  @ensure_cursor_visible = true
end

#prepare_render_target ⇒ Object



1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1175

def prepare_render_target
  # TODO: handle padding correctly
  if @focussed || @will_focus
    bg = @background_color
    sc = @selection_color
  else
    bg = @blurred_background_color
    sc = @blurred_selection_color
  end

  @scroll_w = @font_style.string_width(@value.to_s).ceil
  @content_w = @w.lesser(@scroll_w)
  @scroll_h = @content_h = @h

  rt = @args.outputs[@path]
  rt.w = @w
  rt.h = @h
  rt.background_color = bg
  # TODO: implement sprite background
  rt.transient!

  if @value.empty?
    @cursor_x = 0
    @cursor_y = @padding
    @scroll_x = 0
    rt.primitives << @font_style.label(x: 0, y: @padding, text: @prompt, **@prompt_color)
  else
    # CURSOR AND SCROLL LOCATION
    @cursor_x = @font_style.string_width(@value[0, @selection_end].to_s)
    @cursor_y = @padding

    if @content_w < @w
      @scroll_x = 0
    elsif @ensure_cursor_visible
      if @cursor_x > @scroll_x + @content_w
        @scroll_x = @cursor_x - @content_w
      elsif @cursor_x < @scroll_x
        @scroll_x = @cursor_x
      end
    else
      @scroll_x = @scroll_x.cap_min_max(0, @scroll_w - @w)
    end

    # SELECTION
    if @selection_start != @selection_end
      if @selection_start < @selection_end
        left = (@font_style.string_width(@value[0, @selection_start].to_s) - @scroll_x).cap_min_max(0, @w)
        right = (@font_style.string_width(@value[0, @selection_end].to_s) - @scroll_x).cap_min_max(0, @w)
      elsif @selection_start > @selection_end
        left = (@font_style.string_width(@value[0, @selection_end].to_s) - @scroll_x).cap_min_max(0, @w)
        right = (@font_style.string_width(@value[0, @selection_start].to_s) - @scroll_x).cap_min_max(0, @w)
      end

      rt.primitives << { x: left, y: 0, w: right - left, h: @font_height + @padding * 2, path: :solid }.merge!(sc)
    end

    # TEXT
    f = find_index_at_x(@scroll_x)
    l = find_index_at_x(@scroll_x + @content_w) + 2
    rt.primitives << @font_style.label(x: 0, y: @padding, text: @value[f, l - f], **@text_color)
  end

  draw_cursor(rt)
end