Skip to content

Commit

Permalink
Add line/arrow and drawing tool (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricktu288 committed Nov 18, 2023
1 parent 5693c39 commit 4b0fed1
Show file tree
Hide file tree
Showing 17 changed files with 599 additions and 1 deletion.
20 changes: 20 additions & 0 deletions simulator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,19 @@
<input type="radio" class="btn-check" name="toolsradio" autocomplete="off" id="tool_power">
<label class="btn shadow-none btn-primary dropdown-item" for="tool_power" data-text="tool_power" data-popover="tool_power_popover" data-image="power.svg" data-bs-placement="right" data-bs-offset="60,0"></label>
</li>
<li><hr class="dropdown-divider"></li>
<li>
<input type="radio" class="btn-check" name="toolsradio" autocomplete="off" id="tool_text">
<label class="btn shadow-none btn-primary dropdown-item" for="tool_text" data-text="tool_text" data-popover="tool_text_popover" data-bs-placement="right" data-bs-offset="0,0"></label>
</li>
<li>
<input type="radio" class="btn-check" name="toolsradio" autocomplete="off" id="tool_line">
<label class="btn shadow-none btn-primary dropdown-item" for="tool_line" data-text="tool_line" data-popover="tool_line_popover" data-bs-placement="right" data-bs-offset="0,0"></label>
</li>
<li>
<input type="radio" class="btn-check" name="toolsradio" autocomplete="off" id="tool_drawing">
<label class="btn shadow-none btn-primary dropdown-item" for="tool_drawing" data-text="tool_drawing" data-popover="tool_drawing_popover" data-bs-placement="right" data-bs-offset="0,0"></label>
</li>
</ul>
</div>
<input type="radio" class="btn-check" name="toolsradio" id="tool_" autocomplete="off" checked>
Expand Down Expand Up @@ -682,10 +691,19 @@
<input type="radio" class="btn-check" name="toolsradio_mobile" autocomplete="off" id="tool_power_mobile">
<label class="btn btn-primary dropdown-item" for="tool_power_mobile" data-text="tool_power"></label>
</li>
<li><hr class="dropdown-divider"></li>
<li>
<input type="radio" class="btn-check" name="toolsradio_mobile" autocomplete="off" id="tool_text_mobile">
<label class="btn btn-primary dropdown-item" for="tool_text_mobile" data-text="tool_text"></label>
</li>
<li>
<input type="radio" class="btn-check" name="toolsradio_mobile" autocomplete="off" id="tool_line_mobile">
<label class="btn btn-primary dropdown-item" for="tool_line_mobile" data-text="tool_line"></label>
</li>
<li>
<input type="radio" class="btn-check" name="toolsradio_mobile" autocomplete="off" id="tool_drawing_mobile">
<label class="btn btn-primary dropdown-item" for="tool_drawing_mobile" data-text="tool_drawing"></label>
</li>
</ul>
</div>

Expand Down Expand Up @@ -1182,6 +1200,8 @@ <h5 class="modal-title" id="staticBackdropLabel" data-text="language"></h5>
<script src="js/objs/protractor.js"></script>
<script src="js/objs/power.js"></script>
<script src="js/objs/text.js"></script>
<script src="js/objs/line.js"></script>
<script src="js/objs/drawing.js"></script>
<script src="js/objs/handle.js"></script>
<script src="js/objs/grin_circlelens.js"></script>
<script src="js/objs/grin_refractor.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion simulator/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ function createUndoPoint() {
}

function undo() {
if (isConstructing) {
if (isConstructing && !(objs.length > 0 && objs[objs.length - 1].type == 'drawing')) {
//假如按下復原時,使用者正在建立一個物件,則此時只將建立動作終止,而不做真正的復原 If the user is constructing an object when clicked the undo, then only stop the consturction rather than do the real undo

isConstructing = false;
Expand Down
109 changes: 109 additions & 0 deletions simulator/js/objs/drawing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Mirrors -> Circular Arc
objTypes['drawing'] = {

//建立物件 Create the obj
create: function(mouse) {
return {type: 'drawing', points: [], tmp_isMouseDown: false};
},

//顯示屬性方塊 Show the property box
p_box: function(obj, elem) {
if (isConstructing) {
createButton(getMsg('stop_drawing'), function(obj) {
obj.notDone = false;
isConstructing = false;
selectObj(objs.length - 1);
}, elem);
}
},

//建立物件過程滑鼠按下 Mousedown when the obj is being constructed by the user
c_mousedown: function(obj, mouse, ctrl, shift)
{
obj.points.push([mouse.x, mouse.y]);
obj.tmp_isMouseDown = true;
selectObj(objs.length - 1);
},
//建立物件過程滑鼠移動 Mousemove when the obj is being constructed by the user
c_mousemove: function(obj, mouse, ctrl, shift)
{
if (!obj.tmp_isMouseDown) return;
obj.points[obj.points.length - 1].push(mouse.x, mouse.y);
},
//建立物件過程滑鼠放開 Mouseup when the obj is being constructed by the user
c_mouseup: function(obj, mouse, ctrl, shift)
{
obj.tmp_isMouseDown = false;
createUndoPoint();
},

//將物件畫到Canvas上 Draw the obj on canvas
draw: function(obj, ctx, aboveLight) {
ctx.strokeStyle = getMouseStyle(obj, "white");
ctx.beginPath();
for (var i = 0; i < obj.points.length; i++) {
ctx.moveTo(obj.points[i][0], obj.points[i][1]);
for (var j = 2; j < obj.points[i].length; j += 2) {
ctx.lineTo(obj.points[i][j], obj.points[i][j + 1]);
}
}
ctx.stroke();
},

//平移物件 Move the object
move: function(obj, diffX, diffY) {
for (var i = 0; i < obj.points.length; i++) {
for (var j = 0; j < obj.points[i].length; j += 2) {
obj.points[i][j] += diffX;
obj.points[i][j + 1] += diffY;
}
}
return obj;
},


//繪圖區被按下時(判斷物件被按下的部分) When the drawing area is clicked (test which part of the obj is clicked)
clicked: function(obj, mouse_nogrid, mouse, draggingPart) {
for (var i = 0; i < obj.points.length; i++) {
for (var j = 0; j < obj.points[i].length - 2; j += 2) {
if (mouseOnSegment(mouse_nogrid, graphs.segment(graphs.point(obj.points[i][j], obj.points[i][j + 1]), graphs.point(obj.points[i][j + 2], obj.points[i][j + 3])))) {
draggingPart.part = 0;
draggingPart.mouse0 = mouse; //開始拖曳時的滑鼠位置 Mouse position when the user starts dragging
draggingPart.mouse1 = mouse; //拖曳時上一點的滑鼠位置 Mouse position at the last moment during dragging
draggingPart.snapData = {};
return true;
}
}
}
return false;
},

//拖曳物件時 When the user is dragging the obj
dragging: function(obj, mouse, draggingPart, ctrl, shift) {
if (shift)
{
var mouse_snapped = snapToDirection(mouse, draggingPart.mouse0, [{x: 1, y: 0},{x: 0, y: 1}], draggingPart.snapData);
}
else
{
var mouse_snapped = mouse;
draggingPart.snapData = {}; //放開shift時解除原先之拖曳方向鎖定 Unlock the dragging direction when the user release the shift key
}

var mouseDiffX = draggingPart.mouse1.x - mouse_snapped.x; //目前滑鼠位置與上一次的滑鼠位置的X軸差 The X difference between the mouse position now and at the previous moment
var mouseDiffY = draggingPart.mouse1.y - mouse_snapped.y; //目前滑鼠位置與上一次的滑鼠位置的Y軸差 The Y difference between the mouse position now and at the previous moment

if (draggingPart.part == 0) {
for (var i = 0; i < obj.points.length; i++) {
for (var j = 0; j < obj.points[i].length; j += 2) {
obj.points[i][j] -= mouseDiffX;
obj.points[i][j + 1] -= mouseDiffY;
}
}
}

//更新滑鼠位置 Update the mouse position
draggingPart.mouse1 = mouse_snapped;
}

};
54 changes: 54 additions & 0 deletions simulator/js/objs/line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

objTypes['line'] = {

//建立物件 Create the obj
create: function(mouse) {
return {type: 'line', p1: mouse, p2: mouse, arrow1: false, arrow2: false};
},

//使用lineobj原型 Use the prototype lineobj
c_mousedown: objTypes['lineobj'].c_mousedown,
c_mousemove: objTypes['lineobj'].c_mousemove,
c_mouseup: objTypes['lineobj'].c_mouseup,
move: objTypes['lineobj'].move,
clicked: objTypes['lineobj'].clicked,
dragging: objTypes['lineobj'].dragging,

//將物件畫到Canvas上 Draw the obj on canvas
draw: function(obj, ctx, aboveLight) {
ctx.strokeStyle = getMouseStyle(obj, "white");
ctx.beginPath();
ctx.moveTo(obj.p1.x, obj.p1.y);
ctx.lineTo(obj.p2.x, obj.p2.y);
ctx.stroke();
if (obj.arrow1) {
this.drawArrow(ctx, obj.p1, obj.p2);
}
if (obj.arrow2) {
this.drawArrow(ctx, obj.p2, obj.p1);
}
},

//畫箭頭 Draw the arrow
drawArrow: function(ctx, p1, p2) {
var angle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
var len = 10;
ctx.beginPath();
ctx.moveTo(p2.x, p2.y);
ctx.lineTo(p2.x - len * Math.cos(angle - Math.PI / 6), p2.y - len * Math.sin(angle - Math.PI / 6));
ctx.moveTo(p2.x, p2.y);
ctx.lineTo(p2.x - len * Math.cos(angle + Math.PI / 6), p2.y - len * Math.sin(angle + Math.PI / 6));
ctx.stroke();
},

//顯示屬性方塊 Show the property box
p_box: function(obj, elem) {
createBooleanAttr(getMsg('arrow1'), obj.arrow1, function(obj, value) {
obj.arrow1 = value;
}, elem);
createBooleanAttr(getMsg('arrow2'), obj.arrow2, function(obj, value) {
obj.arrow2 = value;
}, elem);
}

};
10 changes: 10 additions & 0 deletions simulator/js/parameterBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ function createDropdownAttr(label, value, options, func, elem) {
};
}

function createButton(label, func, elem) {
var button = document.createElement('button');
button.className = 'btn btn-secondary';
button.innerHTML = label;
button.onclick = function() {
func(objs[selectedObj]);
};
elem.appendChild(button);
}

function hasSameAttrType(obj1, obj2)
{
return obj1.type==obj2.type;
Expand Down
36 changes: 36 additions & 0 deletions simulator/locales/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ locales["de"] = {
"toolname_text": {
"message": "Text"
},
"toolname_line": {
"incomplete": true,
"message": "Line"
},
"toolname_drawing": {
"incomplete": true,
"message": "Drawing"
},
"tool_more_": {
"message": "Andere"
},
Expand Down Expand Up @@ -245,6 +253,14 @@ locales["de"] = {
"tool_text": {
"message": "Text"
},
"tool_line": {
"incomplete": true,
"message": "Line / Arrow"
},
"tool_drawing": {
"incomplete": true,
"message": "Drawing"
},
"tool_": {
"message": "Ansicht bewegen"
},
Expand Down Expand Up @@ -425,6 +441,18 @@ locales["de"] = {
"incomplete": true,
"message": "Angle (°)"
},
"arrow1": {
"incomplete": true,
"message": "Forward arrow"
},
"arrow2": {
"incomplete": true,
"message": "Backward arrow"
},
"stop_drawing": {
"incomplete": true,
"message": "Finish Drawing"
},
"beam_warning": {
"incomplete": true,
"message": "⚠️ Image detection may not work with divergent/random beams."
Expand Down Expand Up @@ -602,6 +630,14 @@ locales["de"] = {
"tool_text_popover": {
"message": "Ein Text-Label."
},
"tool_line_popover": {
"incomplete": true,
"message": "A line segment or arrow. (Drag or click to create.)"
},
"tool_drawing_popover": {
"incomplete": true,
"message": "A freehand drawing tool. (Drag to draw.)"
},
"tool__popover": {
"message": "Ziehen um die Ansicht zu Verschieben. (Rechte Maustaste hat die gleiche Funktion.)"
},
Expand Down
27 changes: 27 additions & 0 deletions simulator/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ locales["en"] = {
"toolname_text": {
"message": "Text"
},
"toolname_line": {
"message": "Line"
},
"toolname_drawing": {
"message": "Drawing"
},
"tool_more_": {
"message": "Other"
},
Expand Down Expand Up @@ -237,6 +243,12 @@ locales["en"] = {
"tool_text": {
"message": "Text"
},
"tool_line": {
"message": "Line / Arrow"
},
"tool_drawing": {
"message": "Drawing"
},
"tool_": {
"message": "Move view"
},
Expand Down Expand Up @@ -399,6 +411,15 @@ locales["en"] = {
"angle": {
"message": "Angle (°)"
},
"arrow1": {
"message": "Forward arrow"
},
"arrow2": {
"message": "Backward arrow"
},
"stop_drawing": {
"message": "Finish Drawing"
},
"beam_warning": {
"message": "⚠️ Image detection may not work with divergent/random beams."
},
Expand Down Expand Up @@ -564,6 +585,12 @@ locales["en"] = {
"tool_text_popover": {
"message": "A text label."
},
"tool_line_popover": {
"message": "A line segment or arrow. (Drag or click to create.)"
},
"tool_drawing_popover": {
"message": "A freehand drawing tool. (Drag to draw.)"
},
"tool__popover": {
"message": "Drag to move the view. (Mouse right button drag has the same function.)"
},
Expand Down
Loading

0 comments on commit 4b0fed1

Please sign in to comment.