Skip to content
Snippets Groups Projects
Commit 351db895 authored by hertzhaft's avatar hertzhaft
Browse files

recalculate values when units have changed or a shape was clicked

parent 8e8f201c
Branches
Tags
No related merge requests found
...@@ -704,22 +704,22 @@ ...@@ -704,22 +704,22 @@
// implemented measuring shape types, for select widget // implemented measuring shape types, for select widget
implementedShapes : ['Line', 'LineString', 'Rectangle', 'Polygon'], implementedShapes : ['Line', 'LineString', 'Rectangle', 'Polygon'],
// all measuring shape types // all measuring shape types
shapeTypes : { shapeInfo : {
Line : { name : 'line' }, Line : { name : 'line', display : 'length', },
LineString : { name : 'linestring' }, LineString : { name : 'linestring', display : 'length' },
Rectangle : { name : 'rectangle' }, Rectangle : { name : 'rectangle', display : 'area' },
Square : { name : 'square' }, Square : { name : 'square', display : 'length' },
Polygon : { name : 'polygon' }, Polygon : { name : 'polygon', display : 'area' },
Circle : { name : 'circle' }, Circle : { name : 'circle', display : 'radius' },
Arch : { name : 'arch' }, Arch : { name : 'arch', display : 'radius' },
Ratio : { name : 'ratio' }, Ratio : { name : 'ratio', display : 'ratio' },
InterCol : { name : 'intercolumnium' }, Grid : { name : 'linegrid', display : 'spacing' },
Grid : { name : 'linegrid' } InterCol : { name : 'intercolumnium', display : 'ratio' }
}, },
// most recently selected shape // most recently selected shape
activeShapeType : 'Line', activeShapeType : 'Line',
// last measured distance // last measured distance
lastMeasuredDistance : 0, lastMeasuredValue : 0,
// measuring unit (index into unit list) // measuring unit (index into unit list)
unitFrom : 17, unitFrom : 17,
// converted unit (index into unit list) // converted unit (index into unit list)
...@@ -806,12 +806,12 @@ ...@@ -806,12 +806,12 @@
// event handler for renderShape // event handler for renderShape
var onRenderShape = function(event, shape) { var onRenderShape = function(event, shape) {
// event handler for updating shape info // event handler for updating shape info
var shapeInfo = function(event) { var info = function(event) {
updateInfo(data, shape); updateInfo(data, shape);
}; };
var data = this; var data = this;
var $elem = shape.$elem; var $elem = shape.$elem;
$elem.on('click.measure', shapeInfo); $elem.on('click.measure', info);
console.debug('measure: onRenderShape', data, shape); console.debug('measure: onRenderShape', data, shape);
}; };
...@@ -850,92 +850,61 @@ ...@@ -850,92 +850,61 @@
return Math.abs(area/2); return Math.abs(area/2);
}; };
// convert length to second unit
var convertLength = function(data, val) {
var widgets = data.measureWidgets;
var u1 = parseFloat(widgets.unit1.val());
var u2 = parseFloat(widgets.unit2.val());
return val * u1 / u2;
};
// convert area to second unit
var convertArea = function(data, val) {
var widgets = data.measureWidgets;
var u1 = parseFloat(widgets.unit1.val());
var u2 = parseFloat(widgets.unit2.val());
return val * u1 * u1 / (u2 * u2);
};
// recalculate factor after entering a new value in input element "value1" // recalculate factor after entering a new value in input element "value1"
var changeFactor = function(data) { var changeFactor = function(data) {
var widgets = data.measureWidgets; var widgets = data.measureWidgets;
var val = parseFloat(widgets.value1.val()); var val = parseFloat(widgets.value1.val());
var dist = data.lastMeasuredDistance; var fac = val / data.lastMeasuredValue;
var fac = val / dist;
data.measureFactor = fac; data.measureFactor = fac;
// widgets.fac.text(fn.cropFloatStr(fac)); updateCalculation(data);
var conv = convertLength(data, val); };
widgets.value2.text(fn.cropFloatStr(mRound(conv)));
};
// info window for line // convert to second unit and display
var infoLine = function(data, shape) { var updateMeasures = function(data, val, type) {
var dist = rectifiedDist(data, shape); var info = data.settings.shapeInfo[type]
var fac = data.measureFactor;
var val = dist * fac;
var conv = convertLength(data, val);
var widgets = data.measureWidgets; var widgets = data.measureWidgets;
var display = info.display;
var u1 = parseFloat(widgets.unit1.val());
var u2 = parseFloat(widgets.unit2.val());
var ratio = u1 / u2;
var result = (display === 'area')
? val * ratio * ratio
: val * ratio;
widgets.shape.val(type);
widgets.value1.val(fn.cropFloatStr(mRound(val))); widgets.value1.val(fn.cropFloatStr(mRound(val)));
widgets.value2.text(fn.cropFloatStr(mRound(conv))); widgets.value2.text(fn.cropFloatStr(mRound(result)));
widgets.info.text('length'); widgets.info.text(display);
data.lastMeasuredDistance = dist;
}; };
// info window for polygon // recalculate with new units
var infoPolygon = function(data, shape) { var updateCalculation = function(data) {
var area = rectifiedArea(data, shape); var type = getActiveShapeType(data);
var display = data.settings.shapeInfo[type].display;
var val = data.lastMeasuredValue;
var fac = data.measureFactor; var fac = data.measureFactor;
var val = area * fac * fac; var result = (display === 'area')
var conv = convertArea(data, val); ? val * fac * fac
var widgets = data.measureWidgets; : val * fac;
widgets.value1.val(fn.cropFloatStr(mRound(val))); updateMeasures(data, result, type);
widgets.value2.text(fn.cropFloatStr(mRound(conv)));
widgets.info.text('area');
};
// info window for rectangle
var infoRect = function(data, shape) {
var widgets = data.measureWidgets;
widgets.value1.val('rect 1');
widgets.value2.text('rect 2');
}; };
// recalculate after measuring // display info for shape
var updateInfo = function(data, shape) { var updateInfo = function(data, shape) {
var type = shape.geometry.type; var type = shape.geometry.type;
data.measureWidgets.shape.val(type); var display = data.settings.shapeInfo[type].display;
if (type === 'Line') { var val = (display === 'area')
return infoLine(data, shape); ? rectifiedArea(data, shape)
} : rectifiedDist(data, shape);
if (type === 'LineString') { data.lastMeasuredValue = val;
return infoLine(data, shape); setActiveShapeType(data, type);
} updateCalculation(data);
if (type === 'Rectangle') {
return infoRect(data, shape);
}
if (type === 'Polygon') {
return infoPolygon(data, shape);
}
}; };
// recalculate after unit change
var updateConversion = function(data) {
}
// return a shape of the currently selected shape type // return a shape of the currently selected shape type
var newShape = function(data) { var newShape = function(data) {
return { return {
geometry : { geometry : {
type : getSelectedShapeType(data) type : getActiveShapeType(data)
}, },
properties : { properties : {
stroke : getSelectedStroke(data), stroke : getSelectedStroke(data),
...@@ -946,19 +915,18 @@ ...@@ -946,19 +915,18 @@
}; };
// return the currently selected shape type // return the currently selected shape type
var getSelectedShapeType = function(data) { var getActiveShapeType = function(data) {
return data.settings.activeShapeType; return data.settings.activeShapeType;
}; };
// set the currently selected shape type // set the currently selected shape type
var setActiveShapeType = function(data) { var changeShapeType = function(data) {
data.settings.activeShapeType = data.measureWidgets.shape.val(); data.settings.activeShapeType = data.measureWidgets.shape.val();
}; };
// set the currently selected shape type // set the currently selected shape type
var changeActiveShapeType = function(data, type) { var setActiveShapeType = function(data, type) {
data.settings.activeShapeType = type; data.settings.activeShapeType = type;
data.measureWidgets.shape.val(type);
}; };
// return line color chosen by user // return line color chosen by user
...@@ -968,18 +936,18 @@ ...@@ -968,18 +936,18 @@
}; };
// load shapes into select element // load shapes into select element
var loadShapeTypes = function(data) { var populateShapeSelect = function(data) {
var $shape = data.measureWidgets.shape; var $shape = data.measureWidgets.shape;
var shapeTypes = data.settings.shapeTypes; var shapeInfo = data.settings.shapeInfo;
var addOption = function(index, type) { var addOption = function(index, type) {
$shape.append($('<option value="'+ type + '">' + shapeTypes[type].name + '</option>')); $shape.append($('<option value="'+ type + '">' + shapeInfo[type].name + '</option>'));
}; };
$.each(data.settings.implementedShapes, addOption); $.each(data.settings.implementedShapes, addOption);
$shape.children()[0].selected = true; $shape.children()[0].selected = true;
}; };
// load units into select elements // load units into select elements
var loadSections = function(data) { var populateUnitSelects = function(data) {
var widgets = data.measureWidgets; var widgets = data.measureWidgets;
var $u1 = widgets.unit1; var $u1 = widgets.unit1;
var $u2 = widgets.unit2; var $u2 = widgets.unit2;
...@@ -1029,26 +997,21 @@ ...@@ -1029,26 +997,21 @@
var widgets = { var widgets = {
names : [ names : [
'move', 'startb', 'shape', 'move', 'startb', 'shape',
// 'lenlabel', 'len', 'eq1',
'info', 'info',
'value1', 'unit1', 'eq2', 'value1', 'unit1', 'eq',
'value2', 'unit2' 'value2', 'unit2'
], ],
move : $('<img id="dl-measure-move" src="img/move.png" title="move measuring bar around the screen"></img>'), move : $('<img id="dl-measure-move" src="img/move.png" title="move measuring bar around the screen"></img>'),
startb : $('<button id="dl-measure-startb" title="click to draw a measuring shape on top of the image">M</button>'), startb : $('<button id="dl-measure-startb" title="click to draw a measuring shape on top of the image">M</button>'),
shape : $('<select id="dl-measure-shape" title="select a shape to use for measuring" />'), shape : $('<select id="dl-measure-shape" title="select a shape to use for measuring" />'),
// lenlabel : $('<span class="dl-measure-label" >len</span>'), eq : $('<span class="dl-measure-label">=</span>'),
// faclabel : $('<span class="dl-measure-label" >factor</span>'), info : $('<span id="dl-measure-shapetype" class="dl-measure-label">length</span>'),
// eq1 : $('<span class="dl-measure-label">=</span>'), fac : $('<span id="dl-measure-factor" class="dl-measure-number" />'),
eq2 : $('<span class="dl-measure-label">=</span>'),
// len : $('<span id="dl-measure-len" class="dl-measure-number">0.0</span>'),
info : $('<span id="dl-measure-shapeinfo" class="dl-measure-label">length</span>'),
fac : $('<span id="dl-measure-factor" class="dl-measure-number" />'),
value1 : $('<input id="dl-measure-value1" class="dl-measure-input" title="last measured value - click to change the value" value="0.0" />'), value1 : $('<input id="dl-measure-value1" class="dl-measure-input" title="last measured value - click to change the value" value="0.0" />'),
value2 : $('<span id="dl-measure-value2" class="dl-measure-label" title="last measured value, converted to the secondary unit" value="0.0"/>'), value2 : $('<span id="dl-measure-value2" class="dl-measure-label" title="last measured value, converted to the secondary unit" value="0.0"/>'),
unit1 : $('<select id="dl-measure-unit1" title="current measuring unit - click to change" />'), unit1 : $('<select id="dl-measure-unit1" title="current measuring unit - click to change" />'),
unit2 : $('<select id="dl-measure-unit2" title="secondary measuring unit - click to change" />'), unit2 : $('<select id="dl-measure-unit2" title="secondary measuring unit - click to change" />'),
angle : $('<span id="dl-measure-angle" class="dl-measure-number" title="last measured angle" />') angle : $('<span id="dl-measure-angle" class="dl-measure-number" title="last measured angle" />')
}; };
var $measureBar = $('<div id="dl-measure-toolbar" />'); var $measureBar = $('<div id="dl-measure-toolbar" />');
$.each(widgets.names, function(index, item) { $.each(widgets.names, function(index, item) {
...@@ -1058,9 +1021,8 @@ ...@@ -1058,9 +1021,8 @@
data.$measureBar = $measureBar; data.$measureBar = $measureBar;
widgets.fac.text(fn.cropFloatStr(data.measureFactor)); widgets.fac.text(fn.cropFloatStr(data.measureFactor));
data.measureWidgets = widgets; data.measureWidgets = widgets;
populateShapeSelect(data);
loadShapeTypes(data); populateUnitSelects(data);
loadSections(data);
setupMeasureWidgets(data); setupMeasureWidgets(data);
widgets.move.on('mousedown.measure', dragMeasureBar); widgets.move.on('mousedown.measure', dragMeasureBar);
return $measureBar; return $measureBar;
...@@ -1083,17 +1045,17 @@ ...@@ -1083,17 +1045,17 @@
$elem.digilib(action); $elem.digilib(action);
return false; return false;
}); });
widgets.shape.on('change.measure', function(evt) { setActiveShapeType(data) }); widgets.shape.on('change.measure', function(evt) { changeShapeType(data) });
widgets.value1.on('change.measure', function(evt) { changeFactor(data) }); widgets.value1.on('change.measure', function(evt) { changeFactor(data) });
widgets.unit1.on('change.measure', function(evt) { updateInfo(data) }); widgets.unit1.on('change.measure', function(evt) { updateCalculation(data) });
widgets.unit2.on('change.measure', function(evt) { updateInfo(data) }); widgets.unit2.on('change.measure', function(evt) { updateCalculation(data) });
}; };
// event handler // event handler
var handleSetup = function (evt) { var handleSetup = function (evt) {
console.debug("measure: handleSetup"); console.debug("measure: handleSetup");
var data = this; var data = this;
data.lastMeasuredDistance = 0; data.lastMeasuredValue = 0;
data.lastMeasuredAngle = 0; data.lastMeasuredAngle = 0;
data.measureFactor = 1.0, data.measureFactor = 1.0,
setupMeasureBar(data); setupMeasureBar(data);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment