Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "MouseEventMask.h" 21 : 22 : #include "gui/CGUISetting.h" 23 : #include "lib/file/vfs/vfs.h" 24 : #include "lib/file/vfs/vfs_path.h" 25 : #include "lib/path.h" 26 : #include "lib/status.h" 27 : #include "lib/tex/tex.h" 28 : #include "maths/Rect.h" 29 : #include "maths/Vector2D.h" 30 : #include "ps/CLogger.h" 31 : #include "ps/CStr.h" 32 : #include "ps/CacheLoader.h" 33 : #include "ps/Filesystem.h" 34 : #include "scriptinterface/Conversions.h" 35 : 36 : #include <cmath> 37 : #include <cstddef> 38 : #include <js/RootingAPI.h> 39 : #include <string_view> 40 : #include <utility> 41 : #include <vector> 42 : 43 : class IGUIObject; 44 : 45 : namespace 46 : { 47 : const CStr MOUSE_EVENT_MASK = "mouse_event_mask"; 48 : } 49 : 50 0 : class CGUIMouseEventMask::Impl 51 : { 52 : public: 53 0 : virtual ~Impl() = default; 54 : virtual bool IsMouseOver(const CVector2D& mousePos, const CRect& objectSize) const = 0; 55 : }; 56 : 57 0 : CGUIMouseEventMask::CGUIMouseEventMask(IGUIObject* owner) : IGUISetting(MOUSE_EVENT_MASK, owner) 58 : { 59 0 : } 60 : 61 0 : CGUIMouseEventMask::~CGUIMouseEventMask() 62 : { 63 0 : } 64 : 65 0 : void CGUIMouseEventMask::ToJSVal(const Script::Request& rq, JS::MutableHandleValue Value) 66 : { 67 0 : Script::ToJSVal(rq, Value, m_Spec); 68 0 : } 69 : 70 0 : bool CGUIMouseEventMask::DoFromJSVal(const Script::Request& rq, JS::HandleValue value) 71 : { 72 0 : CStrW spec; 73 0 : if (!Script::FromJSVal(rq, value, spec)) 74 : return false; 75 0 : return DoFromString(spec); 76 0 : } 77 : 78 0 : bool CGUIMouseEventMask::IsMouseOver(const CVector2D& mousePos, const CRect& objectSize) const 79 : { 80 0 : if (m_Impl) 81 0 : return m_Impl->IsMouseOver(mousePos, objectSize); 82 : return false; 83 : } 84 : 85 0 : class CGUIMouseEventMaskTexture final : public CGUIMouseEventMask::Impl 86 : { 87 : public: 88 : static constexpr std::string_view identifier = "texture:"; 89 : static constexpr size_t specOffset = identifier.size(); 90 : 91 0 : static std::unique_ptr<CGUIMouseEventMaskTexture> Create(const std::string_view spec) 92 : { 93 0 : std::shared_ptr<std::uint8_t> shapeFile; 94 0 : CCacheLoader loader(g_VFS, L".dds"); 95 0 : VfsPath sourcePath = VfsPath("art") / L"textures" / L"ui" / 96 0 : std::string{spec.substr(specOffset)}; 97 0 : VfsPath archivePath = loader.ArchiveCachePath(sourcePath); 98 0 : Status status; 99 0 : size_t size; 100 0 : if (loader.CanUseArchiveCache(sourcePath, archivePath)) 101 0 : status = g_VFS->LoadFile(archivePath, shapeFile, size); 102 : else 103 0 : status = g_VFS->LoadFile(sourcePath, shapeFile, size); 104 0 : if (status != INFO::OK) 105 : { 106 0 : LOGWARNING("Mouse event mask texture not found ('{}')", spec); 107 0 : return nullptr; 108 : } 109 0 : Tex tex; 110 0 : if (tex.decode(shapeFile, size) != INFO::OK) 111 : { 112 0 : LOGERROR("Could not decode mouse event mask texture '{}'", spec); 113 0 : return nullptr; 114 : } 115 0 : if (tex.transform(TEX_DXT | TEX_MIPMAPS) != INFO::OK) 116 : { 117 0 : LOGERROR("Could not transform texture '{}'", spec); 118 0 : return nullptr; 119 : } 120 : 121 : // TODO > would be nice to downscale, maybe. 122 0 : if (tex.m_Width == 0 || tex.m_Height == 0) 123 : { 124 0 : LOGERROR("Mouse event mask texture must have a non-null size ('{}')", spec); 125 0 : return nullptr; 126 : } 127 : 128 0 : auto mask = std::make_unique<CGUIMouseEventMaskTexture>(); 129 0 : mask->m_Width = static_cast<std::uint16_t>(tex.m_Width); 130 0 : mask->m_Height = static_cast<std::uint16_t>(tex.m_Height); 131 0 : mask->m_Data.reserve(mask->m_Width * mask->m_Height); 132 0 : for (std::uint8_t* ptr = tex.get_data(); ptr < tex.get_data() + tex.m_DataSize; 133 0 : ptr += tex.m_Bpp/8) 134 : { 135 0 : if (tex.m_Bpp == 32) 136 0 : mask->m_Data.push_back(*(ptr + 3) > 0); 137 : else 138 0 : mask->m_Data.push_back(*ptr > 0); 139 : } 140 : 141 0 : return mask; 142 0 : } 143 : 144 0 : bool IsMouseOver(const CVector2D& mousePos, const CRect& objectSize) const override 145 : { 146 0 : if (m_Data.empty()) 147 : return false; 148 : 149 0 : CVector2D delta = mousePos - objectSize.TopLeft(); 150 0 : int x = floor(delta.X * m_Width / objectSize.GetWidth()); 151 0 : int y = floor(delta.Y * m_Height / objectSize.GetHeight()); 152 0 : if (x < 0 || y < 0 || x >= m_Width || y >= m_Height) 153 : return false; 154 : 155 0 : return m_Data[x + y * m_Width]; 156 : } 157 : 158 : private: 159 : // This uses the bool specialization on purpose for the 'compression' effect. 160 : std::vector<bool> m_Data; 161 : std::uint16_t m_Width; 162 : std::uint16_t m_Height; 163 : }; 164 : 165 0 : bool CGUIMouseEventMask::DoFromString(const CStrW& Value) 166 : { 167 0 : std::string spec = Value.ToUTF8(); 168 0 : if (spec == m_Spec) 169 : return true; 170 : 171 : // Empty spec - reset the mask and return. 172 0 : if (spec.empty()) 173 : { 174 0 : m_Impl.reset(); 175 0 : return true; 176 : } 177 : 178 0 : if (spec.find(CGUIMouseEventMaskTexture::identifier) != std::string::npos) 179 : { 180 0 : std::unique_ptr<CGUIMouseEventMask::Impl> newImpl = CGUIMouseEventMaskTexture::Create(spec); 181 0 : if (newImpl) 182 : { 183 0 : m_Impl = std::move(newImpl); 184 0 : m_Spec = spec; 185 : return true; 186 : } 187 : else 188 0 : LOGERROR("Could not create shape for: {}", spec); 189 0 : } 190 : else 191 0 : LOGWARNING("Unknown clickable shape: {}", spec); 192 : return false; 193 0 : }