From 742924fafc3ad5c585ab15c94e816f425c6cb8bf Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 13 Jun 2021 20:40:05 +0200 Subject: [PATCH] editor: can switch tile type --- IslandGenerator.tscn | 2 +- editor/Camera2D.gd | 22 ++++++ editor/GridLevel.gd | 9 +-- editor/IslandEditor.gd | 38 ++++++++-- editor/IslandEditor.tscn | 156 ++++++++++++++++++++++----------------- 5 files changed, 147 insertions(+), 80 deletions(-) create mode 100644 editor/Camera2D.gd diff --git a/IslandGenerator.tscn b/IslandGenerator.tscn index a31acba..68f2ad9 100644 --- a/IslandGenerator.tscn +++ b/IslandGenerator.tscn @@ -87,7 +87,7 @@ script = ExtResource( 1 ) [node name="blur" type="Sprite" parent="."] material = SubResource( 2 ) -scale = Vector2( 0.983894, 1 ) +scale = Vector2( 0.976278, 1 ) centered = false [node name="pixelate" type="Sprite" parent="."] diff --git a/editor/Camera2D.gd b/editor/Camera2D.gd new file mode 100644 index 0000000..628381c --- /dev/null +++ b/editor/Camera2D.gd @@ -0,0 +1,22 @@ +extends Camera2D + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +func ScreenToWorld (pos_screen: Vector2): + var camera_offset = OS.get_window_safe_area().size * 0.5 * zoom - transform.origin + return (pos_screen * zoom - camera_offset) + +func WorldToScreen (pos_world: Vector2): + pass + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass diff --git a/editor/GridLevel.gd b/editor/GridLevel.gd index eebb9b1..1acbee9 100644 --- a/editor/GridLevel.gd +++ b/editor/GridLevel.gd @@ -8,7 +8,7 @@ enum TileType { var HexGrid = preload("res://thirdparty/gdhexgrid/HexGrid.gd").new() -var size = [2,6] +var size = [40,60] var tile_size = 64 var HexPoints = null @@ -34,14 +34,14 @@ func clear(): for c in range (size[0]): tile_data.append(TileType.None) -func GetTile(c, r): +func GetTile(r, c): if r < 0 or r >= size[1] or c < 0 or c >= size[0]: print ("Invalid tile coord: ", r, ", ", c) return return tile_data[c * size[1] + r] -func SetTile(c, r, tile_type): +func SetTile(r, c, tile_type): if r < 0 or r >= size[1] or c < 0 or c >= size[0]: print ("Invalid tile coord: ", r, ", ", c) return @@ -91,8 +91,7 @@ func draw_grid(): var tile_type = GetTile(ri, ci) if tile_type == null: tile_type = TileType.None - else: - print ("moep") + var color_array = HexColors[tile_type] var tile_center = GetHexCenter(Vector2(ri, ci)) diff --git a/editor/IslandEditor.gd b/editor/IslandEditor.gd index b75ef15..2e1ec8b 100644 --- a/editor/IslandEditor.gd +++ b/editor/IslandEditor.gd @@ -9,25 +9,47 @@ var GridLevel var highlight_hex_coord = Vector2(0,0) -onready var screen_coords = get_node("HBoxContainer/Coords") -onready var hex_coords = get_node("HBoxContainer/GridCoords") +onready var camera = get_node("Camera2D") +onready var ui = get_node("Camera2D/UI") +onready var ScreenCoords = get_node("Camera2D/UI/ScreenCoords") +onready var HexCoords = get_node("Camera2D/UI/HexCoords") +onready var WorldCoords = get_node("Camera2D/UI/WorldCoords") +onready var TileType = get_node("Camera2D/UI/TileType") + # Called when the node enters the scene tree for the first time. func _ready(): GridLevel = get_node("GridLevel") + TileType.add_item("None", 0) + TileType.add_item("Sand", 1) + TileType.add_item("Grass", 2) update() +func _process(_delta): + var view_size = OS.get_window_safe_area().size + ui.rect_position = -view_size * 0.5 + update() -func _input(event): +func _unhandled_input(event): if 'position' in event: - screen_coords.text = str(event.position) - var coords = GridLevel.WorldToTileCoords (event.position) - hex_coords.text = str(coords) - highlight_hex_coord = GridLevel.GetHexCenter(coords) + ScreenCoords.text = str(event.position) + var world_coords = camera.ScreenToWorld(event.position) + WorldCoords.text = str(world_coords) + var hex_coords = GridLevel.WorldToTileCoords(world_coords) + + HexCoords.text = str(hex_coords) + highlight_hex_coord = GridLevel.GetHexCenter(hex_coords) if event is InputEventMouseButton: if event.button_index == BUTTON_LEFT and event.pressed: - GridLevel.SetTile (coords.x, coords.y, GridLevel.TileType.Sand) + GridLevel.SetTile (hex_coords.x, hex_coords.y, TileType.selected) + if event.button_index == BUTTON_WHEEL_DOWN and event.pressed: + camera.zoom = camera.zoom * 1.0 / 0.8 + if event.button_index == BUTTON_WHEEL_UP and event.pressed: + camera.zoom = camera.zoom * 0.8 + + if event is InputEventMouseButton and Input.is_mouse_button_pressed(BUTTON_MIDDLE): + camera.transform.origin = world_coords update() diff --git a/editor/IslandEditor.tscn b/editor/IslandEditor.tscn index 362bd6f..30c4925 100644 --- a/editor/IslandEditor.tscn +++ b/editor/IslandEditor.tscn @@ -1,89 +1,34 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://editor/GridLevel.gd" type="Script" id=1] [ext_resource path="res://editor/IslandEditor.gd" type="Script" id=2] [ext_resource path="res://editor/Area2D.gd" type="Script" id=3] +[ext_resource path="res://editor/Camera2D.gd" type="Script" id=4] [node name="Editor" type="Node2D"] script = ExtResource( 2 ) -[node name="HBoxContainer" type="HBoxContainer" parent="."] -margin_right = 40.0 -margin_bottom = 40.0 -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="New" type="Button" parent="HBoxContainer"] -margin_right = 44.0 -margin_bottom = 40.0 -text = "Clear" - -[node name="SpinBox" type="SpinBox" parent="HBoxContainer"] -margin_left = 48.0 -margin_right = 122.0 -margin_bottom = 40.0 -min_value = 1.0 -max_value = 32.0 -value = 1.0 - -[node name="OptionButton" type="OptionButton" parent="HBoxContainer"] -margin_left = 126.0 -margin_right = 163.0 -margin_bottom = 40.0 -text = "0" -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="CoordsLabel" type="Label" parent="HBoxContainer"] -margin_left = 167.0 -margin_top = 13.0 -margin_right = 210.0 -margin_bottom = 27.0 -text = "Screen" - -[node name="Coords" type="Label" parent="HBoxContainer"] -margin_left = 214.0 -margin_top = 13.0 -margin_right = 234.0 -margin_bottom = 27.0 -text = "0,0" - -[node name="GridCoordsLabel" type="Label" parent="HBoxContainer"] -margin_left = 238.0 -margin_top = 13.0 -margin_right = 263.0 -margin_bottom = 27.0 -text = "Hex" -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="GridCoords" type="Label" parent="HBoxContainer"] -margin_left = 267.0 -margin_top = 13.0 -margin_right = 287.0 -margin_bottom = 27.0 -text = "0,0" - [node name="GridLevel" type="Node2D" parent="."] z_index = -2 script = ExtResource( 1 ) -[node name="Area2D" type="Node2D" parent="."] +[node name="Camera2D" type="Camera2D" parent="."] +current = true +script = ExtResource( 4 ) + +[node name="Area2D" type="Node2D" parent="Camera2D"] script = ExtResource( 3 ) -[node name="Highlight" type="Node2D" parent="Area2D"] +[node name="Highlight" type="Node2D" parent="Camera2D/Area2D"] -[node name="Highlight" type="Label" parent="Area2D/Highlight"] +[node name="Highlight" type="Label" parent="Camera2D/Area2D/Highlight"] margin_top = 13.0 margin_bottom = 27.0 __meta__ = { "_edit_use_anchors_": false } -[node name="AreaCoords" type="Label" parent="Area2D/Highlight"] +[node name="AreaCoords" type="Label" parent="Camera2D/Area2D/Highlight"] margin_left = 10.8379 margin_top = 34.1084 margin_right = 29.8379 @@ -92,7 +37,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="HexCoords" type="Label" parent="Area2D/Highlight"] +[node name="HexCoords" type="Label" parent="Camera2D/Area2D/Highlight"] margin_left = 8.0 margin_top = 13.0 margin_right = 27.0 @@ -101,3 +46,82 @@ rect_pivot_offset = Vector2( -534.527, -13 ) __meta__ = { "_edit_use_anchors_": false } + +[node name="UI" type="HBoxContainer" parent="Camera2D"] +margin_right = 287.0 +margin_bottom = 40.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="New" type="Button" parent="Camera2D/UI"] +margin_right = 44.0 +margin_bottom = 40.0 +text = "Clear" + +[node name="SpinBox" type="SpinBox" parent="Camera2D/UI"] +margin_left = 48.0 +margin_right = 122.0 +margin_bottom = 40.0 +min_value = 1.0 +max_value = 32.0 +value = 1.0 + +[node name="TileType" type="OptionButton" parent="Camera2D/UI"] +margin_left = 126.0 +margin_right = 155.0 +margin_bottom = 40.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="ScreenCoordsLabel" type="Label" parent="Camera2D/UI"] +margin_left = 159.0 +margin_top = 13.0 +margin_right = 202.0 +margin_bottom = 27.0 +text = "Screen" + +[node name="ScreenCoords" type="Label" parent="Camera2D/UI"] +margin_left = 206.0 +margin_top = 13.0 +margin_right = 226.0 +margin_bottom = 27.0 +text = "0,0" + +[node name="HexCoordsLabel" type="Label" parent="Camera2D/UI"] +margin_left = 230.0 +margin_top = 13.0 +margin_right = 255.0 +margin_bottom = 27.0 +text = "Hex" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="HexCoords" type="Label" parent="Camera2D/UI"] +margin_left = 259.0 +margin_top = 13.0 +margin_right = 279.0 +margin_bottom = 27.0 +text = "0,0" + +[node name="WorldCoordsLabel" type="Label" parent="Camera2D/UI"] +margin_left = 283.0 +margin_top = 13.0 +margin_right = 321.0 +margin_bottom = 27.0 +text = "World" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="WorldCoords" type="Label" parent="Camera2D/UI"] +margin_left = 325.0 +margin_top = 13.0 +margin_right = 345.0 +margin_bottom = 27.0 +text = "0,0" +__meta__ = { +"_edit_use_anchors_": false +}