[python/turtle] Added write method

Allows user to print strings with the turtle

Change-Id: I06a3832f6fa36d29506be10c48a1b2fb34cb69fb
This commit is contained in:
Arthur
2020-05-19 17:48:37 +02:00
committed by Émilie Feral
parent 7cd0b7e0e0
commit fe7c4b1a8a
5 changed files with 27 additions and 0 deletions

View File

@@ -175,6 +175,21 @@ void Turtle::setVisible(bool visible) {
}
}
void Turtle::write(const char * string) {
// To prevent overlapping between the text and the turtle, force redraw
m_drawn = false;
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDContext * ctx = KDIonContext::sharedContext();
static constexpr KDCoordinate headOffsetLength = 6;
KDCoordinate headOffsetX = headOffsetLength * std::cos(m_heading * k_headingScale);
KDCoordinate headOffsetY = k_invertedYAxisCoefficient * headOffsetLength * std::sin(m_heading * k_headingScale);
KDPoint headOffset(headOffsetX, headOffsetY);
KDPoint head(-k_iconHeadSize, -k_iconHeadSize);
KDPoint stringOffset = KDPoint(0,-k_font->glyphSize().height());
ctx->drawString(string, position().translatedBy(headOffset).translatedBy(head).translatedBy(stringOffset));
draw(true);
}
void Turtle::viewDidDisappear() {
m_drawn = false;