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

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

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


FORKED
  1. // forked from naoto5959's Unionチャットサンプル
  2. package
  3. {
  4. import com.bit101.components.*;
  5. import flash.display.*;
  6. import flash.events.*;
  7. import flash.net.*;
  8. import flash.ui.Keyboard;
  9. import net.user1.logger.*;
  10. import net.user1.reactor.*;
  11. import org.libspark.betweenas3.*;
  12. import org.libspark.betweenas3.easing.*;
  13. import org.libspark.betweenas3.tweens.*;
  14. /**
  15. * http://fla.la/archives/364
  16. * Union Platform(5) チャットを作る
  17. * 解説用コードです。
  18. * @author Copyright (C) naoto koshikawa, All Rights Reserved.
  19. */
  20. public class TryUnion2 extends Sprite
  21. {
  22. //----------------------------------------------------------------------
  23. // static properties
  24. //----------------------------------------------------------------------
  25. //------------------------------
  26. // public static properties
  27. //------------------------------
  28. /**
  29. * 接続先Union Server host
  30. */
  31. public static const UNION_SERVER_HOST:String = "tryunion.com";
  32. /**
  33. * 接続先Union Server port
  34. */
  35. public static const UNION_SERVER_PORT:Number = 9100;
  36. /**
  37. * Room ID
  38. */
  39. public static const ROOM_CHAT:String = "com.asmple.union.chat";
  40. /**
  41. * アバターURL
  42. */
  43. public static const AVATAR_URL:String = "http://swf.wonderfl.net/swf/usercode/a/af/afe5/afe5412cf117b348681e9eb6e3595035cabd06e6.swf";
  44. /**
  45. * アバターの向いている方向(上)
  46. */
  47. public static const DIRECTION_UP:uint = 0;
  48. /**
  49. * アバターの向いている方向(右)
  50. */
  51. public static const DIRECTION_RIGHT:uint = 1;
  52. /**
  53. * アバターの向いている方向(下)
  54. */
  55. public static const DIRECTION_DOWN:uint = 2;
  56. /**
  57. * アバターの向いている方向(左)
  58. */
  59. public static const DIRECTION_LEFT:uint = 3;
  60. //----------------------------------------------------------------------
  61. // properties
  62. //----------------------------------------------------------------------
  63. //------------------------------
  64. // private properties
  65. //------------------------------
  66. /**
  67. * ユーザーインターフェース
  68. */
  69. private var _ui:UnionChatUI;
  70. /**
  71. * ClientグローバルAttribute の初期値を格納します。
  72. */
  73. private var _clientData:Object = {};
  74. /**
  75. * Reactor
  76. */
  77. private var _reactor:Reactor;
  78. /**
  79. * Room Manager
  80. */
  81. private var _roomManager:RoomManager;
  82. /**
  83. * ReactorのLogger
  84. */
  85. private var _logger:Logger;
  86. /**
  87. * 現在のRoom
  88. */
  89. private var _room:Room;
  90. /**
  91. * 自身のClient
  92. */
  93. private var _self:IClient;
  94. /**
  95. * avatarのIDs
  96. */
  97. private var _avatars:Object = { };
  98. /**
  99. * 現在テキスト入力中かを判別します。
  100. */
  101. private var _isInputText:Boolean;
  102. /**
  103. * join or observe
  104. */
  105. private var _attempt:String = "";
  106. //----------------------------------------------------------------------
  107. // methods
  108. //----------------------------------------------------------------------
  109. //------------------------------
  110. // public methods
  111. //------------------------------
  112. /**
  113. * 一番最初に実行されるメソッドです。
  114. */
  115. public function TryUnion2()
  116. {
  117. // ユーザーインターフェースを作成します。
  118. addChild(_ui = new UnionChatUI());
  119. _ui.currentStatusLabel.text = "connecting...";
  120. _ui.playLoading();
  121. // Client Attributeの初期値を作成します。
  122. var initialX:Number = int(Math.random() * _ui.avatarField.width);
  123. var initialY:Number = int(Math.random() * _ui.avatarField.height);
  124. var initialDirection:Number = int(Math.random() * 4);
  125. _clientData = {
  126. nickname: "guest",
  127. color: Math.random() * 0x888888,
  128. avatar: [initialX, initialY, initialDirection, 0].join(",")
  129. };
  130. // UnionServerへ接続します。
  131. connect();
  132. }
  133. /**
  134. * UnionServerへ接続します。
  135. */
  136. private function connect():void
  137. {
  138. _reactor = new Reactor(null, false);
  139. _reactor.addEventListener(ReactorEvent.READY, _reactor_readyHandler);
  140. _reactor.addEventListener(ReactorEvent.CLOSE, _reactor_closeHandler);
  141. _reactor.connect(UNION_SERVER_HOST, UNION_SERVER_PORT);
  142. }
  143. /**
  144. * UserEventを登録します。
  145. */
  146. private function activate():void
  147. {
  148. _ui.stopLoading();
  149. // observeボタンの動作を登録します。
  150. _ui.observeButton.addEventListener(MouseEvent.CLICK,
  151. _ui_observeButton_clickHandler);
  152. // joinボタンの動作を登録します。
  153. _ui.joinButton.addEventListener(MouseEvent.CLICK,
  154. _ui_joinButton_clickHandler);
  155. // updateボタンの動作を登録します。
  156. _ui.updateButton.addEventListener(MouseEvent.CLICK,
  157. _ui_updateButton_clickHandler);
  158. // メッセージ入力時の動作を登録します。
  159. _ui.messageField.addEventListener(TextEvent.TEXT_INPUT,
  160. _ui_messageField_textInputHandler);
  161. _ui.messageField.addEventListener(KeyboardEvent.KEY_UP,
  162. _ui_messageField_keyUpHandler);
  163. // アバターフィールドクリック時の動作を登録します。
  164. _ui.avatarField.addEventListener(MouseEvent.CLICK,
  165. _ui_avatarField_clickHandler);
  166. }
  167. /**
  168. * UserEventを解除します。
  169. */
  170. private function deactivate():void
  171. {
  172. // observeボタンの動作を解除します。
  173. _ui.observeButton.removeEventListener(MouseEvent.CLICK,
  174. _ui_observeButton_clickHandler);
  175. // joinボタンの動作を解除します。
  176. _ui.joinButton.removeEventListener(MouseEvent.CLICK,
  177. _ui_joinButton_clickHandler);
  178. // updateボタンの動作を解除します。
  179. _ui.updateButton.removeEventListener(MouseEvent.CLICK,
  180. _ui_updateButton_clickHandler);
  181. // メッセージ入力時の動作を解除します。
  182. _ui.messageField.removeEventListener(TextEvent.TEXT_INPUT,
  183. _ui_messageField_textInputHandler);
  184. _ui.messageField.removeEventListener(KeyboardEvent.KEY_UP,
  185. _ui_messageField_keyUpHandler);
  186. // アバターフィールドクリック時の解除を設定します。
  187. _ui.avatarField.removeEventListener(MouseEvent.CLICK,
  188. _ui_avatarField_clickHandler);
  189. }
  190. /**
  191. * RoomEventのListener登録
  192. */
  193. private function addRoomEventListeners():void
  194. {
  195. _room.addEventListener(RoomEvent.OBSERVE, _room_observeHandler);
  196. _room.addEventListener(RoomEvent.JOIN, _room_joinHandler);
  197. _room.addEventListener(RoomEvent.CLIENT_COUNT
  198. , _room_clientCountHandler);
  199. _room.addEventListener(RoomEvent.SYNCHRONIZE
  200. , _room_synchronizeHandler);
  201. _room.addEventListener(RoomEvent.ADD_CLIENT
  202. , _room_addClientHandler);
  203. _room.addEventListener(RoomEvent.REMOVE_CLIENT
  204. , _room_removeClientHandler);
  205. _room.addEventListener(RoomEvent.UPDATE_CLIENT_ATTRIBUTE
  206. , _room_updateClientAttributeHandler);
  207. _room.addMessageListener("chatMessage", chatMessageHandler);
  208. }
  209. /**
  210. * RoomEventのListener解除
  211. */
  212. private function removeRoomEventListeners():void
  213. {
  214. _room.removeEventListener(RoomEvent.OBSERVE, _room_observeHandler);
  215. _room.removeEventListener(RoomEvent.JOIN, _room_joinHandler);
  216. _room.removeEventListener(RoomEvent.CLIENT_COUNT
  217. , _room_clientCountHandler);
  218. _room.removeEventListener(RoomEvent.SYNCHRONIZE
  219. , _room_synchronizeHandler);
  220. _room.removeEventListener(RoomEvent.ADD_CLIENT
  221. , _room_addClientHandler);
  222. _room.removeEventListener(RoomEvent.REMOVE_CLIENT
  223. , _room_removeClientHandler);
  224. _room.removeEventListener(RoomEvent.UPDATE_CLIENT_ATTRIBUTE
  225. , _room_updateClientAttributeHandler);
  226. _room.removeMessageListener("chatMessage", chatMessageHandler);
  227. }
  228. /**
  229. * _ui.avatarFieldにアバターを追加します。
  230. * @param client
  231. */
  232. private function addAvatar(client:IClient):void
  233. {
  234. var loader:Loader = new Loader();
  235. loader.contentLoaderInfo.addEventListener(Event.INIT,
  236. function(event:Event):void {
  237. var avatar:* = loader.content;
  238. avatar.color = client.getAttribute(null, "color");
  239. var values:Array = client.getAttribute(null, "avatar").split(",");
  240. updateAvatar.apply(null, [avatar, false].concat(values));
  241. _avatars[client.getClientID()] = avatar;
  242. _ui.avatarField.addChild(avatar);
  243. }
  244. );
  245. loader.load(new URLRequest(AVATAR_URL));
  246. }
  247. /**
  248. * _ui.avatarFieldからアバターを削除します。
  249. * @param client
  250. */
  251. private function removeAvatar(client:IClient):void
  252. {
  253. var avatar:* = _avatars[client.getClientID()];
  254. if (!_ui.avatarField.contains(avatar)) return;
  255. _ui.avatarField.removeChild(avatar);
  256. delete _avatars[client.getClientID()];
  257. }
  258. /**
  259. * アバターを状態更新します。
  260. * @param avatar
  261. * @param isTween
  262. * @param positionX
  263. * @param positionY
  264. * @param direction
  265. * @param distance
  266. */
  267. private function updateAvatar(
  268. avatar:*,
  269. isTween:Boolean,
  270. positionX:Number, positionY:Number,
  271. direction:uint, distance:Number):void
  272. {
  273. avatar.play();
  274. switch (direction)
  275. {
  276. case DIRECTION_UP:
  277. avatar.back();
  278. break;
  279. case DIRECTION_RIGHT:
  280. avatar.right();
  281. break;
  282. case DIRECTION_DOWN:
  283. avatar.front();
  284. break;
  285. case DIRECTION_LEFT:
  286. avatar.left();
  287. break;
  288. }
  289. if (isTween)
  290. {
  291. var tween:IObjectTween = BetweenAS3.tween(
  292. avatar,
  293. {x: positionX, y: positionY },
  294. {x: avatar.x, y: avatar.y },
  295. distance * 0.005, Sine.easeOut
  296. );
  297. tween.play();
  298. }
  299. else
  300. {
  301. avatar.x = positionX;
  302. avatar.y = positionY;
  303. }
  304. }
  305. /**
  306. * Message chatMessageを受け取った際に実行されるハンドラです。
  307. * @param fromClient
  308. * @param message
  309. */
  310. private function chatMessageHandler(
  311. fromClient:IClient, message:String):void
  312. {
  313. var nickname:String = decodeURI(
  314. fromClient.getAttribute(null, "nickname")
  315. );
  316. var color:uint = uint(fromClient.getAttribute(null, "color"));
  317. message = decodeURI(message);
  318. _ui.appendMessage(
  319. "[chatMessage]" + nickname + "(" + fromClient.getClientID() + ")"
  320. + "さん「" + message + "」",
  321. color
  322. );
  323. }
  324. //----------------------------------------------------------------------
  325. // event handler
  326. //----------------------------------------------------------------------
  327. //------------------------------
  328. // User Event系
  329. //------------------------------
  330. /**
  331. * _ui.observeButtonをクリックした際に実行されるハンドラ
  332. * @param event
  333. */
  334. private function _ui_observeButton_clickHandler(event:MouseEvent):void
  335. {
  336. _ui.playLoading();
  337. removeRoomEventListeners();
  338. deactivate();
  339. _attempt = "observe";
  340. _reactor.disconnect();
  341. connect();
  342. }
  343. /**
  344. * _ui.joinButtonをクリックした際に実行されるハンドラ
  345. * @param event
  346. */
  347. private function _ui_joinButton_clickHandler(event:MouseEvent):void
  348. {
  349. _ui.playLoading();
  350. removeRoomEventListeners();
  351. deactivate();
  352. _attempt = "join";
  353. _reactor.disconnect();
  354. connect();
  355. }
  356. /**
  357. * _ui.updateButtonをクリックした際に実行されるハンドラ
  358. * @param event
  359. */
  360. private function _ui_updateButton_clickHandler(event:MouseEvent):void
  361. {
  362. var nickname:String = _ui.nicknameField.text;
  363. var color:uint = _ui.colorChooser.value;
  364. if (nickname.length == 0)
  365. {
  366. _ui.appendMessage("ニックネームは空に出来ません", 0xFF0000);
  367. return;
  368. }
  369. _self.setAttribute("nickname", encodeURI(nickname));
  370. _self.setAttribute("color", String(color));
  371. }
  372. /**
  373. * _ui.messageFieldにテキストを入力中に実行されるハンドラ
  374. * @param event
  375. */
  376. private function _ui_messageField_textInputHandler(event:TextEvent):void
  377. {
  378. _isInputText = true;
  379. }
  380. /**
  381. * _ui.messageFieldにテキストを入力中、キーをアップした際に実行されるハンドラ
  382. * @param event
  383. */
  384. private function _ui_messageField_keyUpHandler(event:KeyboardEvent):void
  385. {
  386. if (_ui.messageField.text.length == 0) return;
  387. if (!_isInputText && event.keyCode == Keyboard.ENTER)
  388. {
  389. var message:String = encodeURI(_ui.messageField.text);
  390. _room.sendMessage("chatMessage", true, null, message);
  391. _ui.messageField.text = "";
  392. }
  393. _isInputText = false;
  394. }
  395. /**
  396. * _ui.avatarFieldをクリックした際に実行されるハンドラ
  397. * @param event
  398. */
  399. private function _ui_avatarField_clickHandler(event:MouseEvent):void
  400. {
  401. // 入室していなければ何もしない。
  402. if (!_self.isInRoom(ROOM_CHAT)) return;
  403. var avatar:* = _avatars[_self.getClientID()];
  404. var direction:uint;
  405. var distanceX:Number = event.localX - avatar.x;
  406. var distanceY:Number = event.localY - avatar.y;
  407. var distance:Number = Math.sqrt(
  408. distanceX * distanceX + distanceY * distanceY
  409. );
  410. // 小数点を切り捨てる
  411. distance = int(distance);
  412. // アバターが向く方向を決定
  413. if (Math.abs(distanceX) <= Math.abs(distanceY))
  414. {
  415. if (distanceY < 0) direction = DIRECTION_UP;
  416. else direction = DIRECTION_DOWN;
  417. }
  418. else
  419. {
  420. if (distanceX < 0) direction = DIRECTION_LEFT;
  421. else direction = DIRECTION_RIGHT;
  422. }
  423. // Client Attributeを更新する。
  424. var values:Array = [event.localX-16, event.localY-16, direction, distance];
  425. _self.setAttribute("avatar", values.join(","));
  426. }
  427. //------------------------------
  428. // Reactor系イベント
  429. //------------------------------
  430. /**
  431. * ReactorとUnion Serverとの接続が完了した際に実行されるハンドラ
  432. * @param event
  433. */
  434. private function _reactor_readyHandler(event:ReactorEvent):void
  435. {
  436. _ui.currentStatusLabel.text = "connect";
  437. _ui.appendMessage("ReadyEvent.READY");
  438. // 自身のClient参照を取得
  439. _self = _reactor.getClientManager().self();
  440. _self.setAttribute("nickname", _clientData.nickname);
  441. _self.setAttribute("color", _clientData.color);
  442. _self.setAttribute("avatar", _clientData.avatar);
  443. // 自身のnicknameを設定
  444. _ui.nicknameField.text = _clientData.nickname;
  445. // 自身のcolorを設定
  446. _ui.colorChooser.value = _clientData.color;
  447. // RoomManager参照を取得
  448. _roomManager = _reactor.getRoomManager();
  449. _roomManager.addEventListener(
  450. RoomManagerEvent.CREATE_ROOM_RESULTS,
  451. _roomManager_createRoomReslutsHandler);
  452. // Roomへの接続が0となっても残す設定
  453. var roomSetting:RoomSettings = new RoomSettings();
  454. roomSetting.dieOnEmpty = false;
  455. // 指定したRoom IDを作成する。
  456. _room = _roomManager.createRoom(ROOM_CHAT, roomSetting);
  457. addRoomEventListeners();
  458. // logger参照を取得
  459. _logger = _reactor.getLog();
  460. _logger.addEventListener(LogEvent.UPDATE, _logger_updateHandler);
  461. }
  462. /**
  463. * ReactorからUnion Serverの接続が切断した際に実行されるハンドラ
  464. * @param event
  465. */
  466. private function _reactor_closeHandler(event:ReactorEvent):void
  467. {
  468. _ui.currentStatusLabel.text = "connect closed.";
  469. _ui.appendMessage("ReadyEvent.CLOSE");
  470. _ui.currentClientsLabel.text = "occupants:unknown";
  471. for (var i:uint = 0; i < _ui.avatarField.numChildren; i++)
  472. {
  473. _ui.avatarField.removeChildAt(0);
  474. }
  475. }
  476. /**
  477. * RoomManagerからRoomの作成結果が通知された際に実行されるハンドラ
  478. * @param event
  479. */
  480. private function _roomManager_createRoomReslutsHandler(
  481. event:RoomManagerEvent):void
  482. {
  483. var status:String = event.getStatus();
  484. _ui.appendMessage("RoomManagerEvent.CREATE_ROOM_RESULTS:" + status);
  485. if (event.getStatus() == Status.SUCCESS
  486. || event.getStatus() == Status.ROOM_EXISTS)
  487. {
  488. if (_attempt == "observe") _room.observe();
  489. else if (_attempt == "join") _room.join();
  490. else activate();
  491. }
  492. }
  493. /**
  494. * LoggerからLogの登録が通知された際に実行されるハンドラ
  495. * @param event
  496. */
  497. private function _logger_updateHandler(event:LogEvent):void
  498. {
  499. var level:String = event.getLevel();
  500. if (level == Logger.DEBUG || level == Logger.INFO) return;
  501. _ui.appendMessage(level + ":" + event.getMessage(), 0xFF0000);
  502. }
  503. /**
  504. * Roomへobserveした際に実行されるハンドラ
  505. */
  506. private function _room_observeHandler(event:RoomEvent):void
  507. {
  508. _ui.currentStatusLabel.text = "observe";
  509. _ui.appendMessage("RoomEvent.OBSERVER");
  510. _ui.stopLoading();
  511. activate();
  512. }
  513. /**
  514. * Roomへjoinした際に実行されるハンドラ
  515. */
  516. private function _room_joinHandler(event:RoomEvent):void
  517. {
  518. _ui.currentStatusLabel.text = "join";
  519. _ui.appendMessage("RoomEvent.JOIN");
  520. _ui.stopLoading();
  521. activate();
  522. }
  523. /**
  524. * Room内のClient数が更新された際に実行されるハンドラ
  525. * @param event
  526. */
  527. private function _room_clientCountHandler(event:RoomEvent):void
  528. {
  529. _ui.appendMessage("RoomEvent.CLIENT_COUNT");
  530. _ui.currentClientsLabel.text = "occupants:" + event.getNumClients() + "人";
  531. }
  532. /**
  533. * Room内の情報が同期された際に実行されるハンドラ
  534. * @param event
  535. */
  536. private function _room_synchronizeHandler(event:RoomEvent):void
  537. {
  538. _ui.appendMessage("RoomEvent.SYNCHRONIZE");
  539. for each (var client:IClient in _room.getClients())
  540. {
  541. addAvatar(client);
  542. }
  543. }
  544. /**
  545. * Room内にClientが追加された際に実行されるハンドラ
  546. * @param event
  547. */
  548. private function _room_addClientHandler(event:RoomEvent):void
  549. {
  550. _ui.appendMessage("RoomEvent.ADD_CLIENT");
  551. addAvatar(event.getClient());
  552. }
  553. /**
  554. * Room内からClientが削除された際に実行されるハンドラ
  555. * @param event
  556. */
  557. private function _room_removeClientHandler(event:RoomEvent):void
  558. {
  559. _ui.appendMessage("RoomEvent.REMOVE_CLIENT");
  560. removeAvatar(event.getClient());
  561. }
  562. /**
  563. * Room内のClient Attributeが更新された際に実行されるハンドラ
  564. * @param event
  565. */
  566. private function _room_updateClientAttributeHandler(event:RoomEvent):void
  567. {
  568. _ui.appendMessage("RoomEvent.UPDATE_CLIENT_ATTRIBUTE");
  569. var attribute:Attribute = event.getChangedAttr();
  570. _ui.appendMessage(" id:" + event.getClientID()
  571. + ", name:" + attribute.name
  572. + ", oldValue:" + attribute.oldValue
  573. + ", value:" + attribute.value
  574. );
  575. var avatar:* = _avatars[event.getClientID()];
  576. if (!avatar) return;
  577. switch (attribute.name)
  578. {
  579. case "color":
  580. {
  581. avatar.color = uint(attribute.value);
  582. break;
  583. }
  584. case "avatar":
  585. {
  586. var values:Array = attribute.value.split(",");
  587. updateAvatar.apply(null, [avatar, true].concat(values));
  588. break;
  589. }
  590. }
  591. }
  592. }
  593. }
  594. //--------------------------------------------------------------------------
  595. // 以降ヘルパークラス
  596. //--------------------------------------------------------------------------
  597. import com.bit101.components.*;
  598. import flash.display.*;
  599. import flash.text.*;
  600. import flash.events.*;
  601. import org.libspark.betweenas3.*;
  602. import org.libspark.betweenas3.easing.*;
  603. import org.libspark.betweenas3.tweens.*;
  604. /**
  605. * Union Chat User Interface
  606. * @author Copyright (C) naoto koshikawa, All Rights Reserved.
  607. */
  608. class UnionChatUI extends Sprite
  609. {
  610. //----------------------------------------------------------------------
  611. // static properties
  612. //----------------------------------------------------------------------
  613. //------------------------------
  614. // public static properties
  615. //------------------------------
  616. /** ステージの横 */
  617. public static const STAGE_WIDTH:Number = 465;
  618. /** ステージの縦 */
  619. public static const STAGE_HEIGHT:Number = 465;
  620. //----------------------------------------------------------------------
  621. // properties
  622. //----------------------------------------------------------------------
  623. //------------------------------
  624. // public properties
  625. //------------------------------
  626. private var _loadingCircle:Shape;
  627. /** ローディング中のサークル */
  628. public function get loadingCircle():Shape
  629. {
  630. return _loadingCircle;
  631. }
  632. private var _loadingTween:IObjectTween;
  633. /** ローディング中のサークルのトゥイーン */
  634. public function get loadingTween():IObjectTween
  635. {
  636. return _loadingTween;
  637. }
  638. private var _messageField:TextField;
  639. /** メッセージ入力フィールド */
  640. public function get messageField():TextField
  641. {
  642. return _messageField;
  643. }
  644. private var _colorChooser:ColorChooser;
  645. /** ユーザの色設定 */
  646. public function get colorChooser():ColorChooser
  647. {
  648. return _colorChooser;
  649. }
  650. private var _nicknameField:TextField;
  651. /** ユーザの名前 */
  652. public function get nicknameField():TextField
  653. {
  654. return _nicknameField;
  655. }
  656. private var _udpateButon:PushButton;
  657. /**
  658. * ユーザ設定の変更ボタン
  659. */
  660. public function get updateButton():PushButton
  661. {
  662. return _udpateButon;
  663. }
  664. private var _observeButton:PushButton;
  665. /**
  666. * Roomへobserveするボタン
  667. */
  668. public function get observeButton():PushButton
  669. {
  670. return _observeButton;
  671. }
  672. private var _joinButton:PushButton;
  673. /**
  674. * Roomへjoinするボタン
  675. */
  676. public function get joinButton():PushButton
  677. {
  678. return _joinButton;
  679. }
  680. private var _currentStatusLabel:Label;
  681. /**
  682. * 現在の状態を表示
  683. */
  684. public function get currentStatusLabel():Label
  685. {
  686. return _currentStatusLabel;
  687. }
  688. private var _currentClientsLabel:Label;
  689. /**
  690. * 現在のクライアント数を表示
  691. */
  692. public function get currentClientsLabel():Label
  693. {
  694. return _currentClientsLabel;
  695. }
  696. private var _avatarField:Sprite;
  697. /**
  698. * アバターフィールド
  699. */
  700. public function get avatarField():Sprite
  701. {
  702. return _avatarField;
  703. }
  704. private var _messageList:TextField;
  705. /**
  706. * メッセージフィールド
  707. */
  708. public function get messageList():TextField
  709. {
  710. return _messageList;
  711. }
  712. //----------------------------------------------------------------------
  713. // methods
  714. //----------------------------------------------------------------------
  715. //------------------------------
  716. // public methods
  717. //------------------------------
  718. /**
  719. * constructor
  720. */
  721. public function UnionChatUI()
  722. {
  723. buildUI();
  724. }
  725. /**
  726. * ローディングサークルを再生します。
  727. */
  728. public function playLoading():void
  729. {
  730. addChild(_loadingCircle);
  731. _loadingCircle.visible = true;
  732. _loadingTween = BetweenAS3.tween(_loadingCircle,
  733. { rotation:0 }, { rotation:360 },
  734. 1.2, Sine.easeInOut
  735. );
  736. _loadingTween.stopOnComplete = false;
  737. _loadingTween.play();
  738. }
  739. /**
  740. * ローディングサークルを停止します。
  741. */
  742. public function stopLoading():void
  743. {
  744. _loadingCircle.visible = false;
  745. _loadingTween.stop();
  746. }
  747. /**
  748. * メッセージリストに追加します。
  749. */
  750. public function appendMessage(text:String, color:uint = 0x000000):void
  751. {
  752. _messageList.htmlText += '' + text + "
    ";
  753. _messageList.scrollV = _messageList.maxScrollV;
  754. }
  755. //------------------------------
  756. // private methods
  757. //------------------------------
  758. /**
  759. * User Interfaceを作成します。
  760. */
  761. private function buildUI():void
  762. {
  763. createLoadingCircle();
  764. createMessageField();
  765. createUserSetting();
  766. createRoomButton();
  767. createField();
  768. createMessageList();
  769. }
  770. /**
  771. * ロード中を示す画像を表示します。
  772. */
  773. private function createLoadingCircle():void
  774. {
  775. _loadingCircle = new Shape();
  776. var radian:Number;
  777. var radius:Number = 25;
  778. var posX:Number;
  779. var posY:Number;
  780. for (var i:uint = 0; i < 10; i++)
  781. {
  782. radian = (Math.PI * 2) * (i/10) - Math.PI;
  783. posX = Math.cos(radian) * radius;
  784. posY = Math.sin(radian) * radius;
  785. _loadingCircle.graphics.beginFill(0xFFFFFF, 1.0 - (i/10) + 0.1);
  786. _loadingCircle.graphics.drawCircle(posX, posY, 5);
  787. }
  788. _loadingCircle.x = STAGE_WIDTH / 2;
  789. _loadingCircle.y = STAGE_HEIGHT / 2;
  790. _loadingCircle.visible = false;
  791. addChild(_loadingCircle);
  792. }
  793. /**
  794. * 共有するMessageを入力するフィールドを作成します。
  795. */
  796. private function createMessageField():void
  797. {
  798. _messageField = createTextField(this, 0, STAGE_HEIGHT - 19)
  799. _messageField.type = TextFieldType.INPUT;
  800. _messageField.width = STAGE_WIDTH - 1;
  801. _messageField.height = 18;
  802. }
  803. /**
  804. * ClientのAttributeを更新するUIを作成します。
  805. */
  806. private function createUserSetting():void
  807. {
  808. _colorChooser = new ColorChooser(this, 0, STAGE_HEIGHT - 40, 0x000000);
  809. var nicknameLabel:Label = new Label(this,
  810. _colorChooser.width + 5, STAGE_HEIGHT - 40, "nickname:");
  811. nicknameLabel.setSize(45, 10);
  812. _nicknameField = createTextField(this,
  813. nicknameLabel.x + nicknameLabel.width + 5, STAGE_HEIGHT - 40, 10, "");
  814. _nicknameField.type = TextFieldType.INPUT;
  815. _nicknameField.width = 100;
  816. _nicknameField.height = 18;
  817. _udpateButon = new PushButton(this,
  818. _nicknameField.x + _nicknameField.width + 5,
  819. STAGE_HEIGHT - 40, "Update Attribute");
  820. }
  821. /**
  822. * Roomボタンを作成します。
  823. */
  824. private function createRoomButton():void
  825. {
  826. var margin:Number = 5;
  827. _observeButton = new PushButton(this, margin, margin, "observe");
  828. _observeButton.setSize(60, 20);
  829. _joinButton = new PushButton(this,
  830. _observeButton.x + _observeButton.width + margin,
  831. margin,
  832. "join"
  833. );
  834. _joinButton.setSize(60, 20);
  835. var label:Label = new Label(this,
  836. _joinButton.x + _joinButton.width + margin,
  837. margin, "status:"
  838. );
  839. label.setSize(40, 10);
  840. _currentStatusLabel = new Label(this,
  841. label.x + label.width + margin,
  842. margin, ""
  843. );
  844. _currentStatusLabel.setSize(100, 20);
  845. _currentClientsLabel = new Label(this,
  846. _currentStatusLabel.x + _currentStatusLabel.width + margin,
  847. margin, "occupants:unknown"
  848. );
  849. }
  850. /**
  851. * ユーザのアバターが活躍する(?)フィールドを作成します。
  852. */
  853. private function createField():void
  854. {
  855. _avatarField = new Sprite();
  856. _avatarField.graphics.beginFill(0xEEEEEE);
  857. _avatarField.graphics.drawRoundRect(0, 0, 455, 300, 10, 10);
  858. _avatarField.x = 5;
  859. _avatarField.y = 30;
  860. _avatarField.mouseChildren = false;
  861. addChild(_avatarField);
  862. }
  863. /**
  864. * メッセージを表示するフィールドを作成します。
  865. */
  866. private function createMessageList():void
  867. {
  868. _messageList = createTextField(this, 5, 335);
  869. _messageList.width = 455;
  870. _messageList.height = 80;
  871. _messageList.wordWrap = true;
  872. _messageList.multiline = true;
  873. }
  874. }
  875. /**
  876. * TextFieldの作成ヘルパーです。
  877. */
  878. function createTextField(
  879. container:DisplayObjectContainer, positionX:Number, positionY:Number,
  880. size:Number = 10, text:String = "", borderColor:uint = 0x000000,
  881. backgroundColor:uint = 0xFFFFFF
  882. ):TextField
  883. {
  884. var textField:TextField = new TextField();
  885. textField.defaultTextFormat = new TextFormat("_等幅", size);
  886. textField.type = TextFieldType.DYNAMIC;
  887. textField.border = true;
  888. textField.borderColor = borderColor;
  889. textField.background = true;
  890. textField.backgroundColor = backgroundColor;
  891. textField.x = positionX;
  892. textField.y = positionY;
  893. textField.width = textField.height = 0;
  894. textField.text = text;
  895. container.addChild(textField);
  896. return textField;
  897. }
noswf
Get Adobe Flash Player