Skip to content

Commit

Permalink
Resize
Browse files Browse the repository at this point in the history
  • Loading branch information
rainlizard committed Oct 31, 2023
1 parent ca027eb commit 05b898c
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 45 deletions.
20 changes: 11 additions & 9 deletions Scenes/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -5678,8 +5678,8 @@ visible = true
margin_left = 1264.0
margin_top = -2160.0
margin_right = 1668.0
margin_bottom = -1820.0
rect_min_size = Vector2( 404, 340 )
margin_bottom = -1937.0
rect_min_size = Vector2( 404, 223 )
mouse_filter = 1
window_title = "Resize map"
resizable = true
Expand All @@ -5700,7 +5700,7 @@ custom_constants/margin_bottom = 20
margin_left = 20.0
margin_top = 20.0
margin_right = 384.0
margin_bottom = 320.0
margin_bottom = 203.0

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

[node name="HBoxContainer2" type="HBoxContainer" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
visible = false
margin_top = 35.0
margin_right = 364.0
margin_bottom = 91.0
Expand Down Expand Up @@ -5845,16 +5846,17 @@ margin_bottom = 56.0
size_flags_horizontal = 10

[node name="ResizeMapApplyBorderCheckbox" type="CheckBox" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
margin_top = 95.0
visible = false
margin_top = 35.0
margin_right = 364.0
margin_bottom = 124.0
margin_bottom = 64.0
pressed = true
text = "Apply border"

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

[node name="ResizeApplyButton" type="Button" parent="Ui/UiSystem/ResizeCurrentMapSize/MarginContainer/VBoxContainer"]
margin_left = 107.0
margin_top = 273.0
margin_top = 156.0
margin_right = 257.0
margin_bottom = 300.0
margin_bottom = 183.0
rect_min_size = Vector2( 150, 0 )
size_flags_horizontal = 6
size_flags_vertical = 8
Expand Down
4 changes: 1 addition & 3 deletions Scenes/OverheadGraphics.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ var overheadTexData = ImageTexture.new()
var arrayOfColorRects = []

func update_map_overhead_2d_textures():


var CODETIME_START = OS.get_ticks_msec()

if arrayOfColorRects.empty() == true:
initialize_display_fields()
else:
update_display_fields_size()

overheadImgData.create((M.xSize*3), (M.ySize*3), false, Image.FORMAT_RGB8)
overheadTexData.create_from_image(overheadImgData, 0)

Expand Down
191 changes: 159 additions & 32 deletions Scenes/ResizeCurrentMapSize.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ onready var oSlabPlacement = Nodelist.list["oSlabPlacement"]
onready var oResizeFillWithID = Nodelist.list["oResizeFillWithID"]
onready var oResizeFillWithIDLabel = Nodelist.list["oResizeFillWithIDLabel"]
onready var oResizeMapApplyBorderCheckbox = Nodelist.list["oResizeMapApplyBorderCheckbox"]
onready var oMessage = Nodelist.list["oMessage"]
onready var oLoadingBar = Nodelist.list["oLoadingBar"]
onready var oDataClmPos = Nodelist.list["oDataClmPos"]

func _on_ResizeCurrentMapSizeButton_pressed():
Utils.popup_centered(self)
Expand All @@ -18,54 +21,106 @@ func _on_ResizeCurrentMapSize_about_to_show():
oSettingsXSizeLine.text = str(M.xSize)
oSettingsYSizeLine.text = str(M.ySize)

func _on_ResizeApplyButton_pressed():
var newWidth = int(oSettingsXSizeLine.text)
var newHeight = int(oSettingsYSizeLine.text)

var previousWidth = M.xSize
var previousHeight = M.ySize
# Function to handle updating the map size
func set_new_map_size(newWidth, newHeight):
M.xSize = newWidth
M.ySize = newHeight
oMapSizeTextLabel.text = str(M.xSize) + " x " + str(M.ySize)

# Function to get positions that need to be updated
func get_positions_to_update(newWidth, newHeight, previousWidth, previousHeight):
var positionsToUpdate = {}

# Handle width
if newWidth > previousWidth:
for x in range(previousWidth, newWidth):
for y in range(newHeight):
for y in newHeight:
positionsToUpdate[Vector2(x, y)] = true
# Handle height
if newHeight > previousHeight:
for y in range(previousHeight, newHeight):
for x in range(newWidth):
for x in newWidth:
positionsToUpdate[Vector2(x, y)] = true
oSlabPlacement.place_shape_of_slab_id(positionsToUpdate.keys(), Slabs.EARTH, 5)
return positionsToUpdate

# Function to remove old borders
func remove_old_borders(newWidth, newHeight, previousWidth, previousHeight):
var removeBorder = []
if newWidth > previousWidth:
for y in previousHeight:
removeBorder.append(Vector2(previousWidth - 1, y))
if newHeight > previousHeight:
for x in previousWidth:
removeBorder.append(Vector2(x, previousHeight - 1))
oSlabPlacement.place_shape_of_slab_id(removeBorder, Slabs.EARTH, 5)
return removeBorder

# Function to add new borders
func add_new_borders(newWidth, newHeight):
var addBorder = []
for x in newWidth:
addBorder.append(Vector2(x, 0))
addBorder.append(Vector2(x, newHeight - 1))
for y in newHeight:
addBorder.append(Vector2(0, y))
addBorder.append(Vector2(newWidth - 1, y))
oSlabPlacement.place_shape_of_slab_id(addBorder, Slabs.ROCK, 5)
return addBorder

# Function to remove instances outside of the new map size
func remove_outside_instances(newWidth, newHeight):
var deletedInstancesCount = 0
var newHeightInSubtiles = newHeight * 3
var newWidthInSubtiles = newWidth * 3

for instance in get_tree().get_nodes_in_group("Instance"):
if instance.locationX >= newWidthInSubtiles or instance.locationY >= newHeightInSubtiles:
deletedInstancesCount += 1
instance.queue_free()

if deletedInstancesCount > 0:
oMessage.quick("Deleted " + str(deletedInstancesCount) + " instances that were outside of the new map size.")

func update_editor_appearance():
oEditor.update_boundaries()
oOverheadOwnership.start()
oOverheadGraphics.update_map_overhead_2d_textures()

# Apply changes for added positions
var newlyAddedPositions = positionsToUpdate.keys()
oSlabPlacement.place_shape_of_slab_id(newlyAddedPositions, int(oResizeFillWithID.value), 5)
# The main function that calls all the helper functions
func _on_ResizeApplyButton_pressed():
var newWidth = int(oSettingsXSizeLine.text)
var newHeight = int(oSettingsYSizeLine.text)
var previousWidth = M.xSize
var previousHeight = M.ySize
set_new_map_size(newWidth, newHeight)
remove_outside_instances(newWidth, newHeight)

if oResizeMapApplyBorderCheckbox.pressed == true:
var borderPositions = []
for x in range(newWidth):
positionsToUpdate[Vector2(x, 0)] = true
positionsToUpdate[Vector2(x, newHeight - 1)] = true
borderPositions.append(Vector2(x, 0)) # Top border
borderPositions.append(Vector2(x, newHeight - 1)) # Bottom border
for y in range(newHeight):
positionsToUpdate[Vector2(0, y)] = true
positionsToUpdate[Vector2(newWidth - 1, y)] = true
borderPositions.append(Vector2(0, y)) # Left border
borderPositions.append(Vector2(newWidth - 1, y)) # Right border
oSlabPlacement.place_shape_of_slab_id(borderPositions, Slabs.ROCK, 5)
var positionsToUpdate = get_positions_to_update(newWidth, newHeight, previousWidth, previousHeight)
var removeBorder = remove_old_borders(newWidth, newHeight, previousWidth, previousHeight)
var addBorder = add_new_borders(newWidth, newHeight)
for pos in removeBorder:
positionsToUpdate[pos] = true
for pos in addBorder:
positionsToUpdate[pos] = true

set_various_grid_data(newWidth, newHeight, previousWidth, previousHeight)

update_editor_appearance()

# Finalize
oSlabPlacement.generate_slabs_based_on_id(positionsToUpdate.keys(), false)

func set_various_grid_data(newWidth, newHeight, previousWidth, previousHeight):
var newWidthInSubtiles = newWidth * 3
var newHeightInSubtiles = newHeight * 3
var prevWidthInSubtiles = previousWidth * 3
var prevHeightInSubtiles = previousHeight * 3

for x in prevWidthInSubtiles:
for y in prevHeightInSubtiles:
if x >= newWidthInSubtiles or y >= newHeightInSubtiles:
oDataClmPos.set_cell(x, y, 0)




func _on_SettingsXSizeLine_focus_exited():
if int(oSettingsXSizeLine.text) > 170:
oSettingsXSizeLine.text = "170"
Expand All @@ -74,6 +129,82 @@ func _on_SettingsYSizeLine_focus_exited():
if int(oSettingsYSizeLine.text) > 170:
oSettingsYSizeLine.text = "170"

func _on_ResizeFillWithID_value_changed(value):
value = int(value)
if Slabs.data.has(value):
oResizeFillWithIDLabel.text = Slabs.data[value][Slabs.NAME]



#func _on_ResizeApplyButton_pressed():
# var newWidth = int(oSettingsXSizeLine.text)
# var newHeight = int(oSettingsYSizeLine.text)
#
# var previousWidth = M.xSize
# var previousHeight = M.ySize
# M.xSize = newWidth
# M.ySize = newHeight
# oMapSizeTextLabel.text = str(M.xSize) + " x " + str(M.ySize)
#
# var positionsToUpdate = {}
#
# # Handle width
# if newWidth > previousWidth:
# for x in range(previousWidth, newWidth):
# for y in newHeight:
# positionsToUpdate[Vector2(x, y)] = true
# # Handle height
# if newHeight > previousHeight:
# for y in range(previousHeight, newHeight):
# for x in newWidth:
# positionsToUpdate[Vector2(x, y)] = true
#
# oEditor.update_boundaries()
# oOverheadOwnership.start()
# oOverheadGraphics.update_map_overhead_2d_textures()
#
# # Apply changes for added positions
# oSlabPlacement.place_shape_of_slab_id(positionsToUpdate.keys(), Slabs.EARTH, 5)
#
# var removeBorder = [] # Remove old south and east borders when enlarging the map
# if newWidth > previousWidth:
# for y in previousHeight:
# removeBorder.append(Vector2(previousWidth - 1, y))
# if newHeight > previousHeight:
# for x in previousWidth:
# removeBorder.append(Vector2(x, previousHeight - 1))
# oSlabPlacement.place_shape_of_slab_id(removeBorder, Slabs.EARTH, 5)
#
# var addBorder = []
# for x in newWidth:
# addBorder.append(Vector2(x, 0))
# addBorder.append(Vector2(x, newHeight - 1))
# for y in newHeight:
# addBorder.append(Vector2(0, y))
# addBorder.append(Vector2(newWidth - 1, y))
# oSlabPlacement.place_shape_of_slab_id(addBorder, Slabs.ROCK, 5)
#
# for pos in addBorder: # Update the appearance of any border alterations
# positionsToUpdate[pos] = true
# for pos in removeBorder: # Update the appearance of any border alterations
# positionsToUpdate[pos] = true
#
# # Remove instances outside of the new map size
# var instances = get_tree().get_nodes_in_group("Instance")
# var deletedInstancesCount = 0
# var newHeightInSubtiles = newHeight * 3
# var newWidthInSubtiles = newWidth * 3
# for instance in instances:
# if instance.locationX >= newWidthInSubtiles or instance.locationY >= newHeightInSubtiles:
# deletedInstancesCount+=1
# instance.queue_free()
# if deletedInstancesCount > 0:
# oMessage.quick("Deleted " + str(deletedInstancesCount) + " instances that were outside of the new map size.")
#
# # Finalize
# oSlabPlacement.generate_slabs_based_on_id(positionsToUpdate.keys(), false)



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

func _on_ResizeFillWithID_value_changed(value):
value = int(value)
if Slabs.data.has(value):
oResizeFillWithIDLabel.text = Slabs.data[value][Slabs.NAME]
2 changes: 1 addition & 1 deletion Scenes/SlabPlacement.gd
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ func place_shape_of_slab_id(shapePositionArray, slabID, ownership):
for i in removeFromShape:
shapePositionArray.erase(i)

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

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

func generate_slabs_based_on_id(shapePositionArray, updateNearby):
oOverheadOwnership.update_ownership_image_based_on_shape(shapePositionArray)
var CODETIME_START = OS.get_ticks_msec()

oEditor.mapHasBeenEdited = true
Expand Down

0 comments on commit 05b898c

Please sign in to comment.