PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Onyx-VJ 4.5.0/Onyx-AIR-Core/src/ui/window/Browser.as

http://onyx-vj.googlecode.com/
ActionScript | 497 lines | 246 code | 105 blank | 146 comment | 23 complexity | 61704ab4f451623acaecda4865585b5f MD5 | raw file
  1. /**
  2. * Copyright (c) 2003-2010 "Onyx-VJ Team" which is comprised of:
  3. *
  4. * Daniel Hai
  5. * Stefano Cottafavi
  6. * Bruce Lane
  7. *
  8. * All rights reserved.
  9. *
  10. * Licensed under the CREATIVE COMMONS Attribution-Noncommercial-Share Alike 3.0
  11. * You may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at: http://creativecommons.org/licenses/by-nc-sa/3.0/us/
  13. *
  14. * Please visit http://www.onyx-vj.com for more information
  15. *
  16. */
  17. package ui.window {
  18. import flash.display.*;
  19. import flash.events.*;
  20. import flash.filesystem.*;
  21. import flash.geom.*;
  22. import flash.net.URLRequest;
  23. import flash.net.navigateToURL;
  24. import flash.utils.*;
  25. import onyx.asset.*;
  26. import onyx.asset.air.*;
  27. import onyx.asset.vp.VPAsset;
  28. import onyx.core.*;
  29. import onyx.display.*;
  30. import onyx.plugin.*;
  31. import ui.assets.*;
  32. import ui.controls.*;
  33. import ui.controls.browser.*;
  34. import ui.core.*;
  35. import ui.events.*;
  36. import ui.file.*;
  37. import ui.layer.*;
  38. import ui.styles.*;
  39. import ui.text.*;
  40. /**
  41. * File Explorer
  42. */
  43. public final class Browser extends Window {
  44. /**
  45. *
  46. */
  47. public static var useTransition:Transition;
  48. /**
  49. * @private
  50. * An array of targets you can drag filters to
  51. */
  52. private static const targets:Dictionary = new Dictionary(true);
  53. /**
  54. * Registers a UIObject to be a target of a Browser drag
  55. */
  56. public static function registerTarget(obj:UIObject, enable:Boolean):void {
  57. (enable) ? targets[obj] = obj : delete targets[obj];
  58. }
  59. /** @private **/
  60. private static const FILES_PER_ROW:int = 6;
  61. /** @private **/
  62. private static const FILE_WIDTH:int = THUMB_WIDTH + 1;
  63. /** @private **/
  64. private static const FILE_HEIGHT:int = THUMB_HEIGHT + 1;
  65. /** @private **/
  66. private static const FOLDER_HEIGHT:int = 14;
  67. /**
  68. * @private
  69. * Holds the file objects
  70. */
  71. private const files:ScrollPane = new ScrollPane(400, 196);
  72. /**
  73. * @private
  74. * Holds the folder objects
  75. */
  76. private const folders:ScrollPane = new ScrollPane(81, 150);
  77. /**
  78. * @private
  79. * The browser files button
  80. */
  81. private var buttonFiles:TextButtonIcon;
  82. /**
  83. * @private
  84. * The Cameras button
  85. */
  86. private var buttonCameras:TextButtonIcon;
  87. /**
  88. * @private
  89. * The VideoPong button
  90. */
  91. private var buttonVideoPong:TextButtonIcon;
  92. /**
  93. * @private
  94. * The Microphone button
  95. */
  96. private var buttonMicrophones:TextButtonIcon;
  97. /**
  98. * @private
  99. */
  100. private var db:AIRThumbnailDB;
  101. /**
  102. * @private
  103. */
  104. private var query:AssetQuery;
  105. /**
  106. * @private
  107. */
  108. private var list:Array;
  109. /**
  110. * @constructor
  111. */
  112. public function Browser(reg:WindowRegistration):void {
  113. super(reg, true, 499, 217);
  114. init();
  115. }
  116. /**
  117. *
  118. */
  119. public function refresh():void {
  120. // query default folder
  121. AssetFile.queryDirectory(query.path, updateList);
  122. }
  123. /**
  124. * @private
  125. */
  126. private function init():void {
  127. // draw the pane background
  128. const bmp:BitmapData = (getChildAt(0) as Bitmap).bitmapData;
  129. bmp.fillRect(new Rectangle(417,15,81, super.height - 17), 0xFF131e28);
  130. // arrange everything else
  131. const options:UIOptions = new UIOptions();
  132. options.width = 82;
  133. buttonFiles = new TextButtonIcon(options, 'FILES', new AssetFolder()),
  134. buttonCameras = new TextButtonIcon(options, 'CAMERAS', new AssetIconCamera()),
  135. buttonMicrophones = new TextButtonIcon(options, 'MICROPHONES', new AssetIconCamera()),
  136. buttonVideoPong = new TextButtonIcon(options, 'VIDEOPONG', new AssetVideoPong()),
  137. files.x = 4,
  138. files.y = 17,
  139. folders.x = 417,
  140. folders.y = 15,
  141. buttonFiles.x = 417,
  142. buttonFiles.y = 192,
  143. buttonCameras.x = 417,
  144. buttonCameras.y = 204,
  145. buttonMicrophones.x = 417,
  146. buttonMicrophones.y = 168,
  147. buttonVideoPong.x = 417,
  148. buttonVideoPong.y = 180;
  149. // add handlers for buttons
  150. buttonFiles.addEventListener(MouseEvent.MOUSE_DOWN, fileDown);
  151. buttonCameras.addEventListener(MouseEvent.MOUSE_DOWN, fileDown);
  152. buttonMicrophones.addEventListener(MouseEvent.MOUSE_DOWN, fileDown);
  153. buttonVideoPong.addEventListener(MouseEvent.MOUSE_DOWN, fileDown);
  154. addChild(folders);
  155. addChild(files);
  156. addChild(buttonVideoPong);// TODO: add this button on vp successful login
  157. addChild(buttonFiles);
  158. addChild(buttonCameras);
  159. addChild(buttonMicrophones);
  160. // query default folder
  161. AssetFile.queryDirectory(ONYX_LIBRARY_PATH, updateList);
  162. // make draggable
  163. DragManager.setDraggable(this);
  164. }
  165. /**
  166. * @private
  167. */
  168. private function updateList(query:AssetQuery, list:Array):void {
  169. // valid query?
  170. if (query) {
  171. // store the query
  172. this.query = query;
  173. this.list = list;
  174. if ( query.path.substr( 0, 19 ) == "onyx-query://vdpong" )
  175. {
  176. createUserObjects( false );
  177. }
  178. else
  179. {
  180. const dbFile:File = new File(AssetFile.resolvePath(query.path + '/.onyx-cache'));
  181. db = new AIRThumbnailDB();
  182. if (dbFile.exists) {
  183. const stream:FileStream = new FileStream();
  184. stream.addEventListener(Event.COMPLETE, dbHandler);
  185. stream.openAsync(dbFile, FileMode.READ);
  186. } else {
  187. createUserObjects();
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * @private
  194. */
  195. private function dbHandler(event:Event):void {
  196. var stream:FileStream = event.currentTarget as FileStream;
  197. stream.removeEventListener(Event.COMPLETE, dbHandler);
  198. var bytes:ByteArray = new ByteArray();
  199. stream.readBytes(bytes);
  200. stream.close();
  201. db.load(bytes);
  202. // create objects
  203. createUserObjects();
  204. }
  205. /**
  206. * @private
  207. */
  208. private function createUserObjects( isAIREnabled:Boolean = true ):void {
  209. var control:DisplayObject, index:int;
  210. // store an array of items we need to thumbnail
  211. const needToThumbnail:Array = [];
  212. const checkForDelete:Object = {};
  213. // kill all previous objects here
  214. _clearChildren();
  215. // reset location
  216. files.reset();
  217. folders.reset();
  218. const path:String = query.path;
  219. for each (var asset:AssetFile in list) {
  220. if (asset.isDirectory) {
  221. // add and position
  222. // check the asset path length for showing 'up one level'
  223. control = folders.addChild( new FolderControl( asset, asset.path.length < path.length ) );
  224. index = folders.getChildIndex(control);
  225. control.x = 0;
  226. control.y = FOLDER_HEIGHT * index;
  227. control.addEventListener(MouseEvent.MOUSE_DOWN, folderDown);
  228. // it's a file, see if we need to thumbnail it ... also, add it to the screen
  229. } else {
  230. control = files.addChild(new FileControl(asset, asset.thumbnail));
  231. index = files.getChildIndex(control);
  232. // position it
  233. control.x = (index % FILES_PER_ROW) * FILE_WIDTH;
  234. control.y = ((index / FILES_PER_ROW) >> 0) * FILE_HEIGHT;
  235. index++;
  236. //compile error: control.y = ((index++ / FILES_PER_ROW) >> 0) * FILE_HEIGHT;
  237. // start listening to start dragging
  238. control.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
  239. // if there is a valid bitmap, that means there is a thumbnail
  240. // if no bitmap, add it to our job queue
  241. if (!asset.thumbnail.bitmapData) {
  242. asset.thumbnail.bitmapData = db.getThumbnail(asset.name);
  243. // doesn't exist, thumbnail it
  244. if (!asset.thumbnail.bitmapData) {
  245. needToThumbnail.push(asset);
  246. // exists, don't send this for thumbnail deletion
  247. } else {
  248. checkForDelete[asset.name] = asset;
  249. }
  250. }
  251. }
  252. }
  253. if ( isAIREnabled )
  254. {
  255. StateManager.loadState(
  256. new AIRThumbnailState(AssetFile.resolvePath(query.path), db, needToThumbnail, checkForDelete)
  257. );
  258. }
  259. }
  260. /**
  261. * @private
  262. * Handlers for Camera/File Button
  263. */
  264. private function fileDown(event:MouseEvent):void {
  265. switch (event.currentTarget) {
  266. case buttonFiles:
  267. AssetFile.queryDirectory(ONYX_LIBRARY_PATH, updateList);
  268. break;
  269. case buttonCameras:
  270. AssetFile.queryDirectory('onyx-query://camera', updateList);
  271. break;
  272. case buttonMicrophones:
  273. AssetFile.queryDirectory('onyx-query://linein', updateList);
  274. break;
  275. case buttonVideoPong:
  276. AssetFile.queryDirectory('onyx-query://vdpong', updateList);
  277. break;
  278. }
  279. }
  280. /**
  281. * @private
  282. * Clears children
  283. */
  284. private function _clearChildren():void {
  285. // clear our controls, etc
  286. while (files.numChildren) {
  287. var control:FileControl = files.removeChildAt(0) as FileControl;
  288. // stop listening to start dragging
  289. control.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
  290. }
  291. if (folders.numChildren > 0) {
  292. while (folders.numChildren > 0) {
  293. var folder:FolderControl = folders.removeChildAt(0) as FolderControl;
  294. folder.removeEventListener(MouseEvent.MOUSE_DOWN, folderDown);
  295. }
  296. }
  297. }
  298. /**
  299. *
  300. */
  301. public function updateFolders():void {
  302. // query default folder
  303. AssetFile.queryDirectory(query.path, updateList);
  304. }
  305. /**
  306. * @private
  307. * when we start dragging
  308. */
  309. private function mouseDown(event:MouseEvent):void {
  310. var control:FileControl = event.currentTarget as FileControl;
  311. // ctrl for asset details in a browser
  312. if ( event.controlKey )
  313. {
  314. if ( control.asset is VPAsset )
  315. {
  316. var rawUrl:String = control.asset.path;
  317. var lastSlashPos:int = rawUrl.lastIndexOf('/');
  318. if ( lastSlashPos > 0 )
  319. {
  320. // & found
  321. rawUrl = rawUrl.substr( 0, lastSlashPos );
  322. lastSlashPos = rawUrl.lastIndexOf('/');
  323. if ( lastSlashPos > 0 )
  324. {
  325. var assetid:String = rawUrl.substr( lastSlashPos + 1 );
  326. navigateToURL( new URLRequest ('https://www.videopong.net/clip/detail/' + assetid ) );
  327. }
  328. }
  329. }
  330. }
  331. else
  332. {
  333. DragManager.startDrag(control, targets, dragOver, dragOut, dragDrop);
  334. }
  335. }
  336. /**
  337. * @private
  338. * when a folder is clicked
  339. */
  340. private function folderDown(event:MouseEvent):void {
  341. var control:FolderControl = event.currentTarget as FolderControl;
  342. AssetFile.queryDirectory(control.asset.path, updateList);
  343. }
  344. /**
  345. * @private
  346. * drag functions
  347. */
  348. private function dragOver(event:DragEvent):void {
  349. var obj:UIObject = event.currentTarget as UIObject;
  350. obj.transform.colorTransform = DRAG_HIGHLIGHT;
  351. }
  352. /**
  353. * @private
  354. * drag functions
  355. */
  356. private function dragOut(event:DragEvent):void {
  357. var obj:UIObject = event.currentTarget as UIObject;
  358. obj.transform.colorTransform = (obj === UIObject.selection) ? LAYER_HIGHLIGHT : DEFAULT;
  359. }
  360. /**
  361. * @private
  362. * Drag functions
  363. */
  364. private function dragDrop(event:DragEvent):void {
  365. var uilayer:ILayerDrop = event.currentTarget as ILayerDrop;
  366. var origin:FileControl = event.origin as FileControl;
  367. uilayer.transform.colorTransform = DEFAULT;
  368. if (event.ctrlKey && uilayer.layer.path) {
  369. var settings:LayerSettings = new LayerSettings();
  370. settings.load(uilayer.layer);
  371. }
  372. _loadFile(uilayer, origin.asset, settings);
  373. }
  374. /**
  375. * @private
  376. * Load
  377. */
  378. private function _loadFile(layer:ILayerDrop, asset:AssetFile, settings:LayerSettings):void {
  379. switch (asset.extension) {
  380. case 'xml':
  381. case 'mix':
  382. case 'onx':
  383. (Display as OutputDisplay).load(asset.path, layer.layer, useTransition);
  384. return;
  385. default:
  386. layer.layer.load(asset.path, settings, useTransition);
  387. break;
  388. }
  389. UIObject.select(layer as UIObject);
  390. }
  391. }
  392. }