A
download sgl-button.rb
Language: Ruby
Copyright: (C) 2004-2005 Kouichirou Eto, All rights reserved.
LOC: 114
Project Info
etocom project(etocom)
Server: RubyForge (SVN)
Type: svn
...s\REL‑start_newgem\sgl\sgl\
   cocoa-app.rb
   cocoa-color.rb
   cocoa-draw.rb
   cocoa-event.rb
   cocoa-media.rb
   cocoa-notuse.rb
   cocoa-window.rb
   cocoa.rb
   make-release.rb
   opengl-app.rb
   opengl-color.rb
   opengl-draw.rb
   opengl-event.rb
   opengl-modules.rb
   opengl-window.rb
   opengl.rb
   qp.rb
   sgl-bass.rb
   sgl-button.rb
   sgl-spring.rb
   test-cocoa-app.rb
   test-opengl-app.rb

# Copyright (C) 2004-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH.unshift("..") if !$LOAD_PATH.include?("..")
require "sgl/opengl"
require "sgl/sgl-spring"

class Button
  def initialize(x, y)
    @dx, @dy = x, y
    @x, @y = x, y
    @w, @h = 8, 8
  end

  attr_accessor :x, :y

  def pos
    return [x, y]
  end

  def move_to(x, y)
    @x, @y = x, y
  end

  def draw(s)
    colorHSV(0, s, 100, 60)
    draw_rect(@w, @h)
    colorHSV(0, s,   0, 30)
    draw_rect(@w-1, @h-1)
  end

  def draw_rect(w, h)
    rect(@x-w, @y-h, @x+w, @y+h)
  end

  def inside?(x, y)
    (@x-@w) < x && x < (@x+@w) && (@y-@h) < y && y < (@y+@h)
  end
end

class ButtonColumn
  def initialize(length, dy, hy)
    @length, @dy, @hy = length, dy, hy #destination y and hide y
    @cur = 0
    @buttons = []
    @y_spring = NumSpring.new(@dy, @dy, 5, 0.6)
    make_buttons
  end

  attr_reader :y_spring
  attr_accessor :cur

  def make_buttons
    y = @y_spring.x
    sw = 46
    w = 0
    @length.times {
      @buttons << Button.new(0, y)
      w += sw
    }
    x = -w/2
    @buttons.each {|b|
      b.x = x
      x += sw
    }
  end

  def move(x, y)
    # @y_spring.target = y.abs < (768/2 - 120) ? @hy : @dy
    #if y.abs < (768/2 - 120)
    #qp @dy, y, (@dy - y), (@dy - y).abs
    if (@dy - y).abs < 50
      @y_spring.target = @dy
    else
      @y_spring.target = @hy
    end
    @y_spring.moving = true
    @y_spring.move
    y = @y_spring.x
    @buttons.each {|b|
      b.y = y
    }
  end

  def draw
    @buttons.each_index {|i|
      b = @buttons[i]
      s = (@cur == i) ? 100 : 0
      b.draw(s)
    }
  end

  def onMouseDown(x, y)
    @buttons.each_index {|i|
      b = @buttons[i]
      if b.inside?(x, y)
	@cur = i
	return i
      end
    }
    nil
  end
end

if $0 == __FILE__
  require "test/unit"
  $test = true
end

if defined?($test) && $test
  class TestSglButton < Test::Unit::TestCase
    def test_button
      b = Button.new(1, 2)
      assert_equal(1, b.x)
      assert_equal(2, b.y)
      assert_equal([1, 2], b.pos)
    end

    def test_draw
      app = SGL::Application.new
      app.set_setup {
	app.window(100, 100)
	app.runtime = 0.1
      }
      i = 0
      button = Button.new(0, 0)
      app.set_display {
	button.move_to(i, i)
	button.draw(i)
	i += 5
	i = 0 if 100 < i
      }
      app.mainloop
    end
  end
end

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us