PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/hislope/gui/FilterPanel.as

http://github.com/og2t/HiSlope
ActionScript | 626 lines | 390 code | 165 blank | 71 comment | 15 complexity | 269a9accfded46409f17df8b9c0feae5 MD5 | raw file
  1. /*---------------------------------------------------------------------------------------------
  2. [AS3] FilterPanel
  3. =======================================================================================
  4. HiSlope toolkit copyright (c) 2008-2011 Tomek 'Og2t' Augustyn
  5. http://play.blog2t.net/HiSlope
  6. You are free to use this source code in any non-commercial project.
  7. You are free to modify this source code in anyway you see fit.
  8. You are free to distribute this source code.
  9. You may NOT charge anything for this source code.
  10. This notice and the copyright information must be left intact in any distribution of this source code.
  11. You are encouraged to release any improvements back to the ActionScript community.
  12. VERSION HISTORY:
  13. v0.1 Born on 09/07/2009
  14. USAGE:
  15. TODOs:
  16. DEV IDEAS:
  17. KNOWN ISSUES:
  18. ---------------------------------------------------------------------------------------------*/
  19. package hislope.gui
  20. {
  21. // IMPORTS ////////////////////////////////////////////////////////////////////////////////
  22. import hislope.filters.FilterBase;
  23. import hislope.gui.HistogramView;
  24. import hislope.events.HiSlopeEvent;
  25. import flash.display.Sprite;
  26. import flash.display.Shape;
  27. import flash.display.Bitmap;
  28. import flash.display.BlendMode;
  29. import flash.events.Event;
  30. import flash.events.MouseEvent;
  31. import hislope.core.FilterChain;
  32. import hislope.core.FilterParser;
  33. import hislope.core.Utils;
  34. import flash.geom.Point;
  35. //import net.blog2t.minimalcomps.*; /*Use Minimal Components+ in the first place*/
  36. import com.bit101.components.*; /*Then default to original Minimal Components*/
  37. // CLASS //////////////////////////////////////////////////////////////////////////////////
  38. public class FilterPanel extends Sprite
  39. {
  40. // CONSTANTS //////////////////////////////////////////////////////////////////////////
  41. public static const CHANGE_SIZE:String = "changeSize";
  42. // MEMBERS ////////////////////////////////////////////////////////////////////////////
  43. private var filter:FilterBase;
  44. private var window:Window;
  45. private var isEnabledCB:CheckBox;
  46. private var showResultCB:CheckBox;
  47. private var showHistogramCB:CheckBox;
  48. private var showDebugVarsCB:CheckBox;
  49. private var previewBmp:Bitmap;
  50. private var previewHolder:Sprite = new Sprite();
  51. private var previewLabel:Label = new Label();
  52. private var histogramView:HistogramView;
  53. private var ui:*;
  54. private var resetButton:PushButton;
  55. private var rndButton:PushButton;
  56. private var copyParamsButton:PushButton;
  57. /*private var undoParamsButton:PushButton;*/
  58. private var line:Shape = new Shape();
  59. private var totalHeight:int;
  60. private var vbox:VBox;
  61. private var debugVarsBox:Sprite;
  62. private var hasDebugVars:Boolean;
  63. private var offsetY:int;
  64. private var decimalPoints:int;
  65. private var tick:Number;
  66. // CONSTRUCTOR ////////////////////////////////////////////////////////////////////////
  67. public function FilterPanel(filter:FilterBase, previewScale:Number = 1.0)
  68. {
  69. this.filter = filter;
  70. filter.panel = this;
  71. filter.previewScale = previewScale;
  72. previewBmp = new Bitmap(filter.previewBmpData);
  73. previewHolder.addChild(previewBmp);
  74. previewLabel = new Label(previewHolder, 10, 0, "100%");
  75. previewLabel.blendMode = BlendMode.DIFFERENCE;
  76. histogramView = new HistogramView(filter);
  77. histogramView.addEventListener(HistogramView.CHANGE_CHANNELS, histogramChannelsChange, false, 0, true);
  78. vbox = new VBox(this, 0, 0);
  79. vbox.spacing = 1;
  80. window = new Window(vbox);
  81. window.hasMinimizeButton = true;
  82. window.title = filter.name.toUpperCase();
  83. window.draggable = false;
  84. window.shadow = false;
  85. window.addEventListener(Event.RESIZE, updateParamsVisible, false, 0, true);
  86. isEnabledCB = new CheckBox(window, 320 - 105, 5, "On", updateFilterEnabled);
  87. showResultCB = new CheckBox(window, 320 - 70, 5, "Prv", updatePreviewVisible);
  88. showHistogramCB = new CheckBox(window, 320 - 35, 5, "Hst", updateHistogramVisible);
  89. offsetY = 0;
  90. window.addEventListener(MouseEvent.MOUSE_DOWN, panelClicked, false, 0, true);
  91. //FIXME try to do it before onResetParams is called, as it does virtually the same
  92. FilterParser.parseParams(filter, this);
  93. resetButton = new PushButton(window.content, 10, offsetY + 5, "RESET", onResetParams);
  94. resetButton.setSize(50, 15);
  95. rndButton = new PushButton(window.content, 10 + 60, offsetY + 5, "RANDOMISE", onRandomise);
  96. rndButton.setSize(70, 15);
  97. copyParamsButton = new PushButton(window.content, 140 + 10, offsetY + 5, "COPY PARAMS", onCopyParams);
  98. copyParamsButton.setSize(80, 15);
  99. /*undoParamsButton = new PushButton(window.content, 230 + 10, offsetY + 5, "UNDO", onUndoParams);
  100. undoParamsButton.setSize(70, 15);*/
  101. window.setSize(320, offsetY + 25 + 20);
  102. if (filter.debugVars && filter.debugVars.length > 0)
  103. {
  104. hasDebugVars = true;
  105. showDebugVarsCB = new CheckBox(window, 320 - 150, 5, "Vars", updateDebugVarsVisible);
  106. showDebugVarsCB.value = true;
  107. debugVarsBox = new Sprite();
  108. offsetY = 0;
  109. for each (var debugVar:String in filter.debugVars)
  110. {
  111. ui = new Label(debugVarsBox, 10, offsetY, Utils.propToLabel(debugVar) + ": --");
  112. ui.name = debugVar;
  113. offsetY += ui.height - 3;
  114. }
  115. vbox.addChild(debugVarsBox);
  116. }
  117. vbox.addChild(previewHolder);
  118. vbox.addChild(histogramView);
  119. drawPanel(0xFFFFFF, 0x000000);
  120. window.addChild(line);
  121. updateParamsVisible();
  122. updatePreviewVisible();
  123. //updateFilterEnabled();
  124. updateTotalHeight();
  125. }
  126. // PUBLIC METHODS /////////////////////////////////////////////////////////////////////
  127. public function destroy():void
  128. {
  129. filter.removeEventListener(HiSlopeEvent.FILTER_PROCESSED, render);
  130. }
  131. // PRIVATE METHODS ////////////////////////////////////////////////////////////////////
  132. public function updateUI(name:String, value:*):void
  133. {
  134. var ui:* = window.content.getChildByName(name);
  135. ui.value = value;
  136. }
  137. public function updateParams(event:Event):void
  138. {
  139. var targetUI:Object = event.currentTarget;
  140. var paramName:String = targetUI.name;
  141. filter.setParam(paramName, targetUI.value);
  142. }
  143. /*private function buttonCallback(event:Event):void
  144. {
  145. var targetUI:Object = event.currentTarget;
  146. var functionName:String = targetUI.name;
  147. filter[functionName]();
  148. }*/
  149. private function updateTotalHeight():void
  150. {
  151. vbox.draw();
  152. dispatchEvent(new Event(FilterPanel.CHANGE_SIZE));
  153. }
  154. // EVENT HANDLERS /////////////////////////////////////////////////////////////////////
  155. private function panelClicked(event:MouseEvent):void
  156. {
  157. FilterChain.currentPanel = this;
  158. }
  159. private function previewMouseMove(event:MouseEvent):void
  160. {
  161. filter.mouseMovePoint(new Point(previewHolder.mouseX / previewBmp.width, previewHolder.mouseY / previewBmp.height));
  162. }
  163. private function previewMouseDown(event:MouseEvent):void
  164. {
  165. filter.mouseDownPoint(new Point(previewHolder.mouseX / previewBmp.width, previewHolder.mouseY / previewBmp.height));
  166. }
  167. private function previewMouseUp(event:MouseEvent):void
  168. {
  169. filter.mouseUpPoint(new Point(previewHolder.mouseX / previewBmp.width, previewHolder.mouseY / previewBmp.height));
  170. }
  171. public function selectPanel():void
  172. {
  173. drawPanel(0xFF0000, 0x400000);
  174. }
  175. public function deselectPanel():void
  176. {
  177. FilterChain.currentPanel.drawPanel(0xFFFFFF, 0x000000);
  178. }
  179. private function onRandomise(event:Event):void
  180. {
  181. filter.randomiseParams();
  182. }
  183. private function onResetParams(event:Event):void
  184. {
  185. filter.resetParams();
  186. }
  187. private function onCopyParams(event:Event):void
  188. {
  189. FilterParser.copyParams(filter);
  190. }
  191. /*private function onUndoParams(event:Event):void
  192. {
  193. FilterParser.undoParams(filter);
  194. }*/
  195. private function render(event:Event):void
  196. {
  197. /*trace(this.filter, "render");*/
  198. updateTime();
  199. if (hasDebugVars) traceDebugVars();
  200. }
  201. private function traceDebugVars():void
  202. {
  203. for (var i:int = 0; i < debugVarsBox.numChildren; i++)
  204. {
  205. var label:Label = Label(debugVarsBox.getChildAt(i));
  206. label.text = Utils.propToLabel(label.name) + ": " + filter.getParamValue(label.name);
  207. }
  208. }
  209. private function updateTime():void
  210. {
  211. window.title = filter.name.toUpperCase() + ": " + filter.time + " ms\t(" + filter.minTime + "-" + filter.maxTime + ")";
  212. }
  213. private function updateFilterEnabled(event:Event = null):void
  214. {
  215. filterEnabled = isEnabledCB.value;
  216. isEnabledCB.label = isEnabledCB.value ? "On":"Off";
  217. }
  218. private function updateParamsVisible(event:Event = null):void
  219. {
  220. updateTotalHeight();
  221. }
  222. private function updateDebugVarsVisible(event:Event = null):void
  223. {
  224. debugVarsBox.visible = showDebugVarsCB.value;
  225. updateTotalHeight();
  226. }
  227. private function updatePreviewVisible(event:Event = null):void
  228. {
  229. previewVisible = showResultCB.value;
  230. updateTotalHeight();
  231. }
  232. private function updateHistogramVisible(event:Event = null):void
  233. {
  234. histogramVisible = showHistogramCB.value;
  235. updateTotalHeight();
  236. }
  237. public function updatePanelState():void
  238. {
  239. isEnabledCB.value = filter.enabled;
  240. isEnabledCB.label = isEnabledCB.value ? "On":"Off"
  241. previewLabel.text = int(filter.previewScale * 100) + "%";
  242. window.alpha = histogramView.alpha = (isEnabledCB.value) ? 1:0.5;
  243. if (debugVarsBox) debugVarsBox.alpha = window.alpha;
  244. }
  245. private function histogramChannelsChange(event:Event):void
  246. {
  247. previewBmp.bitmapData = filter.previewBmpData;
  248. }
  249. // GETTERS & SETTERS //////////////////////////////////////////////////////////////////
  250. public function set previewVisible(state:Boolean):void
  251. {
  252. previewHolder.visible = showResultCB.value = state;
  253. filter.displayPreview = state;
  254. updateTotalHeight();
  255. if (state)
  256. {
  257. previewHolder.addEventListener(MouseEvent.MOUSE_MOVE, previewMouseMove, false, 0, true);
  258. previewHolder.addEventListener(MouseEvent.MOUSE_DOWN, previewMouseDown, false, 0, true);
  259. previewHolder.addEventListener(MouseEvent.MOUSE_UP, previewMouseUp, false, 0, true);
  260. } else {
  261. previewHolder.removeEventListener(MouseEvent.MOUSE_MOVE, previewMouseMove);
  262. previewHolder.removeEventListener(MouseEvent.MOUSE_DOWN, previewMouseDown);
  263. previewHolder.removeEventListener(MouseEvent.MOUSE_UP, previewMouseUp);
  264. }
  265. }
  266. public function get previewVisible():Boolean
  267. {
  268. return previewHolder.visible;
  269. }
  270. public function set paramsVisible(state:Boolean):void
  271. {
  272. window.minimized = !state;
  273. }
  274. public function set histogramVisible(state:Boolean):void
  275. {
  276. histogramView.visible = state;
  277. filter.generateHistogram = state;
  278. showHistogramCB.value = state;
  279. updateTotalHeight();
  280. }
  281. public function get histogramVisible():Boolean
  282. {
  283. return filter.generateHistogram;
  284. }
  285. public function set debugVarsVisible(state:Boolean):void
  286. {
  287. showDebugVarsCB.value = state;
  288. updateDebugVarsVisible();
  289. }
  290. public function get debugVarsVisible():Boolean
  291. {
  292. return showDebugVarsCB.value;
  293. }
  294. public function set filterEnabled(value:Boolean):void
  295. {
  296. trace(this, "filterEnabled", value);
  297. filter.enabled = value;
  298. updatePanelState();
  299. if (value)
  300. {
  301. filter.addEventListener(HiSlopeEvent.FILTER_PROCESSED, render, false, 0, true);
  302. }
  303. else
  304. {
  305. filter.removeEventListener(HiSlopeEvent.FILTER_PROCESSED, render);
  306. window.title = filter.name.toUpperCase();
  307. }
  308. }
  309. public function get filterEnabled():Boolean
  310. {
  311. return filter.enabled;
  312. }
  313. override public function get height():Number
  314. {
  315. return vbox.height;
  316. }
  317. // HELPERS ////////////////////////////////////////////////////////////////////////////
  318. public function drawPanel(lineColor:uint, backgroundColor:uint):void
  319. {
  320. line.graphics.clear();
  321. line.graphics.lineStyle(0, lineColor, 2);
  322. line.graphics.moveTo(0, 0);
  323. line.graphics.lineTo(320, 0);
  324. window.color = backgroundColor;
  325. }
  326. public function showParams():void
  327. {
  328. window.minimized = false;
  329. }
  330. public function hideParams():void
  331. {
  332. window.minimized = true;
  333. }
  334. private function updateNameAndValue(param:Object):void
  335. {
  336. ui.value = param.current;
  337. ui.name = param.name;
  338. }
  339. private function updateOffsetY():void
  340. {
  341. offsetY += ui.height - 3;
  342. }
  343. private function addLabel(component:Component, labelText:String, offset:int = 0):void
  344. {
  345. var label:Label = new Label();
  346. label.text = labelText;
  347. label.draw();
  348. label.x = component.x;
  349. label.y = component.y + offset;
  350. component.x += label.width + 5;
  351. window.content.addChild(label);
  352. }
  353. public function addSlider(param:Object, callback:Function, decimalPoints:int, tick:Number):void
  354. {
  355. if (param.mode != "readonly")
  356. {
  357. ui = new HUIBarSlider(window.content, 10, offsetY, param.label, callback);
  358. /*ui = new HUISlider(window.content, 10, offsetY, param.label, callback);*/
  359. ui.minimum = param.min;
  360. ui.maximum = param.max;
  361. ui.labelPrecision = decimalPoints;
  362. ui.tick = tick;
  363. ui.setSize(320, 18);
  364. } else {
  365. ui = new ProgressBar(window.content, 10, offsetY + 4);
  366. addLabel(ui, param.label, -4);
  367. offsetY += 6;
  368. }
  369. if (param.type == "hex") ui.displayHex = true;
  370. updateNameAndValue(param);
  371. updateOffsetY();
  372. }
  373. /*public function addRange(param:Object, callback:Function):void
  374. {
  375. ui = new HRangeSlider(window.content, 10, offsetY, callback);
  376. ui.minimum = param.min;
  377. ui.maximum = param.max;
  378. ui.name = param.name;
  379. ui.lowValue = param.min;
  380. ui.highValue = param.max;
  381. updateOffsetY();
  382. }*/
  383. public function addCombo(param:Object, callback:Function):void
  384. {
  385. offsetY += 4;
  386. ui = new ComboBox(window.content, 10, offsetY, param.label, param.items);
  387. ui.addEventListener(Event.SELECT, callback);
  388. offsetY += 4;
  389. ui.name = param.name;
  390. updateOffsetY();
  391. }
  392. public function addInput(param:Object, callback:Function):void
  393. {
  394. offsetY += 4;
  395. ui = new InputText(window.content, 10, offsetY, param.label, callback);
  396. offsetY += 4;
  397. updateNameAndValue(param);
  398. updateOffsetY();
  399. }
  400. public function addStepper(param:Object, callback:Function):void
  401. {
  402. offsetY += 4;
  403. ui = new NumericStepper(window.content, 10, offsetY, callback);
  404. addLabel(ui, param.label);
  405. ui.minimum = param.min;
  406. ui.maximum = param.max;
  407. offsetY += 4;
  408. updateNameAndValue(param);
  409. updateOffsetY();
  410. }
  411. public function addKnob(param:Object, callback:Function):void
  412. {
  413. offsetY += 4;
  414. ui = new SmallKnob(window.content, 10, offsetY, param.label, callback);
  415. ui.minimum = param.min;
  416. ui.maximum = param.max;
  417. offsetY += 24;
  418. updateNameAndValue(param);
  419. updateOffsetY();
  420. }
  421. public function addColorChooser(param:Object, callback:Function):void
  422. {
  423. offsetY += 2;
  424. ui = new ColorChooser(window.content, 10, offsetY, param.label, callback);
  425. offsetY += 4;
  426. //ui.usePopup = true;
  427. updateNameAndValue(param);
  428. updateOffsetY();
  429. }
  430. public function addCheckBox(param:Object, callback:Function):void
  431. {
  432. offsetY += 4;
  433. ui = new CheckBox(window.content, 10, offsetY, param.label, callback);
  434. offsetY += 4;
  435. updateNameAndValue(param);
  436. updateOffsetY();
  437. }
  438. public function addPushButton(param:Object, callback:Function):void
  439. {
  440. offsetY += 4;
  441. ui = new PushButton(window.content, 10, offsetY, param.label.toUpperCase(), callback);
  442. /*ui.name = param.callback;*/
  443. ui.height = 15;
  444. offsetY += 4;
  445. updateOffsetY();
  446. }
  447. }
  448. }