lib/vedeu/cursor/move.rb in vedeu-0.4.24 vs lib/vedeu/cursor/move.rb in vedeu-0.4.25

- old
+ new

@@ -1,9 +1,9 @@ module Vedeu - # Adjusts the position of the cursor. To use this class, call the appropriate - # event: + # Adjusts the position of the cursor or view. To use this class, call the + # appropriate event: # # @example # Vedeu.trigger(:_cursor_down_) # # Vedeu.trigger(:_cursor_left_) # When a name is not given, the cursor in @@ -21,18 +21,32 @@ # Vedeu.trigger(:_cursor_origin_) # /or/ # Vedeu.trigger(:_cursor_origin_, 'my_interface') # # Moves the cursor to the top left of the # # named or current interface in focus. # + # Vedeu.trigger(:_view_down_, 'my_interface') + # Vedeu.trigger(:_view_left_, 'my_interface') + # Vedeu.trigger(:_view_right_, 'my_interface') + # Vedeu.trigger(:_view_up_, 'my_interface') + # # @note - # The cursor may not be visible, but it will still move if requested. - # The cursor will not exceed the border or boundary of the interface. + # The cursor or view may not be visible, but it will still move if + # requested. + # The cursor will not exceed the border or boundary of the interface, or + # boundary of the visible terminal. # The cursor will move freely within the bounds of the interface, # irrespective of content. + # The view will not exceed the boundary of the visible terminal. + # The view will move freely within the bounds of the interface, + # irrespective of content. class Move - # Move the named cursor, or that which is currently in focus in the + extend Forwardable + + def_delegators :geometry, :x, :xn, :y, :yn + + # Move the named cursor or view, or that which is currently in focus in the # specified direction. # # @param direction [Symbol] The direction of travel. Directions include: # (:down, :left, :right, :up, :origin). When ':origin' the cursor is moved # to the top left of the interface. @@ -44,47 +58,48 @@ name = name ? name : Vedeu.focus Vedeu::Move.send(direction, entity, name) end - # Moves the cursor down by one row. + # Moves the cursor or view down by one row. # # @param entity [Class] # @param name [String] # @return [Cursor] def self.down(entity, name) new(entity, name, 1, 0).move end - # Moves the cursor left by one column. + # Moves the cursor or view left by one column. # # @param entity [Class] # @param name [String] # @return [Cursor] def self.left(entity, name) new(entity, name, 0, -1).move end - # Moves the cursor right by one column. + # Moves the cursor or view right by one column. # # @param entity [Class] # @param name [String] # @return [Cursor] def self.right(entity, name) new(entity, name, 0, 1).move end - # Moves the cursor up by one row. + # Moves the cursor or view up by one row. # # @param entity [Class] # @param name [String] # @return [Cursor] def self.up(entity, name) new(entity, name, -1, 0).move end - # Moves the cursor to the top left coordinate of the interface. + # Moves the cursor or view to the top left coordinate of the interface or + # terminal screen. # # @param entity [Class] # @param name [String] # @return [Cursor] def self.origin(entity, name) @@ -92,38 +107,67 @@ end # Returns an instance of Vedeu::Move. # # @param entity [Class] - # @param name [String] The name of the cursor. + # @param name [String] The name of the cursor or view. # @param dy [Fixnum] Move up (-1), or down (1), or no action (0). # @param dx [Fixnum] Move left (-1), or right (1), or no action (0). # @return [Move] def initialize(entity, name, dy = 0, dx = 0) @entity = entity @name = name @dy = dy @dx = dx end - # Returns a newly positioned and stored Cursor. + # Returns a newly positioned and stored cursor or view. # - # @return [Cursor] + # @return [Vedeu::Cursor|Vedeu::Geometry] def move - attrs = attributes.merge!(new_attributes) + model = entity.new(merged_attributes).store - model = entity.new(attrs).store + refresh - if model.is_a?(Vedeu::Cursor) - Vedeu.trigger(:_refresh_cursor_, name) + model + end - else + # @return [void] + def refresh + if entity.to_s == 'Vedeu::Geometry' + Vedeu.trigger(:_clear_) + Vedeu.trigger(:_refresh_) + Vedeu.trigger(:_clear_, name) Vedeu.trigger(:_refresh_, name) + else + Vedeu.trigger(:_refresh_cursor_, name) + end + end - model + # @return [Hash<Symbol => Boolean,Fixnum, String>] + def merged_attributes + if entity.to_s == 'Vedeu::Geometry' + { + centred: false, + maximised: false, + name: name, + x: (x + dx), + y: (y + dy), + xn: (xn + dx), + yn: (yn + dy), + } + + else + cursor.attributes.merge!( + x: x_position, + y: y_position, + ox: ox, + oy: oy) + + end end protected # @!attribute [r] dx @@ -142,29 +186,10 @@ # @return [String] attr_reader :name private - # Retrieve the attributes of the cursor. - # - # @return [Hash<Symbol => Fixnum, String>] - def attributes - cursor.attributes - end - - # Provide the new attributes for the cursor. - # - # @return [Hash<Symbol => Fixnum>] - def new_attributes - { - x: x_position, - y: y_position, - ox: ox, - oy: oy, - } - end - # Returns the cursors x position based on its current offset. # # @return [Fixnum] def x_position coordinate.x_position(ox) @@ -183,9 +208,14 @@ end # @return (see Vedeu::Cursors#by_name) def cursor @cursor ||= Vedeu.cursors.by_name(name) + end + + # @return (see Vedeu::Geometries#by_name) + def geometry + @geometry ||= Vedeu.geometries.by_name(name) end # Apply the direction amount to the cursor offset. If the offset is less # than 0, correct to 0. #