modules: add sections
This commit is contained in:
parent
77490fc067
commit
0405c159ac
18 changed files with 629 additions and 199 deletions
132
data/buildings.json
Normal file
132
data/buildings.json
Normal file
|
@ -0,0 +1,132 @@
|
|||
[
|
||||
{
|
||||
"group": "Электричество",
|
||||
"children": [
|
||||
{
|
||||
"id": 1,
|
||||
"label": "Турбинный конденсатор",
|
||||
"energy": 180
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"label": "Химическая камера сгорания",
|
||||
"energy": 600
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Добыча",
|
||||
"children": [
|
||||
{
|
||||
"id": 101,
|
||||
"label": "Жерловой конденсатор",
|
||||
"energy": -30
|
||||
},
|
||||
{
|
||||
"id": 102,
|
||||
"label": "Дробитель скал",
|
||||
"energy": -11
|
||||
},
|
||||
{
|
||||
"id": 103,
|
||||
"label": "Плазменный бур",
|
||||
"energy": -9
|
||||
},
|
||||
{
|
||||
"id": 104,
|
||||
"label": "Ударная дрель",
|
||||
"energy": -160
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Заводы",
|
||||
"children": [
|
||||
{
|
||||
"id": 201,
|
||||
"label": "Кремнивая дуговая печь",
|
||||
"energy": -360
|
||||
},
|
||||
{
|
||||
"id": 202,
|
||||
"label": "Электролизер",
|
||||
"energy": -60
|
||||
},
|
||||
{
|
||||
"id": 203,
|
||||
"label": "Окислительная камера",
|
||||
"energy": -30
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Техника",
|
||||
"children": [
|
||||
{
|
||||
"id": 301,
|
||||
"label": "Грузовая катапульта",
|
||||
"energy": -30
|
||||
},
|
||||
{
|
||||
"id": 302,
|
||||
"label": "Грузовой разгрузчик",
|
||||
"energy": -120
|
||||
},
|
||||
{
|
||||
"id": 303,
|
||||
"label": "Конструктор",
|
||||
"energy": -120
|
||||
},
|
||||
{
|
||||
"id": 310,
|
||||
"label": "Деконструктор",
|
||||
"energy": -60
|
||||
},
|
||||
{
|
||||
"id": 304,
|
||||
"label": "Конструктор танков",
|
||||
"energy": -120
|
||||
},
|
||||
{
|
||||
"id": 305,
|
||||
"label": "Рефабрикатор танков",
|
||||
"energy": -180
|
||||
},
|
||||
{
|
||||
"id": 306,
|
||||
"label": "Конструктор кораблей",
|
||||
"energy": -120
|
||||
},
|
||||
{
|
||||
"id": 309,
|
||||
"label": "Рефабрикатор кораблей",
|
||||
"energy": -150
|
||||
},
|
||||
{
|
||||
"id": 307,
|
||||
"label": "Конструктор мехов",
|
||||
"energy": -120
|
||||
},
|
||||
{
|
||||
"id": 308,
|
||||
"label": "Рефабрикатор мехов",
|
||||
"energy": -150
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"group": "Разное",
|
||||
"children": [
|
||||
{
|
||||
"id": 401,
|
||||
"label": "Радар",
|
||||
"energy": -36
|
||||
},
|
||||
{
|
||||
"id": 402,
|
||||
"label": "Регенерирующий проектор",
|
||||
"energy": -60
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
|
@ -11,8 +11,8 @@ config_version=5
|
|||
[application]
|
||||
|
||||
config/name="MindustryTools"
|
||||
config/version="0.0.1"
|
||||
run/main_scene="res://src/energy_table.tscn"
|
||||
config/version="0.0.2"
|
||||
run/main_scene="res://src/main.tscn"
|
||||
config/features=PackedStringArray("4.2", "GL Compatibility")
|
||||
boot_splash/show_image=false
|
||||
boot_splash/fullsize=false
|
||||
|
|
|
@ -1,102 +0,0 @@
|
|||
extends Control
|
||||
|
||||
const table_row = preload("res://src/energy_table_row.tscn")
|
||||
@onready var table_body = $VBoxContainer/TableBody/VBoxContainer
|
||||
@onready var total_energy_label = $VBoxContainer/TableSummary/HBoxContainer/TotalEnergyLabel
|
||||
|
||||
var data = []
|
||||
|
||||
|
||||
func render_total_energy_label():
|
||||
var total_energy = 0
|
||||
for data_row in data:
|
||||
total_energy += data_row["instance"].total_energy
|
||||
|
||||
total_energy_label.text = str(total_energy)
|
||||
|
||||
|
||||
func add_row(data_row: Dictionary):
|
||||
data.append(data_row)
|
||||
|
||||
var instance = table_row.instantiate()
|
||||
if data_row.has("building_id"):
|
||||
instance.building_id = data_row["building_id"]
|
||||
if data_row.has("building_count"):
|
||||
instance.building_count = data_row["building_count"]
|
||||
table_body.add_child(instance)
|
||||
instance.add_to_group("Persist")
|
||||
instance.total_energy_changed.connect(_on_total_energy_changed)
|
||||
instance.deleted.connect(_on_table_row_deleted)
|
||||
data_row["instance"] = instance
|
||||
|
||||
|
||||
func del_row(node: Node):
|
||||
table_body.remove_child(node)
|
||||
for i in range(0, data.size()):
|
||||
var data_row = data[i]
|
||||
if data_row["instance"] == node:
|
||||
data.pop_at(i)
|
||||
break
|
||||
|
||||
|
||||
func load_data():
|
||||
if not FileAccess.file_exists("user://energy_table.save"):
|
||||
return
|
||||
|
||||
var save_nodes = get_tree().get_nodes_in_group("Persist")
|
||||
for node in save_nodes:
|
||||
del_row(node)
|
||||
|
||||
var save_data_file = FileAccess.open("user://energy_table.save", FileAccess.READ)
|
||||
while save_data_file.get_position() < save_data_file.get_length():
|
||||
var json_string = save_data_file.get_line()
|
||||
|
||||
var json = JSON.new()
|
||||
var parse_result = json.parse(json_string)
|
||||
if not parse_result == OK:
|
||||
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
|
||||
continue
|
||||
|
||||
var node_data = json.get_data()
|
||||
add_row(node_data)
|
||||
|
||||
|
||||
func save_data():
|
||||
var save_data_file = FileAccess.open("user://energy_table.save", FileAccess.WRITE)
|
||||
var save_nodes = get_tree().get_nodes_in_group("Persist")
|
||||
for node in save_nodes:
|
||||
if node.scene_file_path.is_empty():
|
||||
print("persistent node '%s' is not an instanced scene, skipped" % node.name)
|
||||
continue
|
||||
if !node.has_method("get_save_data"):
|
||||
print("persistent node '%s' is missing a get_save_data() function, skipped" % node.name)
|
||||
continue
|
||||
|
||||
var node_data = node.call("get_save_data")
|
||||
var json_string = JSON.stringify(node_data)
|
||||
save_data_file.store_line(json_string)
|
||||
|
||||
|
||||
func _ready():
|
||||
# Add initial empty row
|
||||
load_data()
|
||||
if data.is_empty():
|
||||
add_row({})
|
||||
|
||||
render_total_energy_label()
|
||||
|
||||
|
||||
func _on_total_energy_changed(_value: int):
|
||||
render_total_energy_label()
|
||||
save_data()
|
||||
|
||||
|
||||
func _on_table_row_deleted(instance: Node):
|
||||
del_row(instance)
|
||||
render_total_energy_label()
|
||||
save_data()
|
||||
|
||||
|
||||
func _on_add_building_button_pressed():
|
||||
add_row({})
|
||||
|
|
@ -1,78 +0,0 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://cbegq3jfbfb6t"]
|
||||
|
||||
[ext_resource type="Script" path="res://src/energy_table.gd" id="1_vq16x"]
|
||||
|
||||
[node name="EnergyTable" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_vq16x")
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 1
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="TableHeader" type="PanelContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/TableHeader"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/TableHeader/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "Название"
|
||||
|
||||
[node name="Label3" type="Label" parent="VBoxContainer/TableHeader/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "Кол-во ед."
|
||||
|
||||
[node name="Label4" type="Label" parent="VBoxContainer/TableHeader/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "Энергия/сек"
|
||||
|
||||
[node name="TableBody" type="PanelContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/TableBody"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 3
|
||||
|
||||
[node name="TableSummary" type="PanelContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/TableSummary"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer/TableSummary/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "ИТОГО"
|
||||
|
||||
[node name="Label3" type="Label" parent="VBoxContainer/TableSummary/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="TotalEnergyLabel" type="Label" parent="VBoxContainer/TableSummary/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "0"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="AddBuildingButton" type="Button" parent="VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Добавить здание"
|
||||
|
||||
[connection signal="pressed" from="VBoxContainer/HBoxContainer/AddBuildingButton" to="." method="_on_add_building_button_pressed"]
|
16
src/main.tscn
Normal file
16
src/main.tscn
Normal file
|
@ -0,0 +1,16 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://c1lluapm35rrk"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://l06mevybbnrs" path="res://src/modules/building/energy_calculator/energy_calculator.tscn" id="1_fc644"]
|
||||
|
||||
[node name="Main" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 3
|
||||
|
||||
[node name="EnergyCalculator" parent="." instance=ExtResource("1_fc644")]
|
||||
layout_mode = 1
|
|
@ -1,9 +1,12 @@
|
|||
class_name BuildingOption
|
||||
extends Control
|
||||
|
||||
const buildings_json = preload("res://src/buildings.tres")
|
||||
const buildings_json = preload("res://src/modules/building/building_option/buildings.tres")
|
||||
|
||||
|
||||
signal selected(item: BuildingOptionItem)
|
||||
|
||||
@export var selected_id: int
|
||||
@onready var option_button: OptionButton = $OptionButton;
|
||||
|
||||
var selected_option: BuildingOptionItem = null
|
||||
|
@ -16,7 +19,6 @@ func select(selected_id: int):
|
|||
if option.id == selected_id:
|
||||
selected_option = option
|
||||
selected.emit(option)
|
||||
|
||||
|
||||
func _ready():
|
||||
for building_group in buildings_json.data:
|
||||
|
@ -27,6 +29,9 @@ func _ready():
|
|||
option_button.add_item("{label} ({energy} ⚡)".format(building), building.id)
|
||||
|
||||
option_button.selected = -1
|
||||
|
||||
if selected_id != null:
|
||||
select(selected_id)
|
||||
|
||||
|
||||
func _on_option_button_item_selected(index):
|
|
@ -1,10 +1,10 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://bedenbuamujfg"]
|
||||
|
||||
[ext_resource type="Script" path="res://src/building_option.gd" id="1_m7cva"]
|
||||
[ext_resource type="Script" path="res://src/modules/building/building_option/building_option.gd" id="1_3i1uo"]
|
||||
|
||||
[node name="BuildingOption" type="HBoxContainer"]
|
||||
size_flags_horizontal = 3
|
||||
script = ExtResource("1_m7cva")
|
||||
script = ExtResource("1_3i1uo")
|
||||
|
||||
[node name="OptionButton" type="OptionButton" parent="."]
|
||||
layout_mode = 2
|
|
@ -1,4 +1,4 @@
|
|||
[gd_resource type="JSON" format=3 uid="uid://demufg6yeo1b6"]
|
||||
[gd_resource type="JSON" format=3 uid="uid://b56jfwbi10ywc"]
|
||||
|
||||
[resource]
|
||||
data = [{
|
||||
|
@ -6,6 +6,10 @@ data = [{
|
|||
"energy": 180.0,
|
||||
"id": 1.0,
|
||||
"label": "Турбинный конденсатор"
|
||||
}, {
|
||||
"energy": 600.0,
|
||||
"id": 2.0,
|
||||
"label": "Химическая камера сгорания"
|
||||
}],
|
||||
"group": "Электричество"
|
||||
}, {
|
||||
|
@ -36,6 +40,10 @@ data = [{
|
|||
"energy": -60.0,
|
||||
"id": 202.0,
|
||||
"label": "Электролизер"
|
||||
}, {
|
||||
"energy": -30.0,
|
||||
"id": 203.0,
|
||||
"label": "Окислительная камера"
|
||||
}],
|
||||
"group": "Заводы"
|
||||
}, {
|
||||
|
@ -52,6 +60,10 @@ data = [{
|
|||
"id": 303.0,
|
||||
"label": "Конструктор"
|
||||
}, {
|
||||
"energy": -60.0,
|
||||
"id": 310.0,
|
||||
"label": "Деконструктор"
|
||||
}, {
|
||||
"energy": -120.0,
|
||||
"id": 304.0,
|
||||
"label": "Конструктор танков"
|
||||
|
@ -64,6 +76,10 @@ data = [{
|
|||
"id": 306.0,
|
||||
"label": "Конструктор кораблей"
|
||||
}, {
|
||||
"energy": -150.0,
|
||||
"id": 309.0,
|
||||
"label": "Рефабрикатор кораблей"
|
||||
}, {
|
||||
"energy": -120.0,
|
||||
"id": 307.0,
|
||||
"label": "Конструктор мехов"
|
||||
|
@ -78,6 +94,10 @@ data = [{
|
|||
"energy": -36.0,
|
||||
"id": 401.0,
|
||||
"label": "Радар"
|
||||
}, {
|
||||
"energy": -60.0,
|
||||
"id": 402.0,
|
||||
"label": "Регенерирующий проектор"
|
||||
}],
|
||||
"group": "Разное"
|
||||
}]
|
116
src/modules/building/energy_calculator/energy_calculator.gd
Normal file
116
src/modules/building/energy_calculator/energy_calculator.gd
Normal file
|
@ -0,0 +1,116 @@
|
|||
extends MarginContainer
|
||||
|
||||
@export var initial_sections_count: int = 1
|
||||
|
||||
var sections_container: VBoxContainer
|
||||
|
||||
const energy_section_node = preload("res://src/modules/building/energy_calculator/energy_section.tscn")
|
||||
|
||||
var placeholder_id = 0;
|
||||
var sections: Array[EnergySection] = []
|
||||
|
||||
|
||||
func del_section(node: Node):
|
||||
for i in range(0, sections.size()):
|
||||
var section = sections[i]
|
||||
if section == node:
|
||||
sections_container.remove_child(node)
|
||||
sections.pop_at(i)
|
||||
break
|
||||
|
||||
|
||||
func _on_sections_deleted(section_node: Node):
|
||||
del_section(section_node)
|
||||
|
||||
|
||||
func save_data():
|
||||
var save_data_file = FileAccess.open("user://energy_table.save", FileAccess.WRITE)
|
||||
var sections_data = sections.map(func get_saved_data(section): return section.get_save_data())
|
||||
var json_string = JSON.stringify(sections_data)
|
||||
save_data_file.store_string(json_string)
|
||||
|
||||
|
||||
func _on_energy_section_changed():
|
||||
save_data()
|
||||
|
||||
|
||||
func add_section(data_section: Dictionary):
|
||||
var instance = energy_section_node.instantiate()
|
||||
|
||||
placeholder_id += 1
|
||||
if data_section.has("title_placeholder") and data_section.title_placeholder:
|
||||
instance.title_placeholder = data_section.title_placeholder
|
||||
else:
|
||||
instance.title_placeholder = "Ядро %s" % placeholder_id
|
||||
|
||||
if data_section.has("title"):
|
||||
instance.title = data_section.title
|
||||
|
||||
if data_section.has("rows"):
|
||||
for row in data_section.rows:
|
||||
var d = EnergyTableRowData.new()
|
||||
d.building_id = row.building_id
|
||||
d.building_count = row.building_count
|
||||
instance.rows_data.append(d)
|
||||
|
||||
sections_container.add_child(instance)
|
||||
|
||||
instance.add_theme_constant_override("margin_bottom", 9)
|
||||
instance.add_to_group("Persist")
|
||||
instance.deleted.connect(_on_sections_deleted)
|
||||
instance.changed.connect(_on_energy_section_changed)
|
||||
sections.append(instance)
|
||||
|
||||
|
||||
func load_data():
|
||||
if not FileAccess.file_exists("user://energy_table.save"):
|
||||
return
|
||||
|
||||
for section in sections:
|
||||
del_section(section)
|
||||
|
||||
var save_data_file = FileAccess.open("user://energy_table.save", FileAccess.READ)
|
||||
var file_content = save_data_file.get_as_text()
|
||||
|
||||
print(file_content)
|
||||
|
||||
var json = JSON.new()
|
||||
var parse_result = json.parse(file_content)
|
||||
|
||||
var sections_data: Array = []
|
||||
|
||||
if parse_result == OK:
|
||||
# TODO: check array
|
||||
sections_data = json.get_data()
|
||||
else:
|
||||
save_data_file.seek(0)
|
||||
var section_rows: Array[Dictionary] = []
|
||||
while save_data_file.get_position() < save_data_file.get_length():
|
||||
var json_string = save_data_file.get_line()
|
||||
|
||||
json = JSON.new()
|
||||
parse_result = json.parse(json_string)
|
||||
if not parse_result == OK:
|
||||
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
|
||||
continue
|
||||
|
||||
var node_data = json.get_data()
|
||||
section_rows.append(node_data)
|
||||
|
||||
sections_data.append({ "rows": section_rows })
|
||||
|
||||
for data in sections_data:
|
||||
add_section(data)
|
||||
|
||||
|
||||
func _enter_tree():
|
||||
sections_container = $PanelContainer/MarginContainer/ScrollContainer/VBoxContainer/SectionsVBoxContainer
|
||||
|
||||
load_data()
|
||||
if sections.size() < initial_sections_count:
|
||||
add_section({})
|
||||
|
||||
|
||||
func _on_add_section_button_pressed():
|
||||
add_section({})
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://l06mevybbnrs"]
|
||||
|
||||
[ext_resource type="Script" path="res://src/modules/building/energy_calculator/energy_calculator.gd" id="1_0vw5l"]
|
||||
|
||||
[node name="EnergyCalculator" type="MarginContainer"]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_0vw5l")
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="."]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="PanelContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_left = 9
|
||||
theme_override_constants/margin_top = 9
|
||||
theme_override_constants/margin_right = 9
|
||||
theme_override_constants/margin_bottom = 9
|
||||
|
||||
[node name="ScrollContainer" type="ScrollContainer" parent="PanelContainer/MarginContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
follow_focus = true
|
||||
horizontal_scroll_mode = 0
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer/ScrollContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="SectionsVBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer/ScrollContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/ScrollContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="AddSectionButton" type="Button" parent="PanelContainer/MarginContainer/ScrollContainer/VBoxContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Добавить секцию"
|
||||
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/ScrollContainer/VBoxContainer/HBoxContainer/AddSectionButton" to="." method="_on_add_section_button_pressed"]
|
34
src/modules/building/energy_calculator/energy_section.gd
Normal file
34
src/modules/building/energy_calculator/energy_section.gd
Normal file
|
@ -0,0 +1,34 @@
|
|||
class_name EnergySection
|
||||
extends MarginContainer
|
||||
|
||||
signal changed()
|
||||
signal deleted(section_node: Node)
|
||||
|
||||
@export var title: String
|
||||
@export var title_placeholder: String
|
||||
@export var rows_data: Array[EnergyTableRowData]
|
||||
|
||||
var table: EnergyTable
|
||||
|
||||
|
||||
func _enter_tree():
|
||||
table = $HBoxContainer/EnergyTable
|
||||
table.title = title
|
||||
table.title_placeholder = title_placeholder
|
||||
table.rows_data = rows_data
|
||||
|
||||
|
||||
func _ready():
|
||||
pass
|
||||
|
||||
|
||||
func _on_delete_section_button_pressed():
|
||||
deleted.emit(self)
|
||||
|
||||
|
||||
func get_save_data() -> Dictionary:
|
||||
return table.get_save_data()
|
||||
|
||||
|
||||
func _on_energy_table_changed():
|
||||
changed.emit()
|
33
src/modules/building/energy_calculator/energy_section.tscn
Normal file
33
src/modules/building/energy_calculator/energy_section.tscn
Normal file
|
@ -0,0 +1,33 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://c0vxh1gmo45mv"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cbegq3jfbfb6t" path="res://src/modules/building/energy_calculator/energy_table.tscn" id="1_dbrxy"]
|
||||
[ext_resource type="Script" path="res://src/modules/building/energy_calculator/energy_section.gd" id="1_qbo72"]
|
||||
|
||||
[node name="EnergySection" type="MarginContainer"]
|
||||
anchors_preset = 10
|
||||
anchor_right = 1.0
|
||||
offset_bottom = 89.0
|
||||
grow_horizontal = 2
|
||||
script = ExtResource("1_qbo72")
|
||||
title_placeholder = "Ядро 1"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="."]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="EnergyTable" parent="HBoxContainer" instance=ExtResource("1_dbrxy")]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"]
|
||||
layout_mode = 2
|
||||
alignment = 1
|
||||
|
||||
[node name="DeleteSectionButton" type="Button" parent="HBoxContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
tooltip_text = "Удалить секцию"
|
||||
theme_override_colors/font_color = Color(1, 0.215686, 0.137255, 1)
|
||||
text = "✖"
|
||||
flat = true
|
||||
|
||||
[connection signal="changed" from="HBoxContainer/EnergyTable" to="." method="_on_energy_table_changed"]
|
||||
[connection signal="pressed" from="HBoxContainer/VBoxContainer/DeleteSectionButton" to="." method="_on_delete_section_button_pressed"]
|
106
src/modules/building/energy_calculator/energy_table.gd
Normal file
106
src/modules/building/energy_calculator/energy_table.gd
Normal file
|
@ -0,0 +1,106 @@
|
|||
class_name EnergyTable
|
||||
extends Control
|
||||
|
||||
signal changed()
|
||||
|
||||
@export var initial_table_rows: int = 1
|
||||
|
||||
|
||||
@export var title: String = ""
|
||||
@export var title_placeholder: String
|
||||
@export var rows_data: Array[EnergyTableRowData]
|
||||
|
||||
const table_row_node = preload("res://src/modules/building/energy_calculator/energy_table_row.tscn")
|
||||
var table_title_line_edit: LineEdit
|
||||
var total_energy_label: Label
|
||||
var table_body: VBoxContainer
|
||||
|
||||
var table_rows: Array[EnergyTableRow] = []
|
||||
|
||||
|
||||
func render_total_energy_label():
|
||||
var total_energy = 0
|
||||
for table_row in table_rows:
|
||||
total_energy += table_row.total_energy
|
||||
|
||||
total_energy_label.text = str(total_energy)
|
||||
|
||||
|
||||
func del_row(node: Node):
|
||||
for i in range(0, table_rows.size()):
|
||||
var table_row = table_rows[i]
|
||||
if table_row == node:
|
||||
table_body.remove_child(node)
|
||||
table_rows.pop_at(i)
|
||||
break
|
||||
|
||||
|
||||
func _on_table_row_deleted(instance: Node):
|
||||
del_row(instance)
|
||||
render_total_energy_label()
|
||||
changed.emit()
|
||||
|
||||
|
||||
func add_row(data_row: Dictionary):
|
||||
var instance = table_row_node.instantiate()
|
||||
if data_row.has("building_id"):
|
||||
instance.building_id = data_row["building_id"]
|
||||
if data_row.has("building_count"):
|
||||
instance.building_count = data_row["building_count"]
|
||||
|
||||
table_body.add_child(instance)
|
||||
|
||||
instance.add_to_group("Persist")
|
||||
instance.total_energy_changed.connect(_on_total_energy_changed)
|
||||
instance.deleted.connect(_on_table_row_deleted)
|
||||
|
||||
table_rows.append(instance)
|
||||
|
||||
|
||||
func _enter_tree():
|
||||
table_body = $PanelContainer/MarginContainer/VBoxContainer/TableBody/VBoxContainer
|
||||
table_title_line_edit = $PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/TableTitleLineEdit
|
||||
total_energy_label = $PanelContainer/MarginContainer/VBoxContainer/TableSummary/HBoxContainer/TotalEnergyLabel
|
||||
|
||||
if rows_data != null:
|
||||
for row_data in rows_data:
|
||||
add_row({
|
||||
"building_id": row_data.building_id,
|
||||
"building_count": row_data.building_count
|
||||
})
|
||||
|
||||
if table_rows.size() < initial_table_rows:
|
||||
add_row({})
|
||||
|
||||
table_title_line_edit.text = title
|
||||
if title_placeholder != null:
|
||||
table_title_line_edit.placeholder_text = title_placeholder
|
||||
|
||||
|
||||
func _ready():
|
||||
render_total_energy_label()
|
||||
|
||||
|
||||
func _on_total_energy_changed(_value: int):
|
||||
render_total_energy_label()
|
||||
changed.emit()
|
||||
|
||||
|
||||
func _on_add_building_button_pressed():
|
||||
add_row({})
|
||||
|
||||
|
||||
func _on_table_title_line_edit_text_changed(new_text):
|
||||
title = new_text
|
||||
changed.emit()
|
||||
|
||||
|
||||
func get_save_data() -> Dictionary:
|
||||
var rows_data = table_rows.map(func get_row(row): return row.get_save_data())
|
||||
|
||||
return {
|
||||
"title": title,
|
||||
"title_placeholder": title_placeholder,
|
||||
"rows": rows_data,
|
||||
}
|
||||
|
91
src/modules/building/energy_calculator/energy_table.tscn
Normal file
91
src/modules/building/energy_calculator/energy_table.tscn
Normal file
|
@ -0,0 +1,91 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://cbegq3jfbfb6t"]
|
||||
|
||||
[ext_resource type="Script" path="res://src/modules/building/energy_calculator/energy_table.gd" id="1_vq16x"]
|
||||
|
||||
[node name="EnergyTable" type="MarginContainer"]
|
||||
anchors_preset = 10
|
||||
anchor_right = 1.0
|
||||
grow_horizontal = 2
|
||||
script = ExtResource("1_vq16x")
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="."]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="PanelContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_left = 9
|
||||
theme_override_constants/margin_top = 9
|
||||
theme_override_constants/margin_right = 9
|
||||
theme_override_constants/margin_bottom = 9
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_bottom = 9
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer/MarginContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="TableTitleLineEdit" type="LineEdit" parent="PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
flat = true
|
||||
|
||||
[node name="AddBuildingButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Добавить здание"
|
||||
|
||||
[node name="TableHeader" type="PanelContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer/TableHeader"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/TableHeader/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "Название"
|
||||
|
||||
[node name="Label3" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/TableHeader/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "Кол-во ед."
|
||||
|
||||
[node name="Label4" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/TableHeader/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "Энергия/сек"
|
||||
|
||||
[node name="TableBody" type="PanelContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer/TableBody"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 3
|
||||
|
||||
[node name="TableSummary" type="PanelContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer/TableSummary"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/TableSummary/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "ИТОГО"
|
||||
uppercase = true
|
||||
|
||||
[node name="Label3" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/TableSummary/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="TotalEnergyLabel" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/TableSummary/HBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
text = "0"
|
||||
|
||||
[connection signal="text_changed" from="PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/TableTitleLineEdit" to="." method="_on_table_title_line_edit_text_changed"]
|
||||
[connection signal="pressed" from="PanelContainer/MarginContainer/VBoxContainer/MarginContainer/HBoxContainer/AddBuildingButton" to="." method="_on_add_building_button_pressed"]
|
|
@ -1,3 +1,4 @@
|
|||
class_name EnergyTableRow
|
||||
extends HBoxContainer
|
||||
|
||||
|
||||
|
@ -8,9 +9,9 @@ signal total_energy_changed(new_value: int)
|
|||
@export var building_id: int
|
||||
@export var building_count: int = 1
|
||||
|
||||
@onready var building_option = $BuildingOption
|
||||
@onready var building_count_line_edit: LineEdit = $BuildingCountLineEdit
|
||||
@onready var total_energy_label: Label = $HBoxContainer/EnergyLabel
|
||||
var total_energy_label: Label
|
||||
var building_option: BuildingOption
|
||||
var building_count_line_edit: LineEdit
|
||||
|
||||
var building: BuildingOptionItem
|
||||
var total_energy: int:
|
||||
|
@ -27,10 +28,16 @@ var total_energy: int:
|
|||
func render_energy():
|
||||
total_energy = building_count * (building.energy if building else 0)
|
||||
|
||||
func _ready():
|
||||
func _enter_tree():
|
||||
total_energy_label = $HBoxContainer/EnergyLabel
|
||||
building_option = $BuildingOption
|
||||
if building_id != null:
|
||||
building_option.selected_id = building_id
|
||||
building_count_line_edit = $BuildingCountLineEdit
|
||||
building_count_line_edit.text = str(building_count)
|
||||
if building_id:
|
||||
building_option.select(building_id)
|
||||
|
||||
|
||||
func _ready():
|
||||
render_energy()
|
||||
|
||||
func _on_unit_count_line_edit_text_changed(new_text):
|
||||
|
@ -46,7 +53,9 @@ func _on_building_option_selected(item: BuildingOptionItem):
|
|||
render_energy()
|
||||
|
||||
func get_save_data() -> Dictionary:
|
||||
return {
|
||||
"building_id": building.id if building else null,
|
||||
var data = {
|
||||
"building_count": building_count,
|
||||
}
|
||||
if building != null:
|
||||
data["building_id"] = building.id
|
||||
return data
|
|
@ -1,16 +1,16 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://cvc75dbbiut78"]
|
||||
|
||||
[ext_resource type="Script" path="res://src/energy_table_row.gd" id="1_67d5k"]
|
||||
[ext_resource type="PackedScene" uid="uid://bedenbuamujfg" path="res://src/building_option.tscn" id="2_vf51a"]
|
||||
[ext_resource type="Script" path="res://src/modules/building/energy_calculator/energy_table_row.gd" id="1_206lu"]
|
||||
[ext_resource type="PackedScene" uid="uid://bedenbuamujfg" path="res://src/modules/building/building_option/building_option.tscn" id="2_obebf"]
|
||||
|
||||
[node name="EnergyTableRow" type="HBoxContainer"]
|
||||
anchors_preset = 10
|
||||
anchor_right = 1.0
|
||||
offset_bottom = 35.0
|
||||
grow_horizontal = 2
|
||||
script = ExtResource("1_67d5k")
|
||||
script = ExtResource("1_206lu")
|
||||
|
||||
[node name="BuildingOption" parent="." instance=ExtResource("2_vf51a")]
|
||||
[node name="BuildingOption" parent="." instance=ExtResource("2_obebf")]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="BuildingCountLineEdit" type="LineEdit" parent="."]
|
|
@ -0,0 +1,5 @@
|
|||
class_name EnergyTableRowData
|
||||
extends Resource
|
||||
|
||||
@export var building_id: int
|
||||
@export var building_count: int
|
Loading…
Reference in a new issue