-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor_texture.gd
53 lines (38 loc) · 1.12 KB
/
editor_texture.gd
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
@tool
extends ColorRect
var bitmap: BitMap
var pixel_size: float
var mouse_at: Vector2i
var pressed: bool = false
func _ready() -> void:
bitmap = owner.bitmap
update_size.call_deferred()
func update_size() -> void:
pixel_size = get_parent().size.x / float(bitmap.get_size().x)
custom_minimum_size = Vector2(bitmap.get_size()) * pixel_size
queue_redraw()
func _draw() -> void:
for x: int in bitmap.get_size().x:
for y: int in bitmap.get_size().y:
if bitmap.get_bit(x, y):
draw_rect(Rect2(
Vector2(x, y) * pixel_size,
Vector2(pixel_size, pixel_size)
), Color.WHITE)
draw_rect(Rect2(
mouse_at * pixel_size,
Vector2(pixel_size, pixel_size)
), Color("#80808040"))
func _gui_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
mouse_at = event.position / pixel_size
if pressed:
_set_bit_under_mouse(event)
queue_redraw()
elif event is InputEventMouseButton:
pressed = event.pressed
if pressed:
_set_bit_under_mouse(event)
queue_redraw()
func _set_bit_under_mouse(event: InputEventMouse) -> void:
bitmap.set_bitv(mouse_at, event.button_mask == MOUSE_BUTTON_LEFT)