cp_palette_slider.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, { cp_palette_hsv } from "./cp_palette";
  9. const { ccclass, property } = cc._decorator;
  10. enum cp_palette_enum_rgba {
  11. R,
  12. G,
  13. B,
  14. A,
  15. }
  16. @ccclass
  17. export default class cp_palette_slider extends cc.Component {
  18. @property(cc.Slider)
  19. slider: cc.Slider = null;
  20. @property(cc.EditBox)
  21. edit_box: cc.EditBox = null;
  22. @property({
  23. type: cc.Enum(cp_palette_enum_rgba)
  24. })
  25. type: cp_palette_enum_rgba = cp_palette_enum_rgba.R;
  26. cp_palette: cp_palette = null;
  27. init() {
  28. }
  29. on_change_slider(slider: cc.Slider): void {
  30. if (this.edit_box) {
  31. this.edit_box.string = Math.floor((slider.progress * 255)) + "";
  32. this._update_value();
  33. }
  34. }
  35. on_change_edit_box(edit_box: cc.EditBox) {
  36. this._update_slider();
  37. this._update_value();
  38. }
  39. private _update_slider() {
  40. if (this.slider) {
  41. let value = Number(this.edit_box.string);
  42. value = Math.min(value, 255);
  43. value = Math.max(value, 0);
  44. this.slider.progress = value / 255;
  45. }
  46. }
  47. private _update_value() {
  48. let value = Number(this.edit_box.string);
  49. switch (this.type) {
  50. case cp_palette_enum_rgba.R:
  51. this.cp_palette.set_r(value);
  52. break;
  53. case cp_palette_enum_rgba.G:
  54. this.cp_palette.set_g(value);
  55. break;
  56. case cp_palette_enum_rgba.B:
  57. this.cp_palette.set_b(value);
  58. break;
  59. case cp_palette_enum_rgba.A:
  60. this.cp_palette.alpha = value;
  61. break;
  62. }
  63. this.cp_palette.update_all();
  64. }
  65. update_cursor() {
  66. let value = 0;
  67. let color = this.cp_palette.color;
  68. switch (this.type) {
  69. case cp_palette_enum_rgba.R:
  70. value = color.r;
  71. break;
  72. case cp_palette_enum_rgba.G:
  73. value = color.g;
  74. break;
  75. case cp_palette_enum_rgba.B:
  76. value = color.b;
  77. break;
  78. case cp_palette_enum_rgba.A:
  79. value = this.cp_palette.alpha;
  80. break;
  81. }
  82. this.edit_box.string = value + "";
  83. this._update_slider();
  84. }
  85. }