Skip to content

Commit eb354c8

Browse files
authored
Merge pull request #42 from nellyvo/move_selection
add move selection
2 parents 44f3da6 + ecccbad commit eb354c8

File tree

1 file changed

+96
-26
lines changed

1 file changed

+96
-26
lines changed

js/eventHandlers.js

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,39 @@ function Add_EventHandlers_To_Canvas_Cells()
139139
canvasDiv.appendChild(selection);
140140
}
141141

142-
function Selection_Mousedown(e)
142+
function Selection_Mousedown(e)
143143
{
144144
if (STATE["activeTool"] === "selection") {
145145
let selection = document.getElementById("selection");
146146
let cursorXY = Canvas_Cursor_XY(e);
147147

148-
if ((STATE["altKeyDown"] === true) &&
149-
(STATE["selection"]["isLocked"] === true) &&
150-
(selection) && // selection in DOM
148+
if ((STATE["selection"]["isLocked"] === true) &&
149+
(selection) &&
151150
CursorXY_In_Selection(cursorXY, selection)) {
152-
STATE["selection"]["floatingCopy"] = true;
151+
if (STATE["altKeyDown"] === true) {
152+
STATE["selection"]["floatingCopy"] = true;
153153

154-
let colorArray = Canvas_Pixels_From_Selection();
155-
STATE["selectionCopy"]["colorArray"] = colorArray;
154+
let colorArray = Canvas_Pixels_From_Selection();
155+
STATE["selectionCopy"]["colorArray"] = colorArray;
156156

157-
STATE["selectionCopy"]["initCursorX"] = cursorXY[0] / CELL_WIDTH_PX;
158-
STATE["selectionCopy"]["initCursorY"] = cursorXY[1] / CELL_WIDTH_PX;
157+
STATE["selectionCopy"]["initCursorX"] = cursorXY[0] / CELL_WIDTH_PX;
158+
STATE["selectionCopy"]["initCursorY"] = cursorXY[1] / CELL_WIDTH_PX;
159+
} else {
160+
161+
let colorArray = Canvas_Pixels_From_Selection();
162+
STATE["selectionCopy"]["colorArray"] = colorArray;
163+
164+
let origLeft = Px_To_Int(selection.style.left) / CELL_WIDTH_PX;
165+
let origTop = Px_To_Int(selection.style.top) / CELL_WIDTH_PX;
166+
167+
STATE["selection"]["isMoving"] = true;
168+
STATE["selectionMove"] = {
169+
initCursorX: cursorXY[0] / CELL_WIDTH_PX,
170+
initCursorY: cursorXY[1] / CELL_WIDTH_PX,
171+
initLeft: origLeft,
172+
initTop: origTop
173+
};
174+
}
159175
} else {
160176
Remove_Selection();
161177
Unlock_Selection();
@@ -164,7 +180,7 @@ function Add_EventHandlers_To_Canvas_Cells()
164180
}
165181
}
166182

167-
function Selection_Mousemove(e)
183+
function Selection_Mousemove(e)
168184
{
169185
const selection = document.getElementById("selection");
170186

@@ -206,9 +222,9 @@ function Add_EventHandlers_To_Canvas_Cells()
206222
selection.style.height = (height - 1) + "px";
207223

208224
return;
209-
} else
225+
} else
210226
if ((STATE["activeTool"] === "selection") &&
211-
(STATE["selection"]["isLocked"] === true))
227+
(STATE["selection"]["isLocked"] === true))
212228
{
213229
let cursorXY = Canvas_Cursor_XY(e);
214230
if (STATE["selection"]["floatingCopy"] === true) {
@@ -235,29 +251,78 @@ function Add_EventHandlers_To_Canvas_Cells()
235251
cell.style.backgroundColor = HISTORY_STATES.getCurrentState()[i];
236252
}
237253

238-
let cell0 = Get_CellInt_From_CellXY(selectionLeft + dx,
239-
selectionTop + dy);
254+
let cell0 = Get_CellInt_From_CellXY(selectionLeft + dx, selectionTop + dy);
240255
for (let y = 0; y < selectionHeight; y += 1)
241256
for (let x = 0; x < selectionWidth; x += 1) {
242257
let id = Pad_Start_Int(cell0 + y * CELLS_PER_ROW + x);
243258
let cell = document.getElementById(id);
244259
let idx = x + y * selectionWidth;
245260
let color = STATE["selectionCopy"]["colorArray"][idx];
246-
cell.style.backgroundColor = color;
261+
if (color && color !== "transparent") {
262+
cell.style.backgroundColor = color;
263+
}
247264
}
248265

249266
STATE["selectionCopy"]["left"] = selectionLeft + dx;
250267
STATE["selectionCopy"]["top"] = selectionTop + dy;
251-
} else
252-
if ((CursorXY_In_Selection(cursorXY, selection) &&
253-
STATE["altKeyDown"] === true))
254-
{
268+
} else if (STATE["selection"]["isMoving"] === true) {
269+
Set_Cursor("move");
270+
let dx = (cursorXY[0] / CELL_WIDTH_PX) - STATE["selectionMove"]["initCursorX"];
271+
let dy = (cursorXY[1] / CELL_WIDTH_PX) - STATE["selectionMove"]["initCursorY"];
272+
273+
let newLeft = STATE["selectionMove"]["initLeft"] + dx;
274+
let newTop = STATE["selectionMove"]["initTop"] + dy;
275+
276+
let selectionWidth = (Px_To_Int(selection.style.width) + 1) / CELL_WIDTH_PX;
277+
let selectionHeight = (Px_To_Int(selection.style.height) + 1) / CELL_WIDTH_PX;
278+
279+
if (
280+
newLeft < 0 || newTop < 0 ||
281+
(newLeft + selectionWidth) > CELLS_PER_ROW ||
282+
(newTop + selectionHeight) > CELLS_PER_ROW
283+
) {
284+
return;
285+
}
286+
287+
if (!STATE["selection"]["originalCleared"]) {
288+
for (let y = 0; y < selectionHeight; y++) {
289+
for (let x = 0; x < selectionWidth; x++) {
290+
let index = (STATE["selectionMove"]["initTop"] + y) * CELLS_PER_ROW +
291+
(STATE["selectionMove"]["initLeft"] + x);
292+
HISTORY_STATES.getCurrentState()[index] = "transparent";
293+
}
294+
}
295+
STATE["selection"]["originalCleared"] = true;
296+
}
297+
298+
for (let i = 0; i < CELLS_PER_ROW * CELLS_PER_ROW; i++) {
299+
let cell = document.getElementById(Pad_Start_Int(i, 4));
300+
cell.style.backgroundColor = HISTORY_STATES.getCurrentState()[i];
301+
}
302+
303+
let cell0 = Get_CellInt_From_CellXY(newLeft, newTop);
304+
for (let y = 0; y < selectionHeight; y++) {
305+
for (let x = 0; x < selectionWidth; x++) {
306+
let id = Pad_Start_Int(cell0 + y * CELLS_PER_ROW + x);
307+
let cell = document.getElementById(id);
308+
let idx = x + y * selectionWidth;
309+
let color = STATE["selectionCopy"]["colorArray"][idx];
310+
if (color && color !== "transparent") {
311+
cell.style.backgroundColor = color;
312+
}
313+
}
314+
}
315+
316+
selection.style.left = newLeft * CELL_WIDTH_PX + "px";
317+
selection.style.top = newTop * CELL_WIDTH_PX + "px";
318+
} else
319+
if (CursorXY_In_Selection(cursorXY, selection) && STATE["altKeyDown"] === true) {
255320
Set_Cursor("move");
256321
}
257322
}
258323
}
259324

260-
function Selection_Mouseup(e)
325+
function Selection_Mouseup(e)
261326
{
262327
if (STATE["activeTool"] === "selection" &&
263328
STATE["selection"]["isLocked"] === false) {
@@ -276,15 +341,20 @@ function Add_EventHandlers_To_Canvas_Cells()
276341
Alert_User("<i>Alt</i> to copy");
277342
STATE["selection"]["totalCount"] += 1;
278343
}
279-
} else
344+
} else
280345
if (STATE["activeTool"] === "selection" &&
281346
STATE["selection"]["isLocked"] === true) {
282347
let selection = document.getElementById("selection");
283348

284-
selection.style.left = STATE["selectionCopy"]["left"] * CELL_WIDTH_PX + "px";
285-
selection.style.top = STATE["selectionCopy"]["top"] * CELL_WIDTH_PX + "px";
286-
STATE["selection"]["floatingCopy"] = false;
287-
349+
if (STATE["selection"]["floatingCopy"] === true) {
350+
selection.style.left = STATE["selectionCopy"]["left"] * CELL_WIDTH_PX + "px";
351+
selection.style.top = STATE["selectionCopy"]["top"] * CELL_WIDTH_PX + "px";
352+
STATE["selection"]["floatingCopy"] = false;
353+
}
354+
if (STATE["selection"]["isMoving"] === true) {
355+
STATE["selection"]["isMoving"] = false;
356+
STATE["selection"]["originalCleared"] = false;
357+
}
288358
if (STATE["altKeyDown"] === false) {
289359
Set_Cursor(Tools["selection"]["cursor"]);
290360
}
@@ -511,4 +581,4 @@ function Add_EventHandlers()
511581
Add_EventHandlers_To_Color_Preview();
512582
Add_EventHandlers_To_Save_Button();
513583
Add_EventHandlers_To_Toolbar_Buttons();
514-
}
584+
}

0 commit comments

Comments
 (0)