Added an WebPageEntryImage with an editor.

This commit is contained in:
Relintai 2022-08-25 18:28:01 +02:00
parent 45e6a5238b
commit bf267a5a1a
13 changed files with 329 additions and 6 deletions

View File

@ -1,9 +1,10 @@
[gd_scene load_steps=9 format=2]
[gd_scene load_steps=11 format=2]
[ext_resource path="res://Main.gd" type="Script" id=1]
[ext_resource path="res://addons/web_pages/classes/WebPage.gd" type="Script" id=2]
[ext_resource path="res://addons/web_pages/classes/post_entries/WebPageEntryTitleText.gd" type="Script" id=3]
[ext_resource path="res://addons/web_pages/classes/post_entries/WebPageEntryText.gd" type="Script" id=4]
[ext_resource path="res://addons/web_pages/classes/post_entries/WebPageEntryImage.gd" type="Script" id=5]
[sub_resource type="Resource" id=1]
script = ExtResource( 3 )
@ -20,7 +21,15 @@ asda
TEST
A"
A
"
[sub_resource type="Resource" id=5]
script = ExtResource( 5 )
image_path = "res://test_img/icon.png"
image_url = "img"
alt = "Addalt"
image_size = Vector2i( 100, 100 )
[sub_resource type="Resource" id=3]
script = ExtResource( 3 )
@ -44,4 +53,4 @@ data = "<a href=\"/page\">Page</a>"
[node name="page" type="WebNode" parent="WebRoot"]
uri_segment = "page"
script = ExtResource( 2 )
entries = [ SubResource( 1 ), SubResource( 4 ), SubResource( 3 ), SubResource( 2 ) ]
entries = [ SubResource( 1 ), SubResource( 4 ), SubResource( 5 ), SubResource( 3 ), SubResource( 2 ) ]

View File

@ -9,6 +9,13 @@ export(Array, Resource) var entries : Array
signal entries_changed()
func _handle_request(request : WebServerRequest):
if request.get_remaining_segment_count() > 0:
for i in range(entries.size()):
var e : WebPageEntry = entries[i]
if e && e.handle_request(request):
return
if sohuld_render_menu:
render_menu(request)

View File

@ -2,6 +2,12 @@ tool
extends Resource
class_name WebPageEntry, "res://addons/web_pages/icons/icon_web_page_entry.svg"
func handle_request(request : WebServerRequest) -> bool:
return _handle_request(request)
func _handle_request(request : WebServerRequest) -> bool:
return false
func render(request : WebServerRequest):
_render(request)

View File

@ -0,0 +1,45 @@
tool
extends "res://addons/web_pages/classes/WebPageEntry.gd"
class_name WebPageEntryImage, "res://addons/web_pages/icons/icon_web_page_entry_image.svg"
export(String) var image_path : String
export(String) var image_url : String
export(String) var alt : String
export(Vector2i) var image_size : Vector2i = Vector2i()
func _handle_request(request : WebServerRequest) -> bool:
if image_url.empty() || image_path.empty():
return false
var file : File = File.new()
#if !file.file_exists(image_path):
# return false
if request.get_current_path_segment() == image_url:
request.send_file(image_path)
# handled
return true
return false
func _render(request : WebServerRequest):
if image_url.empty() || image_path.empty():
return
request.body += '<img src="' + request.get_url_root() + image_url + '"'
if !alt.empty():
request.body += ' alt="' + alt + '"'
if image_size.x > 0:
request.body += ' width="' + str(image_size.x) + '"'
if image_size.y > 0:
request.body += ' height="' + str(image_size.y) + '"'
request.body += '>'
func get_page_entry_class_name() -> String:
return "WebPageEntryImage"

View File

@ -13,6 +13,7 @@ func _notification(what):
get_ok().set_text("Close")
get_node("VBC/AddTitleTextButton").connect("pressed", self, "_add_title_text_button_pressed")
get_node("VBC/AddTextButton").connect("pressed", self, "_add_text_button_pressed")
get_node("VBC/AddimageButton").connect("pressed", self, "_add_image_button_pressed")
func _add_title_text_button_pressed() -> void:
emit_signal("on_entry_class_selected" , "WebPageEntryTitleText")
@ -21,3 +22,7 @@ func _add_title_text_button_pressed() -> void:
func _add_text_button_pressed() -> void:
emit_signal("on_entry_class_selected" , "WebPageEntryText")
hide()
func _add_image_button_pressed() -> void:
emit_signal("on_entry_class_selected" , "WebPageEntryImage")
hide()

View File

@ -179,6 +179,8 @@ func _on_add_entry_class_selected(cls_name : String) -> void:
entry = WebPageEntryTitleText.new()
elif cls_name == "WebPageEntryText":
entry = WebPageEntryText.new()
elif cls_name == "WebPageEntryImage":
entry = WebPageEntryImage.new()
if !entry:
PLogger.log_error("PageEditor: Couldn't create entry for: " + cls_name)

View File

@ -1,9 +1,10 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=6 format=2]
[ext_resource path="res://addons/web_pages/editor/PageEditor.gd" type="Script" id=1]
[ext_resource path="res://addons/web_pages/icons/icon_web_page_entry_title_text.svg" type="Texture" id=2]
[ext_resource path="res://addons/web_pages/editor/AddEntryPopup.gd" type="Script" id=3]
[ext_resource path="res://addons/web_pages/icons/icon_web_page_entry_text.svg" type="Texture" id=4]
[ext_resource path="res://addons/web_pages/icons/icon_web_page_entry_image.svg" type="Texture" id=5]
[node name="PostEditor" type="PanelContainer"]
anchor_right = 1.0
@ -100,7 +101,7 @@ script = ExtResource( 3 )
margin_left = 8.0
margin_top = 8.0
margin_right = 192.0
margin_bottom = 56.0
margin_bottom = 82.0
size_flags_horizontal = 3
size_flags_vertical = 3
@ -116,3 +117,10 @@ margin_right = 184.0
margin_bottom = 48.0
text = "Text"
icon = ExtResource( 4 )
[node name="AddimageButton" type="Button" parent="Popups/AddEntryPopup/VBC"]
margin_top = 52.0
margin_right = 184.0
margin_bottom = 74.0
text = "Image"
icon = ExtResource( 5 )

View File

@ -10,6 +10,7 @@ var _editor : Control = null
var WebPageEntryTitleTextEditor : PackedScene = null
var WebPageEntryTextEditor : PackedScene = null
var WebPageEntryImageEditor : PackedScene = null
signal entry_add_requested_after(entry)
signal entry_move_up_requested(entry)
@ -26,6 +27,8 @@ func set_entry(entry : WebPageEntry, undo_redo : UndoRedo) -> void:
_editor = WebPageEntryTitleTextEditor.instance()
elif cls == "WebPageEntryText":
_editor = WebPageEntryTextEditor.instance()
elif cls == "WebPageEntryImage":
_editor = WebPageEntryImageEditor.instance()
if _editor:
_editor.set_entry(entry, undo_redo)
@ -47,6 +50,7 @@ func _notification(what):
if what == NOTIFICATION_INSTANCED:
WebPageEntryTitleTextEditor = ResourceLoader.load("res://addons/web_pages/editor/post_entries/WebPageEntryTitleTextEditor.tscn", "PackedScene")
WebPageEntryTextEditor = ResourceLoader.load("res://addons/web_pages/editor/post_entries/WebPageEntryTextEditor.tscn", "PackedScene")
WebPageEntryImageEditor = ResourceLoader.load("res://addons/web_pages/editor/post_entries/WebPageEntryImageEditor.tscn", "PackedScene")
_entry_type_label = get_node("PC/VBC/TopBar/EntryTypeLabel")
_main_container = get_node("PC/VBC/MainContainer")

View File

@ -0,0 +1,114 @@
tool
extends MarginContainer
var _entry : WebPageEntryImage = null
var undo_redo : UndoRedo = null
var _image_path_line_edit : LineEdit = null
var _image_url_line_edit : LineEdit = null
var _image_alt_line_edit : LineEdit = null
var _image_width_sb : SpinBox = null
var _image_heigth_sb : SpinBox = null
var _image_preview : TextureRect = null
func set_entry(entry : WebPageEntryImage, pundo_redo : UndoRedo) -> void:
undo_redo = pundo_redo
_entry = entry
_image_path_line_edit.text = _entry.image_path
_image_url_line_edit.text = _entry.image_url
_image_alt_line_edit.text = _entry.alt
_image_width_sb.value = _entry.image_size.x
_image_heigth_sb.value = _entry.image_size.y
refresh_image()
func refresh_image() -> void:
_image_preview.texture = null
if _entry.image_path.empty():
return
var file : File = File.new()
if !file.file_exists(_entry.image_path):
PLogger.log_warning("Image Editor: Image file doesn't exists.")
return
var img : Image = ResourceLoader.load(_entry.image_path, "Image")
if !img:
PLogger.log_warning("Image Editor: Couldn't load Image file!")
return
var t : ImageTexture = ImageTexture.new()
t.create_from_image(img)
_image_preview.texture = t
func _notification(what):
if what == NOTIFICATION_INSTANCED:
_image_path_line_edit = get_node("VBoxContainer/ImagePath/ImagePathLE")
_image_url_line_edit = get_node("VBoxContainer/ImageURL/ImageURLLE")
_image_alt_line_edit = get_node("VBoxContainer/ImageAlt/ImageAltLE")
_image_width_sb = get_node("VBoxContainer/ImageWH/ImageWidthSB")
_image_heigth_sb = get_node("VBoxContainer/ImageWH/ImageHeigthSB")
_image_preview = get_node("VBoxContainer/HBoxContainer/ImagePreview")
_image_path_line_edit.connect("text_entered", self, "_on_image_path_le_text_entered")
_image_url_line_edit.connect("text_entered", self, "_on_image_url_le_text_entered")
_image_alt_line_edit.connect("text_entered", self, "_on_image_alt_le_text_entered")
_image_width_sb.connect("value_changed", self, "_image_width_sb_value_changed")
_image_heigth_sb.connect("value_changed", self, "_image_heigth_sb_value_changed")
func _on_image_path_le_text_entered(text : String) -> void:
undo_redo.create_action("Image path text changed")
undo_redo.add_do_property(_entry, "image_path", text)
undo_redo.add_undo_property(_entry, "image_path", _entry.image_path)
undo_redo.add_do_property(_image_path_line_edit, "text", text)
undo_redo.add_undo_property(_image_path_line_edit, "text", _entry.image_path)
undo_redo.commit_action()
refresh_image()
func _on_image_url_le_text_entered(text : String) -> void:
undo_redo.create_action("Image URL text changed")
undo_redo.add_do_property(_entry, "image_url", text)
undo_redo.add_undo_property(_entry, "image_url", _entry.image_url)
undo_redo.add_do_property(_image_url_line_edit, "text", text)
undo_redo.add_undo_property(_image_url_line_edit, "text", _entry.image_url)
undo_redo.commit_action()
func _on_image_alt_le_text_entered(text : String) -> void:
undo_redo.create_action("Image alt text changed")
undo_redo.add_do_property(_entry, "alt", text)
undo_redo.add_undo_property(_entry, "alt", _entry.alt)
undo_redo.add_do_property(_image_alt_line_edit, "text", text)
undo_redo.add_undo_property(_image_alt_line_edit, "text", _entry.alt)
undo_redo.commit_action()
func _image_width_sb_value_changed(val : float) -> void:
var imgsize_orig : Vector2i = _entry.image_size
var imgsize_new : Vector2i = _entry.image_size
imgsize_new.x = val
undo_redo.create_action("Image width changed")
undo_redo.add_do_property(_entry, "image_size", imgsize_new)
undo_redo.add_undo_property(_entry, "image_size", imgsize_orig)
undo_redo.add_do_property(_image_alt_line_edit, "value", imgsize_new.x)
undo_redo.add_undo_property(_image_alt_line_edit, "value", imgsize_orig.x)
undo_redo.commit_action()
func _image_heigth_sb_value_changed(val : float) -> void:
var imgsize_orig : Vector2i = _entry.image_size
var imgsize_new : Vector2i = _entry.image_size
imgsize_new.y = val
undo_redo.create_action("Image width changed")
undo_redo.add_do_property(_entry, "image_size", imgsize_new)
undo_redo.add_undo_property(_entry, "image_size", imgsize_orig)
undo_redo.add_do_property(_image_alt_line_edit, "value", imgsize_new.y)
undo_redo.add_undo_property(_image_alt_line_edit, "value", imgsize_orig.y)
undo_redo.commit_action()

View File

@ -0,0 +1,104 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://addons/web_pages/editor/post_entries/WebPageEntryImageEditor.gd" type="Script" id=1]
[node name="WebPageEntryTitleTextEditor" type="MarginContainer"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
[node name="VBoxContainer" type="VBoxContainer" parent="."]
margin_right = 1024.0
margin_bottom = 600.0
[node name="ImagePath" type="HBoxContainer" parent="VBoxContainer"]
margin_right = 1024.0
margin_bottom = 24.0
[node name="Label" type="Label" parent="VBoxContainer/ImagePath"]
margin_top = 5.0
margin_right = 75.0
margin_bottom = 19.0
text = "image Path:"
[node name="ImagePathLE" type="LineEdit" parent="VBoxContainer/ImagePath"]
margin_left = 79.0
margin_right = 1024.0
margin_bottom = 24.0
size_flags_horizontal = 3
[node name="ImageURL" type="HBoxContainer" parent="VBoxContainer"]
margin_top = 28.0
margin_right = 1024.0
margin_bottom = 52.0
[node name="Label" type="Label" parent="VBoxContainer/ImageURL"]
margin_top = 5.0
margin_right = 72.0
margin_bottom = 19.0
text = "image URL:"
[node name="ImageURLLE" type="LineEdit" parent="VBoxContainer/ImageURL"]
margin_left = 76.0
margin_right = 1024.0
margin_bottom = 24.0
size_flags_horizontal = 3
[node name="ImageAlt" type="HBoxContainer" parent="VBoxContainer"]
margin_top = 56.0
margin_right = 1024.0
margin_bottom = 80.0
[node name="Label" type="Label" parent="VBoxContainer/ImageAlt"]
margin_top = 5.0
margin_right = 127.0
margin_bottom = 19.0
text = "image alt (optional):"
[node name="ImageAltLE" type="LineEdit" parent="VBoxContainer/ImageAlt"]
margin_left = 131.0
margin_right = 1024.0
margin_bottom = 24.0
size_flags_horizontal = 3
[node name="ImageWH" type="HBoxContainer" parent="VBoxContainer"]
margin_top = 84.0
margin_right = 1024.0
margin_bottom = 108.0
[node name="Label" type="Label" parent="VBoxContainer/ImageWH"]
margin_top = 5.0
margin_right = 153.0
margin_bottom = 19.0
text = "Image Width (Optional):"
[node name="ImageWidthSB" type="SpinBox" parent="VBoxContainer/ImageWH"]
margin_left = 157.0
margin_right = 508.0
margin_bottom = 24.0
size_flags_horizontal = 3
max_value = 999999.0
rounded = true
[node name="Label2" type="Label" parent="VBoxContainer/ImageWH"]
margin_left = 512.0
margin_top = 5.0
margin_right = 669.0
margin_bottom = 19.0
text = "Image Heigth (Optional):"
[node name="ImageHeigthSB" type="SpinBox" parent="VBoxContainer/ImageWH"]
margin_left = 673.0
margin_right = 1024.0
margin_bottom = 24.0
size_flags_horizontal = 3
[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"]
margin_top = 112.0
margin_right = 1024.0
margin_bottom = 112.0
alignment = 1
[node name="ImagePreview" type="TextureRect" parent="VBoxContainer/HBoxContainer"]
margin_left = 512.0
margin_right = 512.0

View File

@ -20,6 +20,11 @@ _global_script_classes=[ {
"path": "res://addons/web_pages/classes/WebPageEntry.gd"
}, {
"base": "Resource",
"class": @"WebPageEntryImage",
"language": @"GDScript",
"path": "res://addons/web_pages/classes/post_entries/WebPageEntryImage.gd"
}, {
"base": "Resource",
"class": @"WebPageEntryText",
"language": @"GDScript",
"path": "res://addons/web_pages/classes/post_entries/WebPageEntryText.gd"
@ -39,6 +44,7 @@ _global_script_class_icons={
@"WebPageEntryTitleText": "",
@"WebPageEntry": "res://addons/web_pages/icons/icon_web_page_entry.svg",
@"WebPageList": "res://addons/web_page_list/icons/icon_web_page_list.svg",
@"WebPageEntryImage": "res://addons/web_pages/icons/icon_web_page_entry_image.svg",
@"WebPageEntryText": "res://addons/web_pages/icons/icon_web_page_entry_text.svg"
}

BIN
game/test_img/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -0,0 +1,13 @@
[remap]
importer="image"
type="Image"
path="res://.import/icon.png-a9f6ae2dfab32494edeefa50d716973d.image"
[deps]
source_file="res://test_img/icon.png"
dest_files=[ "res://.import/icon.png-a9f6ae2dfab32494edeefa50d716973d.image" ]
[params]