ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

cloudicon.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/cloud/cloudicon.h"
00024 #include "common/memstream.h"
00025 #include "common/system.h"
00026 #include "image/png.h"
00027 
00028 namespace Cloud {
00029 
00030 const float CloudIcon::ALPHA_SPEED = 0.0005f;
00031 const float CloudIcon::ALPHA_MAX = 1.f;
00032 const float CloudIcon::ALPHA_MIN = 0.6f;
00033 
00034 CloudIcon::CloudIcon() {
00035     initIcons();
00036     hide();
00037     _lastUpdateTime = g_system->getMillis();
00038 }
00039 
00040 CloudIcon::~CloudIcon() {
00041     _icon.free();
00042     _disabledIcon.free();
00043     _alphaIcon.free();
00044 }
00045 
00046 void CloudIcon::show(CloudIcon::Type icon, int duration) {
00047     if (_type == icon) {
00048         return; // Nothing to do
00049     }
00050 
00051     if (icon != kNone) {
00052         _state = kShown;
00053         _type = icon;
00054 
00055         if (duration) {
00056             _hideTime = g_system->getMillis() + duration;
00057         } else {
00058             _hideTime = 0;
00059         }
00060     } else {
00061         _state = kGoingToHide;
00062     }
00063 }
00064 
00065 void CloudIcon::hide() {
00066     _state = kHidden;
00067     _type = kNone;
00068     _hideTime = 0;
00069     _currentAlpha = 0;
00070     _alphaRising = true;
00071 }
00072 
00073 CloudIcon::Type CloudIcon::getShownType() const {
00074     return _type;
00075 }
00076 
00077 bool CloudIcon::needsUpdate() const {
00078     uint32 delaySinceLastUpdate = g_system->getMillis() - _lastUpdateTime;
00079     return delaySinceLastUpdate >= UPDATE_DELAY_MIN_MILLIS;
00080 }
00081 
00082 void CloudIcon::update() {
00083     uint32 currentTime = g_system->getMillis();
00084     uint32 delaySinceLastUpdate = currentTime - _lastUpdateTime;
00085     _lastUpdateTime = currentTime;
00086 
00087     switch (_state) {
00088     default:
00089         // fallthrough intended
00090     case kHidden:
00091         return; // Nothing to do
00092     case kShown:
00093         if (_alphaRising) {
00094             if (_currentAlpha < ALPHA_MIN)
00095                 _currentAlpha += 5 * ALPHA_SPEED * delaySinceLastUpdate;
00096             else
00097                 _currentAlpha += ALPHA_SPEED * delaySinceLastUpdate;
00098             if (_currentAlpha > ALPHA_MAX) {
00099                 _currentAlpha = ALPHA_MAX;
00100                 _alphaRising = false;
00101             }
00102         } else {
00103             _currentAlpha -= ALPHA_SPEED * delaySinceLastUpdate;
00104             if (_currentAlpha < ALPHA_MIN) {
00105                 _currentAlpha = ALPHA_MIN;
00106                 _alphaRising = true;
00107             }
00108         }
00109 
00110         if (_hideTime != 0 && _hideTime <= currentTime) {
00111             _hideTime = 0;
00112             _state = kGoingToHide;
00113         }
00114         break;
00115     case kGoingToHide:
00116         _currentAlpha -= 5 * ALPHA_SPEED * delaySinceLastUpdate;
00117         if (_currentAlpha <= 0) {
00118             hide();
00119         }
00120         break;
00121     }
00122 
00123     if (!_icon.getPixels() || !_disabledIcon.getPixels()) {
00124         // Loading the icons failed. Don't try to draw them.
00125         return;
00126     }
00127 
00128     if (_state != kHidden) {
00129         makeAlphaIcon((_type == kDisabled ? _disabledIcon : _icon), _currentAlpha);
00130         g_system->displayActivityIconOnOSD(&_alphaIcon);
00131     } else {
00132         g_system->displayActivityIconOnOSD(nullptr);
00133     }
00134 }
00135 
00136 #include "backends/cloud/cloudicon_data.h"
00137 #include "backends/cloud/cloudicon_disabled_data.h"
00138 
00139 void CloudIcon::initIcons() {
00140     loadIcon(_icon, cloudicon_data, ARRAYSIZE(cloudicon_data));
00141     loadIcon(_disabledIcon, cloudicon_disabled_data, ARRAYSIZE(cloudicon_disabled_data));
00142 }
00143 
00144 void CloudIcon::loadIcon(Graphics::Surface &icon, byte *data, uint32 size) {
00145     Image::PNGDecoder decoder;
00146     Common::MemoryReadStream stream(data, size);
00147     if (!decoder.loadStream(stream)) {
00148         warning("CloudIcon::loadIcon: error decoding PNG");
00149         return;
00150     }
00151 
00152     const Graphics::Surface *s = decoder.getSurface();
00153     icon.copyFrom(*s);
00154 }
00155 
00156 void CloudIcon::makeAlphaIcon(const Graphics::Surface &icon, float alpha) {
00157     _alphaIcon.copyFrom(icon);
00158 
00159     byte *pixels = (byte *)_alphaIcon.getPixels();
00160     for (int y = 0; y < _alphaIcon.h; y++) {
00161         byte *row = pixels + y * _alphaIcon.pitch;
00162         for (int x = 0; x < _alphaIcon.w; x++) {
00163             uint32 srcColor;
00164             if (_alphaIcon.format.bytesPerPixel == 2)
00165                 srcColor = READ_UINT16(row);
00166             else if (_alphaIcon.format.bytesPerPixel == 3)
00167                 srcColor = READ_UINT24(row);
00168             else
00169                 srcColor = READ_UINT32(row);
00170 
00171             // Update color's alpha
00172             byte r, g, b, a;
00173             _alphaIcon.format.colorToARGB(srcColor, a, r, g, b);
00174             a = (byte)(a * alpha);
00175             uint32 color = _alphaIcon.format.ARGBToColor(a, r, g, b);
00176 
00177             if (_alphaIcon.format.bytesPerPixel == 2)
00178                 *((uint16 *)row) = color;
00179             else
00180                 *((uint32 *)row) = color;
00181 
00182             row += _alphaIcon.format.bytesPerPixel;
00183         }
00184     }
00185 }
00186 
00187 } // End of namespace Cloud


Generated on Sat Aug 1 2020 05:01:02 for ResidualVM by doxygen 1.7.1
curved edge   curved edge