PDA

View Full Version : Use an image file path with CursorManager


d000hg
01-09-2009, 06:12 PM
I'm using Flex3, I want to work with cursors but my problem is CursorManager.setCursor() wants the cursor specified as a Class. It's not convenient for me to embed all the images, I want to dynamically use a file path like "resources/cursor/upCursor.png".

Is this possible? I think I've seen it done but can't find it, or think what to search for.

aktell
01-09-2009, 09:08 PM
I'm using Flex3, I want to work with cursors but my problem is CursorManager.setCursor() wants the cursor specified as a Class. It's not convenient for me to embed all the images, I want to dynamically use a file path like "resources/cursor/upCursor.png".

Is this possible? I think I've seen it done but can't find it, or think what to search for.

Hi there,

If I understand this right than I believe you would find everything what you need in the HELP section of your FLEX3 - just put in 'CursorManager'.
I'm using 'CursorManager' quiet extensively on my site http://www.webflashartistry.com for the visual effect! But for better Image/Module download and Image intervals in a FLEX Image gallery I incorporate a lot other code to make it work. regards aktell

d000hg
01-09-2009, 11:14 PM
I can't find anything in LiveDocs, all the examples on the web are like this:

[Embed("ui/cursor/MyCursor.PNG")]private var cursor:Class;
public function init(obj:DisplayObject)
{

obj.addEventListener(MouseEvent.MOUSE_OVER,
function(e:MouseEvent):void{
CursorManager.setCursor(cursor,CursorManagerPriori ty.HIGH);
});
obj.addEventListener(MouseEvent.MOUSE_OUT,
function(e:MouseEvent):void{
CursorManager.removeAllCursors();
});
}
}

But this is no good since I have to embed the image. I want to be able to use the image path to specify the image, not embed it.

aktell
01-09-2009, 11:48 PM
Hi there,

I said HELP section (Help Contents) in your FLEX3 on top. It is all in there even with Image Embed. regards aktell

wvxvw
01-10-2009, 12:39 AM
Wow, that's actually very strange way of handling cursor appearance (I mean the CursorManager class) :confused:
The only way I can think now is to:
create SWFs for each cursor icon you're going to load. Load them, get the document class of those SWFs and pass it as the first argument to the setCursor().

And, BTW, never use this sort of coding you show. Anonymous functions are evil, especially when you use them in that way.

d000hg
01-10-2009, 01:26 AM
Anonymous functions are a key part of Javascript, AS3, and even Java/C#. I agree they can be ugly but it's certainly not evil, one line functions only ever used in one place don't need to be named since there is no capacity for reuse.

d000hg
01-10-2009, 01:28 AM
create SWFs for each cursor icon you're going to load. Load them, get the document class of those SWFs and pass it as the first argument to the setCursor()I'm not familiar with document classes, can you explain or give an example? Is the class belonging to the loaded SWF file, or to the SWFLoader object you use to load the file?

wvxvw
01-10-2009, 02:06 AM
This is an example build file:
<?xml version="1.0" encoding="utf-8"?>
<flex-config>
<compiler>
<source-path append="true">
<path-element>D:\mxml\AMFArray\src</path-element>
<path-element>C:\flex_sdk_3\frameworks\projects\rpc\src</path-element>
<path-element>C:\Program Files\FlashDevelop\Library\AS3\classes</path-element>
</source-path>
</compiler>
<file-specs>
<path-element>D:\mxml\AMFArray\src\Main.mxml</path-element>
</file-specs>
<default-background-color>#FFFFFF</default-background-color>
<default-frame-rate>30</default-frame-rate>
<default-size>
<width>800</width>
<height>600</height>
</default-size>
</flex-config>
Whatever you specify in <file-specs/> is a document class (entry point) this is the class that is automatically instantiated first, and from this class all your application logic begins. So, when you compile a SWF, and then load it, you can get this class (as everyone else) from it's loaderInfo.applicationDomain by calling getDefinition("fully.qualified.ClassName") which will return Object, that you can cast to class.

And anonymous function are a rudiment from AS1 / JS-like syntax. The reason to never use them is:
when you debug your application they will appear in debugger like this:
...Some error text...
at method-<12345>
or something like it. I'm not sure you'll like searching for the one like that, given your code will be formatted in that way...
More than that, in your specific case you cannot remove those listeners, ever. This also means, that the object, where you have this init method as well as the object you pass in the arguments will stay forever in player's memory :)
Meaning, that if I needed to load that sort of application as a plugin for my application I'd disagree, and ask you to refactor your code in a way objects are garbagecollectable :)

More yet :) as long as you're coding for mx framework... init function misses return type, and for mx framework this is a must.

d000hg
01-10-2009, 02:25 AM
You've just confused me with a build file. I simply want to be able to do:
CursorManager.setCursor(someHandyFunction("myCursor.png")), and I'm hoping for a simple way do this.

Does anyone know WHY the Flex API sometimes uses Class parameters? It's a really ugly system in my opinion, given that embedding resources is often not a great design choice even if you know exactly what resources you will need to use.

wvxvw
01-10-2009, 02:46 AM
OK, what I'm saying is: there's no simple way to do it in mx framework. I'm not responsible for the logic it uses... so, I cannot help it :) I can advise you to file a feature request at http://bugs.adobe.com/ but that's all I can. (I'd ask for additional method like CursorManager.getCursor(index:int): DisplayObject or for CursorManager.setCursor(cursorObject: DisplayObject, priority:int = 2, xOffset:Number = 0, yOffset:Number = 0).

The workaround I suggested is:
you can load SWF which contains classes, where one of those classes is the class you can use for cursor. After you load that SWF you retrieve the class definition and pass it to the CursorManager.

Oh, and there may be another workaround: you may pass the class that will load the image when instantiated, though, it may be a little bit complicated to pass it information... you'll need it to have some static setter to for the URL of the image it will load upon instantiation.
I.e.
CursorFactory.imageURL = "some.png";
CursorManager.setCursor(CursorFactory);
then inside CursorFactory:
public class CursorFactory extends Loader
{
public static var imageURL:String;

public function CursorFactory()
{
super();
if (imageURL) load(imageURL);
}
}

d000hg
01-10-2009, 09:26 AM
Ok thanks. I'm not blaming the Flex design on you :) I was just wondering if anyone can see why they do it this way.

Out of interest, how would I set a cursor in a NON-FLEX AS3 application? Does Flash provide a comparable way to set cursors?

wvxvw
01-10-2009, 11:35 AM
No it doesn't. What you can do is only Mouse.show() or Mouse.hide()
And if you want to have a custom cursor you hide the native cursor and make some of your objects follow it when it moves.

drkstr
01-10-2009, 09:19 PM
Does anyone know WHY the Flex API sometimes uses Class parameters? It's a really ugly system in my opinion, given that embedding resources is often not a great design choice even if you know exactly what resources you will need to use.To force you to use embeded graphics.

Embeded graphics are the only way to quickly and efficiently handle the kind of graphic switching required for something like a mouse cursor. It just does not make any sense to try and load the asset upon request. "Assets" (such as button icons, skins, etc..) should always be compiled in as part of the application, or loaded from a pre-compiled module. Otherwise you are losing out on a significant performance gain.

If the issue you're trying to solve is that you want to load the mouse cursors at runtime, then compile your assets into a module and load it up upon initialization. Just make sure the assets are ready by the time the application needs them.


Best Regards,
~Aaron