1 #include "lua/internal.hpp"
2 #include "core/window.hpp"
3 #include "library/minmax.hpp"
7 class lua_gui_resolution
: public lua_function
10 lua_gui_resolution() : lua_function(LS
, "gui.resolution") {}
11 int invoke(lua_state
& L
)
15 L
.pushnumber(lua_render_ctx
->width
);
16 L
.pushnumber(lua_render_ctx
->height
);
21 template<uint32_t lua_render_context::*gap
>
22 class lua_gui_set_gap
: public lua_function
25 lua_gui_set_gap(const std::string
& name
) : lua_function(LS
, name
) {}
26 int invoke(lua_state
& L
)
30 uint32_t g
= L
.get_numeric_argument
<uint32_t>(1, fname
.c_str());
32 return 0; //Ignore ridiculous gap.
33 lua_render_ctx
->*gap
= g
;
38 lua_gui_set_gap
<&lua_render_context::left_gap
> lg("gui.left_gap");
39 lua_gui_set_gap
<&lua_render_context::right_gap
> rg("gui.right_gap");
40 lua_gui_set_gap
<&lua_render_context::top_gap
> tg("gui.top_gap");
41 lua_gui_set_gap
<&lua_render_context::bottom_gap
> bg("gui.bottom_gap");
43 function_ptr_luafun
gui_repaint(LS
, "gui.repaint", [](lua_state
& L
, const std::string
& fname
) -> int {
44 lua_requests_repaint
= true;
48 function_ptr_luafun
gui_sfupd(LS
, "gui.subframe_update", [](lua_state
& L
, const std::string
& fname
) -> int {
49 lua_requests_subframe_paint
= L
.get_bool(1, fname
.c_str());
53 function_ptr_luafun
gui_color(LS
, "gui.color", [](lua_state
& L
, const std::string
& fname
) -> int {
55 int64_t r
= L
.get_numeric_argument
<uint32_t>(1, fname
.c_str());
56 int64_t g
= L
.get_numeric_argument
<uint32_t>(2, fname
.c_str());
57 int64_t b
= L
.get_numeric_argument
<uint32_t>(3, fname
.c_str());
58 L
.get_numeric_argument
<int64_t>(4, a
, fname
.c_str());
60 L
.pushnumber(((256 - a
) << 24) | (r
<< 16) | (g
<< 8) | b
);
66 function_ptr_luafun
gui_status(LS
, "gui.status", [](lua_state
& L
, const std::string
& fname
) -> int {
67 std::string name
= L
.get_string(1, fname
.c_str());
68 std::string value
= L
.get_string(2, fname
.c_str());
69 auto& w
= platform::get_emustatus();
71 w
.erase("L[" + name
+ "]");
73 w
.set("L[" + name
+ "]", value
);
82 uint8_t hsl2rgb_flags
[] = {24, 52, 6, 13, 33, 19};
84 uint32_t shifthue(uint32_t color
, double shift
)
86 int16_t R
= (color
>> 16) & 0xFF;
87 int16_t G
= (color
>> 8) & 0xFF;
88 int16_t B
= color
& 0xFF;
89 int16_t m
= min(R
, min(G
, B
));
90 int16_t M
= max(R
, max(G
, B
));
101 int16_t ohue
= hue
% (6 * S
);
102 hue
= (hue
+ static_cast<uint32_t>(shift
* S
)) % (6 * S
);
108 uint8_t flag
= hsl2rgb_flags
[hue
/ S
];
109 return (V
[(flag
>> 4) & 3] << 16) | (V
[(flag
>> 2) & 3] << 8) | (V
[flag
& 3]);
112 function_ptr_luafun
gui_rainbow(LS
, "gui.rainbow", [](lua_state
& L
, const std::string
& fname
) -> int {
113 int64_t basecolor
= 0x00FF0000;
114 uint64_t step
= L
.get_numeric_argument
<uint64_t>(1, fname
.c_str());
115 int32_t steps
= L
.get_numeric_argument
<int32_t>(2, fname
.c_str());
116 L
.get_numeric_argument
<int64_t>(3, basecolor
, fname
.c_str());
118 L
.pushstring("Expected nonzero steps for gui.rainbow");
122 //Special: Any rotation of transparent is transparent.
126 uint32_t asteps
= std::abs(steps
);
128 step
= asteps
- step
% asteps
; //Reverse order.
129 double hueshift
= 6.0 * (step
% asteps
) / asteps
;
130 basecolor
= shifthue(basecolor
& 0xFFFFFF, hueshift
) | (basecolor
& 0xFF000000);
131 L
.pushnumber(basecolor
);