FLTKAPP.hpp
#ifndef FLTKAPP_HPP_
#define FLTKAPP_HPP_
#include "FL.h"
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <sstream>
#include <regex>
enum WIDGET_ITEM_TYPE
{
FL_NO_WIDGET,
FL_BUTTON,
FL_INPUT,
FL_OUTPUT
};
struct WidgetItem
{
public:
Fl_Button * button = NULL;
Fl_Input * input = NULL;
Fl_Output * output = NULL;
int type = FL_NO_WIDGET;
std::string name = "";
public:
int x = 0;
int y = 0;
int w = 0;
int h = 0;
std::string label;
public:
bool compareName(std::string name2)
{
return (name == name2);
}
void createWidget()
{
switch(type)
{
case FL_BUTTON:
button = new Fl_Button(x,y,w,h,label.data());
break;
case FL_INPUT:
input = new Fl_Input(x,y,w,h,label.data());
break;
case FL_OUTPUT:
output = new Fl_Output(x,y,w,h,label.data());
break;
}
}
void* getWidget()
{
switch(type)
{
case FL_BUTTON:
return (void*)button;
break;
case FL_INPUT:
return (void*)input;
break;
case FL_OUTPUT:
return (void*)output;
break;
}
}
};
class Activity
{
private:
Fl_Window * _window;
std::map<std::string,WidgetItem*> _widgets_map;
bool _loadWidgets(std::vector<std::string> widgets)
{
for(auto widget : widgets)
{
if(!_loadWidget(widget))
return false;
}
return true;
}
bool _loadWidget(std::string str)
{
std::regex reg("type=(.*),name=(.*),x=(.*),y=(.*),w=(.*),h=(.*),label=(.*)");
std::smatch m;
bool search_success = std::regex_search(str,m,reg);
if(search_success == 0)
return false;
WidgetItem wi;
if(m[1] == "FL_BUTTON")
{
wi.type = FL_BUTTON;
}
else if(m[1] == "FL_INPUT")
{
wi.type = FL_INPUT;
}
else if(m[1] == "FL_OUTPUT")
{
wi.type = FL_OUTPUT;
}
wi.name = m[2];
wi.x = stoi(m[3]);
wi.y = stoi(m[4]);
wi.w = stoi(m[5]);
wi.h = stoi(m[6]);
wi.label = m[7].str();
wi.createWidget();
_widgets_map.insert(std::pair<std::string,WidgetItem*>(wi.name,&wi));
return true;
}
public:
Activity()
{
_window = new Fl_Window(600,800,"");
}
public:
bool setContentWidget(std::string widgetStr)
{
std::vector<std::string> widgets;
for(int i = 0;i < widgetStr.size();i++)
{
std::stringstream str;
for(;widgetStr.at(i) != '\n';i++)
{
str << widgetStr.at(i);
}
widgets.push_back(str.str());
}
return _loadWidgets(widgets);
}
bool setContentWidgetFromTXT(std::string path)
{
std::stringstream str;
std::ifstream fin;
fin.open(path);
str << fin.rdbuf();
fin.close();
return this->setContentWidget(str.str());
}
void* findWidgetByName(std::string name)
{
return _widgets_map.at(name)->getWidget();
}
};
#endif
test.cpp
#include <FL/Fl.H>
#include "FLTKAPP.hpp"
class MainActivity : public Activity
{
public:
MainActivity() : Activity()
{
this->setContentWidget("widget.txt");
Fl_Button * button1 = (Fl_Button*)this->findWidgetByName("button1");
button1->callback([](Fl_Widget * widget,void* data)
{
Fl_Button * button1 = (Fl_Button*)widget;
button1->label("666");
},this);
}
};
int main()
{
MainActivity ma;
return 0;
}
|