int MENUS::ui_do_key_reader(void *id, const RECT *rect, int key) { // process static void *grabbed_id = 0; static bool mouse_released = true; int inside = ui_mouse_inside(rect); int new_key = key; if(!ui_mouse_button(0) && grabbed_id == id) mouse_released = true; if(ui_active_item() == id) { if(binder.got_key) { new_key = binder.key.key; binder.got_key = false; ui_set_active_item(0); mouse_released = false; grabbed_id = id; } } else if(ui_hot_item() == id) { if(ui_mouse_button(0) && mouse_released) { binder.take_key = true; binder.got_key = false; ui_set_active_item(id); } } if(inside) ui_set_hot_item(id); // draw if (ui_active_item() == id) ui_draw_keyselect_button(id, "???", 0, rect, 0); else { if(key == 0) ui_draw_keyselect_button(id, "", 0, rect, 0); else ui_draw_keyselect_button(id, inp_key_name(key), 0, rect, 0); } return new_key; }
vec4 MENUS::button_color_mul(const void *id) { if(ui_active_item() == id) return vec4(1,1,1,0.5f); else if(ui_hot_item() == id) return vec4(1,1,1,1.5f); return vec4(1,1,1,1); }
float MENUS::ui_do_scrollbar_h(const void *id, const RECT *rect, float current) { RECT handle; static float offset_x; ui_vsplit_l(rect, 33, &handle, 0); handle.x += (rect->w-handle.w)*current; /* logic */ float ret = current; int inside = ui_mouse_inside(&handle); if(ui_active_item() == id) { if(!ui_mouse_button(0)) ui_set_active_item(0); float min = rect->x; float max = rect->w-handle.w; float cur = ui_mouse_x()-offset_x; ret = (cur-min)/max; if(ret < 0.0f) ret = 0.0f; if(ret > 1.0f) ret = 1.0f; } else if(ui_hot_item() == id) { if(ui_mouse_button(0)) { ui_set_active_item(id); offset_x = ui_mouse_x()-handle.x; } } if(inside) ui_set_hot_item(id); // render RECT rail; ui_hmargin(rect, 5.0f, &rail); ui_draw_rect(&rail, vec4(1,1,1,0.25f), 0, 0.0f); RECT slider = handle; slider.h = rail.y-slider.y; ui_draw_rect(&slider, vec4(1,1,1,0.25f), CORNER_T, 2.5f); slider.y = rail.y+rail.h; ui_draw_rect(&slider, vec4(1,1,1,0.25f), CORNER_B, 2.5f); slider = handle; ui_margin(&slider, 5.0f, &slider); ui_draw_rect(&slider, vec4(1,1,1,0.25f)*button_color_mul(id), CORNER_ALL, 2.5f); return ret; }
void ui_do_popup_menu() { for(int i = 0; i < ui_num_popups; i++) { bool inside = ui_mouse_inside(&ui_popups[i].rect); ui_set_hot_item(&ui_popups[i].id); if(ui_active_item() == &ui_popups[i].id) { if(!ui_mouse_button(0)) { if(!inside) ui_num_popups--; ui_set_active_item(0); } } else if(ui_hot_item() == &ui_popups[i].id) { if(ui_mouse_button(0)) ui_set_active_item(&ui_popups[i].id); } int corners = CORNER_ALL; if(ui_popups[i].is_menu) corners = CORNER_R|CORNER_B; RECT r = ui_popups[i].rect; ui_draw_rect(&r, vec4(0.5f,0.5f,0.5f,0.75f), corners, 3.0f); ui_margin(&r, 1.0f, &r); ui_draw_rect(&r, vec4(0,0,0,0.75f), corners, 3.0f); ui_margin(&r, 4.0f, &r); if(ui_popups[i].func(r)) ui_num_popups--; if(inp_key_down(KEY_ESCAPE)) ui_num_popups--; } }
int MENUS::ui_do_edit_box(void *id, const RECT *rect, char *str, unsigned str_size, float font_size, bool hidden) { int inside = ui_mouse_inside(rect); int r = 0; static int at_index = 0; if(ui_last_active_item() == id) { int len = strlen(str); if (inside && ui_mouse_button(0)) { int mx_rel = (int)(ui_mouse_x() - rect->x); for (int i = 1; i <= len; i++) { if (gfx_text_width(0, font_size, str, i) + 10 > mx_rel) { at_index = i - 1; break; } if (i == len) at_index = len; } } for(int i = 0; i < num_inputevents; i++) { len = strlen(str); LINEINPUT::manipulate(inputevents[i], str, str_size, &len, &at_index); } } bool just_got_active = false; if(ui_active_item() == id) { if(!ui_mouse_button(0)) ui_set_active_item(0); } else if(ui_hot_item() == id) { if(ui_mouse_button(0)) { if (ui_last_active_item() != id) just_got_active = true; ui_set_active_item(id); } } if(inside) ui_set_hot_item(id); RECT textbox = *rect; ui_draw_rect(&textbox, vec4(1,1,1,0.5f), CORNER_ALL, 5.0f); ui_vmargin(&textbox, 5.0f, &textbox); const char *display_str = str; char stars[128]; if(hidden) { unsigned s = strlen(str); if(s >= sizeof(stars)) s = sizeof(stars)-1; memset(stars, '*', s); stars[s] = 0; display_str = stars; } ui_do_label(&textbox, display_str, font_size, -1); if (ui_last_active_item() == id && !just_got_active) { float w = gfx_text_width(0, font_size, display_str, at_index); textbox.x += w*ui_scale(); ui_do_label(&textbox, "_", font_size, -1); } return r; }
void MENUS::on_render() { /* // text rendering test stuff render_background(); TEXT_CURSOR cursor; gfx_text_set_cursor(&cursor, 10, 10, 20, TEXTFLAG_RENDER); gfx_text_ex(&cursor, "ようこそ - ガイド", -1); gfx_text_set_cursor(&cursor, 10, 30, 15, TEXTFLAG_RENDER); gfx_text_ex(&cursor, "ようこそ - ガイド", -1); //gfx_texture_set(-1); gfx_quads_begin(); gfx_quads_drawTL(60, 60, 5000, 5000); gfx_quads_end(); return;*/ if(client_state() != CLIENTSTATE_ONLINE && client_state() != CLIENTSTATE_DEMOPLAYBACK) set_active(true); if(client_state() == CLIENTSTATE_DEMOPLAYBACK) { RECT screen = *ui_screen(); gfx_mapscreen(screen.x, screen.y, screen.w, screen.h); render_demoplayer(screen); } if(client_state() == CLIENTSTATE_ONLINE && gameclient.servermode == gameclient.SERVERMODE_PUREMOD) { client_disconnect(); set_active(true); popup = POPUP_PURE; } if(!is_active()) { escape_pressed = false; enter_pressed = false; num_inputevents = 0; return; } // update colors vec3 rgb = hsl_to_rgb(vec3(config.ui_color_hue/255.0f, config.ui_color_sat/255.0f, config.ui_color_lht/255.0f)); gui_color = vec4(rgb.r, rgb.g, rgb.b, config.ui_color_alpha/255.0f); color_tabbar_inactive_outgame = vec4(0,0,0,0.25f); color_tabbar_active_outgame = vec4(0,0,0,0.5f); float color_ingame_scale_i = 0.5f; float color_ingame_scale_a = 0.2f; color_tabbar_inactive_ingame = vec4( gui_color.r*color_ingame_scale_i, gui_color.g*color_ingame_scale_i, gui_color.b*color_ingame_scale_i, gui_color.a*0.8f); color_tabbar_active_ingame = vec4( gui_color.r*color_ingame_scale_a, gui_color.g*color_ingame_scale_a, gui_color.b*color_ingame_scale_a, gui_color.a); // update the ui RECT *screen = ui_screen(); float mx = (mouse_pos.x/(float)gfx_screenwidth())*screen->w; float my = (mouse_pos.y/(float)gfx_screenheight())*screen->h; int buttons = 0; if(inp_key_pressed(KEY_MOUSE_1)) buttons |= 1; if(inp_key_pressed(KEY_MOUSE_2)) buttons |= 2; if(inp_key_pressed(KEY_MOUSE_3)) buttons |= 4; ui_update(mx,my,mx*3.0f,my*3.0f,buttons); // render if(client_state() != CLIENTSTATE_DEMOPLAYBACK) render(); // render cursor gfx_texture_set(data->images[IMAGE_CURSOR].id); gfx_quads_begin(); gfx_setcolor(1,1,1,1); gfx_quads_drawTL(mx,my,24,24); gfx_quads_end(); // render debug information if(config.debug) { RECT screen = *ui_screen(); gfx_mapscreen(screen.x, screen.y, screen.w, screen.h); char buf[512]; str_format(buf, sizeof(buf), "%p %p %p", ui_hot_item(), ui_active_item(), ui_last_active_item()); TEXT_CURSOR cursor; gfx_text_set_cursor(&cursor, 10, 10, 10, TEXTFLAG_RENDER); gfx_text_ex(&cursor, buf, -1); } escape_pressed = false; enter_pressed = false; num_inputevents = 0; }
void MENUS::render_demoplayer(RECT main_view) { const DEMOPLAYBACK_INFO *info = client_demoplayer_getinfo(); const float seekbar_height = 15.0f; const float buttonbar_height = 20.0f; const float margins = 5.0f; float total_height; if(menu_active) { total_height = seekbar_height+buttonbar_height+margins*3; /* else total_height = seekbar_height+margins*2; */ ui_hsplit_b(&main_view, total_height, 0, &main_view); ui_vsplit_l(&main_view, 250.0f, 0, &main_view); ui_vsplit_r(&main_view, 250.0f, &main_view, 0); ui_draw_rect(&main_view, color_tabbar_active, CORNER_T, 10.0f); ui_margin(&main_view, 5.0f, &main_view); } RECT seekbar, buttonbar; if(menu_active) { ui_hsplit_t(&main_view, seekbar_height, &seekbar, &buttonbar); ui_hsplit_t(&buttonbar, margins, 0, &buttonbar); } /* else seekbar = main_view; */ // do seekbar if(menu_active) { static int seekbar_id = 0; void *id = &seekbar_id; char buffer[128]; ui_draw_rect(&seekbar, vec4(0,0,0,0.5f), CORNER_ALL, 5.0f); int current_tick = info->current_tick - info->first_tick; int total_ticks = info->last_tick - info->first_tick; float amount = current_tick/(float)total_ticks; RECT filledbar = seekbar; filledbar.w = 10.0f + (filledbar.w-10.0f)*amount; ui_draw_rect(&filledbar, vec4(1,1,1,0.5f), CORNER_ALL, 5.0f); str_format(buffer, sizeof(buffer), "%d:%02d / %d:%02d", current_tick/SERVER_TICK_SPEED/60, (current_tick/SERVER_TICK_SPEED)%60, total_ticks/SERVER_TICK_SPEED/60, (total_ticks/SERVER_TICK_SPEED)%60); ui_do_label(&seekbar, buffer, seekbar.h*0.70f, 0); // do the logic int inside = ui_mouse_inside(&seekbar); if(ui_active_item() == id) { if(!ui_mouse_button(0)) ui_set_active_item(0); else { float amount = (ui_mouse_x()-seekbar.x)/(float)seekbar.w; if(amount > 0 && amount < 1.0f) { gameclient.on_reset(); gameclient.suppress_events = true; client_demoplayer_setpos(amount); gameclient.suppress_events = false; } } } else if(ui_hot_item() == id) { if(ui_mouse_button(0)) ui_set_active_item(id); } if(inside) ui_set_hot_item(id); } if(menu_active) { // do buttons RECT button; // pause button ui_vsplit_l(&buttonbar, buttonbar_height, &button, &buttonbar); static int pause_button = 0; if(ui_do_button(&pause_button, "| |", info->paused, &button, ui_draw_demoplayer_button, 0)) client_demoplayer_setpause(!info->paused); // play button ui_vsplit_l(&buttonbar, margins, 0, &buttonbar); ui_vsplit_l(&buttonbar, buttonbar_height, &button, &buttonbar); static int play_button = 0; if(ui_do_button(&play_button, ">", !info->paused, &button, ui_draw_demoplayer_button, 0)) { client_demoplayer_setpause(0); client_demoplayer_setspeed(1.0f); } // slowdown ui_vsplit_l(&buttonbar, margins, 0, &buttonbar); ui_vsplit_l(&buttonbar, buttonbar_height, &button, &buttonbar); static int slowdown_button = 0; if(ui_do_button(&slowdown_button, "<<", 0, &button, ui_draw_demoplayer_button, 0)) { if(info->speed > 4.0f) client_demoplayer_setspeed(4.0f); else if(info->speed > 2.0f) client_demoplayer_setspeed(2.0f); else if(info->speed > 1.0f) client_demoplayer_setspeed(1.0f); else if(info->speed > 0.5f) client_demoplayer_setspeed(0.5f); else client_demoplayer_setspeed(0.05f); } // fastforward ui_vsplit_l(&buttonbar, margins, 0, &buttonbar); ui_vsplit_l(&buttonbar, buttonbar_height, &button, &buttonbar); static int fastforward_button = 0; if(ui_do_button(&fastforward_button, ">>", 0, &button, ui_draw_demoplayer_button, 0)) { if(info->speed < 0.5f) client_demoplayer_setspeed(0.5f); else if(info->speed < 1.0f) client_demoplayer_setspeed(1.0f); else if(info->speed < 2.0f) client_demoplayer_setspeed(2.0f); else if(info->speed < 4.0f) client_demoplayer_setspeed(4.0f); else client_demoplayer_setspeed(8.0f); } // speed meter ui_vsplit_l(&buttonbar, margins*3, 0, &buttonbar); char buffer[64]; if(info->speed >= 1.0f) str_format(buffer, sizeof(buffer), "x%.0f", info->speed); else str_format(buffer, sizeof(buffer), "x%.1f", info->speed); ui_do_label(&buttonbar, buffer, button.h*0.7f, -1); // exit button ui_vsplit_r(&buttonbar, buttonbar_height*3, &buttonbar, &button); static int exit_button = 0; if(ui_do_button(&exit_button, "Exit", 0, &button, ui_draw_demoplayer_button, 0)) client_disconnect(); } }