Skip to content

Commit 05b898c

Browse files
committed
Resize
1 parent ca027eb commit 05b898c

File tree

4 files changed

+172
-45
lines changed

4 files changed

+172
-45
lines changed

Scenes/Main.tscn

+11-9
Original file line numberDiff line numberDiff line change
@@ -5678,8 +5678,8 @@ visible = true
56785678
margin_left = 1264.0
56795679
margin_top = -2160.0
56805680
margin_right = 1668.0
5681-
margin_bottom = -1820.0
5682-
rect_min_size = Vector2( 404, 340 )
5681+
margin_bottom = -1937.0
5682+
rect_min_size = Vector2( 404, 223 )
56835683
mouse_filter = 1
56845684
window_title = "Resize map"
56855685
resizable = true
@@ -5700,7 +5700,7 @@ custom_constants/margin_bottom = 20
57005700
margin_left = 20.0
57015701
margin_top = 20.0
57025702
margin_right = 384.0
5703-
margin_bottom = 320.0
5703+
margin_bottom = 203.0
57045704

57055705
[node name="HBoxContainer" type="HBoxContainer" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
57065706
margin_right = 364.0
@@ -5814,6 +5814,7 @@ mouse_filter = 1
58145814
color = Color( 0.262745, 0.266667, 0.298039, 1 )
58155815

58165816
[node name="HBoxContainer2" type="HBoxContainer" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
5817+
visible = false
58175818
margin_top = 35.0
58185819
margin_right = 364.0
58195820
margin_bottom = 91.0
@@ -5845,16 +5846,17 @@ margin_bottom = 56.0
58455846
size_flags_horizontal = 10
58465847

58475848
[node name="ResizeMapApplyBorderCheckbox" type="CheckBox" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
5848-
margin_top = 95.0
5849+
visible = false
5850+
margin_top = 35.0
58495851
margin_right = 364.0
5850-
margin_bottom = 124.0
5852+
margin_bottom = 64.0
58515853
pressed = true
58525854
text = "Apply border"
58535855

58545856
[node name="Label" type="Label" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
5855-
margin_top = 224.0
5857+
margin_top = 107.0
58565858
margin_right = 364.0
5857-
margin_bottom = 269.0
5859+
margin_bottom = 152.0
58585860
size_flags_vertical = 10
58595861
custom_colors/font_color = Color( 1, 0.85098, 0.603922, 1 )
58605862
text = "Resizing still needs testing, it may break your map so please keep a backup."
@@ -5864,9 +5866,9 @@ autowrap = true
58645866

58655867
[node name="ResizeApplyButton" type="Button" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
58665868
margin_left = 107.0
5867-
margin_top = 273.0
5869+
margin_top = 156.0
58685870
margin_right = 257.0
5869-
margin_bottom = 300.0
5871+
margin_bottom = 183.0
58705872
rect_min_size = Vector2( 150, 0 )
58715873
size_flags_horizontal = 6
58725874
size_flags_vertical = 8

Scenes/OverheadGraphics.gd

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@ var overheadTexData = ImageTexture.new()
1313
var arrayOfColorRects = []
1414

1515
func update_map_overhead_2d_textures():
16-
17-
1816
var CODETIME_START = OS.get_ticks_msec()
1917

2018
if arrayOfColorRects.empty() == true:
2119
initialize_display_fields()
2220
else:
2321
update_display_fields_size()
24-
22+
2523
overheadImgData.create((M.xSize*3), (M.ySize*3), false, Image.FORMAT_RGB8)
2624
overheadTexData.create_from_image(overheadImgData, 0)
2725

Scenes/ResizeCurrentMapSize.gd

+159-32
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ onready var oSlabPlacement = Nodelist.list["oSlabPlacement"]
1010
onready var oResizeFillWithID = Nodelist.list["oResizeFillWithID"]
1111
onready var oResizeFillWithIDLabel = Nodelist.list["oResizeFillWithIDLabel"]
1212
onready var oResizeMapApplyBorderCheckbox = Nodelist.list["oResizeMapApplyBorderCheckbox"]
13+
onready var oMessage = Nodelist.list["oMessage"]
14+
onready var oLoadingBar = Nodelist.list["oLoadingBar"]
15+
onready var oDataClmPos = Nodelist.list["oDataClmPos"]
1316

1417
func _on_ResizeCurrentMapSizeButton_pressed():
1518
Utils.popup_centered(self)
@@ -18,54 +21,106 @@ func _on_ResizeCurrentMapSize_about_to_show():
1821
oSettingsXSizeLine.text = str(M.xSize)
1922
oSettingsYSizeLine.text = str(M.ySize)
2023

21-
func _on_ResizeApplyButton_pressed():
22-
var newWidth = int(oSettingsXSizeLine.text)
23-
var newHeight = int(oSettingsYSizeLine.text)
24-
25-
var previousWidth = M.xSize
26-
var previousHeight = M.ySize
24+
# Function to handle updating the map size
25+
func set_new_map_size(newWidth, newHeight):
2726
M.xSize = newWidth
2827
M.ySize = newHeight
2928
oMapSizeTextLabel.text = str(M.xSize) + " x " + str(M.ySize)
3029

30+
# Function to get positions that need to be updated
31+
func get_positions_to_update(newWidth, newHeight, previousWidth, previousHeight):
3132
var positionsToUpdate = {}
32-
33-
# Handle width
3433
if newWidth > previousWidth:
3534
for x in range(previousWidth, newWidth):
36-
for y in range(newHeight):
35+
for y in newHeight:
3736
positionsToUpdate[Vector2(x, y)] = true
38-
# Handle height
3937
if newHeight > previousHeight:
4038
for y in range(previousHeight, newHeight):
41-
for x in range(newWidth):
39+
for x in newWidth:
4240
positionsToUpdate[Vector2(x, y)] = true
41+
oSlabPlacement.place_shape_of_slab_id(positionsToUpdate.keys(), Slabs.EARTH, 5)
42+
return positionsToUpdate
4343

44+
# Function to remove old borders
45+
func remove_old_borders(newWidth, newHeight, previousWidth, previousHeight):
46+
var removeBorder = []
47+
if newWidth > previousWidth:
48+
for y in previousHeight:
49+
removeBorder.append(Vector2(previousWidth - 1, y))
50+
if newHeight > previousHeight:
51+
for x in previousWidth:
52+
removeBorder.append(Vector2(x, previousHeight - 1))
53+
oSlabPlacement.place_shape_of_slab_id(removeBorder, Slabs.EARTH, 5)
54+
return removeBorder
55+
56+
# Function to add new borders
57+
func add_new_borders(newWidth, newHeight):
58+
var addBorder = []
59+
for x in newWidth:
60+
addBorder.append(Vector2(x, 0))
61+
addBorder.append(Vector2(x, newHeight - 1))
62+
for y in newHeight:
63+
addBorder.append(Vector2(0, y))
64+
addBorder.append(Vector2(newWidth - 1, y))
65+
oSlabPlacement.place_shape_of_slab_id(addBorder, Slabs.ROCK, 5)
66+
return addBorder
67+
68+
# Function to remove instances outside of the new map size
69+
func remove_outside_instances(newWidth, newHeight):
70+
var deletedInstancesCount = 0
71+
var newHeightInSubtiles = newHeight * 3
72+
var newWidthInSubtiles = newWidth * 3
73+
74+
for instance in get_tree().get_nodes_in_group("Instance"):
75+
if instance.locationX >= newWidthInSubtiles or instance.locationY >= newHeightInSubtiles:
76+
deletedInstancesCount += 1
77+
instance.queue_free()
78+
79+
if deletedInstancesCount > 0:
80+
oMessage.quick("Deleted " + str(deletedInstancesCount) + " instances that were outside of the new map size.")
81+
82+
func update_editor_appearance():
4483
oEditor.update_boundaries()
4584
oOverheadOwnership.start()
4685
oOverheadGraphics.update_map_overhead_2d_textures()
4786

48-
# Apply changes for added positions
49-
var newlyAddedPositions = positionsToUpdate.keys()
50-
oSlabPlacement.place_shape_of_slab_id(newlyAddedPositions, int(oResizeFillWithID.value), 5)
87+
# The main function that calls all the helper functions
88+
func _on_ResizeApplyButton_pressed():
89+
var newWidth = int(oSettingsXSizeLine.text)
90+
var newHeight = int(oSettingsYSizeLine.text)
91+
var previousWidth = M.xSize
92+
var previousHeight = M.ySize
93+
set_new_map_size(newWidth, newHeight)
94+
remove_outside_instances(newWidth, newHeight)
5195

52-
if oResizeMapApplyBorderCheckbox.pressed == true:
53-
var borderPositions = []
54-
for x in range(newWidth):
55-
positionsToUpdate[Vector2(x, 0)] = true
56-
positionsToUpdate[Vector2(x, newHeight - 1)] = true
57-
borderPositions.append(Vector2(x, 0)) # Top border
58-
borderPositions.append(Vector2(x, newHeight - 1)) # Bottom border
59-
for y in range(newHeight):
60-
positionsToUpdate[Vector2(0, y)] = true
61-
positionsToUpdate[Vector2(newWidth - 1, y)] = true
62-
borderPositions.append(Vector2(0, y)) # Left border
63-
borderPositions.append(Vector2(newWidth - 1, y)) # Right border
64-
oSlabPlacement.place_shape_of_slab_id(borderPositions, Slabs.ROCK, 5)
96+
var positionsToUpdate = get_positions_to_update(newWidth, newHeight, previousWidth, previousHeight)
97+
var removeBorder = remove_old_borders(newWidth, newHeight, previousWidth, previousHeight)
98+
var addBorder = add_new_borders(newWidth, newHeight)
99+
for pos in removeBorder:
100+
positionsToUpdate[pos] = true
101+
for pos in addBorder:
102+
positionsToUpdate[pos] = true
103+
104+
set_various_grid_data(newWidth, newHeight, previousWidth, previousHeight)
105+
106+
update_editor_appearance()
65107

66-
# Finalize
67108
oSlabPlacement.generate_slabs_based_on_id(positionsToUpdate.keys(), false)
68109

110+
func set_various_grid_data(newWidth, newHeight, previousWidth, previousHeight):
111+
var newWidthInSubtiles = newWidth * 3
112+
var newHeightInSubtiles = newHeight * 3
113+
var prevWidthInSubtiles = previousWidth * 3
114+
var prevHeightInSubtiles = previousHeight * 3
115+
116+
for x in prevWidthInSubtiles:
117+
for y in prevHeightInSubtiles:
118+
if x >= newWidthInSubtiles or y >= newHeightInSubtiles:
119+
oDataClmPos.set_cell(x, y, 0)
120+
121+
122+
123+
69124
func _on_SettingsXSizeLine_focus_exited():
70125
if int(oSettingsXSizeLine.text) > 170:
71126
oSettingsXSizeLine.text = "170"
@@ -74,6 +129,82 @@ func _on_SettingsYSizeLine_focus_exited():
74129
if int(oSettingsYSizeLine.text) > 170:
75130
oSettingsYSizeLine.text = "170"
76131

132+
func _on_ResizeFillWithID_value_changed(value):
133+
value = int(value)
134+
if Slabs.data.has(value):
135+
oResizeFillWithIDLabel.text = Slabs.data[value][Slabs.NAME]
136+
137+
138+
139+
#func _on_ResizeApplyButton_pressed():
140+
# var newWidth = int(oSettingsXSizeLine.text)
141+
# var newHeight = int(oSettingsYSizeLine.text)
142+
#
143+
# var previousWidth = M.xSize
144+
# var previousHeight = M.ySize
145+
# M.xSize = newWidth
146+
# M.ySize = newHeight
147+
# oMapSizeTextLabel.text = str(M.xSize) + " x " + str(M.ySize)
148+
#
149+
# var positionsToUpdate = {}
150+
#
151+
# # Handle width
152+
# if newWidth > previousWidth:
153+
# for x in range(previousWidth, newWidth):
154+
# for y in newHeight:
155+
# positionsToUpdate[Vector2(x, y)] = true
156+
# # Handle height
157+
# if newHeight > previousHeight:
158+
# for y in range(previousHeight, newHeight):
159+
# for x in newWidth:
160+
# positionsToUpdate[Vector2(x, y)] = true
161+
#
162+
# oEditor.update_boundaries()
163+
# oOverheadOwnership.start()
164+
# oOverheadGraphics.update_map_overhead_2d_textures()
165+
#
166+
# # Apply changes for added positions
167+
# oSlabPlacement.place_shape_of_slab_id(positionsToUpdate.keys(), Slabs.EARTH, 5)
168+
#
169+
# var removeBorder = [] # Remove old south and east borders when enlarging the map
170+
# if newWidth > previousWidth:
171+
# for y in previousHeight:
172+
# removeBorder.append(Vector2(previousWidth - 1, y))
173+
# if newHeight > previousHeight:
174+
# for x in previousWidth:
175+
# removeBorder.append(Vector2(x, previousHeight - 1))
176+
# oSlabPlacement.place_shape_of_slab_id(removeBorder, Slabs.EARTH, 5)
177+
#
178+
# var addBorder = []
179+
# for x in newWidth:
180+
# addBorder.append(Vector2(x, 0))
181+
# addBorder.append(Vector2(x, newHeight - 1))
182+
# for y in newHeight:
183+
# addBorder.append(Vector2(0, y))
184+
# addBorder.append(Vector2(newWidth - 1, y))
185+
# oSlabPlacement.place_shape_of_slab_id(addBorder, Slabs.ROCK, 5)
186+
#
187+
# for pos in addBorder: # Update the appearance of any border alterations
188+
# positionsToUpdate[pos] = true
189+
# for pos in removeBorder: # Update the appearance of any border alterations
190+
# positionsToUpdate[pos] = true
191+
#
192+
# # Remove instances outside of the new map size
193+
# var instances = get_tree().get_nodes_in_group("Instance")
194+
# var deletedInstancesCount = 0
195+
# var newHeightInSubtiles = newHeight * 3
196+
# var newWidthInSubtiles = newWidth * 3
197+
# for instance in instances:
198+
# if instance.locationX >= newWidthInSubtiles or instance.locationY >= newHeightInSubtiles:
199+
# deletedInstancesCount+=1
200+
# instance.queue_free()
201+
# if deletedInstancesCount > 0:
202+
# oMessage.quick("Deleted " + str(deletedInstancesCount) + " instances that were outside of the new map size.")
203+
#
204+
# # Finalize
205+
# oSlabPlacement.generate_slabs_based_on_id(positionsToUpdate.keys(), false)
206+
207+
77208

78209
#onready var resizeSegments = [ # These are ColorRects by the way.
79210
# $MarginContainer/VBoxContainer/GridContainer/ResizeSegment1,
@@ -152,7 +283,3 @@ func _on_SettingsYSizeLine_focus_exited():
152283
# if x >= previousWidth or y >= previousHeight:
153284
# newPositionArray.append(Vector2(x, y))
154285

155-
func _on_ResizeFillWithID_value_changed(value):
156-
value = int(value)
157-
if Slabs.data.has(value):
158-
oResizeFillWithIDLabel.text = Slabs.data[value][Slabs.NAME]

Scenes/SlabPlacement.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ func place_shape_of_slab_id(shapePositionArray, slabID, ownership):
233233
for i in removeFromShape:
234234
shapePositionArray.erase(i)
235235

236-
oOverheadOwnership.update_ownership_image_based_on_shape(shapePositionArray)
237236
print('Slab IDs set in : '+str(OS.get_ticks_msec()-CODETIME_START)+'ms')
238237

239238
onready var oLoadingBar = Nodelist.list["oLoadingBar"]
240239

241240
func generate_slabs_based_on_id(shapePositionArray, updateNearby):
241+
oOverheadOwnership.update_ownership_image_based_on_shape(shapePositionArray)
242242
var CODETIME_START = OS.get_ticks_msec()
243243

244244
oEditor.mapHasBeenEdited = true

0 commit comments

Comments
 (0)