Class: Input::Base

Inherits:
Object
  • Object
show all
Includes:
Keyboard, Util
Defined in:
mygame/lib/sprite_kit/vendor/input.rb

Direct Known Subclasses

Multiline, Text

Constant Summary collapse

CURSOR_FULL_TICKS =
50
CURSOR_FLASH_TICKS =
30
CURSOR_HOT_TICKS =
12

Constants included from Keyboard

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

Instance Attribute Summary collapse

Attributes included from Keyboard

#shift_lock

Instance Method Summary collapse

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

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 578

def initialize(args, **params) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  @args = args
  @x = params[:x] || 0
  @y = params[:y] || 0

  word_chars = (params[:word_chars] || ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + ['_', '-'])
  @font_style = FontStyle.from(word_chars: word_chars, **params.slice(:font, :size_enum, :size_px))
  @font_height = @font_style.font_height
  @word_chars = Hash[word_chars.map { [_1, true] }]
  @punctuation_chars = Hash[(params[:punctuation_chars] || %w[! % , . ; : ' " ` ) \] } * &]).map { [_1, true] }]
  @crlf_chars = { "\r" => true, "\n" => true }

  @padding = params[:padding] || 2

  @w = params[:w] || 256
  @h = params[:h] || @font_height + @padding * 2

  @text_color = parse_color(params, :text).merge(vertical_alignment_enum: 0)
  @background_color = parse_color_nilable(params, :background)
  @blurred_background_color = parse_color_nilable(params, :blurred) || @background_color

  @prompt = params[:prompt] || ''
  @prompt_color = parse_color(params, :prompt, dr: 128, dg: 128, db: 128).merge(vertical_alignment_enum: 0)

  @max_length = params[:max_length] || false

  @selection_start = params[:selection_start] || params[:value].to_s.length
  @selection_end = params[:selection_end] || @selection_start

  @selection_color = parse_color(params, :selection, dr: 102, dg: 178, db: 255, da: 128)
  @blurred_selection_color = parse_color(params, :blurred_selection, dr: 112, dg: 128, db: 144, da: 128)

  # To manage the flashing cursor
  @cursor_color = parse_color(params, :cursor)
  @cursor_width = params[:cursor_width] || 2
  @cursor_ticks = 0
  @cursor_dir = 1
  @ensure_cursor_visible = true

  # Mouse focus for selection
  @mouse_down = false
  @mouse_wheel_speed = params[:mouse_wheel_speed] || @font_height

  # Render target for text scrolling
  @path = "__input_#{object_id}"

  @content_w = @w
  @content_h = @h

  @scroll_x = 0
  @scroll_y = 0
  @scroll_w = @w
  @scroll_h = @h

  @readonly = params[:readonly] || false
  @focussed = params[:focussed] || params[:focused] || false
  @will_focus = false # Get the focus at the end of the tick

  @on_click = params[:on_click] || params[:on_clicked] || NOOP
  # @on_handled_key = params[:on_handled_key] || NOOP
  @on_unhandled_key = params[:on_unhandled_key] || NOOP

  @value_changed = true

  initialize_keyboard(params)
end

Instance Attribute Details

#content_h ⇒ Object (readonly)

Returns the value of attribute content_h.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def content_h
  @content_h
end

#content_w ⇒ Object (readonly)

Returns the value of attribute content_w.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def content_w
  @content_w
end

#cursor_x ⇒ Object (readonly)

Returns the value of attribute cursor_x.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def cursor_x
  @cursor_x
end

#cursor_y ⇒ Object (readonly)

Returns the value of attribute cursor_y.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def cursor_y
  @cursor_y
end

#font_height ⇒ Object (readonly)

Returns the value of attribute font_height.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def font_height
  @font_height
end

#readonly ⇒ Object

Returns the value of attribute readonly.



572
573
574
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 572

def readonly
  @readonly
end

#scroll_h ⇒ Object (readonly)

Returns the value of attribute scroll_h.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def scroll_h
  @scroll_h
end

#scroll_w ⇒ Object (readonly)

Returns the value of attribute scroll_w.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def scroll_w
  @scroll_w
end

#scroll_x ⇒ Object

Returns the value of attribute scroll_x.



572
573
574
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 572

def scroll_x
  @scroll_x
end

#scroll_y ⇒ Object

Returns the value of attribute scroll_y.



572
573
574
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 572

def scroll_y
  @scroll_y
end

#selection_end ⇒ Object

Returns the value of attribute selection_end.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def selection_end
  @selection_end
end

#selection_start ⇒ Object

Returns the value of attribute selection_start.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def selection_start
  @selection_start
end

#value ⇒ Object

Returns the value of attribute value.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def value
  @value
end

#value_changed ⇒ Object (readonly)

Returns the value of attribute value_changed.



570
571
572
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 570

def value_changed
  @value_changed
end

Instance Method Details

#append(str) ⇒ Object



763
764
765
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 763

def append(str)
  insert_at(str, @value.length)
end

#blur ⇒ Object



714
715
716
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 714

def blur
  @focussed = false
end

#content_rect ⇒ Object



955
956
957
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 955

def content_rect
  { x: @scroll_x, y: @scroll_y, w: @content_w, h: @content_h }
end

#copy ⇒ Object



926
927
928
929
930
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 926

def copy
  return if @selection_start == @selection_end

  $gtk.ffi_misc.setclipboard(current_selection)
end

#current_selection ⇒ Object



775
776
777
778
779
780
781
782
783
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 775

def current_selection
  return nil if @selection_start == @selection_end

  if @selection_start < @selection_end
    @value[@selection_start, @selection_end - @selection_start]
  else
    @value[@selection_end, @selection_start - @selection_end]
  end
end

#current_word ⇒ Object



785
786
787
788
789
790
791
792
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 785

def current_word
  return nil if @selection_end == 0
  return nil unless @word_chars[@value[@selection_end - 1]]

  left = find_word_break_left
  right = @word_chars[@value[@selection_end]] ? find_word_break_right : @selection_end
  @value[left, right - left]
end

#cut ⇒ Object



932
933
934
935
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 932

def cut
  copy
  insert('')
end

#delete_back ⇒ Object



937
938
939
940
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 937

def delete_back
  @selection_start -= 1 if @selection_start == @selection_end && @selection_start > 0
  insert('')
end

#delete_forward ⇒ Object



942
943
944
945
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 942

def delete_forward
  @selection_start += 1 if @selection_start == @selection_end
  insert('')
end

#draw_cursor(rt) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 653

def draw_cursor(rt)
  return unless @focussed || @will_focus
  return if @readonly

  if @value_changed
    @cursor_ticks = CURSOR_HOT_TICKS unless @cursor_ticks > CURSOR_HOT_TICKS
    @cursor_dir = 1
  end

  @cursor_ticks += @cursor_dir

  alpha = if @cursor_moving
            255
          elsif
            @cursor_ticks >= CURSOR_FULL_TICKS
            @cursor_dir = -1
            255
          elsif @cursor_ticks <= 0
            @cursor_dir = 1
            0
          elsif @cursor_ticks < CURSOR_FULL_TICKS
            # Easing.smooth_start(start_at: 0, tick_count: @cursor_ticks, end_at: CURSOR_FLASH_TICKS, power: 2) * 255
            # More "abrupt" cursor to match OS.
            Easing.smooth_start(start_at: 0, tick_count: @cursor_ticks, end_at: CURSOR_FLASH_TICKS, power: 8) * 255
          else
            255
          end

  rt.primitives << {
    x: (@cursor_x - 1).greater(0) - @scroll_x,
    y: @cursor_y - @scroll_y,
    w: @cursor_width,
    h: @font_height,
    path: :solid
  }.merge!(**@cursor_color, a: alpha)
end

#draw_override(_ffi) ⇒ Object



645
646
647
648
649
650
651
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 645

def draw_override(_ffi)
  return unless @will_focus

  @will_focus = false
  @focussed = true
  @ensure_cursor_visible = true
end

#find(text) ⇒ Object



767
768
769
770
771
772
773
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 767

def find(text)
  index = @value.index(text)
  return unless index

  @selection_start = index
  @selection_end = index + text.length
end

#find_next ⇒ Object



794
795
796
797
798
799
800
801
802
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 794

def find_next
  text = current_selection
  return if text.nil?

  index = @value.index(text, @selection_end.greater(@selection_start)) || @value.index(text)

  @selection_start = index
  @selection_end = index + text.length
end

#find_prev ⇒ Object



804
805
806
807
808
809
810
811
812
813
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 804

def find_prev
  text = current_selection
  return if text.nil?

  index = @value.rindex(text, (@selection_start - 1).lesser(@selection_end - 1)) ||
          @value.rindex(text, @value.length)

  @selection_start = index
  @selection_end = index + text.length
end

#find_word_break_left ⇒ Object

rubocop:disable Metrics/MethodLength



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 815

def find_word_break_left # rubocop:disable Metrics/MethodLength
  return 0 if @selection_end == 0

  index = @selection_end
  value = @value.to_s

  loop do
    index -= 1
    return 0 if index == 0
    break if @word_chars[value[index]]
  end

  loop do
    index -= 1
    return 0 if index == 0
    return index + 1 unless @word_chars[value[index]]
  end
end

#find_word_break_right(index = @selection_end) ⇒ Object

rubocop:disable Metrics/MethodLength



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 834

def find_word_break_right(index = @selection_end) # rubocop:disable Metrics/MethodLength
  value = @value.to_s
  length = value.length
  return length if index >= length

  loop do
    return length if index == length
    break if @word_chars[value[index]]
    index += 1
  end

  loop do
    index += 1
    return length if index == length
    return index unless @word_chars[value[index]]
  end
end

#focus ⇒ Object



710
711
712
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 710

def focus
  @will_focus = true
end

#focussed? ⇒ Boolean Also known as: focused?

Returns:

  • (Boolean)


701
702
703
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 701

def focussed?
  @focussed
end

#insert(str) ⇒ Object Also known as: replace



742
743
744
745
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 742

def insert(str)
  @selection_end, @selection_start = @selection_start, @selection_end if @selection_start > @selection_end
  insert_at(str, @selection_start, @selection_end)
end

#insert_at(str, start_at, end_at = start_at) ⇒ Object Also known as: replace_at



748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 748

def insert_at(str, start_at, end_at = start_at)
  end_at, start_at = start_at, end_at if start_at > end_at
  if @max_length && @value.length - (end_at - start_at) + str.length > @max_length
    str = str[0, @max_length - @value.length + (end_at - start_at)]
    return if str.nil? # too long
  end

  @value.insert(start_at, end_at, str)
  @value_changed = true

  @selection_start += str.length
  @selection_end = @selection_start
end

#move_char_left ⇒ Object



898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 898

def move_char_left
  start_pos = @selection_start
  end_pos = @selection_end
  @selection_end = if @selection_end > @selection_start
                     @selection_start
                   elsif @selection_end < @selection_start
                     @selection_end
                   else
                     (@selection_start - 1).greater(0)
                   end
  @selection_start = @selection_end
  @cursor_moving = end_pos != @selection_start || start_pos != @selection_end
end

#move_char_right ⇒ Object



912
913
914
915
916
917
918
919
920
921
922
923
924
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 912

def move_char_right
  start_pos = @selection_start
  end_pos = @selection_end
  @selection_end = if @selection_end > @selection_start
                     @selection_end
                   elsif @selection_end < @selection_start
                     @selection_start
                   else
                     (@selection_start + 1).lesser(@value.length)
                   end
  @selection_start = @selection_end
  @cursor_moving = end_pos != @selection_start || start_pos != @selection_end
end

#move_to_end ⇒ Object



869
870
871
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 869

def move_to_end
  @selection_start = @selection_end = @value.length
end

#move_to_start ⇒ Object



861
862
863
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 861

def move_to_start
  @selection_start = @selection_end = 0
end

#move_word_left ⇒ Object



889
890
891
892
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 889

def move_word_left
  index = find_word_break_left
  @selection_start = @selection_end = find_word_break_left
end

#move_word_right ⇒ Object



894
895
896
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 894

def move_word_right
  @selection_start = @selection_end = find_word_break_right
end

#paste ⇒ Object



947
948
949
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 947

def paste
  insert($gtk.ffi_misc.getclipboard)
end

#rect ⇒ Object



951
952
953
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 951

def rect
  { x: @x, y: @y, w: @w, h: @h }
end

#scroll_rect ⇒ Object



959
960
961
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 959

def scroll_rect
  { x: @scroll_x, y: @scroll_y, w: @scroll_w, h: @scroll_h }
end

#select_all ⇒ Object



852
853
854
855
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 852

def select_all
  @selection_start = 0
  @selection_end = @value.length
end

#select_char_left ⇒ Object



881
882
883
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 881

def select_char_left
  @selection_end = (@selection_end - 1).greater(0)
end

#select_char_right ⇒ Object



885
886
887
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 885

def select_char_right
  @selection_end = (@selection_end + 1).lesser(@value.length)
end

#select_to_end ⇒ Object



865
866
867
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 865

def select_to_end
  @selection_end = @value.length
end

#select_to_start ⇒ Object



857
858
859
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 857

def select_to_start
  @selection_end = 0
end

#select_word_left ⇒ Object



873
874
875
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 873

def select_word_left
  @selection_end = find_word_break_left
end

#select_word_right ⇒ Object



877
878
879
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 877

def select_word_right
  @selection_end = find_word_break_right
end

#size_enum ⇒ Object



726
727
728
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 726

def size_enum
  @font_style.size_enum
end

#size_px ⇒ Object



730
731
732
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 730

def size_px
  @font_style.size_px
end

#tick ⇒ Object



690
691
692
693
694
695
696
697
698
699
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 690

def tick
  @value_changed = false
  @cursor_moving = false
  if @focussed
    prepare_special_keys
    handle_keyboard
  end
  handle_mouse
  prepare_render_target
end

#value_changed? ⇒ Boolean

Returns:

  • (Boolean)


706
707
708
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 706

def value_changed?
  @value_changed
end