@@ -52,9 +52,17 @@ local DIRECTION_MAP = {
52
52
53
53
local DIRECTION_MAP_REVERSE = utils .invert (DIRECTION_MAP )
54
54
55
- local function swapElements (tbl , index1 , index2 )
56
- tbl [index1 ], tbl [index2 ] = tbl [index2 ], tbl [index1 ]
57
- return tbl
55
+ --[[
56
+ - swap 2 elements between different indexes in the same table like:
57
+ swap_elements({1, 2, 3}, 1, nil, 3) => {3, 2, 1}
58
+ - swap 2 elements at the specified indexes between 2 tables like:
59
+ swap_elements({1, 2, 3}, 1, {4, 5, 6}, 3) => {6, 2, 3} {4, 5, 1}
60
+ ]] --
61
+ local function swap_elements (tbl1 , index1 , tbl2 , index2 )
62
+ tbl2 = tbl2 or tbl1
63
+ index2 = index2 or index1
64
+ tbl1 [index1 ], tbl2 [index2 ] = tbl2 [index2 ], tbl1 [index1 ]
65
+ return tbl1 , tbl2
58
66
end
59
67
60
68
local function reset_guide_paths (conditions )
@@ -270,6 +278,78 @@ ReorderStopsWindow.ATTRS {
270
278
local SELECT_STOP_HINT = ' Select a stop to move'
271
279
local SELECT_ANOTHER_STOP_HINT = ' Select another stop on the same route'
272
280
281
+
282
+ function ReorderStopsWindow :handleStopSelection (index , item )
283
+ -- Skip routes
284
+ if item .type == ' route' then return end
285
+
286
+ -- Select stop if none selected
287
+ if not self .selected_stop then
288
+ self :toggleStopSelection (item )
289
+ return
290
+ end
291
+
292
+ -- Swap stops
293
+ self :swapStops (index , item )
294
+
295
+ -- Reset stop properties
296
+ self :resetStopProperties (item )
297
+
298
+ self .selected_stop = nil
299
+ self :updateList ()
300
+ end
301
+
302
+ function ReorderStopsWindow :toggleStopSelection (item )
303
+ if not self .selected_stop then
304
+ self .selected_stop = item
305
+ else
306
+ self .selected_stop = nil
307
+ end
308
+
309
+ self :updateList ()
310
+ end
311
+
312
+ function ReorderStopsWindow :swapStops (index , item )
313
+ local hauling = df .global .plotinfo .hauling
314
+ local routes = hauling .routes
315
+ local view_stops = hauling .view_stops
316
+ local item_route = routes [item .route_index ]
317
+ local item_stop_index = item .stop_index
318
+ local same_route = self .selected_stop .route_index == item .route_index
319
+
320
+ if same_route then
321
+ swap_elements (item_route .stops , item_stop_index , nil , self .selected_stop .stop_index )
322
+ else
323
+ swap_elements (
324
+ routes [self .selected_stop .route_index ].stops ,
325
+ self .selected_stop .stop_index ,
326
+ item_route .stops ,
327
+ item_stop_index
328
+ )
329
+ end
330
+
331
+ swap_elements (view_stops , self .selected_stop .list_position , nil , index - 1 )
332
+ end
333
+
334
+ function ReorderStopsWindow :resetStopProperties (item )
335
+ local hauling = df .global .plotinfo .hauling
336
+ local routes = hauling .routes
337
+ local item_route = routes [item .route_index ]
338
+ local same_route = self .selected_stop .route_index == item .route_index
339
+
340
+ for i , stop in ipairs (item_route .stops ) do
341
+ stop .id = i + 1
342
+ reset_guide_paths (stop .conditions )
343
+ end
344
+
345
+ if not same_route and self .selected_stop then
346
+ for i , stop in ipairs (routes [self .selected_stop .route_index ].stops ) do
347
+ stop .id = i + 1
348
+ reset_guide_paths (stop .conditions )
349
+ end
350
+ end
351
+ end
352
+
273
353
function ReorderStopsWindow :init ()
274
354
self .selected_stop = nil
275
355
self :addviews {
@@ -290,40 +370,7 @@ function ReorderStopsWindow:init()
290
370
end
291
371
end ,
292
372
on_submit = function (index , item )
293
- if self .selected_stop then
294
- local hauling = df .global .plotinfo .hauling
295
- local routes = hauling .routes
296
- local view_stops = hauling .view_stops
297
- local route = routes [item .route_index ]
298
-
299
- -- rearrange stops
300
- if item .type == ' stop' then
301
- local stop_index = item .stop_index
302
-
303
- -- don't allow moving stops to a different route for now. TODO: investigate this
304
- if self .selected_stop .route_index ~= item .route_index then
305
- return
306
- end
307
-
308
- swapElements (route .stops , stop_index , self .selected_stop .stop_index )
309
- swapElements (view_stops , self .selected_stop .list_position , index - 1 )
310
-
311
- -- loop over each stop in the route, make the ids sequental and reset guide paths
312
- -- TODO: figure out if changing the ids here breaks anything else
313
- for i , stop in ipairs (route .stops ) do
314
- stop .id = i + 1
315
- reset_guide_paths (stop .conditions )
316
- end
317
-
318
- self .selected_stop = nil
319
- end
320
- else
321
- if item .stop_index then
322
- self .selected_stop = item
323
- end
324
- end
325
-
326
- self :updateList ()
373
+ self :handleStopSelection (index , item )
327
374
end ,
328
375
},
329
376
}
0 commit comments