root/as3/Eseclock/branches/alumican/src/org/libspark/eseclock/object/ClockSprite.as

リビジョン 2440, 6.1 kB (コミッタ: alumican, コミット時期: 3 年 前)

Eseclock:IEseclockTextFieldの仕様を変更

Line 
1 /**
2  * Licensed under the MIT License
3  *
4  * Copyright (c) 2008 BeInteractive! (www.be-interactive.org) and
5  *               2009 alumican.net (www.alumican.net) and
6  *               Spark project (www.libspark.org)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 package org.libspark.eseclock.object
27 {
28         import caurina.transitions.Tweener;
29         import flash.display.DisplayObject;
30         import flash.display.Graphics;
31         import flash.display.Shape;
32         import flash.display.Sprite;
33         import flash.text.TextField;
34         import flash.text.TextFieldAutoSize;
35         import flash.text.TextFormat;
36         import flash.utils.getDefinitionByName;
37        
38         import org.libspark.eseclock.textfield.EseclockDefaultClockTextField;
39         import org.libspark.eseclock.textfield.EseclockDefaultDescriptionTextField;
40         import org.libspark.eseclock.textfield.IEseclockTextField;
41        
42         /**
43          * ClockSprite.as
44          * 時計とその下のテキスト
45          *
46          * @author      alumican.net<Yukiya Okuda>
47          * @link        http://alumican.net/
48          * @link        http://www.libspark.org/
49          */
50        
51         public class ClockSprite extends TweenMaskSprite
52         {
53                 //-------------------------------------
54                 // VARIABLES
55                 //-------------------------------------
56                
57                 //時計のテキストフィールド
58                 private var _field:IEseclockTextField;
59                
60                 //時計の下に表示するテキストフィールド
61                 private var _sub:IEseclockTextField;
62                
63                 //行間
64                 private var _leading:Number;
65                
66                 //テキストが空文字列以外の場合はtrue
67                 private var _hasDescription:Boolean;
68                
69                
70                
71                
72                
73                 //-------------------------------------
74                 // GETTER/SETTER
75                 //-------------------------------------
76                
77                 //テキスト色
78                 public function set textColor(value:uint):void {
79                         _changeTextColor(_field, value);
80                         _changeTextColor(_sub  , value);
81                 }
82                
83                 //背景色
84                 public function set backgroundColor(value:uint):void { _drawBackground(value); }
85                
86                
87                
88                
89                
90                 //-------------------------------------
91                 // CONSTRUCTOR
92                 //-------------------------------------
93                
94                 /**
95                  * コンストラクタ
96                  * @param       textColor               テキスト色
97                  * @param       backgroundColor 背景色
98                  * @param       w                               幅
99                  * @param       h                               高さ
100                  */
101                 public function ClockSprite(textColor:uint,
102                                                                         backgroundColor:uint,
103                                                                         w:Number,
104                                                                         h:Number,
105                                                                         leading:Number,
106                                                                         clockAsset:String = "",
107                                                                         subAsset:String = "")
108                 {
109                         super(w, h, backgroundColor);
110                        
111                         _leading = leading;
112                        
113                         _createFields(textColor, clockAsset, subAsset);
114                 }
115                
116                
117                
118                
119                
120                 //-------------------------------------
121                 // METHODS
122                 //-------------------------------------
123                
124                 /**
125                  * テキストフィールドを生成する関数
126                  * @param       color   テキスト色
127                  */
128                 private function _createFields(color:uint, clockAsset:String = "", subAsset:String = ""):void
129                 {
130                         _field = _createField(clockAsset, EseclockDefaultClockTextField);
131                         _field.setColor(color);
132                         _field.updateClock(99, 99, 99);
133                        
134                         _masked.addChild(_field as DisplayObject);
135                        
136                        
137                         _sub = _createField(subAsset, EseclockDefaultDescriptionTextField);
138                         _sub.setColor(color);
139                         _sub.updateDescription("TOKYO / JAPAN");
140                        
141                         _masked.addChild(_sub as DisplayObject);
142                        
143                        
144                         //中央揃え
145                         resize(_w, _h);
146                 }
147                
148                 /**
149                  * 各テキストフィールドを生成する関数
150                  * @param       asset
151                  * @param       defaultClass
152                  */
153                 private function _createField(asset:String, defaultClass:Class):IEseclockTextField {
154                         var tf:IEseclockTextField;
155                        
156                         if (asset == "") {
157                                 tf = new defaultClass() as IEseclockTextField;
158                         } else {
159                                 var fieldClass:Class = getDefinitionByName(asset) as Class;
160                                 tf = new fieldClass() as IEseclockTextField;
161                         }
162                        
163                         return tf;
164                 }
165                
166                 /**
167                  * 時計の下に表示するテキストを更新する関数
168                  * @param       description     テキスト
169                  */
170                 public function updateDescription(description:String):void
171                 {
172                         _hasDescription = (description != "") ? true : false;
173                        
174                         _sub.updateDescription(description);
175                        
176                         //中央揃え
177                         resize(_w, _h);
178                 }
179                
180                 /**
181                  * 文字盤の表示を更新する関数
182                  * @param       h       時
183                  * @param       m       分
184                  * @param       s       秒
185                  */
186                 public function updateField(h:uint, m:uint, s:uint):void
187                 {
188                         _field.updateClock(h, m, s);
189                 }
190                
191                 /**
192                  * テキストフィールドの色を変更する関数
193                  * @param       tf      ターゲット
194                  * @param       color           色
195                  */
196                 private function _changeTextColor(tf:IEseclockTextField, color:uint):void
197                 {
198                         tf.setColor(color);
199                 }
200                
201                 /**
202                  * 現在の_wと_hに合わせてEseclockをリサイズする関数
203                  * @param       w       幅
204                  * @param       h       高さ
205                  */
206                 override public function resize(w:Number, h:Number):void
207                 {
208                         super.resize(w, h);
209                        
210                         //テキストフィールドの中央揃え
211                         var margin:int = (_hasDescription) ? _leading : 0;
212                         var totalHeight:uint = _field.objectHeight + _sub.objectHeight;
213                        
214                         _field.x = (_w - _field.objectWidth) / 2;
215                         _field.y = (_h - totalHeight       ) / 2;
216                        
217                         _sub.x = (_w - _sub.objectWidth) / 2;
218                         _sub.y = (_h - totalHeight     ) / 2 + _field.objectHeight + margin;
219                 }
220         }
221 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。