Atrinik Client 2.5
events/keys.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 
00032 static int menuRepeatKey = -1;
00033 static Uint32 menuRepeatTicks = 0, menuRepeatTime = KEY_REPEAT_TIME_INIT;
00034 _keys keys[MAX_KEYS];
00035 
00039 static const int screen_definitions[16][2] =
00040 {
00041     {800, 600},
00042     {960, 600},
00043     {1024, 768},
00044     {1100, 700},
00045     {1280, 720},
00046     {1280, 800},
00047     {1280, 960},
00048     {1280, 1024},
00049     {1440, 900},
00050     {1400, 1050},
00051     {1600, 1200},
00052     {1680, 1050},
00053     {1920, 1080},
00054     {1920, 1200},
00055     {2048, 1536},
00056     {2560, 1600},
00057 };
00058 
00061 void init_keys()
00062 {
00063     int i;
00064 
00065     for (i = 0; i < MAX_KEYS; i++)
00066     {
00067         keys[i].time = 0;
00068     }
00069 
00070     reset_keys();
00071 }
00072 
00073 void reset_keys()
00074 {
00075     int i;
00076 
00077     SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
00078 
00079     text_input_string_flag = 0;
00080     text_input_string_end_flag = 0;
00081     text_input_string_esc_flag = 0;
00082 
00083     for (i = 0; i < MAX_KEYS; i++)
00084     {
00085         keys[i].pressed = 0;
00086     }
00087 }
00088 
00089 int key_event(SDL_KeyboardEvent *key)
00090 {
00091     if (key->type == SDL_KEYUP)
00092     {
00093         keys[key->keysym.sym].pressed = 0;
00094 
00095         if (cpl.menustatus == MENU_NO)
00096         {
00097             keybind_process_event(key);
00098         }
00099     }
00100     else if (key->type == SDL_KEYDOWN)
00101     {
00102         if (cpl.menustatus != MENU_NO)
00103         {
00104             keys[key->keysym.sym].pressed = 1;
00105             keys[key->keysym.sym].time = LastTick + KEY_REPEAT_TIME_INIT;
00106             check_menu_keys(cpl.menustatus, key->keysym.sym);
00107         }
00108         /* no menu */
00109         else
00110         {
00111             keys[key->keysym.sym].pressed = 1;
00112             keys[key->keysym.sym].time = LastTick + KEY_REPEAT_TIME_INIT;
00113 
00114             if (keybind_command_matches_event("?COPY", key))
00115             {
00116                 textwin_handle_copy();
00117             }
00118             else if (key->keysym.sym == SDLK_ESCAPE)
00119             {
00120                 settings_open();
00121             }
00122 
00123             keybind_process_event(key);
00124         }
00125     }
00126 
00127     return 0;
00128 }
00129 
00130 /* We have a key event */
00131 int event_poll_key(SDL_Event *event)
00132 {
00133     if (event->type == SDL_KEYUP)
00134     {
00135         /* End of key repeat. */
00136         menuRepeatKey = -1;
00137         menuRepeatTime = KEY_REPEAT_TIME_INIT;
00138     }
00139 
00140     if (lists_handle_keyboard(&event->key))
00141     {
00142         return 0;
00143     }
00144 
00145     if (text_input_string_flag)
00146     {
00147         if (cpl.input_mode != INPUT_MODE_NUMBER)
00148             cpl.inventory_win = IWIN_BELOW;
00149 
00150         text_input_handle(&event->key);
00151     }
00152     else if (!text_input_string_end_flag)
00153     {
00154         if (GameStatus == GAME_STATUS_PLAY)
00155             return key_event(&event->key);
00156     }
00157 
00158     return 0;
00159 }
00160 
00161 void cursor_keys(int num)
00162 {
00163     switch (num)
00164     {
00165         case 0:
00166             if (cpl.inventory_win == IWIN_BELOW)
00167             {
00168                 if (cpl.win_below_slot - INVITEMBELOWXLEN >= 0)
00169                     cpl.win_below_slot -= INVITEMBELOWXLEN;
00170 
00171                 cpl.win_below_tag = get_inventory_data(cpl.below, &cpl.win_below_ctag, &cpl.win_below_slot, &cpl.win_below_start, &cpl.win_below_count, INVITEMBELOWXLEN, INVITEMBELOWYLEN);
00172             }
00173             else
00174             {
00175                 if (cpl.win_inv_slot - INVITEMXLEN >= 0)
00176                     cpl.win_inv_slot -= INVITEMXLEN;
00177 
00178                 cpl.win_inv_tag = get_inventory_data(cpl.ob, &cpl.win_inv_ctag, &cpl.win_inv_slot, &cpl.win_inv_start, &cpl.win_inv_count, INVITEMXLEN, INVITEMYLEN);
00179             }
00180             break;
00181 
00182         case 1:
00183             if (cpl.inventory_win == IWIN_BELOW)
00184             {
00185                 if (cpl.win_below_slot + INVITEMXLEN < cpl.win_below_count)
00186                     cpl.win_below_slot += INVITEMXLEN;
00187 
00188                 cpl.win_below_tag = get_inventory_data(cpl.below, &cpl.win_below_ctag, &cpl.win_below_slot, &cpl.win_below_start, &cpl.win_below_count, INVITEMBELOWXLEN, INVITEMBELOWYLEN);
00189             }
00190             else
00191             {
00192                 if (cpl.win_inv_slot + INVITEMXLEN < cpl.win_inv_count)
00193                     cpl.win_inv_slot += INVITEMXLEN;
00194 
00195                 cpl.win_inv_tag = get_inventory_data(cpl.ob, &cpl.win_inv_ctag, &cpl.win_inv_slot, &cpl.win_inv_start, &cpl.win_inv_count, INVITEMXLEN, INVITEMYLEN);
00196             }
00197             break;
00198 
00199         case 2:
00200             if (cpl.inventory_win == IWIN_BELOW)
00201             {
00202                 cpl.win_below_slot--;
00203 
00204                 cpl.win_below_tag = get_inventory_data(cpl.below, &cpl.win_below_ctag, &cpl.win_below_slot, &cpl.win_below_start, &cpl.win_below_count, INVITEMBELOWXLEN, INVITEMBELOWYLEN);
00205             }
00206             else
00207             {
00208                 cpl.win_inv_slot--;
00209 
00210                 cpl.win_inv_tag = get_inventory_data(cpl.ob, &cpl.win_inv_ctag, &cpl.win_inv_slot, &cpl.win_inv_start, &cpl.win_inv_count, INVITEMXLEN, INVITEMYLEN);
00211             }
00212             break;
00213 
00214         case 3:
00215             if (cpl.inventory_win == IWIN_BELOW)
00216             {
00217                 cpl.win_below_slot++;
00218 
00219                 cpl.win_below_tag = get_inventory_data(cpl.below, &cpl.win_below_ctag, &cpl.win_below_slot, &cpl.win_below_start, &cpl.win_below_count, INVITEMBELOWXLEN, INVITEMBELOWYLEN);
00220             }
00221             else
00222             {
00223                 cpl.win_inv_slot++;
00224 
00225                 cpl.win_inv_tag = get_inventory_data(cpl.ob, &cpl.win_inv_ctag, &cpl.win_inv_slot, &cpl.win_inv_start, &cpl.win_inv_count, INVITEMXLEN, INVITEMYLEN);
00226             }
00227             break;
00228     }
00229 }
00230 
00231 /* Handle key repeating. */
00232 void key_repeat()
00233 {
00234     if (cpl.menustatus == MENU_NO)
00235     {
00236         keybind_repeat();
00237     }
00238     /* check menu keys for repeat */
00239     else
00240     {
00241         if (SDL_GetTicks() - menuRepeatTicks > menuRepeatTime || !menuRepeatTicks || menuRepeatKey < 0)
00242         {
00243             menuRepeatTicks = SDL_GetTicks();
00244 
00245             if (menuRepeatKey >= 0)
00246             {
00247                 check_menu_keys(cpl.menustatus, menuRepeatKey);
00248                 menuRepeatTime = KEY_REPEAT_TIME;
00249             }
00250         }
00251     }
00252 }
00253 
00254 /* Handle keystrokes in menu dialog. */
00255 void check_menu_keys(int menu, int key)
00256 {
00257     if (cpl.menustatus == MENU_NO)
00258         return;
00259 
00260     /* close menu */
00261     if (key == SDLK_ESCAPE)
00262     {
00263         cpl.menustatus = MENU_NO;
00264         map_udate_flag = 2;
00265         reset_keys();
00266         return;
00267     }
00268 
00269     switch (menu)
00270     {
00271         case MENU_BOOK:
00272             book_handle_key(key);
00273             menuRepeatKey = key;
00274             break;
00275 
00276         case MENU_REGION_MAP:
00277             region_map_handle_key(key);
00278             menuRepeatKey = key;
00279             break;
00280     }
00281 }