Skip to content

Instantly share code, notes, and snippets.

@aalin
Created April 3, 2010 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalin/354902 to your computer and use it in GitHub Desktop.
Save aalin/354902 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'glapp'
require 'text_draw'
class EyeTest
include GLApp
class Chooser
ALPHABET = ('A'..'Z').to_a + [' ']
def initialize
@choices = []
end
def pop
@choices.pop
end
def done?
current_characters.size == 1
end
def choice
raise "not done choosing" unless done?
current_characters.first
end
def choices
split_in_two(current_characters)
end
def has_choices?
!@choices.empty?
end
def choose(left_or_right)
@choices << left_or_right
end
private
def split_in_two(characters)
characters.partition { |c| characters.index(c) < characters.size / 2 }
end
def current_characters
@choices.inject(ALPHABET) do |chars, choice|
left, right = split_in_two(chars)
case choice
when :left
left
when :right
right
end
end
end
end
def setup
@message = []
@chooser = Chooser.new
end
def update(s)
if @chooser.done?
@message << @chooser.choice
@chooser = Chooser.new
end
end
def setup_context
glClearColor(0.0, 0.0, 0.0, 0.5)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
end
def pre_draw
glClear(GL_COLOR_BUFFER_BIT)
end
def draw
glMatrixMode(GL_PROJECTION)
glLoadIdentity
glOrtho(0, 200,
0, 200,
-100, 100)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity
glEnable(GL_LINE_SMOOTH)
glEnable(GL_POINT_SMOOTH)
glDepthMask(GL_FALSE)
glLineWidth(2.0)
glPointSize(3.0)
TextDraw.new("Message: " + @message.join).draw(10, 180)
left_chars, right_chars = @chooser.choices
TextDraw.new("Left:").draw(10, 60)
TextDraw.new(left_chars.to_s).draw(10, 40)
TextDraw.new("Right:").draw(100, 60)
TextDraw.new(right_chars.to_s).draw(100, 40)
end
def keyboard_down(key, special)
exit if key == 27
end
def special_keyboard_down(key, special)
case key
when 100 then @chooser.choose(:left) # Blink left
when 102 then @chooser.choose(:right) # Blink right
when 103 then pop # Blink both
end
end
private
def pop
if @chooser.has_choices?
@chooser.pop
else
@message.pop
end
end
end
EyeTest.new.show(1000, 1000)
{
'A' => [[ [0,0], [2,5], [4,0] ], [ [1,2], [3,2] ]],
'B' => [[ [0,0], [0,5], [2,5], [3,4], [2,3], [3,2], [3,1], [2,0], [0,0] ], [ [0,3], [2,3] ]],
'C' => [[ [4,1], [3,0], [1,0], [0,1], [0,4], [1,5], [3,5], [4,4] ]],
'D' => [[ [0,0], [0,5], [2,5], [3,4], [3,1], [2,0], [0,0] ]],
'E' => [[ [3,0], [0,0], [0,5], [3,5] ], [ [0,3], [2,3] ]],
'F' => [[ [0,0], [0,5], [3,5] ], [ [0,3], [2,3] ]],
'G' => [[ [2,2], [4,2], [4,1], [3,0], [1,0], [0,1], [0,4], [1,5], [3,5], [4,4] ]],
'H' => [[ [0,0], [0,5] ], [ [3,0], [3,5] ], [ [0,3], [3,3] ]],
'I' => [[ [1,0], [1,5] ], [ [0,0], [2,0] ], [ [0,5], [2,5] ]],
'J' => [[ [0,1], [1,0], [2,0], [3,1], [3,5] ]],
'K' => [[ [0,0], [0,5] ], [ [3,5], [0,3], [3,0] ]],
'L' => [[ [0,5], [0,0], [3,0] ]],
'M' => [[ [0,0], [0,5], [2,2], [4,5], [4,0] ]],
'N' => [[ [0,0], [0,5], [3,0], [3,5] ]],
'O' => [[ [0,1], [0,4], [1,5], [3,5], [4,4], [4,1], [3,0], [1,0], [0,1] ]],
'P' => [[ [0,0], [0,5], [2,5], [3,4], [3,3], [2,2], [0,2] ]],
'Q' => [[ [0,1], [0,4], [1,5], [3,5], [4,4], [4,1], [3,0], [1,0], [0,1] ], [ [2,2], [5,-1] ]],
'R' => [[ [0,0], [0,5], [2,5], [3,4], [3,3], [2,2], [0,2] ], [ [2,2], [3,0] ]],
'S' => [[ [0,1], [1,0], [2,0], [3,1], [3,2], [0,3], [0,4], [1,5], [2,5], [3,4] ]],
'T' => [[ [0,5], [4,5] ], [ [2,0], [2,5] ]],
'U' => [[ [0,5], [0,1], [1,0], [3,0], [4,1], [4,5] ]],
'V' => [[ [0,5], [2,0], [4,5] ]],
'W' => [[ [0,5], [1,0], [2,2], [3,0], [4,5] ]],
'X' => [[ [0,0], [4,5] ], [ [0,5], [4,0] ]],
'Y' => [[ [0,5], [2,3], [4,5] ], [ [2,3], [2,0] ]],
'Z' => [[ [0,5], [4,5], [0,0], [4,0] ]],
'a' => [[ [3,3], [3,0] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'b' => [[ [0,5], [0,0] ], [ [0,1], [1,0], [2,0], [3,1], [3,2], [2,3], [1,3], [0,2] ]],
'c' => [[ [3,2], [2,3], [1,3], [0,2], [0,1], [1,0], [2,0], [3,1] ]],
'd' => [[ [3,5], [3,0] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'e' => [[ [0,1], [3,2], [2,3], [1,3], [0,2], [0,1], [1,0], [2,0], [3,1] ]],
'f' => [[ [1,0], [1,4], [2,5] ], [ [0,3], [2,3] ]],
'g' => [[ [3,3], [3,-1], [2,-2], [1,-2], [0,-1] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'h' => [[ [0,5], [0,0] ], [ [0,3], [1,3], [2,2], [2,0] ]],
'i' => [[ [0,0], [0,3] ], [ [0,5] ]],
'j' => [[ [0,-1], [1,-2], [2,-1], [2,3] ], [ [2,5] ]],
'k' => [[ [0,0], [0,5] ], [ [2,3], [0,2], [2,0] ]],
'l' => [[ [0,0], [0,5] ]],
'm' => [[ [0,0], [0,3], [1,3], [2,2], [2,3], [3,3], [4,2], [4,0] ], [ [2,2], [2,0] ]],
'n' => [[ [0,0], [0,3], [1,3], [2,2], [2,0] ]],
'o' => [[ [3,2], [2,3], [1,3], [0,2], [0,1], [1,0], [2,0], [3,1], [3,2] ]],
'p' => [[ [0,-2], [0,3] ], [ [0,2], [1,3], [2,3], [3,2], [3,1], [2,0], [1,0], [0,1] ]],
'q' => [[ [3,-2], [3,3] ], [ [3,1], [2,0], [1,0], [0,1], [0,2], [1,3], [2,3], [3,2] ]],
'r' => [[ [0,0], [0,3] ], [ [0,2], [1,3], [2,2] ]],
's' => [[ [0,1], [0,0], [2,0], [2,1], [0,2], [0,3], [2,3], [2,2] ]],
't' => [[ [1,5], [1,1], [2,0] ], [ [0,3], [2,3] ]],
'u' => [[ [0,3], [0,1], [1,0], [2,1] ], [ [2,3], [2,0] ]],
'v' => [[ [0,3], [1,0], [2,3] ]],
'w' => [[ [0,3], [1,0], [2,3], [3,0], [4,3] ]],
'x' => [[ [0,3], [2,0] ], [ [0,0], [2,3] ]],
'y' => [[ [0,3], [1,0] ], [ [2,3], [1,0], [0,-2] ]],
'z' => [[ [0,3], [2,3], [0,0], [2,0] ]],
'0' => [[ [0,1], [0,4], [1,5], [2,5], [3,4], [3,1], [2,0], [1,0], [0,1] ], [ [2,4], [1,1] ]],
'1' => [[ [0,4], [1,5], [1,0] ], [ [0,0], [2,0] ]],
'2' => [[ [0,4], [1,5], [2,5], [3,4], [0,0], [3,0] ]],
'3' => [[ [0,4], [1,5], [2,5], [3,4], [2,3], [3,2], [3,1], [2,0], [1,0], [0,1] ]],
'4' => [[ [0,5], [0,3], [3,3] ], [ [3,5], [3,0] ]],
'5' => [[ [3,5], [0,5], [0,3], [2,3], [3,2], [3,1], [2,0], [1,0], [0,1] ]],
'6' => [[ [2,5], [0,2], [0,1], [1,0], [2,0], [3,1], [3,2], [2,3], [1,3], [0,2] ]],
'7' => [[ [0,5], [3,5], [1,0] ]],
'8' => [[ [0,4], [1,5], [2,5], [3,4], [3,3], [0,2], [0,1], [1,0], [2,0], [3,1], [3,2], [0,3], [0,4] ]],
'9' => [[ [1,0], [3,3], [3,4], [2,5], [1,5], [0,4], [0,3], [1,2], [2,2], [3,3] ]],
' ' => [],
'.' => [[ [0,0] ]],
':' => [[ [0,1] ], [[0,3]] ],
'?' => [[ [0,4], [1,5], [2,4], [2,3], [1,2] ], [ [1,0] ]]
}
class TextDraw
CHARACTERS = eval(File.read("font.rb"))
def initialize(string)
@string = string
end
def draw(x, y)
glPushMatrix
glLoadIdentity
glTranslate(x, y, 0)
glColor(1.0, 1.0, 1.0)
characters.each do |char|
draw_character(char)
glTranslate(character_width(char), 0, 0)
end
glPopMatrix
end
def width
characters.inject(0) do |sum, c|
sum + character_width(c)
end
end
protected
def characters
@string.scan(/./)
end
def draw_character(char)
polys = CHARACTERS[char] || CHARACTERS['?']
polys.each do |poly|
glBegin(poly.size == 1 ? GL_POINTS : GL_LINE_STRIP)
poly.each do |x, y|
glVertex(x, y)
end
glEnd
end
end
def character_width(char)
return 4 if char == " "
polys = CHARACTERS[char] || CHARACTERS['?']
xs = polys.map { |poly| poly.map { |x,y| x } }.flatten
xs.max - xs.min + 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment