Class: Input::Multiline

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

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) ⇒ Multiline

Returns a new instance of Multiline.



1244
1245
1246
1247
1248
1249
1250
1251
1252
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1244

def initialize(args, **params)
  value = params[:value].to_s

  super(args, **params)

  @word_wrap_chars = @word_chars.merge(@punctuation_chars)
  @value = MultilineValue.new(value, @word_wrap_chars, @crlf_chars, @w, font_style: @font_style)
  @fill_from_bottom = params[:fill_from_bottom] || false
end

Instance Method Details

#current_line ⇒ Object



1517
1518
1519
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1517

def current_line
  @value.lines&.line_at(@selection_end)
end

#draw_override(ffi) ⇒ Object



1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1286

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_line_end ⇒ Object



1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1424

def find_line_end
  line = @value.lines.line_at(@selection_end)
  if line.wrapped?
    if @selection_end == line.end
      if @value.lines.length > line.number
        line = @value.lines[line.number + 1]
        line.wrapped? ? line.end - 1 : line.end
      else
        line.end
      end
    else
      line.end - 1
    end
  else
    line.end
  end
end

#handle_keyboard ⇒ Object



1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1308

def handle_keyboard
  # On a Mac:
  # Home is Cmd + ↑ / Fn + ←
  # End is Cmd + ↓ / Fn + →
  # PageUp is Fn + ↑
  # PageDown is Fn + ↓
  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?(:g)
      @shift ? find_prev : find_next
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:left) || @down_keys.include?(:left_arrow)
      @shift ? select_to_line_start : move_to_line_start
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:right) || @down_keys.include?(:right_arrow)
      @shift ? select_to_line_end : move_to_line_end
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:up) || @down_keys.include?(:up_arrow)
      @shift ? select_to_start : move_to_start
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:down) || @down_keys.include?(:down_arrow)
      @shift ? select_to_end : move_to_end
      @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
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:backspace)
      delete_back unless @readonly
      @ensure_cursor_visible = true
    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
    # TODO: Retain a original_cursor_x when moving up/down to try stay generally in the same x range
    elsif @down_keys.include?(:up) || @down_keys.include?(:up_arrow)
      if @shift
        select_line_up
        @ensure_cursor_visible = true
      else
        # TODO: beginning of previous paragraph with alt
        move_line_up
        @ensure_cursor_visible = true
      end
    elsif @down_keys.include?(:down) || @down_keys.include?(:down_arrow)
      if @shift
        select_line_down
        @ensure_cursor_visible = true
      else
        # TODO: end of next paragraph with alt
        move_line_down
        @ensure_cursor_visible = true
      end
    elsif @down_keys.include?(:enter)
      insert("\n") unless @readonly
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:pageup)
      @shift ? select_page_up : move_page_up
      @ensure_cursor_visible = true
    elsif @down_keys.include?(:pagedown)
      @shift ? select_page_down : move_page_down
      @ensure_cursor_visible = true
    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)



1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1522

def handle_mouse # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  mouse = @args.inputs.mouse
  inside = mouse.inside_rect?(self)

  if mouse.wheel && inside
    @scroll_y += mouse.wheel.y * @mouse_wheel_speed
    @ensure_cursor_visible = false
  end

  return unless @mouse_down || (mouse.down && inside)

  if @fill_from_bottom
    relative_y = @content_h < @h ? @y - mouse.y + @content_h : @scroll_h - (mouse.y - @y + @scroll_y)
  else
    relative_y = @scroll_h - (mouse.y - @y + @scroll_y)
    relative_y += @h - @content_h if @content_h < @h
  end
  line = @value.lines[relative_y.idiv(@font_height).cap_min_max(0, @value.lines.length - 1)]
  index = line.index_at(mouse.x - @x + @scroll_x)

  if @mouse_down # dragging
    @selection_end = index
    @mouse_down = false if mouse.up
  else # clicking
    @on_click.call(mouse, self)
    return unless (@focussed || @will_focus) && mouse.button_left

    if @shift
      @selection_end = index
    else
      @selection_start = @selection_end = index
    end
    @mouse_down = true
  end

  @ensure_cursor_visible = true
end

#lines ⇒ Object



1254
1255
1256
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1254

def lines
  @value.lines
end

#move_line_down ⇒ Object



1462
1463
1464
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1462

def move_line_down
  @selection_end = @selection_start = selection_end_down_index
end

#move_line_up ⇒ Object



1454
1455
1456
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1454

def move_line_up
  @selection_end = @selection_start = selection_end_up_index
end

#move_page_down ⇒ Object



1505
1506
1507
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1505

def move_page_down
  (@h / @font_height).floor.times { @selection_start = @selection_end = selection_end_down_index }
end

#move_page_up ⇒ Object



1501
1502
1503
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1501

def move_page_up
  (@h / @font_height).floor.times { @selection_start = @selection_end = selection_end_up_index }
end

#move_to_line_end ⇒ Object



1446
1447
1448
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1446

def move_to_line_end
  @selection_start = @selection_end = find_line_end
end

#move_to_line_start ⇒ Object



1418
1419
1420
1421
1422
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1418

def move_to_line_start
  line = @value.lines.line_at(@selection_end)
  index = line.new_line? ? line.start + 1 : line.start
  @selection_start = @selection_end = index
end

#prepare_render_target ⇒ Object



1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1572

def prepare_render_target # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  reflow! if @reflow_required

  if @focussed || @will_focus
    bg = @background_color
    sc = @selection_color
  else
    bg = @blurred_background_color
    sc = @blurred_selection_color
  end

  # TODO: Implement line spacing
  lines = @value.lines
  @scroll_w = @content_w = @w
  @scroll_h = lines.length * @font_height + 2 * @padding
  @content_h = @h.lesser(@scroll_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_line = 0
    @cursor_x = 0
    @scroll_x = 0
    if @fill_from_bottom
      @cursor_y = -@padding
      rt.primitives << @font_style.label(x: 0, y: 0, text: @prompt, **@prompt_color)
    else
      @cursor_y = @h - @font_height - @padding
      rt.primitives << @font_style.label(x: 0, y: @h - @font_height, text: @prompt, **@prompt_color)
    end
  else
    # CURSOR AND SCROLL LOCATION
    @cursor_line = lines.line_at(@selection_end)
    @cursor_index = @selection_end - @cursor_line.start
    # Move the cursor to the beginning of the next line if the line is wrapped and we're at the end of the line
    if @cursor_index == @cursor_line.length && @cursor_line.wrapped? && lines.length > @cursor_line.number
      @cursor_line = lines[@cursor_line.number + 1]
      @cursor_index = 0
    end

    @cursor_y = @scroll_h - (@cursor_line.number + 1) * @font_height - @padding
    @cursor_y += @fill_from_bottom ? @content_h : @h - @content_h if @content_h < @h
    if @scroll_h <= @h # total height is less than height of the control
      @scroll_y = @fill_from_bottom ? @scroll_h : 0
    elsif @ensure_line_visible
      # TODO: Line visibility
      @scroll_y = (@value.lines.length - @ensure_line_visible) * @font_height - @h + @padding
      @ensure_line_visible = nil
    elsif @ensure_cursor_visible
      if @cursor_y + @font_height > @scroll_y + @content_h
        @scroll_y = @cursor_y + @font_height - @content_h
      elsif @cursor_y < @scroll_y
        @scroll_y = @cursor_y
      end
    else
      @scroll_y = @scroll_y.cap_min_max(0, @scroll_h - @h)
    end
    @cursor_x = @cursor_line.measure_to(@cursor_index).lesser(@w)
    @ensure_cursor_visible = false

    selection_start = @selection_start.lesser(@selection_end)
    selection_end = @selection_start.greater(@selection_end)
    selection_visible = selection_start != selection_end

    content_bottom = @scroll_y - @font_height # internal use only, includes font_height, used for draw
    content_top = @scroll_y + @content_h # internal use only, used for draw
    selection_h = @font_height

    b = @scroll_h - @padding - @scroll_y
    if @content_h < @h
      i = -1
      l = lines.length
      b += @fill_from_bottom ? @content_h : @h - @content_h
    else
      i = (@scroll_h - content_top).idiv(@font_height).greater(0) - 1
      l = (@scroll_h - content_bottom).idiv(@font_height).lesser(lines.length)
    end
    while (i += 1) < l
      line = lines[i]
      y = b - (i + 1) * @font_height

      # SELECTION
      if selection_visible && selection_start <= line.end && selection_end >= line.start
        left = line.measure_to((selection_start - line.start).greater(0))
        right = line.measure_to((selection_end - line.start).lesser(line.length))
        rt.primitives << { x: left, y: y, w: right - left, h: selection_h, path: :solid }.merge!(sc)
      end

      # TEXT FOR LINE
      rt.primitives << @font_style.label(x: 0, y: y, text: line.clean_text, **@text_color)
    end
  end

  draw_cursor(rt)
end

#reflow! ⇒ Object



1279
1280
1281
1282
1283
1284
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1279

def reflow!
  @ensure_line_visible = @value.lines.length - ((@scroll_y + @h) / @font_height).floor
  @font_height = @font_style.font_height
  @value.reflow(@w, @font_style)
  @reflow_required = false
end

#select_line_down ⇒ Object



1458
1459
1460
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1458

def select_line_down
  @selection_end = selection_end_down_index
end

#select_line_up ⇒ Object



1450
1451
1452
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1450

def select_line_up
  @selection_end = selection_end_up_index
end

#select_page_down ⇒ Object



1513
1514
1515
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1513

def select_page_down
  (@h / @font_height).floor.times { @selection_end = selection_end_down_index }
end

#select_page_up ⇒ Object



1509
1510
1511
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1509

def select_page_up
  (@h / @font_height).floor.times { @selection_end = selection_end_up_index }
end

#select_to_line_end ⇒ Object



1442
1443
1444
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1442

def select_to_line_end
  @selection_end = find_line_end
end

#select_to_line_start ⇒ Object



1412
1413
1414
1415
1416
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1412

def select_to_line_start
  line = @value.lines.line_at(@selection_end)
  index = line.new_line? ? line.start + 1 : line.start
  @selection_end = index
end

#selection_end_down_index ⇒ Object



1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1484

def selection_end_down_index
  line = @value.lines.line_at(@selection_end)
  if line.number == @value.lines.length - 1
    @selection_end
  elsif line.new_line? && @selection_end == line.start + 1
    line = @value.lines[line.number + 1]
    line.new_line? ? line.start + 1 : line.start
  elsif @selection_end == line.start
    @value.lines[line.number + 1].start
  elsif line.wrapped? && line.end == @selection_end && line.number < @value.lines.length - 2
    line = @value.lines[line.number + 2]
    line.new_line? ? line.start + 1 : line.start
  else
    @value.lines[line.number + 1].index_at(@cursor_x + @scroll_x)
  end
end

#selection_end_up_index ⇒ Object



1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1466

def selection_end_up_index
  return 0 if selection_end == 0

  line = @value.lines.line_at(@selection_end)
  if line.wrapped? && line.end == @selection_end
    line.new_line? ? line.start + 1 : line.start
  elsif line.number == 0
    @selection_end
  elsif line.new_line? && @selection_end == line.start + 1
    line = @value.lines[line.number - 1]
    line.new_line? ? line.start + 1 : line.start
  elsif @selection_end == line.start
    @value.lines[line.number - 1].start
  else
    @value.lines[line.number - 1].index_at(@cursor_x + @scroll_x)
  end
end

#size_enum=(val) ⇒ Object



1265
1266
1267
1268
1269
1270
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1265

def size_enum=(val)
  return if @font_style.size_enum == val

  @font_style = FontStyle.from(word_chars: @word_chars.keys, font: @font_style.font, size_enum: val)
  @reflow_required = true
end

#size_px=(val) ⇒ Object



1272
1273
1274
1275
1276
1277
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1272

def size_px=(val)
  return if @font_style.size_px == val

  @font_style = FontStyle.from(word_chars: @word_chars.keys, font: @font_style.font, size_px: val)
  @reflow_required = true
end

#w=(val) ⇒ Object



1258
1259
1260
1261
1262
1263
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1258

def w=(val)
  return if @w == val

  @w = val
  @reflow_required = true
end