-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
164 lines (164 loc) · 4.31 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
if jit then
debug.setmetatable(_G,{__index=function(self,k) error("NOPEI: "..tostring(k),2) end,__newindex=function(self,k) print("NOPEN: "..tostring(k),2) error("NOPEN: "..tostring(k),2) end})
end
local lgi=require"lgi"
local Gtk=lgi.require("Gtk","3.0")
local Gdk=lgi.require("Gdk")
local G=lgi.require("GLib")
local cairo=lgi.require"cairo"
local window = Gtk.Window{title="InfiniDRAW",on_destroy=Gtk.main_quit,default_width=500,default_height=500}
local area=Gtk.DrawingArea{}
local image_h=1500
local image_w=1366
local images={}
local scroll=1
local function get_image(y)
y=math.floor(y/image_h)
if not images[y] then
images[y]={image=cairo.ImageSurface.create(0,image_w,image_h)}
local paint_cr=cairo.Context.create(images[y].image)
paint_cr:set_source_rgb(255,255,255)
paint_cr:paint()
images[y].cr=paint_cr
end
return images[y]
end
local last_mouse_pos
local tool
function area:on_draw(cr)
--print(area.width)
local si=math.floor(scroll/image_h)+1
for i=si,si+1 do
cr:identity_matrix()
cr:scale(area.width/1366,area.width/1366)
local y=((i-1)*image_h)-scroll
cr:translate(0,y)
cr:set_source_surface(get_image(y+(si*image_h)).image,0,0)
cr:rectangle(0,0,1366,image_h)
cr:fill()
end
cr:identity_matrix()
cr:set_source_rgba(0,0,0,0.5)
cr:scale(area.width/1366,area.width/1366)
cr:translate(0,math.fmod(-scroll,image_h)-image_h)
local grid_space=30
for y=0,image_h*3,grid_space do
cr:move_to(0,y)
cr:line_to(1366,y)
cr:stroke()
end
for x=0,1366,grid_space do
cr:move_to(x,0)
cr:line_to(x,image_h*3)
cr:stroke()
end
local function a()
cr:identity_matrix()
cr:scale(area.width/1366,area.width/1366)
cr:translate(0,-scroll)
local r=10
if tool=="draw" then r=3 end
if not tool then r=5 end
cr:arc(last_mouse_pos.x,last_mouse_pos.y,r,0,math.pi*2)
end
if last_mouse_pos then
if tool=="draw" then
cr:set_source_rgba(0,0,0,1)
elseif tool then
cr:set_source_rgba(0,0,0,0.3)
else
cr:set_source_rgba(0,0,0,0.5)
end
a()
cr:stroke()
end
end
local function save()
local img=cairo.ImageSurface.create(0,1366,(1+#images)*image_h)
local cr=cairo.Context.create(img)
for i,img in pairs(images) do
cr:identity_matrix()
cr:translate(0,i*image_h)
cr:set_source_surface(img.image,0,0)
cr:paint()
end
img:write_to_png(os.time()..".png")
end
area:add_events(Gdk.EventMask.POINTER_MOTION_MASK)
area:add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
window:add_events(Gdk.EventMask.KEY_PRESS_MASK)
area:add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
local last_draw=0
local f
do
local function mv(this,last,state)
--for k,v in pairs(state) do print(k,v) end
local cr=get_image(this.y).cr
local function dd(su)
su(cr)
local y=math.fmod(this.y,image_h)
local ly=(y-this.y)+last.y
cr:move_to(last.x,ly)
cr:line_to(this.x,y)
cr:stroke()
if ly<0 or ly>=image_h then
local cr=get_image(last.y).cr
su(cr)
local y=math.fmod(last.y,image_h)
local ly=(y-last.y)+this.y
cr:move_to(this.x,ly)
cr:line_to(last.x,y)
cr:stroke()
end
end
if state.BUTTON1_MASK and state.BUTTON3_MASK then
scroll=math.max(scroll-(this.ry-last.ry),1)
elseif state.BUTTON1_MASK and state.BUTTON2_MASK then
tool="erase"
dd(function(cr)
cr:set_source_rgb(255,255,255)
cr:set_line_width(20)
end)
elseif state.BUTTON1_MASK then
tool="draw"
dd(function(cr)
cr:set_source_rgb(0,0,0)
cr:set_line_width(2)
end)
else
end
end
f=function(area,event)
area:grab_focus()
local x=event.x/(area.width/1366)
local y=event.y/(area.width/1366)
local t={x=x,y=y+scroll,ry=y}
tool=nil
if last_mouse_pos then
mv(t,last_mouse_pos,event.state)
end
last_mouse_pos=t
local cur=G.get_monotonic_time()
if (cur-last_draw)>30*1000 then
area:queue_draw()
last_draw=cur
end
end
end
area.on_motion_notify_event=f
area.on_button_press_event=f
area.on_button_release_event=f
function window:on_key_press_event(evt)
if evt.keyval==115 then
save()
elseif evt.keyval==110 then
save()
images={}
else
end
end
window:add(area)
window:fullscreen()
window:set_keep_above(true)
window:show_all()
Gtk.main()