aboutsummaryrefslogtreecommitdiff
path: root/backends/cloud/cloudicon.cpp
diff options
context:
space:
mode:
authorBastien Bouclet2016-09-18 13:05:16 +0200
committerBastien Bouclet2016-09-18 17:54:12 +0200
commit1a1a5b5f692ac74e25b03ba2bdc09e0af3606a4a (patch)
tree462f5a2740370259327a51959a0923da4f28ed0d /backends/cloud/cloudicon.cpp
parent361d84ca74b82151509378e1a5bb868b52049502 (diff)
downloadscummvm-rg350-1a1a5b5f692ac74e25b03ba2bdc09e0af3606a4a.tar.gz
scummvm-rg350-1a1a5b5f692ac74e25b03ba2bdc09e0af3606a4a.tar.bz2
scummvm-rg350-1a1a5b5f692ac74e25b03ba2bdc09e0af3606a4a.zip
CLOUD: Change the cloud icon to be updated by the main thread
The cloud manager registers itself as an event source as a mean to be polled periodically by the GUI or engine code. The periodical polling is used to update the OSD icon indicating background sync activity. Also move the cloud icon from ConnectionManager to CloudManager, allowing to decouple icon handling from network connections updates.
Diffstat (limited to 'backends/cloud/cloudicon.cpp')
-rw-r--r--backends/cloud/cloudicon.cpp178
1 files changed, 178 insertions, 0 deletions
diff --git a/backends/cloud/cloudicon.cpp b/backends/cloud/cloudicon.cpp
new file mode 100644
index 0000000000..972efae57b
--- /dev/null
+++ b/backends/cloud/cloudicon.cpp
@@ -0,0 +1,178 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "backends/cloud/cloudicon.h"
+#include "common/memstream.h"
+#include "common/system.h"
+#include "image/png.h"
+
+namespace Cloud {
+
+const float CloudIcon::ALPHA_SPEED = 0.0005;
+const float CloudIcon::ALPHA_MAX = 1;
+const float CloudIcon::ALPHA_MIN = 0.6;
+
+CloudIcon::CloudIcon() {
+ initIcons();
+ hide();
+ _lastUpdateTime = g_system->getMillis();
+}
+
+CloudIcon::~CloudIcon() {
+ _icon.free();
+ _disabledIcon.free();
+ _alphaIcon.free();
+}
+
+void CloudIcon::show(CloudIcon::Type icon, int duration) {
+ if (_type == icon) {
+ return; // Nothing to do
+ }
+
+ if (icon != kNone) {
+ _state = kShown;
+ _type = icon;
+
+ if (duration) {
+ _hideTime = g_system->getMillis() + duration;
+ } else {
+ _hideTime = 0;
+ }
+ } else {
+ _state = kGoingToHide;
+ }
+}
+
+void CloudIcon::hide() {
+ _state = kHidden;
+ _type = kNone;
+ _hideTime = 0;
+ _currentAlpha = 0;
+ _alphaRising = true;
+}
+
+CloudIcon::Type CloudIcon::getShownType() const {
+ return _type;
+}
+
+bool CloudIcon::needsUpdate() const {
+ uint32 delaySinceLastUpdate = g_system->getMillis() - _lastUpdateTime;
+ return delaySinceLastUpdate >= UPDATE_DELAY_MIN_MILLIS;
+}
+
+void CloudIcon::update() {
+ uint32 currentTime = g_system->getMillis();
+ uint32 delaySinceLastUpdate = currentTime - _lastUpdateTime;
+ _lastUpdateTime = currentTime;
+
+ switch (_state) {
+ case kHidden:
+ return; // Nothing to do
+ case kShown:
+ if (_alphaRising) {
+ if (_currentAlpha < ALPHA_MIN)
+ _currentAlpha += 5 * ALPHA_SPEED * delaySinceLastUpdate;
+ else
+ _currentAlpha += ALPHA_SPEED * delaySinceLastUpdate;
+ if (_currentAlpha > ALPHA_MAX) {
+ _currentAlpha = ALPHA_MAX;
+ _alphaRising = false;
+ }
+ } else {
+ _currentAlpha -= ALPHA_SPEED * delaySinceLastUpdate;
+ if (_currentAlpha < ALPHA_MIN) {
+ _currentAlpha = ALPHA_MIN;
+ _alphaRising = true;
+ }
+ }
+
+ if (_hideTime != 0 && _hideTime <= currentTime) {
+ _hideTime = 0;
+ _state = kGoingToHide;
+ }
+ break;
+ case kGoingToHide:
+ _currentAlpha -= 5 * ALPHA_SPEED * delaySinceLastUpdate;
+ if (_currentAlpha <= 0) {
+ hide();
+ }
+ break;
+ }
+
+ if (_state != kHidden) {
+ makeAlphaIcon((_type == kDisabled ? _disabledIcon : _icon), _currentAlpha);
+ g_system->displayActivityIconOnOSD(&_alphaIcon);
+ } else {
+ g_system->displayActivityIconOnOSD(nullptr);
+ }
+}
+
+#include "backends/cloud/cloudicon_data.h"
+#include "backends/cloud/cloudicon_disabled_data.h"
+
+void CloudIcon::initIcons() {
+ loadIcon(_icon, cloudicon_data, ARRAYSIZE(cloudicon_data));
+ loadIcon(_disabledIcon, cloudicon_disabled_data, ARRAYSIZE(cloudicon_disabled_data));
+}
+
+void CloudIcon::loadIcon(Graphics::Surface &icon, byte *data, uint32 size) {
+ Image::PNGDecoder decoder;
+ Common::MemoryReadStream stream(data, size);
+ if (!decoder.loadStream(stream))
+ error("CloudIcon::loadIcon: error decoding PNG");
+
+ const Graphics::Surface *s = decoder.getSurface();
+ return icon.copyFrom(*s);
+}
+
+void CloudIcon::makeAlphaIcon(const Graphics::Surface &icon, float alpha) {
+ _alphaIcon.copyFrom(icon);
+
+ byte *pixels = (byte *)_alphaIcon.getPixels();
+ for (int y = 0; y < _alphaIcon.h; y++) {
+ byte *row = pixels + y * _alphaIcon.pitch;
+ for (int x = 0; x < _alphaIcon.w; x++) {
+ uint32 srcColor;
+ if (_alphaIcon.format.bytesPerPixel == 2)
+ srcColor = READ_UINT16(row);
+ else if (_alphaIcon.format.bytesPerPixel == 3)
+ srcColor = READ_UINT24(row);
+ else
+ srcColor = READ_UINT32(row);
+
+ // Update color's alpha
+ byte r, g, b, a;
+ _alphaIcon.format.colorToARGB(srcColor, a, r, g, b);
+ a = (byte)(a * alpha);
+ uint32 color = _alphaIcon.format.ARGBToColor(a, r, g, b);
+
+ if (_alphaIcon.format.bytesPerPixel == 2)
+ *((uint16 *)row) = color;
+ else
+ *((uint32 *)row) = color;
+
+ row += _alphaIcon.format.bytesPerPixel;
+ }
+ }
+}
+
+} // End of namespace Cloud