Move things from crosslang to here
This commit is contained in:
110
examples/databaseex.cpp
Normal file
110
examples/databaseex.cpp
Normal file
@ -0,0 +1,110 @@
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include "TessesFramework/Platform/Environment.hpp"
|
||||
#include "TessesFramework/Serialization/SQLite.hpp"
|
||||
#include "TessesFramework/SDL2/GUI.hpp"
|
||||
#include "TessesFramework/SDL2/Views/ButtonView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/AbsoluteView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/LabelView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/TextListView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/ProgressView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/CheckView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/EditTextView.hpp"
|
||||
|
||||
|
||||
using namespace Tesses::Framework::Filesystem;
|
||||
using namespace Tesses::Framework::SDL2;
|
||||
using namespace Tesses::Framework::Serialization;
|
||||
using namespace Tesses::Framework;
|
||||
using namespace Tesses::Framework::Platform::Environment::SpecialFolders;
|
||||
|
||||
|
||||
VFSPath DatabasePath()
|
||||
{
|
||||
auto path = GetConfig() / "Tesses" / "Framework" / "Examples";
|
||||
LocalFS.CreateDirectory(path);
|
||||
return path / "databaseex.sqlite";
|
||||
}
|
||||
|
||||
void CreateTable()
|
||||
{
|
||||
SQLiteDatabase db(DatabasePath());
|
||||
db.Exec("CREATE TABLE IF NOT EXISTS names (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");
|
||||
}
|
||||
void ClearTable()
|
||||
{
|
||||
SQLiteDatabase db(DatabasePath());
|
||||
db.Exec("DROP TABLE names;");
|
||||
db.Exec("CREATE TABLE IF NOT EXISTS names (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");
|
||||
|
||||
}
|
||||
|
||||
void LoadDB(std::vector<std::string>& items)
|
||||
{
|
||||
SQLiteDatabase db(DatabasePath());
|
||||
|
||||
for(auto item : db.Exec("SELECT name FROM names;"))
|
||||
{
|
||||
for(auto kvp : item)
|
||||
{
|
||||
if(kvp.first == "name" && kvp.second)
|
||||
items.push_back(kvp.second.value());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
void Append(std::string item)
|
||||
{
|
||||
SQLiteDatabase db(DatabasePath());
|
||||
db.Exec("INSERT INTO names (name) VALUES (" + SQLiteDatabase::Escape(item) + ");");
|
||||
}
|
||||
|
||||
int main(int argc,char** argv)
|
||||
{
|
||||
std::string json = "{\"Child\":{\"Items\":[{\"Bounds\":{\"Height\":50,\"Width\":440,\"X\":16,\"Y\":16},\"Hint\":\"Enter name\",\"Id\":\"name\",\"Type\":\"EditTextView\"},{\"Bounds\":{\"Height\":50,\"Width\":70,\"X\":470,\"Y\":17},\"Id\":\"addBtn\",\"Text\":\"Add\",\"Type\":\"ButtonView\"},{\"Bounds\":{\"Height\":50,\"Width\":70,\"X\":550,\"Y\":17},\"Id\":\"clearBtn\",\"Text\":\"Clear\",\"Type\":\"ButtonView\"},{\"Bounds\":{\"Height\":382,\"Width\":608,\"X\":16,\"Y\":82},\"Id\":\"listView\",\"Type\":\"TextListView\"}],\"Type\":\"AbsoluteView\"}}";
|
||||
|
||||
TF_Init();
|
||||
CreateTable();
|
||||
GUIPalette pal(true,{.r=255,.g=0,.b=0,.a=255});
|
||||
|
||||
GUIWindow window("Names",640,480,0,pal);
|
||||
window.SetView(Json::Json::Decode(json));
|
||||
|
||||
auto name = dynamic_cast<Views::EditTextView*>(window.FindViewById("name"));
|
||||
auto addBtn = dynamic_cast<Views::ButtonView*>(window.FindViewById("addBtn"));
|
||||
auto clearBtn = dynamic_cast<Views::ButtonView*>(window.FindViewById("clearBtn"));
|
||||
|
||||
auto listView = dynamic_cast<Views::TextListView*>(window.FindViewById("listView"));
|
||||
|
||||
if(name == nullptr || addBtn == nullptr || clearBtn == nullptr || listView == nullptr)
|
||||
{
|
||||
TF_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
LoadDB(listView->items);
|
||||
addBtn->Click += std::make_shared<FunctionalEvent<View*,GUIEventArgs&>>([&](View* view,GUIEventArgs& e)->void {
|
||||
std::string text = name->GetText();
|
||||
if(!text.empty())
|
||||
{
|
||||
name->SetText("");
|
||||
Append(text);
|
||||
listView->items.push_back(text);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
clearBtn->Click += std::make_shared<FunctionalEvent<View*,GUIEventArgs&>>([&](View* view,GUIEventArgs& e)->void {
|
||||
listView->items.clear();
|
||||
ClearTable();
|
||||
});
|
||||
|
||||
|
||||
|
||||
TF_RunEventLoop();
|
||||
TF_Quit();
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -1,5 +1,7 @@
|
||||
|
||||
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
|
||||
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include "TessesFramework/SDL2/GUI.hpp"
|
||||
#include "TessesFramework/SDL2/Views/ButtonView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/AbsoluteView.hpp"
|
||||
@ -8,6 +10,7 @@
|
||||
#include "TessesFramework/Filesystem/LocalFS.hpp"
|
||||
#include "TessesFramework/SDL2/Views/ProgressView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/CheckView.hpp"
|
||||
#include "TessesFramework/SDL2/Views/EditTextView.hpp"
|
||||
#include <iostream>
|
||||
using namespace Tesses::Framework;
|
||||
using namespace Tesses::Framework::SDL2;
|
||||
@ -36,24 +39,26 @@ int main(int argc,char** argv)
|
||||
size_t color_index=0;
|
||||
|
||||
GUIPalette pal0(darkMode,colors[color_index % colors.size()].first,20);
|
||||
GUIWindow window("My Window Title",640,480,SDL_WINDOW_RESIZABLE,pal0);
|
||||
|
||||
TF_LOG("Create pallete");
|
||||
GUIWindow window("My Window Title",1280,720,SDL_WINDOW_RESIZABLE,pal0);
|
||||
TF_LOG("Created GUIWindow success");
|
||||
|
||||
Views::LabelView lbl("A random label\nThat spans lines.");
|
||||
|
||||
Views::ButtonView btn("Dark Mode");
|
||||
Views::ButtonView btn2(colors[0].second);
|
||||
Views::ProgressView progress(42.42);
|
||||
|
||||
Views::CheckView cv(false,"Checkbox");
|
||||
Views::CheckView cv2(false,"Another Checkbox");
|
||||
|
||||
Views::EditTextView edit("Enter some text");
|
||||
|
||||
|
||||
Views::TextListView list;
|
||||
for(auto item : Tesses::Framework::Filesystem::LocalFS.EnumeratePaths((std::string)"/usr/bin"))
|
||||
/*for(auto item : Tesses::Framework::Filesystem::LocalFS.EnumeratePaths((std::string)"/usr/bin"))
|
||||
{
|
||||
list.items.push_back(item.GetFileName());
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
@ -73,15 +78,29 @@ int main(int argc,char** argv)
|
||||
|
||||
abs.Add({.x=32,.y=230,.w=320,.h=240},&list,false);
|
||||
|
||||
abs.Add({.x=32,.y=478,.w=300,.h=200},&edit,false);
|
||||
|
||||
window.SetView(&abs,false);
|
||||
|
||||
window.SDLEvent += std::make_shared<FunctionalEvent<View*,GUISDLEventEventArgs&>>([&window,&lbl](View* sender, GUISDLEventEventArgs& e)->void {
|
||||
std::string sdl2_event = "SDL_Event: " + std::to_string(e.event.type);
|
||||
TF_LOG(sdl2_event);
|
||||
if(e.event.type == SDL_EventType::SDL_WINDOWEVENT)
|
||||
{
|
||||
if(e.event.window.event == SDL_WINDOWEVENT_RESIZED)
|
||||
{
|
||||
std::string text ="A random label\nThat spans lines, WindowSize: "
|
||||
+std::to_string(e.event.window.data1) + "x" + std::to_string(e.event.window.data2) + ".";
|
||||
lbl.SetText(text);
|
||||
}
|
||||
}
|
||||
});
|
||||
btn.Click += std::make_shared<FunctionalEvent<View*,GUIEventArgs&>>([&window,&darkMode,&color_index,&colors,&btn](View* sender, GUIEventArgs& e)->void{
|
||||
darkMode = !darkMode;
|
||||
btn.SetText(darkMode ? "Light Mode" : "Dark Mode");
|
||||
GUIPalette palette(darkMode,colors[color_index % colors.size()].first,20);
|
||||
window.SetPalette(palette);
|
||||
});
|
||||
//"A random label\nThat spans lines."
|
||||
|
||||
btn2.Click += std::make_shared<FunctionalEvent<View*,GUIEventArgs&>>([&window,&darkMode,&color_index,&colors,&btn2](View* sender, GUIEventArgs& e)->void{
|
||||
color_index++;
|
||||
@ -98,4 +117,4 @@ int main(int argc,char** argv)
|
||||
TF_Quit();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user