Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mquickform committed Dec 2, 2011
0 parents commit 7e6dcc9
Show file tree
Hide file tree
Showing 16 changed files with 441 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
main.js
1 change: 1 addition & 0 deletions .tmbuild
@@ -0,0 +1 @@
/Users/mb/Sites/Cube/build.hxml
120 changes: 120 additions & 0 deletions Main.hx
@@ -0,0 +1,120 @@
package ;

/*
Trying to create a mediator that does not exist, should throw exception at runtime.
agent.mediatorMap.createMediator( controlsView );
*/

#if flash9
import flash.display.MovieClip;
import flash.events.MouseEvent;
import sample.FlashContext;
#elseif js
import sample.view.BodyView;
import sample.view.ControlsView;
import jsSample.JSContext;
#end
import sample.view.BlueBoxView;
import sample.view.RedBoxView;
import sample.events.BoxEvent;
import xirsys.cube.events.AgentEvent;
import xirsys.cube.events.IEvent;

#if flash9
class Main extends MovieClip {
private var agent : FlashContext;

public static function main() {
var app = new Main();
flash.Lib.current.addChild( app );
}

public function new() {
super();
agent = new FlashContext( this, false );
agent.addEventHandler( AgentEvent.STARTUP_COMPLETE, handleStartup );
agent.initiate();
}

private function handleStartup( evt : IEvent ) : Void {
var blue = new BlueBoxView();
agent.container.addChild( blue );
agent.mediatorMap.createMediator( blue );
blue.visible = false;

var red = new RedBoxView();
agent.container.addChild( red );
agent.mediatorMap.createMediator( red );

var redBtn = new MovieClip();
redBtn.graphics.beginFill( 0xFF0000 );
redBtn.graphics.drawRect( 0, 0, 50, 50 );
redBtn.graphics.endFill();
redBtn.y = 160;
addChild( redBtn );
redBtn.addEventListener( MouseEvent.CLICK, handleRedClick );

var blueBtn = new MovieClip();
blueBtn.graphics.beginFill( 0x0000FF );
blueBtn.graphics.drawRect( 0, 0, 50, 50 );
blueBtn.graphics.endFill();
blueBtn.y = 160;
blueBtn.x = 60;
addChild( blueBtn );
blueBtn.addEventListener( MouseEvent.CLICK, handleBlueClick );
}
#elseif js
class Main {
private var agent : JSContext;

public static function main() {
haxe.Firebug.redirectTraces();
var app = new Main();
}

public function new() {
agent = new JSContext( new BodyView(), false );
agent.addEventHandler( AgentEvent.STARTUP_COMPLETE, handleStartup );
agent.initiate();
}

private function handleStartup( evt : IEvent ) : Void {
var blue = new BlueBoxView( js.Lib.document.createElement( "div" ) );
agent.container.view.appendChild( blue.view );
agent.mediatorMap.createMediator( blue );
blue.visible = false;

var red = new RedBoxView( js.Lib.document.createElement( "div" ) );
agent.container.view.appendChild( red.view );
agent.mediatorMap.createMediator( red );

var redBtn : js.Dom.Button = cast js.Lib.document.createElement( "input" );
redBtn.type = "button";
redBtn.value = "Red";
redBtn.onclick = handleRedClick;
agent.container.view.appendChild( redBtn );

var blueBtn : js.Dom.Button = cast js.Lib.document.createElement( "input" );
blueBtn.type = "button";
blueBtn.value = "Blue";
blueBtn.onclick = handleBlueClick;
agent.container.view.appendChild( blueBtn );

var controlsView = new ControlsView( js.Lib.document.createElement("div") );
agent.container.view.appendChild(controlsView.view);
agent.mediatorMap.createMediator( controlsView );
}
#end

private function handleRedClick( evt )
{
agent.eventDispatcher.dispatch( BoxEvent.SHOW_BOX, new BoxEvent( "red" ) );
}

private dynamic function handleBlueClick( evt )
{
agent.eventDispatcher.dispatch( BoxEvent.SHOW_BOX, new BoxEvent( "blue" ) );
}
}
9 changes: 9 additions & 0 deletions bin/index.html
@@ -0,0 +1,9 @@
<html>
<head>
<script src="main.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<script type="text/javascript" charset="utf-8">
Main.main();
</script>
</body>
3 changes: 3 additions & 0 deletions build.hxml
@@ -0,0 +1,3 @@
Main
-lib xirsys_cube
-js bin/main.js
31 changes: 31 additions & 0 deletions jsSample/JSContext.hx
@@ -0,0 +1,31 @@
package jsSample;

import xirsys.cube.core.Agent;
import js.Dom.HtmlDom;
import sample.events.BoxEvent;
import xirsys.cube.events.IEvent;
import xirsys.cube.events.AgentEvent;
import sample.controller.ShowBoxCommand;
import sample.view.BlueBoxView;
import sample.view.BlueBoxViewMediator;
import sample.view.RedBoxView;
import sample.view.RedBoxViewMediator;
import sample.view.ControlsView;
import sample.view.ControlsViewMediator;

class JSContext extends Agent<Dynamic,IEvent> {

public function new( container : Dynamic, autoStart : Bool )
{
super( container, autoStart );
}

override public function initiate()
{
mediatorMap.mapView( BlueBoxView, BlueBoxViewMediator );
mediatorMap.mapView( RedBoxView, RedBoxViewMediator );
mediatorMap.mapView(ControlsView, ControlsViewMediator);
commandMap.mapEvent( BoxEvent.SHOW_BOX, ShowBoxCommand, BoxEvent );
dispatch( AgentEvent.STARTUP_COMPLETE, null );
}
}
28 changes: 28 additions & 0 deletions sample/FlashContext.hx
@@ -0,0 +1,28 @@
package sample;

import xirsys.cube.core.Agent;
import flash.display.MovieClip;
import sample.events.BoxEvent;
import xirsys.cube.events.IEvent;
import xirsys.cube.events.AgentEvent;
import sample.controller.ShowBoxCommand;
import sample.view.BlueBoxView;
import sample.view.BlueBoxViewMediator;
import sample.view.RedBoxView;
import sample.view.RedBoxViewMediator;

class FlashContext extends Agent<MovieClip,IEvent> {

public function new( container : MovieClip, autoStart : Bool )
{
super( container, autoStart );
}

override public function initiate()
{
mediatorMap.mapView( BlueBoxView, BlueBoxViewMediator );
mediatorMap.mapView( RedBoxView, RedBoxViewMediator );
commandMap.mapEvent( BoxEvent.SHOW_BOX, ShowBoxCommand, BoxEvent );
dispatch( AgentEvent.STARTUP_COMPLETE, null );
}
}
20 changes: 20 additions & 0 deletions sample/controller/ShowBoxCommand.hx
@@ -0,0 +1,20 @@
package sample.controller;

import xirsys.cube.mvcs.Command;
import sample.events.BoxEvent;

class ShowBoxCommand extends Command {
@Inject
public var evt : BoxEvent;

override public function execute()
{
switch( evt.boxType )
{
case "red":
eventDispatcher.dispatch( BoxEvent.SHOW_RED_BOX );
case "blue":
eventDispatcher.dispatch( BoxEvent.SHOW_BLUE_BOX );
}
}
}
14 changes: 14 additions & 0 deletions sample/events/BoxEvent.hx
@@ -0,0 +1,14 @@
package sample.events;

class BoxEvent implements xirsys.cube.events.IEvent {
public static var SHOW_RED_BOX : String = "showRedBox";
public static var SHOW_BLUE_BOX : String = "showBlueBox";
public static var SHOW_BOX : String = "showBox";

public var boxType : String;

public function new( type : String )
{
boxType = type;
}
}
38 changes: 38 additions & 0 deletions sample/view/BlueBoxView.hx
@@ -0,0 +1,38 @@
package sample.view;

#if flash9
class BlueBoxView extends flash.display.MovieClip {
public function new()
{
super();
var g = graphics;
g.beginFill( 0x000099 );
g.drawRect( 0, 0, 150, 150 );
g.endFill();
}
}

#elseif js

import js.Dom;
import js.Dom.HtmlDom;
class BlueBoxView {

public var view : js.HtmlDom;
public var visible ( null, setVisible ) : Bool;

public function new( view )
{
this.view = view;
view.style.width = "150";
view.style.height = "150";
view.style.backgroundColor = "#0000FF";
}

public function setVisible( show : Bool ) : Bool
{
view.style.display = ( show ) ? "" : "none";
return show;
}
}
#end
26 changes: 26 additions & 0 deletions sample/view/BlueBoxViewMediator.hx
@@ -0,0 +1,26 @@
package sample.view;

import sample.view.BlueBoxView;
import sample.events.BoxEvent;

class BlueBoxViewMediator extends xirsys.cube.mvcs.Mediator {
@Inject
public var view : BlueBoxView;

override public function onRegister()
{
super.onRegister();
eventDispatcher.addEventHandler( BoxEvent.SHOW_BLUE_BOX, showBox );
eventDispatcher.addEventHandler( BoxEvent.SHOW_RED_BOX, hideBox );
}

private function showBox( e )
{
view.visible = true;
}

private function hideBox( e )
{
view.visible = false;
}
}
12 changes: 12 additions & 0 deletions sample/view/BodyView.hx
@@ -0,0 +1,12 @@
package sample.view;

import js.Dom;
import js.Dom.HtmlDom;

class BodyView {
public var view : HtmlDom;

public function new() {
view = js.Lib.document.body;
}
}
37 changes: 37 additions & 0 deletions sample/view/ControlsView.hx
@@ -0,0 +1,37 @@
package sample.view;

import js.Lib;
import js.Dom;
import js.Dom.HtmlDom;

class ControlsView {

public var view : js.HtmlDom;
public var button1 : Button;
public var button2 : Button;

public function new( view )
{
this.view = view;
button1 = cast Lib.document.createElement("input");
button1.type = "button";
button1.value = "Button1";
this.view.appendChild(button1);

button2 = cast Lib.document.createElement("input");
button2.type = "button";
button2.value = "Button2";
this.view.appendChild(button2);
}

public function disableButton1():Void {
button1.disabled = true;
button2.disabled = false;
}

public function disableButton2():Void {
button1.disabled = false;
button2.disabled = true;
}

}
37 changes: 37 additions & 0 deletions sample/view/ControlsViewMediator.hx
@@ -0,0 +1,37 @@
package sample.view;
import js.Dom;

import sample.view.ControlsView;
import sample.events.BoxEvent;

class ControlsViewMediator extends xirsys.cube.mvcs.Mediator {
@Inject
public var view : ControlsView;

override public function onRegister()
{
super.onRegister();
eventDispatcher.addEventHandler( BoxEvent.SHOW_RED_BOX, disableButton1 );
eventDispatcher.addEventHandler( BoxEvent.SHOW_BLUE_BOX, disableButton2 );

view.button1.onclick = handleButtonClick;
view.button2.onclick = handleButtonClick;
}

private function handleButtonClick(e:js.Event):Void {
if (e.target == view.button1) {
eventDispatcher.dispatch( BoxEvent.SHOW_BOX, new BoxEvent("red") );
} else if (e.target == view.button2) {
eventDispatcher.dispatch( BoxEvent.SHOW_BOX, new BoxEvent("blue") );
}
}

private function disableButton1(e) {
view.disableButton1();
}

private function disableButton2(e) {
view.disableButton2();
}

}

0 comments on commit 7e6dcc9

Please sign in to comment.