test-client-godot/addons/ui_window/ui_window.gd

326 lines
No EOL
11 KiB
GDScript

extends MarginContainer
export(bool) var is_movable = true
export(bool) var is_resizable = true
export(bool) var is_borderless = false
export(String) var title = "Window"
export(Color) var background_color = Color( 1.0, 1.0, 1.0, 1.0 )
var current_rect_size = Vector2( 0, 0 )
var current_rect_position = Vector2( -1, -1 )
var is_resizing = false
var is_moving = false
func _ready():
###
# self
self.size_flags_horizontal = SIZE_EXPAND
self.size_flags_vertical = SIZE_EXPAND
self.set( "custom_constants/margin_right", 0)
self.set( "custom_constants/margin_top", 0)
self.set( "custom_constants/margin_left", 0)
self.set( "custom_constants/margin_bottom", 0)
###
###
# Background
var background = NinePatchRect.new()
background.name = "Background"
var background_image = Image.new()
if not background_image.load( "res://assets/GUI/images/bg2.jpg" ):
print("Errur lors du chargement de l'image: res://assets/GUI/images/bg2.jpg" )
background.texture = ImageTexture.new()
background.texture.create_from_image( background_image )
background.axis_stretch_horizontal = NinePatchRect.AXIS_STRETCH_MODE_TILE
background.axis_stretch_vertical = NinePatchRect.AXIS_STRETCH_MODE_TILE
background.size_flags_horizontal = SIZE_EXPAND_FILL
background.size_flags_vertical = SIZE_EXPAND_FILL
background.region_rect = Rect2( 0, 0, 0, 0 )
background.patch_margin_left = 4
background.patch_margin_top = 32
background.patch_margin_right = 4
background.patch_margin_bottom = 3
background.self_modulate = background_color
self.add_child( background )
background.set_owner( self )
###
###
# VBoxContainer
var v_box_container = VBoxContainer.new()
v_box_container.name = "VBoxContainer"
v_box_container.size_flags_horizontal = SIZE_EXPAND_FILL
v_box_container.size_flags_vertical = SIZE_EXPAND_FILL
self.add_child( v_box_container )
v_box_container.set_owner( self )
###
###
# Header
var header = MarginContainer.new()
header.name = "Header"
header.size_flags_horizontal = SIZE_EXPAND_FILL
header.size_flags_vertical = SIZE_SHRINK_CENTER
header.set( "custom_constants/margin_right", 0)
header.set( "custom_constants/margin_top", 4)
header.set( "custom_constants/margin_left", 4)
header.set( "custom_constants/margin_bottom", 4)
header.mouse_default_cursor_shape = CURSOR_MOVE
v_box_container.add_child( header )
header.set_owner( v_box_container )
header.connect ( "gui_input", self, "_on_Header_gui_input" )
###
###
# Header/HBoxContainer
var header_box = HBoxContainer.new()
header_box.name = "HBoxContainer"
header_box.size_flags_horizontal = SIZE_EXPAND_FILL
header_box.size_flags_vertical = SIZE_EXPAND | SIZE_SHRINK_CENTER
header.mouse_default_cursor_shape = CURSOR_MOVE
header.add_child( header_box )
header_box.set_owner( header )
###
###
# Quit
var quit_button = TextureButton.new()
quit_button.name = "Quit"
quit_button.size_flags_horizontal = SIZE_SHRINK_END
quit_button.size_flags_vertical = SIZE_SHRINK_CENTER
var tex_quit = ImageTexture.new()
var img_quit = Image.new()
img_quit.load( "res://assets/GUI/images/button_quit.png" )
tex_quit.create_from_image( img_quit )
quit_button.texture_normal = tex_quit
header_box.add_child( quit_button )
quit_button.set_owner( header_box )
quit_button.connect ( "pressed", self, "_on_Quit_pressed" )
###
# Close
var close_button = TextureButton.new()
close_button.name = "Close"
close_button.size_flags_horizontal = SIZE_SHRINK_END
close_button.size_flags_vertical = SIZE_SHRINK_CENTER
var tex_close = ImageTexture.new()
var img_close = Image.new()
img_close.load( "res://assets/GUI/images/button_close.png" )
tex_close.create_from_image( img_close )
close_button.texture_normal = tex_close
header_box.add_child( close_button )
close_button.set_owner( header_box )
close_button.connect ( "pressed", self, "_on_Close_pressed" )
###
# Open
var open_button = TextureButton.new()
open_button.name = "Open"
open_button.size_flags_horizontal = SIZE_SHRINK_END
open_button.size_flags_vertical = SIZE_SHRINK_CENTER
var tex_open = ImageTexture.new()
var img_open = Image.new()
img_open.load( "res://assets/GUI/images/button_open.png" )
tex_open.create_from_image( img_open )
open_button.texture_normal = tex_open
open_button.visible = false
header_box.add_child( open_button )
open_button.set_owner( header_box )
open_button.connect ( "pressed", self, "_on_Open_pressed" )
###
###
# Title Label
var title_label = Label.new()
title_label.name = "Label"
title_label.text = title
title_label.size_flags_horizontal = SIZE_EXPAND_FILL
title_label.size_flags_vertical = SIZE_SHRINK_CENTER
title_label.mouse_default_cursor_shape = CURSOR_MOVE
header_box.add_child( title_label )
title_label.set_owner( header_box )
###
###
# Content
var content = MarginContainer.new()
content.name = "Content"
content.size_flags_horizontal = SIZE_EXPAND_FILL
content.size_flags_vertical = SIZE_EXPAND_FILL
content.set( "custom_constants/margin_right", 8)
content.set( "custom_constants/margin_top", 8)
content.set( "custom_constants/margin_left", 8)
content.set( "custom_constants/margin_bottom", 8)
v_box_container.add_child( content )
content.set_owner( v_box_container )
###
###
# Footer
var footer = MarginContainer.new()
footer.name = "Footer"
footer.size_flags_horizontal = SIZE_FILL
footer.size_flags_vertical = SIZE_FILL
footer.set( "custom_constants/margin_right", 6)
footer.set( "custom_constants/margin_top", 2)
footer.set( "custom_constants/margin_left", 6)
footer.set( "custom_constants/margin_bottom", 6)
v_box_container.add_child( footer )
footer.set_owner( v_box_container )
###
###
# Header/HBoxContainer
var footer_box = HBoxContainer.new()
footer_box.name = "HBoxContainer"
footer_box.size_flags_horizontal = SIZE_FILL
footer_box.size_flags_vertical = SIZE_FILL
footer.add_child( footer_box )
footer_box.set_owner( footer )
###
###
# Open
var resize_button = TextureButton.new()
resize_button.name = "Resize"
resize_button.size_flags_horizontal = SIZE_EXPAND | SIZE_SHRINK_END
resize_button.size_flags_vertical = SIZE_EXPAND
var tex_resize = ImageTexture.new()
var img_resize = Image.new()
img_resize.load( "res://assets/GUI/images/button_resize.png" )
tex_resize.create_from_image( img_resize )
resize_button.texture_normal = tex_resize
resize_button.mouse_default_cursor_shape = CURSOR_FDIAGSIZE
resize_button.action_mode = Button.ACTION_MODE_BUTTON_PRESS
footer_box.add_child( resize_button )
resize_button.set_owner( footer_box )
resize_button.connect ( "pressed", self, "_on_Resize_pressed" )
###
current_rect_size = self.rect_min_size
if is_borderless:
$Background.region_rect = Rect2( 3, 28+3, 512-6, 512-28-6 )
$VBoxContainer/Header/HBoxContainer/Close.visible = false
$VBoxContainer/Header/HBoxContainer/Open.visible = false
$VBoxContainer/Header/HBoxContainer/Quit.visible = false
$VBoxContainer/Header/HBoxContainer/Label.visible = false
# else:
# $Background.region_rect = Rect2( 0, 0, 512, 512 )
# $VBoxContainer/Header/HBoxContainer/Close.visible = true
# $VBoxContainer/Header/HBoxContainer/Open.visible = false
# $VBoxContainer/Header/HBoxContainer/Quit.visible = true
# $VBoxContainer/Header/HBoxContainer/Label.visible = true
if not is_resizable:
$VBoxContainer/Footer/HBoxContainer/Resize.visible = false
func _on_Window_mouse_entered():
print("mouse_entered")
func _on_Window_focus_entered():
print("focus_entered")
func _on_Quit_pressed():
self.visible = false
func get_content():
return $VBoxContainer/Content
func close():
if not is_borderless:
$VBoxContainer/Header/HBoxContainer/Close.visible = false
$VBoxContainer/Header/HBoxContainer/Open.visible = true
$VBoxContainer/Content.visible = false
$VBoxContainer/Footer.visible = false
current_rect_size = self.rect_size
self.rect_size = Vector2( 0, 0 )
else:
$VBoxContainer/Header/HBoxContainer/Close.visible = false
$VBoxContainer/Header/HBoxContainer/Open.visible = false
$VBoxContainer/Content.visible = false
$VBoxContainer/Footer.visible = false
current_rect_size = self.rect_size
self.rect_size = Vector2( 0, 0 )
func _on_Close_pressed():
close()
func open():
if not is_borderless:
$VBoxContainer/Header/HBoxContainer/Close.visible = true
$VBoxContainer/Header/HBoxContainer/Open.visible = false
$VBoxContainer/Content.visible = true
$VBoxContainer/Footer.visible = true
self.rect_size = current_rect_size
else:
$VBoxContainer/Header/HBoxContainer/Close.visible = false
$VBoxContainer/Header/HBoxContainer/Open.visible = false
$VBoxContainer/Content.visible = true
$VBoxContainer/Footer.visible = true
self.rect_size = current_rect_size
func _on_Open_pressed():
open()
func _on_Resize_pressed():
is_resizing = true
func _input( event ):
if is_resizable:
if is_resizing and event is InputEventMouseButton and not event.pressed:
is_resizing = false
if event is InputEventMouseMotion and is_resizing:
var delta = event.relative
self.rect_size += delta
func _on_Header_gui_input(ev):
if is_movable:
if is_moving and ev is InputEventMouseButton and not ev.pressed:
is_moving = false
elif not is_moving and ev is InputEventMouseButton and ev.pressed:
is_moving = true
if ev is InputEventMouseMotion and is_moving:
var delta = ev.relative
self.rect_position += delta
func load_from_file( config_file ):
if config_file.has_section( self.name ):
self.rect_position = config_file.get_value( self.name, "position" )
self.rect_size = config_file.get_value( self.name, "size" )
self.is_borderless = config_file.get_value( self.name, "borderless" )
current_rect_position = self.rect_position
current_rect_size = self.rect_size
if config_file.get_value( self.name, "opened" ):
open()
else:
close()
func save_to_file( config_file ):
var is_open = $VBoxContainer/Content.visible
config_file.set_value(self.name, "position", self.rect_position)
if not is_open:
config_file.set_value(self.name, "size", current_rect_size)
else:
config_file.set_value(self.name, "size", self.rect_size)
if is_open:
config_file.set_value(self.name, "opened", true)
else:
config_file.set_value(self.name, "opened", false)
config_file.set_value(self.name, "borderless", is_borderless)