-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
187 lines (153 loc) · 3.9 KB
/
main.rb
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require 'gosu'
require 'rtesseract'
FILE = ARGV.join(' ').strip
IMAGE = Gosu::Image.new(FILE)
RED = Gosu::Color.argb(0xff_ff0000)
GREEN = Gosu::Color.argb(0xff_00ff00)
BLUE = Gosu::Color.argb(0xff_0000ff)
PADDING = 4
THICKNESS = 3
Element = Struct.new(:x1, :y1, :x2, :y2) do
def draw(color=RED)
Gosu.draw_rect(x1-THICKNESS, y1-THICKNESS, x2-x1+(THICKNESS*2), THICKNESS, color)
Gosu.draw_rect(x1-THICKNESS, y1-THICKNESS, THICKNESS, y2-y1+(THICKNESS*2), color)
Gosu.draw_rect(x1-THICKNESS, y2, x2-x1+(THICKNESS*2), THICKNESS, color)
Gosu.draw_rect(x2, y1-THICKNESS, THICKNESS, y2-y1+(THICKNESS*2), color)
end
def line_to(element)
start_point = end_point = []
distance = 0
min = (IMAGE.width**2 + IMAGE.height**2)/2
corners().each do |corner|
element.corners().each do |target|
dis = Math.sqrt(
(corner[0] - target[0]).abs ** 2 +
(corner[1] - target[1]).abs ** 2)
if distance < min
min = distance
start_point = corner
end_point = target
end
end
end
return [start_point[0], start_point[1], end_point[0], end_point[1]]
end
def corners
[[x1,y1], [x2,y1], [x2,y2], [x1,y2]]
end
def under_mouse
w.x1 < mouse_x and w.x2 > mouse_x and w.y1 < mouse_y and w.y2 > mouse_y
end
end
WORDS = RTesseract.new(FILE).to_box.map { |box| Element.new(box[:x_start]-PADDING, box[:y_start]-PADDING, box[:x_end]+PADDING, box[:y_end]+PADDING) }
class Screenshot < Gosu::Window
def initialize()
super IMAGE.width, IMAGE.height
self.caption = "Screenshot editor"
reset()
end
def button_down(id)
case id
when Gosu::KbQ
close
when Gosu::MsLeft
WORDS.each { |w| @group << w if under_mouse(w) }
if not @group.empty?
min_x1 = @group.map { |g| g.x1 }.min
max_x2 = @group.map { |g| g.x2 }.max
min_y1 = @group.map { |g| g.y1 }.min
max_y2 = @group.map { |g| g.y2 }.max
@gelem = Element.new(min_x1, min_y1, max_x2, max_y2)
end
when Gosu::KbReturn
@unselected << @gelem unless @gelem.nil?
@group = []
@gelem = nil
when Gosu::KbX
@unselected.each { |c| @unselected.delete(c) if under_mouse(c) }
when Gosu::KbS
output = "#{FILE.split('.')[0..-2].join('.')}_highlighted.png"
Gosu.render(IMAGE.width, IMAGE.height) do
draw()
end.save(output)
puts output
close
when 46, 87 # plus
@unselected.each do |c|
if under_mouse(c)
c.x1 -= 5
c.x2 += 5
c.y1 -= 5
c.y2 += 5
end
end
when 45, 86 # minus
@unselected.each do |c|
if under_mouse(c)
c.x1 += 5
c.x2 -= 5
c.y1 += 5
c.y2 -= 5
end
end
when Gosu::KbO
reset()
when Gosu::KbC
@unselected.each do |c|
if under_mouse(c)
@pair << c
if @pair.length == 2
@lines << @pair[0].line_to(@pair[1])
@pair = []
end
break
end
end
when Gosu::KbR
e = Element.new(mouse_x, mouse_y, mouse_x+1, mouse_y+1)
@group << e
min_x1 = @group.map { |g| g.x1 }.min
max_x2 = @group.map { |g| g.x2 }.max
min_y1 = @group.map { |g| g.y1 }.min
max_y2 = @group.map { |g| g.y2 }.max
@gelem = Element.new(min_x1, min_y1, max_x2, max_y2)
end
end
def reset
@unselected = []
@group = []
@pair = []
@lines = []
@gelem = nil
end
def under_mouse(word)
word.x1 < mouse_x and word.x2 > mouse_x and word.y1 < mouse_y and word.y2 > mouse_y
end
def draw
IMAGE.draw(0,0)
WORDS.each do |word|
if under_mouse(word)
word.draw()
break
end
end
@gelem.draw(GREEN) if not @gelem.nil?
@unselected.each { |c| c.draw() }
@pair.each { |p| p.draw(BLUE) }
@lines.each do |l|
Gosu.draw_quad(
l[0]-1, l[1]-1, RED,
l[0]+1, l[1]+1, RED,
l[2]+1, l[3]+1, RED,
l[2]-1, l[3]-1, RED
)
Gosu.draw_quad(
l[0]-1, l[1]+1, RED,
l[0]+1, l[1]-1, RED,
l[2]+1, l[3]-1, RED,
l[2]-1, l[3]+1, RED
)
end
end
end
Screenshot.new().show