not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.8 KiB

  1. function Editor(stage,xocol,doc,colors,activity,env,datastore,forcereload){
  2. if (forcereload === undefined) forcereload=false;
  3. this.radius = 22.5;
  4. this.scale = stage.canvas.width/1200;
  5. this.cxy = [stage.canvas.width/2,stage.canvas.height/2];
  6. this.xy = [stage.canvas.width/2+(120*this.scale),stage.canvas.height/2-(this.radius*this.scale)];
  7. this.dotsizeplus = this.radius*3*this.scale;
  8. this.dmin = 0;
  9. this.dmax = stage.canvas.height-(this.dotsizeplus/2.2);
  10. this.zones = [];
  11. this.dots = [];
  12. this.stage = stage;
  13. this.xo = null;
  14. this.width = stage.canvas.width;
  15. this.height = stage.canvas.height;
  16. this.env = env;
  17. this.ds = datastore;
  18. this.hexToRgb = function(hex) {
  19. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  20. return result ? {
  21. r: parseInt(result[1], 16),
  22. g: parseInt(result[2], 16),
  23. b: parseInt(result[3], 16)
  24. } : null;
  25. }
  26. this.contrast = function(rgb1, rgb2){
  27. var v1 = rgb1.r * 0.3 + rgb1.g * 0.6 + rgb1.b * 0.1;
  28. var v2 = rgb2.r * 0.3 + rgb2.g * 0.6 + rgb2.b * 0.1;
  29. return Math.abs(v2 - v1);
  30. }
  31. this.hue = function(rgb){
  32. var a = 0.5 * (2.0 * rgb.r - rgb.g - rgb.b);
  33. var b = 0.87 * (rgb.g - rgb.b);
  34. var h = Math.atan2(b, a);
  35. return h * 180 / Math.PI;
  36. }
  37. this.deltahue = function(rgb1, rgb2){
  38. h1 = this.hue(rgb1);
  39. h2 = this.hue(rgb2);
  40. return Math.abs(h2 - h1);
  41. }
  42. this.zone = function(dv, dh){
  43. var zone;
  44. if (dh < 75){
  45. zone = 0;
  46. }
  47. else if (dh > 150){
  48. zone = 1;
  49. } else{
  50. zone = 2;
  51. }
  52. if (dv > 48){
  53. zone += 1;
  54. }
  55. return zone;
  56. }
  57. this.calczones = function(self){
  58. for (var col in xocol.colors){
  59. rgb1 = this.hexToRgb(xocol.colors[col].stroke);
  60. rgb2 = this.hexToRgb(xocol.colors[col].fill);
  61. dv = this.contrast(rgb1, rgb2);
  62. dh = this.deltahue(rgb1, rgb2);
  63. this.zones.push(this.zone(dv, dh));
  64. }
  65. }
  66. this.nextdotposition = function(){
  67. var dx = this.xy[0]-this.cxy[0];
  68. var dy = this.xy[1]-this.cxy[1];
  69. var r = Math.sqrt(dx*dx+dy*dy);
  70. var c = 2*r*Math.PI;
  71. var a = Math.atan2(dy, dx);
  72. var da = (this.dotsizeplus/c)*2*Math.PI;
  73. a += da;
  74. r += this.dotsizeplus/(c/this.dotsizeplus);
  75. this.xy[0] = r*Math.cos(a)+this.cxy[0]
  76. this.xy[1] = r*Math.sin(a)+this.cxy[1]
  77. if (this.xy[1]<this.dmin||this.xy[1]>this.dmax){
  78. this.nextdotposition();
  79. }
  80. }
  81. this.saveColours = function(){
  82. var jsonparsed = this.ds.localStorage.getValue('sugar_settings');
  83. jsonparsed.colorvalue.stroke = this.xo.stroke;
  84. jsonparsed.colorvalue.fill = this.xo.fill;
  85. jsonparsed.color = this.xo.colnumber;
  86. this.ds.localStorage.setValue('sugar_settings', jsonparsed);
  87. if (jsonparsed.networkId != null && jsonparsed.server && jsonparsed.token) {
  88. // HACK: When connected to the server, should call the /api/users to update color on the server
  89. var server = jsonparsed.server.url;
  90. if (server == null) {
  91. if (document.location.protocol.substr(0,4) == "http") {
  92. var url = window.location.href;
  93. server = url.substring(0, url.indexOf('/activities'));;
  94. } else {
  95. server = "http://localhost";
  96. }
  97. }
  98. server = server + "/api/v1/users/" + jsonparsed.networkId;
  99. var request = new XMLHttpRequest();
  100. request.open("PUT",server,true);
  101. request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  102. request.setRequestHeader("x-key",jsonparsed.token.x_key);
  103. request.setRequestHeader("x-access-token",jsonparsed.token.access_token);
  104. request.send("user="+encodeURI(JSON.stringify({color: {stroke: this.xo.stroke, fill: this.xo.fill}})));
  105. }
  106. }
  107. this.stop = function(){
  108. var arr = [];
  109. var temparr = {};
  110. temparr.x = this.width;
  111. temparr.y = this.height;
  112. arr.push(temparr);
  113. for (var i in this.dots){
  114. temparr = {};
  115. temparr.fill = this.dots[i].innercol;
  116. temparr.stroke = this.dots[i].outercol;
  117. temparr.x = this.dots[i].circle.x;
  118. temparr.y = this.dots[i].circle.y;
  119. temparr.num = this.dots[i].number;
  120. arr.push(temparr);
  121. }
  122. var js = JSON.stringify(arr);
  123. activity.getDatastoreObject().setDataAsText(js);
  124. activity.getDatastoreObject().save(function (error) {
  125. if (error === null) {
  126. console.log("write done.");
  127. }
  128. else {
  129. console.log("write failed.");
  130. }
  131. });
  132. }
  133. this.init = function(){
  134. if (forcereload==true){
  135. this.init_getsettings(false,[]);
  136. } else {
  137. activity.getDatastoreObject().getMetadata(this.init_canaccessdatastore.bind(this));
  138. }
  139. }
  140. this.init_canaccessdatastore = function(error,mdata){
  141. var d = new Date().getTime();
  142. if (Math.abs(d-mdata.creation_time)<2000){
  143. this.init_getsettings(false,[]);
  144. } else {
  145. activity.getDatastoreObject().loadAsText(this.init_getdatastore.bind(this));
  146. }
  147. }
  148. this.init_getdatastore = function(error,metadata,data){
  149. if (error==null&&data!=null){
  150. data = JSON.parse(data);
  151. this.init_getsettings(true,data);
  152. } else {
  153. this.init_getsettings(false,[]);
  154. }
  155. }
  156. this.init_getsettings = function(isdata,data) {
  157. this.ds.localStorage.load(function() {
  158. var preferences = this.ds.localStorage.getValue('sugar_settings');
  159. this.init_activity(isdata,data,preferences);
  160. }.bind(this));
  161. }
  162. this.init_activity = function(isdata,data,settings){
  163. this.dots = [];
  164. this.calczones();
  165. var cnum = settings;
  166. var xo = new XOMan(colors.fill,colors.stroke,this,cnum.color);
  167. xo.init();
  168. this.xo = xo;
  169. if (isdata==false) {
  170. for (var z = 0; z<4; z++){
  171. for (var i in xocol.colors){
  172. if (this.zones[i]==z){
  173. var c = new ColourCircle(xocol.colors[i].fill,xocol.colors[i].stroke,this.xy[0]+15,this.xy[1],stage,this.xo,i);
  174. c.init();
  175. this.dots.push(c);
  176. this.nextdotposition();
  177. }
  178. }
  179. }
  180. } else {
  181. var scalex = this.width/data[0].x;
  182. var scaley = this.height/data[0].y;
  183. for (var i = 1; i<data.length; i++){
  184. var c = new ColourCircle(data[i].fill,data[i].stroke,data[i].x*scalex,data[i].y*scaley,stage,this.xo,data[i].num);
  185. c.init();
  186. this.dots.push(c);
  187. }
  188. }
  189. }
  190. }