This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

character / tests / cubism-probe / addons / gd_cubism / example / demo_effect_hit_area.cs
5.3 kB 144 lines
1// SPDX-License-Identifier: MIT 2// SPDX-FileCopyrightText: 2023 MizunagiKB <mizukb@live.jp> 3using Godot; 4using System; 5 6 7#pragma warning disable IDE1006 8 9 10public partial class demo_effect_hit_area : Node2D 11{ 12 const String DEFAULT_ASSET = "res://addons/gd_cubism/example/res/live2d/mao_pro_jp/runtime/mao_pro.model3.json"; 13 14 private GDCubismUserModelCS cubism_model; 15 16 private bool pressed = false; 17 private Vector2 local_pos = new(0.0f, 0.0f); 18 private Vector2 adjust_pos = new(0.0f, 0.0f); 19 private Godot.Collections.Dictionary dict_detail = new(); 20 private Godot.Collections.Dictionary dict_pickup = new(); 21 private String id_mesh = ""; 22 23 24 private void recalc_model_position(GDCubismUserModelCS model) 25 { 26 if(model.Assets == "") return; 27 28 Godot.Collections.Dictionary canvas_info = model.GetCanvasInfo(); 29 30 if(canvas_info.Count > 0) 31 { 32 Vector2 vct_viewport_size = new(GetViewportRect().Size.X, GetViewportRect().Size.Y); 33 Vector2 size_in_pixels = (Vector2)canvas_info["size_in_pixels"]; 34 float scale = vct_viewport_size.Y / Math.Max(size_in_pixels.X, size_in_pixels.Y); 35 model.GetInternalObject().Position = new(vct_viewport_size.X * 0.5f, vct_viewport_size.Y * 0.5f); 36 model.GetInternalObject().Scale = new(scale, scale); 37 } 38 } 39 40 public override void _Ready() 41 { 42 this.cubism_model = new(GetNode<Node2D>("GDCubismUserModel")); 43 if (this.cubism_model.Assets == "") this.cubism_model.Assets = DEFAULT_ASSET; 44 45 GetNode<ItemList>("Control/ItemList").Clear(); 46 Godot.Collections.Dictionary dict_meshes = this.cubism_model.GetMeshes(); 47 foreach (var id in dict_meshes.Keys) 48 { 49 GetNode<ItemList>("Control/ItemList").AddItem(id.AsString()); 50 } 51 } 52 53 public override void _Process(double delta) 54 { 55 this.recalc_model_position(this.cubism_model); 56 57 GDCubismEffectHitAreaCS hit_area = new(GetNode<Node>("GDCubismUserModel/GDCubismEffectHitArea")); 58 59 if (this.pressed == true) 60 { 61 hit_area.SetTarget(this.local_pos); 62 } 63 64 GetNode<Label>("Control/lbl_mouse_x").Text = String.Format("{0:######.000}", this.local_pos.X); 65 GetNode<Label>("Control/lbl_mouse_y").Text = String.Format("{0:######.000}", this.local_pos.Y); 66 67 if (this.id_mesh != "") 68 { 69 this.dict_pickup = hit_area.GetDetail(this.cubism_model, this.id_mesh); 70 } 71 72 GetNode<CanvasItem>("Canvas").QueueRedraw(); 73 74 base._Process(delta); 75 } 76 77 public override void _Input(InputEvent @event) 78 { 79 if (@event is InputEventMouseButton mouseButton) 80 { 81 this.pressed = mouseButton.IsPressed(); 82 } 83 84 if (@event is InputEventMouseMotion mouseMotion) 85 { 86 this.local_pos = this.cubism_model.GetInternalObject().ToLocal(mouseMotion.Position) + this.adjust_pos; 87 } 88 89 base._Input(@event); 90 } 91 92 private void mark_hit_area(Godot.Collections.Dictionary dict_hit_area, Color color_box, Color color_tri) 93 { 94 if (dict_hit_area.Count > 0) 95 { 96 Rect2 rect = (Rect2)dict_hit_area["rect"]; 97 98 Rect2 r = new( 99 (rect.Position - this.adjust_pos) * this.cubism_model.GetInternalObject().Scale, 100 rect.Size * this.cubism_model.GetInternalObject().Scale 101 ); 102 GetNode<CanvasItem>("Canvas").DrawRect(r, color_box, false, 5); 103 104 if (dict_hit_area.ContainsKey("vertices") == true) 105 { 106 Godot.Collections.Array<Vector2> vtx = (Godot.Collections.Array<Vector2>)dict_hit_area["vertices"]; 107 108 Godot.Collections.Array<Vector2> v = new(); 109 v.Add(vtx[0] * this.cubism_model.GetInternalObject().Scale); 110 v.Add(vtx[1] * this.cubism_model.GetInternalObject().Scale); 111 v.Add(vtx[2] * this.cubism_model.GetInternalObject().Scale); 112 113 GetNode<CanvasItem>("Canvas").DrawLine(v[0] - this.adjust_pos, v[1] - adjust_pos, color_tri, 3); 114 GetNode<CanvasItem>("Canvas").DrawLine(v[1] - this.adjust_pos, v[2] - adjust_pos, color_tri, 3); 115 GetNode<CanvasItem>("Canvas").DrawLine(v[2] - this.adjust_pos, v[0] - adjust_pos, color_tri, 3); 116 } 117 } 118 } 119 120 private void _on_gd_cubism_effect_hit_area_hit_area_entered(Node2D _model, String id) 121 { 122 GDCubismEffectHitAreaCS hit_area = new(GetNode<Node>("GDCubismUserModel/GDCubismEffectHitArea")); 123 124 this.dict_detail = hit_area.GetDetail(this.cubism_model, id); 125 GetNode<Label>("Control/lbl_id").Text = id; 126 } 127 128 private void _on_gd_cubism_effect_hit_area_hit_area_exited(Node2D _model, String id) 129 { 130 this.dict_detail = new(); 131 GetNode<Label>("Control/lbl_id").Text = ""; 132 } 133 134 private void _on_canvas_draw() 135 { 136 this.mark_hit_area(this.dict_detail, new Color(0.0f, 1.0f, 0.0f), new Color(1.0f, 0.0f, 0.0f)); 137 this.mark_hit_area(this.dict_pickup, new Color(0.0f, 0.0f, 1.0f), new Color(1.0f, 0.0f, 0.0f)); 138 } 139 140 private void _on_item_list_item_selected(long index) 141 { 142 this.id_mesh = GetNode<ItemList>("Control/ItemList").GetItemText((int)index); 143 } 144}