Skip to content

My first program with CUIF

Salomon edited this page Dec 12, 2016 · 9 revisions

I will make things easy.. In a few lines you going to learn how create your first application that opens a Window that tells "Hello world!"... In addition with a button to finish the program.

Let me start:

/**
 * First, I am creating MyFirstApplication extending of abstract class
 * that defines default behaves and methods that make some stuffs for you.
 * The main() method is essencial. Like C is the start method.
 * 
 * Since I am opening a new window passing to it the 'name' parameter.
 */
class MyFirstApplication extends cuif\Application {
    public function main() {  // CUIF will call the main();
        $this->openWindow('MyFirstWindow', array('name'=>'Salomon')); // my name, change it for your.
    }
}

/**
 * Now I am creating MyFirstWindow that extends of a concrete class Window.
 * Is a best practise exending instead use Window. I let you use it directly for
 * special cases where not make sense specify a window subtype.
 * 
 * Notes how is easy create a label box and a button.
 * 
 * And finaly I want to higliht the event-driven design of CUIF, that allow you
 * control the flow of your progran listening for user interaction with any
 * visual element.
 * 
 * Pressing the button your fist application ends.
 */
class MyFirstWindow extends cuif\Window {
    public function init(array $params = array()) {
		$this->title = 'HELLO!';
		$this->height = 4;
		
        $this->createLabelBox(1, 0, 'Hello ' . $params['name'] . '!');
		$button = $this->createButton(1, 3, 'Hello CUIF!!');
		
		$button->bind('press', function() {
			$this->_application->end();
		});
    }
}

/**
 * The essential point to start... To an application runs and keep alive,
 * CUIF need make a some thinks such setup your console, 
 * listen the Standar Input, catch possible errors, and keep your program alive!
 * 
 * At this point dont worry about how CUIF make it. Just try it!
 */
cuif\CUIF::StartApplication('MyFirstApplication', false);

Congratulations! Your first application is running!

But, if you just want show an alert window don't need to write a new class. Application class improve a set of pre-built actions that aim to resolve common tasks, like alertWindow() method. Let Me show it:

class MyFirstApplication extends cuif\Application {
    public function main() {
        $this->alertWindow('Hello!', 'Hello Salomon!', function() {
            $this->end();
        });
    }
}

Note how in a simple method call you can resolve two problems:

  • open a simple window with their title and a message.
  • provide a listener for close window via callback.

Not only an alertWindow is improved by Application class. Please take a look for other methods:

  • promptWindow() Allow you ask something to user, and receive their response into a callback function.
  • confirmWindow() Similar to prompt, but in this case you provide callbacks for accept or cancel actions.
  • getActiveWindow() Returns the top screen window.
  • more...

Inspect Application class definition and discover that are ready for you. Methods name tell right what they do.

Clone this wiki locally