Numeric range class, obsolete clip_range.
[lsnes.git] / src / lua / gui-box.cpp
blob3b36484a1aa1a0fc782710aecb97114542a3bf3b
1 #include "lua/internal.hpp"
2 #include "library/framebuffer.hpp"
3 #include "library/range.hpp"
4 #include "library/lua-framebuffer.hpp"
6 namespace
8 struct render_object_box : public framebuffer::object
10 render_object_box(int32_t _x, int32_t _y, int32_t _width, int32_t _height,
11 framebuffer::color _outline1, framebuffer::color _outline2, framebuffer::color _fill,
12 int32_t _thickness) throw()
13 : x(_x), y(_y), width(_width), height(_height), outline1(_outline1), outline2(_outline2),
14 fill(_fill), thickness(_thickness) {}
15 ~render_object_box() throw() {}
16 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
18 uint32_t oX = x + scr.get_origin_x();
19 uint32_t oY = y + scr.get_origin_y();
20 range bX = (range::make_w(scr.get_width()) - oX) & range::make_w(width);
21 range bY = (range::make_w(scr.get_height()) - oY) & range::make_w(height);
22 for(uint32_t r = bY.low(); r != bY.high(); r++) {
23 typename framebuffer::fb<X>::element_t* rptr = scr.rowptr(oY + r);
24 size_t eptr = oX + bX.low();
25 for(uint32_t c = bX.low(); c != bX.high(); c++, eptr++)
26 if((r < thickness && r <= (width - c)) || (c < thickness && c < (height - r)))
27 outline1.apply(rptr[eptr]);
28 else if(r < thickness || c < thickness || r >= height - thickness ||
29 c >= width - thickness)
30 outline2.apply(rptr[eptr]);
31 else
32 fill.apply(rptr[eptr]);
35 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
36 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
37 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
38 private:
39 int32_t x;
40 int32_t y;
41 uint32_t width;
42 uint32_t height;
43 framebuffer::color outline1;
44 framebuffer::color outline2;
45 framebuffer::color fill;
46 uint32_t thickness;
49 int box(lua::state& L, lua::parameters& P)
51 int32_t x, y;
52 uint32_t width, height, thickness;
53 framebuffer::color poutline1, poutline2, pfill;
55 if(!lua_render_ctx) return 0;
57 P(x, y, width, height, P.optional(thickness, 1), P.optional(poutline1, 0xFFFFFFU),
58 P.optional(poutline2, 0x808080U), P.optional(pfill, 0xC0C0C0U));
60 lua_render_ctx->queue->create_add<render_object_box>(x, y, width, height, poutline1, poutline2,
61 pfill, thickness);
62 return 0;
65 lua::functions box_fns(lua_func_misc, "gui", {
66 {"box", box},
67 });