1339 lines
57 KiB
GDScript
Executable File
1339 lines
57 KiB
GDScript
Executable File
@tool
|
|
class_name PdUVCanvas
|
|
extends Control
|
|
## The Texture Editor's 2D canvas: shows the active object's paint atlas
|
|
## (a checkerboard placeholder while the texture slot is empty) with
|
|
## every face's UV island outlined at its SAMPLING position.
|
|
##
|
|
## PAINT submode: Brush / Fill / Fill Object / Pixel / Eraser / Picker /
|
|
## Replace paint directly on the atlas texels (right-click does nothing -
|
|
## erasing is the dedicated Eraser tool); clicking an island also selects
|
|
## that face so its island (and the face in the 3D view) highlights.
|
|
## Imported images float as stickers - move, wheel-scale, right-click
|
|
## ROTATES 90 degrees, click to place, Esc cancels.
|
|
##
|
|
## EDIT UV submode: drag island corners, edges or whole islands to re-map
|
|
## UVs (with optional texel snapping); drag empty space to box-select
|
|
## corners; RIGHT-CLICK opens the mesh-ops context menu (rotate, detach,
|
|
## merge, extrude, fill, delete, reset). R rotates the selection 90
|
|
## degrees clockwise, Shift+R counter-clockwise. With the SCALE tool
|
|
## active (toolbar toggle) any press on the selection scales it uniformly
|
|
## around its center instead (aspect ratio preserved).
|
|
## MMB-drag pans, wheel zooms - in both submodes.
|
|
|
|
const HANDLE := 3.4 # corner handle half-size (px)
|
|
const HANDLE_HOVER := 5.0
|
|
const PICK_PX := 10.0
|
|
const EDGE_PICK_PX := 6.0
|
|
const ICONS := preload("res://addons/polydoodle/core/pd_icons.gd")
|
|
|
|
enum Op { NONE, PAN, PAINT, MOVE_UV, BOX, SCALE_UV }
|
|
|
|
var doc: PdDocument
|
|
var main: Control
|
|
|
|
var center := Vector2(128, 128) # texel coords at the view center
|
|
var zoom := 2.0 # screen px per texel
|
|
|
|
var op := Op.NONE
|
|
var pan_last := Vector2.ZERO
|
|
var press_px := Vector2.ZERO
|
|
var press_moved := false
|
|
var erase_stroke := false # current stroke erases (right button)
|
|
var stroke_before := [] # document snapshot at stroke start
|
|
var _last_stamp := Vector2(-1e9, -1e9)
|
|
var _last_cell := Vector2i(-2147483648, -2147483648)
|
|
|
|
var box_now := Vector2.ZERO
|
|
|
|
# UV drag state: corners are Vector2i(fi, corner k); offsets are ATLAS
|
|
# TEXEL units, so the dragged outline tracks the mouse 1:1.
|
|
var drag_set := [] # Array[Vector2i] (fi, corner k)
|
|
var drag_faces := [] # unique face ids in drag_set
|
|
var drag_origs := {} # fi -> PackedVector2Array at press
|
|
var drag_start_texel := Vector2.ZERO
|
|
var _snap_dt := Vector2.ZERO # effective texel delta of the active drag
|
|
var _last_dt := Vector2.INF # delta already applied (event coalescing)
|
|
var _dt_valid := false
|
|
|
|
# Imported-image sticker (paint submode)
|
|
var sticker_src: Image = null # original image
|
|
var sticker_img: Image = null # scaled copy
|
|
var sticker_tex: ImageTexture = null
|
|
var sticker_active := false
|
|
var sticker_pos := Vector2.ZERO # CENTER texel position
|
|
var sticker_scale := 1.0
|
|
var sticker_before := [] # document snapshot when imported
|
|
|
|
var hover := {}
|
|
var mouse_in := false
|
|
var mouse_px := Vector2(-1, -1)
|
|
var expand_btn: Button # fullscreen toggle for this canvas
|
|
var _shown_obj := -2 # object currently displayed (refit on change)
|
|
var _shown_atlas := 0
|
|
var _shown_tex_edit := false # submode watcher: resets stale gestures
|
|
|
|
var _menu: PopupMenu
|
|
|
|
static var _font: Font
|
|
|
|
|
|
func _init() -> void:
|
|
custom_minimum_size = Vector2(120, 90)
|
|
size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
|
size_flags_vertical = Control.SIZE_EXPAND_FILL
|
|
mouse_filter = Control.MOUSE_FILTER_STOP
|
|
mouse_default_cursor_shape = Control.CURSOR_CROSS
|
|
focus_mode = Control.FOCUS_ALL
|
|
clip_contents = true
|
|
texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
|
|
|
|
|
|
## See pd_view2d.gd's _style_corner_btn - same flat, transparent-at-rest
|
|
## look for this canvas's own corner button, for consistency.
|
|
func _style_corner_btn(b: Button) -> void:
|
|
var normal := StyleBoxFlat.new()
|
|
normal.bg_color = Color(1, 1, 1, 0)
|
|
normal.set_corner_radius_all(4)
|
|
var hover := normal.duplicate()
|
|
hover.bg_color = Color(1, 1, 1, 0.12)
|
|
var pressed := normal.duplicate()
|
|
pressed.bg_color = Color(1, 1, 1, 0.2)
|
|
b.add_theme_stylebox_override("normal", normal)
|
|
b.add_theme_stylebox_override("hover", hover)
|
|
b.add_theme_stylebox_override("pressed", pressed)
|
|
b.add_theme_stylebox_override("focus", normal)
|
|
|
|
|
|
func setup(d: PdDocument, m: Control) -> void:
|
|
doc = d
|
|
main = m
|
|
_shown_tex_edit = doc.tex_edit
|
|
doc.doc_changed.connect(_on_doc)
|
|
doc.sel_changed.connect(_on_doc)
|
|
doc.settings_changed.connect(_on_doc)
|
|
# expand/retract toggle in the top-right corner: pressed = the
|
|
# canvas alone fills the whole view area
|
|
expand_btn = Button.new()
|
|
expand_btn.icon = PdIcons.get_icon("expand")
|
|
expand_btn.toggle_mode = true
|
|
expand_btn.focus_mode = Control.FOCUS_NONE
|
|
expand_btn.tooltip_text = "Expand view\nShow only the texture canvas at full size - press again to retract"
|
|
expand_btn.custom_minimum_size = Vector2(22, 22)
|
|
expand_btn.add_theme_constant_override("icon_max_width", 16)
|
|
expand_btn.modulate = Color(1, 1, 1, 0.75)
|
|
_style_corner_btn(expand_btn)
|
|
add_child(expand_btn)
|
|
expand_btn.set_anchors_and_offsets_preset(Control.PRESET_TOP_RIGHT)
|
|
expand_btn.offset_left = -28.0
|
|
expand_btn.offset_top = 5.0
|
|
expand_btn.offset_right = -6.0
|
|
expand_btn.offset_bottom = 27.0
|
|
expand_btn.pressed.connect(func(): main._toggle_expand(self))
|
|
if main.has_method("_sync_expand_btns"):
|
|
main._sync_expand_btns.call_deferred()
|
|
|
|
|
|
func _ready() -> void:
|
|
_build_menu()
|
|
# brush-cursor wiring: without these the canvas never knows the
|
|
# mouse is inside, so the paint ring/square preview never showed
|
|
# in the 2D texture view (it only appeared on the 3D model)
|
|
mouse_entered.connect(func():
|
|
mouse_in = true
|
|
# clear a stale global drag/split cursor left by the panel
|
|
# splitter - the canvas shows its own cross shape
|
|
Input.set_default_cursor_shape(Input.CURSOR_ARROW)
|
|
queue_redraw())
|
|
mouse_exited.connect(func():
|
|
mouse_in = false
|
|
mouse_px = Vector2(-1, -1)
|
|
hover = {}
|
|
queue_redraw())
|
|
|
|
|
|
func _on_doc() -> void:
|
|
var o := active_o()
|
|
var ai := doc.active_obj
|
|
# submode switch: drop any gesture state from the other submode (a
|
|
# stale PAN press from Edit UV must never open the mesh menu after
|
|
# the user switched back to Paint - and vice versa a stale PAINT
|
|
# stroke must not keep committing)
|
|
if doc.tex_edit != _shown_tex_edit:
|
|
_shown_tex_edit = doc.tex_edit
|
|
if op == Op.PAN or op == Op.BOX:
|
|
op = Op.NONE
|
|
elif op == Op.PAINT:
|
|
_end_stroke()
|
|
op = Op.NONE
|
|
elif op == Op.MOVE_UV:
|
|
_end_uv_drag()
|
|
op = Op.NONE
|
|
elif op == Op.SCALE_UV:
|
|
_end_scale_drag()
|
|
op = Op.NONE
|
|
# filter mode drives texture sampling: Pixel = nearest, Smooth =
|
|
# linear (signal-driven so it also applies headless / when hidden)
|
|
texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST \
|
|
if doc.tex_filter == 1 else CanvasItem.TEXTURE_FILTER_LINEAR
|
|
if o != null and (ai != _shown_obj or o.atlas_size() != _shown_atlas):
|
|
_shown_obj = ai
|
|
_shown_atlas = o.atlas_size()
|
|
fit_view()
|
|
queue_redraw()
|
|
|
|
|
|
func active_o() -> PdPolyObj:
|
|
if doc == null:
|
|
return null
|
|
return doc.active()
|
|
|
|
|
|
# ------------------------------------------------------------ frames ---
|
|
func fit_view() -> void:
|
|
var o := active_o()
|
|
var n := float(o.atlas_size()) if o != null else 256.0
|
|
zoom = clampf(minf(size.x, size.y) / n * 0.92, 0.25, 40.0)
|
|
center = Vector2(n, n) * 0.5
|
|
queue_redraw()
|
|
|
|
|
|
func mouse_texel(mp: Vector2) -> Vector2:
|
|
return center + (mp - size * 0.5) / zoom
|
|
|
|
|
|
func texel_px(t: Vector2) -> Vector2:
|
|
return size * 0.5 + (t - center) * zoom
|
|
|
|
|
|
func atlas_px() -> Vector2:
|
|
var o := active_o()
|
|
return Vector2.ONE * (o.atlas_size() if o != null else 256)
|
|
|
|
|
|
# -------------------------------------------------------- context menu ---
|
|
func _build_menu() -> void:
|
|
_menu = PopupMenu.new()
|
|
_menu.add_theme_font_size_override("font_size", 12)
|
|
_menu.add_item("Rotate UV 90° CW", 12)
|
|
_menu.add_item("Rotate UV 90° CCW", 13)
|
|
_menu.add_separator()
|
|
_menu.add_item("Detach Selected Face(s)", 11)
|
|
_menu.add_separator()
|
|
_menu.add_item("Merge Vertices", 0)
|
|
_menu.add_item("Extrude Face", 1)
|
|
_menu.add_item("Extrude Edges", 2)
|
|
_menu.add_separator()
|
|
_menu.add_item("Fill Face(s)", 3)
|
|
_menu.add_item("Clear Face Paint", 4)
|
|
_menu.add_separator()
|
|
_menu.add_item("Delete Face(s)", 5)
|
|
_menu.add_item("Delete Edge(s)", 6)
|
|
_menu.add_item("Delete Vertex(es)", 7)
|
|
_menu.add_separator()
|
|
_menu.add_item("Select All Islands", 8)
|
|
_menu.add_item("Deselect", 9)
|
|
_menu.add_item("Reset UVs", 10)
|
|
_menu.id_pressed.connect(_on_menu)
|
|
add_child(_menu)
|
|
|
|
|
|
func _open_menu(at: Vector2) -> void:
|
|
var o := active_o()
|
|
# the mesh-ops menu belongs to EDIT UV ONLY - painting right-clicks
|
|
# erase; a context menu with extrude/merge/delete must never appear
|
|
# in the paint submode
|
|
if o == null or not doc.tex_edit:
|
|
return
|
|
_menu.set_item_disabled(11, doc.sel_faces.is_empty())
|
|
_menu.set_item_disabled(12, doc.sel_faces.is_empty())
|
|
_menu.set_item_disabled(13, doc.sel_faces.is_empty())
|
|
_menu.set_item_disabled(0, _menu_mesh_verts().size() < 2)
|
|
_menu.set_item_disabled(1, doc.sel_faces.size() != 1)
|
|
_menu.set_item_disabled(2, doc.sel_uv_edges.is_empty())
|
|
_menu.set_item_disabled(3, doc.sel_faces.is_empty())
|
|
_menu.set_item_disabled(4, doc.sel_faces.is_empty())
|
|
_menu.set_item_disabled(5, doc.sel_faces.is_empty())
|
|
_menu.set_item_disabled(6, doc.sel_uv_edges.is_empty())
|
|
_menu.set_item_disabled(7, _menu_mesh_verts().is_empty())
|
|
_menu.set_item_disabled(8, o.nfaces() == 0)
|
|
_menu.set_item_disabled(10, o.uv_ofs.is_empty())
|
|
# popups position in SCREEN coordinates: anchor at the canvas point
|
|
_menu.position = Vector2i(get_global_rect().position + at)
|
|
_menu.popup()
|
|
|
|
|
|
func _menu_mesh_verts() -> PackedInt32Array:
|
|
var o := active_o()
|
|
var ids := PackedInt32Array()
|
|
if o == null:
|
|
return ids
|
|
for c in doc.sel_uvs:
|
|
var fi: int = (c as Vector2i).x
|
|
var k: int = (c as Vector2i).y
|
|
if fi >= 0 and fi < o.nfaces():
|
|
var loop := o.face_loop(fi)
|
|
if k >= 0 and k < loop.size() and not ids.has(loop[k]):
|
|
ids.append(loop[k])
|
|
return ids
|
|
|
|
|
|
func _on_menu(id: int) -> void:
|
|
var oi := doc.active_obj
|
|
var o := doc.obj(oi)
|
|
if o == null:
|
|
return
|
|
match id:
|
|
12:
|
|
doc.rotate_uv_selected(oi, 1)
|
|
13:
|
|
doc.rotate_uv_selected(oi, -1)
|
|
11:
|
|
doc.uv_detach_selected(oi)
|
|
0:
|
|
doc.merge_uv_verts(oi)
|
|
1:
|
|
if doc.sel_faces.size() == 1:
|
|
doc.extrude_uv_face(oi, doc.sel_faces[0])
|
|
2:
|
|
doc.extrude_uv_edges(oi)
|
|
3:
|
|
for fi in doc.sel_faces:
|
|
doc.tex_fill_island(oi, fi, false)
|
|
4:
|
|
for fi in doc.sel_faces:
|
|
doc.tex_fill_island(oi, fi, true)
|
|
5:
|
|
doc.delete_faces_list(oi, PackedInt32Array(doc.sel_faces))
|
|
6:
|
|
var es := []
|
|
for e in doc.sel_uv_edges:
|
|
var fi: int = (e as Vector2i).x
|
|
var k: int = (e as Vector2i).y
|
|
var loop := o.face_loop(fi)
|
|
if loop.size() >= 2:
|
|
es.append(Vector2i(
|
|
loop[k % loop.size()],
|
|
loop[(k + 1) % loop.size()]))
|
|
doc.delete_edges_list(oi, es)
|
|
7:
|
|
doc.delete_verts_list(oi, _menu_mesh_verts())
|
|
8:
|
|
doc.select_all()
|
|
9:
|
|
doc.clear_element_sel()
|
|
10:
|
|
doc.uv_reset_object(oi)
|
|
|
|
|
|
# -------------------------------------------------------------- draw ---
|
|
func _draw() -> void:
|
|
if _font == null:
|
|
_font = get_theme_default_font()
|
|
draw_rect(Rect2(Vector2.ZERO, size), doc.bg_color.darkened(0.28))
|
|
var o := active_o()
|
|
if o == null or o.nfaces() == 0:
|
|
_draw_hud("Select an object, then paint or edit its texture here")
|
|
return
|
|
o.ensure_uv()
|
|
var n := float(o.atlas_size())
|
|
|
|
# texel grid when zoomed in enough to matter; the per-texel minor
|
|
# lines are capped to smaller atlases so a deep zoom on a 1024
|
|
# atlas can't emit thousands of line draws per frame
|
|
if zoom >= 5.0:
|
|
var gp0 := texel_px(Vector2.ZERO)
|
|
var gp1 := texel_px(Vector2(n, n))
|
|
var minor := Color(1, 1, 1, 0.045)
|
|
var major := Color(1, 1, 1, 0.11)
|
|
var minor_step := 1 if n <= 256 else 8
|
|
var x := 0
|
|
while x <= n:
|
|
var px := texel_px(Vector2(x, 0)).x
|
|
var is_major := x % 16 == 0
|
|
if is_major or x % minor_step == 0:
|
|
draw_line(Vector2(px, gp0.y), Vector2(px, gp1.y),
|
|
major if is_major else minor, 1.0)
|
|
var py := texel_px(Vector2(0, x)).y
|
|
if is_major or x % minor_step == 0:
|
|
draw_line(Vector2(gp0.x, py), Vector2(gp1.x, py),
|
|
major if is_major else minor, 1.0)
|
|
x += 1
|
|
|
|
# atlas frame + image. Filter mode: Smooth = linear sampling,
|
|
# Pixel = nearest (crisp texels in the editor too).
|
|
texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST \
|
|
if doc.tex_filter == 1 else CanvasItem.TEXTURE_FILTER_LINEAR
|
|
var ap := texel_px(Vector2.ZERO)
|
|
var asz := Vector2(n, n) * zoom
|
|
draw_rect(Rect2(ap - Vector2.ONE * 2, asz + Vector2.ONE * 4),
|
|
Color(0, 0, 0, 0.55), false, 2.0)
|
|
draw_texture_rect(o.get_texture(doc.show_checker),
|
|
Rect2(ap, asz), false)
|
|
|
|
# islands: outlines at the SAMPLING positions; selected faces filled
|
|
var order := []
|
|
for fi in o.nfaces():
|
|
order.append(fi)
|
|
for fi in order:
|
|
var poly := o.samp_poly(fi)
|
|
if poly.size() < 3:
|
|
continue
|
|
var sp := PackedVector2Array()
|
|
sp.resize(poly.size())
|
|
for k in poly.size():
|
|
sp[k] = texel_px(poly[k])
|
|
# Selection marks (accent fill + bright outline) exist ONLY
|
|
# in Edit UV: while PAINTING the canvas must show no
|
|
# selection highlights - the user judges pure texels there
|
|
# (the 3D model's outline follows the same rule).
|
|
var sel: bool = doc.tex_edit and doc.sel_faces.has(fi)
|
|
var hov: bool = hover.get("kind", "") == "island" \
|
|
and int(hover.get("fi", -1)) == fi
|
|
if sel:
|
|
_draw_island_fill(o, fi, Color(
|
|
PdDocument.ACCENT.r, PdDocument.ACCENT.g,
|
|
PdDocument.ACCENT.b, 0.20))
|
|
# A selected island's outline must stay readable over ANY
|
|
# paint: bright white over a dark casing, in BOTH submodes -
|
|
# a flat ACCENT outline vanishes over blue-ish paint and
|
|
# over the accent selection fill itself.
|
|
var line := Color(1, 1, 1, 0.30)
|
|
if sel:
|
|
line = Color(1, 1, 1, 0.95)
|
|
elif hov:
|
|
line = Color(1, 1, 1, 0.75)
|
|
var closed := PackedVector2Array(sp)
|
|
closed.append(sp[0])
|
|
var wid := 2.0 if sel else 1.2
|
|
if sel:
|
|
draw_polyline(closed, Color(0, 0, 0, 0.6), wid + 2.0)
|
|
draw_polyline(closed, line, wid)
|
|
|
|
# UV edit handles
|
|
if doc.tex_edit:
|
|
for fi in o.nfaces():
|
|
var loop := o.face_loop(fi)
|
|
for k in loop.size():
|
|
var c := Vector2i(fi, k)
|
|
var p := texel_px(o.corner_texel(fi, k))
|
|
var is_sel: bool = doc.sel_uvs.has(c)
|
|
var is_hov: bool = hover.get("kind", "") == "corner" \
|
|
and int(hover.get("fi", -1)) == fi \
|
|
and int(hover.get("k", -1)) == k
|
|
if is_sel:
|
|
var r := Rect2(p - Vector2.ONE * (HANDLE + 1.4),
|
|
Vector2.ONE * (HANDLE + 1.4) * 2)
|
|
draw_rect(r, Color(0, 0, 0, 0.85))
|
|
# white core (was ACCENT): the whole
|
|
# Edit-UV selection language is now
|
|
# white-over-dark like the outline
|
|
draw_rect(r.grow(-1.2), Color(1, 1, 1, 0.95))
|
|
else:
|
|
draw_rect(Rect2(
|
|
p - Vector2.ONE * (HANDLE_HOVER if is_hov else HANDLE),
|
|
Vector2.ONE * (HANDLE_HOVER if is_hov else HANDLE) * 2),
|
|
Color(1, 1, 1, 0.9 if is_hov else 0.62))
|
|
# selected island edges: white over a dark casing (was flat
|
|
# ACCENT, which disappeared over similar colors)
|
|
for e in doc.sel_uv_edges:
|
|
var fi: int = (e as Vector2i).x
|
|
var k: int = (e as Vector2i).y
|
|
if fi < 0 or fi >= o.nfaces():
|
|
continue
|
|
var loop := o.face_loop(fi)
|
|
if loop.is_empty():
|
|
continue
|
|
var kk := k % loop.size()
|
|
var p1 := texel_px(o.corner_texel(fi, kk))
|
|
var p2 := texel_px(o.corner_texel(fi, (kk + 1) % loop.size()))
|
|
draw_line(p1, p2, Color(0, 0, 0, 0.6), 5.0)
|
|
draw_line(p1, p2, Color(1, 1, 1, 0.95), 3.0)
|
|
|
|
# box select rectangle
|
|
if op == Op.BOX:
|
|
var r := Rect2(press_px, box_now - press_px).abs()
|
|
draw_rect(r, Color(PdDocument.ACCENT.r, PdDocument.ACCENT.g,
|
|
PdDocument.ACCENT.b, 0.10))
|
|
draw_rect(r, PdDocument.ACCENT, false, 1.2)
|
|
|
|
# imported-image sticker ghost
|
|
if sticker_active and sticker_tex != null:
|
|
var sz := Vector2(sticker_img.get_width(),
|
|
sticker_img.get_height())
|
|
var tl := texel_px(sticker_pos - sz * 0.5)
|
|
var sr := Rect2(tl, sz * zoom)
|
|
draw_texture_rect(sticker_tex, sr, false,
|
|
Color(1, 1, 1, 0.85))
|
|
draw_rect(sr.grow(1.0), PdDocument.ACCENT, false, 1.4)
|
|
|
|
# brush ring / eraser ring / pixel square / pick+replace crosshair
|
|
# (paint submode only)
|
|
if not doc.tex_edit and mouse_in and mouse_px.x >= 0 \
|
|
and not sticker_active:
|
|
if doc.paint_tool == "Brush" or doc.paint_tool == "Eraser":
|
|
var rad := _brush_radius_texels()
|
|
var bcol := Color(doc.paint_color.lightened(0.25),
|
|
0.9 if doc.paint_tool == "Brush" else 0.65)
|
|
draw_arc(mouse_px, maxf(3.0, rad * zoom), 0, TAU, 40, bcol, 1.6)
|
|
draw_circle(mouse_px, 2.0, Color(1, 1, 1, 0.8))
|
|
if doc.paint_tool == "Eraser":
|
|
# dashed inner ring = erase affordance
|
|
draw_arc(mouse_px, maxf(3.0, rad * zoom) - 3.0,
|
|
0, TAU, 40, Color(1, 1, 1, 0.4), 1.0)
|
|
elif doc.paint_tool == "Pixel":
|
|
var pn := maxi(1, int(doc.pixel_px))
|
|
var t := mouse_texel(mouse_px)
|
|
var tl := Vector2(int(floorf(t.x - (pn - 1) * 0.5)),
|
|
int(floorf(t.y - (pn - 1) * 0.5)))
|
|
var pr := Rect2(texel_px(tl), Vector2(pn, pn) * zoom)
|
|
var pcol := Color(doc.paint_color.lightened(0.25),
|
|
0.95 if not erase_stroke else 0.5)
|
|
draw_rect(pr, pcol, false, 1.6)
|
|
draw_rect(pr.grow(1.0), Color(0, 0, 0, 0.7), false, 1.0)
|
|
elif doc.paint_tool == "Picker" or doc.paint_tool == "Replace":
|
|
var pcol2 := doc.paint_color.lightened(0.25)
|
|
draw_arc(mouse_px, 7.0, 0, TAU, 24, pcol2, 1.5)
|
|
draw_circle(mouse_px, 1.6, Color(1, 1, 1, 0.9))
|
|
if doc.paint_tool == "Replace":
|
|
draw_arc(mouse_px, 10.0, 0, TAU, 24,
|
|
Color(1, 1, 1, 0.45), 1.0)
|
|
|
|
_draw_hud("")
|
|
|
|
|
|
func _draw_island_fill(o: PdPolyObj, fi: int, col: Color) -> void:
|
|
var tr := o.face_triangulation(fi)
|
|
var uv2: PackedVector2Array = tr["uv2"]
|
|
var tris: PackedInt32Array = tr["tris"]
|
|
var loop := o.face_loop(fi)
|
|
if uv2.size() < 3 or tris.size() < 3:
|
|
return
|
|
for t in range(0, tris.size() - 2, 3):
|
|
var pts := PackedVector2Array()
|
|
for kk in 3:
|
|
var j: int = tris[t + kk]
|
|
if j < loop.size():
|
|
pts.append(texel_px(o.corner_texel(fi, j)))
|
|
else:
|
|
pts.append(texel_px(o.island_pos_to_texel(fi, uv2[j])))
|
|
# skip degenerate triangles: Godot's internal polygon
|
|
# triangulation fails on zero-area input and prints an
|
|
# error on every redraw (bowtie faces after vertex drags)
|
|
if pts.size() == 3 and not PdGeo.tri_drawable(
|
|
pts[0], pts[1], pts[2]):
|
|
continue
|
|
draw_primitive(pts, PackedColorArray([col, col, col]),
|
|
PackedVector2Array())
|
|
|
|
|
|
func _draw_hud(msg: String) -> void:
|
|
var f := _font
|
|
if f == null:
|
|
return
|
|
var o := active_o()
|
|
var lab := msg
|
|
var hint := ""
|
|
var hint_col := Color(1, 1, 1, 0.72)
|
|
if lab == "" and o != null:
|
|
var state := "EDIT UV" if doc.tex_edit else "PAINT"
|
|
lab = "%s · %s · atlas %d x %d · %d px/texel" % [
|
|
o.obj_name, state, o.atlas_size(), o.atlas_size(),
|
|
int(zoom)]
|
|
if doc.tex_edit:
|
|
var flags := []
|
|
if doc.uv_snap:
|
|
flags.append("SNAP")
|
|
if not flags.is_empty():
|
|
lab += " · " + " ".join(flags)
|
|
elif sticker_active:
|
|
# picture-placement hints (scroll scales, right-click
|
|
# rotates) - part of the hideable tips
|
|
if doc.hints_enabled:
|
|
lab = "Image placement · Scroll: adjust scale · Right-click: rotate · Click: place · Esc: cancel"
|
|
hint_col = Color(1, 1, 1, 0.9)
|
|
else:
|
|
hint = PdMain.context_hint(doc, "canvas")
|
|
var ts := f.get_string_size(lab, HORIZONTAL_ALIGNMENT_LEFT, -1, 11)
|
|
draw_rect(Rect2(Vector2(4, size.y - ts.y - 12), ts + Vector2(12, 8)),
|
|
Color(0, 0, 0, 0.4))
|
|
draw_string(f, Vector2(10, size.y - 8), lab,
|
|
HORIZONTAL_ALIGNMENT_LEFT, -1, 11, hint_col)
|
|
# contextual hint line just above the state line
|
|
if hint != "":
|
|
var ts2 := f.get_string_size(hint,
|
|
HORIZONTAL_ALIGNMENT_LEFT, -1, 11)
|
|
draw_rect(Rect2(Vector2(4, size.y - ts2.y - ts.y - 20),
|
|
ts2 + Vector2(12, 8)), Color(0, 0, 0, 0.4))
|
|
draw_string(f, Vector2(10, size.y - ts.y - 16), hint,
|
|
HORIZONTAL_ALIGNMENT_LEFT, -1, 11, Color(1, 1, 1, 0.72))
|
|
|
|
|
|
# ------------------------------------------------------------- input ---
|
|
func _gui_input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
_mi(event)
|
|
elif event is InputEventMouseMotion:
|
|
_mm(event)
|
|
elif event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
|
|
if sticker_active:
|
|
_cancel_sticker()
|
|
accept_event()
|
|
|
|
|
|
func _zoom_at(mp: Vector2, factor: float) -> void:
|
|
var t_before := mouse_texel(mp)
|
|
zoom = clampf(zoom * factor, 0.25, 40.0)
|
|
center = t_before - (mp - size * 0.5) / zoom
|
|
queue_redraw()
|
|
|
|
|
|
func _brush_radius_texels() -> float:
|
|
var o := active_o()
|
|
if o == null:
|
|
return 8.0
|
|
return maxf(1.0, doc.brush_size * o.texels_per_unit())
|
|
|
|
|
|
func _mi(e: InputEventMouseButton) -> void:
|
|
var mp := e.position
|
|
match e.button_index:
|
|
MOUSE_BUTTON_MIDDLE:
|
|
if e.pressed:
|
|
op = Op.PAN
|
|
pan_last = mp
|
|
accept_event()
|
|
elif op == Op.PAN:
|
|
op = Op.NONE
|
|
accept_event()
|
|
MOUSE_BUTTON_WHEEL_UP:
|
|
if e.pressed:
|
|
if sticker_active:
|
|
_sticker_scale(1.15)
|
|
else:
|
|
_zoom_at(mp, 1.15)
|
|
accept_event()
|
|
MOUSE_BUTTON_WHEEL_DOWN:
|
|
if e.pressed:
|
|
if sticker_active:
|
|
_sticker_scale(1.0 / 1.15)
|
|
else:
|
|
_zoom_at(mp, 1.0 / 1.15)
|
|
accept_event()
|
|
MOUSE_BUTTON_RIGHT:
|
|
if e.pressed:
|
|
if sticker_active:
|
|
# right-click ROTATES the sticker 90
|
|
# degrees (Esc still cancels)
|
|
_sticker_rotate()
|
|
accept_event()
|
|
return
|
|
press_px = mp
|
|
press_moved = false
|
|
if doc.tex_edit:
|
|
op = Op.PAN # drag pans; bare click = menu
|
|
pan_last = mp
|
|
# PAINT submode: right-click does nothing -
|
|
# erasing is the dedicated Eraser TOOL
|
|
accept_event()
|
|
elif op == Op.PAN and doc.tex_edit:
|
|
op = Op.NONE
|
|
if not press_moved:
|
|
_open_menu(mp)
|
|
accept_event()
|
|
elif op == Op.PAINT:
|
|
_end_stroke()
|
|
accept_event()
|
|
MOUSE_BUTTON_LEFT:
|
|
if e.pressed:
|
|
_press_left(mp)
|
|
else:
|
|
_release_left(mp)
|
|
|
|
|
|
func _press_left(mp: Vector2) -> void:
|
|
press_px = mp
|
|
press_moved = false
|
|
grab_focus()
|
|
# placing an imported image always wins
|
|
if sticker_active:
|
|
_place_sticker()
|
|
return
|
|
if doc.tex_edit:
|
|
_press_edit(mp)
|
|
else:
|
|
_press_paint(mp)
|
|
|
|
|
|
# ----- PAINT submode -----
|
|
func _press_paint(mp: Vector2) -> void:
|
|
var o := active_o()
|
|
if o == null or not doc.obj_editable(doc.active_obj):
|
|
return
|
|
var fi := _pick_island(mp)
|
|
if fi >= 0:
|
|
var add := Input.is_key_pressed(KEY_SHIFT)
|
|
doc.select_faces(PackedInt32Array([fi]), add)
|
|
# FACE LOCK: the stroke locks to the island it started
|
|
# on (-1 when the press landed on empty canvas: no lock applies)
|
|
doc.paint_stroke_begin(doc.active_obj, fi)
|
|
op = Op.PAINT
|
|
_begin_stroke(mp, false)
|
|
|
|
|
|
func _begin_stroke(mp: Vector2, erase: bool) -> void:
|
|
var o := active_o()
|
|
if o == null or not doc.obj_editable(doc.active_obj):
|
|
return
|
|
erase_stroke = erase
|
|
doc.begin_gesture()
|
|
stroke_before = doc.snapshot()
|
|
_last_stamp = Vector2(-1e9, -1e9)
|
|
_last_cell = Vector2i(-2147483648, -2147483648)
|
|
op = Op.PAINT
|
|
_stamp(mp)
|
|
|
|
|
|
func _stamp(mp: Vector2) -> void:
|
|
var o := active_o()
|
|
if o == null:
|
|
return
|
|
var t := mouse_texel(mp)
|
|
var lock_fi := doc.stroke_lock_face(doc.active_obj)
|
|
match doc.paint_tool:
|
|
"Brush":
|
|
var rad := _brush_radius_texels()
|
|
if _last_stamp.distance_to(t) < maxf(1.5, rad * 0.3) \
|
|
and _last_stamp.x > -1e8:
|
|
return
|
|
_last_stamp = t
|
|
o.canvas_brush(t, rad, doc.paint_color, erase_stroke,
|
|
doc.brush_opacity, doc.brush_smooth, lock_fi)
|
|
"Eraser":
|
|
# dedicated erase tool: brush-style stamp restoring
|
|
# blank. Same opacity / smoothness /
|
|
# face-lock settings as the Brush.
|
|
var erad := _brush_radius_texels()
|
|
if _last_stamp.distance_to(t) < maxf(1.5, erad * 0.3) \
|
|
and _last_stamp.x > -1e8:
|
|
return
|
|
_last_stamp = t
|
|
o.canvas_brush(t, erad, doc.paint_color, true,
|
|
doc.brush_opacity, doc.brush_smooth, lock_fi)
|
|
"Picker":
|
|
# eyedropper: click-only, pure setting (no undo)
|
|
if _last_stamp.x > -1e8:
|
|
return
|
|
_last_stamp = t
|
|
var pc := o.texel_color_at(
|
|
int(floorf(t.x)), int(floorf(t.y)))
|
|
if pc.a >= 0.01:
|
|
doc.paint_color = pc
|
|
doc.remember_color(pc)
|
|
doc.settings_changed.emit()
|
|
doc.say("Picked color %s" % pc.to_html(false))
|
|
return
|
|
"Replace":
|
|
# click a texel: its color becomes the paint color
|
|
# across the WHOLE canvas (all faces) - the
|
|
# tolerance slider widens the color match
|
|
if _last_stamp.x > -1e8:
|
|
return
|
|
_last_stamp = t
|
|
var rfrom := o.texel_color_at(
|
|
int(floorf(t.x)), int(floorf(t.y)))
|
|
if rfrom.a >= 0.01 or doc.replace_tolerance > 0.0:
|
|
doc.tex_replace_color(doc.active_obj, rfrom)
|
|
return
|
|
"Pixel":
|
|
# texel-perfect: stamp an N x N block aligned to the
|
|
# atlas texel grid; drag interpolates a texel line so
|
|
# fast strokes stay gap-free. FACE LOCK: stamps go
|
|
# through the face-restricted path (never crosses the
|
|
# locked island's edge).
|
|
var n := maxi(1, int(doc.pixel_px))
|
|
var tl := Vector2i(
|
|
int(floorf(t.x - (n - 1) * 0.5)),
|
|
int(floorf(t.y - (n - 1) * 0.5)))
|
|
if tl == _last_cell:
|
|
return
|
|
if lock_fi >= 0:
|
|
if _last_cell.x > -2147483648:
|
|
o.pixel_face_line(lock_fi,
|
|
Vector2(_last_cell), t, true,
|
|
n, doc.paint_color, erase_stroke)
|
|
else:
|
|
o.pixel_face_block(lock_fi, t, n,
|
|
doc.paint_color, erase_stroke)
|
|
elif _last_cell.x > -2147483648:
|
|
_pixel_line(o, _last_cell, tl, n)
|
|
else:
|
|
o.pixel_block_texels(tl, n,
|
|
doc.paint_color, erase_stroke)
|
|
_last_cell = tl
|
|
"Fill":
|
|
if _last_stamp.x > -1e8:
|
|
return
|
|
_last_stamp = t
|
|
var fi := _pick_island(mp)
|
|
if fi >= 0:
|
|
doc.tex_fill_island(doc.active_obj, fi, erase_stroke)
|
|
"Fill Object":
|
|
if _last_stamp.x > -1e8:
|
|
return
|
|
_last_stamp = t
|
|
doc.tex_fill_object(doc.active_obj, erase_stroke)
|
|
doc.notify_doc()
|
|
|
|
|
|
## Bresenham line of pixel blocks between two block positions (canvas
|
|
## side of the pixel brush - keeps fast strokes continuous).
|
|
func _pixel_line(o: PdPolyObj, from_tl: Vector2i, to_tl: Vector2i,
|
|
n: int) -> void:
|
|
var x0 := from_tl.x
|
|
var y0 := from_tl.y
|
|
var x1 := to_tl.x
|
|
var y1 := to_tl.y
|
|
var dx := absi(x1 - x0)
|
|
var dy := -absi(y1 - y0)
|
|
var sx := 1 if x0 < x1 else -1
|
|
var sy := 1 if y0 < y1 else -1
|
|
var err := dx + dy
|
|
while true:
|
|
o.pixel_block_texels(Vector2i(x0, y0), n,
|
|
doc.paint_color, erase_stroke)
|
|
if x0 == x1 and y0 == y1:
|
|
break
|
|
var e2 := 2 * err
|
|
if e2 >= dy:
|
|
err += dy
|
|
x0 += sx
|
|
if e2 <= dx:
|
|
err += dx
|
|
y0 += sy
|
|
|
|
|
|
func _end_stroke() -> void:
|
|
if op == Op.PAINT and not stroke_before.is_empty():
|
|
doc.commit_tex(
|
|
"Paint texture" if not erase_stroke else "Erase texture",
|
|
stroke_before)
|
|
doc.end_gesture()
|
|
stroke_before = []
|
|
erase_stroke = false
|
|
doc.paint_stroke_end() # face-lock stroke memory (harmless if none)
|
|
|
|
|
|
# ----- EDIT UV submode -----
|
|
## Clicks and box selects respect the element mode (doc.uv_elem_mode):
|
|
## vertex mode grabs corners, edge mode grabs island edges, face mode
|
|
## grabs whole islands. A plain click REPLACES the selection; Ctrl OR
|
|
## Shift+click ADDS to it; a bare click on empty canvas clears it.
|
|
## Grabbing a member of the current selection moves the WHOLE selection.
|
|
func _press_edit(mp: Vector2) -> void:
|
|
var o := active_o()
|
|
if o == null:
|
|
return
|
|
var add := Input.is_key_pressed(KEY_SHIFT) or Input.is_key_pressed(KEY_CTRL)
|
|
var mode: String = doc.uv_elem_mode
|
|
if mode == "vertex":
|
|
var hit := _pick_corner(mp)
|
|
if not hit.is_empty():
|
|
_uv_corner_press(Vector2i(hit["fi"], hit["k"]), add, mp)
|
|
return
|
|
elif mode == "edge":
|
|
var eh := _pick_edge_uv(mp)
|
|
if not eh.is_empty():
|
|
if doc.uv_tool == "scale" and _begin_scale_drag(mp):
|
|
return
|
|
var fi: int = eh["fi"]
|
|
var k: int = eh["k"]
|
|
var loop := o.face_loop(fi)
|
|
var ekey := Vector2i(fi, k)
|
|
var already: bool = doc.sel_uv_edges.has(ekey)
|
|
if add or not already:
|
|
var es := doc.sel_uv_edges.duplicate() if add else []
|
|
if not es.has(ekey):
|
|
es.append(ekey)
|
|
doc.select_uv_edges(es)
|
|
# drag every SELECTED edge's endpoints - pressing one edge of a
|
|
# multi-edge selection moves them all together
|
|
var corners := []
|
|
for e in doc.sel_uv_edges:
|
|
var efi: int = (e as Vector2i).x
|
|
var ek: int = (e as Vector2i).y
|
|
var eloop := o.face_loop(efi)
|
|
if eloop.is_empty():
|
|
continue
|
|
corners.append(Vector2i(efi, ek))
|
|
corners.append(Vector2i(efi, (ek + 1) % eloop.size()))
|
|
if not corners.is_empty():
|
|
doc.select_uvs(corners, false)
|
|
_begin_uv_drag(corners, mp)
|
|
return
|
|
# face mode (default): pick an island
|
|
var fi := _pick_island(mp)
|
|
if fi >= 0:
|
|
var already: bool = doc.sel_faces.has(fi)
|
|
if add or not already:
|
|
doc.select_faces(PackedInt32Array([fi]), add)
|
|
if doc.sel_faces.has(fi):
|
|
# SCALE tool: any press on the selection scales it
|
|
# uniformly around the frozen selection center -
|
|
# grab a corner (or anywhere on the island) and drag
|
|
# out to grow, in to shrink. Aspect ratio preserved.
|
|
if doc.uv_tool == "scale":
|
|
if _begin_scale_drag(mp):
|
|
return
|
|
# drag the whole selection: every corner of every selected
|
|
# face moves together (exactly the selected faces)
|
|
var corners := []
|
|
for sf in doc.sel_faces:
|
|
var loop := o.face_loop(sf)
|
|
for k in loop.size():
|
|
corners.append(Vector2i(sf, k))
|
|
if not corners.is_empty():
|
|
doc.select_uvs(corners, false)
|
|
_begin_uv_drag(corners, mp)
|
|
return
|
|
# empty: box-select the active element type
|
|
op = Op.BOX
|
|
box_now = mp
|
|
|
|
|
|
## A corner press in vertex mode. Plain click on an unselected corner
|
|
## selects it alone; plain click on a corner that is already part of the
|
|
## selection keeps the whole selection; Ctrl/Shift adds the corner. The
|
|
## drag then moves EVERY selected corner - grabbing one vertex of a box
|
|
## selection must drag them all, not just the grabbed one.
|
|
func _uv_corner_press(c: Vector2i, add: bool, mp: Vector2) -> void:
|
|
if doc.uv_tool == "scale" and _begin_scale_drag(mp):
|
|
return
|
|
var was_sel: bool = doc.sel_uvs.has(c)
|
|
if add or not was_sel:
|
|
var sel := doc.sel_uvs.duplicate() if add else []
|
|
if not sel.has(c):
|
|
sel.append(c)
|
|
doc.sel_uv_edges = []
|
|
doc.select_uvs(sel)
|
|
var drag_corners := doc.sel_uvs.duplicate()
|
|
if drag_corners.is_empty():
|
|
drag_corners = [c]
|
|
_begin_uv_drag(drag_corners, mp)
|
|
|
|
|
|
func _begin_uv_drag(corners: Array, mp: Vector2) -> void:
|
|
var o := active_o()
|
|
if o == null:
|
|
return
|
|
o.ensure_uv()
|
|
press_px = mp
|
|
drag_set = corners
|
|
drag_faces = []
|
|
drag_origs = {}
|
|
for c in corners:
|
|
var fi: int = (c as Vector2i).x
|
|
if fi < 0 or fi >= o.nfaces():
|
|
continue
|
|
if not drag_faces.has(fi):
|
|
drag_faces.append(fi)
|
|
if not drag_origs.has(fi):
|
|
drag_origs[fi] = o.uv_offsets(fi)
|
|
stroke_before = doc.snapshot()
|
|
drag_start_texel = mouse_texel(mp)
|
|
op = Op.MOVE_UV
|
|
_last_dt = Vector2.INF
|
|
_dt_valid = false
|
|
doc.begin_gesture()
|
|
|
|
|
|
func _apply_uv_drag(mp: Vector2) -> void:
|
|
var o := active_o()
|
|
if o == null or drag_faces.is_empty():
|
|
return
|
|
var dt := mouse_texel(mp) - drag_start_texel
|
|
# Grid snap: land the dragged corners EXACTLY on the texel grid.
|
|
# The corner TARGET is rounded (not just the delta): an island
|
|
# whose corners sit at fractional texel positions - after vertex
|
|
# stretches the bbox mapping lands them at x.37 - then snaps edge
|
|
# to edge with the atlas pixels.
|
|
# Hold Ctrl for a free (sub-texel) drag even when snapping is on.
|
|
for c in drag_set:
|
|
var fi0: int = (c as Vector2i).x
|
|
var k0: int = (c as Vector2i).y
|
|
if drag_origs.has(fi0):
|
|
var orig0: PackedVector2Array = drag_origs[fi0]
|
|
if k0 < orig0.size():
|
|
_snap_dt = (orig0[k0] + dt).round() - orig0[k0]
|
|
break
|
|
if not doc.uv_snap or Input.is_key_pressed(KEY_CTRL):
|
|
_snap_dt = dt
|
|
# identical target as the already-applied step: nothing to do.
|
|
# Motion events burst at 60-1000 Hz and a hand resting on one
|
|
# snapped texel would otherwise rewrite offsets forever - this
|
|
# guard is what makes "stopped while holding" a true no-op.
|
|
if _dt_valid and _snap_dt == _last_dt:
|
|
return
|
|
_last_dt = _snap_dt
|
|
_dt_valid = true
|
|
var changed := false
|
|
for c in drag_set:
|
|
var fi: int = (c as Vector2i).x
|
|
var k: int = (c as Vector2i).y
|
|
if not drag_origs.has(fi):
|
|
continue
|
|
var orig: PackedVector2Array = drag_origs[fi]
|
|
if k >= orig.size():
|
|
continue
|
|
var want := orig[k] + _snap_dt
|
|
var cur := o.uv_offsets(fi)
|
|
if k < cur.size() \
|
|
and want.distance_squared_to(cur[k]) <= 1e-9:
|
|
continue
|
|
changed = true
|
|
# light write: no ensure_uv re-param, no triangulation wipe;
|
|
# bumps visual_epoch so the pooled 3D mesh rebuilds while
|
|
# the UVs move
|
|
o.set_uv_offset_light(fi, k, want)
|
|
if not changed:
|
|
return
|
|
# the mesh UVs move live - one coalesced mesh rebuild per frame
|
|
# (deferred in the 3D view)
|
|
doc.notify_doc()
|
|
|
|
|
|
func _end_uv_drag() -> void:
|
|
if not stroke_before.is_empty():
|
|
doc.commit_tex("Edit UVs", stroke_before)
|
|
doc.end_gesture()
|
|
stroke_before = []
|
|
drag_set = []
|
|
drag_faces = []
|
|
drag_origs = {}
|
|
_dt_valid = false
|
|
|
|
|
|
# ----- SCALE tool (uniform corner-drag scaling) -----
|
|
var _scale_start_dist := 1.0
|
|
var _scale_last_factor := 1.0
|
|
|
|
|
|
## Start a uniform scale gesture on the current face selection. Returns
|
|
## false when there is nothing selected to scale (the press then falls
|
|
## through to the normal move/box behavior).
|
|
func _begin_scale_drag(mp: Vector2) -> bool:
|
|
var o := active_o()
|
|
if o == null or doc.sel_faces.is_empty():
|
|
return false
|
|
if not doc.uv_gesture_begin(doc.active_obj):
|
|
return false
|
|
var ctr := doc.uv_gesture_center()
|
|
var t := mouse_texel(mp)
|
|
_scale_start_dist = maxf(1.0, t.distance_to(ctr))
|
|
_scale_last_factor = 1.0
|
|
press_px = mp
|
|
op = Op.SCALE_UV
|
|
return true
|
|
|
|
|
|
func _apply_scale_drag(mp: Vector2) -> void:
|
|
if not doc.uv_gesture_active():
|
|
return
|
|
var ctr := doc.uv_gesture_center()
|
|
if ctr == Vector2.INF:
|
|
return
|
|
var t := mouse_texel(mp)
|
|
# single factor for every corner: the aspect ratio can never drift.
|
|
# The gesture accumulates factors multiplicatively, so send the
|
|
# RELATIVE step (current / last)
|
|
var f := clampf(t.distance_to(ctr) / _scale_start_dist, 0.02, 50.0)
|
|
doc.apply_uv_scale(f / _scale_last_factor)
|
|
_scale_last_factor = f
|
|
|
|
|
|
func _end_scale_drag() -> void:
|
|
if doc.uv_gesture_active():
|
|
doc.uv_gesture_end("Scale UVs")
|
|
|
|
|
|
func _release_left(mp: Vector2) -> void:
|
|
match op:
|
|
Op.PAINT:
|
|
_end_stroke()
|
|
Op.MOVE_UV:
|
|
_end_uv_drag()
|
|
Op.SCALE_UV:
|
|
_end_scale_drag()
|
|
Op.BOX:
|
|
_box_select(mp)
|
|
_:
|
|
pass
|
|
if op != Op.NONE:
|
|
op = Op.NONE
|
|
accept_event()
|
|
|
|
|
|
func _box_select(mp: Vector2) -> void:
|
|
var o := active_o()
|
|
if o == null:
|
|
return
|
|
var r := Rect2(press_px, mp - press_px).abs()
|
|
if r.size.length() < 4.0:
|
|
# a bare click on empty canvas (no drag) deselects everything
|
|
doc.clear_element_sel()
|
|
queue_redraw()
|
|
return
|
|
var add := Input.is_key_pressed(KEY_SHIFT) or Input.is_key_pressed(KEY_CTRL)
|
|
var mode: String = doc.uv_elem_mode
|
|
if mode == "vertex":
|
|
var sel := doc.sel_uvs.duplicate() if add else []
|
|
for fi in o.nfaces():
|
|
var loop := o.face_loop(fi)
|
|
for k in loop.size():
|
|
if r.has_point(texel_px(o.corner_texel(fi, k))):
|
|
var c := Vector2i(fi, k)
|
|
if not sel.has(c):
|
|
sel.append(c)
|
|
doc.sel_uv_edges = []
|
|
doc.select_uvs(sel)
|
|
elif mode == "edge":
|
|
var sel := doc.sel_uv_edges.duplicate() if add else []
|
|
for fi in o.nfaces():
|
|
var loop := o.face_loop(fi)
|
|
for k in loop.size():
|
|
var p1 := texel_px(o.corner_texel(fi, k))
|
|
var p2 := texel_px(o.corner_texel(fi,
|
|
(k + 1) % loop.size()))
|
|
if r.has_point(p1) and r.has_point(p2):
|
|
var e := Vector2i(fi, k)
|
|
if not sel.has(e):
|
|
sel.append(e)
|
|
doc.select_uv_edges(sel)
|
|
# the drag set of an edge selection is its endpoints
|
|
var corners := []
|
|
for e in doc.sel_uv_edges:
|
|
var efi: int = (e as Vector2i).x
|
|
var ek: int = (e as Vector2i).y
|
|
var eloop := o.face_loop(efi)
|
|
if eloop.is_empty():
|
|
continue
|
|
corners.append(Vector2i(efi, ek))
|
|
corners.append(Vector2i(efi, (ek + 1) % eloop.size()))
|
|
doc.select_uvs(corners, false)
|
|
else:
|
|
# face mode: any island whose sampling bbox overlaps the rect
|
|
var sel: PackedInt32Array = doc.sel_faces.duplicate() if add \
|
|
else PackedInt32Array()
|
|
var tr := Rect2(mouse_texel(r.position),
|
|
mouse_texel(r.end) - mouse_texel(r.position)).abs()
|
|
for fi in o.nfaces():
|
|
var poly := o.samp_poly(fi)
|
|
if poly.size() < 3:
|
|
continue
|
|
var lo := poly[0]
|
|
var hi := poly[0]
|
|
for p in poly:
|
|
lo = lo.min(p)
|
|
hi = hi.max(p)
|
|
if Rect2(lo, hi - lo).intersects(tr) \
|
|
and not sel.has(fi):
|
|
sel.append(fi)
|
|
doc.select_faces(sel)
|
|
# the island drag set is every corner of every selected face
|
|
var corners := []
|
|
for sf in doc.sel_faces:
|
|
var loop := o.face_loop(sf)
|
|
for k in loop.size():
|
|
corners.append(Vector2i(sf, k))
|
|
doc.select_uvs(corners, false)
|
|
|
|
|
|
func _mm(e: InputEventMouseMotion) -> void:
|
|
mouse_px = e.position
|
|
if e.relative.length() > 1.0:
|
|
press_moved = true
|
|
match op:
|
|
Op.PAN:
|
|
center -= (e.position - pan_last) / zoom
|
|
pan_last = e.position
|
|
Op.PAINT:
|
|
_stamp(e.position)
|
|
Op.MOVE_UV:
|
|
_apply_uv_drag(e.position)
|
|
Op.SCALE_UV:
|
|
_apply_scale_drag(e.position)
|
|
Op.BOX:
|
|
box_now = e.position
|
|
Op.NONE:
|
|
if sticker_active:
|
|
sticker_pos = mouse_texel(e.position)
|
|
elif mouse_in:
|
|
_update_hover(e.position)
|
|
queue_redraw()
|
|
|
|
|
|
# ------------------------------------------------------------ sticker ---
|
|
## Start floating an imported image on the canvas (called by main after
|
|
## the file dialog picked an image).
|
|
func begin_sticker(img: Image) -> void:
|
|
if img == null or img.is_empty():
|
|
return
|
|
var o := active_o()
|
|
if o == null:
|
|
doc.say("Select an object first, then import the image")
|
|
return
|
|
sticker_src = img.duplicate()
|
|
sticker_src.convert(Image.FORMAT_RGBA8)
|
|
sticker_scale = 1.0
|
|
# fit oversized imports into the atlas
|
|
var n := float(o.atlas_size())
|
|
var m := maxi(sticker_src.get_width(), sticker_src.get_height())
|
|
if m > n * 0.6:
|
|
sticker_scale = n * 0.6 / m
|
|
_apply_sticker_scale()
|
|
sticker_pos = center
|
|
sticker_active = true
|
|
sticker_before = doc.snapshot()
|
|
doc.say("Image imported - move the mouse, wheel adjusts the scale, right-click rotates, click places it, Esc cancels")
|
|
queue_redraw()
|
|
|
|
|
|
func _apply_sticker_scale() -> void:
|
|
if sticker_src == null:
|
|
return
|
|
var w := maxi(2, int(roundf(sticker_src.get_width() * sticker_scale)))
|
|
var h := maxi(2, int(roundf(sticker_src.get_height() * sticker_scale)))
|
|
sticker_img = sticker_src.duplicate()
|
|
sticker_img.resize(w, h, Image.INTERPOLATE_NEAREST)
|
|
sticker_tex = ImageTexture.create_from_image(sticker_img)
|
|
|
|
|
|
func _sticker_scale(factor: float) -> void:
|
|
var o := active_o()
|
|
var n := float(o.atlas_size()) if o != null else 256.0
|
|
sticker_scale = clampf(sticker_scale * factor,
|
|
4.0 / maxi(sticker_src.get_width(), sticker_src.get_height()),
|
|
n * 1.2 / maxi(sticker_src.get_width(), sticker_src.get_height()))
|
|
_apply_sticker_scale()
|
|
queue_redraw()
|
|
|
|
|
|
func _place_sticker() -> void:
|
|
if sticker_img == null:
|
|
_cancel_sticker()
|
|
return
|
|
var sz := Vector2(sticker_img.get_width(), sticker_img.get_height())
|
|
var tl := (sticker_pos - sz * 0.5).floor()
|
|
doc.tex_place_image(sticker_img, tl, sticker_before)
|
|
_cancel_sticker()
|
|
|
|
|
|
## Rotate the floating sticker 90 degrees (right-click while placing).
|
|
func _sticker_rotate() -> void:
|
|
if sticker_src == null:
|
|
return
|
|
sticker_src.rotate_90(COUNTERCLOCKWISE)
|
|
_apply_sticker_scale()
|
|
doc.say("Rotated 90 degrees - click places, wheel scales, Esc cancels")
|
|
queue_redraw()
|
|
|
|
|
|
func _cancel_sticker() -> void:
|
|
sticker_active = false
|
|
sticker_src = null
|
|
sticker_img = null
|
|
sticker_tex = null
|
|
sticker_before = []
|
|
doc.notify_doc()
|
|
doc.say("Image placement canceled")
|
|
queue_redraw()
|
|
|
|
|
|
# ------------------------------------------------------------- hover ---
|
|
func _update_hover(mp: Vector2) -> void:
|
|
if doc.tex_edit:
|
|
var mode: String = doc.uv_elem_mode
|
|
if mode == "vertex":
|
|
var c := _pick_corner(mp)
|
|
if not c.is_empty():
|
|
_set_hover({"kind": "corner", "fi": c["fi"],
|
|
"k": c["k"]})
|
|
else:
|
|
_set_hover({})
|
|
return
|
|
if mode == "edge":
|
|
var eh := _pick_edge_uv(mp)
|
|
if not eh.is_empty():
|
|
_set_hover({"kind": "edge", "fi": eh["fi"],
|
|
"k": eh["k"]})
|
|
else:
|
|
_set_hover({})
|
|
return
|
|
var fi := _pick_island(mp)
|
|
_set_hover({} if fi < 0 else {"kind": "island", "fi": fi})
|
|
|
|
|
|
func _set_hover(h: Dictionary) -> void:
|
|
if hover != h:
|
|
hover = h
|
|
queue_redraw()
|
|
|
|
|
|
# ------------------------------------------------------------ picking ---
|
|
func _pick_island(mp: Vector2) -> int:
|
|
var o := active_o()
|
|
if o == null:
|
|
return -1
|
|
var t := mouse_texel(mp)
|
|
# topmost = LAST face whose sampling polygon contains the point
|
|
for fi in range(o.nfaces() - 1, -1, -1):
|
|
if o.island_contains(fi, t):
|
|
return fi
|
|
# tolerance pass: clicking ON the island outline (or in a sliver
|
|
# wedge between neighboring islands) contains nothing exactly -
|
|
# accept a point about one texel away so Fill / Replace / selection
|
|
# still work when the user aims at the visible line
|
|
for off in [Vector2(1, 0), Vector2(-1, 0), Vector2(0, 1),
|
|
Vector2(0, -1), Vector2(0.7, 0.7),
|
|
Vector2(-0.7, 0.7), Vector2(0.7, -0.7),
|
|
Vector2(-0.7, -0.7)]:
|
|
var t2: Vector2 = t + off
|
|
for fi in range(o.nfaces() - 1, -1, -1):
|
|
if o.island_contains(fi, t2):
|
|
return fi
|
|
return -1
|
|
|
|
|
|
func _pick_corner(mp: Vector2) -> Dictionary:
|
|
var o := active_o()
|
|
if o == null:
|
|
return {}
|
|
var best_d := PICK_PX
|
|
var best := {}
|
|
for fi in o.nfaces():
|
|
var loop := o.face_loop(fi)
|
|
for k in loop.size():
|
|
var d := mp.distance_to(texel_px(o.corner_texel(fi, k)))
|
|
if d < best_d:
|
|
best_d = d
|
|
best = {"fi": fi, "k": k}
|
|
return best
|
|
|
|
|
|
func _pick_edge_uv(mp: Vector2) -> Dictionary:
|
|
var o := active_o()
|
|
if o == null:
|
|
return {}
|
|
var best_d := EDGE_PICK_PX
|
|
var best := {}
|
|
for fi in o.nfaces():
|
|
var loop := o.face_loop(fi)
|
|
for k in loop.size():
|
|
var p1 := texel_px(o.corner_texel(fi, k))
|
|
var p2 := texel_px(o.corner_texel(fi, (k + 1) % loop.size()))
|
|
var d := PdGeo.dist_to_seg(mp, p1, p2)
|
|
if d < best_d:
|
|
best_d = d
|
|
best = {"fi": fi, "k": k}
|
|
return best
|