Skip to content

Commit c12feb7

Browse files
committed
add gui
1 parent adbae56 commit c12feb7

3 files changed

Lines changed: 49 additions & 23 deletions

File tree

lib/libgui/edit.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
11
#include "edit.h"
22

3-
font_t *new_font(char *name, float size) {
4-
font_t *font = malloc(sizeof(font_t));
5-
font->name = name;
6-
font->size = size;
7-
font->stash = sth_create(512, 512);
8-
font->id = sth_add_font(font->stash, name);
9-
// sth_add_font_from_memory(self->stash, self->data);
10-
return font;
11-
}
12-
13-
void destroy_font(font_t *font) {
14-
if (font == NULL) return;
15-
sth_delete(font->stash);
16-
free(font);
17-
font = NULL;
18-
}
19-
203
buffer_t *new_buffer() {
214
buffer_t *buffer = malloc(sizeof(buffer_t));
225
buffer->lines = new_lines(LINE_SIZE);
@@ -527,12 +510,6 @@ void gl_render_edit(edit_t *self, float x, float y) {
527510
}
528511
}
529512

530-
float measure_text(font_t *font, char *text, int count) {
531-
float dx, dy;
532-
sth_measure(font->stash, font->id, font->size, -1, text, count, &dx, &dy);
533-
return dx;
534-
}
535-
536513
void update_cursor_pos(buffer_t *self) {
537514
int cur_row = self->cursor_row;
538515
if (cur_row < 0 || self->line_count <= 0) {
@@ -549,6 +526,15 @@ void update_cursor_pos(buffer_t *self) {
549526
measure_text(self->font, self->lines[cur_row]->texts, self->cursor_col);
550527
}
551528

529+
float gl_edit_get_cursor_x(edit_t *self) {
530+
return self->buffer->cursor_x / self->scale +
531+
self->lineno_width / self->scale;
532+
}
533+
534+
float gl_edit_get_cursor_y(edit_t *self) {
535+
return self->buffer->cursor_y/self->scale+ self->scroll_y ;
536+
}
537+
552538
int gl_edit_get_line_count(edit_t *self) { return self->buffer->line_count; }
553539

554540
int gl_edit_get_row_count(edit_t *self, int row) {
@@ -1037,6 +1023,9 @@ edit_t *gl_new_edit(int shader, float w, float h, float width, float height) {
10371023
return self;
10381024
}
10391025

1026+
font_t * gl_edit_get_font(edit_t *self){
1027+
return self->font;
1028+
}
10401029
void gl_resize_edit_window(edit_t *self, float width, float height) {
10411030
glUseProgram(self->mvp.shader);
10421031
mat4_set_orthographic(&self->mvp.projection, 0, width * self->scale,
@@ -1106,6 +1095,13 @@ char *gl_get_edit_text(edit_t *self) {
11061095
return self->texts;
11071096
}
11081097

1098+
float gl_edit_measure_text(edit_t *self){
1099+
if(self->texts==NULL){
1100+
gl_get_edit_text(self);
1101+
}
1102+
return measure_text(self->buffer->font, self->texts, strlen(self->texts));
1103+
}
1104+
11091105
void buffer_color(buffer_t *buffer, void *colors) {
11101106
int *text_colors = colors;
11111107
int count = 0;

lib/libgui/gui.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ font_t* font_create(char* font_name) {
5555
return self;
5656
}
5757

58+
font_t *new_font(char *name, float size) {
59+
font_t *font = malloc(sizeof(font_t));
60+
font->name = name;
61+
font->size = size;
62+
font->stash = sth_create(512, 512);
63+
font->id = sth_add_font(font->stash, name);
64+
// sth_add_font_from_memory(self->stash, self->data);
65+
return font;
66+
}
67+
68+
void destroy_font(font_t *font) {
69+
if (font == NULL) return;
70+
sth_delete(font->stash);
71+
free(font);
72+
font = NULL;
73+
}
74+
75+
float measure_text(font_t *font, char *text, int count) {
76+
float dx, dy;
77+
if(count<0){
78+
count=strlen(text);
79+
}
80+
sth_measure(font->stash, font->id, font->size, -1, text, count, &dx, &dy);
81+
//printf("measure_text=> font=%p %s count=%d width=%f\n", font,text,count,dx);
82+
return dx;
83+
}
84+
5885
long get_time() {
5986
struct timeval tv;
6087
gettimeofday(&tv, NULL);

lib/libgui/gui.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ typedef struct {
5050
unsigned char *data;
5151
} font_t;
5252

53+
float measure_text(font_t *font, char *text, int count);
54+
font_t *new_font(char *name, float size);
55+
void destroy_font(font_t *font);
5356
font_t *font_create(char *font_name);
5457
void glShaderSource2(GLuint shader, GLsizei count, const GLchar *string,
5558
const GLint *length);

0 commit comments

Comments
 (0)