Skip to content

Commit

Permalink
libappfw: Added fade-to-black utility and GuiWidgetRef smart reference
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Aug 23, 2015
1 parent 8f1cd7f commit 44327f2
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions doomsday/sdk/libappfw/include/de/FadeToBlackWidget
@@ -0,0 +1 @@
#include "widgets/fadetoblackwidget.h"
1 change: 1 addition & 0 deletions doomsday/sdk/libappfw/include/de/GuiWidgetRef
@@ -0,0 +1 @@
#include "framework/guiwidgetref.h"
69 changes: 69 additions & 0 deletions doomsday/sdk/libappfw/include/de/framework/guiwidgetref.h
@@ -0,0 +1,69 @@
/** @file guiwidgetref.h Smart pointer to a GuiWidget.
*
* @authors Copyright (c) 2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBAPPFW_GUIWIDGETREF_H
#define LIBAPPFW_GUIWIDGETREF_H

#include "../GuiWidget"

namespace de {

/**
* Smart pointer to a GuiWidget. Does not own the target widget.
*/
template <typename WidgetType>
class GuiWidgetRef : DENG2_OBSERVES(Widget, Deletion)
{
public:
GuiWidgetRef(WidgetType *ptr = nullptr) {
reset(ptr);
}
~GuiWidgetRef() {
reset(nullptr);
}
void reset(WidgetType *ptr) {
if(_ptr) _ptr->Widget::audienceForDeletion() -= this;
_ptr = ptr;
if(_ptr) _ptr->Widget::audienceForDeletion() += this;
}
WidgetType *operator -> () const {
return _ptr;
}
operator WidgetType const * () const {
return _ptr;
}
operator WidgetType * () {
return _ptr;
}
explicit operator bool() const {
return _ptr != nullptr;
}
void widgetBeingDeleted(Widget &widget) {
if(&widget == _ptr) {
_ptr = nullptr;
}
}

private:
WidgetType *_ptr = nullptr;
};

} // namespace de

#endif // LIBAPPFW_GUIWIDGETREF_H

52 changes: 52 additions & 0 deletions doomsday/sdk/libappfw/include/de/widgets/fadetoblackwidget.h
@@ -0,0 +1,52 @@
/** @file fadetoblackwidget.h Fade to/from black.
*
* @authors Copyright (c) 2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBAPPFW_FADETOBLACKWIDGET
#define LIBAPPFW_FADETOBLACKWIDGET

#include "../LabelWidget"
#include <de/Time>

namespace de {

/**
* Fade to/from black.
*/
class FadeToBlackWidget : public LabelWidget
{
public:
FadeToBlackWidget();

void initFadeFromBlack(TimeDelta const &span);

void start();

void cancel();

bool isDone() const;

void disposeIfDone();

private:
DENG2_PRIVATE(d)
};

} // namespace de

#endif // LIBAPPFW_FADETOBLACKWIDGET

62 changes: 62 additions & 0 deletions doomsday/sdk/libappfw/src/widgets/fadetoblackwidget.cpp
@@ -0,0 +1,62 @@
/** @file fadetoblackwidget.cpp Fade to/from black.
*
* @authors Copyright (c) 2015 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "de/FadeToBlackWidget"

namespace de {

DENG2_PIMPL_NOREF(FadeToBlackWidget)
{
TimeDelta span = 1;
};

FadeToBlackWidget::FadeToBlackWidget() : d(new Instance)
{
set(Background(Vector4f(0, 0, 0, 1)));
}

void FadeToBlackWidget::initFadeFromBlack(TimeDelta const &span)
{
setOpacity(1);
d->span = span;
}

void FadeToBlackWidget::start()
{
setOpacity(0, d->span);
}

void FadeToBlackWidget::cancel()
{
setOpacity(0);
}

bool FadeToBlackWidget::isDone() const
{
return opacity().done();
}

void FadeToBlackWidget::disposeIfDone()
{
if(isDone())
{
destroyLater(this);
}
}

} // namespace de

0 comments on commit 44327f2

Please sign in to comment.