Skip to content

Commit

Permalink
[python] add bgcolor to turtule module (#367)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cartoone222 authored Jun 9, 2024
1 parent 3b22656 commit ff54918
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/code/catalog.de.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions apps/code/catalog.en.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions apps/code/catalog.es.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions apps/code/catalog.fr.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
1 change: 1 addition & 0 deletions apps/code/catalog.hu.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions apps/code/catalog.it.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions apps/code/catalog.nl.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions apps/code/catalog.pt.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions apps/code/catalog.universal.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/python_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions python/port/genhdr/qstrdefs.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ Q(pensize)
Q(width)
Q(isdown)
Q(pencolor)
Q(bgcolor)
Q(reset)
Q(showturtle)
Q(st)
Expand Down
33 changes: 33 additions & 0 deletions python/port/mod/turtle/modturtle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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<int>(sTurtle.colorMode()));
Expand Down
1 change: 1 addition & 0 deletions python/port/mod/turtle/modturtle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions python/port/mod/turtle/modturtle_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 },
Expand Down
17 changes: 16 additions & 1 deletion python/port/mod/turtle/turtle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ 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;
m_penSize = k_defaultPenSize;
m_mileage = 0;

// Draw the turtle
drawBg();
draw(true);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -406,4 +421,4 @@ void Turtle::erase() {
KDContext * ctx = KDIonContext::sharedContext();
ctx->fillRectWithPixels(iconRect(), m_underneathPixelBuffer, nullptr);
m_drawn = false;
}
}
12 changes: 12 additions & 0 deletions python/port/mod/turtle/turtle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {
#include <kandinsky.h>
#include <math.h>
#include <python/port/port.h>
#include <escher/palette.h>

/* We check for keyboard interruptions using micropython_port_vm_hook_loop and
* micropython_port_interruptible_msleep, but even if we catch an interruption,
Expand All @@ -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),
Expand Down Expand Up @@ -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){
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ff54918

Please sign in to comment.