TextBlockの縦書き+絵文字 – wonderfl build flash online
▼ActionScript AS3(FP10)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
/** * Copyright umhr ( http://wonderfl.net/user/umhr ) * MIT License ( http://www.opensource.org/licenses/mit-license.php ) * Downloaded from: http://wonderfl.net/c/5Lit */ /* **このAS3のコードはMITライセンスですが、 **絵はGPLライセンスです。 http://start.typepad.jp/typecast/ アニメーションしない顔文字のgif http://mztm.jp/wonderfl/emoticons.gif 縦書き+ケータの絵文字。 絵文字の指定方法は絵文字名がsunの 場合は[m:sun]と書く。 文字コードで指定できるようにすれば、 普通にケータイメールビューアーが作れるかもね。 なんかチェックメイトのお題が テキストエンジンになってたのでついでに応募。 how to useの範囲内の使い方しかしてないので、 賑やかし要員の自覚ありですがw Adobeさん、さらなるテキスト周りの機能拡充を 期待してますよ! という願いを込めて。 参考 Adobe Flash CS4 詳細!ActionScript 3.0入門ノート http://oshige.com/flash/as3note_cs4/ http://help.adobe.com/ja_JP/AS3LCR/Flash_10.0/flash/text/engine/TextBlock.html http://help.adobe.com/ja_JP/AS3LCR/Flash_10.0/flash/text/engine/EastAsianJustifier.html http://level0.kayac.com/2008/12/textbox.php */ package { import flash.display.DisplayObject; import flash.display.Sprite; import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.Matrix; import flash.text.engine.FontDescription; import flash.text.engine.TextBlock; import flash.text.engine.TextElement; import flash.text.engine.TextLine; import flash.text.engine.TextRotation; import flash.text.engine.ElementFormat; import flash.text.engine.EastAsianJustifier; import flash.text.engine.GraphicElement; import flash.text.engine.GroupElement; //import flash.text.engine.TextLine; //import flash.text.engine.ElementFormat; //import flash.text.engine.FontDescription; import flash.text.engine.ContentElement; import flash.text.TextField; import flash.events.MouseEvent; [SWF(width = "465", height = "465", backgroundColor = 0xFFFFFF, frameRate = "30")] public class Main3 extends Sprite { private var numFromName:Array = ["sun","cloud","rain","snow","thunder","typhoon","mist","sprinkle","aries","taurus","gemini","cancer","leo","virgo","libra","scorpius","sagittarius","capricornus","aquarius","pisces","sports","baseball","golf","tennis","soccer","ski","basketball","motorsports","train","pocketbell","subway","bullettrain","car","rvcar","bus","ship","airplane","house","building","postoffice","hospital","bank","atm","hotel","24hours","gasstation","parking","signaler","toilet","restaurant","cafe","bar","beer","fastfood","boutique","hairsalon","karaoke","movie","upwardright","carouselpony","music","art","drama","event","ticket","smoking","nosmoking","camera","bag","book","ribbon","present","birthday","telephone","mobilephone","memo","tv","game","cd","heart","spade","diamond","club","eye","ear","rock","scissors","paper","downwardright","upwardleft","foot","shoe","eyeglass","wheelchair","newmoon","moon1","moon2","moon3","fullmoon","dog","cat","yacht","xmas","downwardleft","phoneto","mailto","faxto","info01","info02","mail","by-d","d-point","yen","free","id","key","enter","clear","search","new","flag","freedial","sharp","mobaq","one","two","three","four","five","six","seven","eight","nine","zero","ok","heart01","heart02","heart03","heart04","smile","angry","despair","sad","wobbly","up","note","cute","spa","kissmark","shine","flair","annoy","punch","bomb","notes","down","sleepy","sign01","sign02","sign03","impact","sweat01","sweat02","dash","sign04","sign05","slate","pouch","pen","shadow","chair","night","soon","on","end","clock","appli01","appli02","t-shirt","moneybag","rouge","denim","snowboard","bell","door","dollar","pc","loveletter","wrench","pencil","crown","ring","sandclock","bicycle","japanesetea","watch","think","confident","coldsweats01","coldsweats02","pout","gawk","lovely","good","bleah","wink","happy02","bearing","catface","crying","weep","ng","clip","copyright","tm","run","secret","recycle","r-mark","danger","ban","empty","pass","full","leftright","updown","school","wave","fuji","clover","cherry","tulip","banana","apple","bud","maple","cherryblossom","riceball","cake","bottle","noodle","bread","snail","chick","penguin","fish","delicious","happy01","horse","pig","wine","shock"]; private var inputTF:TextField = new TextField(); private var commentBase:Sprite = new Sprite(); private var loadFiles_array:Array; private var MultiLoader:MultiLoaderClass = new MultiLoaderClass("http://mztm.jp/crossdomain.xml"); private var emotionIcon:Object = {}; public function Main3():void { //xmlの読み込み loadFiles_array = MultiLoader.setLoad(["http://mztm.jp/wonderfl/emoticons.gif"],onComp); } private function onComp():void{ for (var i:int = 0; i < 250; i++) { var bd:BitmapData = new BitmapData(20, 20, false); bd.draw(loadFiles_array[0],new Matrix(1,0,0,1,-20*(i%20),-20*(Math.floor(i*0.05)))) emotionIcon["m"+i] = new Bitmap(bd); } addChild(commentBase); inputTF.x = 12; inputTF.y = this.stage.stageHeight-24-110; inputTF.width = this.stage.stageWidth - 24; inputTF.height = 108; inputTF.text = "ASオタク[m:pc][m:shadow][m:book]の諸君ハロー[m:sign01]\n[m:impact]むしむし蒸し暑い日[m:sun]が続いていますね[m:wobbly]。涼しい部屋[m:chair]でゲーム[m:tv][m:game]ばっかりしてないで、海[m:wave]へ山[m:fuji]へ外[m:shoe]で遊び[m:run]ましょう!\nそれと食事[m:restaurant]も忘れちゃいけない[m:sign02]暑い日にこそ熱い食べ物[m:noodle]で、たくさん汗[m:sweat01]をかいて[m:beer]たくさん笑って[m:smile]、ぐっすり[m:sleepy]眠れば、心も体も夏バテ知らず[m:up]\n心技体揃ってこそコレFlash[m:shine]クリエイター[m:happy01]なり。\nとか無理してたくさん絵文字を使ってみましたが、なんか電車内のレンタルパレットの広告みたいですよね。ヤングっぽいケータイ文章の勘所が全くわかりません。\n***\nこのAS3のコードはMITライセンスですが、絵はGPLライセンスです。ご注意ください。"; inputTF.border = true; inputTF.type = "input"; inputTF.wordWrap = true; addChild(inputTF); var btnText:TextField = new TextField(); btnText.x = this.stage.stageWidth-90; btnText.y = this.stage.stageHeight - 18; btnText.width = 170; btnText.height = 21; btnText.text = "to TextBlock"; btnText.selectable = false; addChild(btnText); var button:Sprite = new Sprite(); button.graphics.lineStyle (2, 0x999999, 1.0); button.graphics.beginFill (0xCCCCCC, 0); button.graphics.drawRoundRect (this.stage.stageWidth-110, this.stage.stageHeight - 22 , 100 , 20 , 10 , 10); button.buttonMode = true; button.addEventListener(MouseEvent.CLICK,CLICK); addChild(button); CLICK(); } private function CLICK(e:MouseEvent = null):void { while (commentBase.numChildren) { commentBase.removeChildAt(0); } var fontDescription:FontDescription = new FontDescription("MS Mincho"); var format:ElementFormat = new ElementFormat(); format.fontSize = 15; format.fontDescription = fontDescription; var txt_array:Array = inputTF.text.split("\r"); var linePosition:Number = stage.stageWidth - 30; for (var i:int = 0; i < txt_array.length; i++) { setTB(groupElementFromText(txt_array[i])); } function setTB(groupElmt:GroupElement):void{ var textBlock:TextBlock = new TextBlock(groupElmt,null,(new EastAsianJustifier()),TextRotation.ROTATE_90); var previousLine:TextLine = null; while (true) { var textLine:TextLine = textBlock.createTextLine(previousLine,300); if (textLine == null) break; textLine.y = 16; textLine.x = linePosition; linePosition -= 24; commentBase.addChild(textLine); previousLine = textLine; } } } private function groupElementFromText(txt:String):GroupElement{ var i:int; var format:ElementFormat = new ElementFormat((new FontDescription("MS Mincho")),15); var format3:ElementFormat = new ElementFormat(); format3.baselineShift = (20-format.fontSize)/2; //[m:sun]の部分を取り出す。 var pattern:RegExp = /\[m:[a-zA-Z0-9]+\]/g; var num_array:Array = txt.match(pattern); var str_array:Array = txt.split(pattern); var _length:int = Math.max(num_array.length , str_array.length); var txt_array:Array = []; //文章と絵文字を組み合わせる。 for (i = 0; i < _length; i++) { if(str_array[i]){ txt_array.push(str_array[i]) } if(num_array[i]){ var num:int = numFromName.indexOf(num_array[i].slice(3,-1)); txt_array.push(num) } } //文章と絵文字を場合分けしてgroupVectorに入れる。 var groupVector:Vector.<ContentElement> = new Vector.<ContentElement>(); for (i = 0; i < txt_array.length; i++) { if(isNaN(txt_array[i])){ var textElement:TextElement = new TextElement(txt_array[i], format); groupVector.push(textElement); }else{ var graphicElmt1:GraphicElement = new GraphicElement(newBitmap({draw:emotionIcon["m"+txt_array[i]]}) ,24, 24, format3); graphicElmt1.textRotation = TextRotation.ROTATE_270; groupVector.push(graphicElmt1); } } var groupElmt:GroupElement = new GroupElement(groupVector); return groupElmt; } private function newBitmap(... args):Bitmap { var _length:int = args.length; var bd:BitmapData; var bitmap:Bitmap; var strObj:Object = { width:100 , height:100, transparent:true, fillColor:0xFFFFFFFF }; var tempDO:DisplayObject; for (var i:int = 0; i < _length; i++) { var _obj:Object = args[i]; for (var _str:String in _obj) { if (_str == "draw") { tempDO = _obj[_str]; strObj.width = _obj[_str].width; strObj.height = _obj[_str].height; }else { strObj[_str] = _obj[_str]; } } } bd = new BitmapData(strObj.width, strObj.height, strObj.transparent, strObj.fillColor); if (tempDO) { bd.draw(tempDO) } bitmap = new Bitmap(bd); return bitmap; } } } class MultiLoaderClass{ import flash.system.Security; import flash.net.URLRequest; import flash.net.URLLoader; import flash.events.Event; import flash.events.IOErrorEvent; import flash.display.Loader; private var onComplete:Function = function():void{}; private var loadNum:int; private var loadCompNum:int; public function MultiLoaderClass(_str:String = null){ if(_str != null){ Security.loadPolicyFile(_str); } } public function setLoad(__item_array:Array,_onComp:Function = null):Array{ loadCompNum = loadNum = 0; onComplete = _onComp; var _array:Array = new Array(); var _length:int = __item_array.length; for (var i:int = 0; i < _length; i++) { if(__item_array[i] == null){continue}; var _extension:String = __item_array[i].substr(-4,4).toLowerCase();//拡張子を取り出す。 if(_extension == ".xml"){ loadNum ++; _array[i] = fnURLLoader(__item_array[i]); }else if(_extension == ".jpg" || _extension == ".png" || _extension == ".gif"){ loadNum ++; _array[i] = fnLoader(__item_array[i]); }else{ //_array[i] = null; } } return _array; } private function fnURLLoader(__url:String):URLLoader{ var _loader : URLLoader = new URLLoader(); _loader.load(new URLRequest(__url)); _loader.addEventListener (Event.COMPLETE,completeHandler); _loader.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler); return _loader; } private function fnLoader(__url:String):Loader{ var _loader:Loader = new Loader(); _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); _loader.load(new URLRequest(__url)); //_loader.name = __url; return _loader; } private function completeHandler(event:Event = null):void { loadCompNum ++; if(loadCompNum == loadNum){ onComplete(); } //var loaderInfo:LoaderInfo=event.currentTarget as LoaderInfo; //var loader:Loader=loaderInfo.loader; //addChild(loader); } private function ioErrorHandler(event:IOErrorEvent):void { //event.text = "Error #2035: URL が見つかりません。 URL: file:///~~~~~"; //event.text = "Error #2036: 読み込みが未完了です。 URL: http://~~~~~"; //から、URLのみを取り出す。 //trace(String(event.text).substr(String(event.text).indexOf(" URL: ")+6),"*****"); completeHandler(); } } |