Files
edgetx-gx12/radio/src/gui/480x272/widget.h
T
Max PapernoandBertrand Songis 4f988babbf Fix initializer list issue with ZoneOptionValue (#4203)
* [480x272] Fix initializer list issue with ZoneOptionValue (technically not C++11), now works x-platform as well, lowers Flash use; Spit out Zone, ZoneOption, ZoneOptionValue to own unit. (Fixes https://github.com/opentx/opentx/pull/4199/files#r94867147)

* [480x272] Revert to Flash usage for ZoneOptionValue types (still fixes MSVC build). (Ref: https://github.com/opentx/opentx/commit/82dce3ee601049cf548ff4518902ab8e8fa62a0b#commitcomment-20388918 & https://github.com/opentx/opentx/pull/4199#discussion_r94835251)

* Cosmetics.

* [480x272] Cosmetics.
2017-01-08 09:48:37 +01:00

146 lines
3.4 KiB
C++

/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#ifndef _WIDGET_H_
#define _WIDGET_H_
#include <list>
#include "zone.h"
#define MAX_WIDGET_OPTIONS 5
class WidgetFactory;
class Widget
{
public:
struct PersistentData {
ZoneOptionValue options[MAX_WIDGET_OPTIONS];
};
Widget(const WidgetFactory * factory, const Zone & zone, PersistentData * persistentData):
factory(factory),
zone(zone),
persistentData(persistentData)
{
}
virtual ~Widget()
{
}
virtual void update() const
{
}
inline const WidgetFactory * getFactory() const
{
return factory;
}
inline const ZoneOption * getOptions() const;
inline ZoneOptionValue * getOptionValue(unsigned int index) const
{
return &persistentData->options[index];
}
virtual void refresh() = 0;
virtual void background()
{
}
protected:
const WidgetFactory * factory;
Zone zone;
PersistentData * persistentData;
};
void registerWidget(const WidgetFactory * factory);
class WidgetFactory
{
public:
WidgetFactory(const char * name, const ZoneOption * options=NULL):
name(name),
options(options)
{
registerWidget(this);
}
inline const char * getName() const
{
return name;
}
inline const ZoneOption * getOptions() const
{
return options;
}
void initPersistentData(Widget::PersistentData * persistentData) const
{
memset(persistentData, 0, sizeof(Widget::PersistentData));
if (options) {
int i = 0;
for (const ZoneOption * option = options; option->name; option++) {
// TODO compiler bug? The CPU freezes ... persistentData->options[i++] = option->deflt;
memcpy(&persistentData->options[i++], &option->deflt, sizeof(ZoneOptionValue));
}
}
}
virtual Widget * create(const Zone & zone, Widget::PersistentData * persistentData, bool init=true) const = 0;
protected:
const char * name;
const ZoneOption * options;
};
template<class T>
class BaseWidgetFactory: public WidgetFactory
{
public:
BaseWidgetFactory(const char * name, const ZoneOption * options):
WidgetFactory(name, options)
{
}
virtual Widget * create(const Zone & zone, Widget::PersistentData * persistentData, bool init=true) const
{
if (init) {
initPersistentData(persistentData);
}
return new T(this, zone, persistentData);
}
};
inline const ZoneOption * Widget::getOptions() const
{
return getFactory()->getOptions();
}
Widget * loadWidget(const char * name, const Zone & zone, Widget::PersistentData * persistentData);
std::list<const WidgetFactory *> & getRegisteredWidgets();
#endif // _WIDGET_H_