A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

144 行
3.4KB

  1. import GlobalEvents from "/app/js/common/EventCaller.js";
  2. import Utils from "/app/js/common/Utils.js";
  3. import NESPalette from "/app/js/models/NESPalette.js";
  4. var Palettes = [];
  5. var CurrentPaletteIndex = 0;
  6. var BlockEmits = false;
  7. class CTRLPalettesStore{
  8. constructor(){}
  9. get json(){
  10. var d = {
  11. cpi: CurrentPaletteIndex,
  12. pals: []
  13. };
  14. for (let i=0; i < Palettes.length; i++){
  15. d.pals.push([Palettes[i][0], Palettes[i][1].json]);
  16. }
  17. return JSON.stringify(d);
  18. }
  19. set json(j){
  20. try {
  21. var d = JSON.parse(j);
  22. } catch (e) {
  23. throw e;
  24. }
  25. if (d.hasOwnProperty("cpi") && d.hasOwnProperty("pals")){
  26. if (Utils.isInt(d.cpi) && d.pals instanceof Array){
  27. var newPalettes = []
  28. for (let i=0; i < d.pals.length; i++){
  29. if (d.pals[i] instanceof Array){
  30. if (this.getPalette(d.pals[i][0]) === null){
  31. var palette = new NESPalette();
  32. try{
  33. palette.json = d.pals[i][1]
  34. } catch (e) {
  35. console.log("Failed to create palette.", e.toString());
  36. palette = null;
  37. }
  38. if (palette !== null){
  39. newPalettes.push([d.pals[i][0], palette]);
  40. }
  41. }
  42. }
  43. }
  44. CurrentPaletteIndex = 0
  45. if (newPalettes.length > 0){
  46. if (d.cpi >= 0 && d.cpi < newPalettes.length){
  47. CurrentPaletteIndex = d.cpi;
  48. }
  49. Palettes = newPalettes;
  50. GlobalEvents.emit("set_app_palette", Palettes[CurrentPaletteIndex][1]);
  51. }
  52. } else {
  53. throw new TypeError("JSON Property Value types invalid.");
  54. }
  55. } else {
  56. throw new TypeError("JSON missing expected properties.");
  57. }
  58. }
  59. initialize(){
  60. if (Palettes.length <= 0)
  61. this.createPalette("Palette");
  62. return this;
  63. }
  64. paletteIndexFromName(name){
  65. for (let i=1; i < Palettes.length; i++){
  66. if (Palettes[i][0] == name){
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72. getPalette(name){
  73. var i = this.paletteIndexFromName(name);
  74. return (i >= 0) ? Palettes[i][1] : null;
  75. }
  76. createPalette(name){
  77. var palette = this.getPalette(name);
  78. if (palette === null){
  79. palette = new NESPalette();
  80. palette.set_palette([
  81. "0F",
  82. "05","06","07",
  83. "09","0A","0B",
  84. "01","02","03",
  85. "0D","00","20",
  86. "15","16","17",
  87. "19","1A","1B",
  88. "11","21","31",
  89. "1D","10","30"
  90. ]);
  91. Palettes.push([name, palette]);
  92. // TODO: Create an HTML entry for this new palette.
  93. if (Palettes.length <= 1 && !BlockEmits){
  94. GlobalEvents.emit("set_app_palette", palette);
  95. }
  96. }
  97. return this;
  98. }
  99. removePalette(name){
  100. // TODO: Write this function.
  101. return this;
  102. }
  103. renamePalette(oldname, newname){
  104. var i = paletteIndexFromName(oldname);
  105. if (i < 0)
  106. throw new ValueError("Failed to find palette named '" + oldname +"'. Cannot rename.");
  107. Palettes[i][0] = newname;
  108. return this;
  109. }
  110. activatePalette(name){
  111. var i = this.paletteIndexFromName(name);
  112. if (i >= 0 && CurrentPaletteIndex !== i){
  113. CurrentPaletteIndex = i;
  114. if (!BlockEmits){
  115. GlobalEvents.emit("set_app_palette", Palettes[pindex][1]);
  116. }
  117. // TODO: Highlight palette HTML entry and unhighlight old one.
  118. }
  119. return this;
  120. }
  121. }
  122. const instance = new CTRLPalettesStore();
  123. export default instance;