[Stage3D] Feathersを試すのだ! (3) ~Button, TextureAtlas~

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

starling_feathers33

: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.textures.TextureAtlas;
import starling.events.Event;

import feathers.controls.Button;
import feathers.controls.ToggleButton;


internal class MainView extends Sprite {
  [Embed(source="assets/components/button.png")]
  private var ButtonTexture:Class;
  [Embed(source="assets/components/button.xml", mimeType="application/octet-stream")]
  private var ButtonAtlas:Class;
  // プロパティ
  private var playBtn:ToggleButton;
  private var stopBtn:Button;
  private var texture:Texture;
  private var atlas:TextureAtlas;

  // コンストラクタ
  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);
    texture = Texture.fromBitmap(new ButtonTexture());
    atlas = new TextureAtlas(texture, XML(new ButtonAtlas()));

    playBtn = new ToggleButton();
    addChild(playBtn);

    playBtn.upSkin = new Image(atlas.getTexture("button-up"));
    playBtn.upIcon = new Image(atlas.getTexture("icon-play-up"));
    playBtn.hoverSkin = new Image(atlas.getTexture("button-over"));
    playBtn.hoverIcon = new Image(atlas.getTexture("icon-play-over"));
    playBtn.downSkin = new Image(atlas.getTexture("button-down"));
    playBtn.downIcon = new Image(atlas.getTexture("icon-play-down"));
    playBtn.defaultSelectedSkin = new Image(atlas.getTexture("button-down"));
    playBtn.defaultSelectedIcon = new Image(atlas.getTexture("icon-play-down"));
    playBtn.disabledSkin = new Image(atlas.getTexture("button-down"));
    playBtn.disabledIcon = new Image(atlas.getTexture("icon-play-down"));
    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(atlas.getTexture("button-up"));
    stopBtn.upIcon = new Image(atlas.getTexture("icon-stop-up"));
    stopBtn.hoverSkin = new Image(atlas.getTexture("button-over"));
    stopBtn.hoverIcon = new Image(atlas.getTexture("icon-stop-over"));
    stopBtn.downSkin = new Image(atlas.getTexture("button-down"));
    stopBtn.downIcon = new Image(atlas.getTexture("icon-stop-down"));
    stopBtn.disabledSkin = new Image(atlas.getTexture("button-disabled"));
    stopBtn.disabledIcon = new Image(atlas.getTexture("icon-stop-disabled"));
    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;
  }

}

button.png
starling_feathers33_components

button.xml
<?xml version="1.0" encoding="UTF-8"?>
<TextureAtlas imagePath="button.png">
<SubTexture name="button-up" x="4" y="0" width="72" height="34"/>
<SubTexture name="button-over" x="84" y="0" width="72" height="34"/>
<SubTexture name="button-down" x="164" y="0" width="72" height="34"/>
<SubTexture name="button-disabled" x="244" y="0" width="72" height="34"/>
<SubTexture name="icon-play-up" x="20" y="34" width="40" height="16"/>
<SubTexture name="icon-play-over" x="100" y="34" width="40" height="16"/>
<SubTexture name="icon-play-down" x="180" y="34" width="40" height="16"/>
<SubTexture name="icon-stop-up" x="20" y="50" width="40" height="16"/>
<SubTexture name="icon-stop-over" x="100" y="50" width="40" height="16"/>
<SubTexture name="icon-stop-down" x="180" y="50" width="40" height="16"/>
<SubTexture name="icon-stop-disabled" x="260" y="50" width="40" height="16"/>
</TextureAtlas>


[修正] (13/02/23 Sat 14:06)
Feathers 1.0.1 にバージョンアップ。
[修正] (13/07/29 Mon 19:28)
Feathers 1.1.0 にバージョンアップ。
[修正] (13/09/26 Thu 00:13)
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:33)
Feathers 2.0.0 にバージョンアップ。
ToggleButton でないと isSelected が使えないので、playBtn を ToggleButton に変更。
[修正] (14/12/15 Mon 16:41)
Starling 1.6 にバージョンアップ。Feathers 2.0.1 にバージョンアップ。