|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.FieldAngle');
|
|
|
|
goog.require('Blockly.DropDownDiv');
|
|
goog.require('Blockly.FieldTextInput');
|
|
goog.require('goog.math');
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle = function(opt_value, opt_validator) {
|
|
|
|
this.symbol_ = Blockly.utils.createSvgElement('tspan', {}, null);
|
|
this.symbol_.appendChild(document.createTextNode('\u00B0'));
|
|
|
|
var numRestrictor = new RegExp("[\\d]|[\\.]|[-]|[eE]");
|
|
|
|
opt_value = (opt_value && !isNaN(opt_value)) ? String(opt_value) : '0';
|
|
Blockly.FieldAngle.superClass_.constructor.call(
|
|
this, opt_value, opt_validator, numRestrictor);
|
|
this.addArgType('angle');
|
|
};
|
|
goog.inherits(Blockly.FieldAngle, Blockly.FieldTextInput);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.fromJson = function(options) {
|
|
return new Blockly.FieldAngle(options['angle']);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.ROUND = 15;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.HALF = 120 / 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.CLOCKWISE = true;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.OFFSET = 90;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.WRAP = 180;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.HANDLE_RADIUS = 10;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.ARROW_WIDTH = Blockly.FieldAngle.HANDLE_RADIUS;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.HANDLE_GLOW_WIDTH = 3;
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.RADIUS = Blockly.FieldAngle.HALF
|
|
- Blockly.FieldAngle.HANDLE_RADIUS - Blockly.FieldAngle.HANDLE_GLOW_WIDTH;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.CENTER_RADIUS = 2;
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.ARROW_SVG_PATH = 'icons/arrow.svg';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.dispose_ = function() {
|
|
var thisField = this;
|
|
return function() {
|
|
Blockly.FieldAngle.superClass_.dispose_.call(thisField)();
|
|
thisField.gauge_ = null;
|
|
if (thisField.mouseDownWrapper_) {
|
|
Blockly.unbindEvent_(thisField.mouseDownWrapper_);
|
|
}
|
|
if (thisField.mouseUpWrapper_) {
|
|
Blockly.unbindEvent_(thisField.mouseUpWrapper_);
|
|
}
|
|
if (thisField.mouseMoveWrapper_) {
|
|
Blockly.unbindEvent_(thisField.mouseMoveWrapper_);
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.showEditor_ = function() {
|
|
|
|
Blockly.FieldAngle.superClass_.showEditor_.call(this, this.useTouchInteraction_);
|
|
|
|
Blockly.DropDownDiv.hideWithoutAnimation();
|
|
Blockly.DropDownDiv.clearContent();
|
|
var div = Blockly.DropDownDiv.getContentDiv();
|
|
|
|
const srcBlock = this.sourceBlock_.parentBlock_;
|
|
var svg = Blockly.utils.createSvgElement('svg', {
|
|
'xmlns': 'http://www.w3.org/2000/svg',
|
|
'xmlns:html': 'http://www.w3.org/1999/xhtml',
|
|
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
|
|
'version': '1.1',
|
|
'height': (Blockly.FieldAngle.HALF * 2) + 'px',
|
|
'width': (Blockly.FieldAngle.HALF * 2) + 'px'
|
|
}, div);
|
|
Blockly.utils.createSvgElement('circle', {
|
|
'cx': Blockly.FieldAngle.HALF, 'cy': Blockly.FieldAngle.HALF,
|
|
'r': Blockly.FieldAngle.RADIUS,
|
|
'fill': srcBlock.getColourSecondary(),
|
|
'stroke': srcBlock.getColourTertiary(),
|
|
'class': 'blocklyAngleCircle'
|
|
}, svg);
|
|
this.gauge_ = Blockly.utils.createSvgElement('path',
|
|
{'class': 'blocklyAngleGauge'}, svg);
|
|
|
|
this.line_ = Blockly.utils.createSvgElement('line',{
|
|
'x1': Blockly.FieldAngle.HALF,
|
|
'y1': Blockly.FieldAngle.HALF,
|
|
'class': 'blocklyAngleLine'
|
|
}, svg);
|
|
|
|
var offsetRadians = Math.PI * Blockly.FieldAngle.OFFSET / 180;
|
|
Blockly.utils.createSvgElement('line', {
|
|
'x1': Blockly.FieldAngle.HALF,
|
|
'y1': Blockly.FieldAngle.HALF,
|
|
'x2': Blockly.FieldAngle.HALF + Blockly.FieldAngle.RADIUS * Math.cos(offsetRadians),
|
|
'y2': Blockly.FieldAngle.HALF - Blockly.FieldAngle.RADIUS * Math.sin(offsetRadians),
|
|
'class': 'blocklyAngleLine'
|
|
}, svg);
|
|
|
|
for (var angle = 0; angle < 360; angle += 15) {
|
|
Blockly.utils.createSvgElement('line', {
|
|
'x1': Blockly.FieldAngle.HALF + Blockly.FieldAngle.RADIUS - 13,
|
|
'y1': Blockly.FieldAngle.HALF,
|
|
'x2': Blockly.FieldAngle.HALF + Blockly.FieldAngle.RADIUS - 7,
|
|
'y2': Blockly.FieldAngle.HALF,
|
|
'class': 'blocklyAngleMarks',
|
|
'transform': 'rotate(' + angle + ',' +
|
|
Blockly.FieldAngle.HALF + ',' + Blockly.FieldAngle.HALF + ')'
|
|
}, svg);
|
|
}
|
|
|
|
Blockly.utils.createSvgElement('circle', {
|
|
'cx': Blockly.FieldAngle.HALF, 'cy': Blockly.FieldAngle.HALF,
|
|
'r': Blockly.FieldAngle.CENTER_RADIUS,
|
|
'class': 'blocklyAngleCenterPoint'
|
|
}, svg);
|
|
|
|
this.handle_ = Blockly.utils.createSvgElement('g', {}, svg);
|
|
Blockly.utils.createSvgElement('circle', {
|
|
'cx': 0,
|
|
'cy': 0,
|
|
'r': Blockly.FieldAngle.HANDLE_RADIUS,
|
|
'class': 'blocklyAngleDragHandle'
|
|
}, this.handle_);
|
|
this.arrowSvg_ = Blockly.utils.createSvgElement('image',
|
|
{
|
|
'width': Blockly.FieldAngle.ARROW_WIDTH,
|
|
'height': Blockly.FieldAngle.ARROW_WIDTH,
|
|
'x': -Blockly.FieldAngle.ARROW_WIDTH / 2,
|
|
'y': -Blockly.FieldAngle.ARROW_WIDTH / 2,
|
|
'class': 'blocklyAngleDragArrow'
|
|
},
|
|
this.handle_);
|
|
this.arrowSvg_.setAttributeNS(
|
|
'http://www.w3.org/1999/xlink',
|
|
'xlink:href',
|
|
Blockly.mainWorkspace.options.pathToMedia + Blockly.FieldAngle.ARROW_SVG_PATH
|
|
);
|
|
const blockHSL = goog.color.hexToHsl(srcBlock.getColour());
|
|
|
|
this.arrowSvg_.setAttribute("filter", `hue-rotate(${-214.86 + blockHSL[0]}deg) saturate(${blockHSL[1]}) brightness(${blockHSL[2] * 1.8})`);
|
|
|
|
Blockly.DropDownDiv.setColour(srcBlock.getColour(), this.sourceBlock_.getColourTertiary());
|
|
Blockly.DropDownDiv.setCategory(srcBlock.getCategory());
|
|
Blockly.DropDownDiv.showPositionedByBlock(this, this.sourceBlock_);
|
|
|
|
this.mouseDownWrapper_ =
|
|
Blockly.bindEvent_(this.handle_, 'mousedown', this, this.onMouseDown);
|
|
|
|
this.updateGraph_();
|
|
};
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.onMouseDown = function() {
|
|
this.mouseMoveWrapper_ = Blockly.bindEvent_(document.body, 'mousemove', this, this.onMouseMove);
|
|
this.mouseUpWrapper_ = Blockly.bindEvent_(document.body, 'mouseup', this, this.onMouseUp);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.onMouseUp = function() {
|
|
Blockly.unbindEvent_(this.mouseMoveWrapper_);
|
|
Blockly.unbindEvent_(this.mouseUpWrapper_);
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.onMouseMove = function(e) {
|
|
e.preventDefault();
|
|
var bBox = this.gauge_.ownerSVGElement.getBoundingClientRect();
|
|
var dx = e.clientX - bBox.left - Blockly.FieldAngle.HALF;
|
|
var dy = e.clientY - bBox.top - Blockly.FieldAngle.HALF;
|
|
var angle = Math.atan(-dy / dx);
|
|
if (isNaN(angle)) {
|
|
|
|
return;
|
|
}
|
|
angle = goog.math.toDegrees(angle);
|
|
|
|
if (dx < 0) {
|
|
angle += 180;
|
|
} else if (dy > 0) {
|
|
angle += 360;
|
|
}
|
|
if (Blockly.FieldAngle.CLOCKWISE) {
|
|
angle = Blockly.FieldAngle.OFFSET + 360 - angle;
|
|
} else {
|
|
angle -= Blockly.FieldAngle.OFFSET;
|
|
}
|
|
if (Blockly.FieldAngle.ROUND) {
|
|
angle = Math.round(angle / Blockly.FieldAngle.ROUND) *
|
|
Blockly.FieldAngle.ROUND;
|
|
}
|
|
angle = this.callValidator(angle);
|
|
Blockly.FieldTextInput.htmlInput_.value = angle;
|
|
this.setValue(angle);
|
|
this.validate_();
|
|
this.resizeEditor_();
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.setText = function(text) {
|
|
Blockly.FieldAngle.superClass_.setText.call(this, text);
|
|
if (!this.textElement_) {
|
|
|
|
return;
|
|
}
|
|
this.updateGraph_();
|
|
|
|
this.size_.width = 0;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.updateGraph_ = function() {
|
|
if (!this.gauge_) {
|
|
return;
|
|
}
|
|
var angleDegrees = Number(this.getText()) % 360 + Blockly.FieldAngle.OFFSET;
|
|
var angleRadians = goog.math.toRadians(angleDegrees);
|
|
var path = ['M ', Blockly.FieldAngle.HALF, ',', Blockly.FieldAngle.HALF];
|
|
var x2 = Blockly.FieldAngle.HALF;
|
|
var y2 = Blockly.FieldAngle.HALF;
|
|
if (!isNaN(angleRadians)) {
|
|
var angle1 = goog.math.toRadians(Blockly.FieldAngle.OFFSET);
|
|
var x1 = Math.cos(angle1) * Blockly.FieldAngle.RADIUS;
|
|
var y1 = Math.sin(angle1) * -Blockly.FieldAngle.RADIUS;
|
|
if (Blockly.FieldAngle.CLOCKWISE) {
|
|
angleRadians = 2 * angle1 - angleRadians;
|
|
}
|
|
x2 += Math.cos(angleRadians) * Blockly.FieldAngle.RADIUS;
|
|
y2 -= Math.sin(angleRadians) * Blockly.FieldAngle.RADIUS;
|
|
|
|
var largeFlag = Math.abs(angleDegrees - Blockly.FieldAngle.OFFSET) > 180 ? 1 : 0;
|
|
var sweepFlag = Number(Blockly.FieldAngle.CLOCKWISE);
|
|
if (angleDegrees < Blockly.FieldAngle.OFFSET) {
|
|
sweepFlag = 1 - sweepFlag;
|
|
}
|
|
path.push(' l ', x1, ',', y1,
|
|
' A ', Blockly.FieldAngle.RADIUS, ',', Blockly.FieldAngle.RADIUS,
|
|
' 0 ', largeFlag, ' ', sweepFlag, ' ', x2, ',', y2, ' z');
|
|
|
|
|
|
if (Blockly.FieldAngle.CLOCKWISE) {
|
|
var imageRotation = angleDegrees + 2 * Blockly.FieldAngle.OFFSET;
|
|
} else {
|
|
var imageRotation = -angleDegrees;
|
|
}
|
|
this.arrowSvg_.setAttribute('transform', 'rotate(' + (imageRotation) + ')');
|
|
}
|
|
this.gauge_.setAttribute('d', path.join(''));
|
|
this.line_.setAttribute('x2', x2);
|
|
this.line_.setAttribute('y2', y2);
|
|
this.handle_.setAttribute('transform', 'translate(' + x2 + ',' + y2 + ')');
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Blockly.FieldAngle.prototype.classValidator = function(text) {
|
|
if (text === null) {
|
|
return null;
|
|
}
|
|
var n = parseFloat(text || 0);
|
|
if (isNaN(n)) {
|
|
return null;
|
|
}
|
|
n = n % 360;
|
|
if (n < 0) {
|
|
n += 360;
|
|
}
|
|
if (n > Blockly.FieldAngle.WRAP) {
|
|
n -= 360;
|
|
}
|
|
return String(n);
|
|
};
|
|
|
|
Blockly.Field.register('field_angle', Blockly.FieldAngle);
|
|
|