From 07e31be1298ab2df9b7b1442a2b5979c74820674 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 23 Feb 2012 15:17:24 +0100 Subject: Move 'em boxes... --- munia.html | 65 ++++++++++++++++++++--------------------------------------- src/muniad.cc | 34 ++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/munia.html b/munia.html index 74f5e75..dfe59e7 100644 --- a/munia.html +++ b/munia.html @@ -108,7 +108,7 @@ var last_x = 0, last_y = 0; var ctx; var socket_lm; var color = "#000000"; -var dragged = 0; +var dragged = ""; // alert(BrowserDetect.browser); if (BrowserDetect.browser == "Firefox") { @@ -126,7 +126,7 @@ try { socket_lm.onmessage = function got_packet(msg) { // alert(msg.data); - document.getElementById("wslm_lastmsg").textContent = msg.data; +// document.getElementById("wslm_lastmsg").textContent = msg.data; j = msg.data.split(';'); f = 0; @@ -151,11 +151,17 @@ try { // task.style.display = "none"; document.body.removeChild(task); } + else if (i[0] == "move") { +// alert("Moving task"); + var task = document.getElementById("task_" + i[1]); + task.style.left = i[2] + "px"; + task.style.top = i[3] + "px"; + } else if (i[0] == "add") { var task = document.createElement("div"); task.name = "task"; task.id = "task_" + i[1]; - + var taskText = document.createTextNode(i[2] + ": " + i[3] + " :" + task.id); task.appendChild(taskText); @@ -167,6 +173,7 @@ try { task.style.borderWidth= "medium"; task.style.borderRadius= "15px"; task.style.padding = "4px"; + task.setAttribute("onMouseDown", "dragged = '" + i[1] + "';"); var dlButton = document.createElement("input"); dlButton.type = "button"; @@ -199,10 +206,12 @@ ctx = canvas.getContext("2d"); document.getElementById('wslm_drawing').appendChild(canvas); -canvas.addEventListener('mousemove', ev_mousemove, false); +document.addEventListener('mousemove', ev_mousemove, false); +document.addEventListener('mouseup', ev_mouseup, false); +/* canvas.addEventListener('mousedown', ev_mousedown, false); -canvas.addEventListener('mouseup', ev_mouseup, false); //cancas.addEventListener('dragend', ev_mousemove, false); +*/ offsetX = offsetY = 0; element = canvas; @@ -217,49 +226,19 @@ function update_color() { color = document.getElementById("color").value; } -function ev_mousedown (ev) { - down = 1; - - var x, y; - - if (ev.offsetX) { - x = ev.offsetX; - y = ev.offsetY; - } else { - x = ev.layerX - offsetX; - y = ev.layerY - offsetY; - } -} - function ev_mouseup(ev) { - down = 0; - no_last = 1; + dragged = ''; } -function ev_mousemove (ev) { - var x, y; - - if (ev.offsetX) { - x = ev.offsetX; - y = ev.offsetY; - } else { - x = ev.layerX - offsetX; - y = ev.layerY - offsetY; - - } +function ev_mousemove (ev) +{ + if(dragged == '') return; - if (!down) - return; - if (no_last) { - no_last = 0; - last_x = x; - last_y = y; - return; - } - socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';'); + var x, y; + x = ev.clientX; + y = ev.clientY; - last_x = x; - last_y = y; + socket_lm.send("move " + dragged + " " + x + " " + y); } function submit() { diff --git a/src/muniad.cc b/src/muniad.cc index e349e7c..9cb947c 100644 --- a/src/muniad.cc +++ b/src/muniad.cc @@ -358,15 +358,47 @@ static int callback_lws_mirror(struct libwebsocket_context * context, break; } } - + if(!id_found) { printf("\t!!!Could not locate task with id %d\n", id); } buf_len = sprintf(buf, "del %d;", id); printf("Deleting task: %s\n", buf); + } else if(cmd == "move") { printf("Move\n"); + + int offset = cmd.length() + 1; + std::string s_id = data.substr(offset, data.find(' ', offset) - offset); + int id = atoi(s_id.c_str()); + offset += s_id.length() + 1; + std::string x_str = data.substr(offset, data.find(' ', offset) - offset); + int x = atoi(x_str.c_str()); + offset += x_str.length() + 1; + std::string y_str = data.substr(offset, data.find(' ', offset) - offset); + int y = atoi(y_str.c_str()); + + printf("Moving task with id %d to (%d,%d)\n", id, x, y); + + bool id_found = false; + TaskList::iterator it; + for(it = tasklist.begin(); it != tasklist.end(); it++) { + struct task t = *it; + if(t.id == id) { + id_found = true; + t.x = x; + t.y = y; + break; + } + } + + if(!id_found) { + printf("\t!!!Could not locate task with id %d\n", id); + } + + buf_len = sprintf(buf, "move %d %d %d;", id, x, y); + printf("Moving task: %s\n", buf); } else if(cmd == "update") { printf("Update\n"); } else if(cmd == "d") { -- cgit v1.2.3