File: MouseEventMask.cpp

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (186 lines) | stat: -rw-r--r-- 4,995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright (C) 2021 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "MouseEventMask.h"

#include "gui/CGUISetting.h"
#include "lib/tex/tex.h"
#include "maths/Rect.h"
#include "maths/Vector2D.h"
#include "ps/Filesystem.h"
#include "ps/CacheLoader.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "scriptinterface/ScriptConversions.h"

class IGUIObject;
class IGUISetting;

namespace
{
	const std::string MOUSE_EVENT_MASK = "mouse_event_mask";
}

class CGUIMouseEventMask::Impl
{
public:
	virtual ~Impl() = default;
	virtual bool IsMouseOver(const CVector2D& mousePos, const CRect& objectSize) const = 0;
};

CGUIMouseEventMask::CGUIMouseEventMask(IGUIObject* owner) : IGUISetting(MOUSE_EVENT_MASK, owner)
{
}

CGUIMouseEventMask::~CGUIMouseEventMask()
{
}

void CGUIMouseEventMask::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue Value)
{
	Script::ToJSVal(rq, Value, m_Spec);
}

bool CGUIMouseEventMask::DoFromJSVal(const ScriptRequest& rq, JS::HandleValue value)
{
	CStrW spec;
	if (!Script::FromJSVal(rq, value, spec))
		return false;
	return DoFromString(spec);
}

CStr CGUIMouseEventMask::GetName() const
{
	return MOUSE_EVENT_MASK;
}

bool CGUIMouseEventMask::IsMouseOver(const CVector2D& mousePos, const CRect& objectSize) const
{
	if (m_Impl)
		return m_Impl->IsMouseOver(mousePos, objectSize);
	return false;
}

class CGUIMouseEventMaskTexture final : public CGUIMouseEventMask::Impl
{
public:
	static constexpr std::string_view identifier = "texture:";
	static constexpr size_t specOffset = identifier.size();

	static std::unique_ptr<CGUIMouseEventMaskTexture> Create(const std::string& spec)
	{
		std::shared_ptr<u8> shapeFile;
		CCacheLoader loader(g_VFS, L".dds");
		VfsPath sourcePath = VfsPath("art") / L"textures" / L"ui" / spec.substr(specOffset);
		VfsPath archivePath = loader.ArchiveCachePath(sourcePath);
		Status status;
		size_t size;
		if (loader.CanUseArchiveCache(sourcePath, archivePath))
			status = g_VFS->LoadFile(archivePath, shapeFile, size);
		else
			status = g_VFS->LoadFile(sourcePath, shapeFile, size);
		if (status != INFO::OK)
		{
			LOGWARNING("Mouse event mask texture not found ('%s')", spec);
			return nullptr;
		}
		Tex tex;
		if (tex.decode(shapeFile, size) != INFO::OK)
		{
			LOGERROR("Could not decode mouse event mask texture '%s'", spec);
			return nullptr;
		}
		if (tex.transform(TEX_DXT | TEX_MIPMAPS) != INFO::OK)
		{
			LOGERROR("Could not transform texture '%s'", spec);
			return nullptr;
		}

		// TODO > would be nice to downscale, maybe.
		if (tex.m_Width == 0 || tex.m_Height == 0)
		{
			LOGERROR("Mouse event mask texture must have a non-null size ('%s')", spec);
			return nullptr;
		}

		auto mask = std::make_unique<CGUIMouseEventMaskTexture>();
		mask->m_Width = static_cast<u16>(tex.m_Width);
		mask->m_Height = static_cast<u16>(tex.m_Height);
		mask->m_Data.reserve(mask->m_Width * mask->m_Height);
		for (u8* ptr = tex.get_data(); ptr < tex.get_data() + tex.m_DataSize; ptr += tex.m_Bpp/8)
		{
			if (tex.m_Bpp == 32)
				mask->m_Data.push_back(*(ptr + 3) > 0);
			else
				mask->m_Data.push_back(*ptr > 0);
		}

		return mask;
	}

	virtual bool IsMouseOver(const CVector2D& mousePos, const CRect& objectSize) const override
	{
		if (m_Data.empty())
			return false;

		CVector2D delta = mousePos - objectSize.TopLeft();
		int x = floor(delta.X * m_Width / objectSize.GetWidth());
		int y = floor(delta.Y * m_Height / objectSize.GetHeight());
		if (x < 0 || y < 0 || x >= m_Width || y >= m_Height)
			return false;

		return m_Data[x + y * m_Width];
	}

private:
	// This uses the bool specialization on purpose for the 'compression' effect.
	std::vector<bool> m_Data;
	u16 m_Width;
	u16 m_Height;
};

bool CGUIMouseEventMask::DoFromString(const CStrW& Value)
{
	std::string spec = Value.ToUTF8();
	if (spec == m_Spec)
		return true;

	// Empty spec - reset the mask and return.
	if (spec.empty())
	{
		m_Impl.reset();
		return true;
	}

	if (spec.find(CGUIMouseEventMaskTexture::identifier) != std::string::npos)
	{
		std::unique_ptr<CGUIMouseEventMask::Impl> newImpl = CGUIMouseEventMaskTexture::Create(spec);
		if (newImpl)
		{
			m_Impl = std::move(newImpl);
			m_Spec = spec;
			return true;
		}
		else
			LOGERROR("Could not create shape for: %s", spec);
	}
	else
		LOGWARNING("Unknown clickable shape: %s", spec);
	return false;
}