cp_palette_preview.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import * as cc from "cc";
  8. import cp_palette, { cp_palette_options } from "./cp_palette";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class cp_palette_preview extends cc.Component {
  12. cp_palette: cp_palette = null;
  13. @property(cc.Node)
  14. in_node: cc.Node = null;
  15. @property(cc.Node)
  16. out_node: cc.Node = null;
  17. @property(cc.EditBox)
  18. out_editor: cc.EditBox = null;
  19. init(options: cp_palette_options) {
  20. this.in_node.getComponent(cc.Sprite).color = options.color.clone();
  21. this.in_node.getComponent(cc.UIOpacity).opacity = options.alpha;
  22. }
  23. on_change_edit_box() {
  24. try {
  25. if (this.out_editor.string.length < 7) {
  26. return;
  27. }
  28. let color = new cc.Color().fromHEX(this.out_editor.string + "FF");
  29. this.cp_palette.set_color(color);
  30. this.cp_palette.update_all();
  31. } catch (error) {
  32. console.log(error.message);
  33. }
  34. }
  35. update_sprite() {
  36. this.out_node.getComponent(cc.Sprite).color = this.cp_palette.color;
  37. this.out_node.getComponent(cc.UIOpacity).opacity = this.cp_palette.alpha;
  38. this.out_editor.string = "#" + this.cp_palette.color.toHEX().toUpperCase();
  39. }
  40. }