/* * 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 #define MAX_WIDGET_OPTIONS 4 struct Zone { uint16_t x, y, w, h; }; struct ZoneOption { enum Type { Bool, Integer, String, TextSize, Timer, Source, Switch, Color }; const char * name; Type type; }; union ZoneOptionValue { bool boolValue; uint32_t unsignedValue; int32_t signedValue; char stringValue[8]; }; 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 void create() { memset(persistentData, 0, sizeof(PersistentData)); } virtual void load() { } const WidgetFactory * getFactory() const { return factory; } virtual ZoneOptionValue getOptionValue(unsigned int index) const { return persistentData->options[index]; } virtual void setOptionValue(unsigned int index, ZoneOptionValue value) const { persistentData->options[index] = value; } virtual void refresh() = 0; protected: const WidgetFactory * factory; Zone zone; PersistentData * persistentData; }; void registerWidget(const WidgetFactory * factory); class WidgetFactory { public: WidgetFactory(const char * name, const char * bitmap): name(name), bitmap(bitmap) { registerWidget(this); } const char * getName() const { return name; } virtual const ZoneOption * getOptions() const = 0; virtual Widget * create(const Zone & zone, Widget::PersistentData * persistentData) const = 0; virtual Widget * load(const Zone & zone, Widget::PersistentData * persistentData) const = 0; protected: const char * name; const char * bitmap; }; template class BaseWidgetFactory: public WidgetFactory { public: BaseWidgetFactory(const char * name, const char * bitmap, const ZoneOption * options): WidgetFactory(name, bitmap), options(options) { } virtual const ZoneOption * getOptions() const { return options; } virtual Widget * create(const Zone & zone, Widget::PersistentData * persistentData) const { Widget * widget = new T(this, zone, persistentData); widget->create(); return widget; } virtual Widget * load(const Zone & zone, Widget::PersistentData * persistentData) const { Widget * widget = new T(this, zone, persistentData); widget->load(); return widget; } protected: const ZoneOption * options; }; #define MAX_REGISTERED_WIDGETS 10 extern unsigned int countRegisteredWidgets; extern const WidgetFactory * registeredWidgets[MAX_REGISTERED_WIDGETS]; Widget * createWidget(const char * name, const Zone & zone, Widget::PersistentData * persistentData); Widget * loadWidget(const char * name, const Zone & zone, Widget::PersistentData * persistentData); #endif // _WIDGET_H_