Cleanup lua code by introducing lua::functions
[lsnes.git] / src / lua / gui-box.cpp
bloba418226667a3691bf7d6a828a5205750bebf56f9
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
3 #include "library/lua-framebuffer.hpp"
5 namespace
7 struct render_object_box : public framebuffer::object
9 render_object_box(int32_t _x, int32_t _y, int32_t _width, int32_t _height,
10 framebuffer::color _outline1, framebuffer::color _outline2, framebuffer::color _fill,
11 int32_t _thickness) throw()
12 : x(_x), y(_y), width(_width), height(_height), outline1(_outline1), outline2(_outline2),
13 fill(_fill), thickness(_thickness) {}
14 ~render_object_box() throw() {}
15 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
17 outline1.set_palette(scr);
18 outline2.set_palette(scr);
19 fill.set_palette(scr);
20 uint32_t originx = scr.get_origin_x();
21 uint32_t originy = scr.get_origin_y();
22 int32_t xmin = 0;
23 int32_t xmax = width;
24 int32_t ymin = 0;
25 int32_t ymax = height;
26 framebuffer::clip_range(originx, scr.get_width(), x, xmin, xmax);
27 framebuffer::clip_range(originy, scr.get_height(), y, ymin, ymax);
28 for(int32_t r = ymin; r < ymax; r++) {
29 typename framebuffer::fb<X>::element_t* rptr = scr.rowptr(y + r + originy);
30 size_t eptr = x + xmin + originx;
31 for(int32_t c = xmin; c < xmax; c++, eptr++)
32 if((r < thickness && r <= (width - c)) || (c < thickness && c < (height - r)))
33 outline1.apply(rptr[eptr]);
34 else if(r < thickness || c < thickness || r >= height - thickness ||
35 c >= width - thickness)
36 outline2.apply(rptr[eptr]);
37 else
38 fill.apply(rptr[eptr]);
41 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
42 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
43 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
44 private:
45 int32_t x;
46 int32_t y;
47 int32_t width;
48 int32_t height;
49 framebuffer::color outline1;
50 framebuffer::color outline2;
51 framebuffer::color fill;
52 int32_t thickness;
55 int box(lua::state& L, lua::parameters& P)
57 int32_t x, y;
58 uint32_t width, height, thickness;
59 framebuffer::color poutline1, poutline2, pfill;
61 if(!lua_render_ctx) return 0;
63 P(x, y, width, height, P.optional(thickness, 1), P.optional(poutline1, 0xFFFFFFU),
64 P.optional(poutline2, 0x808080U), P.optional(pfill, 0xC0C0C0U));
66 lua_render_ctx->queue->create_add<render_object_box>(x, y, width, height, poutline1, poutline2,
67 pfill, thickness);
68 return 0;
71 lua::functions box_fns(lua_func_misc, "gui", {
72 {"box", box},
73 });