/Tool/src/com/colorpicker/sections/paletteview/PaletteView.as

https://github.com/nodename/Color-Tool
ActionScript | 144 lines | 91 code | 50 blank | 3 comment | 10 complexity | 189e8f2304032dba893e699c13203efa MD5 | raw file
  1. package com.colorpicker.sections.paletteview {
  2. import com.colorpicker.sections.paletteview.events.PaletteChangeEvent;
  3. import com.colorpicker.sections.paletteview.events.PaletteChipEvent;
  4. import flash.display.Sprite;
  5. /**
  6. * @author mikesven
  7. */
  8. public class PaletteView extends Sprite {
  9. private var theWidth:int;
  10. private var theHeight:int;
  11. private var currentColors:Vector.<String>;
  12. private var currentChips:Vector.<PaletteChip>;
  13. private var currentChipWidth:Number;
  14. public function PaletteView(colorList:Vector.<String>, palletWidth:int, palletHeight:int) {
  15. theWidth = palletWidth;
  16. theHeight = palletHeight;
  17. currentColors = colorList;
  18. addEventListener(PaletteChipEvent.REMOVE_CHIP, onRemoveChip);
  19. addEventListener(PaletteChipEvent.REPOSTION_CHIP, onRepositionChip);
  20. init();
  21. }
  22. public function destroy():void{
  23. clearPalletView();
  24. removeEventListener(PaletteChipEvent.REMOVE_CHIP, onRemoveChip);
  25. removeEventListener(PaletteChipEvent.REPOSTION_CHIP, onRepositionChip);
  26. }
  27. public function clearPalletView():void{
  28. if(currentChips != null){
  29. var len:int = currentChips.length;
  30. var i:int = 0;
  31. for(i; i < len; i++){
  32. removeChild(currentChips[i]);
  33. }
  34. currentChips = null;
  35. currentColors = null;
  36. }
  37. }
  38. private function init() : void {
  39. var chipCount:int = currentColors.length;
  40. currentChipWidth = theWidth / chipCount;
  41. if(currentChips == null){
  42. currentChips = new Vector.<PaletteChip>();
  43. currentColors.forEach(createChip);
  44. } else {
  45. currentChips.forEach(redrawChip);
  46. }
  47. }
  48. private function createChip(color:String, index:int, vec:Vector.<String>):void{
  49. var chip:PaletteChip = new PaletteChip(uint(color), index, currentChipWidth, theHeight, currentChipWidth * index);
  50. currentChips.push(chip);
  51. addChild(chip);
  52. }
  53. private function redrawChip(color:PaletteChip, index:int, vec:Vector.<PaletteChip>):void{
  54. color.update(index, index * currentChipWidth, currentChipWidth);
  55. addChild(color);
  56. }
  57. private function onRemoveChip(e:PaletteChipEvent):void{
  58. currentChips.forEach(removeAllChips);
  59. currentChips[e.chipID].destroy();
  60. currentChips[e.chipID] = null;
  61. currentChips.splice(e.chipID, 1);
  62. currentColors.splice(e.chipID, 1);
  63. dispatchEvent(new PaletteChangeEvent(PaletteChangeEvent.PALLET_CHANGE, currentColors, true, false));
  64. init();
  65. }
  66. private function onRepositionChip(e:PaletteChipEvent):void{
  67. var targetID:int = e.chipID;
  68. var targetChip:PaletteChip = currentChips[targetID];
  69. var i:int = 0;
  70. var max:int = currentChips.length;
  71. for(i; i < max; i++){
  72. var chip:PaletteChip = currentChips[i];
  73. if(chip.getBounds(this).contains(targetChip.x, targetChip.y) && chip.chipID != targetID){
  74. insertItemAt(targetID, chip.chipID);
  75. init();
  76. return;
  77. }
  78. }
  79. init();
  80. }
  81. private function removeAllChips(color:PaletteChip, index:int, vec:Vector.<PaletteChip>):void{
  82. removeChild(color);
  83. }
  84. private function insertItemAt(item:int, positionB:int):void{
  85. var tempChip:PaletteChip = currentChips[item];
  86. currentChips.splice(item, 1);
  87. currentChips.splice(positionB, 0, tempChip);
  88. var tempColor:String = currentColors[item];
  89. currentColors.splice(item, 1);
  90. currentColors.splice(positionB, 0, tempColor);
  91. dispatchEvent(new PaletteChangeEvent(PaletteChangeEvent.PALLET_CHANGE, currentColors, true, false));
  92. }
  93. }
  94. }