Many are familer with CGI::Application and CGI::Prototype. Each of these can be seen as a way to define a set of states, with the user transitioning between them. However, they are both intimately tied to a web application framework. Though web applications is what I usually get paid to do, I've recently had the need for a more generic framework.
Thus, I'm developing a state transition module for user interfaces. In it, you define a module which handles that state and what other states the current state is allowed to jump to.
Here's a quick demonstration of an application that starts with a 'menu' state, and then allows the user to change the program's options, start a new file, or open an existing file. You can exit at any time from each state, except for Options.
use UI::State; my $state = UI::State->new; # Define the states we will use $state->add_states( qw/ menu new open options / ); $state->state( menu => { # Class that handles this state class => 'My::UI::Menu', # States allowed to jump to. The 'exit' # state is always implicitly available. next => [qw/ new open options exit /], }); $state->state( new => { class => 'My::UI::New', next => [qw/ menu exit /], }); $state->state( open => { class => 'My::UI::Open', next => [qw/ menu exit /], }); $state->state( options => { class => 'My::UI::Options', next => [qw/ menu /], }); # Error! State 'foo' was not declared in add_states() $state->state( foo => { class => 'My::UI::Foo', next => [qw/ menu exit /], }); $state->state( new => { class => 'My::UI::New', # Error! State 'bar' was not declared in add_states() next => [qw/ menu bar exit /], }); # Start running from 'menu' $state->run( 'menu' );
The run() method starts running a loop until one of the states returns exit. The exit state is always available, but can be overriden with your own cleanup code if needed--just create it with the state() method like any other state.
Each module is required to have a new() method which returns a blessed object, and a run() method which recieves the last state used. You perform whatever actions are needed within the state, and then return one of the states the module is allowed to return. Returning any other state is an error.
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Generic State Transition UI Library
by dragonchild (Archbishop) on Mar 15, 2005 at 15:04 UTC | |
by hardburn (Abbot) on Mar 15, 2005 at 15:19 UTC | |
by BUU (Prior) on Mar 17, 2005 at 16:13 UTC | |
|
Re: Generic State Transition UI Library
by metaperl (Curate) on Mar 15, 2005 at 18:41 UTC | |
by hardburn (Abbot) on Mar 16, 2005 at 20:04 UTC | |
|
Re: Generic State Transition UI Library
by zengargoyle (Deacon) on Mar 17, 2005 at 20:28 UTC |