Created
June 18, 2012 00:29
-
-
Save cha0s/2946163 to your computer and use it in GitHub Desktop.
TexTy UI 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# jQuery. | |
$ = jQuery | |
# The slot where the next guessed letter will fall. | |
guessSlot = 0 | |
# The prefix necessary to keep the letter lists centered. | |
prefix = 2 | |
# Triggered when a guess is submitted. | |
submitGuess = -> | |
# Drop out early if there are no letters selected. | |
$guessLetters = $('#guess .guessed') | |
return if $guessLetters.length is 0 | |
# Get the current guess. | |
guess = '' | |
$guessLetters.each -> guess += $(this).text() | |
# Reset the scrambled and guess letters. | |
$('#letters .letter') | |
.stop(true, true) | |
.removeClass('guessed') | |
.css('opacity', 1) | |
$('#guess .letter') | |
.stop(true, true) | |
.removeClass('guessed') | |
.text('') | |
guessSlot = 0 | |
# Only actually submit words that are 3 or more letters. | |
return if guess.length < 3 | |
alert guess | |
# Triggered when a letter is selected. | |
selectLetter = (index) -> | |
# Check if the letter has already been guessed. | |
$letter = $('#letters .letter').eq index | |
return if $letter.hasClass 'guessed' | |
# If not, start fading it out. | |
$letter.addClass 'guessed' | |
$letter.animate {opacity: 0}, 100 | |
# Insert the letter into the next available guessed letter slot. | |
$guessElement = $('#guess .letter').eq guessSlot++ | |
$guessElement.text $letter.text() | |
# Fade it in. | |
$guessElement.addClass 'guessed' | |
$guessElement.css 'opacity', 0 | |
$guessElement.animate {opacity: 1}, 100 | |
# Triggered when the letters are shuffled around. | |
shuffleLetters = -> | |
# Remove the leading margin. | |
$('#letters .letter:first').removeClass('prefix_' + prefix) | |
# Shuffle the letters. | |
$('#letters').shuffle() | |
# Put the leading margin back. | |
$('#letters .letter:first').addClass('prefix_' + prefix) | |
# DOM event handlers. | |
$(document).ready -> | |
# Catch keypresses. | |
$('body').keypress (e) -> | |
# Save keystrokes. | |
event = e.originalEvent | |
# Keep track if we ate the event. | |
ate = false | |
# First differentiate keycodes. | |
switch event.keyCode | |
# Enter key submits a guess. | |
when 13 | |
submitGuess() | |
ate = true | |
# Otherwise... | |
else | |
# Look at the character code. | |
switch event.charCode | |
# Space key shuffles the letters. | |
when 32 | |
shuffleLetters() | |
ate = true | |
# Otherwise... | |
else | |
# Get the textual representation of the character code | |
# normalized to upper case. | |
letter = String.fromCharCode( | |
event.charCode | |
).toUpperCase() | |
return if letter <= 'A' or letter >= 'Z' | |
# Get the index of the first matching letter that | |
# hasn't already been guessed. | |
index = $(' | |
#letters .letter:contains(' + letter + ') | |
:not(.guessed) | |
:first | |
').index() | |
return if index is -1 | |
# Select it. | |
selectLetter index | |
ate = true | |
return not ate | |
# Clicking the yin yang shuffles the letters. | |
$('#shuffleLetters').click -> shuffleLetters() | |
# Clicking the check mark submits a guess. | |
$('#submitGuess').click -> submitGuess() | |
# Clicking on a letter selects it. | |
$('#letters .letter').each -> $(this).click -> selectLetter $(this).index() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment