cp_palette.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import * as cc from "cc";
  8. import cp_palette_color_alpha from "./cp_palette_color_alpha";
  9. import cp_palette_color_hue from "./cp_palette_color_hue";
  10. import cp_palette_color_main from "./cp_palette_color_main";
  11. import cp_palette_preview from "./cp_palette_preview";
  12. import cp_palette_slider from "./cp_palette_slider";
  13. import cp_palette_util from "./cp_palette_util";
  14. const { ccclass, property } = cc._decorator;
  15. export interface cp_palette_hsv {
  16. h: number;
  17. s: number;
  18. v: number;
  19. }
  20. export interface cp_palette_options {
  21. color: cc.Color;
  22. alpha: number;
  23. update_listener: (color: cc.Color, alpha: number) => void;
  24. }
  25. @ccclass
  26. export default class cp_palette extends cc.Component {
  27. @property(cp_palette_color_hue)
  28. cp_palette_color_hue: cp_palette_color_hue = null;
  29. @property(cp_palette_color_main)
  30. cp_palette_color_main: cp_palette_color_main = null;
  31. @property(cp_palette_color_alpha)
  32. cp_palette_color_alpha: cp_palette_color_alpha = null;
  33. @property(cp_palette_preview)
  34. cp_palette_preview: cp_palette_preview = null;
  35. private options: cp_palette_options;
  36. density: number = 32;
  37. color: cc.Color = cc.Color.GRAY;
  38. alpha: number = 255;
  39. onLoad() {
  40. this.cp_palette_color_hue.cp_palette = this;
  41. this.cp_palette_color_main.cp_palette = this;
  42. this.cp_palette_color_alpha.cp_palette = this;
  43. this.cp_palette_preview.cp_palette = this;
  44. this.update_all();
  45. this.show({
  46. color: cc.Color.RED,
  47. alpha: 255,
  48. update_listener: () => { }
  49. });
  50. }
  51. show(options: cp_palette_options) {
  52. this.options = options;
  53. this.color = options.color.clone();
  54. this.alpha = options.alpha;
  55. this.node.active = true;
  56. this.cp_palette_preview.init(options);
  57. this.update_all();
  58. }
  59. hide() {
  60. this.node.active = false;
  61. this.options = null;
  62. }
  63. on_click_close() {
  64. this.hide();
  65. }
  66. // ======== get set
  67. get_hsv() {
  68. return this.color.toHSV();
  69. }
  70. set_color(color: cc.Color) {
  71. this.color.r = color.r;
  72. this.color.g = color.g;
  73. this.color.b = color.b;
  74. }
  75. set_hsv(h: number, s: number, v: number) {
  76. const color = new cc.Color().fromHSV(h, s, v);
  77. this.set_color(color);
  78. }
  79. set_r(v: number) {
  80. this.color.r = v;
  81. }
  82. set_g(v: number) {
  83. this.color.g = v;
  84. }
  85. set_b(v: number) {
  86. this.color.b = v;
  87. }
  88. // ======== update
  89. update_listener() {
  90. if (this.options) {
  91. this.options.update_listener(this.color.clone(), this.alpha);
  92. }
  93. }
  94. update_all() {
  95. this.update_color_hub();
  96. this.update_color_main();
  97. this.update_color_alpha();
  98. this.update_slider_rgba();
  99. this.update_preview();
  100. this.update_listener();
  101. }
  102. update_render_from_main() {
  103. this.update_slider_rgba();
  104. this.update_preview();
  105. this.update_listener();
  106. }
  107. private update_color_hub() {
  108. this.cp_palette_color_hue.update_sprite();
  109. this.cp_palette_color_hue.update_cursor();
  110. }
  111. private update_color_main() {
  112. this.cp_palette_color_main.update_sprite();
  113. this.cp_palette_color_main.update_cursor();
  114. }
  115. private update_color_alpha() {
  116. this.cp_palette_color_alpha.update_sprite();
  117. this.cp_palette_color_alpha.update_cursor();
  118. }
  119. private update_slider_rgba() {
  120. let comps = this.node.getComponentsInChildren(cp_palette_slider);
  121. for (let value of comps) {
  122. value.cp_palette = this;
  123. value.update_cursor();
  124. }
  125. }
  126. private update_preview() {
  127. this.cp_palette_preview.update_sprite();
  128. }
  129. }