Skip to content

Commit

Permalink
Merge pull request #105 from paxo-phone/gabriel_dev
Browse files Browse the repository at this point in the history
Gabriel dev
  • Loading branch information
paxo-rch authored Sep 5, 2024
2 parents c1a992b + b4cd394 commit 55124f4
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/applications/src/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <gui.hpp>
#include <GuiManager.hpp>
#include <standby.hpp>
#include <threads.hpp>


/**
Expand Down Expand Up @@ -139,6 +140,9 @@ void applications::launcher::update() {
launcherWindow->updateAll();
}

// Update all events
eventHandlerApp.update();

// Check touch events

if (brightnessSliderBox->isFocused(true)) {
Expand Down
25 changes: 25 additions & 0 deletions lib/gui/src/elements/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace gui::elements
m_lineSpace = 25;
m_verticalScrollEnabled = true;
m_hasEvents = true;
m_selectionFocus == SelectionFocus::UP;
}

VerticalList::~VerticalList() = default;
Expand All @@ -22,6 +23,14 @@ namespace gui::elements
m_surface->fillRect(0, 0, m_width, m_height, COLOR_WHITE);
}

void VerticalList::postRender()
{
if(m_selectionFocus == SelectionFocus::CENTER && m_children.size())
{
m_surface->fillRect(0, getHeight()/2 - m_children[m_focusedIndex]->getHeight()/2, 1, m_children[m_focusedIndex]->getHeight(), COLOR_BLACK);
}
}

void VerticalList::add(ElementBase* widget)
{
m_verticalScrollEnabled = true;
Expand All @@ -46,14 +55,19 @@ namespace gui::elements
void VerticalList::updateFocusedIndex()
{
eventHandlerApp.setTimeout(new Callback<>([&](){
std::cout << "updateFocusedIndex" << std::endl;
if(m_children.size() == 0)
{
m_focusedIndex = 0;
return;
}

m_verticalScroll = m_children[m_focusedIndex]->m_y;
if(m_selectionFocus == SelectionFocus::CENTER)
m_verticalScroll = m_verticalScroll - getHeight() / 2 + m_children[m_focusedIndex]->getHeight() / 2;

localGraphicalUpdate();
std::cout << "updateFocusedIndex end: " << m_selectionFocus << std::endl;

// for (int i = 0; i < m_children.size(); i++)
// {
Expand Down Expand Up @@ -82,6 +96,17 @@ namespace gui::elements
updateFocusedIndex();
}
}

void VerticalList::setSelectionFocus(SelectionFocus focus)
{
m_selectionFocus = focus;
updateFocusedIndex();
}

int VerticalList::getFocusedElement()
{
return m_focusedIndex;
}


HorizontalList::HorizontalList(uint16_t x, uint16_t y, uint16_t width, uint16_t height)
Expand Down
11 changes: 11 additions & 0 deletions lib/gui/src/elements/List.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace gui::elements
~VerticalList() override;

void render() override;
void postRender();

void add(ElementBase* widget);
void setIndex(int index);
Expand All @@ -22,9 +23,19 @@ namespace gui::elements
void onScrollUp();
void onScrollDown();

enum SelectionFocus
{
UP,
CENTER
};

void setSelectionFocus(SelectionFocus focus);
int getFocusedElement();

private:
int16_t m_focusedIndex = 0;
uint16_t m_lineSpace = 0;
SelectionFocus m_selectionFocus = SelectionFocus::UP;
};

class HorizontalList final : public ElementBase
Expand Down
4 changes: 2 additions & 2 deletions lib/lua/src/lua_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ sol::table LuaCanvas::getTouch()
int16_t x = gui::ElementBase::touchX, y = gui::ElementBase::touchY;

sol::table result = lua->lua.create_table();
result.set(1, x);
result.set(2, y);
result.set(1, x - widget->getAbsoluteX());
result.set(2, y - widget->getAbsoluteY());

return result;
}
Expand Down
173 changes: 173 additions & 0 deletions lib/lua/src/lua_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include <libsystem.hpp>
#include <standby.hpp>

#include <fstream>
#include <iostream>


/*
LuaHttpClient::LuaHttpClient(LuaFile* lua)
Expand Down Expand Up @@ -112,6 +115,150 @@ int custom_panic_handler(lua_State* L) {
return 0;
}

template <typename T>
void writeBinaryValue(std::ofstream& file, const T& value) {
file.write(reinterpret_cast<const char*>(&value), sizeof(T));
}

void saveTableToBinaryFile(std::ofstream& file, const sol::table& table) {
// Write the number of key-value pairs
size_t numPairs = table.size();
writeBinaryValue(file, numPairs);

for (const auto& pair : table) {
sol::object key = pair.first;
sol::object value = pair.second;

// Write the key type
if (key.is<std::string>()) {
writeBinaryValue(file, static_cast<uint8_t>(1)); // 1 for string key
std::string keyStr = key.as<std::string>();
size_t keySize = keyStr.size();
writeBinaryValue(file, keySize);
file.write(keyStr.c_str(), keySize);
} else if (key.is<double>() || key.is<int>()) {
writeBinaryValue(file, static_cast<uint8_t>(2)); // 2 for numeric key
double numericKey = key.as<double>();
writeBinaryValue(file, numericKey);
} else {
throw std::runtime_error("Unsupported table key type for binary serialization");
}

// Write the value type and value
if (value.is<std::string>()) {
writeBinaryValue(file, static_cast<uint8_t>(1)); // 1 for string
std::string valueStr = value.as<std::string>();
size_t valueSize = valueStr.size();
writeBinaryValue(file, valueSize);
file.write(valueStr.c_str(), valueSize);
} else if (value.is<double>() || value.is<int>()) {
writeBinaryValue(file, static_cast<uint8_t>(2)); // 2 for number
double numericValue = value.as<double>();
writeBinaryValue(file, numericValue);
} else if (value.is<bool>()) {
writeBinaryValue(file, static_cast<uint8_t>(3)); // 3 for boolean
bool boolValue = value.as<bool>();
writeBinaryValue(file, boolValue);
} else if (value.is<sol::nil_t>()) {
writeBinaryValue(file, static_cast<uint8_t>(4)); // 4 for nil
} else if (value.is<sol::table>()) {
writeBinaryValue(file, static_cast<uint8_t>(5)); // 5 for table
saveTableToBinaryFile(file, value.as<sol::table>()); // Recursively save
} else {
throw std::runtime_error("Unsupported table value type for binary serialization");
}
}
}

void saveTableToBinaryFile(const std::string& filename, const sol::table& table) {
std::ofstream file(filename, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Error opening file for writing: " + filename);
}

try {
saveTableToBinaryFile(file, table);
} catch (const std::exception& e) {
file.close();
throw std::runtime_error(std::string("Error while writing to file: ") + e.what());
}

file.close();
}

template <typename T>
T readBinaryValue(std::ifstream& file) {
T value;
file.read(reinterpret_cast<char*>(&value), sizeof(T));
return value;
}

sol::table loadTableFromBinaryFile(sol::state& lua, std::ifstream& file) {
sol::table table = lua.create_table();

size_t numPairs = readBinaryValue<size_t>(file);

for (size_t i = 0; i < numPairs; ++i) {
// Read key
uint8_t keyType = readBinaryValue<uint8_t>(file);
sol::object key;

if (keyType == 1) { // String key
size_t keySize = readBinaryValue<size_t>(file);
std::vector<char> keyBuffer(keySize);
file.read(keyBuffer.data(), keySize);
key = sol::make_object(lua, std::string(keyBuffer.data(), keySize));
} else if (keyType == 2) { // Numeric key
key = sol::make_object(lua, readBinaryValue<double>(file));
} else {
throw std::runtime_error("Unsupported key type in binary file");
}

// Read value
uint8_t valueType = readBinaryValue<uint8_t>(file);

switch (valueType) {
case 1: { // String
size_t valueSize = readBinaryValue<size_t>(file);
std::vector<char> valueBuffer(valueSize);
file.read(valueBuffer.data(), valueSize);
table[key] = std::string(valueBuffer.data(), valueSize);
break;
}
case 2: // Number
table[key] = readBinaryValue<double>(file);
break;
case 3: // Boolean
table[key] = readBinaryValue<bool>(file);
break;
case 4: // Nil
table[key] = sol::nil;
break;
case 5: // Nested table
table[key] = loadTableFromBinaryFile(lua, file);
break;
default:
throw std::runtime_error("Unsupported value type in binary file");
}
}

return table;
}

sol::table loadTableFromBinaryFile(sol::state& lua, const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Error opening file for reading: " + filename);
}

try {
return loadTableFromBinaryFile(lua, file);
} catch (const std::exception& e) {
file.close();
throw std::runtime_error(std::string("Error while reading from file: ") + e.what());
}
}

void LuaFile::load()
{
StandbyMode::triggerPower();
Expand Down Expand Up @@ -155,6 +302,30 @@ void LuaFile::load()
lua.set_function("nonothing", []() {
});

lua["require"] = [&](const std::string& filename) -> sol::object {
storage::Path lib(filename);

// Load the file
sol::load_result chunk = lua.load_file(this->lua_storage.convertPath(lib).str());
if (!chunk.valid()) {
sol::error err = chunk;
throw std::runtime_error("Error loading module '" + filename + "': " + err.what());
}

// 4. Execute the loaded chunk and return its results
return chunk();
};

lua["saveTable"] = [&](const std::string& filename, const sol::table& table)
{
saveTableToBinaryFile(lua_storage.convertPath(filename).str(), table);
};

lua["loadTable"] = [&](const std::string& filename)
{
return loadTableFromBinaryFile(lua, lua_storage.convertPath(filename).str());
};

if (perms.acces_hardware) // si hardware est autorisé
{
lua.new_usertype<LuaHardware>("hardware",
Expand Down Expand Up @@ -351,6 +522,8 @@ void LuaFile::load()
lua.new_usertype<LuaVerticalList>("LuaVList",
"setIndex", &LuaVerticalList::setIndex,
"setSpaceLine", &LuaVerticalList::setSpaceLine,
"setSelectionFocus", &LuaVerticalList::setFocus,
"getSelected", &LuaVerticalList::getSelected,
sol::base_classes, sol::bases<LuaWidget>());

lua.new_usertype<LuaHorizontalList>("LuaHList",
Expand Down
2 changes: 2 additions & 0 deletions lib/lua/src/lua_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class LuaVerticalList : public LuaWidget
void addChild(LuaWidget* widget){ this->children.push_back(widget); widget->parent = this; this->widget->add(widget->widget); }
void setSpaceLine(int line){ this->widget->setSpaceLine(line); }
void setIndex(int i = 0) { this->widget->setIndex(i); };
void setFocus(VerticalList::SelectionFocus focus) { std::cout << "setFocus: " << focus << std::endl; this->widget->setSelectionFocus(focus); };
int getSelected() { return this->widget->getFocusedElement(); };

VerticalList* widget = nullptr;
};
Expand Down

0 comments on commit 55124f4

Please sign in to comment.