-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_draw_text.cr
More file actions
96 lines (77 loc) · 2.58 KB
/
basic_draw_text.cr
File metadata and controls
96 lines (77 loc) · 2.58 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
require "../../src/uing"
UIng.init
handler = UIng::Area::Handler.new
area = UIng::Area.new(handler)
title = "Michael Ende (1929-1995) The Neverending Story"
str1 = \
" At last Ygramul sensed that something was coming toward " \
"her. With the speed of lightning, she turned about, confronting " \
"Atreyu with an enormous steel-blue face. Her single eye had a " \
"vertical pupil, which stared at Atreyu with inconceivable malignancy. "
str2 = \
" A cry of fear escaped Bastian. "
str3 = \
" A cry of terror passed through the ravine and echoed from " \
"side to side. Ygramul turned her eye to left and right, to see if " \
"someone else had arrived, for that sound could not have been " \
"made by the boy who stood there as though paralyzed with " \
"horror. "
str4 = \
" Could she have heard my cry? Bastion wondered in alarm. " \
"But that's not possible. "
str5 = \
" And then Atreyu heard Ygramuls voice. It was very high " \
"and slightly hoarse, not at all the right kind of voice for that " \
"enormous face. Her lips did not move as she spoke. It was the " \
"buzzing of a great swarm of hornets that shaped itself into " \
"words. "
ATTR_STR = UIng::Area::AttributedString.new("")
RED_COLOR = {0.0, 0.5, 0.0, 0.7}
GREEN_COLOR = {0.5, 0.0, 0.25, 0.7}
DEFAULT_FONT = UIng::FontDescriptor.new(
family: "Georgia",
size: 13,
weight: :normal,
italic: :normal,
stretch: :normal
)
def append_to_attr_str(attr_str, text, color)
start = attr_str.len
attr_str.append_unattributed(text)
attribute = UIng::Area::Attribute.new_color(*color)
attr_str.set_attribute(attribute, start, start + text.bytesize)
attr_str.append_unattributed("\n\n")
end
append_to_attr_str(ATTR_STR, str1, GREEN_COLOR)
append_to_attr_str(ATTR_STR, str2, RED_COLOR)
append_to_attr_str(ATTR_STR, str3, GREEN_COLOR)
append_to_attr_str(ATTR_STR, str4, RED_COLOR)
append_to_attr_str(ATTR_STR, str5, GREEN_COLOR)
handler.draw do |area, params|
UIng::Area::Draw::TextLayout.open(
string: ATTR_STR,
default_font: DEFAULT_FONT,
width: params.area_width,
align: UIng::Area::Draw::TextAlign::Left
) do |text_layout|
params.context.draw_text_layout(text_layout, 0, 0)
end
end
handler.mouse_event { |_, _| }
handler.mouse_crossed { |_, _| }
handler.drag_broken { |_| }
handler.key_event { |_, _| false }
box = UIng::Box.new(:vertical)
box.padded = true
box.append area, true
main_window = UIng::Window.new(title, 765, 430)
main_window.margined = true
main_window.child = box
main_window.on_closing do
ATTR_STR.free
UIng.quit
true
end
main_window.show
UIng.main
UIng.uninit