-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtext.rb
More file actions
105 lines (87 loc) · 3.2 KB
/
text.rb
File metadata and controls
105 lines (87 loc) · 3.2 KB
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
module Input
class Text < Base
def draw_override(ffi)
ffi.draw_sprite_3(@x, @y, @source_w, @h, @path, 0, 255, 255, 255, 255, nil, nil, nil, nil, false, false, 0, 0, @source_x, 0, @source_w, @h)
super # handles focus and draws the cursor
end
def handle_keyboard
end
# TODO: Word selection (double click), All selection (triple click)
def handle_mouse
mouse = $args.inputs.mouse
if !@mouse_down && mouse.down && mouse.inside_rect?(self)
@on_clicked.call(mouse, self)
return unless @focussed || @will_focus
@mouse_down = true
index = find_index_at_x(mouse.x - @x + @source_x)
if @shift
@selection_end = index
else
@selection_start = @selection_end = index
end
elsif @mouse_down
index = find_index_at_x(mouse.x - @x + @source_x) # TODO: handle scrolling to the right with mouse
@selection_end = index
@mouse_down = false if mouse.up
end
end
def find_index_at_x(x, str = @value)
return 0 if x < @padding
index = -1
width = 0
while (index += 1) < str.length
char_w = $gtk.calcstringbox(str[index, 1].to_s, @size_enum, @font)[0]
return index if width + char_w / 2 > x
return index + 1 if width + char_w > x
width += char_w
end
index
end
def prepare_render_target
# TODO: handle padding correctly
if @focussed || @will_focus
bg = @background_color
sc = @selection_color
else
bg = @blurred_background_color
sc = @blurred_selection_color
end
@content_w = $gtk.calcstringbox(@value, @size_enum, @font)[0].ceil
rt = $args.outputs[@path]
rt.w = @content_w
rt.h = @h
rt.background_color = bg
# TODO: implement sprite background
rt.transient!
# SELECTION
if @selection_start != @selection_end
if @selection_start < @selection_end
left, = $gtk.calcstringbox(@value[0, @selection_start].to_s, @size_enum, @font)
right, = $gtk.calcstringbox(@value[0, @selection_end].to_s, @size_enum, @font)
elsif @selection_start > @selection_end
left, = $gtk.calcstringbox(@value[0, @selection_end].to_s, @size_enum, @font)
right, = $gtk.calcstringbox(@value[0, @selection_start].to_s, @size_enum, @font)
end
rt.primitives << { x: left, y: @padding, w: right - left, h: @font_height + @padding * 2 }.solid!(sc)
end
# TEXT
rt.primitives << { x: 0, y: @padding, text: @value, size_enum: @size_enum, font: @font }.label!(@text_color)
# CURSOR LOCATION
@cursor_x = $gtk.calcstringbox(@value[0, @selection_end].to_s, @size_enum, @font)[0]
@cursor_y = 0
draw_cursor(rt)
@source_w = @content_w < @w ? @content_w : @w
if @source_w < @w
@source_x = 0
else
relative_cursor_x = cursor_x - @source_x
if relative_cursor_x <= 0
@source_x = @cursor_x.greater(0)
elsif relative_cursor_x > @w
@source_x = (@cursor_x - @w).lesser(@content_w - @w)
end
end
@source_x = @content_w - @w if @content_w - @source_x < @w && @content_w > @w
end
end
end