moved space key into text input

This commit is contained in:
vurtun 2015-10-13 13:03:30 +02:00
parent 7223a1ee23
commit bf426606f3
6 changed files with 59 additions and 56 deletions

View File

@ -184,8 +184,6 @@ key(struct zr_input *in, SDL_Event *evt, zr_bool down)
zr_input_key(in, ZR_KEY_DEL, down);
else if (sym == SDLK_RETURN)
zr_input_key(in, ZR_KEY_ENTER, down);
else if (sym == SDLK_SPACE)
zr_input_key(in, ZR_KEY_SPACE, down);
else if (sym == SDLK_BACKSPACE)
zr_input_key(in, ZR_KEY_BACKSPACE, down);
else if (sym == SDLK_LEFT)

View File

@ -34,6 +34,8 @@
/* macros */
#define DTIME 33
#define MAX_DRAW_COMMAND_MEMORY (4 * 1024)
#define MAX_VERTEX_MEMORY 128 * 1024
#define MAX_ELEMENT_MEMORY 32 * 1024
#include "../../zahnrad.h"
@ -314,8 +316,6 @@ device_draw(struct device *dev, struct zr_command_queue *queue, int width, int h
/* convert from command queue into draw list and draw to screen */
struct zr_draw_list draw_list;
const struct zr_draw_command *cmd;
static const GLsizeiptr max_vertex_memory = 128 * 1024;
static const GLsizeiptr max_element_memory = 32 * 1024;
void *vertexes, *elements;
const zr_draw_index *offset = NULL;
@ -325,16 +325,16 @@ device_draw(struct device *dev, struct zr_command_queue *queue, int width, int h
glBindBuffer(GL_ARRAY_BUFFER, dev->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, dev->ebo);
glBufferData(GL_ARRAY_BUFFER, max_vertex_memory, NULL, GL_STREAM_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, max_element_memory, NULL, GL_STREAM_DRAW);
glBufferData(GL_ARRAY_BUFFER, MAX_VERTEX_MEMORY, NULL, GL_STREAM_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_ELEMENT_MEMORY, NULL, GL_STREAM_DRAW);
/* load draw vertexes & elements directly into vertex + element buffer */
vertexes = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
{
struct zr_buffer vbuf, ebuf;
zr_buffer_init_fixed(&vbuf, vertexes, (zr_size)max_vertex_memory);
zr_buffer_init_fixed(&ebuf, elements, (zr_size)max_element_memory);
zr_buffer_init_fixed(&vbuf, vertexes, (zr_size)MAX_VERTEX_MEMORY);
zr_buffer_init_fixed(&ebuf, elements, (zr_size)MAX_ELEMENT_MEMORY);
zr_draw_list_init(&draw_list, &dev->cmds, &vbuf, &ebuf,
fsin, fcos, dev->null, ZR_ANTI_ALIASING_ON);
zr_draw_list_load(&draw_list, queue, 1.0f, 22);
@ -366,7 +366,7 @@ device_draw(struct device *dev, struct zr_command_queue *queue, int width, int h
}
static void
key(struct zr_input *in, SDL_Event *evt, zr_bool down)
input_key(struct zr_input *in, SDL_Event *evt, zr_bool down)
{
const Uint8* state = SDL_GetKeyboardState(NULL);
SDL_Keycode sym = evt->key.keysym.sym;
@ -376,8 +376,6 @@ key(struct zr_input *in, SDL_Event *evt, zr_bool down)
zr_input_key(in, ZR_KEY_DEL, down);
else if (sym == SDLK_RETURN)
zr_input_key(in, ZR_KEY_ENTER, down);
else if (sym == SDLK_SPACE)
zr_input_key(in, ZR_KEY_SPACE, down);
else if (sym == SDLK_BACKSPACE)
zr_input_key(in, ZR_KEY_BACKSPACE, down);
else if (sym == SDLK_LEFT)
@ -393,7 +391,7 @@ key(struct zr_input *in, SDL_Event *evt, zr_bool down)
}
static void
motion(struct zr_input *in, SDL_Event *evt)
input_motion(struct zr_input *in, SDL_Event *evt)
{
const zr_int x = evt->motion.x;
const zr_int y = evt->motion.y;
@ -401,7 +399,7 @@ motion(struct zr_input *in, SDL_Event *evt)
}
static void
btn(struct zr_input *in, SDL_Event *evt, zr_bool down)
input_button(struct zr_input *in, SDL_Event *evt, zr_bool down)
{
const zr_int x = evt->button.x;
const zr_int y = evt->button.y;
@ -412,7 +410,7 @@ btn(struct zr_input *in, SDL_Event *evt, zr_bool down)
}
static void
text(struct zr_input *in, SDL_Event *evt)
input_text(struct zr_input *in, SDL_Event *evt)
{
zr_glyph glyph;
memcpy(glyph, evt->text.text, ZR_UTF_SIZE);
@ -426,7 +424,6 @@ resize(SDL_Event *evt)
glViewport(0, 0, evt->window.data1, evt->window.data2);
}
static void* mem_alloc(zr_handle unused, zr_size size)
{UNUSED(unused); return calloc(1, size);}
static void mem_free(zr_handle unused, void *ptr)
@ -487,12 +484,18 @@ main(int argc, char *argv[])
while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_WINDOWEVENT) resize(&evt);
else if (evt.type == SDL_QUIT) goto cleanup;
else if (evt.type == SDL_KEYUP) key(&gui.input, &evt, zr_false);
else if (evt.type == SDL_KEYDOWN) key(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONUP) btn(&gui.input, &evt, zr_false);
else if (evt.type == SDL_MOUSEMOTION) motion(&gui.input, &evt);
else if (evt.type == SDL_TEXTINPUT) text(&gui.input, &evt);
else if (evt.type == SDL_KEYUP)
input_key(&gui.input, &evt, zr_false);
else if (evt.type == SDL_KEYDOWN)
input_key(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONDOWN)
input_button(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONUP)
input_button(&gui.input, &evt, zr_false);
else if (evt.type == SDL_MOUSEMOTION)
input_motion(&gui.input, &evt);
else if (evt.type == SDL_TEXTINPUT)
input_text(&gui.input, &evt);
else if (evt.type == SDL_MOUSEWHEEL)
zr_input_scroll(&gui.input,(float)evt.wheel.y);
}

View File

@ -318,7 +318,6 @@ input_key(struct zr_input *in, MSG *msg, zr_bool down)
case VK_SHIFT: zr_input_key(in, ZR_KEY_SHIFT, down); break;
case VK_DELETE: zr_input_key(in, ZR_KEY_DEL, down); break;
case VK_RETURN: zr_input_key(in, ZR_KEY_ENTER, down); break;
case VK_SPACE: zr_input_key(in, ZR_KEY_SPACE, down); break;
case VK_BACK: zr_input_key(in, ZR_KEY_BACKSPACE, down); break;
case VK_LEFT: zr_input_key(in, ZR_KEY_LEFT, down); break;
case VK_RIGHT: zr_input_key(in, ZR_KEY_RIGHT, down); break;

View File

@ -25,11 +25,13 @@
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
/* macros */
#define DTIME 16
#define CURVE_STEPS 22
#include "../../zahnrad.h"
#define UNUSED(a) ((void)(a))
@ -281,6 +283,8 @@ surface_draw_text(XSurface *surf, zr_short x, zr_short y, zr_ushort w, zr_ushort
XFillRectangle(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y, (unsigned)w, (unsigned)h);
if(!text || !font || !len) return;
tx = (int)x;
th = font->ascent + font->descent;
ty = (int)y + ((int)h / 2) - (th / 2) + font->ascent;
@ -356,7 +360,7 @@ draw(XSurface *surf, struct zr_command_queue *queue)
}
static void
key(struct XWindow *xw, struct zr_input *in, XEvent *evt, zr_bool down)
input_key(struct XWindow *xw, struct zr_input *in, XEvent *evt, zr_bool down)
{
int ret;
KeySym *code = XGetKeyboardMapping(xw->dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
@ -366,8 +370,8 @@ key(struct XWindow *xw, struct zr_input *in, XEvent *evt, zr_bool down)
zr_input_key(in, ZR_KEY_DEL, down);
else if (*code == XK_Return)
zr_input_key(in, ZR_KEY_ENTER, down);
else if (*code == XK_space)
zr_input_key(in, ZR_KEY_SPACE, down);
else if (*code == XK_space && !down)
zr_input_char(in, ' ');
else if (*code == XK_Left)
zr_input_key(in, ZR_KEY_LEFT, down);
else if (*code == XK_Right)
@ -387,7 +391,7 @@ key(struct XWindow *xw, struct zr_input *in, XEvent *evt, zr_bool down)
}
static void
motion(struct zr_input *in, XEvent *evt)
input_motion(struct zr_input *in, XEvent *evt)
{
const zr_int x = evt->xmotion.x;
const zr_int y = evt->xmotion.y;
@ -395,7 +399,7 @@ motion(struct zr_input *in, XEvent *evt)
}
static void
btn(struct zr_input *in, XEvent *evt, zr_bool down)
input_button(struct zr_input *in, XEvent *evt, zr_bool down)
{
const zr_int x = evt->xbutton.x;
const zr_int y = evt->xbutton.y;
@ -466,11 +470,15 @@ main(int argc, char *argv[])
zr_input_begin(&gui.input);
while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)) {
if (evt.type == KeyPress)
key(&xw, &gui.input, &evt, zr_true);
else if (evt.type == KeyRelease) key(&xw, &gui.input, &evt, zr_false);
else if (evt.type == ButtonPress) btn(&gui.input, &evt, zr_true);
else if (evt.type == ButtonRelease) btn(&gui.input, &evt, zr_false);
else if (evt.type == MotionNotify) motion(&gui.input, &evt);
input_key(&xw, &gui.input, &evt, zr_true);
else if (evt.type == KeyRelease)
input_key(&xw, &gui.input, &evt, zr_false);
else if (evt.type == ButtonPress)
input_button(&gui.input, &evt, zr_true);
else if (evt.type == ButtonRelease)
input_button(&gui.input, &evt, zr_false);
else if (evt.type == MotionNotify)
input_motion(&gui.input, &evt);
else if (evt.type == Expose || evt.type == ConfigureNotify)
resize(&xw, xw.surf);
}

View File

@ -1817,6 +1817,7 @@ zr_draw_list_clear(struct zr_draw_list *list)
zr_buffer_clear(list->vertexes);
if (list->vertexes)
zr_buffer_clear(list->elements);
list->element_count = 0;
list->vertex_count = 0;
list->cmd_offset = 0;
@ -1844,6 +1845,7 @@ zr_draw_list_begin(const struct zr_draw_list *list)
ZR_ASSERT(list);
ZR_ASSERT(list->buffer);
if (!list || !list->buffer || !list->cmd_count) return 0;
memory = (zr_byte*)list->buffer->memory.ptr;
offset = list->buffer->memory.size - list->cmd_offset;
cmd = zr_ptr_add(const struct zr_draw_command, memory, offset);
@ -1858,6 +1860,7 @@ zr_draw_list_next(const struct zr_draw_list *list, const struct zr_draw_command
const struct zr_draw_command *end;
ZR_ASSERT(list);
if (!list || !list->buffer || !list->cmd_count) return 0;
memory = (zr_byte*)list->buffer->memory.ptr;
offset = list->buffer->memory.size - list->cmd_offset;
end = zr_ptr_add(const struct zr_draw_command, memory, offset);
@ -1875,6 +1878,7 @@ zr_draw_list_alloc_path(struct zr_draw_list *list, zr_size count)
static const zr_size point_size = sizeof(struct zr_vec2);
points = (struct zr_vec2*)
zr_buffer_alloc(list->buffer, ZR_BUFFER_FRONT, point_size * count, point_align);
if (!points) return 0;
if (!list->path_offset) {
memory = zr_buffer_memory(list->buffer);
@ -1930,6 +1934,7 @@ zr_draw_list_command_last(struct zr_draw_list *list)
zr_size size;
struct zr_draw_command *cmd;
ZR_ASSERT(list->cmd_count);
memory = zr_buffer_memory(list->buffer);
size = zr_buffer_total(list->buffer);
cmd = zr_ptr_add(struct zr_draw_command, memory, size - list->cmd_offset);
@ -1971,6 +1976,7 @@ zr_draw_list_alloc_vertexes(struct zr_draw_list *list, zr_size count)
static const zr_size vtx_size = sizeof(struct zr_draw_vertex);
ZR_ASSERT(list);
if (!list) return 0;
vtx = (struct zr_draw_vertex*)
zr_buffer_alloc(list->vertexes, ZR_BUFFER_FRONT, vtx_size * count, vtx_align);
if (!vtx) return 0;
@ -1987,6 +1993,7 @@ zr_draw_list_alloc_elements(struct zr_draw_list *list, zr_size count)
static const zr_size elem_size = sizeof(zr_draw_index);
ZR_ASSERT(list);
if (!list) return 0;
ids = (zr_draw_index*)
zr_buffer_alloc(list->elements, ZR_BUFFER_FRONT, elem_size * count, elem_align);
if (!ids) return 0;
@ -2037,8 +2044,8 @@ zr_draw_list_add_poly_line(struct zr_draw_list *list, struct zr_vec2 *points,
struct zr_draw_vertex *vtx = zr_draw_list_alloc_vertexes(list, vtx_count);
zr_draw_index *ids = zr_draw_list_alloc_elements(list, idx_count);
struct zr_vec2 *normals, *temp;
zr_size size;
struct zr_vec2 *normals, *temp;
if (!vtx || !ids) return;
/* temporary allocate normals + points */
@ -2123,9 +2130,8 @@ zr_draw_list_add_poly_line(struct zr_draw_list *list, struct zr_vec2 *points,
zr_size idx1, i;
const zr_float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
if (!closed) {
struct zr_vec2 d1, d2;
d1 = zr_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
d2 = zr_vec2_muls(normals[0], half_inner_thickness);
struct zr_vec2 d1 = zr_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
struct zr_vec2 d2 = zr_vec2_muls(normals[0], half_inner_thickness);
temp[0] = zr_vec2_add(points[0], d1);
temp[1] = zr_vec2_add(points[0], d2);
@ -2144,14 +2150,13 @@ zr_draw_list_add_poly_line(struct zr_draw_list *list, struct zr_vec2 *points,
/* add all elements */
idx1 = index;
for (i1 = 0; i1 < count; ++i1) {
zr_float dmr2;
struct zr_vec2 dm_out, dm_in, dm;
struct zr_vec2 dm_out, dm_in;
const zr_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
zr_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
/* average normals */
dm = zr_vec2_muls(zr_vec2_add(normals[i1], normals[i2]), 0.5f);
dmr2 = dm.x * dm.x + dm.y* dm.y;
struct zr_vec2 dm = zr_vec2_muls(zr_vec2_add(normals[i1], normals[i2]), 0.5f);
zr_float dmr2 = dm.x * dm.x + dm.y* dm.y;
if (dmr2 > 0.000001f) {
zr_float scale = 1.0f/dmr2;
scale = MIN(100.0f, scale);
@ -3216,14 +3221,14 @@ zr_font_text_width(zr_handle handle, const zr_char *text, zr_size len)
struct zr_font *font;
zr_size text_len = 0;
zr_size text_width = 0;
zr_size glyph_len;
zr_size glyph_len = 0;
font = (struct zr_font*)handle.ptr;
ZR_ASSERT(font);
if (!font || !text || !len)
return 0;
glyph_len = zr_utf_decode(text, &unicode, len);
while (text_len < len && glyph_len) {
while (text_len < len) {
if (unicode == ZR_UTF_INVALID) return 0;
glyph = zr_font_find_glyph(font, unicode);
text_len += glyph_len;
@ -4216,11 +4221,6 @@ zr_widget_editbox(struct zr_command_buffer *out, struct zr_rect r,
zr_edit_box_remove(box);
if (zr_input_is_key_pressed(in, ZR_KEY_ENTER))
box->active = zr_false;
if (zr_input_is_key_pressed(in, ZR_KEY_SPACE)) {
if (diff && box->cursor != box->glyphes)
zr_edit_box_remove(box);
zr_edit_box_add(box, " ", 1);
}
if (in->keyboard.text_len) {
if (diff && box->cursor != box->glyphes) {
/* replace text selection */
@ -5529,7 +5529,6 @@ zr_queue(struct zr_context *layout)
struct zr_rect
zr_space(struct zr_context *layout)
{ZR_ASSERT(layout); return layout->clip;}
/*
* -------------------------------------------------------------
*
@ -6055,7 +6054,6 @@ zr_layout_row(struct zr_context *layout, enum zr_layout_format fmt,
layout->row.item_width = 0;
layout->row.item_offset = 0;
}
layout->row.item_offset = 0;
layout->row.filled = 0;
}
@ -6187,6 +6185,7 @@ zr_layout_row_tiled_begin(struct zr_context *layout,
config = layout->style;
padding = config->properties[ZR_PROPERTY_PADDING];
spacing = config->properties[ZR_PROPERTY_ITEM_SPACING];
tiled->spacing = spacing;
tiled->bounds.x = layout->at_x + padding.x - layout->offset.x;
tiled->bounds.y = layout->at_y - layout->offset.y;
@ -6234,8 +6233,7 @@ zr_layout_widget_space(struct zr_rect *bounds, struct zr_context *layout,
const struct zr_style *config;
zr_float panel_padding, panel_spacing, panel_space;
zr_float item_offset = 0, item_width = 0, item_spacing = 0;
struct zr_vec2 spacing;
struct zr_vec2 padding;
struct zr_vec2 spacing, padding;
ZR_ASSERT(layout);
ZR_ASSERT(layout->style);
@ -6248,7 +6246,6 @@ zr_layout_widget_space(struct zr_rect *bounds, struct zr_context *layout,
spacing = zr_style_property(config, ZR_PROPERTY_ITEM_SPACING);
padding = zr_style_property(config, ZR_PROPERTY_PADDING);
/* calculate the useable panel space */
panel_padding = 2 * padding.x;
panel_spacing = (zr_float)(layout->row.columns - 1) * spacing.x;
@ -7132,7 +7129,6 @@ zr_spinner(struct zr_context *layout, zr_int min, zr_int value,
return zr_widget_spinner(out, bounds, &spinner, min, value, max, step, active,
i, &layout->style->font);
}
/*
* -------------------------------------------------------------
*

View File

@ -244,7 +244,6 @@ enum zr_keys {
ZR_KEY_DEL,
ZR_KEY_ENTER,
ZR_KEY_BACKSPACE,
ZR_KEY_SPACE,
ZR_KEY_COPY,
ZR_KEY_CUT,
ZR_KEY_PASTE,
@ -1080,9 +1079,9 @@ typedef zr_float(*zr_cos_f)(zr_float);
enum zr_anti_aliasing {
ZR_ANTI_ALIASING_OFF = zr_false,
/* renderes all primitives without anit-aliasing */
/* renderes all primitives without anti-aliasing */
ZR_ANTI_ALIASING_ON
/* renderes all primitives with anit-aliasing */
/* renderes all primitives with anti-aliasing */
};
struct zr_draw_vertex {