PageRenderTime 2666ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/DesktopPureAS3/src/DesktopPureAS3.as

http://facebook-actionscript-api.googlecode.com/
ActionScript | 319 lines | 265 code | 47 blank | 7 comment | 13 complexity | 64f06e187848d0bad90968efdb2c47ea MD5 | raw file
  1. package {
  2. import com.adobe.serialization.json.JSON;
  3. import com.facebook.graph.FacebookDesktop;
  4. import flash.desktop.NativeApplication;
  5. import flash.display.GradientType;
  6. import flash.display.Graphics;
  7. import flash.display.Sprite;
  8. import flash.display.StageAlign;
  9. import flash.display.StageScaleMode;
  10. import flash.events.Event;
  11. import flash.events.MouseEvent;
  12. import flash.geom.Matrix;
  13. import flash.text.TextField;
  14. import flash.text.TextFieldAutoSize;
  15. import flash.text.TextFieldType;
  16. import flash.text.TextFormat;
  17. [SWF(width="650", height="700", backgroundColor="0xffffff", frameRate="15")]
  18. public class DesktopPureAS3 extends Sprite {
  19. // Static/Constant Properties:
  20. protected static const APP_ID:String = "YOUR_APP_ID"; // Your App ID.
  21. protected static const APP_ORIGIN:String = "YOUR_APP_ORGIN"; // The site URL of your application (specified in your app settings); needed for clearing cookie when logging out.
  22. // Public Properties:
  23. public var loginBtn:Sprite;
  24. public var loginBtnLabel:TextField;
  25. public var methodText:TextField;
  26. public var methodInputText:TextField;
  27. public var postBtn:Sprite;
  28. public var postBtnLabel:TextField;
  29. public var getBtn:Sprite;
  30. public var getBtnLabel:TextField;
  31. public var postNoteText:TextField;
  32. public var postInputText:TextField;
  33. public var outputNoteText:TextField;
  34. public var clearBtn:Sprite;
  35. public var clearBtnLabel:TextField;
  36. public var outputText:TextField;
  37. // Protected Properties:
  38. protected var isLoggedIn:Boolean = false;
  39. protected var permissions:Array = ["read_stream"];
  40. // Getter/Setter Properties:
  41. // Initialization:
  42. public function DesktopPureAS3() {
  43. configUI();
  44. FacebookDesktop.init(APP_ID, handleInit);
  45. addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
  46. }
  47. // Protected Methods:
  48. protected function configUI():void {
  49. loginBtn = new Sprite();
  50. setBtnState(loginBtn, "up");
  51. loginBtnLabel = new TextField();
  52. setupTextField(loginBtnLabel);
  53. setBtnLabelText(loginBtn, loginBtnLabel, "Login");
  54. loginBtn.addChild(loginBtnLabel);
  55. loginBtn.x = 10;
  56. loginBtn.y = 10;
  57. loginBtn.addEventListener(MouseEvent.MOUSE_DOWN, handleLoginBtnMouseDown, false, 0, true);
  58. loginBtn.addEventListener(MouseEvent.CLICK, handleLoginBtnClick, false, 0, true);
  59. addChild(loginBtn);
  60. methodText = new TextField();
  61. setupTextField(methodText);
  62. methodText.text = "method:";
  63. methodText.x = 10;
  64. methodText.y = loginBtn.y + loginBtn.height + 20;
  65. addChild(methodText);
  66. methodInputText = new TextField();
  67. methodInputText.type = TextFieldType.INPUT;
  68. methodInputText.defaultTextFormat = new TextFormat("_sans", 14);
  69. methodInputText.border = true;
  70. methodInputText.text = "/me";
  71. methodInputText.width = 260;
  72. methodInputText.height = methodInputText.textHeight + 5;
  73. methodInputText.x = methodText.x + methodText.width + 10;
  74. methodInputText.y = methodText.y;
  75. addChild(methodInputText);
  76. postBtn = new Sprite();
  77. setBtnState(postBtn, "up");
  78. postBtnLabel = new TextField();
  79. setupTextField(postBtnLabel);
  80. setBtnLabelText(postBtn, postBtnLabel, "Post");
  81. postBtn.addChild(postBtnLabel);
  82. postBtn.x = methodInputText.x + methodInputText.width + 10;
  83. postBtn.y = methodInputText.y;
  84. postBtn.addEventListener(MouseEvent.MOUSE_DOWN, handlePostBtnMouseDown, false, 0, true);
  85. postBtn.addEventListener(MouseEvent.CLICK, handlePostBtnClick, false, 0, true);
  86. addChild(postBtn);
  87. getBtn = new Sprite();
  88. setBtnState(getBtn, "up");
  89. getBtnLabel = new TextField();
  90. setupTextField(getBtnLabel);
  91. setBtnLabelText(getBtn, getBtnLabel, "Get");
  92. getBtn.addChild(getBtnLabel);
  93. getBtn.x = postBtn.x + postBtn.width + 10;
  94. getBtn.y = methodInputText.y;
  95. getBtn.addEventListener(MouseEvent.MOUSE_DOWN, handleGetBtnMouseDown, false, 0, true);
  96. getBtn.addEventListener(MouseEvent.CLICK, handleGetBtnClick, false, 0, true);
  97. addChild(getBtn);
  98. postNoteText = new TextField();
  99. setupTextField(postNoteText);
  100. postNoteText.wordWrap = true;
  101. postNoteText.multiline = true;
  102. postNoteText.text = "Place JSON encoded string here to pass in POST request arguments.\nFor example, for Graph API call: /me/feed\n{ \"message\" : \"I am a message!\" }";
  103. postNoteText.width = 500;
  104. postNoteText.x = 10;
  105. postNoteText.y = methodText.y + methodText.height + 10;
  106. addChild(postNoteText);
  107. postInputText = new TextField();
  108. postInputText.type = TextFieldType.INPUT;
  109. postInputText.defaultTextFormat = new TextFormat("_sans", 14);
  110. postInputText.border = true;
  111. postInputText.wordWrap = true;
  112. postInputText.multiline = true;
  113. postInputText.width = 630;
  114. postInputText.height = 90;
  115. postInputText.x = 10;
  116. postInputText.y = postNoteText.y + postNoteText.height + 10;
  117. addChild(postInputText);
  118. outputNoteText = new TextField();
  119. setupTextField(outputNoteText);
  120. outputNoteText.text = "output: The API result appears below in a JSON encoded string";
  121. outputNoteText.x = 10;
  122. outputNoteText.y = postInputText.y + postInputText.height + 20;
  123. addChild(outputNoteText);
  124. clearBtn = new Sprite();
  125. setBtnState(clearBtn, "up");
  126. clearBtnLabel = new TextField();
  127. setupTextField(clearBtnLabel);
  128. setBtnLabelText(clearBtn, clearBtnLabel, "Clear");
  129. clearBtn.addChild(clearBtnLabel);
  130. clearBtn.x = stage.stageWidth - clearBtn.width - 10;
  131. clearBtn.y = outputNoteText.y;
  132. clearBtn.addEventListener(MouseEvent.MOUSE_DOWN, handleClearBtnMouseDown, false, 0, true);
  133. clearBtn.addEventListener(MouseEvent.CLICK, handleClearBtnClick, false, 0, true);
  134. addChild(clearBtn);
  135. outputText = new TextField();
  136. outputText.defaultTextFormat = new TextFormat("_sans", 14);
  137. outputText.x = 10;
  138. outputText.y = outputNoteText.y + outputNoteText.height + 10;
  139. outputText.border = true;
  140. outputText.wordWrap = true;
  141. outputText.multiline = true;
  142. outputText.width = 630;
  143. outputText.height = stage.stageHeight - outputText.y - 10;
  144. addChild(outputText);
  145. }
  146. protected function setupTextField(textField:TextField):void {
  147. textField.autoSize = TextFieldAutoSize.LEFT;
  148. textField.selectable = false;
  149. textField.defaultTextFormat = new TextFormat("_sans", 14);
  150. }
  151. protected function setBtnLabelText(button:Sprite, label:TextField, text:String):void {
  152. label.text = text;
  153. label.x = (button.width >> 1) - (label.width >> 1);
  154. label.y = (button.height >> 1) - (label.height >> 1);
  155. }
  156. protected function setBtnState(button:Sprite, state:String):void {
  157. var g:Graphics = button.graphics;
  158. g.clear();
  159. var matr:Matrix = new Matrix();
  160. matr.createGradientBox(30, 21, Math.PI * 0.5, 0, 0);
  161. switch (state) {
  162. case "up":
  163. button.x -= 1;
  164. button.y -= 1;
  165. g.lineStyle(1, 0x333333, 1, true);
  166. g.beginGradientFill(GradientType.LINEAR, [0xffffff, 0xcccccc], [1, 1], [0, 255], matr);
  167. break;
  168. case "down":
  169. button.x += 1;
  170. button.y += 1;
  171. g.lineStyle(1, 0x000000, 1, true);
  172. g.beginGradientFill(GradientType.LINEAR, [0xffffff, 0xaaaaaa], [1, 1], [0, 255], matr);
  173. break;
  174. }
  175. g.drawRoundRect(0, 0, 100, 21, 10, 10);
  176. g.endFill();
  177. }
  178. // Event Handlers:
  179. protected function handleInit(result:Object, fail:Object):void {
  180. if (result) { // User is already logged in.
  181. setBtnLabelText(loginBtn, loginBtnLabel, "Logout");
  182. isLoggedIn = true;
  183. } else { // User hasn't logged in.
  184. }
  185. }
  186. protected function handleLogin(result:Object, fail:Object):void {
  187. if (result) { // User successfully logged in.
  188. setBtnLabelText(loginBtn, loginBtnLabel, "Logout");
  189. isLoggedIn = true;
  190. trace(FacebookDesktop.getSession().availablePermissions);
  191. } else { // User unsuccessfully logged in.
  192. }
  193. }
  194. protected function handleLogout(success:Boolean):void {
  195. setBtnLabelText(loginBtn, loginBtnLabel, "Login");
  196. isLoggedIn = false;
  197. }
  198. protected function handleLoginBtnClick(event:MouseEvent):void {
  199. if (isLoggedIn) {
  200. FacebookDesktop.logout(handleLogout, APP_ORIGIN);
  201. } else {
  202. FacebookDesktop.login(handleLogin, permissions);
  203. }
  204. }
  205. protected function handlePostBtnClick(event:MouseEvent):void {
  206. if (isLoggedIn) {
  207. var params:Object = null;
  208. try {
  209. params = JSON.decode(postInputText.text);
  210. } catch (e:Error) {
  211. outputText.appendText("\n\nERROR DECODING JSON: " + e.message);
  212. }
  213. FacebookDesktop.api(methodInputText.text, handleAPICall, params, "POST");
  214. }
  215. }
  216. protected function handleGetBtnClick(event:MouseEvent):void {
  217. if (isLoggedIn) {
  218. FacebookDesktop.api(methodInputText.text, handleAPICall, null, "GET");
  219. }
  220. }
  221. protected function handleClearBtnClick(event:MouseEvent):void {
  222. outputText.text = "";
  223. }
  224. protected function handleAPICall(result:Object, fail:Object):void {
  225. if (result) {
  226. outputText.appendText("\n\nRESULT:\n" + JSON.encode(result));
  227. } else {
  228. outputText.appendText("\n\nFAIL:\n" + JSON.encode(fail));
  229. }
  230. }
  231. protected function handleLoginBtnMouseDown(event:MouseEvent):void {
  232. setBtnState(loginBtn, "down");
  233. stage.addEventListener(MouseEvent.MOUSE_UP, handleLoginBtnStageMouseUp, false, 0, true);
  234. }
  235. protected function handleLoginBtnStageMouseUp(event:MouseEvent):void {
  236. setBtnState(loginBtn, "up");
  237. stage.removeEventListener(MouseEvent.MOUSE_UP, handleLoginBtnStageMouseUp);
  238. }
  239. protected function handlePostBtnMouseDown(event:MouseEvent):void {
  240. if (isLoggedIn) {
  241. setBtnState(postBtn, "down");
  242. stage.addEventListener(MouseEvent.MOUSE_UP, handlePostBtnStageMouseUp, false, 0, true);
  243. }
  244. }
  245. protected function handlePostBtnStageMouseUp(event:MouseEvent):void {
  246. setBtnState(postBtn, "up");
  247. stage.removeEventListener(MouseEvent.MOUSE_UP, handlePostBtnStageMouseUp);
  248. }
  249. protected function handleGetBtnMouseDown(event:MouseEvent):void {
  250. if (isLoggedIn) {
  251. setBtnState(getBtn, "down");
  252. stage.addEventListener(MouseEvent.MOUSE_UP, handleGetBtnStageMouseUp, false, 0, true);
  253. }
  254. }
  255. protected function handleGetBtnStageMouseUp(event:MouseEvent):void {
  256. setBtnState(getBtn, "up");
  257. stage.removeEventListener(MouseEvent.MOUSE_UP, handleGetBtnStageMouseUp);
  258. }
  259. protected function handleClearBtnMouseDown(event:MouseEvent):void {
  260. setBtnState(clearBtn, "down");
  261. stage.addEventListener(MouseEvent.MOUSE_UP, handleClearBtnStageMouseUp, false, 0, true);
  262. }
  263. protected function handleClearBtnStageMouseUp(event:MouseEvent):void {
  264. setBtnState(clearBtn, "up");
  265. stage.removeEventListener(MouseEvent.MOUSE_UP, handleClearBtnStageMouseUp);
  266. }
  267. protected function handleDeactivate(event:Event):void {
  268. FacebookDesktop.logout(handleLogout, APP_ORIGIN);
  269. NativeApplication.nativeApplication.exit();
  270. }
  271. }
  272. }