static void textinput_render(component *c) { textinput *tb = widget_get_obj(c); int chars = strlen(tb->buf); if(tb->bg_enabled) { video_render_sprite(&tb->sur, c->x + (c->w - tb->sur.w)/2, c->y - 2, BLEND_ALPHA, 0); } if(component_is_selected(c)) { if(chars > 0) { tb->tconf.cforeground = color_create(80, 220, 80, 255); tb->buf[chars] = '\x7F'; tb->buf[chars+1] = 0; text_render(&tb->tconf, c->x, c->y, c->w, c->h, tb->buf); tb->buf[chars] = 0; } } else if(component_is_disabled(c)) { if(chars > 0) { tb->tconf.cforeground = color_create(121, 121, 121, 255); text_render(&tb->tconf, c->x, c->y, c->w, c->h, tb->buf); } } else { if(chars > 0) { tb->tconf.cforeground = color_create(0, 121, 0, 255); text_render(&tb->tconf, c->x, c->y, c->w, c->h, tb->buf); } } if(chars == 0) { tb->tconf.cforeground = color_create(121, 121, 121, 255); text_render(&tb->tconf, c->x, c->y, c->w, c->h, tb->text); } }
void textbutton_set_text(component *c, const char* text) { textbutton *tb = widget_get_obj(c); if(tb->text) { free(tb->text); } tb->text = strdup(text); }
static void textinput_free(component *c) { textinput *tb = widget_get_obj(c); surface_free(&tb->sur); free(tb->text); free(tb->buf); free(tb); }
static void textbutton_free(component *c) { textbutton *tb = widget_get_obj(c); if(tb->border_created) { surface_free(&tb->border); } free(tb->text); free(tb); }
static int textinput_event(component *c, SDL_Event *e) { // Handle selection if (e->type == SDL_TEXTINPUT) { textinput *tb = widget_get_obj(c); size_t len = strlen(tb->buf); if (strlen(e->text.text) == 1) { // make sure it is not a unicode sequence unsigned char c = e->text.text[0]; if (c >= 32 && c <= 126) { // only allow ASCII through if (len < sizeof(tb->buf)-1) { tb->buf[len+1] = '\0'; tb->buf[len] = c; } } } } else if (e->type == SDL_KEYDOWN) { textinput *tb = widget_get_obj(c); size_t len = strlen(tb->buf); const unsigned char *state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_BACKSPACE] || state[SDL_SCANCODE_DELETE]) { if (len > 0) { tb->buf[len-1] = '\0'; } } else if(state[SDL_SCANCODE_LEFT]) { // TODO move cursor to the left } else if(state[SDL_SCANCODE_RIGHT]) { // TODO move cursor to the right } else if(state[SDL_SCANCODE_V] && state[SDL_SCANCODE_LCTRL]) { if(SDL_HasClipboardText()) { char* clip_text = SDL_GetClipboardText(); int c_size = strlen(clip_text); if((c_size + len) > sizeof(tb->buf)-1) { c_size = sizeof(tb->buf) - 1 - len; } memcpy(tb->buf + len, clip_text, c_size); len += c_size; tb->buf[len] = 0; } } } return 1; }
static void spritebutton_render(component *c) { spritebutton *sb = widget_get_obj(c); sizer *s = component_get_obj(c->parent); if(sb->active > 0) { video_render_sprite(sb->img, c->x, c->y, BLEND_ALPHA, 0); } if(sb->text) { sb->tconf.opacity = clamp(s->opacity * 255, 0, 255); text_render(&sb->tconf, c->x, c->y, c->w, c->h, sb->text); } }
static int spritebutton_action(component *c, int action) { spritebutton *sb = widget_get_obj(c); // Handle selection if(action == ACT_KICK || action == ACT_PUNCH) { sb->active = 10; if(sb->click_cb) { sb->click_cb(c, sb->userdata); } return 0; } return 1; }
static int textbutton_action(component *c, int action) { textbutton *tb = widget_get_obj(c); // Handle selection if(action == ACT_KICK || action == ACT_PUNCH) { if(tb->click_cb) { tb->click_cb(c, tb->userdata); } sound_play(20, 0.5f, 0.0f, 2.0f); return 0; } return 1; }
static void textinput_tick(component *c) { textinput *tb = widget_get_obj(c); if(!tb->dir) { tb->ticks++; } else { tb->ticks--; } if(tb->ticks > 120) { tb->dir = 1; } if(tb->ticks == 0) { tb->dir = 0; } }
void textbutton_set_border(component *c, color col) { textbutton *tb = widget_get_obj(c); tb->border_enabled = 1; tb->border_color = col; if(tb->border_created) { // destroy the old border first surface_free(&tb->border); } // create new border int chars = strlen(tb->text); int fsize = text_char_width(&tb->tconf); int width = chars * fsize; menu_background_border_create(&tb->border, width+6, fsize+3); tb->border_created = 1; }
static void textbutton_render(component *c) { textbutton *tb = widget_get_obj(c); // Select color and render if(component_is_selected(c)) { int t = tb->ticks / 2; tb->tconf.cforeground = color_create(80 - t, 220 - t*2, 80 - t, 255); } else if (component_is_disabled(c)) { tb->tconf.cforeground = color_create(121, 121, 121, 255); } else { tb->tconf.cforeground = color_create(0, 121, 0, 255); } text_render(&tb->tconf, c->x, c->y, c->w, c->h, tb->text); // Border if(tb->border_enabled) { video_render_sprite(&tb->border, c->x-2, c->y-2, BLEND_ALPHA, 0); } }
static void spritebutton_tick(component *c) { spritebutton *sb = widget_get_obj(c); if(sb->active > 0) { sb->active--; } }
static void spritebutton_free(component *c) { spritebutton *sb = widget_get_obj(c); free(sb->text); free(sb); }
void textinput_enable_background(component *c, int enabled) { textinput *tb = widget_get_obj(c); tb->bg_enabled = enabled; }
char* textinput_value(const component *c) { textinput *tb = widget_get_obj(c); return tb->buf; }
void textinput_set_max_chars(component *c, int max_chars) { textinput *tb = widget_get_obj(c); tb->buf = realloc(tb->buf, max_chars+1); tb->buf[max_chars] = 0; tb->max_chars = max_chars; }
void textbutton_remove_border(component *c) { textbutton *tb = widget_get_obj(c); tb->border_enabled = 0; }