From ff54918502e7d5ca54d9c8e220795b187c35207f Mon Sep 17 00:00:00 2001 From: cartoone222 <69402484+cartoone222@users.noreply.github.com> Date: Sun, 9 Jun 2024 09:01:17 +0200 Subject: [PATCH] [python] add bgcolor to turtule module (#367) * add bgcolor to turtul module * add bgcolor to toolbox and a minor fix * ok is ok now i hope however * Submodule Omega-Kawaii-Theme remove --- apps/code/catalog.de.i18n | 1 + apps/code/catalog.en.i18n | 1 + apps/code/catalog.es.i18n | 1 + apps/code/catalog.fr.i18n | 1 + apps/code/catalog.hu.i18n | 1 + apps/code/catalog.it.i18n | 1 + apps/code/catalog.nl.i18n | 1 + apps/code/catalog.pt.i18n | 1 + apps/code/catalog.universal.i18n | 1 + apps/code/python_toolbox.cpp | 2 ++ python/port/genhdr/qstrdefs.in.h | 1 + python/port/mod/turtle/modturtle.cpp | 33 ++++++++++++++++++++++++ python/port/mod/turtle/modturtle.h | 1 + python/port/mod/turtle/modturtle_table.c | 2 ++ python/port/mod/turtle/turtle.cpp | 17 +++++++++++- python/port/mod/turtle/turtle.h | 12 +++++++++ 16 files changed, 76 insertions(+), 1 deletion(-) diff --git a/apps/code/catalog.de.i18n b/apps/code/catalog.de.i18n index b5a22857c9e..1cac8628fdb 100644 --- a/apps/code/catalog.de.i18n +++ b/apps/code/catalog.de.i18n @@ -262,3 +262,4 @@ PythonFileReadable = "Kann Datei gelesen werden?" PythonFileWritable = "Kann Datei geschrieben werden?" PythonImportUtils = "Importieren von ulab.utils" PythonUtilsFunction = "Funktionspräfix des utils-Moduls" +PythonTurtleBgcolor = "Ändern Sie die Hintergrundfarbe" diff --git a/apps/code/catalog.en.i18n b/apps/code/catalog.en.i18n index dd3936409d8..00bc6311e9b 100644 --- a/apps/code/catalog.en.i18n +++ b/apps/code/catalog.en.i18n @@ -262,3 +262,4 @@ PythonFileReadable = "Tells if read can be used on a file" PythonFileWritable = "Tells if write can be used on a file" PythonImportUtils = "Importing ulab.utils" PythonUtilsFunction = "utils module function prefix" +PythonTurtleBgcolor = "Change the background color" diff --git a/apps/code/catalog.es.i18n b/apps/code/catalog.es.i18n index 62d5bf2c991..80145d28bb5 100644 --- a/apps/code/catalog.es.i18n +++ b/apps/code/catalog.es.i18n @@ -262,3 +262,4 @@ PythonFileReadable = "Tells if read can be used on a file" PythonFileWritable = "Tells if write can be used on a file" PythonImportUtils = "Importando ulab.utils" PythonUtilsFunction = "prefijo de función del módulo utils" +PythonTurtleBgcolor = "Cambiar el color de fondo" diff --git a/apps/code/catalog.fr.i18n b/apps/code/catalog.fr.i18n index 3185c5ce5a8..29d2620bd23 100644 --- a/apps/code/catalog.fr.i18n +++ b/apps/code/catalog.fr.i18n @@ -212,6 +212,7 @@ PythonTurtleBackward = "Recule de x pixels" PythonTurtleCircle = "Cercle de rayon r pixels" PythonTurtleColor = "Modifie la couleur du tracé" PythonTurtleColorMode = "Met le mode de couleur à 1.0 ou 255" +PythonTurtleBgcolor = "Modifie la couleur du fond" PythonTurtleForward = "Avance de x pixels" PythonTurtleFunction = "Préfixe fonction du module turtle" PythonTurtleGoto = "Va au point de coordonnées (x,y)" diff --git a/apps/code/catalog.hu.i18n b/apps/code/catalog.hu.i18n index 7dd162659e9..e13a9788601 100644 --- a/apps/code/catalog.hu.i18n +++ b/apps/code/catalog.hu.i18n @@ -262,3 +262,4 @@ PythonKeyAns = "ANS kulcs" PythonKeyExe = "EXE kulcs" PythonImportUtils = "Az ulab.utils importálása" PythonUtilsFunction = "utils modul függvény előtagja" +PythonTurtleBgcolor = "Módosítsa a háttérszínt" diff --git a/apps/code/catalog.it.i18n b/apps/code/catalog.it.i18n index d839de5418a..db2f8bbed09 100644 --- a/apps/code/catalog.it.i18n +++ b/apps/code/catalog.it.i18n @@ -262,3 +262,4 @@ PythonFileReadable = "Dice se si può leggere sul file" PythonFileWritable = "Dice se si può scrivere sul file" PythonImportUtils = "Importazione di ulab.utils" PythonUtilsFunction = "Prefisso funzione del modulo utils" +PythonTurtleBgcolor = "Cambia il colore dello sfondo" diff --git a/apps/code/catalog.nl.i18n b/apps/code/catalog.nl.i18n index 4ba8d9906c5..91fe3b6b39f 100644 --- a/apps/code/catalog.nl.i18n +++ b/apps/code/catalog.nl.i18n @@ -263,3 +263,4 @@ PythonFileReadable = "Tells if read can be used on a file" PythonFileWritable = "Tells if write can be used on a file" PythonImportUtils = "Ulab.utils importeren" PythonUtilsFunction = "utils module functie prefix" +PythonTurtleBgcolor = "Verander de achtergrondkleur" diff --git a/apps/code/catalog.pt.i18n b/apps/code/catalog.pt.i18n index bc740acdf7f..aef378835fe 100644 --- a/apps/code/catalog.pt.i18n +++ b/apps/code/catalog.pt.i18n @@ -262,3 +262,4 @@ PythonFileReadable = "Tells if read can be used on a file" PythonFileWritable = "Tells if write can be used on a file" PythonImportUtils = "Importando ulab.utils" PythonUtilsFunction = "prefixo de função do módulo utils" +PythonTurtleBgcolor = "Alterar a cor de fundo" diff --git a/apps/code/catalog.universal.i18n b/apps/code/catalog.universal.i18n index 09d292f7c25..d1878d9def6 100644 --- a/apps/code/catalog.universal.i18n +++ b/apps/code/catalog.universal.i18n @@ -404,6 +404,7 @@ PythonTurtleCommandBackward = "backward(x)" PythonTurtleCommandCircle = "circle(r)" PythonTurtleCommandColor = "color('c')" PythonTurtleCommandColorMode = "colormode(x)" +PythonTurtleCommandBgcolor = "bgcolor('c')" PythonTurtleCommandForward = "forward(x)" PythonTurtleCommandGoto = "goto(x,y)" PythonTurtleCommandHeading = "heading()" diff --git a/apps/code/python_toolbox.cpp b/apps/code/python_toolbox.cpp index f8a74902223..9affd7a6f62 100644 --- a/apps/code/python_toolbox.cpp +++ b/apps/code/python_toolbox.cpp @@ -338,6 +338,7 @@ const ToolboxMessageTree TurtleModuleChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonTurtleCommandHideturtle, I18n::Message::PythonTurtleHideturtle, false), ToolboxMessageTree::Leaf(I18n::Message::PythonTurtleCommandColor, I18n::Message::PythonTurtleColor), ToolboxMessageTree::Leaf(I18n::Message::PythonTurtleCommandColorMode, I18n::Message::PythonTurtleColorMode), + ToolboxMessageTree::Leaf(I18n::Message::PythonTurtleCommandBgcolor, I18n::Message::PythonTurtleBgcolor), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandColorBlue, I18n::Message::PythonColorBlue, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandColorRed, I18n::Message::PythonColorRed, false), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandColorGreen, I18n::Message::PythonColorGreen, false), @@ -542,6 +543,7 @@ const ToolboxMessageTree catalogChildren[] = { ToolboxMessageTree::Leaf(I18n::Message::PythonCommandCmathFunction, I18n::Message::PythonCmathFunction, false, I18n::Message::PythonCommandCmathFunctionWithoutArg), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandColor, I18n::Message::PythonColor), ToolboxMessageTree::Leaf(I18n::Message::PythonTurtleCommandColorMode, I18n::Message::PythonTurtleColorMode), + ToolboxMessageTree::Leaf(I18n::Message::PythonTurtleCommandBgcolor, I18n::Message::PythonTurtleBgcolor), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandComplex, I18n::Message::PythonComplex), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandCopySign, I18n::Message::PythonCopySign), ToolboxMessageTree::Leaf(I18n::Message::PythonCommandCos, I18n::Message::PythonCos), diff --git a/python/port/genhdr/qstrdefs.in.h b/python/port/genhdr/qstrdefs.in.h index 78cfdb182c8..c390f4ad084 100644 --- a/python/port/genhdr/qstrdefs.in.h +++ b/python/port/genhdr/qstrdefs.in.h @@ -520,6 +520,7 @@ Q(pensize) Q(width) Q(isdown) Q(pencolor) +Q(bgcolor) Q(reset) Q(showturtle) Q(st) diff --git a/python/port/mod/turtle/modturtle.cpp b/python/port/mod/turtle/modturtle.cpp index e08aa4dbd1f..440d696aa75 100644 --- a/python/port/mod/turtle/modturtle.cpp +++ b/python/port/mod/turtle/modturtle.cpp @@ -20,6 +20,7 @@ void modturtle_view_did_disappear() { mp_obj_t modturtle___init__() { sTurtle = Turtle(); + sTurtle.drawBg(); /* Note: we don't even bother writing a destructor for Turtle because this * init function is called once, and only once, per MicroPython init cycle. * When the previous Turtle object is destroyed, its VM is long gone. */ @@ -165,6 +166,38 @@ mp_obj_t modturtle_pencolor(size_t n_args, const mp_obj_t *args) { return mp_const_none; } +mp_obj_t modturtle_bgcolor(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + // bgcolor() + KDColor c = sTurtle.colorBg(); + mp_obj_t mp_col[3]; + if(sTurtle.colorMode() == MicroPython::Color::Mode::MaxIntensity255){ + mp_col[0] = mp_obj_new_int_from_uint(c.red()); + mp_col[1] = mp_obj_new_int_from_uint(c.green()); + mp_col[2] = mp_obj_new_int_from_uint(c.blue()); + } else { + mp_col[0] = mp_obj_new_float(uint8tColorToDouble(c.red())); + mp_col[1] = mp_obj_new_float(uint8tColorToDouble(c.green())); + mp_col[2] = mp_obj_new_float(uint8tColorToDouble(c.blue())); + } + return mp_obj_new_tuple(3, mp_col); + } + if (n_args == 2) { + mp_raise_TypeError("bgcolor() takes 0, 1 or 3 arguments"); + return mp_const_none; + } + mp_obj_t color; + if (n_args == 1) { + color = args[0]; + } else { + assert(n_args == 3); + color = mp_obj_new_tuple(n_args, args); + } + sTurtle.setColorBg(MicroPython::Color::Parse(color, sTurtle.colorMode())); + sTurtle.drawBg(); + return mp_const_none; +} + mp_obj_t modturtle_colormode(size_t n_args, const mp_obj_t *args) { if(n_args == 0){ return mp_obj_new_int_from_uint(static_cast(sTurtle.colorMode())); diff --git a/python/port/mod/turtle/modturtle.h b/python/port/mod/turtle/modturtle.h index 363a6a47d11..e7a3378e79a 100644 --- a/python/port/mod/turtle/modturtle.h +++ b/python/port/mod/turtle/modturtle.h @@ -26,6 +26,7 @@ mp_obj_t modturtle_isvisible(); mp_obj_t modturtle_write(mp_obj_t s); mp_obj_t modturtle_pencolor(size_t n_args, const mp_obj_t *args); +mp_obj_t modturtle_bgcolor(size_t n_args, const mp_obj_t *args); mp_obj_t modturtle_colormode(size_t n_args, const mp_obj_t *args); mp_obj_t modturtle_showturtle(); diff --git a/python/port/mod/turtle/modturtle_table.c b/python/port/mod/turtle/modturtle_table.c index ccadb1de171..96a9ea2e853 100644 --- a/python/port/mod/turtle/modturtle_table.c +++ b/python/port/mod/turtle/modturtle_table.c @@ -18,6 +18,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_pensize_obj, 0, 1, modturtl STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_isdown_obj, modturtle_isdown); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_pencolor_obj, 0, 3, modturtle_pencolor); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_bgcolor_obj, 0, 3, modturtle_bgcolor); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modturtle_colormode_obj, 0, 1, modturtle_colormode); STATIC MP_DEFINE_CONST_FUN_OBJ_0(modturtle_reset_obj, modturtle_reset); @@ -66,6 +67,7 @@ STATIC const mp_rom_map_elem_t modturtle_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_color), (mp_obj_t)&modturtle_pencolor_obj }, { MP_ROM_QSTR(MP_QSTR_pencolor), (mp_obj_t)&modturtle_pencolor_obj }, + { MP_ROM_QSTR(MP_QSTR_bgcolor), (mp_obj_t)&modturtle_bgcolor_obj }, { MP_ROM_QSTR(MP_QSTR_colormode), (mp_obj_t)&modturtle_colormode_obj }, { MP_ROM_QSTR(MP_QSTR_reset), (mp_obj_t)&modturtle_reset_obj }, diff --git a/python/port/mod/turtle/turtle.cpp b/python/port/mod/turtle/turtle.cpp index abba04f4605..1935252888c 100644 --- a/python/port/mod/turtle/turtle.cpp +++ b/python/port/mod/turtle/turtle.cpp @@ -33,6 +33,7 @@ void Turtle::reset() { m_y = 0; m_heading = 0; m_color = k_defaultColor; + m_bgcolor = k_defaultColorBG; m_penDown = true; m_visible = true; m_speed = k_defaultSpeed; @@ -40,6 +41,7 @@ void Turtle::reset() { m_mileage = 0; // Draw the turtle + drawBg(); draw(true); } @@ -202,6 +204,19 @@ bool Turtle::isOutOfBounds() const { return absF(x()) > k_maxPosition || absF(y()) > k_maxPosition; }; +void Turtle::drawBg() { + MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox(); + + KDContext * ctx = KDIonContext::sharedContext(); + + KDRect bg = KDRect( + 0, + 0, + 320, + 229); + ctx->fillRect(bg, m_bgcolor); +} + // Private functions void Turtle::setHeadingPrivate(mp_float_t angle) { @@ -406,4 +421,4 @@ void Turtle::erase() { KDContext * ctx = KDIonContext::sharedContext(); ctx->fillRectWithPixels(iconRect(), m_underneathPixelBuffer, nullptr); m_drawn = false; -} +} \ No newline at end of file diff --git a/python/port/mod/turtle/turtle.h b/python/port/mod/turtle/turtle.h index e6480839607..fdac0a5f551 100644 --- a/python/port/mod/turtle/turtle.h +++ b/python/port/mod/turtle/turtle.h @@ -9,6 +9,7 @@ extern "C" { #include #include #include +#include /* We check for keyboard interruptions using micropython_port_vm_hook_loop and * micropython_port_interruptible_msleep, but even if we catch an interruption, @@ -30,6 +31,7 @@ class Turtle { m_y(0), m_heading(0), m_color(k_defaultColor), + m_bgcolor(k_defaultColorBG), m_colorMode(MicroPython::Color::Mode::MaxIntensity255), m_penDown(true), m_visible(true), @@ -67,11 +69,18 @@ class Turtle { void setVisible(bool visible); KDColor color() const { return m_color; } + KDColor colorBg() const { return m_bgcolor; } void setColor(KDColor c) { m_color = c; } void setColor(uint8_t r, uint8_t g, uint8_t b) { m_color = KDColor::RGB888(r, g, b); + } + void setColorBg(KDColor c) { + m_bgcolor = c; + } + void setColorBg(uint8_t r, uint8_t g, uint8_t b) { + m_bgcolor = KDColor::RGB888(r, g, b); } MicroPython::Color::Mode colorMode() const {return m_colorMode; } void setColorMode(MicroPython::Color::Mode colorMode){ @@ -88,6 +97,7 @@ class Turtle { * when out of bound, and can prevent text that would have been visible to be * drawn. We use very large bounds to temper these effects. */ bool isOutOfBounds() const; + void drawBg(); private: static constexpr mp_float_t k_headingScale = M_PI / 180; @@ -99,6 +109,7 @@ class Turtle { static constexpr uint8_t k_defaultSpeed = 8; static constexpr uint8_t k_maxSpeed = 10; static constexpr KDColor k_defaultColor = KDColorBlack; + static constexpr KDColor k_defaultColorBG = Palette::CodeBackground; static constexpr uint8_t k_defaultPenSize = 1; static constexpr const KDFont * k_font = KDFont::LargeFont; static constexpr mp_float_t k_maxPosition = KDCOORDINATE_MAX * 0.75f; @@ -152,6 +163,7 @@ class Turtle { mp_float_t m_heading; KDColor m_color; + KDColor m_bgcolor; MicroPython::Color::Mode m_colorMode; bool m_penDown; bool m_visible;