Skip to content

Commit

Permalink
Merge branch 'main' into gabriel_dev
Browse files Browse the repository at this point in the history
  • Loading branch information
paxo-rch authored May 21, 2024
2 parents 527f95f + d29c64e commit 5f966bd
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 10 deletions.
25 changes: 25 additions & 0 deletions INSTRUCTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,34 @@ pio test -e test
Reason: no LC_RPATH's found
zsh: abort .pio/build/macos/program
```
Try to do ```DYLD_LIBRARY_PATH="`brew --prefix sdl2`/lib" .pio/build/macos/program``` and if it still doesn't work and you haven't installed SDL2 locally as decribed in the next bullet point (otherwise run ```DYLD_LIBRARY_PATH="`eval echo ~$USER`/sdl2/lib" .pio/build/macos/program```) do:
1. Get the location of SDL's dynamic library by executing `brew info sdl2`, you should get a path similar to `/opt/homebrew/Cellar/sdl2/2.28.5`
2. Run `ls {the path you got from the last command}/lib/libSDL2-2.0.0.dylib`. If you get the exact same path as an output you're ready to go. Otherwise install SDL as described above in this document and retry the procedure.
3. Re-run the command by adding `DYLD_LIBRARY_PATH="{the path you got from step 1}/lib/"` as a prefix before the command like this `DYLD_LIBRARY_PATH="{the path you got from step 1}/lib/ .pio/build/macos/program`
- When compiling the project using pio you might encounter an error where your shell can't find `pio` even if you installed it via the quick-setup. To fix that, execute `pio` directly from the binaries path that is located at `/Users/username/.platformio/penv/bin/pio`. If you have PaxOS9 installed directly in your home (i.e. `/Users/username/PaxOS9`), run pio via `../.platformio/penv/bin/pio`. To build for macOS it would give something similar this `../.platformio/penv/bin/pio run -e macos`.
- If you get the `unknown platform` error when compiling, that probalby means that SDL2 is not installed where it should or even just not installed at all. If you have admin permissions for your machine run `brew install sdl2`. If you don't, then install SDL2 locally by following those steps:
1. Execute the following command (don't worry it won't hack into your computer)
```
cd `eval echo ~$USER` && rm -rf sdl2-build && rm -rf sdl2 && mkdir sdl2 && git clone https://github.com/libsdl-org/SDL.git -b SDL2 sdl2-build && cd sdl2-build && mkdir build && cd build && ../configure --prefix=`eval echo ~$USER`/sdl2 && make -j`sysctl -n hw.ncpu` && make install && cd `eval echo ~$USER` && rm -rf sdl2-build && echo "Installed SDL at `eval echo ~$USER`/sdl2" && echo "\033[0;32mThe two lines to add to the macos target (\033[1;34mbuild_flags\033[0;32m) of the \033[1;34mplatform.ini\033[0;32m in your PaxOS9 directory are \033[1;33m-I`eval echo ~$USER`/sdl2/include\033[0;32m and \033[1;33m-L`eval echo ~$USER`/sdl2/lib\033[0;32m"
```
2. Follow the instructions that the command gave you (add the two lines). The place where you should put the lines looks like this (the filename is called `platform.ini` and is present in the `PaxOS9` directory):
```
[env:macos]
platform = native
lib_deps =
x
x
x
test_framework = googletest
build_flags =
x
x
x
x
-I/opt/homebrew/include/SDL2
-L/opt/homebrew/lib
-->PLACE THE LINES SEPARATED BY A NEW LINE HERE <--
```
# CLion Instructions
Expand Down
38 changes: 32 additions & 6 deletions lib/graphics/src/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ namespace graphics
return (float) scale * m_sprite.fontHeight(getFont(m_font, fontSize));
}

void Surface::drawText(std::string &otext, const int16_t x, const int16_t y)
void Surface::drawText(std::string &otext, int16_t x, int16_t y, std::optional<color_t> color)
{
std::string text = decodeString(otext);
// Get the correct font size
Expand Down Expand Up @@ -329,7 +329,14 @@ namespace graphics

// Init the buffer
buffer->m_sprite.setFont(getFont(m_font, fontSize));
buffer->m_sprite.setTextColor(m_text_color);
if (color.has_value())
{
buffer->m_sprite.setTextColor(color.value());
}
else
{
buffer->m_sprite.setTextColor(m_text_color);
}

// Set the background as transparent
// FIXME : Using "m_color" as the transparency color can cause issues
Expand Down Expand Up @@ -364,18 +371,37 @@ namespace graphics
delete buffer;
}

void Surface::drawTextCentered(std::string &text, const int16_t x, const int16_t y, const uint16_t w)
void Surface::drawTextCentered(std::string &text, const int16_t x, const int16_t y, const uint16_t w, const uint16_t h, const bool horizontallyCentered, const bool verticallyCentered, const std::optional<color_t> color)
{
const uint16_t textWidth = m_sprite.textWidth(text.c_str());
int16_t textPositionX;
if (horizontallyCentered)
{
if (w == (uint16_t)-1)
textPositionX = x + (double)this->getWidth() * 0.5 - (double)textWidth * 0.5;
else
textPositionX = x + (double)w * 0.5 - (double)textWidth * 0.5;
}
else
{
textPositionX = x;
}

if (w == -1)
const uint16_t textHeight = this->getTextHeight(); // maybe it should take in account the bounding box height
int16_t textPositionY;
if (verticallyCentered)
{
drawText(text, static_cast<int16_t>(x + getScreenWidth() * 0.5 - textWidth * 0.5), y);
if(h == (uint16_t)-1)
textPositionY = y + (double)this->getHeight() * 0.5 - (double)textHeight * 0.5;
else
textPositionY = y + (double)h * 0.5 - (double)textHeight * 0.5;
}
else
{
drawText(text, static_cast<int16_t>(x + w * 0.5 - textWidth * 0.5), y);
textPositionY = y;
}

drawText(text, textPositionX, textPositionY, color);
}

void Surface::blur(uint8_t radius)
Expand Down
7 changes: 5 additions & 2 deletions lib/graphics/src/Surface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Image.hpp"
#include "fonts.hpp"
#include "color.hpp"
#include <optional>

namespace graphics
{
Expand Down Expand Up @@ -73,8 +74,10 @@ namespace graphics
void setTextColor(const color_t textColor);
uint16_t getTextWidth(std::string text);
uint16_t getTextHeight() const;
void drawText(std::string &text, int16_t x, int16_t y);
void drawTextCentered(std::string &text, int16_t x, int16_t y, uint16_t w = -1);
void drawText(std::string &otext, int16_t x, int16_t y, std::optional<color_t> color = std::nullopt);

// w and h are the width and height of the bounding box where the text will be centered
void drawTextCentered(std::string &text, const int16_t x, const int16_t y, const uint16_t w = -1, const uint16_t h = -1, const bool horizontallyCentered = true, const bool verticallyCentered = true, const std::optional<color_t> color = std::nullopt);

void blur(uint8_t radius);
};
Expand Down
20 changes: 20 additions & 0 deletions lib/gui/src/elements/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,25 @@ namespace gui::elements
this->localGraphicalUpdate();
}

void Canvas::drawText(int16_t x, int16_t y, std::string& text, color_t color)
{
StandbyMode::triggerPower();
this->getAndSetSurface()->drawText(text, x, y, color);
this->localGraphicalUpdate();
}

// w and h are the width and height of the text bounding box
void Canvas::drawTextCentered(int16_t x, int16_t y, std::string& text, color_t color, bool horizontallyCentered, bool verticallyCentered)
{
this->drawTextCenteredInRect(x, y, -1, -1, text, color, horizontallyCentered, verticallyCentered);
}

void Canvas::drawTextCenteredInRect(int16_t x, int16_t y, uint16_t w, uint16_t h, std::string& text, color_t color, bool horizontallyCentered, bool verticallyCentered)
{
StandbyMode::triggerPower();
this->getAndSetSurface()->drawTextCentered(text, x, y, w, h, horizontallyCentered, verticallyCentered, color);
this->localGraphicalUpdate();
}

Canvas::~Canvas() = default;
}
4 changes: 4 additions & 0 deletions lib/gui/src/elements/Canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace gui::elements
void fillPolygon(std::vector<std::pair<int16_t, int16_t>> vertices, color_t color);

void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, color_t color);

void drawText(int16_t x, int16_t y, std::string& text, color_t color);
void drawTextCentered(int16_t x, int16_t y, std::string& text, color_t color, bool horizontallyCentered = true, bool verticallyCentered = true);
void drawTextCenteredInRect(int16_t x, int16_t y, uint16_t w, uint16_t h, std::string& text, color_t color, bool horizontallyCentered = true, bool verticallyCentered = true);
};
} // gui::elements

Expand Down
6 changes: 6 additions & 0 deletions lib/lua/src/lua_canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class LuaCanvas : public LuaWidget

void drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, color_t color) { widget->drawLine(x1, y1, x2, y2, color); }

void drawText(int16_t x, int16_t y, std::string text, color_t color) { widget->drawText(x, y, text, color); }

// w and h are the width and height of the text bounding box
void drawTextCentered(int16_t x, int16_t y, std::string text, color_t color, bool horizontallyCentered, bool verticallyCentered) { widget->drawTextCentered(x, y, text, color, horizontallyCentered, verticallyCentered); }
void drawTextCenteredInRect(int16_t x, int16_t y, uint16_t w, uint16_t h, std::string text, color_t color, bool horizontallyCentered, bool verticallyCentered) { widget->drawTextCenteredInRect(x, y, w, h, text, color, horizontallyCentered, verticallyCentered); }

void onTouch(sol::function function) { this->onTouchFunc = function; }

sol::table getTouch();
Expand Down
3 changes: 3 additions & 0 deletions lib/lua/src/lua_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ void LuaFile::run(std::vector<std::string> arg)
"drawPolygon", &LuaCanvas::drawPolygon,
"fillPolygon", &LuaCanvas::fillPolygon,
"drawLine", &LuaCanvas::drawLine,
"drawText", &LuaCanvas::drawText,
"drawTextCentered", &LuaCanvas::drawTextCentered,
"drawTextCenteredInBounds", &LuaCanvas::drawTextCenteredInRect,
"getTouch", &LuaCanvas::getTouch,
"onTouch", &LuaCanvas::onTouch,
sol::base_classes, sol::bases<LuaWidget>());
Expand Down
3 changes: 2 additions & 1 deletion lib/lua/src/lua_gsm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GSM_LUA_MODULE

#include <string>
#include <cstdint>

namespace LuaGSM
{
Expand All @@ -14,4 +15,4 @@ namespace LuaGSM
uint8_t getCallState();
};

#endif
#endif
2 changes: 1 addition & 1 deletion storage/apps/snake/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function drawSnake()
end

function drawFood()
drawRect_canvas:fillRect(int(food.x*factor), int(food.y*factor), 15, 15, COLOR_WARNING)
drawRect_canvas:fillRect(int(food.x*factor), int(food.y*factor), 15, 15, COLOR_ERROR)
end

function updateSnake()
Expand Down
10 changes: 10 additions & 0 deletions storage/apps/test/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,15 @@ function run()
fillConvexPolygon_canvas = gui:canvas(win, 245, 215, 25, 25)
fillConvexPolygon_canvas:fillPolygon({{4, 4}, {4, 18}, {18, 18}}, COLOR_WARNING)

drawText_canvas = gui:canvas(win, 275, 215, 25, 25)
drawText_canvas:drawText(0, 0, "E", COLOR_WARNING)

drawTextCentered_canvas = gui:canvas(win, 5, 245, 25, 25)
drawTextCentered_canvas:drawTextCentered(0, 0, "E", COLOR_WARNING, true, true)

drawTextCenteredInBounds_canvas = gui:canvas(win, 35, 245, 50, 25)
drawTextCenteredInBounds_canvas:drawRect(0, 0, 40, 20, COLOR_WARNING)
drawTextCenteredInBounds_canvas:drawTextCenteredInBounds(0, 0, 40, 20, "E", COLOR_WARNING, true, true)

gui:setWindow(win)
end

0 comments on commit 5f966bd

Please sign in to comment.