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.0005;
00031 const float CloudIcon::ALPHA_MAX = 1;
00032 const float CloudIcon::ALPHA_MIN = 0.6;
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     case kHidden:
00089         return; // Nothing to do
00090     case kShown:
00091         if (_alphaRising) {
00092             if (_currentAlpha < ALPHA_MIN)
00093                 _currentAlpha += 5 * ALPHA_SPEED * delaySinceLastUpdate;
00094             else
00095                 _currentAlpha += ALPHA_SPEED * delaySinceLastUpdate;
00096             if (_currentAlpha > ALPHA_MAX) {
00097                 _currentAlpha = ALPHA_MAX;
00098                 _alphaRising = false;
00099             }
00100         } else {
00101             _currentAlpha -= ALPHA_SPEED * delaySinceLastUpdate;
00102             if (_currentAlpha < ALPHA_MIN) {
00103                 _currentAlpha = ALPHA_MIN;
00104                 _alphaRising = true;
00105             }
00106         }
00107 
00108         if (_hideTime != 0 && _hideTime <= currentTime) {
00109             _hideTime = 0;
00110             _state = kGoingToHide;
00111         }
00112         break;
00113     case kGoingToHide:
00114         _currentAlpha -= 5 * ALPHA_SPEED * delaySinceLastUpdate;
00115         if (_currentAlpha <= 0) {
00116             hide();
00117         }
00118         break;
00119     }
00120 
00121     if (!_icon.getPixels() || !_disabledIcon.getPixels()) {
00122         // Loading the icons failed. Don't try to draw them.
00123         return;
00124     }
00125 
00126     if (_state != kHidden) {
00127         makeAlphaIcon((_type == kDisabled ? _disabledIcon : _icon), _currentAlpha);
00128         g_system->displayActivityIconOnOSD(&_alphaIcon);
00129     } else {
00130         g_system->displayActivityIconOnOSD(nullptr);
00131     }
00132 }
00133 
00134 #include "backends/cloud/cloudicon_data.h"
00135 #include "backends/cloud/cloudicon_disabled_data.h"
00136 
00137 void CloudIcon::initIcons() {
00138     loadIcon(_icon, cloudicon_data, ARRAYSIZE(cloudicon_data));
00139     loadIcon(_disabledIcon, cloudicon_disabled_data, ARRAYSIZE(cloudicon_disabled_data));
00140 }
00141 
00142 void CloudIcon::loadIcon(Graphics::Surface &icon, byte *data, uint32 size) {
00143     Image::PNGDecoder decoder;
00144     Common::MemoryReadStream stream(data, size);
00145     if (!decoder.loadStream(stream)) {
00146         warning("CloudIcon::loadIcon: error decoding PNG");
00147         return;
00148     }
00149 
00150     const Graphics::Surface *s = decoder.getSurface();
00151     icon.copyFrom(*s);
00152 }
00153 
00154 void CloudIcon::makeAlphaIcon(const Graphics::Surface &icon, float alpha) {
00155     _alphaIcon.copyFrom(icon);
00156 
00157     byte *pixels = (byte *)_alphaIcon.getPixels();
00158     for (int y = 0; y < _alphaIcon.h; y++) {
00159         byte *row = pixels + y * _alphaIcon.pitch;
00160         for (int x = 0; x < _alphaIcon.w; x++) {
00161             uint32 srcColor;
00162             if (_alphaIcon.format.bytesPerPixel == 2)
00163                 srcColor = READ_UINT16(row);
00164             else if (_alphaIcon.format.bytesPerPixel == 3)
00165                 srcColor = READ_UINT24(row);
00166             else
00167                 srcColor = READ_UINT32(row);
00168 
00169             // Update color's alpha
00170             byte r, g, b, a;
00171             _alphaIcon.format.colorToARGB(srcColor, a, r, g, b);
00172             a = (byte)(a * alpha);
00173             uint32 color = _alphaIcon.format.ARGBToColor(a, r, g, b);
00174 
00175             if (_alphaIcon.format.bytesPerPixel == 2)
00176                 *((uint16 *)row) = color;
00177             else
00178                 *((uint32 *)row) = color;
00179 
00180             row += _alphaIcon.format.bytesPerPixel;
00181         }
00182     }
00183 }
00184 
00185 } // End of namespace Cloud


Generated on Sat Feb 23 2019 05:00:59 for ResidualVM by doxygen 1.7.1
curved edge   curved edge