UnusualButton.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright aXiuShen. All Rights Reserved.
  2. #include "UnusualButton.h"
  3. #include "IImageWrapper.h"
  4. #include "IImageWrapperModule.h"
  5. #include "Modules/ModuleManager.h"
  6. #include "Misc/FileHelper.h"
  7. namespace syt {
  8. SUnusualButton::SUnusualButton()
  9. : HitTexutre(NULL)
  10. {
  11. }
  12. SUnusualButton::~SUnusualButton()
  13. {
  14. if (HitTexutre && HitTexutre->IsValidLowLevel() && HitTexutre->IsRooted())
  15. {
  16. HitTexutre->RemoveFromRoot();
  17. }
  18. }
  19. FReply SUnusualButton::OnMouseButtonDown(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
  20. {
  21. if (!IsHovered())
  22. {
  23. return FReply::Unhandled();
  24. }
  25. return SButton::OnMouseButtonDown(MyGeometry, MouseEvent);
  26. }
  27. FReply SUnusualButton::OnMouseButtonDoubleClick(const FGeometry& InMyGeometry, const FPointerEvent& InMouseEvent)
  28. {
  29. if (!IsHovered())
  30. {
  31. return FReply::Unhandled();
  32. }
  33. return SButton::OnMouseButtonDoubleClick(InMyGeometry, InMouseEvent);
  34. }
  35. FReply SUnusualButton::OnMouseButtonUp(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
  36. {
  37. if (!IsHovered())
  38. {
  39. return FReply::Unhandled();
  40. }
  41. return SButton::OnMouseButtonUp(MyGeometry, MouseEvent);
  42. }
  43. FReply SUnusualButton::OnMouseMove(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent)
  44. {
  45. //const float AlphaThreshold = 0.1f;
  46. //UTexture2D* ButtonTexutre = GetHitTexture();
  47. //if (ButtonTexutre)
  48. //{
  49. // const int32 TextureWidth = ButtonTexutre->GetPlatformData()->SizeX;
  50. // const int32 TextureHeight = ButtonTexutre->GetPlatformData()->SizeY;
  51. // FVector2D LocalPos = MyGeometry.AbsoluteToLocal(MouseEvent.GetScreenSpacePosition());
  52. // //FPlatformMath::FloorToInt()
  53. // const int32 X = FPlatformMath::FloorToInt(LocalPos.X / MyGeometry.GetLocalSize().X * TextureWidth);
  54. // const int32 Y = FPlatformMath::FloorToInt(LocalPos.Y / MyGeometry.GetLocalSize().Y * TextureHeight);
  55. //
  56. // FColor* ImageData = static_cast<FColor*>((ButtonTexutre->GetPlatformData()->Mips[0]).BulkData.Lock(LOCK_READ_ONLY));
  57. // const int32 CurPos = Y * TextureWidth + X;
  58. // bool bHoverCheck = ImageData && ImageData[CurPos].A > AlphaThreshold;
  59. // ButtonTexutre->GetPlatformData()->Mips[0].BulkData.Unlock();
  60. //
  61. // {
  62. // SetHover( bHoverCheck);
  63. // if (IsHovered())
  64. // {
  65. // SButton::OnMouseEnter(MyGeometry, MouseEvent);
  66. // }
  67. // else
  68. // {
  69. // SButton::OnMouseLeave(MouseEvent);
  70. // }
  71. // }
  72. //}
  73. return SButton::OnMouseMove(MyGeometry, MouseEvent);
  74. }
  75. FCursorReply SUnusualButton::OnCursorQuery(const FGeometry& MyGeometry, const FPointerEvent& CursorEvent) const
  76. {
  77. if (!IsHovered())
  78. {
  79. return FCursorReply::Unhandled();
  80. }
  81. // TOptional<EMouseCursor::Type> TheCursor = Cursor.Get();
  82. // return (TheCursor.IsSet()) ? FCursorReply::Cursor(TheCursor.GetValue()) : FCursorReply::Unhandled();
  83. return SButton::OnCursorQuery(MyGeometry, CursorEvent);
  84. }
  85. TSharedPtr<IToolTip> SUnusualButton::GetToolTip()
  86. {
  87. if (IsHovered())
  88. {
  89. return SWidget::GetToolTip();
  90. }
  91. return NULL;
  92. }
  93. UTexture2D* SUnusualButton::GetHitTexture()
  94. {
  95. //if (HitTexutre)
  96. //{
  97. // return HitTexutre;
  98. //}
  99. //if (NormalImage)
  100. //{
  101. // {
  102. // FString AlphaTexture = FPaths::GetBaseFilename(NormalImage->GetResourceName().ToString());
  103. // FString HitTextureAsset = FString::Printf(TEXT("/PivotTool/%s.%s"), *AlphaTexture, *AlphaTexture);
  104. // HitTexutre = LoadObject<UTexture2D>(NULL, *HitTextureAsset, NULL, LOAD_None, NULL);
  105. // }
  106. // if (!HitTexutre)
  107. // {
  108. // HitTexutre = LoadTexture(NormalImage->GetResourceName().ToString());
  109. // }
  110. //}
  111. return HitTexutre;
  112. }
  113. UTexture2D* SUnusualButton::LoadTexture(const FString& ImagePath)
  114. {
  115. TArray<uint8> RawFileData;
  116. // if (FFileHelper::LoadFileToArray(RawFileData, *ImagePath))
  117. // {
  118. // IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
  119. // TSharedPtr<IImageWrapper> ImageWrappers[4] =
  120. // {
  121. // ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG),
  122. // ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP),
  123. // ImageWrapperModule.CreateImageWrapper(EImageFormat::ICO),
  124. // ImageWrapperModule.CreateImageWrapper(EImageFormat::ICNS),
  125. // };
  126. //
  127. // struct Local
  128. // {
  129. // static void WriteRawToTexture(UTexture2D* NewTexture2D, const TArray<uint8>& RawData, bool bUseSRGB = true)
  130. // {
  131. // int32 Height = NewTexture2D->GetSizeY();
  132. // int32 Width = NewTexture2D->GetSizeX();
  133. //
  134. // // Fill in the base mip for the texture we created
  135. // uint8* MipData = (uint8*)NewTexture2D->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
  136. // for (int32 y = 0; y < Height; y++)
  137. // {
  138. // uint8* DestPtr = &MipData[(Height - 1 - y) * Width * sizeof(FColor)];
  139. // const FColor* SrcPtr = &((FColor*)(RawData.GetData()))[(Height - 1 - y) * Width];
  140. // for (int32 x = 0; x < Width; x++)
  141. // {
  142. // *DestPtr++ = SrcPtr->B;
  143. // *DestPtr++ = SrcPtr->G;
  144. // *DestPtr++ = SrcPtr->R;
  145. // *DestPtr++ = SrcPtr->A;
  146. // SrcPtr++;
  147. // }
  148. // }
  149. // NewTexture2D->GetPlatformData()->Mips[0].BulkData.Unlock();
  150. //
  151. // // Set options
  152. // NewTexture2D->SRGB = bUseSRGB;
  153. //#if WITH_EDITORONLY_DATA
  154. // NewTexture2D->CompressionNone = true;
  155. // NewTexture2D->MipGenSettings = TMGS_NoMipmaps;
  156. //#endif
  157. // NewTexture2D->CompressionSettings = TC_EditorIcon;
  158. //
  159. // NewTexture2D->UpdateResource();
  160. // }
  161. // };
  162. //
  163. // for (auto ImageWrapper : ImageWrappers)
  164. // {
  165. // if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
  166. // {
  167. // TArray<uint8> RawData;
  168. // if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData))
  169. // {
  170. // if (UTexture2D* Texture = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight()))
  171. // {
  172. // Local::WriteRawToTexture(Texture, RawData);
  173. // Texture->AddToRoot();
  174. // return Texture;
  175. // }
  176. // }
  177. // }
  178. // }
  179. // }
  180. return nullptr;
  181. }
  182. }