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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use eframe::{egui::*};
use eframe::egui;
use epaint::text::cursor::Cursor;
use epaint::{Color32, text::{LayoutJob, TextFormat}, FontFamily, FontId};
pub struct ClipTextEdit {
pub text: Vec<String>,
row_index: Option<usize>,
cursor: Option<Cursor>,
}
impl ClipTextEdit {
pub fn new(text: String) -> Self{
Self {
text: text.split('\n').map(|s| s.to_string()).collect(),
row_index: None,
cursor: None,
}
}
}
impl ClipTextEdit {
pub fn get_now_row(self)->Option<usize>{
return self.row_index;
}
pub fn get_now_cursor(self)->Option<Cursor>{
return self.cursor;
}
pub fn get_now_cursor_pos(self)->Option<usize>{
match self.cursor{
None=> {return None},
Some(c)=>{
return Some(c.ccursor.index)
}
}
}
pub fn get_text(self)->String{
return self.text.join("\n")
}
}
impl ClipTextEdit {
pub fn load_text(&mut self, text: String){
self.text = text.split('\n').map(|s| s.to_string()).collect();
}
fn update_text(&mut self, input_text: Vec<&String>){
input_text.iter().for_each(|t| {
//if let Some(c) = self.cursor {
if let (Some(ri) , Some(c)) = (self.row_index, self.cursor) {
let mut c = c;
self.text[ri].insert_str(c.ccursor.index, t);
c.ccursor.index += 1;
self.cursor = Some(c); //Some(galley.from_ccursor(c.ccursor));
}
});
}
fn event_key(&mut self, key: &Key){
if key == &Key::Backspace {
//if let Some(c) = self.cursor {
if let (Some(ri) , Some(c)) = (self.row_index, self.cursor) {
let mut c = c;
if c.ccursor.index > 0 {
let cindex = c.ccursor.index - 1;
self.text[ri].remove(cindex);
c.ccursor.index -= 1;
self.cursor = Some(c); //Some(galley.from_ccursor(c.ccursor));
}else if c.ccursor.index == 0 && ri > 0{
let row_text = self.text[ri].clone();
let add_idx = self.text[ri-1].len();
self.text[ri-1].insert_str(add_idx, &row_text);
self.row_index = Some(ri - 1);
c.ccursor.index = add_idx; //+ row_text.len();
self.cursor = Some(c);
self.text.remove(ri);
}
}
}else if key == &Key::Enter {
if let (Some(ri) , Some(c)) = (self.row_index, self.cursor) {
let mut c = c;
let mut row_text = self.text[ri].clone();
let new_row_text = row_text.split_off(c.ccursor.index);
self.text[ri] = row_text;
self.text.insert(ri+1, new_row_text);
c.ccursor.index = 0;
self.cursor = Some(c);
self.row_index = Some(ri + 1);
}
}
}
fn paint_cursor(&mut self, ui: &mut Ui, row_height: f32, painter:&Painter, pos: Pos2, galley: &Galley, row_index:usize ,cursor: &Cursor){
let stroke = ui.visuals().selection.stroke;
let mut cursor_pos = galley.pos_from_cursor(cursor).translate(pos.to_vec2());
cursor_pos.max.y = cursor_pos.max.y.at_least(cursor_pos.min.y + row_height); // Handle completely empty galleys
cursor_pos = cursor_pos.expand(1.5); // slightly above/below row
let top = cursor_pos.center_top();
let bottom = cursor_pos.center_bottom();
painter.line_segment(
[top, bottom],
(ui.visuals().text_cursor_width, stroke.color),
);
}
}
impl ClipTextEdit {
fn events(&mut self, ui: &mut Ui){
let events = ui.input(|i| i.events.clone());
let mut input_text: Vec<&String> = vec![];
for event in events.iter() {
match event {
Event::Text(t) => input_text.push(t),
Event::Key {
key: Key::Backspace,
pressed: true,
modifiers:_,
..
} => {
if let (Some(ri) , Some(c)) = (self.row_index, self.cursor) {
let mut c = c;
if c.ccursor.index > 0 {
let cindex = c.ccursor.index - 1;
self.text[ri].remove(cindex);
c.ccursor.index -= 1;
self.cursor = Some(c); //Some(galley.from_ccursor(c.ccursor));
}else if c.ccursor.index == 0 && ri > 0{
let row_text = self.text[ri].clone();
let add_idx = self.text[ri-1].len();
self.text[ri-1].insert_str(add_idx, &row_text);
self.row_index = Some(ri - 1);
c.ccursor.index = add_idx; //+ row_text.len();
self.cursor = Some(c);
self.text.remove(ri);
}
}
},
Event::Key {
key: Key::Enter,
pressed: true,
modifiers:_,
..
} => {
if let (Some(ri) , Some(c)) = (self.row_index, self.cursor) {
let mut c = c;
let mut row_text = self.text[ri].clone();
let new_row_text = row_text.split_off(c.ccursor.index);
self.text[ri] = row_text;
self.text.insert(ri+1, new_row_text);
c.ccursor.index = 0;
self.cursor = Some(c);
self.row_index = Some(ri + 1);
}
},
_ => {},
}
}
if input_text.len() >= 1 {
println!("Input text len: {}", input_text.len());
self.update_text(input_text);
}
}
}
impl ClipTextEdit {
pub fn show_editor(&mut self, ui: &mut Ui, allocate_max_rect: Rect){
ui.allocate_ui_at_rect(
allocate_max_rect,
|ui|{
//エディタの外周
let outer_rect = ui.available_rect_before_wrap();
ui.painter().rect_filled(
outer_rect,
2.0, // curve
Color32::from_rgb(48, 48, 48) // color
);
//フォントの設定
let fontid = FontId::new(16.0, FontFamily::Monospace);
//フォントの高さ
let row_height = ui.fonts(|f| f.row_height(&fontid));
//行数を取得
let total_rows = self.text.len();
//layouterの設定
const MIN_WIDTH:f32 = 24.0;
let available_width = ui.available_width().at_least(MIN_WIDTH);
let layouter_font_id = fontid.clone();
let mut default_layouter = move |ui: &Ui, text: &str, wrap_width: f32| {
let layout_job = LayoutJob::simple_singleline(text.to_string(), layouter_font_id.clone(), Color32::from_rgba_premultiplied(100, 100, 100, 100));
ui.fonts(|f| f.layout_job(layout_job))
};
let layouter = &mut default_layouter;
let output = egui::ScrollArea::both()
//.max_width(f32::INFINITY)
//.stick_to_bottom(true)
.auto_shrink([false; 2])
.show_rows(ui, row_height, total_rows, |ui, row_range| {
//EVENT
let ctx = ui.ctx();
let evts = ctx.input(|i| i.events.clone());
let mut input_text: Vec<&String> = vec![];
for ev in evts.iter() {
match ev {
Event::Text(t) => input_text.push(t),
Event::Key {key, pressed, modifiers:_, ..} => {
if pressed == &true { self.event_key(key);}
},
_ => {},
}
}
if input_text.len() >= 1 {
println!("Input text len: {}", input_text.len());
self.update_text(input_text);
}
let mut row_number_width = 0.0; //row_number_galley.mesh_bounds.max.x - row_number_galley.mesh_bounds.min.x;
let row_number_enable = true;
if row_number_enable {
let _row_num_galley = layouter(ui, &self.text.len().to_string(), available_width);
row_number_width = _row_num_galley.mesh_bounds.max.x - _row_num_galley.mesh_bounds.min.x + 10.0;
//println!("galley: min:{:?}, max:{:?}", row_number_galley.mesh_bounds.min, row_number_galley.mesh_bounds.max);
}
for row in row_range {
if row < self.text.len() {
ui.horizontal(|ui|{
let mut galley = layouter(ui, &self.text[row], available_width);
let mut row_rect = ui.available_rect_before_wrap();
row_rect.set_height(galley.size().y.max(row_height));
//fill row
ui.painter().rect_filled(
row_rect,
0.0, // curve
Color32::from_rgb(55,55, 55) // color
);
if let (Some(ri) , Some(c)) = (self.row_index, self.cursor) {
if ri == row{
self.cursor = Some(galley.from_ccursor(c.ccursor));
}
}
if row_number_enable {
let mut row_num_galley = layouter(ui, &(row+1).to_string(), available_width);
let desired_width = row_number_width;
let desired_height = row_height;
let desired_size = vec2(desired_width, galley.size().y.max(desired_height));//.at_least(min_size - margin * 2.0);
let (id, rect) = ui.allocate_space(desired_size);
//paint row back ground
let row_number_rect = Rect::from_min_max(row_rect.min, Pos2::new(row_rect.min.x+desired_width, row_rect.max.y));
//fill
ui.painter().rect_filled(
row_number_rect,
0.0, // curve
Color32::from_rgb(0, 50,0) // color
);
let painter = ui.painter_at(rect);
painter.galley(rect.min, row_num_galley.clone());
}
//let desired_width = available_width - row_number_width;
let desired_width = galley.size().x;
let desired_height = row_height;
let desired_size = vec2(desired_width, galley.size().y.max(desired_height));//.at_least(min_size - margin * 2.0);
let (id, rect) = ui.allocate_space(desired_size);
let painter = ui.painter_at(rect);
let sense = Sense::click();
let mut response = ui.interact(rect, id, sense);
if response.clicked() {
if let Some(click_pos) = response.interact_pointer_pos() {
let pos = click_pos - rect.min;
let c = galley.cursor_from_pos(pos);
println!("Click index: {:?}, Row: {:?}", c.ccursor.index, row);
self.row_index = Some(row);
self.cursor = Some(c);
}
}
painter.galley(rect.min, galley.clone());
if let (Some(ri) , Some(c)) = (self.row_index, self.cursor) {
if ri == row {
self.paint_cursor(ui, row_height, &painter, rect.min, &galley, ri, &c);
}
}
//let mut layout_job = LayoutJob::simple(self.input_text[row].clone(), FontId::new(16.0, FontFamily::Monospace) , Color32::GREEN, 100.0);
//layout_job.halign = Align::LEFT;
////ui.label(self.input_text[row].clone());
//ui.label(layout_job);
});
}
}
});
}
);
}
}