<commit_before>require 'chingu'

class RingMenu < Chingu::GameState
  class Icon < Chingu::GameObject
    attr_accessor :title
    
    def initialize title, image, options = {}, &block
      super options.merge(:image => image)
      @title  = title
      @action = block
      puts "Created #{self} (#{title}) -> #{block}"
    end
    
    def perform_action
      @action[self]
    end
  end
  
  DEFAULTS = { :radius => 50 }
  
  def initialize options = {}, &block
    super
    
    options = DEFAULTS.merge options
    @radius = options[:radius]
    
    @items = []
    @index = 0
    @step  = 0
    
    self.input = {
      :left  => :left!,
      :right => :right!,
      :return => :act!
    }
    
    yield self if block_given?
  end
  
  def item caption, image, options = {}, &block
    # FIXME when chingu is updated, this should just use the create method
    @items << Icon.new(caption, image, options, &block)
    add_game_object @items.last
  end
  
  def left!
    @index -= 1
    @index  = @items.count - 1 if @index < 0
  end
  
  def right!
    @index += 1
    @index  = 0 if @index >= @items.count
  end
  
  def act!
    @items[@index].perform_action
  end
  
  def update
    super
    
    cx = $window.width / 2
    cy = $window.height / 2
    
    angle_step = 2 * Math::PI / @items.count
    angle      = angle_step * @index
    @items.each do |icon|
      icon.x = cx + @radius * Math.sin(angle)
      icon.y = cy - @radius * Math.cos(angle)
      angle += angle_step
    end
  end
  
end<commit_msg>animate ring rotation<commit_after>require 'chingu'

class RingMenu < Chingu::GameState
  class Icon < Chingu::GameObject
    attr_accessor :title
    
    def initialize title, image, options = {}, &block
      super options.merge(:image => image)
      @title  = title
      @action = block
    end
    
    def perform_action
      @action[self]
    end
    
  end
  
  DEFAULTS = { :radius => 50 }
  
  def initialize options = {}, &block
    super
    
    options = DEFAULTS.merge options
    @radius = options[:radius]
    
    @items = []
    @index = 0
    @step  = 0
    
    self.input = {
      :left  => :left!,
      :right => :right!,
      :return => :act!
    }
    
    yield self if block_given?
  end
  
  def item caption, image, options = {}, &block
    # FIXME when chingu is updated, this should just use the create method
    @items << Icon.new(caption, image, options, &block)
    add_game_object @items.last
    @count = @items.count
  end
  
  def left!
    @index -= 1
    if @index < 0
      @index += @count
      @step  += @count
    end
  end
  
  def right!
    @index += 1
    if @index >= @count
      @index -= @count
      @step  -= @count
    end
  end
  
  def act!
    @items[@index].perform_action
  end
  
  def update
    super
    
    # update rotation step
    if (@step - @index).abs < 0.1
      @step = @index
    elsif @step < @index
      @step += 0.1
    elsif @step > @index
      @step -= 0.1
    end
    
    cx = $window.width / 2
    cy = $window.height / 2
    
    angle_diff = 2 * Math::PI / @count
    this_angle = angle_diff * @step
    
    @items.each do |icon|
      icon.x = cx + @radius * Math.sin(this_angle)
      icon.y = cy - @radius * Math.cos(this_angle)
      this_angle += angle_diff
    end
  end
  
end
{'fraction_non_alphanumeric': 0.11775362318840579, 'fraction_numerical': 0.008454106280193236, 'mean_word_length': 1.9215167548500882, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2046899', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1115, 'n_words': 317}