PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/FlickrMosaic/src/Controls.as

http://in-spirit.googlecode.com/
ActionScript | 480 lines | 400 code | 41 blank | 39 comment | 29 complexity | 3e81bb90393719c9ccd7df5758789e9c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package
  2. {
  3. import com.bit101.components.HUISlider;
  4. import com.bit101.components.InputText;
  5. import com.bit101.components.Label;
  6. import com.bit101.components.Panel;
  7. import com.bit101.components.PushButton;
  8. import com.bit101.components.RadioButton;
  9. import com.bit101.components.RadioButtonGroup;
  10. import flash.display.Bitmap;
  11. import flash.display.BitmapData;
  12. import flash.display.Graphics;
  13. import flash.display.Sprite;
  14. import flash.events.Event;
  15. import flash.events.MouseEvent;
  16. import flash.events.StatusEvent;
  17. import flash.media.Video;
  18. import flash.media.Camera;
  19. import ru.inspirit.flickr.Flickr;
  20. import ru.inspirit.mosaic.TileLayer;
  21. /**
  22. * Control Panel.
  23. * I use Keith Peter's Miniml Components.
  24. *
  25. * @author Eugene Zatepyakin
  26. */
  27. public class Controls extends Sprite
  28. {
  29. private var _owner:Main;
  30. private var panel:Panel;
  31. private var openFileBtn:PushButton;
  32. private var captureImageBtn:PushButton;
  33. private var capturePanel:Panel;
  34. private var _video:Video;
  35. private var _cam:Camera;
  36. private var fader:Sprite;
  37. private var fPoolG:RadioButtonGroup;
  38. private var radio_pool_color:RadioButton;
  39. private var radio_pool_macro:RadioButton;
  40. private var radio_pool_urban:RadioButton;
  41. private var radio_pool_digits:RadioButton;
  42. private var radio_pool_interesting:RadioButton;
  43. private var tileNumberSlider:HUISlider;
  44. private var pixelSizeSlider:HUISlider;
  45. private var startBtn:PushButton;
  46. private var poolsBtn:PushButton;
  47. private var poolsPanel:Panel;
  48. private var fOptionsG:RadioButtonGroup;
  49. private var radio_flickr_options_pool:RadioButton;
  50. private var radio_flickr_options_tags:RadioButton;
  51. private var radio_flickr_options_user:RadioButton;
  52. private var _enabled:Boolean = true;
  53. private var tmp_data:Object = { };
  54. private var texture_bmp:BitmapData;
  55. [Embed(source = '../assets/line.png')] public var texture: Class;
  56. public function Controls(owner:Main)
  57. {
  58. _owner = owner;
  59. //
  60. tmp_data.flickrMethod = Main.flickrMethod;
  61. tmp_data.flickrPool = Main.flickrPool;
  62. tmp_data.flickrTags = Main.flickrTags;
  63. tmp_data.flickrUser = Main.flickrUser;
  64. tmp_data.flickrUserID = Main.flickrUserID;
  65. //
  66. texture_bmp = (new texture() as Bitmap).bitmapData;
  67. //
  68. if (stage) init();
  69. else addEventListener(Event.ADDED_TO_STAGE, init);
  70. }
  71. private function init(e:Event = null):void
  72. {
  73. removeEventListener(Event.ADDED_TO_STAGE, init);
  74. //
  75. panel = new Panel(this, 0, 0);
  76. var p:Panel = panel;
  77. p.height = 55;
  78. //
  79. var alls:Sprite = new Sprite();
  80. alls.name = "all_controls";
  81. var g:Graphics = alls.graphics;
  82. //
  83. openFileBtn = new PushButton(alls, 10, 5, "OPEN IMAGE", _owner.onOpenImagePress);
  84. captureImageBtn = new PushButton(alls, 10, 30, "CAPTURE IMAGE", onCaptureImagePress);
  85. openFileBtn.width = captureImageBtn.width = 90;
  86. drawSep(g, 110);
  87. var lb:Label;
  88. poolsBtn = new PushButton(alls, 120, 5, "", showPoolSettings);
  89. poolsBtn.label = "FLICKR SETTINGS";
  90. poolsBtn.setSize(90, 45);
  91. //
  92. drawSep(g, 220);
  93. lb = new Label(alls, 230, 3, "TILE IMAGES SETTINGS");
  94. tileNumberSlider = new HUISlider(alls, 230, 18, "IMAGES TO LOAD", onTilesNumberChanged);
  95. pixelSizeSlider = new HUISlider(alls, 230, 33, "IMAGE TILE SIZE ", onPixelSizeChanged);
  96. tileNumberSlider.width = pixelSizeSlider.width = 250;
  97. tileNumberSlider.backClick = pixelSizeSlider.backClick = true;
  98. tileNumberSlider.setSliderParams(50, 500, 100);
  99. pixelSizeSlider.setSliderParams(5, 20, 10);
  100. tileNumberSlider.labelPrecision = 0;
  101. pixelSizeSlider.labelPrecision = 0;
  102. drawSep(g, 455);
  103. lb = new Label(alls, 465, 3, "OVERLAY METHOD");
  104. var gr:RadioButtonGroup = new RadioButtonGroup();
  105. new RadioButton(alls, 465, 22, "CLEAR", false, onDrawMethodChanged, gr);
  106. new RadioButton(alls, 515, 22, "BLEND", true, onDrawMethodChanged, gr);
  107. new RadioButton(alls, 465, 38, "COLORIZE", false, onDrawMethodChanged, gr);
  108. drawSep(g, 565);
  109. startBtn = new PushButton(alls, 575, 5, "LOAD & RENDER", onStartPress);
  110. startBtn.setSize(90, 20);
  111. var render_btn:PushButton = new PushButton(alls, 575, 30, "RENDER", onRenderPress);
  112. render_btn.name = "render_btn";
  113. render_btn.setSize(90, 20);
  114. var save_btn:PushButton = new PushButton(alls, 675, 5, "SAVE", onSavePoster);
  115. save_btn.name = "save_btn";
  116. save_btn.setSize(50, 45);
  117. //
  118. var txtr:Sprite = new Sprite();
  119. txtr.name = "txtr";
  120. txtr.blendMode = "multiply";
  121. txtr.alpha = .2;
  122. p.content.addChild(txtr);
  123. p.content.addChild(alls);
  124. //
  125. initStep(0);
  126. //
  127. stage.addEventListener(Event.RESIZE, onResize);
  128. onResize();
  129. }
  130. private function onDrawMethodChanged(e:Event):void
  131. {
  132. var r:RadioButton = RadioButton(e.currentTarget);
  133. if (r.label == 'CLEAR') {
  134. TileLayer.DRAW_STYLE = 'clear';
  135. } else if (r.label == 'BLEND') {
  136. TileLayer.DRAW_STYLE = 'blend';
  137. } else {
  138. TileLayer.DRAW_STYLE = 'colorize';
  139. }
  140. }
  141. public function set enabled(val:Boolean):void
  142. {
  143. if (!val) {
  144. fader = new Sprite();
  145. fader.graphics.beginFill(0x000000, .5);
  146. fader.graphics.drawRect(0, 0, stage.stageWidth, panel.height);
  147. addChild(fader);
  148. panel.alpha = .5;
  149. } else {
  150. removeChild(fader);
  151. fader = null;
  152. panel.alpha = 1;
  153. }
  154. _enabled = val;
  155. }
  156. private function onSavePoster(e:Event):void
  157. {
  158. _owner.savePoster();
  159. }
  160. private function showPoolSettings(e:Event):void
  161. {
  162. fader = new Sprite();
  163. fader.graphics.beginFill(0x000000, .5);
  164. fader.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  165. addChild(fader);
  166. //
  167. var tx:Number = (stage.stageWidth - 400) / 2;
  168. var ty:Number = (stage.stageHeight - 200) / 2;
  169. poolsPanel = new Panel(this, tx, ty);
  170. var p:Panel = poolsPanel;
  171. p.setSize(400, 200);
  172. //
  173. var lb:Label;
  174. fOptionsG = new RadioButtonGroup();
  175. fPoolG = new RadioButtonGroup();
  176. radio_flickr_options_pool = new RadioButton(p.content, 10, 10, "TAKE FROM FLICKR POOL", Main.flickrMethod=="pool", onFlickrOptionsChanged, fOptionsG);
  177. radio_pool_color = new RadioButton(p.content, 25, 33, "COLOR FIELDS", Main.flickrPool==Flickr.pool_colorfields, onFlickrPoolChanged, fPoolG);
  178. radio_pool_macro = new RadioButton(p.content, 120, 33, "MACRO", Main.flickrPool==Flickr.pool_macro, onFlickrPoolChanged, fPoolG);
  179. radio_pool_urban = new RadioButton(p.content, 185, 33, "URBAN", Main.flickrPool==Flickr.pool_urban, onFlickrPoolChanged, fPoolG);
  180. radio_pool_digits = new RadioButton(p.content, 245, 33, "DIGITS", Main.flickrPool == Flickr.pool_digit, onFlickrPoolChanged, fPoolG);
  181. radio_pool_interesting = new RadioButton(p.content, 300, 33, "INTERESTING", Main.flickrPool=="interesting", onFlickrPoolChanged, fPoolG);
  182. //
  183. radio_flickr_options_tags = new RadioButton(p.content, 10, 60, "SEARCH FLICKR TAGS", Main.flickrMethod=="tags", onFlickrOptionsChanged, fOptionsG);
  184. var flickr_tags:InputText = new InputText(p.content, 25, 80, Main.flickrTags.split(",").join(", "), onFlickrTagsChanged);
  185. flickr_tags.name = "flickr_tags";
  186. flickr_tags.width = 350;
  187. //
  188. radio_flickr_options_user = new RadioButton(p.content, 10, 115, "FLICKR USER", Main.flickrMethod=="user", onFlickrOptionsChanged, fOptionsG);
  189. var flickr_user:InputText = new InputText(p.content, 25, 135, Main.flickrUser);
  190. flickr_user.name = "flickr_user";
  191. flickr_user.restrict = "^ ";
  192. flickr_user.width = 300;
  193. lb = new Label(p.content, 25, 150, "");
  194. lb.name = "user_info";
  195. var ucheck:PushButton = new PushButton(p.content, 330, 135, "SET", onFlickrUserCheck);
  196. ucheck.name = "check_btn";
  197. ucheck.setSize(45, 16);
  198. //
  199. var ok_btn:PushButton = new PushButton(p.content, 260, 170, "OK", onPoolSettingsOK);
  200. var close_btn:PushButton = new PushButton(p.content, 330, 170, "CLOSE", closePoolSettings);
  201. ok_btn.width = close_btn.width = 60;
  202. //
  203. onFlickrOptionsChanged();
  204. }
  205. private function closePoolSettings(e:Event = null):void
  206. {
  207. removeChild(poolsPanel);
  208. removeChild(fader);
  209. //
  210. poolsPanel = null;
  211. fader = null;
  212. }
  213. private function onPoolSettingsOK(e:Event):void
  214. {
  215. Main.flickrMethod = tmp_data.flickrMethod;
  216. Main.flickrPool = tmp_data.flickrPool;
  217. Main.flickrTags = tmp_data.flickrTags.split(" ").join("");
  218. Main.flickrUser = tmp_data.flickrUser;
  219. Main.flickrUserID = tmp_data.flickrUserID;
  220. closePoolSettings();
  221. }
  222. private function onFlickrUserCheck(e:Event):void
  223. {
  224. var fu:InputText = poolsPanel.content.getChildByName("flickr_user") as InputText;
  225. var lab:Label = poolsPanel.content.getChildByName("user_info") as Label;
  226. lab.text = "Checking user name...";
  227. _owner.checkFlickrUser(fu.text);
  228. }
  229. public function setUserInfo(inf:*):void
  230. {
  231. var lab:Label = poolsPanel.content.getChildByName("user_info") as Label;
  232. var fu:InputText = poolsPanel.content.getChildByName("flickr_user") as InputText;
  233. lab.text = "Found " + inf.photoCount + " photos by " + inf.username;
  234. tmp_data.flickrUser = (inf.photoCount > 0) ? fu.text : Main.flickrUser;
  235. tmp_data.flickrUserID = (inf.photoCount > 0) ? inf.nsid : Main.flickrUserID;
  236. }
  237. private function onFlickrTagsChanged(e:Event):void
  238. {
  239. var ft:InputText = poolsPanel.content.getChildByName("flickr_tags") as InputText;
  240. tmp_data.flickrTags = ft.text;
  241. }
  242. private function onFlickrOptionsChanged(e:Event = null):void
  243. {
  244. var method:String;
  245. if (radio_flickr_options_pool.selected) method = "pool";
  246. if (radio_flickr_options_tags.selected) method = "tags";
  247. if (radio_flickr_options_user.selected) method = "user";
  248. //
  249. var ft:InputText = poolsPanel.content.getChildByName("flickr_tags") as InputText;
  250. var fu:InputText = poolsPanel.content.getChildByName("flickr_user") as InputText;
  251. var uc:PushButton = poolsPanel.content.getChildByName("check_btn") as PushButton;
  252. ft.tf.type = fu.tf.type = "dynamic";
  253. ft.tf.selectable = fu.tf.selectable = false;
  254. uc.buttonMode = radio_pool_color.buttonMode = radio_pool_macro.buttonMode = radio_pool_urban.buttonMode = radio_pool_digits.buttonMode = radio_pool_interesting.buttonMode = false;
  255. uc.mouseEnabled = radio_pool_color.mouseEnabled = radio_pool_macro.mouseEnabled = radio_pool_urban.mouseEnabled = radio_pool_digits.mouseEnabled = radio_pool_interesting.mouseEnabled = false;
  256. uc.alpha = ft.alpha = fu.alpha = radio_pool_color.alpha = radio_pool_macro.alpha = radio_pool_urban.alpha = radio_pool_digits.alpha = radio_pool_interesting.alpha = .5;
  257. //
  258. switch(method) {
  259. case "pool":
  260. radio_pool_color.buttonMode = radio_pool_macro.buttonMode = radio_pool_urban.buttonMode = radio_pool_digits.buttonMode = radio_pool_interesting.buttonMode = true;
  261. radio_pool_color.mouseEnabled = radio_pool_macro.mouseEnabled = radio_pool_urban.mouseEnabled = radio_pool_digits.mouseEnabled = radio_pool_interesting.mouseEnabled = true;
  262. radio_pool_color.alpha = radio_pool_macro.alpha = radio_pool_urban.alpha = radio_pool_digits.alpha = radio_pool_interesting.alpha = 1;
  263. break;
  264. case "tags":
  265. ft.tf.type = "input";
  266. ft.tf.selectable = true;
  267. ft.alpha = 1;
  268. break;
  269. case "user":
  270. fu.tf.type = "input";
  271. fu.tf.selectable = true;
  272. uc.buttonMode = uc.mouseEnabled = true;
  273. fu.alpha = uc.alpha = 1;
  274. break;
  275. }
  276. //
  277. tmp_data.flickrMethod = method;
  278. }
  279. public function initStep(step:int):void
  280. {
  281. var rb:PushButton = (panel.content.getChildByName("all_controls") as Sprite).getChildByName("render_btn") as PushButton;
  282. var sb:PushButton = (panel.content.getChildByName("all_controls") as Sprite).getChildByName("save_btn") as PushButton;
  283. startBtn.buttonMode = rb.buttonMode = sb.buttonMode = false;
  284. startBtn.mouseEnabled = rb.mouseEnabled = sb.mouseEnabled = false;
  285. startBtn.alpha = rb.alpha = sb.alpha = .5;
  286. //
  287. switch(step) {
  288. case 1:
  289. startBtn.alpha = 1;
  290. startBtn.buttonMode = true;
  291. startBtn.mouseEnabled = true;
  292. break;
  293. case 2:
  294. startBtn.alpha = rb.alpha = 1;
  295. startBtn.buttonMode = rb.buttonMode = true;
  296. startBtn.mouseEnabled = rb.mouseEnabled = true;
  297. break;
  298. case 3:
  299. startBtn.alpha = rb.alpha = 1;
  300. startBtn.buttonMode = rb.buttonMode = true;
  301. startBtn.mouseEnabled = rb.mouseEnabled = true;
  302. sb.alpha = 1;
  303. sb.buttonMode = true;
  304. sb.mouseEnabled = true;
  305. break;
  306. }
  307. }
  308. private function onStartPress(e:Event):void
  309. {
  310. _owner.startRender(true);
  311. }
  312. private function onRenderPress(e:Event):void
  313. {
  314. _owner.startRender(false);
  315. }
  316. private function onPixelSizeChanged(e:Event):void
  317. {
  318. Main.pixelSize = pixelSizeSlider.value;
  319. }
  320. private function onTilesNumberChanged(e:Event):void
  321. {
  322. Main.totalTiles = tileNumberSlider.value;
  323. }
  324. private function onFlickrPoolChanged(e:Event):void
  325. {
  326. var pool:String;
  327. if (radio_pool_color.selected) pool = Flickr.pool_colorfields;
  328. if (radio_pool_macro.selected) pool = Flickr.pool_macro;
  329. if (radio_pool_urban.selected) pool = Flickr.pool_urban;
  330. if (radio_pool_digits.selected) pool = Flickr.pool_digit;
  331. if (radio_pool_interesting.selected) pool = 'interesting';
  332. //
  333. tmp_data.flickrPool = pool;
  334. }
  335. private function onCaptureImagePress(e:Event):void
  336. {
  337. try {
  338. _cam = Camera.getCamera();
  339. _cam.setMode(640, 480, 30);
  340. _cam.setQuality(0, 100);
  341. _video = new Video(640, 480);
  342. _video.attachCamera(_cam);
  343. //
  344. if(_cam.muted){
  345. _cam.addEventListener(StatusEvent.STATUS, CameraStatus);
  346. } else {
  347. initCameraModule();
  348. }
  349. } catch (e:*) {
  350. // no camera
  351. trace("no camera");
  352. }
  353. }
  354. private function CameraStatus(e:StatusEvent):void
  355. {
  356. if (e.code == "Camera.Unmuted")
  357. {
  358. _cam.removeEventListener(StatusEvent.STATUS, CameraStatus);
  359. initCameraModule();
  360. }
  361. }
  362. private function initCameraModule():void
  363. {
  364. fader = new Sprite();
  365. fader.graphics.beginFill(0x000000, .5);
  366. fader.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
  367. addChild(fader);
  368. //
  369. capturePanel = new Panel(this, int((stage.stageWidth - 640)/2), int((stage.stageHeight-530)/2));
  370. capturePanel.setSize(640, 530);
  371. //
  372. var capBtn:PushButton = new PushButton(capturePanel.content, 205, 495, "CAPTURE", doCapture);
  373. var closeBtn:PushButton = new PushButton(capturePanel.content, 315, 495, "CLOSE", doCloseCapture);
  374. //
  375. capturePanel.content.addChild(_video);
  376. }
  377. private function doCloseCapture(e:Event = null):void
  378. {
  379. capturePanel.content.removeChild(_video);
  380. removeChild(capturePanel);
  381. removeChild(fader);
  382. //
  383. _video.attachCamera(null);
  384. //
  385. capturePanel = null;
  386. _cam = null;
  387. fader = null;
  388. _video = null;
  389. }
  390. private function doCapture(e:Event):void
  391. {
  392. var bmp:BitmapData = new BitmapData(_video.width, _video.height, false, 0xFFFFFF);
  393. bmp.draw(_video);
  394. //
  395. doCloseCapture();
  396. //
  397. _owner.onImageCaptured(bmp);
  398. }
  399. private function onResize(e:Event = null):void
  400. {
  401. var w:Number = stage.stageWidth;
  402. var h:Number = stage.stageHeight;
  403. var alls:Sprite = panel.content.getChildByName("all_controls") as Sprite;
  404. panel.width = w;
  405. alls.x = int((w - 734 ) * .5);
  406. //
  407. var g:Graphics = (panel.content.getChildByName("txtr") as Sprite).graphics;
  408. g.clear();
  409. g.beginBitmapFill(texture_bmp);
  410. g.drawRect(0, 0, w, 55);
  411. g.endFill();
  412. //
  413. if (capturePanel != null) {
  414. capturePanel.x = int((w - capturePanel.width) / 2);
  415. capturePanel.y = int((h - capturePanel.height) / 2);
  416. fader.width = w;
  417. fader.height = h;
  418. } else if (poolsPanel != null) {
  419. poolsPanel.x = int((w - poolsPanel.width) / 2);
  420. poolsPanel.y = int((h - poolsPanel.height) / 2);
  421. fader.width = w;
  422. fader.height = h;
  423. }
  424. if (!_enabled) {
  425. fader.width = w;
  426. fader.height = panel.height;
  427. }
  428. }
  429. private function drawSep(g:Graphics, x:int):void
  430. {
  431. g.lineStyle(1, 0x999999, 1, true, "normal", "SQUARE");
  432. g.moveTo(x, 5);
  433. g.lineTo(x, 49);
  434. g.lineStyle(1, 0xFFFFFF, 1, true, "normal", "SQUARE");
  435. g.moveTo(x+1, 5);
  436. g.lineTo(x+1, 49);
  437. }
  438. }
  439. }