Add GUI Support

This commit is contained in:
2025-06-12 15:43:58 -05:00
parent dd4527645e
commit 71a2c83e5a
59 changed files with 3114 additions and 103 deletions

View File

@ -87,6 +87,17 @@ namespace Tesses::Framework
}
mtx.Unlock();
}
void Remove(std::function<bool(std::shared_ptr<Event<TArgs...>>)> cb)
{
for(auto index = this->items.begin(); index != this->items.end(); index++)
{
if(cb(*index))
{
this->items.erase(index);
index--;
}
}
}
};
extern EventList<uint64_t> OnItteraton;

View File

@ -147,6 +147,7 @@ struct CaseInsensitiveLess {
static std::string UrlPathDecode(std::string v);
static std::string UrlPathEncode(std::string v, bool ignoreSpace=false);
static std::string HtmlEncode(std::string v);
static std::string HtmlDecodeOnlyEntityNumber(std::string v);
static std::vector<std::string> SplitString(std::string text, std::string delimiter,std::size_t maxCnt = std::string::npos);
static std::string Replace(std::string str, std::string find, std::string replace);
static std::string StatusCodeString(StatusCode code);

View File

@ -1,4 +1,5 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include <array>
#include <SDL2/SDL.h>
@ -11,17 +12,20 @@ class FontCache
{
std::array<SDL_Texture*,96> font_chrs;
int mw,mh,ps;
void Load(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color);
void Load(SDL_Renderer* renderer,TTF_Font* font);
public:
FontCache(SDL_Renderer* renderer,TTF_Font* font,const SDL_Color& color);
FontCache(SDL_Renderer* renderer,std::string font,int sz,const SDL_Color& color);
FontCache(SDL_Renderer* renderer,const uint8_t* mem,size_t cnt,int sz,const SDL_Color& color);
FontCache(SDL_Renderer* renderer,const std::vector<uint8_t>& v,int sz,const SDL_Color& color);
FontCache(SDL_Renderer* renderer,TTF_Font* font);
FontCache(SDL_Renderer* renderer,std::string font,int sz);
FontCache(SDL_Renderer* renderer,const uint8_t* mem,size_t cnt,int sz);
FontCache(SDL_Renderer* renderer,const std::vector<uint8_t>& v,int sz);
FontCache(SDL_Renderer* renderer,int sz);
SDL_Texture* operator[](char c);
SDL_Texture* GetCharOfColor(char c, const SDL_Color& color);
int MaxWidth();
int MaxHeight();
int PointSize();
void CalculateSize(std::string text, int& x,int& y);
void Render(SDL_Renderer* renderer,int x,int y, std::string text, const SDL_Color& color);
~FontCache();
};
}

View File

@ -0,0 +1,244 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include <SDL2/SDL.h>
#include "FontCache.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "../Filesystem/VFS.hpp"
#include "../Common.hpp"
#include "../Serialization/Json.hpp"
namespace Tesses::Framework::SDL2
{
class GUIPalette {
public:
GUIPalette();
GUIPalette(bool isDarkMode, SDL_Color accent,int fontSize=24);
GUIPalette(SDL_Color accent, SDL_Color background, SDL_Color border_color, SDL_Color border_hover, SDL_Color border_active, SDL_Color border_hover_active, int fontSize=24);
SDL_Color accent; //color is used for font when not over accent background
SDL_Color background;
SDL_Color border_color; //color is used for font when over accent background
SDL_Color border_hover;
SDL_Color border_active;
SDL_Color border_hover_active;
int fontSize;
SDL_Color& GetBorderColor(bool isHovering, bool isActive, bool isMouseDown);
};
class GUIEventArgs
{
public:
virtual std::string Type();
virtual ~GUIEventArgs();
};
class View;
class GUIMouseButtonEventArgs : public GUIEventArgs
{
public:
Uint32 which;
int x;
int y;
Uint8 button;
std::string Type();
};
class GUIJsonViewNotFoundEventArgs : public GUIEventArgs
{
public:
std::string Type();
View* destView;
Tesses::Framework::Serialization::Json::JObject jsonObject;
std::string typeString;
};
class GUISDLEventEventArgs : public GUIEventArgs
{
public:
std::string Type();
SDL_Event event;
};
constexpr uint64_t VIEWFLAG_HOVER_STATE=(uint64_t)1 << 0;
constexpr uint64_t VIEWFLAG_MOUSEDOWN_STATE =(uint64_t)1<<1;
constexpr uint64_t VIEWFLAG_ISACTIVE=(uint64_t)1<<2;
constexpr uint64_t VIEWFLAG_TABSTOP=(uint64_t)1<<3;
constexpr uint64_t VIEWFLAG_INTERCEPT_TAB=(uint64_t)1<<4;
constexpr uint64_t VIEWFLAG_CHECKED=(uint64_t)1<<5;
class GUIPopup;
class GUIWindow;
class ContainerView;
class View {
protected:
std::string text;
std::string id;
uint64_t flags;
protected:
View();
View(std::string text);
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
virtual void OnDragDropFile(Tesses::Framework::Filesystem::VFSPath filePath,SDL_Rect myRect, SDL_Point dropLoc);
virtual void OnDragDropText(std::string text,SDL_Rect myRect, SDL_Point dropLoc);
virtual void OnEnter(GUIEventArgs& evt);
virtual void OnLeave(GUIEventArgs& evt);
virtual void OnMouseDown(GUIMouseButtonEventArgs& evt);
virtual void OnMouseUp(GUIMouseButtonEventArgs& evt);
virtual void OnClick(GUIEventArgs& evt);
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
virtual void OnSetParent(View* v);
void CallOnDraw(View* view, SDL_Renderer* renderer, SDL_Rect& myRect)
{
view->OnDraw(renderer,myRect);
}
bool CallOnEvent(View* view,SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds)
{
return view->OnEvent(event,myBounds,visibleBounds);
}
ContainerView* parent;
public:
bool GetViewFlag(uint64_t flag)
{
return (this->flags & flag) != 0;
}
void SetViewFlag(uint64_t flag, bool value)
{
if(value)
this->flags |= flag;
else
this->flags &= ~flag;
}
EventList<View*,GUIMouseButtonEventArgs&> MouseUp;
EventList<View*,GUIMouseButtonEventArgs&> MouseDown;
EventList<View*,GUIEventArgs&> Click;
EventList<View*,GUIEventArgs&> Enter;
EventList<View*,GUIEventArgs&> Leave;
EventList<View*,GUISDLEventEventArgs&> SDLEvent;
virtual ~View();
friend class GUIWindow;
virtual GUIWindow* GetWindow();
virtual std::string GetText();
virtual void SetText(std::string text);
virtual void SetId(std::string id);
virtual std::string GetId();
virtual View* FindViewById(std::string id);
friend class ContainerView;
};
class ContainerView : public View {
public:
virtual size_t ViewCount()=0;
virtual View* GetViewAt(size_t index)=0;
virtual View* FindViewById(std::string id);
protected:
ContainerView();
ContainerView(std::string text);
void AssignChildParentToThis(View* view)
{
if(view != nullptr)
{
view->parent = this;
view->OnSetParent(this);
}
}
};
enum class TabNextResult {
KeepGoing,
TabNext,
Done
};
class GUIPopup : public ContainerView {
View* child;
bool ownsChild;
protected:
void OnDraw(SDL_Renderer* renderer, SDL_Rect& myRect);
bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
public:
size_t ViewCount();
View* GetViewAt(size_t index);
GUIPopup();
GUIPopup(SDL_Rect bounds);
GUIPopup(int x, int y,int w, int h);
SDL_Rect bounds;
void SetView(View* view, bool owns=true);
~GUIPopup();
friend class GUIWindow;
};
class GUIWindow : public ContainerView
{
std::vector<GUIPopup*> popups;
View* child;
bool ownsChild;
SDL_Window* window;
SDL_Renderer* renderer;
void Event(SDL_Event& event);
void Draw();
void DeactivateAll(View* view);
void TabNext(View* view,TabNextResult& nr);
protected:
void OnDraw(SDL_Renderer* renderer, SDL_Rect& myRect);
bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
public:
EventList<View*,GUIJsonViewNotFoundEventArgs&> JsonViewNotFound;
size_t ViewCount();
View* GetViewAt(size_t index);
FontCache* normal_font;
FontCache* monospaced_font;
GUIPalette palette;
GUIWindow(std::string title, int w, int h, Uint32 flags, const GUIPalette& palette);
void SetPalette(const GUIPalette& palette);
void SetView(View* view,bool owns=true);
void ShowPopup(GUIPopup* popup);
void ShowPopup(GUIPopup& popup);
void MakeActive(View* view);
void TabNext();
GUIWindow* GetWindow();
~GUIWindow();
friend class GUI;
friend class GUIPopup;
void SetText(std::string text);
void SetView(Tesses::Framework::Serialization::Json::JToken json);
View* CreateViewFromJson(Tesses::Framework::Serialization::Json::JObject json);
};
class GUI {
std::vector<GUIWindow*> windows;
public:
void Update();
friend class GUIWindow;
};
extern GUI gui;
class Clipper {
SDL_Rect theRect;
SDL_Renderer* renderer;
bool isClipped;
public:
Clipper(SDL_Renderer* renderer, SDL_Rect& myRect);
bool Clip(SDL_Rect rect);
~Clipper();
static void ClipRect(SDL_Rect& child, SDL_Rect& parent);
};
}
#endif

View File

@ -0,0 +1,12 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include <SDL2/SDL.h>
#include <string>
namespace Tesses::Framework::SDL2
{
bool TryParseSDLColor(std::string str, SDL_Color& col);
}
#endif

View File

@ -0,0 +1,9 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "../Streams/Stream.hpp"
#include <SDL2/SDL_rwops.h>
namespace Tesses::Framework::SDL2
{
SDL_RWops* RwopsFromStream(Tesses::Framework::Streams::Stream* strm, bool owns=true);
}
#endif

View File

@ -0,0 +1,33 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "../GUI.hpp"
namespace Tesses::Framework::SDL2::Views
{
class AbsoluteView : public ContainerView {
std::vector<std::pair<std::pair<View*,bool>,SDL_Rect>> views;
protected:
void OnDraw(SDL_Renderer* renderer, SDL_Rect& myRect);
bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
public:
size_t ViewCount();
View* GetViewAt(size_t index);
AbsoluteView();
void Add(SDL_Rect rect, View* view, bool owns=true);
void Set(View* view, SDL_Rect rect);
void Remove(View* view);
~AbsoluteView();
};
/* class AbsoluteContainer : public View {
std::vector<std::pair<std::pair<View*,bool>,SDL_Rect>> views;
public:
void Add(SDL_Rect rect, View* view, bool owns=true);
void Set(View* view, SDL_Rect rect);
void Remove(View* view);
~AbsoluteContainer();
};*/
}
#endif

View File

@ -0,0 +1,18 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "../GUI.hpp"
namespace Tesses::Framework::SDL2::Views
{
class ButtonView : public View {
protected:
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
public:
ButtonView();
ButtonView(std::string text);
};
}
#endif

View File

@ -0,0 +1,25 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "../GUI.hpp"
namespace Tesses::Framework::SDL2::Views
{
class CheckView : public View {
protected:
virtual void OnCheckChanged(GUIEventArgs& event);
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
public:
CheckView(bool checked, std::string label);
CheckView();
virtual bool GetChecked();
virtual void SetChecked(bool value);
EventList<View*,GUIEventArgs&> CheckChanged;
};
}
#endif

View File

@ -0,0 +1,15 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "../GUI.hpp"
namespace Tesses::Framework::SDL2::Views
{
class LabelView : public View {
protected:
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
public:
LabelView();
LabelView(std::string text);
};
}
#endif

View File

@ -0,0 +1,17 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "../GUI.hpp"
namespace Tesses::Framework::SDL2::Views
{
class ProgressView : public View
{
protected:
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
public:
ProgressView();
ProgressView(double value);
double value;
};
};
#endif

View File

@ -0,0 +1,20 @@
#pragma once
#if defined(TESSESFRAMEWORK_ENABLE_SDL2)
#include "../GUI.hpp"
namespace Tesses::Framework::SDL2::Views
{
class TextListView : public View {
protected:
virtual void OnDraw(SDL_Renderer* renderer, SDL_Rect& r);
virtual bool OnEvent(SDL_Event& event, SDL_Rect& myBounds, SDL_Rect& visibleBounds);
public:
TextListView();
size_t firstIndex;
int selected;
std::vector<std::string> items;
};
}
#endif

View File

@ -17,19 +17,7 @@ namespace Tesses::Framework::Serialization::Json
class JOItem;
class JObject {
std::map<std::string,JToken> map;
public:
JObject();
JObject(std::initializer_list<JOItem> items);
void SetValue(std::string key, JToken item);
JToken GetValue(std::string key);
void Remove(std::string key);
std::map<std::string,JToken>& GetMap();
std::map<std::string,JToken>::iterator begin();
std::map<std::string,JToken>::iterator end();
};
class JArray
class JArray
{
std::vector<JToken> items;
public:
@ -46,15 +34,36 @@ namespace Tesses::Framework::Serialization::Json
std::vector<JToken>::iterator begin();
std::vector<JToken>::iterator end();
};
class JOItem {
class JObject {
std::map<std::string,JToken> map;
public:
JOItem();
JOItem(std::string key, JToken value);
std::string key;
JToken value;
JObject();
JObject(std::initializer_list<JOItem> items);
void SetValue(std::string key, JToken item);
template<typename T>
bool TryGetValueAsType(std::string key, T& value)
{
auto obj = GetValue(key);
if(std::holds_alternative<T>(obj))
{
value = std::get<T>(obj);
return true;
}
return false;
}
JToken GetValue(std::string key);
void Remove(std::string key);
std::map<std::string,JToken>& GetMap();
std::map<std::string,JToken>::iterator begin();
std::map<std::string,JToken>::iterator end();
};
template<typename T>
template<typename T>
bool TryGetJToken(JToken json, T& item)
{
if(std::holds_alternative<T>(json))
@ -64,6 +73,16 @@ namespace Tesses::Framework::Serialization::Json
}
return false;
}
class JOItem {
public:
JOItem();
JOItem(std::string key, JToken value);
std::string key;
JToken value;
};
class Json
{

View File

@ -21,6 +21,13 @@ namespace Tesses::Framework::Streams
bool IsValid();
void Close();
};
enum class SocketType {
ST_IPv4_TCP,
ST_IPv4_UDP,
ST_IPv6_TCP,
ST_IPv6_UDP,
ST_UNIX
};
class NetworkStream : public Stream {
int32_t sock;
bool owns;
@ -30,7 +37,7 @@ namespace Tesses::Framework::Streams
bool EndOfStream();
bool CanRead();
bool CanWrite();
NetworkStream(bool ipV6,bool datagram);
NetworkStream(SocketType type);
NetworkStream(std::string unixPath,bool isServer);
NetworkStream(std::string ipOrFqdn, uint16_t port, bool datagram,bool broadcast,bool supportIPv6);
NetworkStream(int32_t sock, bool owns);

View File

@ -0,0 +1,29 @@
#pragma once
#include "Stream.hpp"
namespace Tesses::Framework::Streams
{
struct WindowSize {
uint16_t Width;
uint16_t Height;
uint16_t Columns;
uint16_t Rows;
};
class PtyStream : public Stream
{
int socket;
int64_t pid;
bool eos;
WindowSize wS;
public:
PtyStream(WindowSize sz,std::string filename, std::vector<std::string> args,std::vector<std::string> env);
bool EndOfStream();
bool CanRead();
bool CanWrite();
size_t Read(uint8_t* buff, size_t sz);
size_t Write(const uint8_t* buff, size_t sz);
void Resize(WindowSize sz);
WindowSize GetWindowSize();
~PtyStream();
};
}

View File

@ -14,6 +14,10 @@
#include "Streams/ByteWriter.hpp"
#include "TextStreams/StreamReader.hpp"
#include "TextStreams/StreamWriter.hpp"
#include "TextStreams/StdIOReader.hpp"
#include "TextStreams/StdIOWriter.hpp"
#include "TextStreams/StringReader.hpp"
#include "TextStreams/StringWriter.hpp"
#include "Threading/Thread.hpp"
#include "Threading/Mutex.hpp"
#include "Threading/ThreadPool.hpp"
@ -28,4 +32,6 @@
#include "Mail/Smtp.hpp"
#include "HiddenField.hpp"
#include "Serialization/Json.hpp"
#include "SDL2/FontCache.hpp"
#include "SDL2/FontCache.hpp"
#include "SDL2/Stream.hpp"
#include "SDL2/GUI.hpp"

View File

@ -0,0 +1,13 @@
#include "TextReader.hpp"
namespace Tesses::Framework::TextStreams
{
class ConsoleReader : public TextReader {
public:
ConsoleReader();
bool ReadBlock(std::string& str,size_t sz);
};
ConsoleReader StdIn();
}

View File

@ -0,0 +1,15 @@
#include "TextWriter.hpp"
namespace Tesses::Framework::TextStreams
{
class ConsoleWriter : public TextWriter {
bool isError;
public:
ConsoleWriter(bool isError=false);
void WriteData(const char* text, size_t len);
};
ConsoleWriter StdOut();
ConsoleWriter StdErr();
}

View File

@ -14,6 +14,7 @@ namespace Tesses::Framework::TextStreams
StreamReader(Tesses::Framework::Streams::Stream* strm, bool owns);
StreamReader(std::filesystem::path filename);
bool ReadBlock(std::string& str,size_t sz);
bool Rewind();
~StreamReader();
};
}

View File

@ -0,0 +1,15 @@
#include "TextReader.hpp"
namespace Tesses::Framework::TextStreams {
class StringReader : public TextReader {
std::string str;
size_t offset;
public:
StringReader();
StringReader(std::string str);
size_t& GetOffset();
std::string& GetString();
bool Rewind();
bool ReadBlock(std::string& str,size_t sz);
};
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "TextWriter.hpp"
namespace Tesses::Framework::TextStreams
{
class StringWriter : public TextWriter {
private:
std::string text;
public:
std::string& GetString();
StringWriter();
StringWriter(std::string str);
void WriteData(const char* text, size_t len);
};
}

View File

@ -6,6 +6,7 @@ namespace Tesses::Framework::TextStreams
class TextReader
{
public:
virtual bool Rewind();
virtual bool ReadBlock(std::string& str,size_t sz)=0;
int32_t ReadChar();
std::string ReadLine();

View File

@ -3,14 +3,73 @@
namespace Tesses::Framework::TextStreams
{
class NewLine {}; //dummy class
class TextWriter {
public:
TextWriter();
std::string newline;
virtual void WriteData(const char* text, size_t len)=0;
void Write(std::string txt);
void Write(int64_t n);
void Write(uint64_t n);
void Write(const void* ptr);
void Write(const char* ptr);
void Write(char c);
void Write(double d);
void Write(std::string text);
inline TextWriter& operator<<(int64_t n)
{
Write(n);
return *this;
}
inline TextWriter& operator<<(uint64_t n)
{
Write(n);
return *this;
}
inline TextWriter& operator<<(const void* n)
{
Write(n);
return *this;
}
inline TextWriter& operator<<(const char* n)
{
Write(n);
return *this;
}
inline TextWriter& operator<<(char n)
{
Write(n);
return *this;
}
inline TextWriter& operator<<(double n)
{
Write(n);
return *this;
}
inline TextWriter& operator<<(std::string n)
{
Write(n);
return *this;
}
inline TextWriter& operator<<(NewLine nl)
{
WriteLine();
return *this;
}
void WriteLine(std::string txt);
void WriteLine(int64_t n);
void WriteLine(uint64_t n);
void WriteLine(const void* ptr);
void WriteLine(const char* ptr);
void WriteLine(char c);
void WriteLine(double d);
void WriteLine();
virtual ~TextWriter();
};
}