Initial commit.

This commit is contained in:
jmsgrogan 2020-05-02 08:31:03 +01:00
commit 59c6161fdb
134 changed files with 4751 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.cproject
.project
.settings/

11
CMakeLists.txt Normal file
View file

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.10)
# set the project name
project(media-tools)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
link_libraries(stdc++fs)
add_subdirectory(src)
add_subdirectory(apps)

32
apps/CMakeLists.txt Normal file
View file

@ -0,0 +1,32 @@
# Sample GUI
add_executable(sample_gui gui-main.cpp)
target_include_directories(sample_gui PUBLIC
"${PROJECT_SOURCE_DIR}/src/console"
"${PROJECT_SOURCE_DIR}/src/client"
)
target_link_libraries(sample_gui PUBLIC client windows console core
network database geometry audio graphics)
# Sample Console
add_executable(sample_console console-main.cpp)
target_include_directories(sample_console PUBLIC
"${PROJECT_SOURCE_DIR}/src/console"
)
target_link_libraries(sample_console PUBLIC console core network
database geometry audio)
# Xml practice
add_executable(xml_practice xml-practice.cpp)
target_include_directories(xml_practice PUBLIC
"${PROJECT_SOURCE_DIR}/src/core"
"${PROJECT_SOURCE_DIR}/src/core/xml"
)
target_link_libraries(xml_practice PUBLIC core)
# Markdown practice
add_executable(markdown_practice markdown-practice.cpp)
target_include_directories(markdown_practice PUBLIC
"${PROJECT_SOURCE_DIR}/src/core"
"${PROJECT_SOURCE_DIR}/src/web/markdown"
)
target_link_libraries(markdown_practice PUBLIC web core)

16
apps/console-main.cpp Normal file
View file

@ -0,0 +1,16 @@
#include <filesystem>
#include <unistd.h>
#include "MainApplication.h"
int main()
{
// Start the main app
auto main_app = MainApplication::Create();
main_app->Initialize(std::filesystem::current_path());
//main_app->RunServer();
main_app->PlayAudio();
main_app->ShutDown();
return 0;
}

20
apps/gui-main.cpp Normal file
View file

@ -0,0 +1,20 @@
#include <filesystem>
#include <memory>
#include "GuiApplication.h"
#include "MainApplication.h"
int main()
{
// Start the main app
auto main_app = MainApplication::Create();
main_app->Initialize(std::filesystem::current_path());
// Start the gui app
auto gui_app = std::make_shared<GuiApplication>();
gui_app->SetMainApplication(main_app);
gui_app->Run();
main_app->ShutDown();
return 0;
}

View file

@ -0,0 +1,47 @@
#include <filesystem>
#include <iostream>
#include <fstream>
#include <ostream>
#include "CommandLineArgs.h"
#include "MarkdownParser.h"
#include "HtmlDocument.h"
#include "HtmlWriter.h"
int main(int argc, char *argv[])
{
CommandLineArgs command_line_args;
command_line_args.Process(argc, argv);
if(command_line_args.GetNumberOfArgs() < 2)
{
std::cerr << "Expected a filepath argument" << std::endl;
return -1;
}
MarkdownParser parser;
const auto filepath = command_line_args.GetArg(1);
if(!std::filesystem::exists(filepath))
{
std::cerr << "Couldn't find file: " << filepath << std::endl;
return -1;
}
std::ifstream md_file;
md_file.open(filepath, std::ifstream::in);
while(md_file.good())
{
std::string line;
std::getline(md_file, line);
parser.ProcessLine(line);
}
md_file.close();
auto html_document = parser.GetHtml();
HtmlWriter writer;
std::string html_string = writer.ToString(html_document);
std::ofstream out("/home/james/test.html");
out << html_string;
out.close();
return 0;
}

38
apps/xml-practice.cpp Normal file
View file

@ -0,0 +1,38 @@
#include <filesystem>
#include <iostream>
#include <fstream>
#include "CommandLineArgs.h"
#include "XmlParser.h"
int main(int argc, char *argv[])
{
CommandLineArgs command_line_args;
command_line_args.Process(argc, argv);
if(command_line_args.GetNumberOfArgs() < 2)
{
std::cerr << "Expected a filepath argument" << std::endl;
return -1;
}
XmlParser parser;
const auto filepath = command_line_args.GetArg(1);
if(!std::filesystem::exists(filepath))
{
std::cerr << "Couldn't find file: " << filepath << std::endl;
return -1;
}
std::ifstream xml_file;
xml_file.open(filepath, std::ifstream::in);
while(xml_file.good())
{
std::string line;
std::getline(xml_file, line);
parser.ProcessLine(line);
}
xml_file.close();
return 0;
}

11
src/CMakeLists.txt Normal file
View file

@ -0,0 +1,11 @@
add_subdirectory(core)
add_subdirectory(database)
add_subdirectory(network)
add_subdirectory(geometry)
add_subdirectory(audio)
add_subdirectory(console)
add_subdirectory(client)
add_subdirectory(graphics)
add_subdirectory(windows)
add_subdirectory(web)
add_subdirectory(ui_elements)

71
src/audio/AudioDevice.cpp Normal file
View file

@ -0,0 +1,71 @@
#include "AudioDevice.h"
AudioDevice::AudioDevice()
: mName("plughw:1,0"),
mSampleRate(44100),
mNumChannels(2),
mPeriod(2),
mBufferSize()
{
}
AudioDevice::~AudioDevice()
{
}
std::shared_ptr<AudioDevice> AudioDevice::Create()
{
return std::make_shared<AudioDevice>();
}
void AudioDevice::SetNumChannels(unsigned numChannels)
{
mNumChannels = numChannels;
}
void AudioDevice::SetPeriod(unsigned period)
{
mPeriod = period;
}
void AudioDevice::SetBufferSize(std::size_t bufferSize)
{
mBufferSize = bufferSize;
}
unsigned AudioDevice::GetNumChannels()
{
return mNumChannels;
}
unsigned AudioDevice::GetPeriod()
{
return mPeriod;
}
std::size_t AudioDevice::GetBufferSize()
{
return mBufferSize;
}
void AudioDevice::SetSampleRate(unsigned rate)
{
mSampleRate = rate;
}
unsigned AudioDevice::GetSampleRate()
{
return mSampleRate;
}
void AudioDevice::SetName(const std::string& name)
{
mName = name;
}
std::string AudioDevice::GetName()
{
return mName;
}

46
src/audio/AudioDevice.h Normal file
View file

@ -0,0 +1,46 @@
#pragma once
#include <memory>
#include <string>
class AudioDevice
{
private:
std::string mName;
unsigned mSampleRate;
unsigned mNumChannels;
unsigned mPeriod;
std::size_t mBufferSize;
public:
AudioDevice();
~AudioDevice();
void SetSampleRate(unsigned rate);
unsigned GetSampleRate();
void SetName(const std::string& name);
void SetNumChannels(unsigned numChannels);
void SetPeriod(unsigned period);
void SetBufferSize(std::size_t bufferSize);
unsigned GetNumChannels();
unsigned GetPeriod();
std::size_t GetBufferSize();
std::string GetName();
static std::shared_ptr<AudioDevice> Create();
};
using AudioDevicePtr = std::shared_ptr<AudioDevice>;

View file

@ -0,0 +1,33 @@
#include "AudioManager.h"
AudioManager::AudioManager()
: mAudioDevices(),
mAudioInterface()
{
mAudioInterface = AlsaInterface::Create();
}
AudioManager::~AudioManager()
{
}
std::shared_ptr<AudioManager> AudioManager::Create()
{
return std::make_shared<AudioManager>();
}
void AudioManager::AddAudioDevice(AudioDevicePtr device)
{
mAudioDevices.push_back(device);
}
AlsaInterfacePtr AudioManager::GetAudioInterface()
{
return mAudioInterface;
}
std::vector<AudioDevicePtr> AudioManager::GetAudioDevices()
{
return mAudioDevices;
}

32
src/audio/AudioManager.h Normal file
View file

@ -0,0 +1,32 @@
#pragma once
#include <memory>
#include <vector>
#include "AlsaInterface.h"
#include "AudioDevice.h"
class AudioManager
{
private:
std::vector<AudioDevicePtr> mAudioDevices;
AlsaInterfacePtr mAudioInterface;
public:
AudioManager();
~AudioManager();
static std::shared_ptr<AudioManager> Create();
void AddAudioDevice(AudioDevicePtr device);
std::vector<AudioDevicePtr> GetAudioDevices();
AlsaInterfacePtr GetAudioInterface();
};
using AudioManagerPtr = std::shared_ptr<AudioManager>;

13
src/audio/CMakeLists.txt Normal file
View file

@ -0,0 +1,13 @@
list(APPEND audio_LIB_INCLUDES
AudioDevice.cpp
AudioManager.cpp
audio_interfaces/AlsaInterface.cpp)
add_library(audio SHARED ${audio_LIB_INCLUDES})
target_include_directories(audio PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/audio_interfaces"
)
target_link_libraries(audio PUBLIC asound)

View file

@ -0,0 +1,141 @@
#include "AlsaInterface.h"
#include <iostream>
AlsaInterface::AlsaInterface()
:mHandle(),
mHardwareParams(),
mPeriodSize(8192)
{
}
AlsaInterface::~AlsaInterface()
{
}
std::shared_ptr<AlsaInterface> AlsaInterface::Create()
{
return std::make_shared<AlsaInterface>();
}
void AlsaInterface::OpenDevice(AudioDevicePtr device)
{
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
if (snd_pcm_open(&mHandle, device->GetName().c_str(), stream, 0) < 0)
{
std::cerr << "Error opening PCM device: " << device->GetName() << std::endl;
return;
}
snd_pcm_hw_params_alloca(&mHardwareParams);
if (snd_pcm_hw_params_any(mHandle, mHardwareParams) < 0)
{
std::cerr << "Can not configure this PCM device.\n" << std::endl;
return;
}
SetAccessType(device);
SetSampleFormat(device);
SetSampleRate(device);
SetPeriod(device);
SetBufferSize(device);
SetChannelNumber(device);
/* Apply HW parameter settings to */
/* PCM device and prepare device */
if (snd_pcm_hw_params(mHandle, mHardwareParams) < 0) {
std::cerr << "Error setting HW params." << std::endl;
return;
}
}
void AlsaInterface::SetAccessType(AudioDevicePtr device)
{
if (snd_pcm_hw_params_set_access(mHandle, mHardwareParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
std::cerr << "Error setting device access." << std::endl;
return;
}
}
void AlsaInterface::SetSampleFormat(AudioDevicePtr device)
{
/* Set sample format */
if (snd_pcm_hw_params_set_format(mHandle, mHardwareParams, SND_PCM_FORMAT_S16_LE) < 0) {
std::cerr << "Error setting format. " << std::endl;
return;
}
}
void AlsaInterface::SetSampleRate(AudioDevicePtr device)
{
unsigned rate = device->GetSampleRate();
unsigned exact_rate = rate;
if (snd_pcm_hw_params_set_rate_near(mHandle, mHardwareParams, &exact_rate, 0) < 0)
{
std::cerr << "Error setting rate. " << std::endl;
return;
}
if (rate != exact_rate) {
std::cerr << "The rate is not supported by your hardware." << std::endl;
}
}
void AlsaInterface::SetPeriod(AudioDevicePtr device)
{
/* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(mHandle, mHardwareParams, device->GetPeriod(), 0) < 0)
{
std::cerr << "Error setting periods. " << std::endl;
return;
}
}
void AlsaInterface::SetBufferSize(AudioDevicePtr device)
{
snd_pcm_uframes_t periodsize = 8192; /* Periodsize (bytes) */
int periods = static_cast<int>(device->GetPeriod());
/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if (snd_pcm_hw_params_set_buffer_size(mHandle, mHardwareParams, (mPeriodSize * periods)>>2) < 0)
{
std::cerr << "Error setting buffersize. " << std::endl;
return;
}
}
void AlsaInterface::SetChannelNumber(AudioDevicePtr device)
{
/* Set number of channels */
if (snd_pcm_hw_params_set_channels(mHandle, mHardwareParams, device->GetNumChannels()) < 0)
{
std::cout << "Error setting channels" << std::endl;
return;
}
}
void AlsaInterface::Play(AudioDevicePtr device)
{
int num_frames = 10;
unsigned char *data = (unsigned char *)malloc(mPeriodSize);
int frames = mPeriodSize >> 2;
for(int l1 = 0; l1 < 100; l1++)
{
for(int l2 = 0; l2 < num_frames; l2++)
{
short s1 = (l2 % 128) * 100 - 5000;
short s2 = (l2 % 256) * 100 - 5000;
data[4*l2] = (unsigned char)s1;
data[4*l2+1] = s1 >> 8;
data[4*l2+2] = (unsigned char)s2;
data[4*l2+3] = s2 >> 8;
}
while ((snd_pcm_writei(mHandle, data, frames)) < 0)
{
snd_pcm_prepare(mHandle);
fprintf(stderr, "<<<<<<<<<<<<<<< Buffer Underrun >>>>>>>>>>>>>>>\n");
}
}
}

View file

@ -0,0 +1,39 @@
#pragma once
#include <memory>
#include <alsa/asoundlib.h>
#include "AudioDevice.h"
class AlsaInterface
{
snd_pcm_t* mHandle;
snd_pcm_hw_params_t* mHardwareParams;
snd_pcm_uframes_t mPeriodSize;
public:
AlsaInterface();
~AlsaInterface();
static std::shared_ptr<AlsaInterface> Create();
void OpenDevice(AudioDevicePtr device);
void SetAccessType(AudioDevicePtr device);
void SetSampleFormat(AudioDevicePtr device);
void SetSampleRate(AudioDevicePtr device);
void SetPeriod(AudioDevicePtr device);
void SetBufferSize(AudioDevicePtr device);
void SetChannelNumber(AudioDevicePtr device);
void Play(AudioDevicePtr device);
};
using AlsaInterfacePtr = std::shared_ptr<AlsaInterface>;

10
src/client/CMakeLists.txt Normal file
View file

@ -0,0 +1,10 @@
list(APPEND client_LIB_INCLUDES GuiApplication.cpp)
add_library(client SHARED ${client_LIB_INCLUDES})
target_link_libraries(client ui_elements windows core console database geometry)
target_include_directories(client PUBLIC
"${PROJECT_SOURCE_DIR}/src/console"
"${PROJECT_SOURCE_DIR}/src/ui_elements/widgets"
)

View file

@ -0,0 +1,70 @@
#include "GuiApplication.h"
#include <iostream>
#include "Widget.h"
#include "HorizontalSpacer.h"
#include "Button.h"
#include "Label.h"
#include "XcbInterface.h"
#include "XcbKeyboard.h"
#include "Window.h"
#include "TextElement.h"
#include "WindowManager.h"
GuiApplication::GuiApplication()
: AbstractDesktopApp(),
mMainApplication(),
mDesktopManager()
{
mDesktopManager = DesktopManager::Create();
}
GuiApplication::~GuiApplication()
{
}
void GuiApplication::SetMainApplication(MainApplicationPtr app)
{
mMainApplication = app;
//mDesktopManager->SetMainApp(shared_from_this());
}
void GuiApplication::Run()
{
auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow();
mainWindow->SetSize(800, 600);
auto label = Label::Create();
label->SetLabel("Click the button!!");
label->SetBackgroundColor(Color::Create(0, 200, 200));
auto button = Button::Create();
button->SetLabel("Ok");
button->SetBackgroundColor(Color::Create(0, 0, 200));
auto spacer = HorizontalSpacer::Create();
spacer->AddWidget(label);
spacer->AddWidget(button);
mainWindow->AddWidget(spacer);
mDesktopManager->SetKeyboard(XcbKeyboard::Create());
bool useOpenGl = false;
XcbInterface window_interface;
window_interface.SetUseOpenGl(useOpenGl);
window_interface.Initialize();
window_interface.AddWindow(mainWindow);
window_interface.ShowWindow(mainWindow);
if(useOpenGl)
{
window_interface.CreateOpenGlDrawable(mainWindow);
}
window_interface.Loop(mDesktopManager);
window_interface.ShutDown();
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <memory>
#include "MainApplication.h"
#include "AbstractDesktopApp.h"
#include "DesktopManager.h"
class GuiApplication : public AbstractDesktopApp, std::enable_shared_from_this<GuiApplication>
{
private:
DesktopManagerPtr mDesktopManager;
MainApplicationPtr mMainApplication;
public:
GuiApplication();
~GuiApplication();
void SetMainApplication(MainApplicationPtr app);
void Run();
};

View file

@ -0,0 +1,13 @@
list(APPEND console_LIB_INCLUDES MainApplication.cpp)
add_library(console SHARED ${console_LIB_INCLUDES})
target_include_directories(console PUBLIC
"${PROJECT_SOURCE_DIR}/src/core/loggers"
"${PROJECT_SOURCE_DIR}/src/database"
"${PROJECT_SOURCE_DIR}/src/database/database_interfaces"
"${PROJECT_SOURCE_DIR}/src/network"
"${PROJECT_SOURCE_DIR}/src/network/sockets"
"${PROJECT_SOURCE_DIR}/src/audio"
"${PROJECT_SOURCE_DIR}/src/audio/audio_interfaces"
)

View file

@ -0,0 +1,52 @@
#include "MainApplication.h"
MainApplication::MainApplication()
:mLogger(),
mDatabaseManager()
{
}
MainApplication::~MainApplication()
{
}
void MainApplication::Initialize(const std::filesystem::path& workDir)
{
mLogger = FileLogger::Create();
mLogger->SetWorkDirectory(workDir.string());
mLogger->Open();
mLogger->LogLine("Launched");
mDatabaseManager = DatabaseManager::Create();
mDatabaseManager->CreateDatabase(workDir.string() + "/database.db");
mNetworkManager = NetworkManager::Create();
mAudioManager = AudioManager::Create();
}
void MainApplication::RunServer()
{
mNetworkManager->RunHttpServer();
}
void MainApplication::PlayAudio()
{
auto device = AudioDevice::Create();
mAudioManager->GetAudioInterface()->OpenDevice(device);
mAudioManager->GetAudioInterface()->Play(device);
}
void MainApplication::ShutDown()
{
mLogger->Close();
mDatabaseManager->OnShutDown();
mNetworkManager->ShutDown();
}
std::shared_ptr<MainApplication> MainApplication::Create()
{
return std::make_shared<MainApplication>();
}

View file

@ -0,0 +1,37 @@
#pragma once
#include <memory>
#include <filesystem>
#include "AudioManager.h"
#include "FileLogger.h"
#include "DatabaseManager.h"
#include "NetworkManager.h"
class MainApplication
{
private:
FileLoggerPtr mLogger;
DatabaseManagerPtr mDatabaseManager;
NetworkManagerPtr mNetworkManager;
AudioManagerPtr mAudioManager;
public:
MainApplication();
~MainApplication();
void Initialize(const std::filesystem::path& workDir);
void RunServer();
void PlayAudio();
void ShutDown();
static std::shared_ptr<MainApplication> Create();
};
using MainApplicationPtr = std::shared_ptr<MainApplication>;

13
src/core/CMakeLists.txt Normal file
View file

@ -0,0 +1,13 @@
list(APPEND core_LIB_INCLUDES
Event.cpp
Color.cpp
CommandLineArgs.cpp
loggers/FileLogger.cpp
StringUtils.cpp)
# add the executable
add_library(core SHARED ${core_LIB_INCLUDES})
target_include_directories(core PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
)

36
src/core/Color.cpp Normal file
View file

@ -0,0 +1,36 @@
#include "Color.h"
Color::Color(unsigned r, unsigned g, unsigned b, double a)
: mR(r),
mG(g),
mB(b),
mAlpha(a)
{
}
std::shared_ptr<Color> Color::Create(unsigned r, unsigned g, unsigned b,
double a )
{
return std::make_shared<Color>(r, g, b, a);
}
unsigned Color::GetR()
{
return mR;
}
unsigned Color::GetG()
{
return mG;
}
unsigned Color::GetB()
{
return mB;
}
double Color::GetAlpha()
{
return mAlpha;
}

23
src/core/Color.h Normal file
View file

@ -0,0 +1,23 @@
#pragma once
#include <memory>
class Color
{
unsigned mR;
unsigned mG;
unsigned mB;
double mAlpha;
public:
Color(unsigned r, unsigned g, unsigned b, double a = 1.0);
static std::shared_ptr<Color> Create(unsigned r, unsigned g, unsigned b, double a = 1.0);
unsigned GetR();
unsigned GetG();
unsigned GetB();
double GetAlpha();
};
using ColorPtr = std::shared_ptr<Color>;

View file

@ -0,0 +1,29 @@
#include "CommandLineArgs.h"
CommandLineArgs::CommandLineArgs()
: mArugments()
{
}
void CommandLineArgs::Process(int argc, char *argv[])
{
for(int idx=0; idx<argc; idx++)
{
mArugments.push_back(std::string(argv[idx]));
}
}
std::size_t CommandLineArgs::GetNumberOfArgs() const
{
return mArugments.size();
}
std::string CommandLineArgs::GetArg(std::size_t index) const
{
if(index<mArugments.size())
{
return mArugments[index];
}
return "";
}

View file

@ -0,0 +1,19 @@
#pragma once
#include <memory>
#include <vector>
#include <string>
class CommandLineArgs
{
std::vector<std::string> mArugments;
public:
CommandLineArgs();
void Process(int argc, char *argv[]);
std::size_t GetNumberOfArgs() const;
std::string GetArg(std::size_t index) const;
};

11
src/core/Event.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "Event.h"
Event::Event()
{
}
Event::~Event()
{
}

8
src/core/Event.h Normal file
View file

@ -0,0 +1,8 @@
class Event
{
public:
Event();
~Event();
};

14
src/core/StringUtils.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "StringUtils.h"
#include <locale>
bool StringUtils::IsAlphaNumeric(char c)
{
std::locale loc;
return std::isalnum(c, loc);
}
bool StringUtils::IsSpace(char c)
{
std::locale loc;
return std::isspace(c, loc);
}

19
src/core/StringUtils.h Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <string>
class StringUtils
{
public:
static constexpr char LEFT_BRACKET = '<';
static constexpr char RIGHT_BRACKET = '>';
static constexpr char FORWARD_SLASH = '/';
static constexpr char BACK_SLASH = '\\';
static constexpr char QUESTION_MARK = '?';
static constexpr char EQUALS = '=';
static constexpr char DOUBLE_QUOTE = '"';
static constexpr char SINGLE_QUOTE = '\'';
static bool IsAlphaNumeric(char c);
static bool IsSpace(char c);
};

View file

@ -0,0 +1,38 @@
#include "HttpResponse.h"
HttpResponse::HttpResponse()
:mHttpVersion("1.1"),
mResponseCode("200 OK"),
mContentType("text/plain"),
mBody()
{
}
HttpResponse::~HttpResponse()
{
}
void HttpResponse::SetBody(const std::string& body)
{
mBody = body;
}
unsigned HttpResponse::GetBodyLength()
{
return mBody.length();
}
std::string HttpResponse::GetHeaderString()
{
std::string header = "HTTP/" + mHttpVersion + " " + mResponseCode + "\n";
header += "Content-Type: " + mContentType + "\n";
header += "Content-Length: " + std::to_string(GetBodyLength()) + "\n";
return header;
}
std::string HttpResponse::ToString()
{
return GetHeaderString() + "\n\n" + mBody;
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <string>
class HttpResponse
{
std::string mHttpVersion;
std::string mResponseCode;
std::string mContentType;
std::string mBody;
public:
HttpResponse();
~HttpResponse();
void SetBody(const std::string& body);
unsigned GetBodyLength();
std::string GetHeaderString();
std::string ToString();
};

View file

@ -0,0 +1,44 @@
#include "FileLogger.h"
FileLogger::FileLogger()
:mWorkDirectory(),
mFileName("MT_Log.txt"),
mFileStream()
{
}
FileLogger::~FileLogger()
{
}
void FileLogger::SetWorkDirectory(const std::string& workDir)
{
mWorkDirectory = workDir;
}
void FileLogger::SetFileName(const std::string& fileName)
{
mFileName = fileName;
}
void FileLogger::Open()
{
mFileStream.open(mWorkDirectory + "/" + mFileName);
}
void FileLogger::Close()
{
mFileStream.close();
}
void FileLogger::LogLine(const std::string& line)
{
mFileStream << line << std::endl;
}
std::shared_ptr<FileLogger> FileLogger::Create()
{
return std::make_shared<FileLogger>();
}

View file

@ -0,0 +1,35 @@
#pragma once
#include <memory>
#include <string>
#include <fstream>
class FileLogger
{
std::string mWorkDirectory;
std::string mFileName;
std::ofstream mFileStream;
public:
FileLogger();
~FileLogger();
void SetWorkDirectory(const std::string& workDir);
void SetFileName(const std::string& fileName);
void Open();
void Close();
void LogLine(const std::string& line);
static std::shared_ptr<FileLogger> Create();
};
using FileLoggerPtr = std::shared_ptr<FileLogger>;

View file

@ -0,0 +1,13 @@
list(APPEND database_LIB_INCLUDES
Database.cpp
DatabaseManager.cpp
database_interfaces/SqliteInterface.cpp)
add_library(database SHARED ${database_LIB_INCLUDES})
target_include_directories(database PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/database_interfaces"
)
target_link_libraries(database PUBLIC sqlite3)

27
src/database/Database.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "Database.h"
Database::Database()
: mPath()
{
}
Database::~Database()
{
}
std::shared_ptr<Database> Database::Create()
{
return std::make_shared<Database>();
}
void Database::SetPath(const std::string& path)
{
mPath = path;
}
std::string Database::GetPath()
{
return mPath;
}

24
src/database/Database.h Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include <memory>
#include <string>
class Database
{
std::string mPath;
public:
Database();
~Database();
static std::shared_ptr<Database> Create();
void SetPath(const std::string& path);
std::string GetPath();
};
using DatabasePtr = std::shared_ptr<Database>;

View file

@ -0,0 +1,36 @@
#include "DatabaseManager.h"
DatabaseManager::DatabaseManager()
: mDatabase(),
mDatabaseInterface()
{
}
DatabaseManager::~DatabaseManager()
{
}
std::shared_ptr<DatabaseManager> DatabaseManager::Create()
{
return std::make_shared<DatabaseManager>();
}
void DatabaseManager::CreateDatabase(const std::string& path)
{
mDatabase = Database::Create();
mDatabase->SetPath(path);
mDatabaseInterface = SqliteInterface::Create();
mDatabaseInterface->Open(mDatabase);
}
void DatabaseManager::OnShutDown()
{
if(mDatabaseInterface)
{
mDatabaseInterface->Close();
}
}

View file

@ -0,0 +1,27 @@
#pragma once
#include <memory>
#include "SqliteInterface.h"
#include "Database.h"
class DatabaseManager
{
DatabasePtr mDatabase;
SqliteInterfacePtr mDatabaseInterface;
public:
DatabaseManager();
~DatabaseManager();
static std::shared_ptr<DatabaseManager> Create();
void CreateDatabase(const std::string& path);
void OnShutDown();
};
using DatabaseManagerPtr = std::shared_ptr<DatabaseManager>;

View file

@ -0,0 +1,56 @@
#include "SqliteInterface.h"
#include <iostream>
SqliteInterface::SqliteInterface()
: mSqliteDb(nullptr)
{
}
SqliteInterface::~SqliteInterface()
{
}
std::shared_ptr<SqliteInterface> SqliteInterface::Create()
{
return std::make_shared<SqliteInterface>();
}
void SqliteInterface::Open(DatabasePtr db)
{
int rc = sqlite3_open(db->GetPath().c_str(), &mSqliteDb);
if( rc )
{
std::cout << "Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb) << std::endl;
sqlite3_close(mSqliteDb);
}
}
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
for(int i=0; i<argc; i++)
{
//std::cout << "%s = %s\n" << azColName[i] << argv[i] ? argv[i] : "NULL" << std::endl;
std::cout << "NULL" << std::endl;
}
return 0;
}
void SqliteInterface::Run(const std::string& statement)
{
char *zErrMsg = 0;
int rc = sqlite3_exec(mSqliteDb, statement.c_str(), callback, 0, &zErrMsg);
if( rc!=SQLITE_OK )
{
std::cout << "SQL error: %s\n" << zErrMsg << std::endl;
sqlite3_free(zErrMsg);
}
}
void SqliteInterface::Close()
{
sqlite3_close(mSqliteDb);
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <memory>
#include <sqlite3.h>
#include "Database.h"
class SqliteInterface
{
sqlite3* mSqliteDb;
public:
SqliteInterface();
~SqliteInterface();
static std::shared_ptr<SqliteInterface> Create();
void Open(DatabasePtr db);
void Close();
void Run(const std::string& statement);
};
using SqliteInterfacePtr = std::shared_ptr<SqliteInterface>;

View file

@ -0,0 +1,7 @@
list(APPEND geometry_LIB_INCLUDES
DiscretePoint.cpp
Point.cpp)
# add the library
add_library(geometry SHARED ${geometry_LIB_INCLUDES})

View file

@ -0,0 +1,28 @@
#include "DiscretePoint.h"
DiscretePoint::DiscretePoint(unsigned x, unsigned y)
: mX(x),
mY(y)
{
}
DiscretePoint::~DiscretePoint()
{
};
std::shared_ptr<DiscretePoint> DiscretePoint::Create(unsigned x, unsigned y)
{
return std::make_shared<DiscretePoint>(x, y);
}
unsigned DiscretePoint::GetX() const
{
return mX;
}
unsigned DiscretePoint::GetY() const
{
return mY;
}

View file

@ -0,0 +1,25 @@
#pragma once
#include <memory>
class DiscretePoint
{
unsigned mX;
unsigned mY;
public:
DiscretePoint(unsigned x, unsigned y);
~DiscretePoint();
std::shared_ptr<DiscretePoint> Create(unsigned x, unsigned y);
unsigned GetX() const;
unsigned GetY() const;
};
using Pixel = DiscretePoint;
using DiscretePointPtr = std::shared_ptr<DiscretePoint>;
using PixelPtr = DiscretePointPtr;

18
src/geometry/Point.cpp Normal file
View file

@ -0,0 +1,18 @@
#include "Point.h"
Point::Point(double x, double y)
: mX(x),
mY(y)
{
}
Point::~Point()
{
};
std::shared_ptr<Point> Point::Create(double x, double y)
{
return std::make_shared<Point>(x, y);
}

19
src/geometry/Point.h Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <memory>
class Point
{
double mX;
double mY;
public:
Point(double x, double y);
~Point();
std::shared_ptr<Point> Create(double x, double y);
};
using PointPtr = std::shared_ptr<Point>;

View file

7
src/geometry/Rectangle.h Normal file
View file

@ -0,0 +1,7 @@
#pragma once
class Rectangle
{
};

View file

@ -0,0 +1,5 @@
list(APPEND graphics_LIB_INCLUDES OpenGlInterface)
add_library(graphics SHARED ${graphics_LIB_INCLUDES})
target_link_libraries(graphics PUBLIC GL)

View file

@ -0,0 +1,20 @@
#include "OpenGlInterface.h"
#include <GL/gl.h>
void OpenGlInterface::draw()
{
glClearColor(0.4, 0.4, 0.4, 0.4);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.7, 0.7, 0);
glVertex3f(0.7, 0.7, 0);
glVertex3f(0, -1, 0);
glEnd();
glFlush();
}

View file

@ -0,0 +1,10 @@
#pragma once
class OpenGlInterface
{
public:
static void draw();
};

View file

@ -0,0 +1,14 @@
list(APPEND network_LIB_INCLUDES
NetworkManager.cpp
sockets/Socket.cpp
sockets/UnixSocketInterface.cpp)
add_library(network SHARED ${network_LIB_INCLUDES})
target_include_directories(network PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/sockets"
"${PROJECT_SOURCE_DIR}/src/core/http"
)
target_link_libraries(network PUBLIC core)

View file

@ -0,0 +1,36 @@
#include "NetworkManager.h"
NetworkManager::NetworkManager()
: mActiveSockets(),
mSocketInterface()
{
}
NetworkManager::~NetworkManager()
{
}
std::shared_ptr<NetworkManager> NetworkManager::Create()
{
return std::make_shared<NetworkManager>();
}
void NetworkManager::Initialize()
{
mSocketInterface = UnixSocketInterface::Create();
}
void NetworkManager::RunHttpServer()
{
auto socket = Socket::Create();
mSocketInterface->CreateSocket(socket);
mSocketInterface->Listen(socket);
mSocketInterface->Run(socket);
}
void NetworkManager::ShutDown()
{
}

View file

@ -0,0 +1,33 @@
#pragma once
#include <memory>
#include <vector>
#include "Socket.h"
#include "UnixSocketInterface.h"
class NetworkManager
{
std::vector<SocketPtr> mActiveSockets;
UnixSocketInterfacePtr mSocketInterface;
public:
NetworkManager();
~NetworkManager();
static std::shared_ptr<NetworkManager> Create();
void Initialize();
void OpenSocket(SocketPtr socket);
void CloseSocket(SocketPtr socket);
void RunHttpServer();
void ShutDown();
};
using NetworkManagerPtr = std::shared_ptr<NetworkManager>;

View file

@ -0,0 +1,38 @@
#include "Socket.h"
Socket::Socket()
: mPort(8888),
mMessage()
{
}
Socket::~Socket()
{
}
std::shared_ptr<Socket> Socket::Create()
{
return std::make_shared<Socket>();
}
std::string Socket::GetMessage()
{
return mMessage;
}
void Socket::SetMessage(const std::string& message)
{
mMessage = message;
}
void Socket::SetPort(unsigned port)
{
mPort = port;
}
unsigned Socket::GetPort()
{
return mPort;
}

View file

@ -0,0 +1,28 @@
#pragma once
#include <memory>
#include <string>
class Socket
{
unsigned mPort;
std::string mMessage;
public:
Socket();
~Socket();
static std::shared_ptr<Socket> Create();
void SetPort(unsigned port);
unsigned GetPort();
std::string GetMessage();
void SetMessage(const std::string& message);
};
using SocketPtr = std::shared_ptr<Socket>;

View file

@ -0,0 +1,100 @@
#include "UnixSocketInterface.h"
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "HttpResponse.h"
UnixSocketInterface::UnixSocketInterface()
: mOpeningHandles(),
mBufferSize(1024)
{
}
UnixSocketInterface::~UnixSocketInterface()
{
}
std::shared_ptr<UnixSocketInterface> UnixSocketInterface::Create()
{
return std::make_shared<UnixSocketInterface>();
}
void UnixSocketInterface::CreateSocket(SocketPtr socketPtr)
{
mOpeningHandles[socketPtr] = socket(AF_INET, SOCK_STREAM, 0);
}
void UnixSocketInterface::Listen(SocketPtr socket)
{
if(mOpeningHandles[socket] < 0)
{
std::cerr << "Error opening socket" << std::endl;
return;
}
int port = static_cast<int>(socket->GetPort());
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
int result = bind(mOpeningHandles[socket], (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(result< 0)
{
std::cerr << "Error binding socket" << std::endl;
return;
}
listen(mOpeningHandles[socket], 5);
}
void UnixSocketInterface::Run(SocketPtr socket)
{
if(mOpeningHandles[socket] < 0)
{
std::cerr << "Error opening socket" << std::endl;
return;
}
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
while(true)
{
SocketHandle new_socket_handle = accept(mOpeningHandles[socket],
(struct sockaddr *) &cli_addr, &clilen);
if (new_socket_handle < 0)
{
std::cerr << "Error on accept" << std::endl;
return;
}
char buffer[mBufferSize] = {0};
int n = read(new_socket_handle, buffer, mBufferSize);
if (n < 0)
{
std::cerr << "Error on read" << std::endl;
return;
}
socket->SetMessage(buffer);
std::cout << "Here is the message: " << buffer << std::endl;
HttpResponse response;
response.SetBody("Hello world!");
std::string response_message = response.ToString();
n = write(new_socket_handle, response_message.c_str(), response_message.length());
if (n < 0)
{
std::cerr << "Error on write" << std::endl;
return;
}
close(new_socket_handle);
}
}

View file

@ -0,0 +1,30 @@
#pragma once
#include <memory>
#include <map>
#include <cstddef>
#include "Socket.h"
class UnixSocketInterface
{
using SocketHandle = int;
std::map<SocketPtr, SocketHandle> mOpeningHandles;
std::size_t mBufferSize;
public:
UnixSocketInterface();
~UnixSocketInterface();
static std::shared_ptr<UnixSocketInterface> Create();
void CreateSocket(SocketPtr socket);
void Listen(SocketPtr socket);
void Run(SocketPtr socket);
};
using UnixSocketInterfacePtr = std::shared_ptr<UnixSocketInterface>;

View file

@ -0,0 +1,29 @@
list(APPEND ui_elements_LIB_INCLUDES
desktop_elements/Keyboard.cpp
desktop_elements/Screen.cpp
desktop_elements/Window.cpp
ui_events/KeyboardEvent.cpp
ui_events/MouseEvent.cpp
ui_events/UiEvent.cpp
ui_events/PaintEvent.cpp
widgets/Widget.cpp
widgets/Button.cpp
widgets/Label.cpp
widgets/HorizontalSpacer.cpp
widgets/elements/GeometryElement.cpp
widgets/elements/RectangleElement.cpp
widgets/elements/TextElement.cpp
widgets/elements/VisualLayer.cpp)
add_library(ui_elements SHARED ${ui_elements_LIB_INCLUDES})
target_include_directories(ui_elements PUBLIC
"${PROJECT_SOURCE_DIR}/src/core/"
"${PROJECT_SOURCE_DIR}/src/geometry/"
"${CMAKE_CURRENT_SOURCE_DIR}/"
"${CMAKE_CURRENT_SOURCE_DIR}/widgets"
"${CMAKE_CURRENT_SOURCE_DIR}/widgets/elements"
"${CMAKE_CURRENT_SOURCE_DIR}/ui_events"
"${CMAKE_CURRENT_SOURCE_DIR}/desktop_elements"
)
target_link_libraries(ui_elements PUBLIC core geometry)

View file

@ -0,0 +1,13 @@
#include "Keyboard.h"
Keyboard::Keyboard()
:mKeyMap()
{
}
std::shared_ptr<Keyboard> Keyboard::Create()
{
return std::make_shared<Keyboard>();
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <memory>
#include <map>
#include <string>
class Keyboard
{
public:
using KeyCode = unsigned;
protected:
std::map<KeyCode, std::string> mKeyMap;
public:
Keyboard();
virtual ~Keyboard() = default;
static std::shared_ptr<Keyboard> Create();
virtual std::string GetKeyString(KeyCode code)
{
return "";
}
};
using KeyboardPtr = std::shared_ptr<Keyboard>;

View file

@ -0,0 +1,18 @@
#include "Screen.h"
namespace mt{
Screen::Screen()
{
}
Screen::~Screen()
{
}
std::shared_ptr<Screen> Screen::Create()
{
return std::make_shared<Screen>();
}
}

View file

@ -0,0 +1,17 @@
#pragma once
#include <memory>
namespace mt{
class Screen
{
public:
Screen();
~Screen();
static std::shared_ptr<Screen> Create();
};
}
using ScreenPtr = std::shared_ptr<mt::Screen>;

View file

@ -0,0 +1,67 @@
#include "Window.h"
namespace mt{
Window::Window()
:mWidth(400),
mHeight(300),
mWidget()
{
}
Window::~Window()
{
}
std::vector<VisualLayerPtr> Window::GetLayers()
{
return mLayers;
}
void Window::OnMouseEvent(MouseEventPtr event)
{
mWidget->OnMouseEvent(event);
}
void Window::OnPaint(PaintEventPtr event)
{
mLayers.clear();
mWidget->SetSize(mWidth, mHeight);
mWidget->OnPaintEvent(event);
auto layers = mWidget->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
}
std::shared_ptr<Window> Window::Create()
{
return std::make_shared<Window>();
}
void Window::AddWidget(WidgetPtr widget)
{
mWidget = widget;
}
WidgetPtr Window::GetWidget()
{
return mWidget;
}
unsigned Window::GetWidth()
{
return mWidth;
}
unsigned Window::GetHeight()
{
return mHeight;
}
void Window::SetSize(unsigned width, unsigned height)
{
mWidth = width;
mHeight = height;
}
}

View file

@ -0,0 +1,50 @@
#pragma once
#include <memory>
#include <vector>
#include "PaintEvent.h"
#include "MouseEvent.h"
#include "VisualLayer.h"
#include "Widget.h"
namespace mt
{
class Window
{
private:
WidgetPtr mWidget;
unsigned mWidth;
unsigned mHeight;
std::vector<VisualLayerPtr> mLayers;
public:
Window();
~Window();
static std::shared_ptr<Window> Create();
void AddWidget(WidgetPtr widget);
WidgetPtr GetWidget();
std::vector<VisualLayerPtr> GetLayers();
unsigned GetWidth();
unsigned GetHeight();
void SetSize(unsigned width, unsigned height);
void OnPaint(PaintEventPtr event);
void OnMouseEvent(MouseEventPtr event);
};
}
using WindowPtr = std::shared_ptr<mt::Window>;

View file

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,39 @@
#include "KeyboardEvent.h"
KeyboardEvent::KeyboardEvent()
: UiEvent(),
mAction(),
mKeyString()
{
mType = UiEvent::Type::Keyboard;
}
KeyboardEvent::~KeyboardEvent()
{
}
std::shared_ptr<KeyboardEvent> KeyboardEvent::Create()
{
return std::make_shared<KeyboardEvent>();
}
void KeyboardEvent::SetKeyString(const std::string& key)
{
mKeyString = key;
}
std::string KeyboardEvent::GetKeyString()
{
return mKeyString;
}
void KeyboardEvent::SetAction(Action action)
{
mAction = action;
}
KeyboardEvent::Action KeyboardEvent::GetAction()
{
return mAction;
}

View file

@ -0,0 +1,39 @@
#pragma once
#include <memory>
#include <string>
#include "UiEvent.h"
class KeyboardEvent : public UiEvent
{
public:
enum class Action
{
Pressed,
Released
};
private:
Action mAction;
std::string mKeyString;
public:
KeyboardEvent();
~KeyboardEvent();
static std::shared_ptr<KeyboardEvent> Create();
void SetKeyString(const std::string& key);
std::string GetKeyString();
void SetAction(Action action);
Action GetAction();
};
using KeyboardEventPtr = std::shared_ptr<KeyboardEvent>;

View file

@ -0,0 +1,51 @@
#include "MouseEvent.h"
MouseEvent::MouseEvent()
: UiEvent(),
mClientLocation(0, 0),
mScreenLocation(0, 0),
mAction()
{
mType = UiEvent::Type::Mouse;
}
MouseEvent::~MouseEvent()
{
}
std::shared_ptr<MouseEvent> MouseEvent::Create()
{
return std::make_shared<MouseEvent>();
}
void MouseEvent::SetClientLocation(Pixel location)
{
mClientLocation = location;
}
void MouseEvent::SetScreenLocation(Pixel location)
{
mScreenLocation = location;
}
void MouseEvent::SetAction(MouseEvent::Action action)
{
mAction = action;
}
Pixel MouseEvent::GetClientLocation()
{
return mClientLocation;
}
Pixel MouseEvent::GetScreenLocation()
{
return mScreenLocation;
}
MouseEvent::Action MouseEvent::GetAction()
{
return mAction;
}

View file

@ -0,0 +1,42 @@
#pragma once
#include <memory>
#include "DiscretePoint.h"
#include "UiEvent.h"
class MouseEvent : public UiEvent
{
public:
enum class Action
{
Pressed,
Released
};
private:
Pixel mClientLocation;
Pixel mScreenLocation;
Action mAction;
public:
MouseEvent();
~MouseEvent();
static std::shared_ptr<MouseEvent> Create();
void SetClientLocation(Pixel location);
void SetScreenLocation(Pixel location);
void SetAction(Action action);
Pixel GetClientLocation();
Pixel GetScreenLocation();
Action GetAction();
};
using MouseEventPtr = std::shared_ptr<MouseEvent>;

View file

@ -0,0 +1,18 @@
#include "PaintEvent.h"
PaintEvent::PaintEvent()
: UiEvent()
{
mType = UiEvent::Type::Paint;
}
PaintEvent::~PaintEvent()
{
}
std::shared_ptr<PaintEvent> PaintEvent::Create()
{
return std::make_shared<PaintEvent>();
}

View file

@ -0,0 +1,17 @@
#pragma once
#include <memory>
#include "UiEvent.h"
class PaintEvent : public UiEvent
{
public:
PaintEvent();
~PaintEvent();
static std::shared_ptr<PaintEvent> Create();
};
using PaintEventPtr = std::shared_ptr<PaintEvent>;

View file

@ -0,0 +1,23 @@
#include "UiEvent.h"
UiEvent::UiEvent()
: mType(Type::Unknown)
{
}
UiEvent::~UiEvent()
{
}
std::shared_ptr<UiEvent> UiEvent::Create()
{
return std::make_shared<UiEvent>();
}
UiEvent::Type UiEvent::GetType()
{
return mType;
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <memory>
class UiEvent
{
public:
enum class Type{
Unknown,
Paint,
Mouse,
Keyboard
};
protected:
UiEvent::Type mType;
public:
UiEvent();
virtual ~UiEvent();
static std::shared_ptr<UiEvent> Create();
Type GetType();
};
using UiEventPtr = std::shared_ptr<UiEvent>;

View file

@ -0,0 +1,44 @@
#include "Button.h"
#include <iostream>
Button::Button()
: Widget(),
mLabel()
{
}
std::shared_ptr<Button> Button::Create()
{
return std::make_shared<Button>();
}
void Button::SetLabel(const std::string& text)
{
mLabel = text;
}
void Button::OnMyMouseEvent(MouseEventPtr event)
{
if(event->GetAction() == MouseEvent::Action::Pressed)
{
std::cout << "Clicked !!" << std::endl;
SetBackgroundColor(Color::Create(0, 255, 0));
}
}
void Button::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddBackground(event);
if(!mLabel.empty())
{
auto middle = DiscretePoint(mLocation.GetX() + mWidth/2,
mLocation.GetY() + mHeight/2);
auto text = TextElement::Create(mLabel, middle);
auto textLayer = VisualLayer::Create();
textLayer->SetText(text);
mLayers.push_back(textLayer);
}
}

View file

@ -0,0 +1,24 @@
#pragma once
#include "Widget.h"
class Button : public Widget
{
private:
std::string mLabel;
public:
Button();
void SetLabel(const std::string& text);
static std::shared_ptr<Button> Create();
void OnPaintEvent(PaintEventPtr event);
protected:
void OnMyMouseEvent(MouseEventPtr event);
};
using ButtonPtr = std::shared_ptr<Button>;

View file

@ -0,0 +1,33 @@
#include "HorizontalSpacer.h"
HorizontalSpacer::HorizontalSpacer()
: Widget()
{
}
std::shared_ptr<HorizontalSpacer> HorizontalSpacer::Create()
{
return std::make_shared<HorizontalSpacer>();
}
void HorizontalSpacer::AddChildLayers(PaintEventPtr event)
{
unsigned delta = mHeight / mChildren.size();
for(std::size_t idx=0; idx<mChildren.size(); idx++)
{
auto child = mChildren[idx];
child->SetSize(mWidth, delta);
child->SetLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + delta*idx));
child->OnPaintEvent(event);
auto layers = child->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
}
}
void HorizontalSpacer::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddChildLayers(event);
}

View file

@ -0,0 +1,18 @@
#pragma once
#include "Widget.h"
class HorizontalSpacer : public Widget
{
public:
HorizontalSpacer();
static std::shared_ptr<HorizontalSpacer> Create();
void AddChildLayers(PaintEventPtr event) override;
void OnPaintEvent(PaintEventPtr event) override;
};
using HorizontalSpacerPtr = std::shared_ptr<HorizontalSpacer>;

View file

@ -0,0 +1,35 @@
#include "Label.h"
#include "TextElement.h"
Label::Label()
: Widget(),
mLabel()
{
}
std::shared_ptr<Label> Label::Create()
{
return std::make_shared<Label>();
}
void Label::SetLabel(const std::string& text)
{
mLabel = text;
}
void Label::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddBackground(event);
if(!mLabel.empty())
{
auto middle = DiscretePoint(mLocation.GetX() + mWidth/2,
mLocation.GetY() + mHeight/2);
auto text = TextElement::Create(mLabel, middle);
auto textLayer = VisualLayer::Create();
textLayer->SetText(text);
mLayers.push_back(textLayer);
}
}

View file

@ -0,0 +1,22 @@
#pragma once
#include "Widget.h"
class Label : public Widget
{
private:
std::string mLabel;
public:
Label();
void SetLabel(const std::string& text);
static std::shared_ptr<Label> Create();
void OnPaintEvent(PaintEventPtr event);
};
using LabelPtr = std::shared_ptr<Label>;

View file

@ -0,0 +1,134 @@
#include "RectangleElement.h"
#include "Widget.h"
Widget::Widget()
: mLocation(DiscretePoint(0, 0)),
mWidth(100),
mHeight(100),
mLayers(),
mChildren(),
mBackgroundColor(Color::Create(200, 0, 0))
{
}
Widget::~Widget()
{
}
void Widget::AddWidget(WidgetPtr widget)
{
mChildren.push_back(widget);
}
void Widget::SetSize(unsigned width, unsigned height)
{
mWidth = width;
mHeight = height;
}
std::shared_ptr<Widget> Widget::Create()
{
return std::make_shared<Widget>();
}
DiscretePoint Widget::GetLocation()
{
return mLocation;
}
std::vector<VisualLayerPtr> Widget::GetLayers()
{
return mLayers;
}
void Widget::AddChildLayers(PaintEventPtr event)
{
for(std::size_t idx=0; idx<mChildren.size(); idx++)
{
auto child = mChildren[idx];
child->SetSize(mWidth, mHeight);
child->SetLocation(mLocation);
child->OnPaintEvent(event);
auto layers = child->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
}
}
void Widget::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddBackground(event);
AddChildLayers(event);
}
bool Widget::Contains(const DiscretePoint& loc)
{
if(loc.GetX() < mLocation.GetX()) return false;
if(loc.GetX() > mLocation.GetX() + mWidth) return false;
if(loc.GetY() > mLocation.GetY() + mHeight) return false;
if(loc.GetY() < mLocation.GetY()) return false;
return true;
}
bool Widget::OnMouseEvent(MouseEventPtr event)
{
bool inChild = false;
for(const auto& child : mChildren)
{
if(child->OnMouseEvent(event))
{
inChild = true;
break;
}
}
if(!inChild)
{
if(Contains(event->GetClientLocation()))
{
OnMyMouseEvent(event);
return true;
}
else
{
return false;
}
}
return true;
}
void Widget::OnMyMouseEvent(MouseEventPtr event)
{
}
void Widget::AddBackground(PaintEventPtr event)
{
auto shape = RectangleElement::Create(mLocation, mWidth, mHeight);
shape->SetFillColor(mBackgroundColor);
auto shapeLayer = VisualLayer::Create();
shapeLayer->SetShape(shape);
mLayers.push_back(shapeLayer);
}
void Widget::SetBackgroundColor(ColorPtr color)
{
mBackgroundColor = color;
}
void Widget::SetLocation(const DiscretePoint& loc)
{
mLocation = loc;
}
unsigned Widget::GetWidth()
{
return mWidth;
}
unsigned Widget::GetHeight()
{
return mHeight;
}

View file

@ -0,0 +1,62 @@
#pragma once
#include <memory>
#include <vector>
#include "DiscretePoint.h"
#include "VisualLayer.h"
#include "PaintEvent.h"
#include "Color.h"
#include "MouseEvent.h"
class Widget
{
protected:
DiscretePoint mLocation;
unsigned mWidth;
unsigned mHeight;
std::vector<VisualLayerPtr> mLayers;
std::vector<std::shared_ptr<Widget> > mChildren;
ColorPtr mBackgroundColor;
public:
Widget();
virtual ~Widget();
void AddWidget(std::shared_ptr<Widget> widget);
void SetBackgroundColor(ColorPtr color);
void SetSize(unsigned mWidth, unsigned mHeight);
DiscretePoint GetLocation();
void SetLocation(const DiscretePoint& loc);
unsigned GetWidth();
unsigned GetHeight();
std::vector<VisualLayerPtr> GetLayers();
static std::shared_ptr<Widget> Create();
virtual void OnPaintEvent(PaintEventPtr event);
virtual bool OnMouseEvent(MouseEventPtr event);
bool Contains(const DiscretePoint& loc);
protected:
virtual void OnMyMouseEvent(MouseEventPtr event);
virtual void AddChildLayers(PaintEventPtr event);
virtual void AddBackground(PaintEventPtr event);
};
using WidgetPtr = std::shared_ptr<Widget>;

View file

@ -0,0 +1,36 @@
#include "GeometryElement.h"
GeometryElement::GeometryElement()
: mFillColor(Color::Create(255, 255, 255)),
mStrokeColor(Color::Create(0, 0, 0)),
mStrokeThickness(1),
mType(Type::Path)
{
}
ColorPtr GeometryElement::GetFillColor()
{
return mFillColor;
}
ColorPtr GeometryElement::GetStrokeColor()
{
return mStrokeColor;
}
unsigned GeometryElement::GetStrokeThickness()
{
return mStrokeThickness;
}
void GeometryElement::SetFillColor(ColorPtr color)
{
mFillColor = color;
}
void GeometryElement::SetStrokeColor(ColorPtr color)
{
mStrokeColor = color;
}
void GeometryElement::SetStrokeThickness(unsigned thickness)
{
mStrokeThickness = thickness;
}

View file

@ -0,0 +1,39 @@
#pragma once
#include "Color.h"
class GeometryElement
{
public:
enum class Type
{
Path,
Rectangle,
Circle,
Arc
};
private:
ColorPtr mFillColor;
ColorPtr mStrokeColor;
unsigned mStrokeThickness;
Type mType;
public:
GeometryElement();
virtual ~GeometryElement() = default;
ColorPtr GetFillColor();
ColorPtr GetStrokeColor();
unsigned GetStrokeThickness();
virtual Type GetType() = 0;
void SetFillColor(ColorPtr color);
void SetStrokeColor(ColorPtr color);
void SetStrokeThickness(unsigned thickness);
};
using GeometryElementPtr = std::shared_ptr<GeometryElement>;

View file

@ -0,0 +1,36 @@
#include "RectangleElement.h"
RectangleElement::RectangleElement(const DiscretePoint& loc,
unsigned width, unsigned height)
: mLocation(loc),
mWidth(width),
mHeight(height)
{
}
std::shared_ptr<RectangleElement> RectangleElement::Create(const DiscretePoint& loc,
unsigned width, unsigned height)
{
return std::make_shared<RectangleElement>(loc, width, height);
}
GeometryElement::Type RectangleElement::GetType()
{
return GeometryElement::Type::Rectangle;
}
DiscretePoint RectangleElement::GetLocation()
{
return mLocation;
}
unsigned RectangleElement::GetWidth()
{
return mWidth;
}
unsigned RectangleElement::GetHeight()
{
return mHeight;
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <memory>
#include "GeometryElement.h"
#include "DiscretePoint.h"
class RectangleElement : public GeometryElement
{
DiscretePoint mLocation;
unsigned mWidth;
unsigned mHeight;
public:
RectangleElement(const DiscretePoint& loc,
unsigned width, unsigned height);
static std::shared_ptr<RectangleElement> Create(const DiscretePoint& loc,
unsigned width, unsigned height);
GeometryElement::Type GetType() override;
DiscretePoint GetLocation();
unsigned GetWidth();
unsigned GetHeight();
};
using RectangleElementPtr = std::shared_ptr<RectangleElement>;

View file

@ -0,0 +1,39 @@
#include "TextElement.h"
TextElement::TextElement(const std::string& content, const DiscretePoint& loc)
: mContent(content),
mLocation(loc),
mFontLabel("fixed")
{
}
TextElement::~TextElement()
{
}
std::shared_ptr<TextElement> TextElement::Create(const std::string& content, const DiscretePoint& loc)
{
return std::make_shared<TextElement>(content, loc);
}
DiscretePoint TextElement::GetLocation()
{
return mLocation;
}
void TextElement::SetContent(const std::string& content)
{
mContent = content;
}
std::string TextElement::GetFontLabel()
{
return mFontLabel;
}
std::string TextElement::GetContent()
{
return mContent;
}

View file

@ -0,0 +1,31 @@
#pragma once
#include <memory>
#include <string>
#include "DiscretePoint.h"
class TextElement
{
std::string mContent;
DiscretePoint mLocation;
std::string mFontLabel;
public:
TextElement(const std::string& content, const DiscretePoint& loc);
~TextElement();
static std::shared_ptr<TextElement> Create(const std::string& content, const DiscretePoint& loc);
DiscretePoint GetLocation();
std::string GetContent();
void SetContent(const std::string& content);
std::string GetFontLabel();
};
using TextElementPtr = std::shared_ptr<TextElement>;

View file

@ -0,0 +1,38 @@
#include "VisualLayer.h"
VisualLayer::VisualLayer()
: mShape(),
mText()
{
}
std::shared_ptr<VisualLayer> VisualLayer::Create()
{
return std::make_shared<VisualLayer>();
}
void VisualLayer::SetShape(GeometryElementPtr shape)
{
mShape = shape;
}
void VisualLayer::SetText(TextElementPtr text)
{
mText = text;
}
bool VisualLayer::HasShape()
{
return bool(mShape);
}
bool VisualLayer::HasText()
{
return bool(mText);
}
GeometryElementPtr VisualLayer::GetShape()
{
return mShape;
}
TextElementPtr VisualLayer::GetText()
{
return mText;
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <memory>
#include "GeometryElement.h"
#include "TextElement.h"
class VisualLayer
{
GeometryElementPtr mShape;
TextElementPtr mText;
public:
VisualLayer();
static std::shared_ptr<VisualLayer> Create();
void SetShape(GeometryElementPtr shape);
void SetText(TextElementPtr text);
bool HasShape();
bool HasText();
GeometryElementPtr GetShape();
TextElementPtr GetText();
};
using VisualLayerPtr = std::shared_ptr<VisualLayer>;

19
src/web/CMakeLists.txt Normal file
View file

@ -0,0 +1,19 @@
list(APPEND web_LIB_INCLUDES
xml/XmlParser.cpp
xml/XmlDocument.cpp
xml/XmlElement.cpp
xml/XmlAttribute.cpp
xml/XmlProlog.cpp
markdown/MarkdownParser.cpp
html/HtmlWriter.cpp
html/HtmlDocument.cpp)
# add the executable
add_library(web SHARED ${web_LIB_INCLUDES})
target_include_directories(web PUBLIC
"${PROJECT_SOURCE_DIR}/src/core/"
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/xml"
"${CMAKE_CURRENT_SOURCE_DIR}/html"
)

View file

@ -0,0 +1,12 @@
#include "HtmlDocument.h"
HtmlDocument::HtmlDocument()
: XmlDocument()
{
}
std::shared_ptr<HtmlDocument> HtmlDocument::Create()
{
return std::make_shared<HtmlDocument>();
}

View file

@ -0,0 +1,14 @@
#pragma once
#include <memory>
#include "XmlDocument.h"
class HtmlDocument : public XmlDocument
{
public:
HtmlDocument();
static std::shared_ptr<HtmlDocument> Create();
};
using HtmlDocumentPtr = std::shared_ptr<HtmlDocument>;

View file

View file

View file

@ -0,0 +1,11 @@
#include "HtmlElement.h"
HtmlElement::HtmlElement()
{
}
static std::shared_ptr<HtmlElement> HtmlElement::Create()
{
return std::make_shared<HtmlElement>();
}

View file

@ -0,0 +1,12 @@
#pragma once
#include <memory>
#include "XmlElement.h"
class HtmlElement : public XmlElement
{
HtmlElement();
static std::shared_ptr<HtmlElement> Create();
};
using HtmlElementPtr = std::shared_ptr<HtmlElement>;

View file

@ -0,0 +1,11 @@
#include "HtmlWriter.h"
HtmlWriter::HtmlWriter()
{
}
std::string HtmlWriter::ToString(HtmlDocumentPtr document)
{
return "<html>Uh oh!!!!</html>";
}

12
src/web/html/HtmlWriter.h Normal file
View file

@ -0,0 +1,12 @@
#pragma once
#include "HtmlDocument.h"
class HtmlWriter
{
public:
HtmlWriter();
std::string ToString(HtmlDocumentPtr document);
};

Some files were not shown because too many files have changed in this diff Show more