[python/turtle] Turtle::m_drawn is false if sandbox not displayed

This way, Turtle::draw() and erase() are more symetrical and no not
perform themselves if the turtle is already drawn / erased.
This commit is contained in:
Léa Saviot
2018-12-17 17:20:25 +01:00
parent ed19d19070
commit 4b306791c0
9 changed files with 49 additions and 23 deletions

View File

@@ -11,6 +11,10 @@ void modturtle_gc_collect() {
gc_collect_root((void **)&sTurtle, sizeof(Turtle));
}
void modturtle_view_did_disappear() {
sTurtle.viewDidDisappear();
}
mp_obj_t modturtle___init__() {
sTurtle = Turtle();
/* Note: we don't even bother writing a destructor for Turtle because this

View File

@@ -2,6 +2,7 @@
mp_obj_t modturtle___init__();
void modturtle_gc_collect();
void modturtle_view_did_disappear();
mp_obj_t modturtle_reset();

View File

@@ -166,6 +166,10 @@ void Turtle::setColor(const char * color) {
}
}
void Turtle::viewDidDisappear() {
m_drawn = false;
}
// Private functions
void Turtle::setHeadingPrivate(mp_float_t angle) {
@@ -225,7 +229,7 @@ KDRect Turtle::iconRect() const {
bool Turtle::draw(bool force) {
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
if ((m_speed > 0 || force) && m_visible && hasUnderneathPixelBuffer()) {
if ((m_speed > 0 || force) && m_visible && !m_drawn && hasUnderneathPixelBuffer()) {
KDContext * ctx = KDIonContext::sharedContext();
// Get the pixels underneath the turtle
@@ -322,8 +326,8 @@ bool Turtle::dot(mp_float_t x, mp_float_t y) {
}
void Turtle::drawPaw(PawType type, PawPosition pos) {
assert(!m_drawn && m_underneathPixelBuffer != nullptr);
assert(!m_drawn);
assert(m_underneathPixelBuffer != nullptr);
KDCoordinate pawOffset = 5;
constexpr float crawlOffset = 0.6f;
constexpr float angles[] = {M_PI_2/2, M_PI_2+M_PI_2/2, -M_PI_2-M_PI_2/2, -M_PI_2/2};

View File

@@ -72,6 +72,9 @@ public:
m_color = KDColor::RGB888(r, g, b);
}
void setColor(const char * color);
void viewDidDisappear();
private:
static constexpr mp_float_t k_headingOffset = M_PI_2;
static constexpr mp_float_t k_headingScale = M_PI / 180;

View File

@@ -24,11 +24,6 @@ extern "C" {
static MicroPython::ScriptProvider * sScriptProvider = nullptr;
static MicroPython::ExecutionEnvironment * sCurrentExecutionEnvironment = nullptr;
MicroPython::ExecutionEnvironment::ExecutionEnvironment() :
m_sandboxIsDisplayed(false)
{
}
MicroPython::ExecutionEnvironment * MicroPython::ExecutionEnvironment::currentExecutionEnvironment() {
return sCurrentExecutionEnvironment;
}
@@ -90,6 +85,13 @@ void MicroPython::ExecutionEnvironment::interrupt() {
mp_keyboard_interrupt();
}
void MicroPython::ExecutionEnvironment::setSandboxIsDisplayed(bool display) {
if (m_sandboxIsDisplayed && !display) {
modturtle_view_did_disappear();
}
m_sandboxIsDisplayed = display;
}
extern "C" {
extern const void * _stack_start;
extern const void * _stack_end;

View File

@@ -14,7 +14,7 @@ public:
class ExecutionEnvironment {
public:
ExecutionEnvironment();
ExecutionEnvironment() : m_sandboxIsDisplayed(false) {}
static ExecutionEnvironment * currentExecutionEnvironment();
void runCode(const char * );
virtual const char * inputText(const char * prompt) { return nullptr; }
@@ -22,7 +22,10 @@ public:
virtual void resetSandbox() {}
virtual void printText(const char * text, size_t length) {}
void interrupt();
void setSandboxIsDisplayed(bool display);
protected:
bool sandboxIsDisplayed() const { return m_sandboxIsDisplayed; }
private:
bool m_sandboxIsDisplayed;
};