From bf071043f4f9194a46554f3b5dc75f49e2e1e130 Mon Sep 17 00:00:00 2001 From: Michael Aschauer Date: Thu, 15 Nov 2018 22:02:43 +0100 Subject: [PATCH] add color settings --- stitchcode/gui.js | 84 ++++++++++++++++++++++++++++++++++-- stitchcode/objects.js | 71 +++++++++++++++++++++++------- stitchcode/turtleShepherd.js | 57 ++++++++++++++++++++---- 3 files changed, 184 insertions(+), 28 deletions(-) diff --git a/stitchcode/gui.js b/stitchcode/gui.js index 74885171..8bf98a31 100644 --- a/stitchcode/gui.js +++ b/stitchcode/gui.js @@ -154,6 +154,8 @@ IDE_Morph.prototype.applySavedTurtleStitchSettings = function () { hidestitches = this.getSetting('hidestitches'); warnings = this.getSetting('ignoreWarning'); isImperial = this.getSetting('isImperial'); + backgroundColor = this.getSetting('backgroundColor') + defaultPenColor = this.getSetting('defaultPenColor') console.log("apply settings"); @@ -201,6 +203,18 @@ IDE_Morph.prototype.applySavedTurtleStitchSettings = function () { this.stage.turtleShepherd.sMetric = true; } + if (backgroundColor) { + this.stage.renderer.setBackgroundColorHex(backgroundColor); + } else { + this.stage.renderer.setBackgroundColorHex('#fffffff'); + } + + if (defaultPenColor) { + this.stage.renderer.setDefaultPenColorHex(defaultPenColor); + this.currentSprite.setColor(StageMorph.prototype.defaultPenColor); + } else { + this.stage.renderer.setDefaultPenColorHex('#000000'); + } } @@ -830,8 +844,7 @@ IDE_Morph.prototype.turtlestitchMenu = function () { }, !stage.turtleShepherd.isMetric() , 'uncheck to display dimensions in millimeters', - 'check to show dimensions in inch' - ); + 'check to show dimensions in inch', ); menu.addLine(); addPreference( @@ -903,14 +916,21 @@ IDE_Morph.prototype.turtlestitchMenu = function () { } }, StageMorph.prototype.ignoreWarnings , - 'uncheck to show embroidery specific warnings', - 'check to ignore embroidery specific warnings', + + 'uncheck to show turtle', + 'check to hide turtle', ); + menu.addLine(); + menu.addItem('default background color...', 'userSetBackgroundColor'); + menu.addItem('default pen color...', 'userSetPenColor'); + menu.popup(world, pos); }; + + IDE_Morph.prototype.toggleAppMode = function (appMode) { var world = this.world(), elements = [ @@ -2723,3 +2743,59 @@ ProjectDialogMorph.prototype.saveProject = function () { this.saveProjectOrig(); }; + +StageMorph.prototype.backgroundColor = new Color(255,255,255); +StageMorph.prototype.defaultPenColor = new Color(0,0,0,1); + +IDE_Morph.prototype.userSetBackgroundColor = function () { + new DialogBoxMorph( + this, + function (value) { + this.stage.renderer.setBackgroundColorHex(value); + if (value != '#ffffff') + this.saveSetting('backgroundColor', value); + else { + this.removeSetting('backgroundColor'); + } + }, + this + ).prompt( + "Default background color", + new String("#" + ((1 << 24) + + (Math.round(StageMorph.prototype.backgroundColor.r) << 16) + + (Math.round(StageMorph.prototype.backgroundColor.g) << 8) + + Math.round(StageMorph.prototype.backgroundColor.b)).toString(16).slice(1)), + this.world(), + null, // pic + null, // choices + null, // read only + false // numeric + ); +}; + +IDE_Morph.prototype.userSetPenColor = function () { + new DialogBoxMorph( + this, + function (value) { + this.stage.renderer.setDefaultPenColorHex(value); + this.currentSprite.setColor(StageMorph.prototype.defaultPenColor); + if (value != '#000000') + this.saveSetting('defaultPenColor', value); + else { + this.removeSetting('defaultPenColor'); + } + }, + this + ).prompt( + "Default pen color", + new String("#" + ((1 << 24) + + (Math.round(StageMorph.prototype.defaultPenColor.r) << 16) + + (Math.round(StageMorph.prototype.defaultPenColor.g) << 8) + + Math.round(StageMorph.prototype.defaultPenColor.b)).toString(16).slice(1)), + this.world(), + null, // pic + null, // choices + null, // read only + false // numeric + ); +}; diff --git a/stitchcode/objects.js b/stitchcode/objects.js index 0fe561f6..4dc55867 100644 --- a/stitchcode/objects.js +++ b/stitchcode/objects.js @@ -40,7 +40,7 @@ SpriteMorph.prototype.init = function(globals) { this.turtle = null; this.isDown = true; this.cache = new Cache; - this.color = new Color(0,0,0,1); + this.color = StageMorph.prototype.defaultPenColor; this.stitchtype = 0; this.isRunning = false; this.stitchoptions = {}; @@ -1260,14 +1260,13 @@ SpriteMorph.prototype.resetAll = function () { myself.stitchtype = 0; myself.stitchoptions = {}; myself.isRunning = false; - myself.setColor(new Color(0, 0, 0, 1.0)); + myself.setColor(StageMorph.prototype.defaultPenColor); myself.gotoXY(0,0); myself.setHeading(90); myself.clear(); myself.isDown = true; } - SpriteMorph.prototype.resetStitchSettings = function () { var myself = this; myself.stitchoptions = {} @@ -2225,6 +2224,10 @@ StageMorph.prototype.init = function (globals) { var myself = this; console.log("init stage"); + this.turtleShepherd = new TurtleShepherd(); + this.turtleShepherd.ignoreWarning = StageMorph.prototype.ignoreWarnings; + this.turtleShepherd.setDefaultColor(StageMorph.prototype.defaultPenColor); + this.originalInit(globals); this.initScene(); this.initRenderer(); @@ -2252,8 +2255,6 @@ StageMorph.prototype.init = function (globals) { }); } - this.turtleShepherd = new TurtleShepherd(); - this.turtleShepherd.ignoreWarning = StageMorph.prototype.ignoreWarnings; this.scene.grid.draw(); this.myObjects = new THREE.Object3D(); this.myStitchPoints = new THREE.Object3D(); @@ -2386,7 +2387,7 @@ StageMorph.prototype.clearAll = function () { StageMorph.prototype.initRenderer = function () { var myself = this; - console.log("set up renderer"); + console.log("set up renderer"); if(!this.renderer) { if (Detector.webgl) { @@ -2405,13 +2406,17 @@ StageMorph.prototype.initRenderer = function () { {canvas: this.penTrails()}); } - /* console.log(myself); - this.renderer.setBackgroundColor( - myself.parentThatIsA(IDE_Morph).defaultStageColor - ); -*/ - this.renderer.setClearColor(0xffffff, 1); + this.renderer.setBackgroundColor = function(color) { + StageMorph.prototype.backgroundColor = color; + myself.turtleShepherd.setBackgroundColor(color); + myself.renderer.setClearColor( + new THREE.Color("rgb("+color.r + "," + color.g + "," + color.b + ")"), + 1); + myself.reRender(); + } + + this.renderer.setBackgroundColor(StageMorph.prototype.backgroundColor); this.renderer.changed = false; this.renderer.showingAxes = true; @@ -2440,10 +2445,44 @@ StageMorph.prototype.initRenderer = function () { myself.reRender(); }; - this.renderer.setBackgroundColor = function(color) { - this.renderer.setClearColor( - new THREE.Color("rgb("+c.r + "," + c.g + "," + c.b + ")"), - 1); + + this.renderer.setBackgroundColorHex = function(hex) { + // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") + var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; + + hex = hex.replace(shorthandRegex, function(m, r, g, b) { + return r + r + g + g + b + b; + }); + + var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + if (result) { + r = parseInt(result[1], 16); + g = parseInt(result[2], 16); + b = parseInt(result[3], 16); + StageMorph.prototype.backgroundColor = new Color(r, g, b); + myself.turtleShepherd.setBackgroundColor(StageMorph.prototype.backgroundColor); + myself.renderer.setBackgroundColor(StageMorph.prototype.backgroundColor); + } + myself.reRender(); + } + + this.renderer.setDefaultPenColorHex = function(hex) { + // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") + var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; + + hex = hex.replace(shorthandRegex, function(m, r, g, b) { + return r + r + g + g + b + b; + }); + + var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + if (result) { + r = parseInt(result[1], 16); + g = parseInt(result[2], 16); + b = parseInt(result[3], 16); + StageMorph.prototype.defaultPenColor = new Color(r, g, b); + myself.turtleShepherd.setDefaultColor(StageMorph.prototype.defaultPenColor); + } + myself.reRender(); } }; diff --git a/stitchcode/turtleShepherd.js b/stitchcode/turtleShepherd.js index 6de5f522..e2b2324f 100644 --- a/stitchcode/turtleShepherd.js +++ b/stitchcode/turtleShepherd.js @@ -24,6 +24,8 @@ TurtleShepherd.prototype.init = function() { this.densityMax = 15; this.ignoreColors = false; this.ignoreWarning = false; + this.backgroundColor = {r:0,g:0,b:0,a:1}; + this.defaultColor = {r:0,g:0,b:0,a:1}; }; TurtleShepherd.prototype.clear = function() { @@ -50,6 +52,7 @@ TurtleShepherd.prototype.clear = function() { this.oldColor = 0; this.penSize = 1; this.newPenSize = 0; + }; TurtleShepherd.prototype.toggleMetric = function() { @@ -164,7 +167,7 @@ TurtleShepherd.prototype.moveTo= function(x1, y1, x2, y2, penState) { this.colors.push(this.newColor); this.newColor = false; } else { - this.colors.push({r:0,g:0,b:0,a:1}); + this.colors.push(this.defaultColor); } this.oldColor = this.colors[this.colors.length-1]; } @@ -230,6 +233,45 @@ TurtleShepherd.prototype.moveTo= function(x1, y1, x2, y2, penState) { this.lastY = y2; }; +TurtleShepherd.prototype.setDefaultColor= function(color) { + var c = { + r: Math.round(color.r), + g: Math.round(color.g), + b: Math.round(color.b), + a: color.a + }; + this.defaultColor = c; +}; + +TurtleShepherd.prototype.getDefaultColorAsHex = function (){ + return new String( + "#" + ( + (1 << 24) + + (Math.round(this.defaultColor.r) << 16) + + (Math.round(this.defaultColor.g) << 8) + + Math.round(this.defaultColor.b) + ).toString(16).slice(1)); +}; + +TurtleShepherd.prototype.setBackgroundColor= function(color) { + var c = { + r: Math.round(color.r), + g: Math.round(color.g), + b: Math.round(color.b), + a: color.a + }; + this.backgroundColor = c; +}; + +TurtleShepherd.prototype.getBackgroundColorAsHex = function (){ + return new String( + "#" + ( + (1 << 24) + + (Math.round(this.backgroundColor.r) << 16) + + (Math.round(this.backgroundColor.g) << 8) + + Math.round(this.backgroundColor.b) + ).toString(16).slice(1)); +} TurtleShepherd.prototype.addColorChange= function(color) { var c = { @@ -239,7 +281,6 @@ TurtleShepherd.prototype.addColorChange= function(color) { a: color.a }; this.newColor = c; - }; TurtleShepherd.prototype.pushColorChangeNow = function() { @@ -307,7 +348,7 @@ TurtleShepherd.prototype.toSVG = function() { var svgStr = "\n"; svgStr += "\n"; svgStr += '