Skip to content

Commit

Permalink
Merge pull request #34 from pimoroni/text_improvements
Browse files Browse the repository at this point in the history
new text api with measurement function
  • Loading branch information
Gadgetoid authored Oct 21, 2021
2 parents 3209138 + 9656e33 commit c4a2271
Show file tree
Hide file tree
Showing 23 changed files with 766 additions and 613 deletions.
7 changes: 3 additions & 4 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ builtins =
Buffer,
Voice,
pen,
tick,
flip,
init,
start,
quit,
_reset,
_logo,
play,
clip,
Expand All @@ -35,7 +34,7 @@ builtins =
blit,
sprite,
text,
text_width,
measure,
rgb,
hsv,
intersects,
Expand Down Expand Up @@ -213,4 +212,4 @@ builtins =
SWORD9,
SWORD10,
SWORD11,
CROWN2,
CROWN2,
4 changes: 2 additions & 2 deletions examples/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ void draw_dial(std::string name, int32_t x, int32_t y) {
fcircle(dotx + x + 30, doty + y + 20, 3);

// centre dial label and value
uint32_t lw = text_width(d->name);
uint32_t lw = 60; //text_width(d->name);
text(d->name, x + 30 - (lw / 2), y + 60 - 22);
std::string unit_label = str(d->value) + d->unit;
lw = text_width(unit_label);
lw = 20;//text_width(unit_label);
text(unit_label, x + 30 - (lw / 2), y + 60 - 12);

// if this is the active dial then highlight it
Expand Down
44 changes: 28 additions & 16 deletions examples/shapes/shapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ struct shape_t {
color_t p;
};

std::array<shape_t, 200> shapes;
std::array<shape_t, 250> shapes;

void reset() {
for(auto &s : shapes) {
s.x = std::rand() % 120;
s.y = std::rand() % 120;
s.w = std::rand() % 20;
s.h = std::rand() % 20;
s.x = std::rand() % SCREEN->w;
s.y = std::rand() % SCREEN->h;
s.w = std::rand() % 40;
s.h = std::rand() % 40;
s.r = std::rand() % 360;
s.p = hsv((std::rand() % 100) / 100.0f, 1.0f, 1.0f);
}
Expand All @@ -39,6 +39,7 @@ void init() {

int32_t view = -1;


void update(uint32_t tick) {
move();

Expand All @@ -53,18 +54,22 @@ void update(uint32_t tick) {
}

void label(std::string s) {
pen(0, 0, 0, 8);
frect(0, 11, 120, 15);
blend(ALPHA);
pen(1, 2, 3, 8);
frect(0, 11, SCREEN->w, 15);
pen(0, 0, 0);
text(s + ":", 3, 16);
pen(15, 15, 15);
text(s + ":", 2, 15);
}

void draw(uint32_t tick) {
uint32_t start = time_us();
pen(0, 0, 0);
clear();

blend(COPY);

switch(view) {
case 0: {
for(auto &s : shapes) {pen(s.p); frect(s.x, s.y, s.w, s.h);}
Expand All @@ -78,7 +83,7 @@ void draw(uint32_t tick) {
uint32_t i = 0;
for(auto &s : shapes) {
pen(s.p);
text(char((i++ % 96) + 32), s.x, s.y);
text(std::string(1, char((i++ % 96) + 32)), s.x, s.y);
}
label("Text");
} break;
Expand All @@ -88,6 +93,7 @@ void draw(uint32_t tick) {
label("Ellipses");
} break;
case 4: {
blend(MASK);
uint32_t i = 0;
for(auto &s : shapes) {sprite(i++, s.x, s.y);}
label("Sprites");
Expand Down Expand Up @@ -142,19 +148,25 @@ void draw(uint32_t tick) {
} break;
}

blend(ALPHA);

// draw title
pen(15, 15, 15);
frect(0, 0, 120, 11);
frect(0, 0, SCREEN->w, 11);
pen(0, 0, 0);
text("Shapes Test", 2, 2);

pen(0, 0, 0, 8);
frect(0, 60, 120, 60);
// draw stats
pen(1, 2, 3, 8);
frect(0, SCREEN->h - 74, 80, 74);

pen(15, 15, 15);
text("fps: " + str(stats.fps), 10, 70);
text("draw us: " + str(stats.draw_us), 10, 80);
text("update us: " + str(stats.update_us), 10, 90);
text("tick us: " + str(stats.tick_us), 10, 100);
text("idle: " + str(stats.idle) + "%", 10, 110);
cursor(2, SCREEN->h - 70);
text("shapes: " + str(shapes.size()));
text("fps: " + str(stats.fps));
text("draw us: " + str(stats.draw_us));
text("update us: " + str(stats.update_us));
text("tick us: " + str(stats.tick_us));
text("idle: " + str(stats.idle) + "%");
text("flip us: " + str(stats.flip_us));
}
2 changes: 1 addition & 1 deletion examples/sprites/sprites.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void draw(uint32_t tick) {
alpha(15);

// centre name of weapon at bottom of screen
int label_width = text_width(weapons[selected].name);
int label_width = 40;//text_width(weapons[selected].name);
pen(8, 11, 11);
frect(60 - label_width / 2 - 3, 104 - 3, label_width + 6, 13);
pen(0, 0, 0);
Expand Down
35 changes: 22 additions & 13 deletions examples/text/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,49 @@ using namespace picosystem;
uint32_t wrap_width;

void init() {
//int32_t w = -1, int32_t h = 8, int32_t s = 1,
// uint8_t *data = nullptr
// font(4, 6, 1);
}

void update(uint32_t tick) {
wrap_width = (fsin(time() / 1000.0f) * 40.0f) + 76;
wrap_width = (fsin(time() / 1000.0f) * 20.0f) + 96;
}

void draw(uint32_t tick) {
pen(0, 0, 0);
pen(0, 0, 0, 2);
clear();

// draw title
pen(15, 15, 15);
frect(0, 0, 120, 11);
pen(0, 0, 0);
text("Word Wrap Test", 2, 2);
text("Word wrap test", 2, 2);

pen(15, 15, 15);
text("Wrap at " + str(wrap_width) + " pixels:", 2, 15);

// define a long message to word wrap
std::string message = "\
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. \
The Quick Brown Fox Jumped Over The Lazy Dog. \
1234567890 ?><,.;'# []{}-=_+ \\|";
std::string message = "\\rgbcccWe are just an advanced breed of \\rgbc0cmonkeys\\rgbccc on a minor planet of a \\rgbcc0very average star\\rgbccc. But we can \\rgb0ccunderstand\\rgbccc the Universe. That makes us something very special.\
\n\
\n\t\\rgbfff- Stephen Hawking";

int32_t w, h;

uint32_t s = time_us();
measure(message, w, h, wrap_width);
pen(0, 0, 0);
text(str(time_us() - s), 100, 2);

// given the message, and a width in pixels, the wrap() method will insert
// newline characters where needed (modifying the original string)
wrap(message, wrap_width);
// draw measured rectangle for text
pen(4, 4, 4, 4);
frect(2, 28, w, h);

// draw wrapped text
pen(8, 8, 8);
text(message, 2, 28);
text(message, 2, 28, wrap_width);

// draw green box to show wrap width
// draw green line to show wrap width
pen(0, 12, 0);
rect(1, 27, wrap_width + 2, 92);
vline(wrap_width + 2, 27, 92);
}
Loading

0 comments on commit c4a2271

Please sign in to comment.