Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RingList with shifting elements replacing Array

  • Loading branch information...
commit d66c48acb6e04429a68acd2a8ff3f467ceb602d8 1 parent b598758
@erisdev authored
Showing with 40 additions and 26 deletions.
  1. +25 −26 lib/ring_menu.rb
  2. +15 −0 lib/ring_menu/ring_list.rb
View
51 lib/ring_menu.rb
@@ -1,7 +1,8 @@
require 'chingu'
class RingMenu < Chingu::GameState
- autoload :Icon, 'ring_menu/icon'
+ autoload :Icon, 'ring_menu/icon'
+ autoload :RingList, 'ring_menu/ring_list'
module Z
BACKGROUND = 0
@@ -29,7 +30,8 @@ def initialize options = {}, &block
@opaque = options[:opaque]
@rotation = options[:icon_rotation]
- @radius = options[:radius]
+ @x_radius = options[:x_radius] || options[:radius]
+ @y_radius = options[:y_radius] || options[:radius]
@z_base = options[:z_base]
@cx = options[:x] || $window.width / 2
@@ -41,8 +43,7 @@ def initialize options = {}, &block
@caption = Chingu::Text.new '',
:zorder => @z_base + Z::CAPTION
- @items = []
- @index = 0
+ @items = RingList[]
@step = 0
# FIXME when chingu is updated, just pass :center => 0.5
@@ -102,30 +103,28 @@ def font name, height
end
end
+ # utility methods
+
+ def close_enough?; @step.abs < 0.1 end
+
# input methods
- def left?; left! if @index == @step end
+ def left?; left! if close_enough? end
def left!
- @index -= 1
- if @index < 0
- @index += @count
- @step += @count
- end
+ @items.rotate! +1
+ @step -= 1
update_caption!
end
- def right?; right! if @index == @step end
+ def right?; right! if close_enough? end
def right!
- @index += 1
- if @index >= @count
- @index -= @count
- @step -= @count
- end
+ @items.rotate! -1
+ @step += 1
update_caption!
end
def perform_action
- @items[@index].perform_action
+ @items.first.perform_action
end
# chingu methods
@@ -134,11 +133,11 @@ def update
super
# update rotation step
- if (@step - @index).abs < 0.1
- @step = @index
- elsif @step < @index
+ if close_enough?
+ @step = 0
+ elsif @step < 0
@step += 0.1
- elsif @step > @index
+ elsif @step > 0
@step -= 0.1
end
@@ -149,13 +148,13 @@ def update
# position cursor
if @cursor
@cursor.x = @cx
- @cursor.y = @cy - @radius
+ @cursor.y = @cy - @y_radius
end
# position icons
@items.each do |icon|
- icon.x = @cx + @radius * Math.sin(this_angle)
- icon.y = @cy - @radius * Math.cos(this_angle)
+ icon.x = @cx + @x_radius * Math.sin(this_angle)
+ icon.y = @cy - @y_radius * Math.cos(this_angle)
# turn icons if desired
icon.angle = @rotation * this_angle.radians_to_degrees
@@ -165,7 +164,7 @@ def update
end
def update_caption!
- @caption.text = @items[@index].title
+ @caption.text = @items.first.title
@caption.x = @cx
@caption.y = @cy
end
@@ -184,7 +183,7 @@ def draw
@cursor.draw if @cursor
# draw caption if selected item is at the front
- @caption.draw if @index == @step
+ @caption.draw if close_enough?
end
end
View
15 lib/ring_menu/ring_list.rb
@@ -0,0 +1,15 @@
+class RingMenu
+ class RingList < Array
+
+ def rotate! n = 1
+ if n >= 0 then push *shift(n)
+ else unshift *pop(-n)
+ end
+ end
+
+ def rotate n
+ clone.rotate! n
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.