Refactor a lot of duplicated code
This commit is contained in:
parent
da43ba87e2
commit
b403ea7246
1 changed files with 93 additions and 53 deletions
|
@ -12,10 +12,11 @@ const TileTabScene := preload("res://Scenes/Editor/TileTab.tscn")
|
|||
|
||||
const objects = {
|
||||
"Machines": [
|
||||
{
|
||||
"scene": preload("res://Actors/Objects/Computer/Computer.tscn"),
|
||||
"script": GameObjectComputer
|
||||
}
|
||||
GameObjectComputer,
|
||||
GameObjectDoor,
|
||||
GameObjectEngine,
|
||||
GameObjectLightbulb,
|
||||
GameObjectPowerStorage
|
||||
]
|
||||
}
|
||||
|
||||
|
@ -35,6 +36,16 @@ enum PlacingMode {
|
|||
OBJECT
|
||||
}
|
||||
|
||||
enum Tool {
|
||||
NONE
|
||||
TILE_FREEHAND,
|
||||
TILE_RECT,
|
||||
TILE_FILL,
|
||||
OBJ_PLACE,
|
||||
OBJ_EDIT,
|
||||
OBJ_REMOVE
|
||||
}
|
||||
|
||||
# Prevent input handler from running when other dialogs/actions are focused
|
||||
var input_lock := false
|
||||
|
||||
|
@ -49,8 +60,7 @@ var deleting := false
|
|||
var placing_mode = PlacingMode.NONE
|
||||
var placing_layer = null
|
||||
var placing_tile_id := -1
|
||||
var current_brush := "none"
|
||||
var current_obj_action := "none"
|
||||
var current_tool = Tool.NONE
|
||||
|
||||
# Cursor variables
|
||||
var cursor_pos := Vector2.ZERO
|
||||
|
@ -74,25 +84,17 @@ func _input(ev: InputEvent):
|
|||
var old_pos = cursor_pos
|
||||
cursor_pos = new_cursor_pos
|
||||
if placing or deleting:
|
||||
handle_held_cursor_move(old_pos, new_cursor_pos)
|
||||
if current_tool != Tool.NONE:
|
||||
tile_mouse_move(old_pos, cursor_pos)
|
||||
cursor.position = cursor_pos * TILE_SIZE
|
||||
|
||||
if ev is InputEventMouseButton:
|
||||
var mouse := ev as InputEventMouseButton
|
||||
if mouse.pressed:
|
||||
match ev.button_index:
|
||||
BUTTON_LEFT:
|
||||
if current_brush == "freehand":
|
||||
if placing_layer != null:
|
||||
place_tiles(placing_layer, [cursor_pos], placing_tile_id)
|
||||
placing = true
|
||||
pressed_pos = cursor_pos
|
||||
BUTTON_RIGHT:
|
||||
if current_brush == "freehand":
|
||||
if placing_layer != null:
|
||||
place_tiles(placing_layer, [cursor_pos], -1)
|
||||
pressed_pos = cursor_pos
|
||||
deleting = true
|
||||
BUTTON_LEFT, BUTTON_RIGHT:
|
||||
if current_tool != Tool.NONE:
|
||||
tile_pressed(ev.button_index, cursor_pos)
|
||||
BUTTON_WHEEL_UP:
|
||||
# Zoom in
|
||||
var old_scale = map_node.scale
|
||||
|
@ -117,36 +119,13 @@ func _input(ev: InputEvent):
|
|||
dragging = true
|
||||
else:
|
||||
match ev.button_index:
|
||||
BUTTON_LEFT:
|
||||
match current_brush:
|
||||
"rect":
|
||||
place_rect(pressed_pos, cursor_pos, placing_tile_id)
|
||||
"fill":
|
||||
if placing_layer != null:
|
||||
flood_fill(placing_layer, cursor_pos, placing_tile_id)
|
||||
placing = false
|
||||
BUTTON_RIGHT:
|
||||
match current_brush:
|
||||
"rect":
|
||||
place_rect(pressed_pos, cursor_pos, -1)
|
||||
"fill":
|
||||
if placing_layer != null:
|
||||
flood_fill(placing_layer, cursor_pos, -1)
|
||||
deleting = false
|
||||
BUTTON_LEFT, BUTTON_RIGHT:
|
||||
if current_tool != Tool.NONE:
|
||||
tile_released(ev.button_index, cursor_pos)
|
||||
BUTTON_MIDDLE:
|
||||
dragging = false
|
||||
map_node.global_position = view_origin - (mouse_origin - ev.global_position)
|
||||
|
||||
func handle_held_cursor_move(old_pos: Vector2, new_pos: Vector2):
|
||||
match current_brush:
|
||||
"freehand":
|
||||
var id = placing_tile_id
|
||||
# If deleting, null tile instead
|
||||
if deleting:
|
||||
id = -1
|
||||
if placing_layer != null:
|
||||
place_tiles(placing_layer, [new_pos], id)
|
||||
|
||||
func place_rect(a: Vector2, b: Vector2, id: int):
|
||||
if placing_layer == null:
|
||||
return
|
||||
|
@ -205,7 +184,7 @@ func add_object_list(name: String, objects: Array):
|
|||
tab.connect("item_selected", self, "_item_selected", [tab])
|
||||
tab.name = name
|
||||
for obj in objects:
|
||||
var editor_info = obj.script.editor_info()
|
||||
var editor_info = obj.editor_info()
|
||||
tab.add_item(editor_info.name, editor_info.icon)
|
||||
tab.set_item_metadata(tab.get_item_count()-1, editor_info)
|
||||
$objects/tabs.add_child(tab)
|
||||
|
@ -245,21 +224,37 @@ onready var obj_action_panel = $tools/objPanel
|
|||
func _tool_selected(tool_type):
|
||||
layers_panel.visible = false
|
||||
brush_panel.visible = false
|
||||
placing_mode = PlacingMode.NONE
|
||||
objects_panel.visible = false
|
||||
obj_action_panel.visible = false
|
||||
placing_mode = PlacingMode.NONE
|
||||
match tool_type:
|
||||
"tile":
|
||||
layers_panel.visible = current_brush != "none"
|
||||
layers_panel.visible = tool_type(current_tool) == "brush"
|
||||
brush_panel.visible = true
|
||||
placing_mode = PlacingMode.TILEMAP
|
||||
"obj":
|
||||
obj_action_panel.visible = true
|
||||
objects_panel.visible = current_obj_action == "add"
|
||||
objects_panel.visible = tool_type(current_tool) == "obj"
|
||||
placing_mode = PlacingMode.OBJECT
|
||||
|
||||
func _set_brush(brush_type: String):
|
||||
current_brush = brush_type
|
||||
layers_panel.visible = current_brush != "none"
|
||||
match brush_type:
|
||||
"freehand":
|
||||
current_tool = Tool.TILE_FREEHAND
|
||||
"rect":
|
||||
current_tool = Tool.TILE_RECT
|
||||
"fill":
|
||||
current_tool = Tool.TILE_FILL
|
||||
layers_panel.visible = true
|
||||
|
||||
func tool_type(brush):
|
||||
match brush:
|
||||
Tool.TILE_FILL, Tool.TILE_FREEHAND, Tool.TILE_RECT:
|
||||
return "brush"
|
||||
Tool.OBJ_EDIT, Tool.OBJ_PLACE, Tool.OBJ_REMOVE:
|
||||
return "obj"
|
||||
_:
|
||||
return "none"
|
||||
|
||||
var ff_cells = {}
|
||||
var ff_origin = Vector2.ZERO
|
||||
|
@ -307,5 +302,50 @@ func flood_fill(tilemap: TileMap, pos: Vector2, id: int):
|
|||
tilemap.update_bitmask_region()
|
||||
|
||||
func _set_obj_action(action):
|
||||
current_obj_action = action
|
||||
objects_panel.visible = action == "add"
|
||||
match action:
|
||||
"add":
|
||||
current_tool = Tool.OBJ_PLACE
|
||||
"edit":
|
||||
current_tool = Tool.OBJ_EDIT
|
||||
"remove":
|
||||
current_tool = Tool.OBJ_REMOVE
|
||||
objects_panel.visible = true
|
||||
|
||||
func tile_pressed(button: int, pos: Vector2):
|
||||
match button:
|
||||
BUTTON_LEFT:
|
||||
placing = true
|
||||
BUTTON_RIGHT:
|
||||
deleting = true
|
||||
match current_tool:
|
||||
Tool.TILE_FREEHAND:
|
||||
if placing_layer != null:
|
||||
place_tiles(placing_layer, [pos], get_active_tile())
|
||||
Tool.OBJ_PLACE:
|
||||
pass
|
||||
pressed_pos = pos
|
||||
|
||||
func tile_released(button: int, pos: Vector2):
|
||||
match current_tool:
|
||||
Tool.TILE_RECT:
|
||||
if placing_layer != null:
|
||||
place_rect(pressed_pos, cursor_pos, get_active_tile())
|
||||
Tool.TILE_FILL:
|
||||
if placing_layer != null:
|
||||
flood_fill(placing_layer, cursor_pos, get_active_tile())
|
||||
match button:
|
||||
BUTTON_LEFT:
|
||||
placing = false
|
||||
BUTTON_RIGHT:
|
||||
deleting = false
|
||||
|
||||
func tile_mouse_move(from: Vector2, to: Vector2):
|
||||
match current_tool:
|
||||
Tool.TILE_FREEHAND:
|
||||
if placing_layer != null:
|
||||
place_tiles(placing_layer, [to], get_active_tile())
|
||||
|
||||
func get_active_tile():
|
||||
if deleting:
|
||||
return -1
|
||||
return placing_tile_id
|
||||
|
|
Reference in a new issue