class_name ThreadStrip extends RefCounted ## A theme's thread picture: one greyscale strip repeated along a thread's ## curve and recoloured by GradientMap (the thread's colour, else THREAD; lit ## threads use ACCENT), with an optional pin picture at both ends. ## See docs/THEMES.md, "Thread strip". ## The strip for the theme in use, or null for the drawn threads. ThemeStore.apply sets it. static var active: ThreadStrip = null var texture: Texture2D var pin: Texture2D ## Source size of one tile [length, height]; drawn `width` px thick. var strip_size := Vector2(64, 16) var width := 10.0 var pin_size := 14.0 var lo := 0.05 var mid := 0.5 var highlight := 0.45 static func for_theme(theme_set: Dictionary) -> ThreadStrip: var keys: Dictionary = theme_set.get("thread", {}) var tex := ThemeAssets.texture(theme_set, str(keys.get("strip", ""))) if tex == null: return null var ts := ThreadStrip.new() ts.texture = tex ts.strip_size = ThemeAssets.vec2(keys.get("strip_size"), tex.get_size()) ts.width = float(keys.get("width", 10.0)) ts.pin = ThemeAssets.texture(theme_set, str(keys.get("pin", ""))) ts.pin_size = float(keys.get("pin_size", 14.0)) ts.lo = float(keys.get("lo", 0.05)) ts.mid = float(keys.get("mid", 0.5)) ts.highlight = float(keys.get("highlight", 0.45)) return ts ## Draws the strip along `pts` into the canvas item `rid`, coloured `trim` ## over `dark`. `rid` gets the recolouring material, so draw nothing else in it. func add_to(rid: RID, pts: PackedVector2Array, trim: Color, dark: Color) -> void: var count := pts.size() if count < 2: return var tile_length := strip_size.x * width / strip_size.y var half := width / 2.0 var points := PackedVector2Array() var uvs := PackedVector2Array() var colors := PackedColorArray() var indices := PackedInt32Array() var along := 0.0 for i in count: if i > 0: along += pts[i].distance_to(pts[i - 1]) var dir := (pts[mini(i + 1, count - 1)] - pts[maxi(i - 1, 0)]).normalized() var normal := Vector2(-dir.y, dir.x) * half points.append(pts[i] + normal) points.append(pts[i] - normal) uvs.append(Vector2(along / tile_length, 0.0)) uvs.append(Vector2(along / tile_length, 1.0)) colors.append(Color.WHITE) colors.append(Color.WHITE) for i in count - 1: var a := i * 2 indices.append_array([a, a + 1, a + 2, a + 1, a + 3, a + 2]) RenderingServer.canvas_item_set_default_texture_repeat(rid, RenderingServer.CANVAS_ITEM_TEXTURE_REPEAT_ENABLED) RenderingServer.canvas_item_set_default_texture_filter(rid, RenderingServer.CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS) RenderingServer.canvas_item_set_material(rid, GradientMap.material(dark, trim, lo, mid, highlight).get_rid()) RenderingServer.canvas_item_add_triangle_array(rid, indices, points, colors, uvs, PackedInt32Array(), PackedFloat32Array(), texture.get_rid()) ## Draws the end pins (uncoloured) into `rid`, if the theme has a pin. func add_pins(rid: RID, pts: PackedVector2Array) -> void: if pin == null or pts.size() < 2: return for at: Vector2 in [pts[0], pts[pts.size() - 1]]: var box := Rect2(at - Vector2.ONE * pin_size / 2.0, Vector2.ONE * pin_size) RenderingServer.canvas_item_add_texture_rect(rid, box, pin.get_rid())