[Stage3D] Feathersを試すのだ! (4) ~Button, BitmapFont~

Flash Player 11 から採用された Stage3D を学習するよ。 :bouzu:
Starling FrameworkFeathers を試しちゃうんだからね。 :girl:

starling_feathers34

:caution: 要 Flash Player 11.8 以上

This movie requires Flash Player 11.8.0
Main.as
package {

  import flash.display.Sprite;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;

  import starling.core.Starling;

  [SWF(backgroundColor="#FFFFFF", width="600", height="200", frameRate="60")]

  public class Main extends Sprite {
    // プロパティ
    private var starling:Starling;

    // コンストラクタ
    public function Main() {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      init();
    }

    // メソッド
    private function init():void {
      starling = new Starling(MainView, stage, null, null);
      starling.enableErrorChecking = true;
      starling.start();
    }

  }

}



import starling.core.Starling;
import starling.display.Sprite;
import starling.display.Image;
import starling.textures.Texture;
import starling.events.Event;
import starling.text.TextField;
import starling.text.BitmapFont;
import starling.textures.TextureSmoothing;

import feathers.controls.Button;
import feathers.controls.ToggleButton;
import feathers.text.BitmapFontTextFormat;


internal class MainView extends Sprite {
  [Embed(source="assets/components/upButton.png")]
  private var UpButton:Class;
  [Embed(source="assets/components/overButton.png")]
  private var OverButton:Class;
  [Embed(source="assets/components/downButton.png")]
  private var DownButton:Class;
  [Embed(source="assets/components/disabledButton.png")]
  private var DisabledButton:Class;
  [Embed(source="assets/components/font.png")]
  private var FontTexture:Class;
  [Embed(source="assets/components/font.fnt", mimeType="application/octet-stream")]
  private var FontXML:Class;
  // プロパティ
  private var playBtn:ToggleButton;
  private var stopBtn:Button;

  // コンストラクタ
  public function MainView() {
    addEventListener(Event.ADDED_TO_STAGE, init);
    addEventListener(Event.REMOVED_FROM_STAGE, remove);
  }

  // メソッド
  private function init(evt:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    var upTexture:Texture = Texture.fromBitmap(new UpButton());
    var overTexture:Texture = Texture.fromBitmap(new OverButton());
    var downTexture:Texture = Texture.fromBitmap(new DownButton());
    var selectedTexture:Texture = Texture.fromBitmap(new DownButton());
    var disabledTexture:Texture = Texture.fromBitmap(new DisabledButton());
    var texture:Texture = Texture.fromBitmap(new FontTexture());
    var bitmapFont:BitmapFont = new BitmapFont(texture, XML(new FontXML()));
    bitmapFont.smoothing = TextureSmoothing.NONE;

    TextField.registerBitmapFont(bitmapFont);
    var upBitmapFontTextFormat:BitmapFontTextFormat = new BitmapFontTextFormat(bitmapFont);
    upBitmapFontTextFormat.size = 14;
    upBitmapFontTextFormat.color = 0x333333;
    var downBitmapFontTextFormat:BitmapFontTextFormat = new BitmapFontTextFormat(bitmapFont);
    downBitmapFontTextFormat.size = 14;
    downBitmapFontTextFormat.color = 0x000000;
    var disabledBitmapFontTextFormat:BitmapFontTextFormat = new BitmapFontTextFormat(bitmapFont);
    disabledBitmapFontTextFormat.size = 14;
    disabledBitmapFontTextFormat.color = 0x999999;
    playBtn = new ToggleButton();
    addChild(playBtn);

    playBtn.upSkin = new Image(upTexture);
    playBtn.upLabelProperties.textFormat = upBitmapFontTextFormat;
    playBtn.hoverSkin = new Image(overTexture);
    playBtn.hoverLabelProperties.textFormat = downBitmapFontTextFormat;
    playBtn.downSkin = new Image(downTexture);
    playBtn.downLabelProperties.textFormat = downBitmapFontTextFormat;
    playBtn.defaultSelectedSkin = new Image(selectedTexture);
    playBtn.defaultSelectedLabelProperties.textFormat = downBitmapFontTextFormat;
    playBtn.disabledSkin = new Image(selectedTexture);
    playBtn.disabledLabelProperties.textFormat = downBitmapFontTextFormat;
    playBtn.label = "play";
    playBtn.validate();
    playBtn.addEventListener(Event.TRIGGERED, play);
    playBtn.x = 260 - playBtn.width/2;
    playBtn.y = 100 - playBtn.height/2;
    stopBtn = new Button();
    addChild(stopBtn);

    stopBtn.upSkin = new Image(upTexture);
    stopBtn.upLabelProperties.textFormat = upBitmapFontTextFormat;
    stopBtn.hoverSkin = new Image(overTexture);
    stopBtn.hoverLabelProperties.textFormat = downBitmapFontTextFormat;
    stopBtn.downSkin = new Image(downTexture);
    stopBtn.downLabelProperties.textFormat = downBitmapFontTextFormat;
    stopBtn.disabledSkin = new Image(disabledTexture);
    stopBtn.disabledLabelProperties.textFormat = disabledBitmapFontTextFormat;
    stopBtn.label = "stop";
    stopBtn.validate();
    stopBtn.addEventListener(Event.TRIGGERED, stop);
    stopBtn.x = 340 - stopBtn.width/2;
    stopBtn.y = 100 - stopBtn.height/2;
    stopBtn.isEnabled = false;
  }
  private function remove(evt:Event):void {
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
  }
  override public function dispose():void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
    super.dispose();
  }
  private function play(evt:Event):void {
    playBtn.isSelected = true;
    playBtn.isEnabled = false;
    stopBtn.isEnabled = true;

  }
  private function stop(evt:Event):void {
    playBtn.isSelected = false;
    playBtn.isEnabled = true;

    stopBtn.isEnabled = false;
  }

}

font.png
starling_feathers34_font

font.fnt
<font>
  <info face="FontName" size="32" />
  <common lineHeight="39" />
  <pages>
    <page id="0" file="font.png" />
  </pages>
  <chars>
  <char id="32" x="0" y="0" width="0" height="0" xoffset="0" yoffset="24" xadvance="7" />
  <char id="124" x="0" y="0" width="4" height="34" xoffset="2" yoffset="0" xadvance="8" />

    (中略)

  <char id="95" x="110" y="126" width="19" height="4" xoffset="0" yoffset="26" xadvance="16" />
  <char id="45" x="129" y="126" width="10" height="4" xoffset="0" yoffset="14" xadvance="10" />
  </chars>
</font>

:check: 「hiero.jar - libgdx - Hiero - Android/iOS/HTML5/desktop game development framework - Google Project Hosting

TextField(Label) の位置が調整できない。 :bouzu:


[修正] (13/07/29 Mon 19:28)
Feathers 1.1.0 にバージョンアップ。
[修正] (13/09/26 Thu 00:14)
Starling 1.4 にバージョンアップ。Feathers 1.1.1 にバージョンアップ。
[修正] (13/11/12 Tue 18:58)
Starling 1.4.1 にバージョンアップ。
[修正] (13/11/28 Thu 23:18)
Feathers 1.2.0 にバージョンアップ。
[修正] (14/06/25 Wed 15:11)
Starling 1.5 にバージョンアップ。Feathers 1.3.0 にバージョンアップ。
[修正] (14/06/25 Wed 17:07)
Starling 1.5.1 にバージョンアップ。
[修正] (14/10/20 Mon 16:36)
Feathers 2.0.0 にバージョンアップ。
ToggleButton でないと isSelected が使えないので、playBtn を ToggleButton に変更。
[修正] (14/12/15 Mon 16:41)
Starling 1.6 にバージョンアップ。Feathers 2.0.1 にバージョンアップ。