Class: Input::Menu
- Inherits:
-
Object
- Object
- Input::Menu
- Defined in:
- mygame/lib/sprite_kit/vendor/input.rb
Constant Summary
Constants included from Keyboard
Keyboard::ALT_KEYS, Keyboard::CTRL_KEYS, Keyboard::IGNORE_KEYS, Keyboard::META_KEYS, Keyboard::SHIFT_KEYS
Instance Attribute Summary collapse
-
#content_h ⇒ Object
readonly
Returns the value of attribute content_h.
-
#content_w ⇒ Object
readonly
Returns the value of attribute content_w.
-
#items ⇒ Object
Returns the value of attribute items.
-
#readonly ⇒ Object
Returns the value of attribute readonly.
-
#scroll_h ⇒ Object
readonly
Returns the value of attribute scroll_h.
-
#scroll_w ⇒ Object
readonly
Returns the value of attribute scroll_w.
-
#scroll_x ⇒ Object
Returns the value of attribute scroll_x.
-
#scroll_y ⇒ Object
Returns the value of attribute scroll_y.
-
#selected_index ⇒ Object
Returns the value of attribute selected_index.
Attributes included from Keyboard
Instance Method Summary collapse
- #blur ⇒ Object
- #content_rect ⇒ Object
- #draw_override(ffi) ⇒ Object
- #focus ⇒ Object
- #focussed? ⇒ Boolean (also: #focused?)
- #handle_keyboard ⇒ Object
-
#handle_mouse ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#initialize(**params) ⇒ Menu
constructor
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
- #items_to_show(avail_h) ⇒ Object
-
#position ⇒ Object
TODO: implement positioning.
- #prepare_render_target ⇒ Object
-
#rect ⇒ Object
TODO: update @w and @h with @content_*.
- #scroll_rect ⇒ Object
- #size_height_to_fit ⇒ Object
- #size_to_fit ⇒ Object
- #size_width_to_fit ⇒ Object
- #tick ⇒ Object
- #value ⇒ Object
-
#value=(text) ⇒ Object
TODO: Fix menu value=.
Methods included from Keyboard
#initialize_keyboard, #prepare_special_keys
Methods included from Util
#parse_color, #parse_color_nilable, #position_in_window
Constructor Details
#initialize(**params) ⇒ Menu
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1684 def initialize(**params) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity @x = params[:x] || 0 @y = params[:y] || 0 @font_style = FontStyle.from(word_chars: ['W'], **params.slice(:font, :size_enum, :size_px)) @font_height = @font_style.font_height @padding = params[:padding] || 2 self.items = params[:items] # self.items so that items= is called @w = params[:w] || size_width_to_fit @h = params[:h] || size_height_to_fit @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 @selected_index = params[:selected_index] || 0 @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) # Mouse focus for selection @mouse_down = false @mouse_wheel_speed = params[:mouse_wheel_speed] || @font_height # Render target for text scrolling @path = "__input_m_#{object_id}" @scroll_x = 0 @scroll_y = 0 @content_w = @w @content_h = @h @scroll_x = 0 @scroll_y = 0 @scroll_w = @w @scroll_h = @h @focussed = params[:focussed] || false @will_focus = false # Get the focus at the end of the tick @on_selected = params[:on_selected] || NOOP @on_unhandled_key = params[:on_unhandled_key] || NOOP initialize_keyboard(params) end |
Instance Attribute Details
#content_h ⇒ Object (readonly)
Returns the value of attribute content_h.
1681 1682 1683 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1681 def content_h @content_h end |
#content_w ⇒ Object (readonly)
Returns the value of attribute content_w.
1681 1682 1683 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1681 def content_w @content_w end |
#items ⇒ Object
Returns the value of attribute items.
1681 1682 1683 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1681 def items @items end |
#readonly ⇒ Object
Returns the value of attribute readonly.
1682 1683 1684 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1682 def readonly @readonly end |
#scroll_h ⇒ Object (readonly)
Returns the value of attribute scroll_h.
1681 1682 1683 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1681 def scroll_h @scroll_h end |
#scroll_w ⇒ Object (readonly)
Returns the value of attribute scroll_w.
1681 1682 1683 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1681 def scroll_w @scroll_w end |
#scroll_x ⇒ Object
Returns the value of attribute scroll_x.
1682 1683 1684 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1682 def scroll_x @scroll_x end |
#scroll_y ⇒ Object
Returns the value of attribute scroll_y.
1682 1683 1684 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1682 def scroll_y @scroll_y end |
#selected_index ⇒ Object
Returns the value of attribute selected_index.
1681 1682 1683 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1681 def selected_index @selected_index end |
Instance Method Details
#blur ⇒ Object
1828 1829 1830 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1828 def blur @focussed = false end |
#content_rect ⇒ Object
1978 1979 1980 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1978 def content_rect { x: @scroll_x, y: @scroll_y, w: @content_w, h: @content_h } end |
#draw_override(ffi) ⇒ Object
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1733 def draw_override(ffi) return if @items.length == 0 # 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, @content_w, @content_h, @path, 0, 255, 255, 255, 255, nil, nil, nil, nil, false, false, 0, 0, 0, 0, @content_w, @content_h ) return unless @will_focus @will_focus = false @focussed = true end |
#focus ⇒ Object
1824 1825 1826 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1824 def focus @will_focus = true end |
#focussed? ⇒ Boolean Also known as: focused?
1819 1820 1821 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1819 def focussed? @focussed end |
#handle_keyboard ⇒ Object
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1770 def handle_keyboard # TODO: home, pgup, end, pgdn # TODO: optional find by text if @down_keys.include?(:up) || @down_keys.include?(:up_arrow) @selected_index = (@selected_index - 1).clamp_wrap(0, @items.length - 1) elsif @down_keys.include?(:down) || @down_keys.include?(:down_arrow) @selected_index = (@selected_index + 1).clamp_wrap(0, @items.length - 1) else @on_unhandled_key.call(@down_keys.first, self) end end |
#handle_mouse ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1782 def handle_mouse # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity return # mouse = $args.inputs.mouse # inside = mouse.inside_rect?(self) # @scroll_y += mouse.wheel.y * @mouse_wheel_speed if mouse.wheel && inside # 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_clicked.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 |
#items_to_show(avail_h) ⇒ Object
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1941 def items_to_show(avail_h) # TODO: line spacing in menu num_items = (avail_h / @font_height).floor.lesser(@items.length) - 1 half_num_items = num_items.idiv(2) if half_num_items > @selected_index [0, num_items + 1] elsif half_num_items > @items.length - @selected_index - (num_items.even? ? 1 : 2) [@items.length - num_items - 1, num_items + 1] else [@selected_index - half_num_items, num_items + 1] end end |
#position ⇒ Object
TODO: implement positioning
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1955 def position if @y + @content_h > @args.grid.h # over the top? if @y - @content_h < 0 # also down under? @y = @args.grid.h - @content_h # move down so we fit else @y -= @content_h # render down end end if @x + @content_w > @args.grid.w # too far? if @x - @content_w < 0 # also too left? @x = $args.grid.w - @content_w # move left so we fit else @x -= @content_w # render left end end end |
#prepare_render_target ⇒ Object
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1896 def prepare_render_target # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity l = @items.length return if l == 0 if @focussed || @will_focus bg = @background_color sc = @selection_color else bg = @blurred_background_color sc = @blurred_selection_color end # TODO: Attach menu to another widget so we space above of below # TODO: A bounding box? Assuming grid for now grid = @args.grid avail_h = if @y - @h > 0 || @y + @h < grid.h # can we render fully below or above? @h else # ok, we gotta scroll @y.greater(grid.h - @y) end first_item, num_items = items_to_show(avail_h) @content_h = num_items * @font_height @content_w = @w rt = @args.outputs[@path] rt.w = @content_w rt.h = @content_h rt.background_color = bg rt.transient! # TODO: implement mouse scrolling @scroll_w = @content_w @scroll_h = @items.length * @font_height + 2 * @padding @scroll_x = 0 @scroll_y = first_item * @font_height i = -1 while (i += 1) < num_items y = (num_items - i - 1) * @font_height rt.primitives << { x: 0, y: y, w: @content_w, h: @font_height, path: :solid }.merge!(sc) if @selected_index == first_item + i rt.primitives << @font_style.label(x: @padding, y: y, text: @items[first_item + i].text, **@text_color) end position end |
#rect ⇒ Object
TODO: update @w and @h with @content_*
1974 1975 1976 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1974 def rect { x: @x, y: @y, w: @content_w, h: @content_h } end |
#scroll_rect ⇒ Object
1982 1983 1984 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1982 def scroll_rect { x: @scroll_x, y: @scroll_y, w: @scroll_w, h: @scroll_h } end |
#size_height_to_fit ⇒ Object
1864 1865 1866 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1864 def size_height_to_fit @h = @font_height * @items.length + @padding * 2 end |
#size_to_fit ⇒ Object
1852 1853 1854 1855 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1852 def size_to_fit size_width_to_fit size_height_to_fit end |
#size_width_to_fit ⇒ Object
1857 1858 1859 1860 1861 1862 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1857 def size_width_to_fit @w = @items.reduce(0) do |max, item| iw = @font_style.string_width(item.text) + @padding * 2 iw > max ? iw : max end end |
#tick ⇒ Object
1761 1762 1763 1764 1765 1766 1767 1768 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1761 def tick if @focussed prepare_special_keys handle_keyboard end handle_mouse prepare_render_target end |
#value ⇒ Object
1868 1869 1870 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1868 def value @items[@selected_index].value end |
#value=(text) ⇒ Object
TODO: Fix menu value=
1873 1874 1875 1876 1877 1878 |
# File 'mygame/lib/sprite_kit/vendor/input.rb', line 1873 def value=(text) # text = text[0, @max_length] if @max_length # @value.replace(text) # @selection_start = @selection_start.lesser(text.length) # @selection_end = @selection_end.lesser(text.length) end |