Add new Lua function gui.line()

This commit is contained in:
Ilari Liusvaara 2011-10-22 12:25:37 +03:00
parent feea8e41cc
commit 92cc0b0d89
2 changed files with 131 additions and 5 deletions

95
lua/gui-line.cpp Normal file
View file

@ -0,0 +1,95 @@
#include "lua-int.hpp"
#include "render.hpp"
namespace
{
struct render_object_line : public render_object
{
render_object_line(int32_t _x1, int32_t _x2, int32_t _y1, int32_t _y2, premultiplied_color _color)
throw()
: x1(_x1), y1(_y1), x2(_x2), y2(_y2), color(_color) {}
~render_object_line() throw() {}
void operator()(struct screen& scr) throw()
{
int32_t xdiff = x2 - x1;
int32_t ydiff = y2 - y1;
if(xdiff < 0)
xdiff = -xdiff;
if(ydiff < 0)
ydiff = -ydiff;
if(xdiff >= ydiff) {
//X-major line.
if(x2 < x1) {
//Swap points so that x1 < x2.
std::swap(x1, x2);
std::swap(y1, y2);
}
//The slope of the line is (y2 - y1) / (x2 - x1) = +-ydiff / xdiff
int32_t y = y1;
int32_t ysub = 0;
for(int32_t x = x1; x <= x2; x++) {
if(x < 0 || static_cast<uint32_t>(x) >= scr.width)
goto nodraw1;
if(y < 0 || static_cast<uint32_t>(y) >= scr.height)
goto nodraw1;
color.apply(scr.rowptr(y)[x]);
nodraw1:
ysub += ydiff;
if(ysub >= xdiff) {
ysub -= xdiff;
if(y2 > y1)
y++;
else
y--;
}
}
} else {
//Y-major line.
if(x2 < x1) {
//Swap points so that y1 < y2.
std::swap(x1, x2);
std::swap(y1, y2);
}
//The slope of the line is (x2 - x1) / (y2 - y1) = +-xdiff / ydiff
int32_t x = x1;
int32_t xsub = 0;
for(int32_t y = y1; y <= y2; y++) {
if(x < 0 || static_cast<uint32_t>(x) >= scr.width)
goto nodraw2;
if(y < 0 || static_cast<uint32_t>(y) >= scr.height)
goto nodraw2;
color.apply(scr.rowptr(y)[x]);
nodraw2:
xsub += xdiff;
if(xsub >= ydiff) {
xsub -= ydiff;
if(x2 > x1)
x++;
else
x--;
}
}
}
}
private:
int32_t x1;
int32_t y1;
int32_t x2;
int32_t y2;
premultiplied_color color;
};
function_ptr_luafun gui_pixel("gui.line", [](lua_State* LS, const std::string& fname) -> int {
if(!lua_render_ctx)
return 0;
int64_t color = 0xFFFFFFU;
int32_t x1 = get_numeric_argument<int32_t>(LS, 1, fname.c_str());
int32_t y1 = get_numeric_argument<int32_t>(LS, 2, fname.c_str());
int32_t x2 = get_numeric_argument<int32_t>(LS, 3, fname.c_str());
int32_t y2 = get_numeric_argument<int32_t>(LS, 4, fname.c_str());
get_numeric_argument<int64_t>(LS, 5, color, fname.c_str());
premultiplied_color pcolor(color);
lua_render_ctx->queue->add(*new render_object_line(x1, x2, y1, y2, pcolor));
return 0;
});
}

View file

@ -1808,10 +1808,12 @@ Most of these functions can only be called in on_paint and on_video callbacks.
\end_layout
\begin_layout Standard
Colors are 15-bit.
Each red, green and blue values are in range 0-31 and the final color value
is 1024*r+32*g+b.
Alpha range is 0 (fully transparent) to 32 (sic! fully opaque).
Colors are 32-bit.
Bits 0-7 are the blue component, bits 8-15 are the green component, bits
16-23 are the red component, bits 24-31 are alpha component (0 is fully
opaque, 255 is almost transparent).
-1 is the fully transparent color.
Alpha values greater than 127 do work.
\end_layout
\begin_layout Standard
@ -1927,7 +1929,7 @@ color: Color of the pixel (default is 0xFFFFFF (white))
\end_layout
\begin_layout Subsubsection
gui.crosshair(number x, number y[, number length[, number color]]
gui.crosshair(number x, number y[, number length[, number color]])
\end_layout
\begin_layout Standard
@ -1951,6 +1953,35 @@ length: Length of the crosshair lines (default 10).
color: Color of the crosshair (default is 0xFFFFFF (white))
\end_layout
\begin_layout Subsubsection
gui.line(number x1, number y1, number x2, number y2[, number color])
\end_layout
\begin_layout Standard
Draw a thin line.
Parameters:
\end_layout
\begin_layout Itemize
x1: X-coordinate of one end.
\end_layout
\begin_layout Itemize
y1: Y-coordinate of one end.
\end_layout
\begin_layout Itemize
x2: X-coordinate of the other end.
\end_layout
\begin_layout Itemize
y2: Y-coordinate of the other end.
\end_layout
\begin_layout Itemize
color: Color of the line (default is 0xFFFFFF (white)).
\end_layout
\begin_layout Subsubsection
gui.repaint()
\end_layout