Giter Club home page Giter Club logo

robotlegs-starling-plugin's Introduction

robotlegs-starling-plugin

This plugin adds some classes to Robotlegs to support usage with the Starling framework. Because the Starling framework uses Stage3D and not the flash.display.DisplayList a StarlingContext must be used along with a StarlingMediator. The rest of the framework usage remains the same.

Pull Requests are encouraged! Please if you add/fix anything contribute it back! :)

Added Classes

  • StarlingMediatorMap.as
  • StarlingViewMap.as
  • StarlingViewMapBase.as
  • IStarlingMediatorMap.as
  • IStarlingViewMap.as
  • StarlingContext.as
  • StarlingMediator.as
  • StarlingCommand.as

Usage Example

Below are some excerpts from a very simple usage example. The .fxp file for the example file is in the Downloads section. Below are the key parts.

The main class set up remains the same:

package
{
	import com.example.MyGame;

	import flash.display.Sprite;
	import flash.events.Event;

	import org.robotlegs.mvcs.StarlingContext;

	import starling.core.Starling;

	public class Main extends Sprite
	{
		private var _starling:Starling;
		private var _starlingContext:StarlingContext;

		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		protected function onAddedToStage(event:Event):void
		{
			_starling = new Starling(MyGame, stage);
			_starling.start();
		}
	}
}

Inside the main Starling class (MyGame) sent into the first Starling constructor argument start a subclass of a StarlingContext instance. Usage of the StarlingContext is identical to the default Robotlegs Context class.

package com.example
{
	import flash.utils.setTimeout;

	import org.robotlegs.mvcs.StarlingContext;

	import starling.display.Sprite;
	import starling.events.Event;

	public class MyGame extends Sprite
	{
		private var _starlingContext:StarlingContext;

		public function MyGame()
		{
			super();

			_starlingContext = new MyStarlingGameContext(this);

			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(event:Event):void
		{
			var secondView:SecondView = new SecondView();
			addChild(secondView);

			// Test mediator removal
			setTimeout(secondView.parent.removeChild, 3000, secondView);
		}
	}
}

Contents of the MyStarlingGameContext class (StarlingContext subclass):

package com.example
{
	import org.robotlegs.mvcs.StarlingContext;

	import starling.display.DisplayObjectContainer;

	public class MyStarlingGameContext extends StarlingContext
	{
		public function MyStarlingGameContext(contextView:DisplayObjectContainer=null, autoStartup:Boolean=true)
		{
			super(contextView, autoStartup);
		}

		override public function startup():void
		{
			mediatorMap.mapView(MyGame, MyGameMediator);
			mediatorMap.mapView(SecondView, SecondViewMediator);
			
			commandMap.mapEvent(FlashEvent.EVENT_NAME, EventCommand);

			super.startup();
		}
	}
}

Contents of the EventCommand (StarlingCommand sub-class, alternatively you can no subclass anything and declare a public execute() method, but do not use the default Robotlegs Command class, it will crash.)

package com.example
{
	import org.robotlegs.mvcs.StarlingCommand;

	public class EventCommand extends StarlingCommand
	{
		public function EventCommand()
		{
			super();
		}

		override public function execute():void
		{
			trace("EventCommand.execute()");
		}
	}
}

Alternate EventCommand class declaration (you dont get access to default things you might be used to with the default Robotlegs command class, like the injector, mediator map, etc):

package com.example
{
	public class EventCommand
	{
		public function EventCommand()
		{
		}

		public function execute():void
		{
			trace("EventCommand.execute()");
		}
	}
}

And finally, the contents of one of the mediators (SecondViewMediator):

package com.example
{
	import org.robotlegs.mvcs.StarlingMediator;

	public class SecondViewMediator extends StarlingMediator
	{
		public function SecondViewMediator()
		{
			super();
		}

		override public function onRegister():void
		{
			trace("SecondViewMediator.onRegister()");
		}

		override public function onRemove():void
		{
			trace("SecondViewMediator.onRemove()");
		}
	}
}

robotlegs-starling-plugin's People

Contributors

joshtynjala avatar niggeulimann avatar s9tpepper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

robotlegs-starling-plugin's Issues

Command crashes because of missing IMediatorMap injection

The default Robotlegs Command object does not work with the Starling plugin because it is trying to inject a IMediatorMap and it uses a contextView declared as flash.display.DisplayObjectContainer. Updating the plugin to have its own StarlingCommand object so it injects the appropriate objects and expects the correct type of contextView.

AutoMediation doesn't work for nested views

I've tracked the issue down to the fact that Starling doesn't bubble ADDED_TO_STAGE, ADDED, REMOVED, REMOVED_FROM_STAGE events.

That coupled with the StarlingMediatorMap expecting all the events to be coming through the contextView breaks automediation if you add a DisplayObject to a DisplayObjectContainer already on the stage.

The solution I've come up with to work around it for now is to added a listen(dispatcher:DisplayObjectContainer) function on the StarlingMediatorMap. This then calls a modified addListeners passing in the dispatcher.

A bit more manual than I'd like but it seems to work.

MediatorMap doesn't map View Interfaces

So I have a problem with map View Interfaces.

if we try to map view as Interface:
mediatorMap.mapView(IIntro, IntroMediator);
function onRegister will not be called
onRegister only was called with Starling Sprite classes but not with some Interfaces, maybe Interface should implement some special starling Interface?!

StarlingContext runtime error in eventMap getter

Using the latest code the StarlingContext has a runtime error in the eventMap getter.

TypeError: Error #1034: Type Coercion failed: cannot convert to org.robotlegs.core.IStarlingEventMap.
at org.robotlegs.mvcs::StarlingContext/get eventMap()[...\org\robotlegs\mvcs\StarlingContext.as:295]
at org.robotlegs.mvcs::StarlingContext/mapInjections()[...\org\robotlegs\mvcs\StarlingContext.as:325]
at org.robotlegs.mvcs::StarlingContext()[...\org\robotlegs\mvcs\StarlingContext.as:135]

StarlingActor required?

There is no StarlingActor class so if creating Actors that dispatch events flash.events.XXX is required.

This is fine in theory within the Actor (my model) but within the mediators that listen I have starling and flash event types.

Do you think this is a required addition?

TouchEvent.TOUCH can work in StarlingMediator?

hi, I try to make a demo use starling with robotlegs. but i got an error when i want to add a touchevent in starlingMediator,the view is a starling sprite. code as follow:

eventMap.mapListener(view,TouchEvent.TOUCH,clickHandler);

the error:

Error: Implicit coercion of a value of type view:AnimationSwitcher to an unrelated type flash.events:IEventDispatcher.

how can i fix this error?

regard.

SecondViewMediator.onRegister() is never triggered

Hi,
thanks for your robotlegs port for Starling. I still have troubles get your example running properly.
I only get log output for "MyGameMediator.onRegister()". The SecondViewMediator is never instantiated.

Do you have an idea what's going wrong?

Thanks,

Christoph

Mediator is mapped for the second time before it was removed

If I have 2 views (say ViewA with MediatorA and ViewB with MediatorB), and i switch between them like this: ViewA -> ViewB -> ViewA I get a warning:

Warning: Injector already has a rule for type "MediatorA", named "".
If you have overwritten this mapping intentionally you can use "injector.unmap()" prior to your replacement mapping in order to avoid seeing this message.

I have found that the reason for this is in the next line in StarlingMediatorMap:

setTimeout(removeMediatorLater, 500, null);

(See https://github.com/s9tpepper/robotlegs-starling-plugin/blob/master/src/org/robotlegs/base/StarlingMediatorMap.as#L317)

missing LICENSE and AUTHORS file

Copyright (c) 2009 the original author or authors
Permission is hereby granted to use, modify, and distribute this file in accordance with the terms of the license agreement accompanying it.

The above bit of text implies I should find a LICENSE and AUTHORS file but I didn't see either at the top level folder. Please include both so I know how your software is licensed :)

Thanks!

Add a custom StarlingEventMap implementation

Here is what the robotlegs documentation says on the mediators' eventMap:

Mediators are equipped with an EventMap that has a method mapListener(). This method registers each event added to the Mediator, and ensures that the event is removed when the mediator is unregistered from the framework. It is important to remove events in Flash, as events that are added, but not removed from a class eliminate the Player’s ability to perform runtime Garbage Collection on that class. It is possible to add your event listeners with the traditional Flash syntax, but be aware that you will also need to remove them manually as well.

So, it is an important and neat feature. However, everywhere in the standard implementation eventMap relies heavily on the fact that eventDispatcher passed to the "mapEvent" method implements the "flash.events.IEventDispatcher" interface. Which is not implemented by the starling.events.EventDispatcher class.

Because of this you can't really use eventMap.mapListener in my mediators which means you can't do much at all.

I first thought to tweak starling's EventDispatcher quickly just so it would implement the interface, but the signatures there are really different.

So, if anybody would have time and willingness to add the StarlingEventMap and any other custom specific implementations necessary to make this plugin complete - that'd be great.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.