Skip to content

Commit 3c35a32

Browse files
committed
Add mouse
1 parent 2458cfd commit 3c35a32

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

projects/spirograph.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ config=make_config({
3636
{"R",400,type="int",min=0,max=max_size},
3737
{"ticking",100,type="float",min=1,max=10000},
3838
{"ticking2",100,type="float",min=1,max=10000},
39+
{"dist",100,type="float",min=1,max=1000},
3940
},config)
4041
image_no=image_no or 0
4142
function draw_config( tbl )
@@ -98,10 +99,17 @@ function update( )
9899
img_buf:save("saved_"..image_no..".png","Saved by PixelDance")
99100
image_no=image_no+1
100101
end
102+
local eps=0.000001
101103
imgui.End()
102104
for i=1,config.ticking do
103105
local x,y=pos(tick/config.ticking2);
104-
img_buf:set(x+s[1]/2,y+s[2]/2,c_u8)
106+
local x2,y2=pos(tick/config.ticking2+eps);
107+
local dx=x2-x
108+
local dy=y2-y
109+
local dl=math.sqrt(dx*dx+dy*dy)
110+
local tx=dx/dl
111+
local ty=dy/dl
112+
img_buf:set(x+tx*config.dist+s[1]/2,y+ty*config.dist+s[2]/2,c_u8)
105113
tick=tick+1
106114
end
107115
img_buf:present()

src/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ struct project {
245245
init_lua();
246246
reload_file();
247247
}
248+
void set_mouse()
249+
{
250+
auto& io = ImGui::GetIO();
251+
lua_newtable(L);
252+
253+
lua_pushnumber(L, io.MousePos.x);
254+
lua_setfield(L, -2, "x");
255+
256+
lua_pushnumber(L, io.MousePos.y);
257+
lua_setfield(L, -2, "y");
258+
259+
#define ADD_MOUSE(x) lua_pushboolean(L, io.MouseDownOwned[x]); lua_setfield(L, -2, "owned" ## # x); lua_pushboolean(L, io.MouseClicked[x]); lua_setfield(L, -2, "clicked" ## # x); lua_pushboolean(L, io.MouseReleased[x]); lua_setfield(L, -2, "released" ## # x)
260+
ADD_MOUSE(0);
261+
ADD_MOUSE(1);
262+
ADD_MOUSE(2);
263+
ADD_MOUSE(3);
264+
ADD_MOUSE(4);
265+
#undef ADD_MOUSE
266+
lua_setfield(L, LUA_GLOBALSINDEX, "__mouse");
267+
}
248268
void update()
249269
{
250270
if (is_errored)
@@ -390,6 +410,7 @@ int main(int argc, char** argv)
390410

391411
if(need_reload)
392412
current_project.reload_file();
413+
current_project.set_mouse();
393414
current_project.update();
394415

395416
ImGui::Begin("Projects");

0 commit comments

Comments
 (0)