-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialog.c
155 lines (129 loc) · 3.26 KB
/
dialog.c
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
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "dialog.h"
size_t
render_dialogs(struct asciibuffer *asciibuffer,
char *message,
struct dialog *dialogs[],
size_t dialogs_count,
size_t option)
{
clear(asciibuffer);
size_t offsetx = 2;
size_t offsety = 2;
size_t paddingx = 2;
size_t paddingy = 2;
size_t paddingchoice = 2;
size_t paddingmessage = 2;
size_t y = offsety;
size_t x = offsetx;
// Add top border
for (x = offsetx;
x < asciibuffer->width - offsetx;
++x)
{
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
}
++y;
for (size_t i = 1;
i < paddingy;
++i, ++y)
{
// Add left border
x = offsetx;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
//Add right border
x = asciibuffer->width - offsetx - 1;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
}
// Add message
// Add left border
x = offsetx;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
// Add text
size_t j;
for (j = 0, x += paddingx + paddingchoice;
message[j] != '\0' &&
x < asciibuffer->width - offsetx - paddingx + paddingchoice;
++x, ++j)
{
*index_gray((struct imagebuffer *) asciibuffer, x, y) = message[j];
}
//Add right border
x = asciibuffer->width - offsetx - 1;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
++y;
// Add message-response padding
for (size_t i = 1;
i < paddingmessage;
++i, ++y)
{
// Add left border
x = offsetx;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
//Add right border
x = asciibuffer->width - offsetx - 1;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
}
// Add responses
for (size_t i = 0;
i < dialogs_count;
++i, ++y)
{
// Add left border
x = offsetx;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
// Add bullet
if (i == option)
{
*index_gray((struct imagebuffer *) asciibuffer, x + paddingx , y) = '*';
}
// Add text
size_t j;
for (j = 0, x += paddingx + paddingchoice;
dialogs[i]->message[j] != '\0' &&
x < asciibuffer->width - offsetx - paddingx + paddingchoice;
++x, ++j)
{
*index_gray((struct imagebuffer *) asciibuffer, x, y) = dialogs[i]->message[j];
}
//Add right border
x = asciibuffer->width - offsetx - 1;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
}
// Add bottom border
for (size_t i = 1;
i < paddingy;
++i, ++y)
{
// Add left border
x = offsetx;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
//Add right border
x = asciibuffer->width - offsetx - 1;
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
}
for (x = offsetx;
x < asciibuffer->width - offsetx;
++x)
{
*index_gray((struct imagebuffer *) asciibuffer, x, y) = '+';
}
y += offsety;
flatten(asciibuffer);
asciibuffer->height = y;
return y;
}
struct dialog *
make_dialog(struct slide *next,
char *message)
{
int n = strlen(message);
struct dialog *dialog = malloc(sizeof *dialog + n + 1);
*dialog = (struct dialog) {
.next = next,
.message = message
};
return dialog;
}