/client/src/fl/controls/ColorPicker.as

https://github.com/lilin01/haha · ActionScript · 1690 lines · 676 code · 156 blank · 858 comment · 110 complexity · beba97eef809f3b35376d05962ecd2db MD5 · raw file

  1. // Copyright 2007. Adobe Systems Incorporated. All Rights Reserved.
  2. package fl.controls {
  3. import fl.core.UIComponent;
  4. import fl.core.InvalidationType;
  5. import fl.controls.BaseButton;
  6. import fl.controls.TextInput;
  7. import fl.controls.TextArea;
  8. import fl.events.ComponentEvent;
  9. import fl.events.ColorPickerEvent;
  10. import fl.managers.IFocusManager;
  11. import fl.managers.IFocusManagerComponent;
  12. import flash.events.MouseEvent;
  13. import flash.events.Event;
  14. import flash.events.KeyboardEvent;
  15. import flash.events.FocusEvent;
  16. import flash.display.DisplayObject;
  17. import flash.display.Graphics;
  18. import flash.display.Sprite;
  19. import flash.geom.ColorTransform;
  20. import flash.geom.Point;
  21. import flash.geom.Rectangle;
  22. import flash.text.TextField;
  23. import flash.text.TextFieldType;
  24. import flash.text.TextFormat;
  25. import flash.ui.Keyboard;
  26. import flash.system.IME;
  27. //--------------------------------------
  28. // Events
  29. //--------------------------------------
  30. /**
  31. * Dispatched when the user opens the color palette.
  32. *
  33. * @eventType flash.events.Event.OPEN
  34. *
  35. * @includeExample examples/ColorPicker.open.2.as -noswf
  36. *
  37. * @see #event:close
  38. * @see #open()
  39. *
  40. * @langversion 3.0
  41. * @playerversion Flash 9.0.28.0
  42. *
  43. * @playerversion AIR 1.0
  44. * @productversion Flash CS3
  45. */
  46. [Event(name="open", type="flash.events.Event")]
  47. /**
  48. * Dispatched when the user closes the color palette.
  49. *
  50. * @eventType flash.events.Event.CLOSE
  51. *
  52. * @see #event:open
  53. * @see #close()
  54. *
  55. * @langversion 3.0
  56. * @playerversion Flash 9.0.28.0
  57. *
  58. * @playerversion AIR 1.0
  59. * @productversion Flash CS3
  60. */
  61. [Event(name="close", type="flash.events.Event")]
  62. /**
  63. * Dispatched when the user clicks a color in the palette.
  64. *
  65. * @includeExample examples/ColorPicker.hexValue.1.as -noswf
  66. *
  67. * @eventType fl.events.ColorPickerEvent.CHANGE
  68. *
  69. * @langversion 3.0
  70. * @playerversion Flash 9.0.28.0
  71. *
  72. * @playerversion AIR 1.0
  73. * @productversion Flash CS3
  74. */
  75. [Event(name="change", type="fl.events.ColorPickerEvent")]
  76. /**
  77. * Dispatched when the user rolls over a swatch in the color palette.
  78. *
  79. * @eventType fl.events.ColorPickerEvent.ITEM_ROLL_OVER
  80. *
  81. * @see #event:itemRollOut
  82. *
  83. * @langversion 3.0
  84. * @playerversion Flash 9.0.28.0
  85. *
  86. * @playerversion AIR 1.0
  87. * @productversion Flash CS3
  88. */
  89. [Event(name="itemRollOver", type="fl.events.ColorPickerEvent")]
  90. /**
  91. * Dispatched when the user rolls out of a swatch in the color palette.
  92. *
  93. * @eventType fl.events.ColorPickerEvent.ITEM_ROLL_OUT
  94. *
  95. * @see #event:itemRollOver
  96. *
  97. * @langversion 3.0
  98. * @playerversion Flash 9.0.28.0
  99. *
  100. * @playerversion AIR 1.0
  101. * @productversion Flash CS3
  102. */
  103. [Event(name="itemRollOut", type="fl.events.ColorPickerEvent")]
  104. /**
  105. * Dispatched when the user presses the Enter key after editing the internal text field of the ColorPicker component.
  106. *
  107. * @eventType fl.events.ColorPickerEvent.ENTER
  108. *
  109. * @includeExample examples/ColorPicker.enter.1.as -noswf
  110. *
  111. * @see #editable
  112. * @see #textField
  113. *
  114. * @langversion 3.0
  115. * @playerversion Flash 9.0.28.0
  116. *
  117. * @playerversion AIR 1.0
  118. * @productversion Flash CS3
  119. */
  120. [Event(name="enter", type="fl.events.ColorPickerEvent")]
  121. //--------------------------------------
  122. // Styles
  123. //--------------------------------------
  124. /**
  125. * Defines the padding that appears around each swatch in the color palette, in pixels.
  126. *
  127. * @default 1
  128. *
  129. * @langversion 3.0
  130. * @playerversion Flash 9.0.28.0
  131. *
  132. * @playerversion AIR 1.0
  133. * @productversion Flash CS3
  134. */
  135. [Style(name="swatchPadding", type="Number", format="Length")]
  136. /**
  137. * The class that provides the skin for a disabled button in the ColorPicker.
  138. *
  139. * @default ColorPicker_disabledSkin
  140. *
  141. * @langversion 3.0
  142. * @playerversion Flash 9.0.28.0
  143. *
  144. * @internal [kenos] Changed description from "Disabled skin for the ColorPicker button" to the current.
  145. * Is this correct?
  146. *
  147. * @playerversion AIR 1.0
  148. * @productversion Flash CS3
  149. */
  150. [Style(name="disabledSkin", type="Class")]
  151. /**
  152. * The padding that appears around the color TextField, in pixels.
  153. *
  154. * @default 3
  155. *
  156. * @langversion 3.0
  157. * @playerversion Flash 9.0.28.0
  158. *
  159. * @playerversion AIR 1.0
  160. * @productversion Flash CS3
  161. */
  162. [Style(name="textPadding", type="Number", format="Length")]
  163. /**
  164. * The class that provides the skin for the color well when the pointing device rolls over it.
  165. *
  166. * @default ColorPicker_overSkin
  167. *
  168. * @langversion 3.0
  169. * @playerversion Flash 9.0.28.0
  170. *
  171. * @playerversion AIR 1.0
  172. * @productversion Flash CS3
  173. */
  174. [Style(name="overSkin", type="Class")]
  175. /**
  176. * The padding that appears around the group of color swatches, in pixels.
  177. *
  178. * @default 5
  179. *
  180. * @langversion 3.0
  181. * @playerversion Flash 9.0.28.0
  182. *
  183. * @playerversion AIR 1.0
  184. * @productversion Flash CS3
  185. */
  186. [Style(name="backgroundPadding", type="Number", format="Length")]
  187. /**
  188. * The class that provides the skin for the color well when it is filled with a color.
  189. *
  190. * @default ColorPicker_colorWell
  191. *
  192. * @langversion 3.0
  193. * @playerversion Flash 9.0.28.0
  194. *
  195. *
  196. * @playerversion AIR 1.0
  197. * @productversion Flash CS3
  198. */
  199. [Style(name="colorWell", type="Class")]
  200. /**
  201. * The class that provides the skin for the ColorPicker button when it is in the down position.
  202. *
  203. * @default ColorPicker_downSkin
  204. *
  205. * @langversion 3.0
  206. * @playerversion Flash 9.0.28.0
  207. *
  208. * @internal [kenos] Description was "Down skin for the ColorPicker button." Is the revised description correct?
  209. *
  210. * @playerversion AIR 1.0
  211. * @productversion Flash CS3
  212. */
  213. [Style(name="downSkin", type="Class")]
  214. /**
  215. * The class that provides the background for the text field of the ColorPicker component.
  216. *
  217. * @default ColorPicker_textFieldSkin
  218. *
  219. * @langversion 3.0
  220. * @playerversion Flash 9.0.28.0
  221. *
  222. * @playerversion AIR 1.0
  223. * @productversion Flash CS3
  224. */
  225. [Style(name="textFieldSkin", type="Class")]
  226. /**
  227. * The class that provides the background of the palette that appears in the ColorPicker component.
  228. *
  229. * @default ColorPicker_backgroundSkin
  230. *
  231. * @langversion 3.0
  232. * @playerversion Flash 9.0.28.0
  233. *
  234. * @playerversion AIR 1.0
  235. * @productversion Flash CS3
  236. */
  237. [Style(name="background", type="Class")]
  238. /**
  239. * The class that provides the skin which is used to draw the swatches contained in the ColorPicker component.
  240. *
  241. * @default ColorPicker_swatchSkin
  242. *
  243. * @langversion 3.0
  244. * @playerversion Flash 9.0.28.0
  245. *
  246. * @playerversion AIR 1.0
  247. * @productversion Flash CS3
  248. */
  249. [Style(name="swatchSkin", type="Class")]
  250. /**
  251. * The class that provides the skin which is used to highlight the currently selected color.
  252. *
  253. * @default ColorPicker_swatchSelectedSkin
  254. *
  255. * @langversion 3.0
  256. * @playerversion Flash 9.0.28.0
  257. *
  258. * @playerversion AIR 1.0
  259. * @productversion Flash CS3
  260. */
  261. [Style(name="swatchSelectedSkin", type="Class")]
  262. /**
  263. * The width of each swatch, in pixels.
  264. *
  265. * @default 10
  266. *
  267. * @langversion 3.0
  268. * @playerversion Flash 9.0.28.0
  269. *
  270. * @playerversion AIR 1.0
  271. * @productversion Flash CS3
  272. */
  273. [Style(name="swatchWidth", type="Number", format="Length")]
  274. /**
  275. * The height of each swatch, in pixels.
  276. *
  277. * @default 10
  278. *
  279. * @langversion 3.0
  280. * @playerversion Flash 9.0.28.0
  281. *
  282. * @playerversion AIR 1.0
  283. * @productversion Flash CS3
  284. */
  285. [Style(name="swatchHeight", type="Number", format="Length")]
  286. /**
  287. * The number of columns to be drawn in the ColorPicker color palette.
  288. *
  289. * @default 18
  290. *
  291. * @langversion 3.0
  292. * @playerversion Flash 9.0.28.0
  293. *
  294. * @playerversion AIR 1.0
  295. * @productversion Flash CS3
  296. */
  297. [Style(name="columnCount", type="Number", format="Length")]
  298. /**
  299. * The class that provides the skin for the ColorPicker button when it is in the up position.
  300. *
  301. * @default ColorPicker_upSkin
  302. *
  303. * @langversion 3.0
  304. * @playerversion Flash 9.0.28.0
  305. *
  306. * @internal [kenos] The previous description was "Up skin for the ColorPicker button." Is the revised description
  307. * correct?
  308. *
  309. * @playerversion AIR 1.0
  310. * @productversion Flash CS3
  311. */
  312. [Style(name="upSkin", type="Class")]
  313. /**
  314. * @copy fl.controls.LabelButton#style:embedFonts
  315. *
  316. * @default false
  317. *
  318. * @langversion 3.0
  319. * @playerversion Flash 9.0.28.0
  320. *
  321. * @playerversion AIR 1.0
  322. * @productversion Flash CS3
  323. */
  324. [Style(name="embedFonts", type="Boolean")]
  325. //--------------------------------------
  326. // Class description
  327. //--------------------------------------
  328. /**
  329. * The ColorPicker component displays a list of one or more swatches
  330. * from which the user can select a color.
  331. *
  332. * <p>By default, the component displays a single swatch of color on a
  333. * square button. When the user clicks this button, a panel opens to
  334. * display the complete list of swatches.</p>
  335. *
  336. * @includeExample examples/ColorPickerExample.as
  337. *
  338. * @see fl.events.ColorPickerEvent ColorPickerEvent
  339. *
  340. * @langversion 3.0
  341. * @playerversion Flash 9.0.28.0
  342. *
  343. * @playerversion AIR 1.0
  344. * @productversion Flash CS3
  345. */
  346. public class ColorPicker extends UIComponent implements IFocusManagerComponent {
  347. /**
  348. * A reference to the internal text field of the ColorPicker component.
  349. *
  350. * @see #showTextField
  351. *
  352. * @langversion 3.0
  353. * @playerversion Flash 9.0.28.0
  354. *
  355. * @playerversion AIR 1.0
  356. * @productversion Flash CS3
  357. */
  358. public var textField:TextField;
  359. /**
  360. * @private (protected)
  361. *
  362. * @langversion 3.0
  363. * @playerversion Flash 9.0.28.0
  364. */
  365. protected var customColors:Array;
  366. /**
  367. * @private (protected)
  368. *
  369. * @langversion 3.0
  370. * @playerversion Flash 9.0.28.0
  371. */
  372. public static var defaultColors:Array;
  373. /**
  374. * @private (protected)
  375. *
  376. * @langversion 3.0
  377. * @playerversion Flash 9.0.28.0
  378. */
  379. protected var colorHash:Object;
  380. /**
  381. * @private (protected)
  382. *
  383. * @langversion 3.0
  384. * @playerversion Flash 9.0.28.0
  385. */
  386. protected var paletteBG:DisplayObject;
  387. /**
  388. * @private (protected)
  389. *
  390. * @langversion 3.0
  391. * @playerversion Flash 9.0.28.0
  392. */
  393. protected var selectedSwatch:Sprite;
  394. /**
  395. * @private (protected)
  396. *
  397. * @langversion 3.0
  398. * @playerversion Flash 9.0.28.0
  399. */
  400. protected var _selectedColor:uint;
  401. /**
  402. * @private (protected)
  403. *
  404. * @langversion 3.0
  405. * @playerversion Flash 9.0.28.0
  406. */
  407. protected var rollOverColor:int = -1;
  408. /**
  409. * @private (protected)
  410. *
  411. * @langversion 3.0
  412. * @playerversion Flash 9.0.28.0
  413. */
  414. protected var _editable:Boolean = true;
  415. /**
  416. * @private (protected)
  417. *
  418. * @langversion 3.0
  419. * @playerversion Flash 9.0.28.0
  420. */
  421. protected var _showTextField:Boolean = true;
  422. /**
  423. * @private (protected)
  424. *
  425. * @langversion 3.0
  426. * @playerversion Flash 9.0.28.0
  427. */
  428. protected var isOpen:Boolean = false;
  429. /**
  430. * @private (protected)
  431. *
  432. * @langversion 3.0
  433. * @playerversion Flash 9.0.28.0
  434. */
  435. protected var doOpen:Boolean = false;
  436. /**
  437. * @private (protected)
  438. *
  439. * @langversion 3.0
  440. * @playerversion Flash 9.0.28.0
  441. */
  442. protected var swatchButton:BaseButton;
  443. /**
  444. * @private (protected)
  445. *
  446. * @langversion 3.0
  447. * @playerversion Flash 9.0.28.0
  448. */
  449. protected var colorWell:DisplayObject;
  450. /**
  451. * @private (protected)
  452. *
  453. * @langversion 3.0
  454. * @playerversion Flash 9.0.28.0
  455. */
  456. protected var swatchSelectedSkin:DisplayObject;
  457. /**
  458. * @private (protected)
  459. *
  460. * @langversion 3.0
  461. * @playerversion Flash 9.0.28.0
  462. */
  463. protected var palette:Sprite;
  464. /**
  465. * @private (protected)
  466. *
  467. * @langversion 3.0
  468. * @playerversion Flash 9.0.28.0
  469. */
  470. protected var textFieldBG:DisplayObject;
  471. /**
  472. * @private (protected)
  473. *
  474. * @langversion 3.0
  475. * @playerversion Flash 9.0.28.0
  476. */
  477. protected var swatches:Sprite;
  478. /**
  479. * @private (protected)
  480. *
  481. * @langversion 3.0
  482. * @playerversion Flash 9.0.28.0
  483. */
  484. protected var swatchMap:Array;
  485. /**
  486. * @private (protected)
  487. *
  488. * @langversion 3.0
  489. * @playerversion Flash 9.0.28.0
  490. */
  491. protected var currRowIndex:int;
  492. /**
  493. * @private (protected)
  494. *
  495. * @langversion 3.0
  496. * @playerversion Flash 9.0.28.0
  497. */
  498. protected var currColIndex:int;
  499. /**
  500. * @private
  501. *
  502. * @langversion 3.0
  503. * @playerversion Flash 9.0.28.0
  504. */
  505. private static var defaultStyles:Object = {upSkin:"ColorPicker_upSkin", disabledSkin:"ColorPicker_disabledSkin",
  506. overSkin:"ColorPicker_overSkin", downSkin:"ColorPicker_downSkin", colorWell:"ColorPicker_colorWell",
  507. swatchSkin:"ColorPicker_swatchSkin", swatchSelectedSkin:"ColorPicker_swatchSelectedSkin",
  508. swatchWidth:10, swatchHeight:10,
  509. columnCount:18, swatchPadding:1,
  510. textFieldSkin:"ColorPicker_textFieldSkin",
  511. textFieldWidth:null, textFieldHeight:null, textPadding:3,
  512. background:"ColorPicker_backgroundSkin", backgroundPadding:5,
  513. textFormat:null, focusRectSkin:null, focusRectPadding:null,
  514. embedFonts:false
  515. };
  516. /**
  517. * @copy fl.core.UIComponent#getStyleDefinition()
  518. *
  519. * @includeExample ../core/examples/UIComponent.getStyleDefinition.1.as -noswf
  520. *
  521. * @see fl.core.UIComponent#getStyle() UIComponent.getStyle()
  522. * @see fl.core.UIComponent#setStyle() UIComponent.setStyle()
  523. * @see fl.managers.StyleManager StyleManager
  524. *
  525. * @langversion 3.0
  526. * @playerversion Flash 9.0.28.0
  527. *
  528. * @playerversion AIR 1.0
  529. * @productversion Flash CS3
  530. */
  531. public static function getStyleDefinition():Object {
  532. return defaultStyles;
  533. }
  534. /**
  535. * @private (protected)
  536. *
  537. * @langversion 3.0
  538. * @playerversion Flash 9.0.28.0
  539. */
  540. protected static const POPUP_BUTTON_STYLES:Object = {
  541. disabledSkin:"disabledSkin",
  542. downSkin:"downSkin",
  543. overSkin:"overSkin",
  544. upSkin:"upSkin"
  545. };
  546. /**
  547. * @private (protected)
  548. *
  549. * @langversion 3.0
  550. * @playerversion Flash 9.0.28.0
  551. */
  552. protected static const SWATCH_STYLES:Object = {
  553. disabledSkin:"swatchSkin",
  554. downSkin:"swatchSkin",
  555. overSkin:"swatchSkin",
  556. upSkin:"swatchSkin"
  557. };
  558. /**
  559. * Creates an instance of the ColorPicker class.
  560. *
  561. * @langversion 3.0
  562. * @playerversion Flash 9.0.28.0
  563. *
  564. * @playerversion AIR 1.0
  565. * @productversion Flash CS3
  566. */
  567. public function ColorPicker() {
  568. super();
  569. }
  570. [Inspectable(type="Color",defaultValue="#000000")]
  571. /**
  572. * Gets or sets the swatch that is currently highlighted in the palette of the ColorPicker component.
  573. *
  574. * @default 0x000000
  575. *
  576. * @includeExample examples/ColorPicker.selectedColor.1.as -noswf
  577. *
  578. * @langversion 3.0
  579. * @playerversion Flash 9.0.28.0
  580. *
  581. * @playerversion AIR 1.0
  582. * @productversion Flash CS3
  583. */
  584. public function get selectedColor():uint {
  585. if (colorWell == null) {
  586. return 0;
  587. }
  588. return colorWell.transform.colorTransform.color;
  589. }
  590. /**
  591. * @private (setter)
  592. *
  593. * @langversion 3.0
  594. * @playerversion Flash 9.0.28.0
  595. */
  596. public function set selectedColor(value:uint):void {
  597. if (!_enabled) {
  598. return;
  599. }
  600. _selectedColor = value;
  601. rollOverColor = -1;
  602. currColIndex = currRowIndex = 0;
  603. // Set the color value immediately to avoid invalidation.
  604. var ct:ColorTransform = new ColorTransform();
  605. ct.color = value;
  606. setColorWellColor(ct);
  607. invalidate(InvalidationType.DATA);
  608. }
  609. /**
  610. * Gets the string value of the current color selection.
  611. *
  612. * @includeExample examples/ColorPicker.hexValue.1.as -noswf
  613. *
  614. * @langversion 3.0
  615. * @playerversion Flash 9.0.28.0
  616. *
  617. * @playerversion AIR 1.0
  618. * @productversion Flash CS3
  619. */
  620. public function get hexValue():String {
  621. if (colorWell == null) {
  622. return colorToString(0);
  623. }
  624. return colorToString(colorWell.transform.colorTransform.color);
  625. }
  626. [Inspectable(defaultValue=true, verbose=1)]
  627. /**
  628. * @copy fl.core.UIComponent#enabled
  629. *
  630. * @default true
  631. *
  632. * @langversion 3.0
  633. * @playerversion Flash 9.0.28.0
  634. *
  635. * @playerversion AIR 1.0
  636. * @productversion Flash CS3
  637. */
  638. override public function get enabled():Boolean {
  639. return super.enabled;
  640. }
  641. /**
  642. * @private (setter)
  643. *
  644. * @langversion 3.0
  645. * @playerversion Flash 9.0.28.0
  646. */
  647. override public function set enabled(value:Boolean):void {
  648. super.enabled = value;
  649. if (!value) {
  650. close();
  651. }
  652. swatchButton.enabled = value;
  653. }
  654. /**
  655. * Gets or sets a Boolean value that indicates whether the internal text field of the
  656. * ColorPicker component is editable. A value of <code>true</code> indicates that
  657. * the internal text field is editable; a value of <code>false</code> indicates
  658. * that it is not.
  659. *
  660. * @default true
  661. *
  662. * @langversion 3.0
  663. * @playerversion Flash 9.0.28.0
  664. *
  665. * @playerversion AIR 1.0
  666. * @productversion Flash CS3
  667. */
  668. public function get editable():Boolean {
  669. return _editable;
  670. }
  671. /**
  672. * @private (setter)
  673. *
  674. * @langversion 3.0
  675. * @playerversion Flash 9.0.28.0
  676. */
  677. public function set editable(value:Boolean):void {
  678. _editable = value;
  679. invalidate(InvalidationType.STATE);
  680. }
  681. [Inspectable(defaultValue=true)]
  682. /**
  683. * Gets or sets a Boolean value that indicates whether the internal text field
  684. * of the ColorPicker component is displayed. A value of <code>true</code> indicates
  685. * that the internal text field is displayed; a value of <code>false</code> indicates
  686. * that it is not.
  687. *
  688. * @default true
  689. *
  690. * @includeExample examples/ColorPicker.showTextField.1.as -noswf
  691. *
  692. * @see #textField
  693. *
  694. * @langversion 3.0
  695. * @playerversion Flash 9.0.28.0
  696. *
  697. * @playerversion AIR 1.0
  698. * @productversion Flash CS3
  699. */
  700. public function get showTextField():Boolean {
  701. return _showTextField;
  702. }
  703. /**
  704. * @private (setter)
  705. *
  706. * @langversion 3.0
  707. * @playerversion Flash 9.0.28.0
  708. */
  709. public function set showTextField(value:Boolean):void {
  710. invalidate(InvalidationType.STYLES);
  711. _showTextField = value;
  712. }
  713. /**
  714. * Gets or sets the array of custom colors that the ColorPicker component
  715. * provides. The ColorPicker component draws and displays the colors that are
  716. * described in this array.
  717. *
  718. * <p><strong>Note:</strong> The maximum number of colors that the ColorPicker
  719. * component can display is 1024.</p>
  720. *
  721. * <p>By default, this array contains 216 autogenerated colors.</p>
  722. *
  723. * @includeExample examples/ColorPicker.colors.1.as -noswf
  724. *
  725. * @langversion 3.0
  726. * @playerversion Flash 9.0.28.0
  727. *
  728. *
  729. * @playerversion AIR 1.0
  730. * @productversion Flash CS3
  731. */
  732. public function get colors():Array {
  733. return (customColors != null)?customColors:ColorPicker.defaultColors;
  734. }
  735. /**
  736. * @private (setter)
  737. *
  738. * @langversion 3.0
  739. * @playerversion Flash 9.0.28.0
  740. */
  741. public function set colors(value:Array):void {
  742. customColors = value;
  743. invalidate(InvalidationType.DATA);
  744. }
  745. /**
  746. * @copy fl.controls.TextArea#imeMode
  747. *
  748. * @see flash.system.IMEConversionMode IMEConversionMode
  749. *
  750. * @langversion 3.0
  751. * @playerversion Flash 9.0.28.0
  752. *
  753. * @playerversion AIR 1.0
  754. * @productversion Flash CS3
  755. */
  756. public function get imeMode():String {
  757. return _imeMode;
  758. }
  759. /**
  760. * @private (protected)
  761. *
  762. * @langversion 3.0
  763. * @playerversion Flash 9.0.28.0
  764. */
  765. public function set imeMode(value:String):void {
  766. _imeMode = value;
  767. }
  768. /**
  769. * Shows the color palette. Calling this method causes the <code>open</code>
  770. * event to be dispatched. If the color palette is already open or disabled,
  771. * this method has no effect.
  772. *
  773. * @includeExample examples/ColorPicker.open.2.as -noswf
  774. *
  775. * @see #close()
  776. * @see #event:open
  777. *
  778. * @langversion 3.0
  779. * @playerversion Flash 9.0.28.0
  780. *
  781. * @playerversion AIR 1.0
  782. * @productversion Flash CS3
  783. */
  784. public function open():void {
  785. if (!_enabled) {
  786. return;
  787. }
  788. doOpen = true;
  789. var fm:IFocusManager = focusManager;
  790. if (fm) {
  791. fm.defaultButtonEnabled = false;
  792. }
  793. invalidate(InvalidationType.STATE);
  794. }
  795. /**
  796. * Hides the color palette. Calling this method causes the <code>close</code>
  797. * event to be dispatched. If the color palette is already closed or disabled,
  798. * this method has no effect.
  799. *
  800. * @see #event:close
  801. * @see #open()
  802. *
  803. * @langversion 3.0
  804. * @playerversion Flash 9.0.28.0
  805. *
  806. * @playerversion AIR 1.0
  807. * @productversion Flash CS3
  808. */
  809. public function close():void {
  810. if (isOpen) {
  811. stage.removeChild(palette);
  812. isOpen = false;
  813. dispatchEvent(new Event(Event.CLOSE));
  814. }
  815. var fm:IFocusManager = focusManager;
  816. if (fm) {
  817. fm.defaultButtonEnabled = true;
  818. }
  819. removeStageListener();
  820. cleanUpSelected();
  821. }
  822. /**
  823. * @private
  824. *
  825. * @langversion 3.0
  826. * @playerversion Flash 9.0.28.0
  827. */
  828. private function addCloseListener(event:Event) {
  829. removeEventListener(Event.ENTER_FRAME, addCloseListener);
  830. if (!isOpen) {
  831. return;
  832. }
  833. addStageListener();
  834. }
  835. /**
  836. * @private (protected)
  837. *
  838. * @langversion 3.0
  839. * @playerversion Flash 9.0.28.0
  840. */
  841. protected function onStageClick(event:MouseEvent):void {
  842. if (!contains(event.target as DisplayObject) && !palette.contains(event.target as DisplayObject)) {
  843. selectedColor = _selectedColor;
  844. close();
  845. }
  846. }
  847. /**
  848. * @private (protected)
  849. *
  850. * @langversion 3.0
  851. * @playerversion Flash 9.0.28.0
  852. */
  853. protected function setStyles():void {
  854. var bg:DisplayObject = colorWell;
  855. var colorWellStyle:Object = getStyleValue("colorWell");
  856. if (colorWellStyle != null) {
  857. colorWell = getDisplayObjectInstance(colorWellStyle) as DisplayObject;
  858. }
  859. addChildAt(colorWell, getChildIndex(swatchButton));
  860. copyStylesToChild(swatchButton, POPUP_BUTTON_STYLES);
  861. swatchButton.drawNow();
  862. if (bg != null && contains(bg) && bg != colorWell) {
  863. removeChild(bg);
  864. }
  865. }
  866. /**
  867. * @private (protected)
  868. *
  869. * @langversion 3.0
  870. * @playerversion Flash 9.0.28.0
  871. */
  872. protected function cleanUpSelected():void {
  873. if (swatchSelectedSkin && palette.contains(swatchSelectedSkin)) {
  874. palette.removeChild(swatchSelectedSkin);
  875. }
  876. }
  877. /**
  878. * @private (protected)
  879. *
  880. * @langversion 3.0
  881. * @playerversion Flash 9.0.28.0
  882. */
  883. protected function onPopupButtonClick(event:MouseEvent):void {
  884. if (isOpen) {
  885. close();
  886. } else {
  887. open();
  888. }
  889. }
  890. /**
  891. * @private (protected)
  892. *
  893. * @langversion 3.0
  894. * @playerversion Flash 9.0.28.0
  895. */
  896. override protected function draw():void {
  897. if (isInvalid(InvalidationType.STYLES, InvalidationType.DATA)) {
  898. setStyles();
  899. drawPalette();
  900. setEmbedFonts();
  901. invalidate(InvalidationType.DATA, false);
  902. invalidate(InvalidationType.STYLES, false);
  903. }
  904. if (isInvalid(InvalidationType.DATA)) {
  905. drawSwatchHighlight();
  906. setColorDisplay();
  907. }
  908. if (isInvalid(InvalidationType.STATE)) {
  909. setTextEditable();
  910. if (doOpen) {
  911. doOpen = false;
  912. showPalette();
  913. }
  914. colorWell.visible = enabled;
  915. }
  916. if (isInvalid(InvalidationType.SIZE, InvalidationType.STYLES)) {
  917. swatchButton.setSize(width, height);
  918. swatchButton.drawNow();
  919. colorWell.width = width;
  920. colorWell.height = height;
  921. }
  922. super.draw();
  923. }
  924. /**
  925. * @private (protected)
  926. *
  927. * @langversion 3.0
  928. * @playerversion Flash 9.0.28.0
  929. */
  930. protected function showPalette():void {
  931. if (isOpen) {
  932. positionPalette();
  933. return;
  934. }
  935. addEventListener(Event.ENTER_FRAME, addCloseListener, false, 0, true);
  936. stage.addChild(palette);
  937. isOpen = true;
  938. positionPalette();
  939. dispatchEvent(new Event(Event.OPEN));
  940. stage.focus = textField; // This is causing some issues.
  941. // Highlight the appropriate swatch.
  942. var swatch:Sprite = selectedSwatch;
  943. if (swatch == null) {
  944. swatch = findSwatch(_selectedColor);
  945. }
  946. setSwatchHighlight(swatch);
  947. }
  948. /**
  949. * @private (protected)
  950. *
  951. * @langversion 3.0
  952. * @playerversion Flash 9.0.28.0
  953. */
  954. protected function setEmbedFonts():void {
  955. var embed:Object = getStyleValue('embedFonts');
  956. if (embed != null) {
  957. textField.embedFonts = embed;
  958. }
  959. }
  960. /**
  961. * @private (protected)
  962. *
  963. * @langversion 3.0
  964. * @playerversion Flash 9.0.28.0
  965. */
  966. protected function drawSwatchHighlight():void {
  967. cleanUpSelected();
  968. var skin:Object = getStyleValue("swatchSelectedSkin");
  969. var swatchPadding:Number = getStyleValue("swatchPadding") as Number;
  970. if (skin != null) {
  971. swatchSelectedSkin = getDisplayObjectInstance(skin);
  972. swatchSelectedSkin.x = 0;
  973. swatchSelectedSkin.y = 0;
  974. swatchSelectedSkin.width = (getStyleValue("swatchWidth") as Number) + 2;
  975. swatchSelectedSkin.height = (getStyleValue("swatchHeight") as Number) + 2;
  976. }
  977. }
  978. /**
  979. * @private (protected)
  980. *
  981. * @langversion 3.0
  982. * @playerversion Flash 9.0.28.0
  983. */
  984. protected function drawPalette():void {
  985. if (isOpen) {
  986. stage.removeChild(palette);
  987. }
  988. palette = new Sprite();
  989. drawTextField();
  990. drawSwatches();
  991. drawBG();
  992. }
  993. /**
  994. * @private (protected)
  995. *
  996. * @langversion 3.0
  997. * @playerversion Flash 9.0.28.0
  998. */
  999. protected function drawTextField():void {
  1000. if (!showTextField) {
  1001. return;
  1002. }
  1003. var padding:Number = getStyleValue("backgroundPadding") as Number;
  1004. var textPadding:Number = getStyleValue("textPadding") as Number;
  1005. // Add the TextField background
  1006. textFieldBG = getDisplayObjectInstance(getStyleValue("textFieldSkin"));
  1007. if (textFieldBG != null) {
  1008. palette.addChild(textFieldBG);
  1009. textFieldBG.x = textFieldBG.y = padding;
  1010. }
  1011. // Format the text field
  1012. var uiStyles:Object = UIComponent.getStyleDefinition();
  1013. var defaultTF:TextFormat = enabled ? uiStyles.defaultTextFormat as TextFormat : uiStyles.defaultDisabledTextFormat as TextFormat;
  1014. textField.setTextFormat(defaultTF);
  1015. var tf:TextFormat = getStyleValue("textFormat") as TextFormat;
  1016. if (tf != null) {
  1017. textField.setTextFormat(tf);
  1018. } else {
  1019. tf = defaultTF;
  1020. }
  1021. textField.defaultTextFormat = tf;
  1022. setEmbedFonts();
  1023. textField.restrict = "A-Fa-f0-9#";
  1024. textField.maxChars = 6;
  1025. palette.addChild(textField);
  1026. textField.text = " #888888 ";
  1027. textField.height = textField.textHeight + 3;
  1028. textField.width = textField.textWidth + 3;
  1029. textField.text = "";
  1030. textField.x = textField.y = padding + textPadding;
  1031. textFieldBG.width = textField.width + (textPadding*2);
  1032. textFieldBG.height = textField.height + (textPadding*2);
  1033. setTextEditable();
  1034. }
  1035. /**
  1036. * @private (protected)
  1037. *
  1038. * @langversion 3.0
  1039. * @playerversion Flash 9.0.28.0
  1040. */
  1041. protected function drawSwatches():void {
  1042. var padding:Number = getStyleValue("backgroundPadding") as Number;
  1043. var swatchY:Number = (showTextField ? textFieldBG.y + textFieldBG.height + padding : padding);
  1044. swatches = new Sprite();
  1045. palette.addChild(swatches);
  1046. swatches.x = padding;
  1047. swatches.y = swatchY;
  1048. var cols:uint = getStyleValue("columnCount") as uint;
  1049. var pad:uint = getStyleValue("swatchPadding") as uint;
  1050. var w:Number = getStyleValue("swatchWidth") as Number;
  1051. var h:Number = getStyleValue("swatchHeight") as Number;
  1052. colorHash = {};
  1053. swatchMap = [];
  1054. var l:uint = Math.min(1024, colors.length);
  1055. var rc:int = -1;
  1056. for (var i:uint=0; i<l; i++) {
  1057. var s:Sprite = createSwatch(colors[i]);
  1058. s.x = (w + pad) * (i%cols);
  1059. if (s.x == 0) {
  1060. swatchMap.push([s]);
  1061. rc++;
  1062. } else {
  1063. swatchMap[rc].push(s);
  1064. }
  1065. colorHash[colors[i]] = {swatch:s, row:rc, col:swatchMap[rc].length-1};
  1066. s.y = Math.floor(i/cols) * (h + pad);
  1067. swatches.addChild(s);
  1068. }
  1069. }
  1070. /**
  1071. * @private (protected)
  1072. *
  1073. * @langversion 3.0
  1074. * @playerversion Flash 9.0.28.0
  1075. */
  1076. protected function drawBG():void {
  1077. var bg:Object = getStyleValue("background");
  1078. if (bg != null) {
  1079. paletteBG = getDisplayObjectInstance(bg) as Sprite;
  1080. }
  1081. if (paletteBG == null) {
  1082. return;
  1083. }
  1084. var padding:Number = Number(getStyleValue("backgroundPadding"));
  1085. paletteBG.width = Math.max(showTextField?textFieldBG.width:0, swatches.width) + padding*2;
  1086. paletteBG.height = swatches.y + swatches.height + padding;
  1087. palette.addChildAt(paletteBG, 0);
  1088. }
  1089. /**
  1090. * @private (protected)
  1091. *
  1092. * @langversion 3.0
  1093. * @playerversion Flash 9.0.28.0
  1094. */
  1095. protected function positionPalette():void {
  1096. var p:Point = swatchButton.localToGlobal(new Point(0,0));
  1097. var padding:Number = getStyleValue("backgroundPadding") as Number;
  1098. if (p.x + palette.width > stage.stageWidth) {// Doesn't fit
  1099. palette.x = (p.x - palette.width) <<0;
  1100. } else {
  1101. palette.x = (p.x + swatchButton.width + padding) <<0;
  1102. }
  1103. palette.y = Math.max(0, Math.min(p.y, stage.stageHeight-palette.height)) <<0;
  1104. }
  1105. /**
  1106. * @private (protected)
  1107. *
  1108. * @langversion 3.0
  1109. * @playerversion Flash 9.0.28.0
  1110. */
  1111. protected function setTextEditable():void {
  1112. if (!showTextField) {
  1113. return;
  1114. }
  1115. textField.type = editable?TextFieldType.INPUT:TextFieldType.DYNAMIC;
  1116. textField.selectable = editable;
  1117. }
  1118. /**
  1119. * @private (protected)
  1120. *
  1121. * @langversion 3.0
  1122. * @playerversion Flash 9.0.28.0
  1123. */
  1124. override protected function keyUpHandler(event:KeyboardEvent):void {
  1125. if (!isOpen) { return; }
  1126. var newColor:uint;
  1127. var cTransform:ColorTransform = new ColorTransform();
  1128. if (editable && showTextField) {
  1129. // Get the color that is currently entered.
  1130. var color:String = textField.text;
  1131. // Works with or without the #
  1132. if (color.indexOf("#") > -1) {
  1133. color = color.replace(/^\s+|\s+$/g, "");
  1134. color = color.replace(/#/g, "");
  1135. }
  1136. // Convert to a color.
  1137. newColor = parseInt(color, 16);
  1138. // Try and select the swatch
  1139. var swatch:Sprite = findSwatch(newColor);
  1140. setSwatchHighlight(swatch);
  1141. // Colorize the ColorWell
  1142. cTransform.color = newColor;
  1143. setColorWellColor(cTransform);
  1144. } else {
  1145. newColor = rollOverColor;
  1146. cTransform.color = newColor;
  1147. }
  1148. // If the ENTER key, select the color
  1149. if (event.keyCode != Keyboard.ENTER) {
  1150. return;
  1151. }
  1152. // Verify the color, and close the palette.
  1153. dispatchEvent(new ColorPickerEvent(ColorPickerEvent.ENTER, newColor));
  1154. _selectedColor = rollOverColor;
  1155. setColorText(cTransform.color);
  1156. rollOverColor = cTransform.color;
  1157. dispatchEvent(new ColorPickerEvent(ColorPickerEvent.CHANGE, selectedColor));
  1158. close();
  1159. }
  1160. /**
  1161. * @private (protected)
  1162. *
  1163. * @langversion 3.0
  1164. * @playerversion Flash 9.0.28.0
  1165. */
  1166. protected function positionTextField():void {
  1167. if (!showTextField) {
  1168. return;
  1169. }
  1170. var padding:Number = getStyleValue("backgroundPadding") as Number;
  1171. var textPadding:Number = getStyleValue("textPadding") as Number;
  1172. textFieldBG.x = paletteBG.x + padding;
  1173. textFieldBG.y = paletteBG.y + padding;
  1174. textField.x = textFieldBG.x + textPadding;
  1175. textField.y = textFieldBG.y + textPadding;
  1176. }
  1177. /**
  1178. * @private (protected)
  1179. *
  1180. * @langversion 3.0
  1181. * @playerversion Flash 9.0.28.0
  1182. */
  1183. protected function setColorDisplay():void {
  1184. if (!swatchMap.length) {
  1185. return;
  1186. }
  1187. var ct:ColorTransform = new ColorTransform(0,0,0,1,_selectedColor>>16,_selectedColor>>8&0xFF,_selectedColor&0xFF,0);
  1188. setColorWellColor(ct);
  1189. setColorText(_selectedColor);
  1190. var swatch:Sprite = findSwatch(_selectedColor);
  1191. setSwatchHighlight(swatch);
  1192. if (swatchMap.length && colorHash[_selectedColor] == undefined) {
  1193. cleanUpSelected();
  1194. }
  1195. }
  1196. /**
  1197. * @private (protected)
  1198. *
  1199. * @langversion 3.0
  1200. * @playerversion Flash 9.0.28.0
  1201. */
  1202. protected function setSwatchHighlight(swatch:Sprite):void {
  1203. if (swatch == null) {
  1204. if (palette.contains(swatchSelectedSkin)) {
  1205. palette.removeChild(swatchSelectedSkin);
  1206. }
  1207. return;
  1208. } else if (!palette.contains(swatchSelectedSkin) && colors.length > 0) {
  1209. palette.addChild(swatchSelectedSkin);
  1210. } else if (!colors.length) {
  1211. return;
  1212. }
  1213. var swatchPadding:Number = getStyleValue("swatchPadding") as Number;
  1214. palette.setChildIndex(swatchSelectedSkin, palette.numChildren-1);
  1215. swatchSelectedSkin.x = swatches.x + swatch.x - 1;
  1216. swatchSelectedSkin.y = swatches.y + swatch.y - 1;
  1217. var color = swatch.getChildByName('color').transform.colorTransform.color;
  1218. currColIndex = colorHash[color].col;
  1219. currRowIndex = colorHash[color].row;
  1220. }
  1221. /**
  1222. * @private (protected)
  1223. *
  1224. * @langversion 3.0
  1225. * @playerversion Flash 9.0.28.0
  1226. */
  1227. protected function findSwatch(color:uint):Sprite {
  1228. if (!swatchMap.length) {
  1229. return null;
  1230. }
  1231. var so:Object = colorHash[color];
  1232. if (so != null) {
  1233. return so.swatch;
  1234. }
  1235. return null;
  1236. }
  1237. /**
  1238. * @private (protected)
  1239. *
  1240. * @langversion 3.0
  1241. * @playerversion Flash 9.0.28.0
  1242. */
  1243. protected function onSwatchClick(event:MouseEvent):void {
  1244. var cTransform:ColorTransform = event.target.getChildByName('color').transform.colorTransform;
  1245. _selectedColor = cTransform.color;
  1246. dispatchEvent(new ColorPickerEvent(ColorPickerEvent.CHANGE, selectedColor));
  1247. close();
  1248. }
  1249. /**
  1250. * @private (protected)
  1251. *
  1252. * @langversion 3.0
  1253. * @playerversion Flash 9.0.28.0
  1254. */
  1255. protected function onSwatchOver(event:MouseEvent):void {
  1256. var color:BaseButton = event.target.getChildByName("color") as BaseButton;
  1257. var cTransform:ColorTransform = color.transform.colorTransform;
  1258. setColorWellColor(cTransform);
  1259. setSwatchHighlight(event.target as Sprite);
  1260. setColorText(cTransform.color);
  1261. dispatchEvent(new ColorPickerEvent(ColorPickerEvent.ITEM_ROLL_OVER, cTransform.color));
  1262. }
  1263. /**
  1264. * @private (protected)
  1265. *
  1266. * @langversion 3.0
  1267. * @playerversion Flash 9.0.28.0
  1268. */
  1269. protected function onSwatchOut(event:MouseEvent):void {
  1270. var cTransform:ColorTransform = event.target.transform.colorTransform;
  1271. dispatchEvent(new ColorPickerEvent(ColorPickerEvent.ITEM_ROLL_OUT, cTransform.color));
  1272. }
  1273. /**
  1274. * @private (protected)
  1275. *
  1276. * @langversion 3.0
  1277. * @playerversion Flash 9.0.28.0
  1278. */
  1279. protected function setColorText(color:uint):void {
  1280. if (textField == null) {
  1281. return;
  1282. }
  1283. textField.text = "#"+colorToString(color);
  1284. }
  1285. /**
  1286. * @private (protected)
  1287. *
  1288. * @langversion 3.0
  1289. * @playerversion Flash 9.0.28.0
  1290. */
  1291. protected function colorToString(color:uint):String {
  1292. var colorText:String = color.toString(16);
  1293. while (colorText.length < 6) {
  1294. colorText = "0" + colorText;
  1295. }
  1296. return colorText;
  1297. }
  1298. /**
  1299. * @private (protected)
  1300. *
  1301. * @langversion 3.0
  1302. * @playerversion Flash 9.0.28.0
  1303. */
  1304. protected function setColorWellColor(colorTransform:ColorTransform):void {
  1305. if (!colorWell) {
  1306. return;
  1307. }
  1308. colorWell.transform.colorTransform = colorTransform;
  1309. }
  1310. /**
  1311. * @private (protected)
  1312. *
  1313. * @langversion 3.0
  1314. * @playerversion Flash 9.0.28.0
  1315. */
  1316. protected function createSwatch(color:uint):Sprite {
  1317. var swatch:Sprite = new Sprite();
  1318. // Draw the Color part of the swatch
  1319. var swatchSkin:BaseButton = new BaseButton();
  1320. swatchSkin.focusEnabled = false;
  1321. var w:Number = getStyleValue("swatchWidth") as Number;
  1322. var h:Number = getStyleValue("swatchHeight") as Number;
  1323. swatchSkin.setSize(w, h);
  1324. swatchSkin.transform.colorTransform = new ColorTransform(0,0,0,1,color>>16,color>>8&0xFF,color&0xFF,0);
  1325. copyStylesToChild(swatchSkin, SWATCH_STYLES);
  1326. swatchSkin.mouseEnabled = false;
  1327. swatchSkin.drawNow();
  1328. swatchSkin.name = "color";
  1329. swatch.addChild(swatchSkin);
  1330. // Draw the border/background
  1331. var padding:Number = getStyleValue("swatchPadding") as Number;
  1332. var g:Graphics = swatch.graphics;
  1333. g.beginFill(0x000000);
  1334. g.drawRect(-padding, -padding, w+padding*2, h+padding*2);
  1335. g.endFill();
  1336. swatch.addEventListener(MouseEvent.CLICK, onSwatchClick, false, 0, true);
  1337. swatch.addEventListener(MouseEvent.MOUSE_OVER, onSwatchOver, false, 0, true);
  1338. swatch.addEventListener(MouseEvent.MOUSE_OUT, onSwatchOut, false, 0, true);
  1339. return swatch;
  1340. }
  1341. /**
  1342. * @private (protected)
  1343. *
  1344. * @langversion 3.0
  1345. * @playerversion Flash 9.0.28.0
  1346. */
  1347. protected function addStageListener(event:Event = null):void {
  1348. stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageClick, false, 0, true);
  1349. }
  1350. /**
  1351. * @private (protected)
  1352. *
  1353. * @langversion 3.0
  1354. * @playerversion Flash 9.0.28.0
  1355. */
  1356. protected function removeStageListener(event:Event = null):void {
  1357. stage.removeEventListener(MouseEvent.MOUSE_DOWN, onStageClick, false);
  1358. }
  1359. /**
  1360. * @private (protected)
  1361. *
  1362. * @langversion 3.0
  1363. * @playerversion Flash 9.0.28.0
  1364. */
  1365. override protected function focusInHandler(event:FocusEvent):void {
  1366. super.focusInHandler(event);
  1367. setIMEMode(true);
  1368. }
  1369. /**
  1370. * @private (protected)
  1371. *
  1372. * @langversion 3.0
  1373. * @playerversion Flash 9.0.28.0
  1374. */
  1375. override protected function focusOutHandler(event:FocusEvent):void {
  1376. if (event.relatedObject == textField) {
  1377. setFocus();
  1378. return; } // New focus is our textfield.
  1379. if (isOpen) { close(); }
  1380. super.focusOutHandler(event);
  1381. setIMEMode(false);
  1382. }
  1383. /**
  1384. * @private (protected)
  1385. *
  1386. * @langversion 3.0
  1387. * @playerversion Flash 9.0.28.0
  1388. */
  1389. override protected function isOurFocus(target:DisplayObject):Boolean {
  1390. return target == textField || super.isOurFocus(target);
  1391. }
  1392. /**
  1393. * @private (protected)
  1394. *
  1395. * @langversion 3.0
  1396. * @playerversion Flash 9.0.28.0
  1397. */
  1398. override protected function keyDownHandler(event:KeyboardEvent):void {
  1399. switch (event.keyCode) {
  1400. case Keyboard.SHIFT :
  1401. case Keyboard.CONTROL :
  1402. return;
  1403. }
  1404. if (event.ctrlKey) {
  1405. switch (event.keyCode) {
  1406. case Keyboard.DOWN :
  1407. open();
  1408. break;
  1409. case Keyboard.UP :
  1410. close();
  1411. break;
  1412. }
  1413. return;
  1414. }
  1415. // Palette is closed. Just open it.
  1416. if (!isOpen) {
  1417. switch(event.keyCode) {
  1418. case Keyboard.UP:
  1419. case Keyboard.DOWN:
  1420. case Keyboard.LEFT:
  1421. case Keyboard.RIGHT:
  1422. case Keyboard.SPACE:
  1423. open();
  1424. return;
  1425. }
  1426. }
  1427. // Change the number of allowed chatacters depending on input
  1428. textField.maxChars = (event.keyCode == "#".charCodeAt(0) || textField.text.indexOf("#") > -1) ? 7 : 6;
  1429. // Palette is open. Do appropriate action.
  1430. switch (event.keyCode) {
  1431. case Keyboard.TAB :
  1432. var swatch:Sprite = findSwatch(_selectedColor);
  1433. setSwatchHighlight(swatch);
  1434. return;
  1435. break;
  1436. case Keyboard.HOME :
  1437. currColIndex = currRowIndex = 0;// Set to first color
  1438. break;
  1439. case Keyboard.END :
  1440. currColIndex = swatchMap[swatchMap.length-1].length-1;
  1441. currRowIndex = swatchMap.length-1;// Set to last color
  1442. break;
  1443. case Keyboard.PAGE_DOWN :
  1444. currRowIndex = swatchMap.length-1;// set to bottom of the column.
  1445. break;
  1446. case Keyboard.PAGE_UP :
  1447. currRowIndex = 0;// Set to top of the column.
  1448. break;
  1449. case Keyboard.ESCAPE :
  1450. if (isOpen) {
  1451. selectedColor = _selectedColor;
  1452. }
  1453. close();
  1454. return;
  1455. break;
  1456. case Keyboard.ENTER :
  1457. // This is handled in the TextKeyUp method...
  1458. return;
  1459. case Keyboard.UP :
  1460. currRowIndex = Math.max(-1, currRowIndex-1);
  1461. if (currRowIndex == -1) {
  1462. currRowIndex = swatchMap.length-1;
  1463. }
  1464. break;
  1465. case Keyboard.DOWN :
  1466. currRowIndex = Math.min(swatchMap.length, currRowIndex+1);
  1467. if (currRowIndex == swatchMap.length) {
  1468. currRowIndex = 0;
  1469. }
  1470. break;
  1471. case Keyboard.RIGHT :
  1472. currColIndex = Math.min(swatchMap[currRowIndex].length, currColIndex+1);
  1473. if (currColIndex == swatchMap[currRowIndex].length) {
  1474. currColIndex = 0;
  1475. currRowIndex = Math.min(swatchMap.length, currRowIndex+1);
  1476. if (currRowIndex == swatchMap.length) {
  1477. currRowIndex = 0;
  1478. }
  1479. }
  1480. break;
  1481. case Keyboard.LEFT :
  1482. currColIndex = Math.max(-1, currColIndex-1);
  1483. if (currColIndex == -1) {
  1484. currColIndex = swatchMap[currRowIndex].length-1;
  1485. currRowIndex = Math.max(-1, currRowIndex-1);
  1486. if (currRowIndex == -1) {
  1487. currRowIndex = swatchMap.length-1;
  1488. }
  1489. }
  1490. break;
  1491. default :
  1492. return;// Do Nothing.
  1493. }
  1494. var cTransform:ColorTransform = swatchMap[currRowIndex][currColIndex].getChildByName("color").transform.colorTransform;
  1495. rollOverColor = cTransform.color;
  1496. setColorWellColor(cTransform);
  1497. setSwatchHighlight(swatchMap[currRowIndex][currColIndex]);
  1498. setColorText(cTransform.color);
  1499. }
  1500. /**
  1501. * @private (protected)
  1502. *
  1503. * @langversion 3.0
  1504. * @playerversion Flash 9.0.28.0
  1505. */
  1506. override protected function configUI():void {
  1507. super.configUI();
  1508. tabChildren = false;
  1509. // Only create the ColorPicker default colors once in an swf.
  1510. if (ColorPicker.defaultColors == null) {
  1511. ColorPicker.defaultColors = [];
  1512. for (var i:uint=0; i<216; i++) {
  1513. ColorPicker.defaultColors.push( ((i/6%3<<0)+((i/108)<<0)*3)*0x33<<16 | i%6*0x33<<8 | (i/18<<0)%6*0x33 );
  1514. }
  1515. }
  1516. colorHash = {};
  1517. swatchMap = [];
  1518. textField = new TextField();
  1519. textField.tabEnabled = false;
  1520. //textField.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler,false,0,true);
  1521. //textField.addEventListener(KeyboardEvent.KEY_UP, onTextKeyUp, false,0,true);
  1522. swatchButton = new BaseButton();
  1523. swatchButton.focusEnabled = false;
  1524. swatchButton.useHandCursor = false;
  1525. swatchButton.autoRepeat = false;
  1526. swatchButton.setSize(25, 25);
  1527. swatchButton.addEventListener(MouseEvent.CLICK, onPopupButtonClick, false, 0, true);
  1528. addChild(swatchButton);
  1529. palette = new Sprite();
  1530. palette.tabChildren = false;
  1531. palette.cacheAsBitmap = true;
  1532. }
  1533. }
  1534. }