刀光预研
初步实现了刀光交互效果
This commit is contained in:
		
							
								
								
									
										175
									
								
								Content/Lua/HomeLand/UI/Hearth/Widgets/PreCookCenterWidget.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										175
									
								
								Content/Lua/HomeLand/UI/Hearth/Widgets/PreCookCenterWidget.lua
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,175 @@ | ||||
| local DataTableUtils = require("Utils.DataTableUtils") | ||||
| local ESlateVisibility = import("ESlateVisibility") | ||||
| local SlateBlueprintLibrary = import("SlateBlueprintLibrary") | ||||
| local WidgetLayoutLibrary = import("WidgetLayoutLibrary") | ||||
|  | ||||
|  | ||||
|  | ||||
| --- @class PreCookCenterWidget | ||||
| --- @field ImgContainer table | ||||
| --- @field ImgCookMaterial table | ||||
| local PreCookCenterWidget = {} | ||||
|  | ||||
| function PreCookCenterWidget:ctor() | ||||
|     self.mouse_tracks = {} | ||||
|     self.is_pressed = false | ||||
| end | ||||
|  | ||||
|  | ||||
| function PreCookCenterWidget:OnInitialized() | ||||
|     self.bHasScriptImplementedTick = true | ||||
|  | ||||
|     self.BtnMain.OnReleased:Add(function() | ||||
|         -- self.bHasScriptImplementedTick = false | ||||
|         self.is_pressed = false | ||||
|         print("release") | ||||
|     end) | ||||
|     self.BtnMain.OnPressed:Add(function() | ||||
|         self.mouse_tracks = {} | ||||
|         self.is_pressed = true | ||||
|         -- self.bHasScriptImplementedTick = true | ||||
|         print("pressed") | ||||
|     end) | ||||
|     -- self.BtnMain.OnClicked:Add(function() | ||||
|     --     print("onclicked") | ||||
|     -- end) | ||||
| end | ||||
|  | ||||
| function PreCookCenterWidget:Construct() | ||||
|     -- self.bHasScriptImplementedTick = true | ||||
| end | ||||
|  | ||||
| function PreCookCenterWidget:Destruct() | ||||
|  | ||||
| end | ||||
|  | ||||
| function PreCookCenterWidget:SetEmpty() | ||||
|     self.ImgContainer:SetVisibility(ESlateVisibility.Collapsed) | ||||
|     self.ImgCookMaterial:SetVisibility(ESlateVisibility.Collapsed) | ||||
| end | ||||
|  | ||||
| function PreCookCenterWidget:AddContainer(pre_cook_contianer_id) | ||||
|     local row = DataTableUtils.GetDataTableRow("PreCookItemConfig", pre_cook_contianer_id) | ||||
|     if not row then return end | ||||
|     self.ImgContainer:SetBrushFromSoftTexture(row.CenterDisplayResource, true) | ||||
|     self.ImgContainer:SetVisibility(ESlateVisibility.SelfHitTestInvisible) | ||||
| end | ||||
|  | ||||
| function PreCookCenterWidget:RemoveContainer() | ||||
|     self.ImgContainer:SetVisibility(ESlateVisibility.Collapsed) | ||||
| end | ||||
|  | ||||
| function PreCookCenterWidget:AddCookMaterial(pre_cook_material_id) | ||||
|     local row = DataTableUtils.GetDataTableRow("PreCookItemConfig", pre_cook_material_id) | ||||
|     if not row then return end | ||||
|     self.ImgCookMaterial:SetBrushFromSoftTexture(row.CenterDisplayResource, true) | ||||
|     self.ImgCookMaterial:SetVisibility(ESlateVisibility.SelfHitTestInvisible) | ||||
| end | ||||
|  | ||||
|  | ||||
| local function UpdateOldMouseTrack(old_mouse_tracks, delta_time) | ||||
|     local new_mouse_tracks = {} | ||||
|     -- local new_track = {x=fixed_x, y=fixed_y, remain=1.0} | ||||
|     for _, track in pairs(old_mouse_tracks) do | ||||
|         track.remain = track.remain - delta_time | ||||
|         if track.remain > 0 then | ||||
|             table.insert(new_mouse_tracks, track) | ||||
|         end | ||||
|     end | ||||
|     return new_mouse_tracks | ||||
| end | ||||
|  | ||||
| --- 将鼠标的轨迹坐标规范到以起始点到终点所连直线为x轴的坐标系下 | ||||
| local function NormalizeTrackParam(mouse_tracks) | ||||
|  | ||||
| end | ||||
|  | ||||
| --- 根据指定点坐标,求解三次样条参数 | ||||
| local function GetSplineParams(normalize_params) | ||||
|  | ||||
| end | ||||
|  | ||||
| local function UpdateCutMaskByTracks(widget, mouse_tracks) | ||||
|     local FVector2D = import("Vector2D") | ||||
|     local FWidgetTransform = import("WidgetTransform") | ||||
|     if #mouse_tracks < 2 then return end | ||||
|  | ||||
|     local translation, scale = FVector2D(), FVector2D() | ||||
|     local render_transform = FWidgetTransform() | ||||
|     local first_point, last_point = mouse_tracks[1], mouse_tracks[#mouse_tracks] | ||||
|     local delta_x = last_point.X - first_point.X | ||||
|     local delta_y = last_point.Y - first_point.Y | ||||
|     local mask_length = (delta_x^2 + delta_y^2)^0.5 | ||||
|  | ||||
|  | ||||
|  | ||||
|     translation.X, translation.Y = first_point.X, first_point.Y | ||||
|     scale.X, scale.Y = mask_length / 512, 1 | ||||
|     render_transform.Scale = scale | ||||
|     render_transform.Translation = translation | ||||
|     render_transform.Angle = (math.atan(delta_y, delta_x) / (2 * math.pi)) * 360 | ||||
|     -- render_transform.Scale = FVector2D(mask_length / 512, 1) | ||||
|     print(render_transform.Angle, math.atan(delta_y, delta_x)) | ||||
|  | ||||
|     widget:SetRenderTransform(render_transform) | ||||
| --[[ | ||||
|  | ||||
| 	/** The amount to translate the widget in slate units */ | ||||
| 	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Transform, meta=( Delta = "1" )) | ||||
| 	FVector2D Translation; | ||||
|  | ||||
| 	/** The scale to apply to the widget */ | ||||
| 	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Transform, meta=( UIMin = "-5", UIMax = "5", Delta = "0.05" )) | ||||
| 	FVector2D Scale; | ||||
|  | ||||
| 	/** The amount to shear the widget in slate units */ | ||||
| 	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Transform, meta=( UIMin = "-89", ClampMin = "-89", UIMax = "89", ClampMax = "89", Delta = "1" )) | ||||
| 	FVector2D Shear; | ||||
|  | ||||
| 	/** The angle in degrees to rotate */ | ||||
| 	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=Transform, meta=( UIMin = "-180", UIMax = "180", Delta = "1" )) | ||||
| 	float Angle; | ||||
|  | ||||
| ]] | ||||
|  | ||||
| end | ||||
|  | ||||
| function PreCookCenterWidget:Tick(geometry, delta_time) | ||||
|     local size = SlateBlueprintLibrary.GetLocalSize(geometry) | ||||
|     local cursor_pos = WidgetLayoutLibrary.GetMousePositionOnViewport(self) | ||||
|     local left_top = SlateBlueprintLibrary.GetLocalTopLeft(geometry) | ||||
|     local fixed_x = math.min(math.max(cursor_pos.X - left_top.X, 0), size.X) | ||||
|     local fixed_y = math.min(math.max(cursor_pos.Y - left_top.Y, 0), size.Y) | ||||
|  | ||||
|     local mouse_tracks = UpdateOldMouseTrack(self.mouse_tracks, delta_time) | ||||
|  | ||||
|     if self.is_pressed then | ||||
|         table.insert(mouse_tracks, {X=fixed_x, Y=fixed_y, remain=0.5}) | ||||
|     end | ||||
|  | ||||
|     -- 计算样条参数 | ||||
|     local normalize_params = NormalizeTrackParam(mouse_tracks) | ||||
|     local A, B, C, D = GetSplineParams(normalize_params)  -- Ax^3 + Bx^2 + Cx + D | ||||
|  | ||||
|     -- 计算划痕位置 | ||||
|  | ||||
|     UpdateCutMaskByTracks(self.ImgMask, mouse_tracks) | ||||
|  | ||||
|     self.mouse_tracks = mouse_tracks | ||||
|  | ||||
|  | ||||
|     -- print(fixed_x, fixed_y, #self.mouse_tracks) | ||||
| end | ||||
|  | ||||
|  | ||||
|  | ||||
| -- function PreCookCenterWidget:LuaMouseButtonDown() | ||||
| --     print("on mouse button down") | ||||
| -- end | ||||
|  | ||||
| -- function PreCookCenterWidget:LuaMouseButtonUp() | ||||
| --     print("on mouse button up") | ||||
| -- end | ||||
|  | ||||
|  | ||||
| return Class(nil, nil, PreCookCenterWidget) | ||||
		Reference in New Issue
	
	Block a user