Nick wrote:
>Really stupid question, but have you wrapped this all in an App class? If
you don''t have a
>wxApp subclass driving your applications then all sorts of badness would
happen.
>
>
Yep, It is: Below is my code thus far. I am a big fan of refactoring so
it ain''t pretty yet!!
Here are my main classes: GameUI, Game, GameWriter, PlayLog and Play.
When a Game starts (is created) it starts the GameUI. The GameUI runs
GameUI.start which prompts the user with the modal dialog. This is where
my problem lies, in the ok_btn event handler.
-------------START CODE----------------
#!/usr/bin/ruby
require ''wxruby''
class GameUI < Wx::App
@@APP_TITLE = "Rayder Football Game Tracker";
MENU_NEWGAME = 1000;
MENU_EXIT = 1001;
def on_init( )
@file_menu = Wx::Menu.new();
@file_menu.append( MENU_NEWGAME , "&New Game...\t" , "New
Game" );
@file_menu.append( MENU_EXIT , "E&xit...\t" , "Exit"
);
@menubar = Wx::MenuBar.new();
@menubar.append( @file_menu , "File" );
@frame = Wx::Frame.new( nil , -1 , @@APP_TITLE , Wx::Point.new( 20 ,
0 ) , Wx::Size.new( 800 , 600 ) );
@frame.set_menu_bar( @menubar );
self.gen_top( @top_panel = Wx::Panel.new( @frame , -1 ) );
@bottom_panel = Wx::Panel.new( @frame , -1 );
sizer = Wx::BoxSizer.new(Wx::VERTICAL);
sizer.add( @top_panel, 2, Wx::GROW|Wx::ALL, 2);
sizer.add( @bottom_panel , 1 , Wx::GROW|Wx::ALL, 2 );
@frame.set_sizer( sizer );
@frame.show( true );
GameUI.start( @@game );
end
def GameUI.filler( parent )
return Wx::StaticText.new( parent , -1 ); end
def GameUI.font_label
return Wx::Font.new( 12 , Wx::DEFAULT , Wx::NORMAL , Wx::BOLD ); end
def GameUI.font_header( size=15 );
#Wx::Font constructor( point size , font family , font style , font
weight , underlining? , face name , encoding )
return Wx::Font.new( size , Wx::DEFAULT , Wx::NORMAL , Wx::BOLD ); end
def game_label=( lbl )
@game_label.set_label( lbl ); end
def GameUI.new_game( game )
@@game = game;
return GameUI.new(); end
def gen_top( parent )
y = 2;
##ROW 1
@game_label = Wx::StaticText.new( parent , -1 , "Charlevoix vs. East
Jordan" , Wx::Point.new( 2 , y ) );
@game_label.set_font( GameUI.font_header );
##ROW 2
y = 52;
posession_label = Wx::StaticText.new( parent , -1 , "Posession :"
,
Wx::Point.new( 2 , y ) );
posession_label.set_font( GameUI.font_label );
posession_text = Wx::TextCtrl.new( parent , -1 , "" ,
Wx::Point.new(
125, y ) , Wx::Size.new( 200 , 22 ) );
down_label = Wx::StaticText.new( parent , -1 , "Down : " ,
Wx::Point.new( 640 , y ) );
down_label.set_font( GameUI.font_label );
down_cb = Wx::ComboBox.new( parent , -1 , "" , Wx::Point.new( 700
,
y ) , Wx::Size.new( 70 , 22 ) );
down_cb.append( "1" );
down_cb.append( "2" );
down_cb.append( "3" );
down_cb.append( "4" );
down_cb.append( "kick/punt" );
##ROW 3
y = 92;
position_label = Wx::StaticText.new( parent , -1 , "Field Position
:" , Wx::Point.new( 2 , y ) );
position_label.set_font( GameUI.font_label );
position_cb = Wx::ComboBox.new( parent , -1 , "" , Wx::Point.new(
125 , y ) , Wx::Size.new( 50 , 22 ) );
position_cb.append( "Our" );
position_cb.append( "Their" );
position_cb.set_selection( 0 );
position_text = Wx::TextCtrl.new( parent , -1 , "" ,
Wx::Point.new(
175 , y ) , Wx::Size.new( 35 , 22 ) );
position_label2 = Wx::StaticText.new( parent , -1 , " yard line."
,
Wx::Point.new( 215 , y ) );
position_label2.set_font( GameUI.font_label );
##ROW 4
y = 132;
playtype_label = Wx::StaticText.new( parent , -1 , "Play Type : "
,
Wx::Point.new( 2 , y ) );
playtype_label.set_font( GameUI.font_label );
playtype_cb = Wx::ComboBox.new( parent , -1 , "" , Wx::Point.new(
125 , y ) , Wx::Size.new( 75 , 22 ) );
playtype_cb.append( "Run" );
playtype_cb.append( "Pass" );
playtype_cb.append( "QB Sneak" );
playtype_cb.append( "Kick" );
playtype_cb.append( "Punt" );
playtype_cb.append( "Field Goal" );
playtype_cb.append( "Interception" );
playtype_cb.set_selection( 0 );
##Row 5
y = 202;
results_label = Wx::StaticText.new( parent , -1 , "Play Results" ,
Wx::Point.new( 2 , y ) );
results_label.set_font( GameUI.font_header );
##Row 5
y = 242;
player_label = Wx::StaticText.new( parent , -1 , "Player : " ,
Wx::Point.new( 2 , y ) );
player_label.set_font( GameUI.font_label );
player_text = Wx::TextCtrl.new( parent , -1 , "" , Wx::Point.new(
125 , y ) , Wx::Size.new( 50 , 22 ) );
player_label2 = Wx::StaticText.new( parent , -1 , "yards." ,
Wx::Point.new( 185 , y ) );
player_label2.set_font( GameUI.font_label );
end
def gen_bottom
end
def GameUI.start( game )
width = 300; height = 130;
popup = Wx::Dialog.new( @frame , -1 , "Who''s Our
Opponent" ,
Wx::Point.new( 300 , 300 ) , Wx::Size.new( width , height ));
##ROW 1
label = Wx::StaticText.new( popup , -1 , "Who is playing the
Rayders?" , Wx::Point.new( 30 , 5 ));
label.set_font( GameUI.font_header( 12 ) );
##ROW 2
input_text = Wx::TextCtrl.new( popup , -1 , "<enter opponent name
here>" , Wx::Point.new( 40 , 35 ) , Wx::Size.new( 200 , 22 ) );
##ROW 3
cancel_btn = Wx::Button.new( popup , -1 , "&Cancel" ,
Wx::Point.new(
150 , 75 ) , Wx::Size.new( 60 , 25 ) );
ok_btn = Wx::Button.new( popup , -1 , "&Ok" , Wx::Point.new(
225 ,
75 ) , Wx::Size.new( 60 , 25 ) );
##BIND EVENTS TO BUTTONS, quit if cancel button is pressed
cancel_btn.evt_button( cancel_btn.get_id() ){ |e| exit(0); }
ok_btn.evt_button( ok_btn.get_id() ){
opponent = input_text.get_value();
#@game_label.set_label( "Charlevoix vs. #{opponent}" );
game_label="here";
game.set_teams( "Charlevoix" , opponent );
popup.end_modal( Wx::ID_OK ); }
popup.show_modal();
end
end
class Game
@@gameui = nil;
def initialize( home=nil , away=nil )
if( !home ) then
@@gameui = GameUI.new_game( self );
else
self.set_teams( home , away ); end
@possesion = nil;
GameWriter.initialize(); end
def Game.posession=( team )
@possesion = team; end
def run( play )
GameWriter.write( play.run() ); end
def set_teams( team1 , team2 )
(@home_team, @away_team) = team1, team2; end
def Game.over
GameWriter.close(); end
def over
Game.over(); end
end
class GameWriter
def GameWriter.close
@@file.close(); end
def GameWriter.initialize
@@file = File.new( "report.txt" , "w+" );
end
def GameWriter.write( playlog )
PlayLog.get_fields().each{ |field|
@@file.write( "#{playlog.fields_hsh[ field ]}\t" ); }
@@file.write( "\n" ); end
end
class PlayLog
@@fields = nil;
attr_reader :fields_hsh
def initialize( playlog_hsh )
@fields_hsh = {};
@@fields.each{ |val| @fields_hsh[ val ] = playlog_hsh[ val ]; } end
def PlayLog.get_fields
return @@fields; end
def PlayLog.set_fields( fields_array )
@@fields = fields_array; end
end
class Play
def initialize( team )
@team = team;
Game.posession = @team; end
def run
return PlayLog.new(); end
end
##
# Kickoff is a subclass of Play. It runs the Kickoff play for the game.
class Kickoff < Play
def initialize( team )
super( team ); end
def run
pl_hsh = { "Field Position" => "",
"Down" => "",
"Distance To Go" => "",
"Play Type" => "Kick Off",
"Result In Yards" => "#{@team} kicks 60 yard kick",
"Result In Down" => "",
"Player" => "Kicker #25",
"Posession" => @team }
return PlayLog.new( pl_hsh ); end
end
##
# Return is a subclass of Play that happens after a Kick or Punt.
class Return < Play
def initialize( team )
super( team ); end
def run
pl_hsh = { "Field Position" => "#{@team} 20yd. line",
"Down" => "1st",
"Distance To Go" => "10",
"Play Type" => "Return",
"Result In Yards" => "#{@team} returns 10 yards",
"Result In Down" => "3yd. gain",
"Player" => "#35",
"Posession" => @team }
return PlayLog.new( pl_hsh ); end
end
##
# RunPlay
class Run < Play
def initialize( team )
super( team ); end
def run
pl_hsh = { "Field Position" => "#{@team} 20yd. line",
"Down" => "2nd",
"Distance To Go" => "10",
"Play Type" => "Run Play",
"Result In Yards" => "#{@team} returns 10 yards",
"Result In Down" => "3yd. gain",
"Player" => "#35",
"Posession" => @team }
return PlayLog.new( pl_hsh ); end
end
PlayLog.set_fields( [ "Field Position",
"Down",
"Distance To Go",
"Play Type",
"Result In Yards",
"Result In Down",
"Player",
"Posession" ] );
# game1 = Game.new( "Charlevoix" , "East Jordan" );
# game1.run( Kickoff.new( "Charlevoix" ) );
# game1.run( Return.new( "East Jordan" ) );
# game1.run( Run.new( "East Jordan" ) );
# game1.over();
game = Game.new();