Skip to content

Commit

Permalink
initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed May 27, 2009
0 parents commit a72bc19
Show file tree
Hide file tree
Showing 15 changed files with 1,235 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
Raffle.app
*/.DS_Store
4 changes: 4 additions & 0 deletions Rakefile
@@ -0,0 +1,4 @@
require 'hotcocoa/application_builder'
require 'hotcocoa/standard_rake_tasks'

task :default => [:run]
8 changes: 8 additions & 0 deletions config/build.yml
@@ -0,0 +1,8 @@
name: Raffle
load: lib/application.rb
version: "1.0"
icon: resources/sdruby.icns
resources:
- resources/**/*.*
sources:
- lib/**/*.rb
120 changes: 120 additions & 0 deletions lib/application.rb
@@ -0,0 +1,120 @@
require 'HotCocoa'
include HotCocoa
require File.join(File.dirname(__FILE__), 'raffle')
require File.join(File.dirname(__FILE__), 'stylist')
require File.join(File.dirname(__FILE__), 'models', 'participants_store')

class RaffleApplication

attr_reader :current_view, :main_window, :about_button, :winner_field, :participants
attr_accessor :cached_views

## Class methods
def self.register(view_class)
view_classes << view_class
end

def self.view_classes
@view_classes ||= []
end

def self.cached_views
@cached_views ||= {}
end

# Find a view using a description
def self.view_with_description(description)
cached_views[description] ||= @view_classes.detect {|view| view.description == description}
end

def self.about_button
@about_button
end

def participants
@participants ||= ::ParticipantsStore.new([{first_name: "Matt", last_name: "Aimonetti"}])
end

## Instance methods
def start
load_demo_files
application :name => "Raffle" do |app|
app.delegate = self

@main_window = window :frame => [100, 100, 604, 500], :title => "SDRuby Raffle", :style => [:titled, :closable, :miniaturizable, :resizable] do |win|
win.contentView.margin = 0
win.background_color = color(:name => 'white')
@logo = image_view(:frame => [0,0,604,100], :layout => {:start => false})
@logo.file = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "resources", "sdruby.png"))
win << @logo
win.will_close { exit }
end

get_started
end
end

# file/open
def on_open(menu)
end

# file/new
def on_new(menu)
end

# help menu item
def on_help(menu)
end

# This is commented out, so the minimize menu item is disabled
#def on_minimize(menu)
#end

# window/zoom
def on_zoom(menu)
end

# window/bring_all_to_front
def on_bring_all_to_front(menu)
end

def get_started
@winner_field = label(:text => "SDRuby Raffle #{Time.now.strftime('%Y-%m-%d')}", :layout => {:start => false, :align => :left}, :frame => [0, 0, 600, 90])
Stylist.large_style(@winner_field)

winner_view = layout_view :frame => [0, 0, 600, 80], :layout => {:start => false}, :margin => 10, :spacing => 0 do |view|
view << @winner_field
end

about_and_winner = layout_view(:frame => [0, 0, 0, 60], :mode => :horizontal, :layout => {:expand => :width, :align => :left}) do |hview|
# @about_button = button(:title => "About", :frame => [0, 0, 70, 30], :bezel => :regular_square, :layout => {:align => :right}, :on_action => Proc.new { display_view("About SDRuby Raffle") })
@winner_button = button(:title => "Pick A winner", :frame => [0, 0, 170, 30], :bezel => :regular_square, :layout => {:align => :right}, :on_action => Proc.new { display_view("Pick a winner") })
@add_participants = button(:title => "Add Participants", :frame => [0, 0, 170, 30], :bezel => :regular_square, :layout => {:align => :right}, :on_action => Proc.new { display_view("Participants") })
# hview << @about_button
hview << @add_participants
hview << @winner_button
end

main_window << winner_view
#display_view("About SDRuby Raffle")
main_window << about_and_winner
end

def display_view(description)
main_window.view.remove(current_view) if current_view
@current_view = RaffleApplication.view_with_description(description).create(self)
main_window << @current_view
end

private

def load_demo_files
Dir.glob(File.join(File.dirname(__FILE__), 'views', '*.rb')).each do |file|
load file
end
end

end

APP = RaffleApplication.new
APP.start
32 changes: 32 additions & 0 deletions lib/menu.rb
@@ -0,0 +1,32 @@
module HotCocoa
def application_menu
menu do |main|
main.submenu :apple do |apple|
apple.item :about, :title => "About #{NSApp.name} (click here!)", :on_action => proc { speech_synthesizer.speak('Welcome to SDRuby Raffle Application written by Matt Aimonetti.') }
apple.separator
apple.item :preferences, :key => ","
apple.separator
apple.submenu :services
apple.separator
apple.item :hide, :title => "Hide #{NSApp.name}", :key => "h"
apple.item :hide_others, :title => "Hide Others", :key => "h", :modifiers => [:command, :alt]
apple.item :show_all, :title => "Show All"
apple.separator
apple.item :quit, :title => "Quit #{NSApp.name}", :key => "q"
end
main.submenu :file do |file|
file.item :new, :key => "n"
file.item :open, :key => "o"
end
main.submenu :window do |win|
win.item :minimize, :key => "m"
win.item :zoom
win.separator
win.item :bring_all_to_front, :title => "Bring All to Front", :key => "o"
end
main.submenu :help do |help|
help.item :help, :title => "#{NSApp.name} Help"
end
end
end
end
22 changes: 22 additions & 0 deletions lib/models/participants_store.rb
@@ -0,0 +1,22 @@
class ParticipantsStore < HotCocoa::TableDataSource

def add_participant(sender, participant)
# alert(:message => "You need to fill up the form") unless participant.keys.size == 3
NSLog(participant.inspect)
@data << participant
sender.reloadData
end

def tableView(view, setObjectValue:object, forTableColumn:column, row:index)
participant = @data[index]
case column.identifier
when 'first_name'
participant[:first_name] = object
when 'last_name'
participant[:last_name] = object
when 'email'
participant[:email] = object
end
end

end
71 changes: 71 additions & 0 deletions lib/raffle.rb
@@ -0,0 +1,71 @@
class Array

def randomize
a = self.dup
result = []
self.length.times do
result << a.slice!(rand(a.length))
end
return result
end

def randomize!
a = self.dup
result = []
self.length.times do
result << a.slice!(rand(a.length))
end
self.replace result
end

end

class Raffle

attr_accessor :participants, :mode

def initialize(participants = [])
@participants = participants
@mode = 'teasing'
end

def pick_a_winner
srand Time.now.to_i
winner = participants[rand(participants.length)]
remove_participant(winner)
if @mode == 'teasing'
tease_the_loosers(winner)
"congratulations #{winner}"
else
winner
end
end

def teasing_mode
@mode = 'teasing'
end

def normal_mode
@mode = 'normal'
end

protected

def tease_the_loosers(winner)
p "...and the winner is..."
@participants.randomize!
@participants.each do |looser|
looser(looser)
end
p "... #{winner} ... YAY!"
end

def looser(name)
sleep 0.5
p "... #{name} ... NOT"
end

def remove_participant(participant)
@participants = @participants.reject{|ppt| ppt == participant}
end
end
13 changes: 13 additions & 0 deletions lib/stylist.rb
@@ -0,0 +1,13 @@
class Stylist

def self.large_style(obj)
obj.setFont font(:name => "Monaco", :size => 36)
obj.setTextColor color(:red => 0.170, :green => 0.15, :blue => 0.19)
end

def self.small_style(obj)
obj.setFont font(:name => "Monaco", :size => 24)
obj.setTextColor color(:red => 0.170, :green => 0.15, :blue => 0.19)
end

end
19 changes: 19 additions & 0 deletions lib/views/about_view.rb.retired
@@ -0,0 +1,19 @@
framework 'webkit'

class AboutView

def self.description
"About SDRuby Raffle"
end

def self.create(parent=nil)

layout_view :frame => [0, 0, 0, 0], :layout => {:expand => [:width, :height]}, :margin => 0, :spacing => 0 do |view|
site = web_view(:layout => {:expand => [:width, :height]}, :url => "http://sdruby.com")
view << site
view << label(:text => "This Application was written by Matt Aimonetti using MacRuby 0.4")
end
end

RaffleApplication.register(self)
end
21 changes: 21 additions & 0 deletions lib/views/intro_view.rb
@@ -0,0 +1,21 @@
class IntroView

def self.description
"Intro"
end

def self.create(parent=nil)
layout_view :frame => [10, 0, 300, 300], :layout => {:expand => [:width, :height]}, :margin => 10, :spacing => 0 do |view|
@run_button = button(:title => "Pick a winner", :bezel => :regular_square, :layout => {:start => false}, :on_action => Proc.new{parent.display_view("Pick a winner")})
@add_participants = button(:title => "Add Participants", :bezel => :regular_square, :layout => {:start => false}, :on_action => Proc.new{parent.display_view("Participants")})

Stylist.small_style(parent.winner_field)
parent.winner_field.text = "SDRuby Raffle #{Time.now.strftime('%Y-%m-%d')}" if parent
view << @run_button
view << @add_participants
end
end

RaffleApplication.register(self)

end
51 changes: 51 additions & 0 deletions lib/views/participants.rb
@@ -0,0 +1,51 @@
class Participants

def self.description
"Participants"
end

def self.create(parent=nil)

layout_view :frame => [0, 0, 0, 0], :layout => {:expand => [:width, :height]}, :margin => 0, :spacing => 0 do |view|
view << scroll_view(:layout => {:expand => [:width, :height]}) do |scroll|
@tv = table_view(
:columns => [
column(:id => :first_name, :title => "First Name"),
column(:id => :last_name, :title => "Last Name"),
column(:id => :email, :title => "Email")
]
)
@tv.data = APP.participants
scroll << @tv
end

new_field = layout_view :frame => [0, 0, 0, 0], :layout => {:expand => [:width, :height]}, :margin => 0, :spacing => 0 do |new_participant|
fields_layout = layout_view(:frame => [0, 0, 0, 60], :mode => :horizontal, :layout => {:expand => :width, :start => false, :layout => :left}) do |hview|
hview << @email = text_field(:text => "", :frame => [0,0,110,25], :layout => {:start => false, :align => :left, :right_padding => 10})
hview << label(:text => "Email: ", :frame => [0,0,50,25], :layout => {:start => false, :align => :left, :right_padding => 10, :left_padding => 10})
hview << @last_name = text_field(:text => "", :frame => [0,0,110,25], :layout => {:start => false, :align => :left, :right_padding => 10})
@email.nextResponder = @last_name
hview << label(:text => "Last Name: ", :frame => [0,0,75,25], :layout => {:start => false, :align => :left, :right_padding => 10, :left_padding => 10})
hview << @first_name = text_field(:text => "", :frame => [0,0,110,25], :layout => {:start => false, :align => :left, :right_padding => 10})
hview << label(:text => "First Name: ", :frame => [0,0,75,25], :layout => {:start => false, :align => :left})

parent.main_window.makeFirstResponder @first_name
@first_name.nextKeyView = @last_name
@last_name.nextKeyView = @email
end
new_participant << fields_layout
new_participant << @add = button(:title => "Add", :frame => [0, 0, 70, 30], :bezel => :regular_square, :on_action => Proc.new { APP.participants.add_participant(@tv, {:first_name => @first_name.stringValue, :last_name => @last_name.stringValue, :email => @email.stringValue}) })
@email.nextKeyView = @add

# new_participant << button(:title => "Save Participant List",
# :frame => [0, 0, 40, 30],
# :bezel => :regular_square,
# :on_action => Proc.new { APP.participants })
end

view << new_field
end
end

RaffleApplication.register(self)
end

0 comments on commit a72bc19

Please sign in to comment.