Skip to content

Commit

Permalink
Added Gui.drawImage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Apr 19, 2021
1 parent 3fb24f6 commit b1605ae
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 13 deletions.
24 changes: 12 additions & 12 deletions doc/luaGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Graphics {
*
* @param x - X coordinate of the pixel.
* @param y - Y coordinate of the pixel.
* @param img - A valid image id.
* @param img - A valid image ID.
*
* @return The pixel color value (See ::Color).
*/
Expand Down Expand Up @@ -169,7 +169,7 @@ class Graphics {
* @param height - Image height.
* @param color - A valid color (See ::Color) <b>(optional)</b>.
*
* @return A valid image id.
* @return A valid image ID.
*/
int createImage(int width, int height, int color);

Expand All @@ -184,7 +184,7 @@ class Graphics {
*
* @param filename - Name of the file to open.
*
* @return A valid image id.
* @return A valid image ID.
*/
int loadImage(string filename);

Expand All @@ -197,7 +197,7 @@ class Graphics {
* Graphics.freeImage(img)
* @endcode
*
* @param img - A valid image id.
* @param img - A valid image ID.
*/
void freeImage(int img);

Expand All @@ -210,7 +210,7 @@ class Graphics {
* Graphics.setImageFilters(img, FILTER_LINEAR, FILTER_LINEAR)
* @endcode
*
* @param img - A valid image id.
* @param img - A valid image ID.
* @param min_filter - Min filter to use.
* @param mag_filter - Mag filter to use.
*/
Expand All @@ -225,7 +225,7 @@ class Graphics {
* width = Graphics.getImageWidth(img)
* @endcode
*
* @param img - A valid image id.
* @param img - A valid image ID.
*
* @return The image width in pixels.
*/
Expand All @@ -240,7 +240,7 @@ class Graphics {
* height = Graphics.getImageHeight(img)
* @endcode
*
* @param img - A valid image id.
* @param img - A valid image ID.
*
* @return The image height in pixels.
*/
Expand All @@ -257,7 +257,7 @@ class Graphics {
*
* @param x - X coordinate of the image in pixels.
* @param y - Y coordinate of the image in pixels.
* @param img - A valid image id.
* @param img - A valid image ID.
* @param color - Image tint color (See ::Color) <b>(optional)</b>.
*
*/
Expand All @@ -274,7 +274,7 @@ class Graphics {
*
* @param x - X coordinate of the image in pixels.
* @param y - Y coordinate of the image in pixels.
* @param img - A valid image id.
* @param img - A valid image ID.
* @param rad - Rotation radius.
* @param color - Image tint color (See ::Color) <b>(optional)</b>.
*
Expand All @@ -292,7 +292,7 @@ class Graphics {
*
* @param x - X coordinate of the image in pixels.
* @param y - Y coordinate of the image in pixels.
* @param img - A valid image id.
* @param img - A valid image ID.
* @param x_scale - Scale value for X parameter.
* @param y_scale - Scale value for Y parameter.
* @param color - Image tint color (See ::Color) <b>(optional)</b>.
Expand All @@ -311,7 +311,7 @@ class Graphics {
*
* @param x - X coordinate of the image in pixels.
* @param y - Y coordinate of the image in pixels.
* @param img - A valid image id.
* @param img - A valid image ID.
* @param x_start - Image X coordinate for the partial drawing.
* @param y_start - Image Y coordinate for the partial drawing.
* @param width - Partial drawing width.
Expand All @@ -332,7 +332,7 @@ class Graphics {
*
* @param x - X coordinate of the image in pixels.
* @param y - Y coordinate of the image in pixels.
* @param img - A valid image id.
* @param img - A valid image ID.
* @param x_start - Image X coordinate for the partial drawing.
* @param y_start - Image Y coordinate for the partial drawing.
* @param width - Partial drawing width.
Expand Down
21 changes: 21 additions & 0 deletions doc/luaGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,27 @@ class Gui {
*/
void drawProgressbar(number fraction, number w, number h);

/**
* Draw an image in the Gui subsystem.
* \ingroup Gui
*
* @par Usage example:
* @code
* icon = Graphics.loadImage("app0:/icon.png")
* Gui.drawImage(icon, 32, 32, 0, 0, 20, 20)
* @endcode
*
* @param img - A valid image ID.
* @param width - Width in pixels of the widget <b>(optional)</b>.
* @param height - Height in pixels of the widget <b>(optional)</b>.
* @param img_x - Source image X coordinate <b>(optional)</b>.
* @param img_y - Source image Y coordinate <b>(optional)</b>.
* @param img_w - Source image width <b>(optional)</b>.
* @param img_h - Source image height <b>(optional)</b>.
* @param color - Image tint color (See ::Color) <b>(optional)</b>.
*/
void drawImage(int img, number width, number height, number img_x, number img_y, number img_w, number img_h, int color);

/**
* Set next widget position.
* \ingroup Gui
Expand Down
2 changes: 1 addition & 1 deletion doc/luaRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Render {
* @endcode
*
* @param v - A table with the model vertices.
* @param texture - Texture ID to use.
* @param texture - A valid image ID.
*
* @return A valid model ID.
*/
Expand Down
51 changes: 51 additions & 0 deletions source/luaGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,56 @@ static int lua_listbox(lua_State *L) {
return 1;
}

static int lua_gimg(lua_State *L) {
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc < 1) return luaL_error(L, "wrong number of arguments");
#endif
lpp_texture *text = (lpp_texture*)(luaL_checkinteger(L, 1));
#ifndef SKIP_ERROR_HANDLING
if (text->magic != 0xABADBEEF) luaL_error(L, "attempt to access wrong memory block type.");
#endif

float width, height, tex_x, tex_y, tex_w, tex_h;
uint32_t color;

if (argc > 1) {
width = luaL_checknumber(L, 2);
height = luaL_checknumber(L, 3);
if (argc > 3) {
tex_x = luaL_checknumber(L, 4);
tex_y = luaL_checknumber(L, 5);
tex_w = luaL_checknumber(L, 6);
tex_h = luaL_checknumber(L, 7);
if (argc == 8) color = luaL_checkinteger(L, 8);
}
}

switch (argc) {
case 1:
ImGui::Image(text->text, ImVec2(vita2d_texture_get_width(text->text), vita2d_texture_get_height(text->text)));
break;
case 3:
ImGui::Image(text->text, ImVec2(width, height));
break;
case 7:
ImGui::Image(text->text, ImVec2(width, height),
ImVec2(tex_x / (float)vita2d_texture_get_width(text->text), tex_y / (float)vita2d_texture_get_height(text->text)),
ImVec2((tex_x + tex_w) / (float)vita2d_texture_get_width(text->text), (tex_y + tex_h) / (float)vita2d_texture_get_height(text->text)));
break;
case 8:
ImGui::Image(text->text, ImVec2(width, height),
ImVec2(tex_x / (float)vita2d_texture_get_width(text->text), tex_y / (float)vita2d_texture_get_height(text->text)),
ImVec2((tex_x + tex_w) / (float)vita2d_texture_get_width(text->text), (tex_y + tex_h) / (float)vita2d_texture_get_height(text->text)),
ImVec4((float)(color & 0xFF) / 255.0f, (float)((color >> 8) & 0xFF) / 255.0f, (float)((color >> 16) & 0xFF) / 255.0f, (float)((color >> 24) & 0xFF) / 255.0f));
break;
default:
return luaL_error(L, "wrong number of arguments");
}

return 0;
}

//Register our Gui Functions
static const luaL_Reg Gui_functions[] = {
{"init", lua_init},
Expand Down Expand Up @@ -671,6 +721,7 @@ static const luaL_Reg Gui_functions[] = {
{"setWidgetWidth", lua_widgetwidth},
{"resetWidgetWidth", lua_widgetwidthr},
{"drawListBox", lua_listbox},
{"drawImage", lua_gimg},
{0, 0}
};

Expand Down

0 comments on commit b1605ae

Please sign in to comment.