cp_messageoptions_callbacks.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (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.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "../cl_shared.h"
00027 #include "../ui/ui_main.h"
00028 #include "../ui/ui_data.h"
00029 #include "cp_campaign.h"
00030 #include "cp_messageoptions.h"
00031 #include "cp_messageoptions_callbacks.h"
00032 
00033 static msoMenuState_t msoMenuState = MSO_MSTATE_REINIT;
00034 static int messageList_size = 0; 
00035 static int messageList_scroll = 0; 
00036 static int visibleMSOEntries = 0; 
00037 messageSettings_t backupMessageSettings[NT_NUM_NOTIFYTYPE]; 
00043 static void MSO_InitList (void)
00044 {
00045     uiNode_t *messageSetting = NULL;
00046     uiNode_t *lastCategory = NULL;
00047     int idx;
00048 
00049     /* option already allocated, nothing to do */
00050     if (UI_GetOption(TEXT_MESSAGEOPTIONS) != NULL)
00051         return;
00052 
00053     UI_ResetData(TEXT_MESSAGEOPTIONS);
00054     for (idx = 0; idx < ccs.numMsgCategoryEntries; idx++) {
00055         const msgCategoryEntry_t *entry = &ccs.msgCategoryEntries[idx];
00056         const char *id = va("%d", idx);
00057 
00058         if (entry->isCategory) {
00059             lastCategory = UI_AddOption(&messageSetting, id, va("_%s", entry->notifyType), id);
00060         } else {
00061             if (!lastCategory)
00062                 Sys_Error("MSO_InitList: The first entry must be a category");
00063             UI_AddOption(&lastCategory->firstChild, id, va("_%s", entry->notifyType), id);
00064         }
00065     }
00066     UI_RegisterOption(TEXT_MESSAGEOPTIONS, messageSetting);
00067     MSO_SetMenuState(MSO_MSTATE_PREPARED, qfalse, qtrue);
00068 }
00069 
00074 static void MSO_UpdateVisibleButtons (void)
00075 {
00076     int visible;/* current line */
00077     uiOptionIterator_t iterator;
00078     uiNode_t *messageSetting = UI_GetOption(TEXT_MESSAGEOPTIONS);
00079 
00080     UI_InitOptionIteratorAtIndex(messageList_scroll, messageSetting, &iterator);
00081 
00082     /* update visible button lines based on current displayed values */
00083     for (visible = 0; visible < messageList_size; visible++) {
00084         const uiNode_t *option = iterator.option;
00085         int idx;
00086         msgCategoryEntry_t *entry;
00087 
00088         if (!option)
00089             break;
00090         idx = atoi(OPTIONEXTRADATACONST(option).value);
00091 
00092         entry = &ccs.msgCategoryEntries[idx];
00093         if (!entry)
00094             break;
00095         if (entry->isCategory) {
00096             /* category is visible anyway*/
00097             UI_ExecuteConfunc("ms_disable %i", visible);
00098 
00099         } else {
00100             assert(entry->category);
00101             UI_ExecuteConfunc("ms_enable %i", visible);
00102             UI_ExecuteConfunc("ms_btnstate %i %i %i %i", visible, entry->settings->doPause,
00103                 entry->settings->doNotify, entry->settings->doSound);
00104         }
00105 
00106         UI_OptionIteratorNextOption(&iterator);
00107     }
00108 
00109     for (; visible < messageList_size; visible++)
00110         UI_ExecuteConfunc("ms_disable %i", visible);
00111 }
00112 
00120 static void MSOCB_Init (void)
00121 {
00122     if (msoMenuState < MSO_MSTATE_PREPARED) {
00123         MSO_InitList();
00124         msoMenuState = MSO_MSTATE_PREPARED;
00125     }
00126 
00127     if (msoMenuState < MSO_MSTATE_INITIALIZED) {
00128         MSO_UpdateVisibleButtons();
00129         msoMenuState = MSO_MSTATE_INITIALIZED;
00130     }
00131 }
00132 
00137 static void MSO_Init_f (void)
00138 {
00139     if (Cmd_Argc() == 2) {
00140         messageList_size = atoi(Cmd_Argv(1));
00141     }
00142 
00143     MSOCB_Init();
00144 }
00145 
00150 static void MSO_Toggle_f (void)
00151 {
00152     if (Cmd_Argc() != 3)
00153         Com_Printf("Usage: %s <listId> <pause|notify|sound>\n", Cmd_Argv(0));
00154     else {
00155         uiOptionIterator_t iterator;
00156         const int listIndex = atoi(Cmd_Argv(1));
00157         int idx;
00158         const msgCategoryEntry_t *selectedEntry;
00159         int optionType;
00160         qboolean activate;
00161         notify_t type;
00162         uiNode_t *messageSetting = UI_GetOption(TEXT_MESSAGEOPTIONS);
00163 
00164         UI_InitOptionIteratorAtIndex(messageList_scroll + listIndex, messageSetting, &iterator);
00165         if (!iterator.option)
00166             return;
00167 
00168         idx = atoi(OPTIONEXTRADATA(iterator.option).value);
00169         selectedEntry = &ccs.msgCategoryEntries[idx];
00170         if (!selectedEntry)
00171             return;
00172         if (selectedEntry->isCategory) {
00173             Com_Printf("Toggle command with selected category entry ignored.\n");
00174             return;
00175         }
00176         for (type = 0; type < NT_NUM_NOTIFYTYPE; type++) {
00177             if (!strcmp(nt_strings[type], selectedEntry->notifyType))
00178                 break;
00179         }
00180         if (type == NT_NUM_NOTIFYTYPE) {
00181             Com_Printf("Unrecognized messagetype during toggle '%s' ignored\n", selectedEntry->notifyType);
00182             return;
00183         }
00184 
00185         if (!strcmp(Cmd_Argv(2), "pause")) {
00186             optionType = MSO_PAUSE;
00187             activate = !selectedEntry->settings->doPause;
00188         } else if (!strcmp(Cmd_Argv(2), "notify")) {
00189             optionType = MSO_NOTIFY;
00190             activate = !selectedEntry->settings->doNotify;
00191         } else {
00192             optionType = MSO_SOUND;
00193             activate = !selectedEntry->settings->doSound;
00194         }
00195         MSO_Set(listIndex, type, optionType, activate, qtrue);
00196     }
00197 }
00198 
00203 static void MSO_Scroll_f (void)
00204 {
00205     if (Cmd_Argc() < 2)
00206         return;
00207 
00208     /* no scrolling if visible entry count is less than max on page (due to folding) */
00209     messageList_scroll = atoi(Cmd_Argv(1));
00210 
00211     MSO_UpdateVisibleButtons();
00212 }
00213 
00217 static void MSO_BackupSettings_f (void)
00218 {
00219     memcpy(backupMessageSettings, messageSettings, sizeof(backupMessageSettings));
00220 }
00221 
00227 static void MSO_RestoreSettings_f (void)
00228 {
00229     memcpy(messageSettings, backupMessageSettings, sizeof(messageSettings));
00230     MSO_SetMenuState(MSO_MSTATE_REINIT,qfalse,qtrue);
00231 }
00232 
00233 void MSO_SetMenuState (const msoMenuState_t newState, const qboolean callInit, const qboolean preserveIndex)
00234 {
00235     msoMenuState = newState;
00236     if (newState == MSO_MSTATE_REINIT && !preserveIndex) {
00237         visibleMSOEntries = 0;
00238         messageList_scroll = 0;
00239     }
00240     if (callInit)
00241         MSOCB_Init();
00242 
00243 }
00244 
00245 void MSO_InitCallbacks (void)
00246 {
00247     memset(backupMessageSettings, 1, sizeof(backupMessageSettings));
00248     Cmd_AddCommand("msgoptions_toggle", MSO_Toggle_f, "Toggles pause, notification or sound setting for a message category");
00249     Cmd_AddCommand("msgoptions_scroll", MSO_Scroll_f, "Scroll callback function for message options menu text");
00250     Cmd_AddCommand("msgoptions_init", MSO_Init_f, "Initializes message options menu");
00251     Cmd_AddCommand("msgoptions_backup", MSO_BackupSettings_f, "Backup message settings");
00252     Cmd_AddCommand("msgoptions_restore",MSO_RestoreSettings_f, "Restore message settings from backup");
00253 
00254 }
00255 
00256 void MSO_ShutdownCallbacks (void)
00257 {
00258     Cmd_RemoveCommand("msgoptions_toggle");
00259     Cmd_RemoveCommand("msgoptions_scroll");
00260     Cmd_RemoveCommand("msgoptions_init");
00261     Cmd_RemoveCommand("msgoptions_backup");
00262     Cmd_RemoveCommand("msgoptions_restore");
00263     UI_ResetData(TEXT_MESSAGEOPTIONS);
00264 }

Generated by  doxygen 1.6.2