Skip to content

Commit

Permalink
[license] Fix selection and right click menu
Browse files Browse the repository at this point in the history
  • Loading branch information
IceflowRE committed Dec 18, 2023
1 parent 6932a4e commit b980c1c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 71 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ License class.

### Changelog

#### 1.7.3

- Fix component selection and right click menu

#### 1.7.2

- Fix current selection of component on popup
Expand Down
116 changes: 79 additions & 37 deletions addons/licenses/internal/components_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,32 @@ var show_readonly_components: bool = false:
var _components: ComponentsContainer
## cached value
var _readonly_components: Array[Component] = []
var _item_menu: PopupMenu

func set_show_readonly_components(show: bool) -> void:
show_readonly_components = show
func set_show_readonly_components(show_: bool) -> void:
var sel_comp: Component = self._get_selected_component()
show_readonly_components = show_
if show:
self._readonly_components = Licenses.get_required_engine_components()
self.reload(sel_comp)
else:
self._readonly_components = []
self.reload()
if sel_comp != null && sel_comp.readonly:
self.reload()
self.component_selected.emit(null)
else:
self.reload(sel_comp)

func set_components(comp: ComponentsContainer) -> void:
self._components = comp
self.reload()

func _create_category_item(category_cache: Dictionary, category: String, root: TreeItem) -> TreeItem:
if category in category_cache:
return category_cache[category]
var category_item: TreeItem
category_item = self.create_item(root)
category_item.set_text(0, category)
category_item.set_editable(0, true)
category_item.set_meta("category", category)
category_cache[category] = category_item
return category_item

func _add_tree_item(component: Component, idx: int, parent: TreeItem) -> TreeItem:
var item: TreeItem = self.create_item(parent)
item.set_text(0, component.name)
item.set_meta("idx", idx)
item.set_meta("readonly", component.readonly)
if not component.readonly:
item.add_button(0, self.get_theme_icon("Remove", "EditorIcons"), _BTN_ID_REMOVE)
var tooltip = component.name
var comp_warnings: PackedStringArray = component.get_warnings()
if comp_warnings.size() != 0:
tooltip += "\n- " + "\n- ".join(comp_warnings)
item.set_icon(0, self.get_theme_icon("NodeWarning", "EditorIcons"))
item.set_tooltip_text(0, tooltip)
return item
func _ready() -> void:
self._create_item_menu()
self.gui_input.connect(self._on_gui_input)

func reload(scroll_to: Component = null) -> void:
self.clear()
if self.get_root() != null:
push_error("could not clear")

var category_cache: Dictionary = {}
var root: TreeItem = self.create_item(null)
Expand Down Expand Up @@ -90,6 +73,41 @@ func reload(scroll_to: Component = null) -> void:
self.scroll_to_item(item)
item.select(0)

func _create_item_menu() -> void:
self._item_menu = PopupMenu.new()
self._item_menu.name = "item_menu"
self._item_menu.id_pressed.connect(self._on_item_menu_pressed)
self._item_menu.add_icon_item(self.get_theme_icon("Duplicate", "EditorIcons"), "Duplicate")
self._item_menu.add_icon_item(self.get_theme_icon("Remove", "EditorIcons"), "Delete")
self._item_menu.size = self._item_menu.get_contents_minimum_size()
self.add_child(self._item_menu)

func _create_category_item(category_cache: Dictionary, category: String, root: TreeItem) -> TreeItem:
if category in category_cache:
return category_cache[category]
var category_item: TreeItem
category_item = self.create_item(root)
category_item.set_text(0, category)
category_item.set_editable(0, true)
category_item.set_meta("category", category)
category_cache[category] = category_item
return category_item

func _add_tree_item(component: Component, idx: int, parent: TreeItem) -> TreeItem:
var item: TreeItem = self.create_item(parent)
item.set_text(0, component.name)
item.set_meta("idx", idx)
item.set_meta("readonly", component.readonly)
if not component.readonly:
item.add_button(0, self.get_theme_icon("Remove", "EditorIcons"), _BTN_ID_REMOVE)
var tooltip: String = component.name
var comp_warnings: PackedStringArray = component.get_warnings()
if comp_warnings.size() != 0:
tooltip += "\n- " + "\n- ".join(comp_warnings)
item.set_icon(0, self.get_theme_icon("NodeWarning", "EditorIcons"))
item.set_tooltip_text(0, tooltip)
return item

func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
var item: TreeItem = self.get_item_at_position(at_position)
return item != null && data is TreeItem && item.has_meta("category")
Expand Down Expand Up @@ -119,7 +137,7 @@ func _get_drag_data(at_position: Vector2) -> Variant:
func _drop_data(at_position: Vector2, data: Variant) -> void:
var to_item: TreeItem = self.get_item_at_position(at_position)
var category = to_item.get_meta("category")
var cur_component: Component = self._components.get_at(data.get_meta("idx") as int)
var cur_component: Component = self._get_selected_component()
if cur_component.category == category:
return
cur_component.category = category
Expand All @@ -135,21 +153,24 @@ func _notification(what: int):
item.clear_custom_color(0)
item = item.get_next()

func _on_item_selected() -> void:
func _get_selected_component() -> Component:
var item: TreeItem = self.get_selected()
if not item.has_meta("readonly"):
return
var component: Component
if item == null || !item.has_meta("idx"):
return null
var component: Component = null
if item.get_meta("readonly") as bool:
component = self._readonly_components[item.get_meta("idx")]
else:
component = self._components.get_at(item.get_meta("idx") as int)
self.component_selected.emit(component)
return component

func _on_item_selected() -> void:
self.component_selected.emit(self._get_selected_component())

func _on_button_clicked(item: TreeItem, column: int, id: int, mouse_button_idx: int) -> void:
match id:
_BTN_ID_REMOVE:
self.component_remove.emit(self._components.get_at(item.get_meta("idx") as int))
self.component_remove.emit(self._get_selected_component())

func _on_item_edited() -> void:
var category_item: TreeItem = self.get_edited()
Expand All @@ -162,3 +183,24 @@ func _on_item_edited() -> void:

self._components.sort_custom(Licenses.compare_components_ascending)
self._components.emit_changed()

func _on_gui_input(event: InputEvent) -> void:
if !((event is InputEventMouseButton) && (event as InputEventMouseButton).pressed && (event as InputEventMouseButton).button_index == MOUSE_BUTTON_RIGHT):
return
var item: TreeItem = self.get_item_at_position((event as InputEventMouseButton).position)
if item == null || !item.has_meta("idx") || !item.has_meta("readonly"):
return

self._item_menu.set_item_disabled(1, item.get_meta("readonly") as bool)
self._item_menu.popup_on_parent(Rect2(self.get_local_mouse_position() + Vector2(9, self._item_menu.size.y + 3), self._item_menu.size))

func _on_item_menu_pressed(btn_idx: int) -> void:
var comp: Component = self._get_selected_component()
match btn_idx:
0:
var new_comp: Component = comp.duplicate()
new_comp.readonly = false
new_comp.name = new_comp.name + " Copy"
self.component_add.emit(new_comp)
1:
self.component_remove.emit(comp)
32 changes: 0 additions & 32 deletions addons/licenses/internal/licenses.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const StringMultiLineHandler := preload("handler/string_multiline.gd")
@onready var _component_detail_tree: ComponentDetailTree = self.get_node(self._component_detail_tree_path)
@export_node_path("HBoxContainer") var _toolbar_path
@onready var _toolbar: Toolbar = self.get_node(self._toolbar_path)
var _item_menu: PopupMenu
@export var _license_file_edit: LineEdit = null
@export var _license_file_load_button: Button = null
@export var _set_license_filepath_button: Button = null
Expand All @@ -38,8 +37,6 @@ func _ready() -> void:
self._license_file_edit.text_submitted.connect(self._on_data_file_edit_changed)
self._license_file_edit.text = Licenses.get_license_data_filepath()

self._create_item_menu()
self._components_tree.gui_input.connect(self._on_gui_input)
self.add_child(self._components)

self._components_tree.set_components(self._components)
Expand Down Expand Up @@ -68,20 +65,10 @@ func reload() -> void:
self._license_file_edit.right_icon = self.get_theme_icon("NodeWarning", "EditorIcons")
self._license_file_edit.tooltip_text = res.err_msg

self._component_detail_tree.set_component(null)
self._components.set_components(res.components)
self._components.sort_custom(Licenses.compare_components_ascending)
self._components.emit_changed()

func _create_item_menu() -> void:
self._item_menu = PopupMenu.new()
self._item_menu.name = "item_menu"
self._item_menu.id_pressed.connect(self._on_item_menu_pressed)
self._item_menu.add_icon_item(self.get_theme_icon("Duplicate", "EditorIcons"), "Duplicate")
self._item_menu.add_icon_item(self.get_theme_icon("Remove", "EditorIcons"), "Delete")
self._item_menu.size = self._item_menu.get_contents_minimum_size()
self.add_child(self._item_menu)

func _update_set_license_filepath_button() -> void:
if Licenses.get_license_data_filepath() == self._license_file_edit.text:
self._set_license_filepath_button.icon = self.get_theme_icon("ImportCheck", "EditorIcons")
Expand Down Expand Up @@ -146,24 +133,5 @@ func _on_components_changed() -> void:
self._component_detail_tree.reload()
self._components_tree.reload(self._component_detail_tree.get_component())

func _on_gui_input(event: InputEvent) -> void:
if !(event is InputEventMouseButton) || !event.pressed || event.button_index != MOUSE_BUTTON_RIGHT:
return
var item: TreeItem = self._components_tree.get_item_at_position(event.position)
if item == null || !item.has_meta("idx") || !item.has_meta("readonly") || (item.get_meta("readonly") as bool):
return

self._item_menu.popup_on_parent(Rect2(self.get_local_mouse_position(), self._item_menu.size))

func _on_item_menu_pressed(btn_idx: int) -> void:
var idx: int = self._components_tree.get_selected().get_meta("idx") as int
match btn_idx:
0:
var new_comp: Component = self._components.get_at(idx).duplicate()
new_comp.name = new_comp.name + " Copy"
self._on_component_tree_add(new_comp)
1:
self._on_component_tree_remove(self._components.get_at(idx))

func _emit_changed():
self._components.emit_changed()
4 changes: 2 additions & 2 deletions addons/licenses/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
name="License Manager"
description="Manage license and copyright for third party graphics, software or libraries."
author="Iceflower S"
version="1.7.2"
version="1.7.3"
script="plugin.gd"
license="MIT"
repository="https://github.com/kenyoni-software/godot-addons"
Expand All @@ -16,4 +16,4 @@ classifiers=[
]

[plugin.dependencies]
godot=">=4.2"
godot=">=4.2"

0 comments on commit b980c1c

Please sign in to comment.