assigning values to @:fakeEnum elements

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

assigning values to @:fakeEnum elements

Joshua Harlan Lifton
Hi all,

I'm trying to use @:fakeEnum for NME's TextFormatAlign like so:

@:fakeEnum(String) enum TextFormatAlign
{
  LEFT;
  RIGHT;
  CENTER;
  JUSTIFY;
}

Unfortunately, as is standard in the Flash API, the LEFT static const
maps to the string "left", not "LEFT", whereas the fakeEnum mapping is
more literal, i.e., LEFT is "LEFT". I tried something like:

@:fakeEnum(String) enum TextFormatAlign
{
  LEFT="left";
  RIGHT="right";
  CENTER="center";
  JUSTIFY="justify";
}

but this doesn't compile. I also tried:

@:fakeEnum(String) enum TextFormatAlign
{
  left;
  right;
  center;
  justify;
}

Which worked, but doesn't follow the Flash API. So, how can I get
TextFormatAlign.CENTER enum element be considered as the string
literal "center"?

Also, sorry if my latest change to NME's TextFormatAlign broke things
for anyone - I'm trying to fix it now.

Thanks,
Josh

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning values to @:fakeEnum elements

Joshua Harlan Lifton
On Fri, Jul 22, 2011 at 11:46 PM, Joshua Harlan Lifton
<[hidden email]> wrote:
> I'm trying to use @:fakeEnum for NME's TextFormatAlign like so:

I see that nme.text.TextFieldAutoSize handles a similar problem in a
different way, though I think doing the same for TextFormatAlign would
require changing project/common/TextField.cpp. I'll start looking into
that now, but I'm still curious about the possibility of using
@:fakeEnum.

Josh

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning values to @:fakeEnum elements

Nicolas Cannasse
In reply to this post by Joshua Harlan Lifton
Le 23/07/2011 05:46, Joshua Harlan Lifton a écrit :
> Hi all,
[...]
> Which worked, but doesn't follow the Flash API. So, how can I get
> TextFormatAlign.CENTER enum element be considered as the string
> literal "center"?

You need a class TextFormatAlign which has a static field CENTER with
value "center".

Another possibility would be to handle directly @:fakeEnum fields access
at the genCPP level to replace the field access directly by
corresponding constant String.

Nicolas


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning values to @:fakeEnum elements

Joshua Harlan Lifton
>> Which worked, but doesn't follow the Flash API. So, how can I get
>> TextFormatAlign.CENTER enum element be considered as the string
>> literal "center"?
>
> You need a class TextFormatAlign which has a static field CENTER with value
> "center".

This was the previous solution. This breaks compatibility with the
Flash API. For example, the following example works in Flash but won't
work in NME because the align variable would need to be of type String
instead of TextFormatAlign:

public function createLabel(text:String):TextField {
  var color:Int = 0x0000BB;
  var size:Int;
  var align:TextFormatAlign;
  switch(text) {
    case("foo"):
      size = 12;
      align = TextFormatAlign.CENTER;
      color = 0x000FFF;
    case("bar"):
      size = 14;
      align = TextFormatAlign.LEFT;
    default:
      size = 16;
      align = TextFormatAlign.RIGHT;
  }
  var out:TextField = new TextField();
  var format:TextFormat = new TextFormat();
  out.defaultTextFormat = format;
  out.text = text;
  return out;
}

There are, of course, ways around this, like using Dynamic, but it
would be nice to maintain Flash compatibility. I think I see the
change that needs to be made in NME and TextFieldAutoSize serves as a
good example, I just haven't had time to do it yet. I've reverted the
changes I made in SVN trunk and will get back to this problem later.

> Another possibility would be to handle directly @:fakeEnum fields access at
> the genCPP level to replace the field access directly by corresponding
> constant String.

This would be interesting. I'd like to know if this comes about.

Thanks,
Josh

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: assigning values to @:fakeEnum elements

singmajesty
I am running into this exact same problem!

For now, I am implementing as a class with public static values, but it would be great to be able to do an enum instead, to provide better Haxe compatibility.

I wonder if it would be possible to handle a custom meta-tag in HXCPP?
Reply | Threaded
Open this post in threaded view
|

Re: assigning values to @:fakeEnum elements

singmajesty
flash.display.StageAlign, for example, uses "B" for StageAlign.BOTTOM, "BL" for StageAlign.BOTTOM_LEFT, etc.
Reply | Threaded
Open this post in threaded view
|

Re: assigning values to @:fakeEnum elements

singmajesty
Looking at the standard flash9 classes for Haxe, I don't understand how it works. Does a Flash SWF, generated by Haxe, expect a different value than "B" or "BL", or is the compiler making a special exception for these enums?
Reply | Threaded
Open this post in threaded view
|

Re: assigning values to @:fakeEnum elements

singmajesty
I understand now...

Since the standard classes are defined as a @:fakeEnum and as an extern,  
it pulls the values that are provided by Flash, rather than having to  
define its own.

I suppose the real problem I have run into is that you cannot use an enum  
that uses a @:native tag. If I do something like this:


package nme.display;

@:native ("flash.display.StageAlign")
@:fakeEnum(String) extern enum StageAlign {
        BOTTOM;
        BOTTOM_LEFT;
        BOTTOM_RIGHT;
        LEFT;
        RIGHT;
        TOP;
        TOP_LEFT;
        TOP_RIGHT;
}



Then I get a compiler error that "nme.display::StageAlign is not defined".




On Fri, 09 Sep 2011 17:07:25 -0700, singmajesty  
<[hidden email]> wrote:

> Looking at the standard flash9 classes for Haxe, I don't understand how  
> it
> works. Does a Flash SWF, generated by Haxe, expect a different value than
> "B" or "BL", or is the compiler making a special exception for these  
> enums?
>
> --
> View this message in context:  
> http://haxe.1354130.n2.nabble.com/assigning-values-to-fakeEnum-elements-tp6612576p6777712.html
> Sent from the Haxe mailing list archive at Nabble.com.

--
haXe - an open source web programming language
http://haxe.org