Skip to content

Commit

Permalink
UI: implemented image.draw(), fix image.save in andoid smallbasic#115
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisws committed Jun 6, 2021
1 parent 4596fb0 commit 0d02d9b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2021-06-06 (12.22)
UI: implemented image.draw(), fix image.save in andoid #115

2021-06-05 (12.22)
COMMON: Fixes 'Socket Client doesn't receive byte with value 13' #112
COMMON: Fixes TSAVE of arrays includes extra null character. #119
Expand Down
5 changes: 5 additions & 0 deletions src/ui/ansiwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ void AnsiWidget::drawEllipse(int xc, int yc, int rx, int ry, int fill) {
flush(false, false, MAX_PENDING_GRAPHICS);
}

void AnsiWidget::drawImage(ImageDisplay &image) {
_back->drawImage(image);
flush(false, false, MAX_PENDING_GRAPHICS);
}

// draw a line onto the offscreen buffer
void AnsiWidget::drawLine(int x1, int y1, int x2, int y2) {
_back->drawLine(x1, y1, x2, y2);
Expand Down
1 change: 1 addition & 0 deletions src/ui/ansiwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct AnsiWidget {
void draw();
void drawArc(int xc, int yc, double r, double start, double end, double aspect);
void drawEllipse(int xc, int yc, int rx, int ry, int fill);
void drawImage(ImageDisplay &image);
void drawOverlay(bool vscroll) { _back->drawOverlay(vscroll); }
void drawLine(int x1, int y1, int x2, int y2);
void drawRect(int x1, int y1, int x2, int y2);
Expand Down
70 changes: 47 additions & 23 deletions src/ui/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,56 +301,70 @@ ImageBuffer *load_xpm_image(char **data) {
return result;
}

void cmd_image_show(var_s *self, var_s *) {
ImageDisplay image;
image._bid = map_get_int(self, IMG_BID, -1);
void get_image_display(var_s *self, ImageDisplay *image) {
image->_bid = map_get_int(self, IMG_BID, -1);

List_each(ImageBuffer *, it, cache) {
ImageBuffer *next = (*it);
if (next->_bid == image._bid) {
image._buffer = next;
if (next->_bid == image->_bid) {
image->_buffer = next;
break;
}
}

var_int_t x, y, z, op;
int count = par_massget("iiii", &x, &y, &z, &op);

if (prog_error || image._buffer == nullptr || count == 1 || count > 4) {
if (prog_error || image->_buffer == nullptr || count == 1 || count > 4) {
err_throw(ERR_PARAM);
} else {
// 0, 2, 3, 4 arguments accepted
if (count >= 2) {
image._x = x;
image._y = y;
image->_x = x;
image->_y = y;
map_set_int(self, IMG_X, x);
map_set_int(self, IMG_Y, y);
} else {
image._x = map_get_int(self, IMG_X, -1);
image._y = map_get_int(self, IMG_Y, -1);
image->_x = map_get_int(self, IMG_X, -1);
image->_y = map_get_int(self, IMG_Y, -1);
}
if (count >= 3) {
image._zIndex = z;
image->_zIndex = z;
map_set_int(self, IMG_ZINDEX, z);
} else {
image._zIndex = map_get_int(self, IMG_ZINDEX, -1);
image->_zIndex = map_get_int(self, IMG_ZINDEX, -1);
}
if (count == 4) {
image._opacity = op;
image->_opacity = op;
map_set_int(self, IMG_OPACITY, op);
} else {
image._opacity = map_get_int(self, IMG_OPACITY, -1);
image->_opacity = map_get_int(self, IMG_OPACITY, -1);
}

image._offsetLeft = map_get_int(self, IMG_OFFSET_LEFT, -1);
image._offsetTop = map_get_int(self, IMG_OFFSET_TOP, -1);
image._width = map_get_int(self, IMG_WIDTH, -1);
image._height = map_get_int(self, IMG_HEIGHT, -1);
image._id = map_get_int(self, IMG_ID, -1);
image->_offsetLeft = map_get_int(self, IMG_OFFSET_LEFT, -1);
image->_offsetTop = map_get_int(self, IMG_OFFSET_TOP, -1);
image->_width = map_get_int(self, IMG_WIDTH, -1);
image->_height = map_get_int(self, IMG_HEIGHT, -1);
image->_id = map_get_int(self, IMG_ID, -1);
}
}

void cmd_image_show(var_s *self, var_s *) {
ImageDisplay image;
get_image_display(self, &image);
if (!prog_error) {
g_system->getOutput()->addImage(image);
}
}

void cmd_image_draw(var_s *self, var_s *) {
ImageDisplay image;
get_image_display(self, &image);
if (!prog_error) {
g_system->getOutput()->drawImage(image);
}
}

void cmd_image_hide(var_s *self, var_s *) {
int id = map_get_int(self, IMG_ID, -1);
g_system->getOutput()->removeImage(id);
Expand Down Expand Up @@ -397,9 +411,18 @@ void cmd_image_save(var_s *self, var_s *) {
int yoffs = (4 * y * w);
for (int x = 0; x < w; x++) {
int offs = yoffs + (4 * x);
uint8_t r = image->_image[offs + 0];
uint8_t g = image->_image[offs + 1];
uint8_t b = image->_image[offs + 2];
#if defined(PIXELFORMAT_RGBA8888)
int r_offs = offs + 2;
int g_offs = offs + 1;
int b_offs = offs + 0;
#else
int r_offs = offs + 0;
int g_offs = offs + 1;
int b_offs = offs + 2;
#endif
uint8_t r = image->_image[r_offs];
uint8_t g = image->_image[g_offs];
uint8_t b = image->_image[b_offs];
pixel_t px = SET_RGB(r, g, b);
int pos = y * w + x;
var_t *elem = v_elem(array, pos);
Expand Down Expand Up @@ -427,9 +450,10 @@ void create_image(var_p_t var, ImageBuffer *image) {
map_add_var(var, IMG_WIDTH, image->_width);
map_add_var(var, IMG_HEIGHT, image->_height);
map_add_var(var, IMG_BID, image->_bid);
v_create_func(var, "show", cmd_image_show);
v_create_func(var, "draw", cmd_image_draw);
v_create_func(var, "hide", cmd_image_hide);
v_create_func(var, "save", cmd_image_save);
v_create_func(var, "show", cmd_image_show);
}

// loads an image for the form image input type
Expand Down
2 changes: 1 addition & 1 deletion src/ui/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ inline void GET_ARGB(pixel_t c, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b)
#define SET_RGB(r, g, b) ((r << 16) | (g << 8) | (b))
#define GET_RGB RGB888_to_RGB
#define GET_RGB2 RGB888_to_RGB
#define GET_FROM_RGB888(c) (c)
#define GET_FROM_RGB888(c) (c)
#endif

#endif
5 changes: 5 additions & 0 deletions src/ui/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,11 @@ void GraphicScreen::drawEllipse(int xc, int yc, int rx, int ry, int fill) {
maEllipse(xc, yc, rx, ry, fill);
}

void GraphicScreen::drawImage(ImageDisplay &image) {
drawInto();
image.draw(image._x, image._y, image._width, image._height, 0);
}

void GraphicScreen::drawInto(bool background) {
maSetDrawTarget(_image);
Screen::drawInto(background);
Expand Down
3 changes: 3 additions & 0 deletions src/ui/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct Screen : public Shape {
virtual void drawArc(int xc, int yc, double r, double start, double end, double aspect) = 0;
virtual void drawBase(bool vscroll, bool update=true) = 0;
virtual void drawEllipse(int xc, int yc, int rx, int ry, int fill) = 0;
virtual void drawImage(ImageDisplay &image) = 0;
virtual void drawInto(bool background=false);
virtual void drawLine(int x1, int y1, int x2, int y2) = 0;
virtual void drawRect(int x1, int y1, int x2, int y2) = 0;
Expand Down Expand Up @@ -107,6 +108,7 @@ struct GraphicScreen : public Screen {
void drawArc(int xc, int yc, double r, double start, double end, double aspect);
void drawBase(bool vscroll, bool update=true);
void drawEllipse(int xc, int yc, int rx, int ry, int fill);
void drawImage(ImageDisplay &image);
void drawInto(bool background=false);
void drawLine(int x1, int y1, int x2, int y2);
void drawRect(int x1, int y1, int x2, int y2);
Expand Down Expand Up @@ -326,6 +328,7 @@ struct TextScreen : public Screen {
void clear();
void drawArc(int xc, int yc, double r, double start, double end, double aspect) {}
void drawBase(bool vscroll, bool update=true);
void drawImage(ImageDisplay &image) {}
void drawEllipse(int xc, int yc, int rx, int ry, int fill) {}
void drawLine(int x1, int y1, int x2, int y2);
void drawText(const char *text, int len, int x, int lineHeight);
Expand Down

0 comments on commit 0d02d9b

Please sign in to comment.