aboutsummaryrefslogtreecommitdiff
path: root/addons/voxel-core/controls/voxel_button/voxel_button.gd
blob: f45724c13932a5c77754fbca883ab74b5e1ce996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
tool
extends Button
# Button representing a voxel's face



## Exported Variables
# Color of voxel
export var voxel_color := Color.black setget set_voxel_color

# Texture of voxel
export var voxel_texture : Texture = null setget set_voxel_texture

# ID of voxel to represented
export(int, -1, 100000) var voxel_id := -1 setget set_voxel_id

# Voxel's face to represent
export var voxel_face := Vector3.ZERO setget set_voxel_face

# VoxelSet being used
export(Resource) var voxel_set = null setget set_voxel_set



## Built-In Virtual Methods
func _ready():
	set_voxel_color(voxel_color)
	set_voxel_texture(voxel_texture)



## Public Methods
# Sets voxel_color
func set_voxel_color(value : Color) -> void:
	voxel_color = value
	
	$VoxelColor.color = voxel_color
	property_list_changed_notify()


# Sets voxel_texture
func set_voxel_texture(value : Texture) -> void:
	voxel_texture = value
	
	$VoxelColor/VoxelTexture.texture = voxel_texture
	property_list_changed_notify()


# Sets voxel_id, and calls on update_view by default
func set_voxel_id(value : int, update := true) -> void:
	if value < -1:
		return
	
	voxel_id = value
	
	if update:
		update_view()


# Sets voxel_face, and calls on update_view by default
func set_voxel_face(value : Vector3, update := true) -> void:
	voxel_face = value
	if update:
		update_view()


# Sets voxel_set, and calls on update_view by default
func set_voxel_set(value : Resource, update := true) -> void:
	if not (typeof(value) == TYPE_NIL or value is VoxelSet):
		printerr("Invalid Resource given expected VoxelSet")
		return
	
	voxel_set = value
	if update:
		update_view()


# Quick setup of voxel_set, voxel_id and voxel_face; calls on update_view
func setup(voxel_set : VoxelSet, voxel_id : int, voxel_face := Vector3.ZERO) -> void:
	set_voxel_set(voxel_set, false)
	set_voxel_id(voxel_id, false)
	set_voxel_face(voxel_face, false)
	update_view()


# Sets up the voxel to visualize the face of the voxel id given
func update_view() -> void:
	if typeof(voxel_set) == TYPE_NIL:
		return
	
	var voxel : Dictionary = voxel_set.get_voxel(voxel_id)
	
	hint_tooltip = str(voxel_id)
	var name = voxel_set.id_to_name(voxel_id)
	if not name.empty():
		hint_tooltip += "|" + name
	
	set_voxel_color(Voxel.get_face_color(voxel, voxel_face))
	
	if not typeof(voxel_set.tiles) == TYPE_NIL:
		var uv := Voxel.get_face_uv(voxel, voxel_face)
		if uv == -Vector2.ONE:
			set_voxel_texture(null)
		else:
			var img_texture := ImageTexture.new()
			img_texture.create_from_image(
					voxel_set.tiles.get_data().get_rect(Rect2(
							Vector2.ONE * uv * voxel_set.tile_size,
							Vector2.ONE * voxel_set.tile_size)))
			set_voxel_texture(img_texture)