Atrinik Client 2.5
gui/input.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00036 void widget_number_event(widgetdata *widget, int x, int y)
00037 {
00038     int mx = 0, my = 0;
00039     mx = x - widget->x1;
00040     my = y - widget->y1;
00041 
00042     /* Close number input */
00043     if (text_input_string_flag && cpl.input_mode == INPUT_MODE_NUMBER)
00044     {
00045         if (mx > 239 && mx < 249 && my > 5 && my < 17)
00046         {
00047             SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
00048             text_input_string_flag = 0;
00049             text_input_string_end_flag = 1;
00050         }
00051     }
00052 }
00053 
00058 void widget_show_console(widgetdata *widget)
00059 {
00060     SDL_Rect box;
00061 
00062     box.x = 3;
00063     box.y = 0;
00064     box.w = Bitmaps[BITMAP_TEXTINPUT]->bitmap->w;
00065     box.h = Bitmaps[BITMAP_TEXTINPUT]->bitmap->h;
00066     text_input_show(ScreenSurface, widget->x1, widget->y1, FONT_ARIAL11, text_input_string, COLOR_WHITE, 0, BITMAP_TEXTINPUT, &box);
00067 }
00068 
00073 void widget_show_number(widgetdata *widget)
00074 {
00075     SDL_Rect box;
00076     char buf[MAX_BUF];
00077 
00078     box.x = 3;
00079     box.y = 9;
00080     box.w = Bitmaps[BITMAP_NUMBER]->bitmap->w - 22;
00081     box.h = Bitmaps[BITMAP_NUMBER]->bitmap->h;
00082     text_input_show(ScreenSurface, widget->x1, widget->y1, FONT_ARIAL11, text_input_string, COLOR_WHITE, 0, BITMAP_NUMBER, &box);
00083 
00084     snprintf(buf, sizeof(buf), "%s how many from %d %s", cpl.nummode == NUM_MODE_GET ? "get" : "drop", cpl.nrof, cpl.num_text);
00085     string_truncate_overflow(FONT_ARIAL10, buf, 220);
00086     string_blt(ScreenSurface, FONT_ARIAL10, buf, widget->x1 + 8, widget->y1 + 6, COLOR_HGOLD, 0, NULL);
00087 }
00088 
00092 void do_number()
00093 {
00094     int held = 0;
00095 
00096     map_udate_flag = 2;
00097 
00098     if (text_input_string_esc_flag)
00099     {
00100         reset_keys();
00101         cpl.input_mode = INPUT_MODE_NO;
00102         cur_widget[IN_NUMBER_ID]->show = 0;
00103     }
00104 
00105     if (((cpl.nummode == NUM_MODE_GET && keybind_command_matches_state("?GET")) || (cpl.nummode == NUM_MODE_DROP && keybind_command_matches_state("?DROP"))) && SDL_GetTicks() - text_input_opened > 125)
00106     {
00107         SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
00108         text_input_string_flag = 0;
00109         text_input_string_end_flag = 1;
00110         held = 1;
00111     }
00112 
00113     /* if set, we got a finished input!*/
00114     if (text_input_string_flag == 0 && text_input_string_end_flag)
00115     {
00116         if (text_input_string[0])
00117         {
00118             int tmp;
00119             tmp = atoi(text_input_string);
00120 
00121             /* If you enter a number higher than the real nrof, you will pickup all */
00122             if (tmp > cpl.nrof)
00123                 tmp = cpl.nrof;
00124 
00125             if (tmp > 0 && tmp <= cpl.nrof)
00126             {
00127                 client_send_move(cpl.loc, cpl.tag, tmp);
00128                 draw_info_format(COLOR_DGOLD, "%s %d from %d %s", cpl.nummode == NUM_MODE_GET ? "get" : "drop", tmp, cpl.nrof, cpl.num_text);
00129 
00130                 if (cpl.nummode == NUM_MODE_GET)
00131                     sound_play_effect("get.ogg", 100);
00132                 else
00133                     sound_play_effect("drop.ogg", 100);
00134             }
00135         }
00136 
00137         reset_keys();
00138 
00139         if (held)
00140         {
00141             keybind_struct *keybind = NULL;
00142 
00143             if (cpl.nummode == NUM_MODE_GET)
00144             {
00145                 keybind = keybind_find_by_command("?GET");
00146             }
00147             else if (cpl.nummode == NUM_MODE_DROP)
00148             {
00149                 keybind = keybind_find_by_command("?DROP");
00150             }
00151 
00152             if (keybind)
00153             {
00154                 keys[keybind->key].pressed = 1;
00155                 keys[keybind->key].time = LastTick + 125;
00156             }
00157         }
00158 
00159         cpl.input_mode = INPUT_MODE_NO;
00160         cur_widget[IN_NUMBER_ID]->show = 0;
00161     }
00162     else
00163         cur_widget[IN_NUMBER_ID]->show = 1;
00164 }
00165 
00169 void do_console()
00170 {
00171     map_udate_flag = 2;
00172 
00173     /* If ESC was pressed, close console. */
00174     if (text_input_string_esc_flag)
00175     {
00176         sound_play_effect("console.ogg", 100);
00177         reset_keys();
00178         cpl.input_mode = INPUT_MODE_NO;
00179         cur_widget[IN_CONSOLE_ID]->show = 0;
00180     }
00181 
00182     /* If set, we've got a finished input */
00183     if (text_input_string_flag == 0 && text_input_string_end_flag)
00184     {
00185         sound_play_effect("console.ogg", 100);
00186 
00187         if (text_input_string[0])
00188         {
00189             char buf[MAX_INPUT_STRING + 32];
00190 
00191             /* If it's not command, it's say */
00192             if (*text_input_string != '/')
00193             {
00194                 snprintf(buf, sizeof(buf), "/say %s", text_input_string);
00195             }
00196             else
00197             {
00198                 strcpy(buf, text_input_string);
00199             }
00200 
00201             if (!client_command_check(text_input_string))
00202                 send_command(buf);
00203         }
00204 
00205         reset_keys();
00206         cpl.input_mode = INPUT_MODE_NO;
00207         cur_widget[IN_CONSOLE_ID]->show = 0;
00208     }
00209     else
00210         cur_widget[IN_CONSOLE_ID]->show = 1;
00211 }