※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


FORKED
  1. // forked from taiga's TextField 用 行スクロールバー
  2. package {
  3.     import flash.display.DisplayObject;
  4.     import flash.display.Graphics;
  5.     import flash.display.Shape;
  6.     import flash.display.Sprite;
  7.     import flash.events.Event;
  8.     import flash.events.MouseEvent;
  9.     import flash.geom.ColorTransform;
  10.     import flash.text.TextField;
  11.     import flash.text.TextFieldType;
  12.     [SWF (backgroundColor="0xcccccc")]
  13.     /**
  14.      * TextField 用 行スクロールバー
  15.      * @author taiga
  16.      */
  17.     public class TextFieldScrollTest extends Sprite {
  18.         /** @private */
  19.         private var _sampleText                    :String;
  20.         /** @private */
  21.         private var _lineHeight                    :int;
  22.         /** @private */
  23.         private var _isScrollDown                  :Boolean;
  24.         /** @private */
  25.         private var _scrollClickPointY             :int;
  26.         /** @private */
  27.         private var _enabledColor                  :uint;
  28.         /** @private */
  29.         private var _disabledColor                 :uint;
  30.         /** TextField インスタンスです。 */
  31.         protected var textField                    :TextField;
  32.         /** Sprite インスタンスです。 */
  33.         protected var textFieldScrollBar           :Sprite;
  34.         /** Shape インスタンスです。 */
  35.         protected var textFieldScroolBarBackground :Shape;
  36.         /** ResetButton インスタンスです。 */
  37.         protected var resetButton                  :ResetButton;
  38.         /**
  39.          * コンストラクタ
  40.          */
  41.         public function TextFieldScrollTest() {
  42.             super();
  43.             _sampleText    = "TextField クラスは、テキストの表示と入力用の表示オブジェクトを作成するために使用されます。SWF ファイルのダイナミックテキストフィールドおよびテキスト入力フィールドは、すべて TextField クラスのインスタンスです。TextField クラスを使用すると、低レベルでのテキストレンダリングが可能になります。ただし、Flex では一般的に Label、Text、TextArea、TextInput コントロールを使用して、テキスト処理を行います。\n\n" +
  44.                              "テキストフィールドを動的に作成する場合は、TextField() コンストラクタを使用します。\n\n" +
  45.                              "TextField クラスのメソッドを使用すると、オーサリング時または実行時に作成したダイナミックテキストフィールドやテキスト入力フィールドにテキストを設定、選択、および操作できます。\n\n" +
  46.                              "ActionScript には、テキストを実行時にフォーマットする方法がいくつか用意されています。TextFormat クラスでは、TextField オブジェクトの文字フォーマットと段落フォーマットを設定できます。TextField.styleSheet プロパティおよび StyleSheet クラスを使用して、テキストフィールドに CSS (Cascading Style Sheet) スタイルを適用できます。CSS を使用すると、ビルトイン HTML タグのスタイル設定、新しいフォーマットタグの定義、またはスタイルの適用を行うことができます。HTML 形式のテキスト(CSS スタイルを使用している場合も可)をテキストフィールドに直接割り当てることができます。テキストフィールドに割り当てる HTML テキストに埋め込みメディア(ムービークリップ、SWF ファイル、GIF ファイル、PNG ファイル、および JPEG ファイル)を含めることができます。この場合テキストは、Web ブラウザ上で HTML ドキュメントの埋め込みメディアの周りをテキストが囲むのと同じように、埋め込みメディアの周りを囲みます。\n\n" +
  47.                              "Flash Player では、テキストのフォーマットに利用できる HTML タグのサブセットをサポートしています。htmlText プロパティの説明の、サポートされている HTML タグのリストを参照してください。 ";
  48.             _enabledColor  = 0x333333;
  49.             _disabledColor = 0x999999;
  50.             createChildren();
  51.         }
  52.         /** 子表示オブジェクトを生成します。 */
  53.         protected function createChildren():void {
  54.             if(stage != null) {
  55.                 stage.addEventListener(MouseEvent.MOUSE_MOVE, scrollMouseMoveHandler, false0true);
  56.                 stage.addEventListener(MouseEvent.MOUSE_UP,   scrollMouseUpHandler,   false0true);
  57.             }
  58.             textField            = addChildAt(new TextField(), 0as TextField;
  59.             textField.multiline  = true;
  60.             textField.wordWrap   = true;
  61.             textField.background = true;
  62.             textField.border     = true;
  63.             textField.type       = TextFieldType.INPUT;
  64.             textField.addEventListener(Event.CHANGE, textFieldChangeHandler,    false0true);
  65.             textFieldScrollBar = addChildAt(new Sprite(), 0as Sprite;
  66.             textFieldScroolBarBackground = addChildAt(new Shape(), 0as Shape;
  67.             resetButton = addChildAt(new ResetButton(), 0as ResetButton;
  68.             resetButton.addEventListener(MouseEvent.CLICK, resetButtonClickHandler, false0true);
  69.             updateDisplayList();
  70.         }
  71.         /** 表示リスト上のオブジェクトを再描画します。 */
  72.         protected function updateDisplayList():void {
  73.             const TEXTFIELD_WIDTH  :int = 435;
  74.             const TEXTFIELD_HEIGHT :int = 350;
  75.             if(textField != null) {
  76.                 textField.text                   = "あj";
  77.                 _lineHeight                      = textField.getLineMetrics(0).height;
  78.                 textField.text                   = _sampleText;
  79.                 textField.width                  = TEXTFIELD_WIDTH;
  80.                 textField.height                 = TEXTFIELD_HEIGHT;
  81.                 textField.x                      = 10;
  82.                 textField.y                      = 10;
  83.             }
  84.             checkNumLines(textField);
  85.             if(textFieldScrollBar != null) {
  86.                 textFieldScrollBar.x             = textField.x + textField.width + 2;
  87.                 textFieldScrollBar.y             = textField.y;
  88.                 drawScrollBar(textFieldScrollBar);
  89.             }
  90.             if(textFieldScroolBarBackground != null) {
  91.                 textFieldScroolBarBackground.x   = textFieldScrollBar.x + (textFieldScrollBar.width / 2);
  92.                 textFieldScroolBarBackground.y   = textField.y;
  93.                 drawScrollBarBackground(textFieldScroolBarBackground);
  94.             }
  95.             if(resetButton != null) {
  96.                 resetButton.x = textField.x + textField.width - resetButton.width;
  97.                 resetButton.y = textField.x + textField.height + 5;
  98.             }
  99.         }
  100.         /** TextField インスタンスの CHANGE ハンドラです。 */
  101.         protected function textFieldChangeHandler(event:Event):void {
  102.             drawScrollBar(textFieldScrollBar);
  103.             checkNumLines(event.currentTarget as TextField);
  104.         }
  105.         /** TextField インスタンスの表示行数を取得します。 */
  106.         protected function getShowNumLines(target:TextField):int {
  107.             return int((target.height - 4) / _lineHeight);
  108.         }
  109.         /** スクロールバーとステージのマウスアップ処理です。 */
  110.         protected function scrollMouseUpHandler(event:MouseEvent):void   {
  111.             textField.addEventListener(Event.SCROLL, textFieldKeyScrollHandler, trueint.MAX_VALUE, true);
  112.             _isScrollDown = false;
  113.         }
  114.         /** スクロールバーのマウスダウン処理です。 */
  115.         protected function scrollMouseDownHandler(event:MouseEvent):void {
  116.             textField.removeEventListener(Event.SCROLL, textFieldKeyScrollHandler, true);
  117.             _isScrollDown = true;
  118.             _scrollClickPointY = mouseY;
  119.         }
  120.         /** スクロールバーをマウスダウン中に動かすときの処理です。 */
  121.         protected function scrollMouseMoveHandler(event:MouseEvent):void {
  122.             var scrollRectHeight_   :int;
  123.             var scrollPaneHeight_   :int;
  124.             var invisibledNumLines_ :int;
  125.             var minY_               :int;
  126.             var maxY_               :int;
  127.             var dY_                 :int;
  128.             var dScrollV_           :Number;
  129.             scrollRectHeight_ = textField.height;
  130.             scrollPaneHeight_ = textField.textHeight;
  131.             if(scrollRectHeight_ > scrollPaneHeight_ || !_isScrollDown) {
  132.                 return;
  133.             }
  134.             invisibledNumLines_ = textField.numLines - getShowNumLines(textField);
  135.             minY_ = textField.y;
  136.             maxY_ = scrollRectHeight_ - textFieldScrollBar.height + textField.y;
  137.             dY_   = textFieldScrollBar.y + (mouseY - _scrollClickPointY);
  138.             if(dY_  <= minY_) {
  139.                 textFieldScrollBar.y = minY_;
  140.                 dScrollV_ = 0;
  141.             }
  142.             else if(dY_ >= maxY_) {
  143.                 textFieldScrollBar.y = maxY_;
  144.                 dScrollV_ = invisibledNumLines_;
  145.             }
  146.             else {
  147.                 textFieldScrollBar.y = dY_;
  148.                 dScrollV_ = Math.ceil(dY_ / (maxY_ / invisibledNumLines_));
  149.             }
  150.             textField.scrollV = dScrollV_ + 1;
  151.             _scrollClickPointY = mouseY;
  152.             event.updateAfterEvent();
  153.         }
  154.         /** TextField インスタンスのキースクロール処理です。 */
  155.         protected function textFieldKeyScrollHandler(event:Event):void {
  156.             if(_isScrollDown) {
  157.                 return;
  158.             }
  159.             revisionPoint(event.currentTarget as TextField);
  160.         }
  161.         /** ResetButton インスタンスの CLICK ハンドラです。*/
  162.         protected function resetButtonClickHandler(event:MouseEvent):void {
  163.             textField.text = _sampleText;
  164.             drawScrollBar(textFieldScrollBar);
  165.             checkNumLines(textField);
  166.         }
  167.         /** スクロールバーの表示 / 非表示 を制御します。*/
  168.         protected function checkNumLines(target:TextField):void {
  169.             revisionPoint(textField);
  170.             if(target.height < target.textHeight + 4) {
  171.                 textField.addEventListener(Event.SCROLL, textFieldKeyScrollHandler, false0true);
  172.                 textFieldScrollBar.useHandCursor = true;
  173.                 textFieldScrollBar.buttonMode    = true;
  174.                 textFieldScrollBar.addEventListener(MouseEvent.MOUSE_UP,   scrollMouseUpHandler,   false0true);
  175.                 textFieldScrollBar.addEventListener(MouseEvent.MOUSE_DOWN, scrollMouseDownHandler, false0true);
  176.                 textFieldScrollBar.addEventListener(MouseEvent.MOUSE_MOVE, scrollMouseMoveHandler, false0true);
  177.                 hexToRGB(textFieldScrollBar,           _enabledColor);
  178.                 hexToRGB(textFieldScroolBarBackground, _enabledColor);
  179.             }
  180.             else {
  181.                 textField.removeEventListener(Event.SCROLL, textFieldKeyScrollHandler, false);
  182.                 textFieldScrollBar.useHandCursor = false;
  183.                 textFieldScrollBar.buttonMode    = false;
  184.                 textFieldScrollBar.removeEventListener(MouseEvent.MOUSE_UP,   scrollMouseUpHandler,   false);
  185.                 textFieldScrollBar.removeEventListener(MouseEvent.MOUSE_DOWN, scrollMouseDownHandler, false);
  186.                 textFieldScrollBar.removeEventListener(MouseEvent.MOUSE_MOVE, scrollMouseMoveHandler, false);
  187.                 hexToRGB(textFieldScrollBar,           _disabledColor);
  188.                 hexToRGB(textFieldScroolBarBackground, _disabledColor);
  189.             }
  190.         }
  191.         /** スクロールバーの座標を補正します。 */
  192.         protected function revisionPoint(target:TextField):void {
  193.             var dHeight_     :int    = target.height - textFieldScrollBar.height;
  194.             var dScrollV_    :int    = target.scrollV - 1;
  195.             var resultY_     :Number = dHeight_ / (target.numLines - getShowNumLines(target)) * dScrollV_;
  196.             var y_           :Number = resultY_ + target.y
  197.             if(y_ + textFieldScrollBar.height > target.y + target.height) {
  198.                 y_ = target.y + target.height - textFieldScrollBar.height;
  199.             }
  200.             if(y_ < target.y || isNaN(y_)) {
  201.                 y_ = target.y;
  202.             }
  203.             textFieldScrollBar.y = y_;
  204.         }
  205.         /** スクロールバーの外観を描画します。 */
  206.         protected function drawScrollBar(target:Sprite):void {
  207.             var g         :Graphics = target.graphics;
  208.             var minHeight_:int      = 20;
  209.             var height_   :Number   = textField.height * (getShowNumLines(textField) / textField.numLines);
  210.             if(height_ > textField.height) {
  211.                 height_ = textField.height;
  212.             }
  213.             else if(height_ < minHeight_) {
  214.                 height_ = minHeight_;
  215.             }
  216.             g.clear();
  217.             g.beginFill(_enabledColor);
  218.             g.drawRoundRect(008, height_, 1010);
  219.             g.endFill();
  220.         }
  221.         /** スクロールバーの下地の外観を描画します。 */
  222.         protected function drawScrollBarBackground(target:Shape):void {
  223.             var g:Graphics = target.graphics;
  224.             g.clear();
  225.             g.beginFill(_enabledColor);
  226.             g.drawRect(000.5, textField.height);
  227.             g.endFill();
  228.         }
  229.         /** 指定ターゲットの色を指定カラーに変更します。 */
  230.         protected function hexToRGB(target:DisplayObject, v:uint):void {
  231.             var r:uint = (v >> 16) & 0xff;
  232.             var g:uint = (v >> 8) & 0xff;
  233.             var b:uint = v & 0xff;
  234.             target.transform.colorTransform = new ColorTransform(0001, r, g, b);
  235.         }
  236.     }
  237. }
  238. import flash.display.DisplayObject;
  239. import flash.display.GradientType;
  240. import flash.display.Graphics;
  241. import flash.display.Shape;
  242. import flash.display.SimpleButton;
  243. import flash.geom.Matrix;
  244. /**
  245.  * ResetButton クラスは SimpleButton のサブクラスです。
  246.  * サイズは固定です。外部からの編集はしません。
  247.  */
  248. class ResetButton extends SimpleButton {
  249.     /** upState の Shape です。 */
  250.     protected var upStateSkin      :Shape;
  251.     /** overStateShape の Shape です。 */
  252.     protected var overStateSkin    :Shape;
  253.     /** downState の Shape です。 */
  254.     protected var downStateSkin    :Shape;
  255.     /** hitTestState の Shape です。 */
  256.     protected var hitTestStateSkin :Shape;
  257.     /**
  258.      * コンストラクタ
  259.      */
  260.     public function ResetButton(upState:DisplayObject=null, overState:DisplayObject=null, downState:DisplayObject=null, hitTestState:DisplayObject=null) {
  261.         super(upState, overState, downState, hitTestState);
  262.         upStateSkin          = new Shape();
  263.         overStateSkin        = new Shape();
  264.         downStateSkin        = new Shape();
  265.         hitTestStateSkin     = new Shape();
  266.         super.upState        = createShape(upStateSkin,      [0x333333, 0x666666], [0xeeeeee, 0xcccccc], 0x000000);
  267.         super.overState      = createShape(overStateSkin,    [0x666666, 0x999999], [0xffffff, 0xdddddd], 0x666666);
  268.         super.downState      = createShape(downStateSkin,    [0x999999, 0x666666], [0xaaaaaa, 0x999999], 0x333333);
  269.         super.hitTestState   = createHitTestShape(hitTestStateSkin);
  270.         super.hitTestState.x = 0;
  271.         super.hitTestState.y = 0;
  272.         super.useHandCursor  = true;
  273.     }
  274.     /** コンポーネントの外観を作成します。 */
  275.     protected function createShape(target:Shape, backgroundColors:Array, fillColors:Array, arrowColor:uint):Shape {
  276.         const PI :Number   = Math.PI;
  277.         var w    :int      = 60;
  278.         var h    :int      = 13;
  279.         var r    :int      = 10;
  280.         var g    :Graphics = target.graphics;
  281.         var m    :Matrix   = new Matrix();
  282.         var rad  :Number   = PI / 180;
  283.         m.createGradientBox(w, h, PI / 200);
  284.         g.beginGradientFill(GradientType.LINEAR, backgroundColors, [100100], [0255], m);
  285.         g.drawRoundRect(00, w, h, r, r);
  286.         g.endFill();
  287.         g.beginGradientFill(GradientType.LINEAR, fillColors,       [100100], [0255], m);
  288.         g.drawRoundRect(11, w - 2, h - 2, r - 1, r - 1);
  289.         g.endFill();
  290.         return target;
  291.     }
  292.     /** ボタン領域用表示オブジェクトを作成します。 */
  293.     protected function createHitTestShape(target:Shape):Shape {
  294.         var w :int      = 60;
  295.         var h :int      = 13;
  296.         var g :Graphics = target.graphics;
  297.         g.beginFill(0);
  298.         g.drawRect(00, w, h);
  299.         g.endFill();
  300.         return target;
  301.     }
  302. }
noswf
Get Adobe Flash Player