Mercurial > vba-clojure
diff src/gtk/joypadconfig.cpp @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/gtk/joypadconfig.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,276 @@ 1.4 +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. 1.5 +// Copyright (C) 1999-2003 Forgotten 1.6 +// Copyright (C) 2004 Forgotten and the VBA development team 1.7 + 1.8 +// This program is free software; you can redistribute it and/or modify 1.9 +// it under the terms of the GNU General Public License as published by 1.10 +// the Free Software Foundation; either version 2, or(at your option) 1.11 +// any later version. 1.12 +// 1.13 +// This program is distributed in the hope that it will be useful, 1.14 +// but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +// GNU General Public License for more details. 1.17 +// 1.18 +// You should have received a copy of the GNU General Public License 1.19 +// along with this program; if not, write to the Free Software Foundation, 1.20 +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1.21 + 1.22 +#include "joypadconfig.h" 1.23 + 1.24 +#include <string.h> 1.25 + 1.26 +#include "intl.h" 1.27 + 1.28 +namespace VBA 1.29 +{ 1.30 + 1.31 +guint * JoypadConfig::puiAt(int _iIndex) 1.32 +{ 1.33 + guint * puiMember; 1.34 + 1.35 + switch (_iIndex) 1.36 + { 1.37 + case 0: 1.38 + puiMember = &m_uiUp; 1.39 + break; 1.40 + case 1: 1.41 + puiMember = &m_uiDown; 1.42 + break; 1.43 + case 2: 1.44 + puiMember = &m_uiLeft; 1.45 + break; 1.46 + case 3: 1.47 + puiMember = &m_uiRight; 1.48 + break; 1.49 + case 4: 1.50 + puiMember = &m_uiA; 1.51 + break; 1.52 + case 5: 1.53 + puiMember = &m_uiB; 1.54 + break; 1.55 + case 6: 1.56 + puiMember = &m_uiL; 1.57 + break; 1.58 + case 7: 1.59 + puiMember = &m_uiR; 1.60 + break; 1.61 + case 8: 1.62 + puiMember = &m_uiSelect; 1.63 + break; 1.64 + case 9: 1.65 + puiMember = &m_uiStart; 1.66 + break; 1.67 + case 10: 1.68 + puiMember = &m_uiSpeed; 1.69 + break; 1.70 + case 11: 1.71 + puiMember = &m_uiCapture; 1.72 + break; 1.73 + default: 1.74 + puiMember = NULL; 1.75 + } 1.76 + 1.77 + return puiMember; 1.78 +} 1.79 + 1.80 +int JoypadConfig::iFind(guint _uiKeycode) 1.81 +{ 1.82 + for (guint i = 0; i < 12; i++) 1.83 + { 1.84 + if (*puiAt(i) == _uiKeycode) 1.85 + { 1.86 + return i; 1.87 + } 1.88 + } 1.89 + 1.90 + return -1; 1.91 +} 1.92 + 1.93 +void JoypadConfig::vSetDefault() 1.94 +{ 1.95 + guint auiKeyval[] = 1.96 + { 1.97 + GDK_Up, GDK_Down, GDK_Left, GDK_Right, 1.98 + GDK_z, GDK_x, GDK_a, GDK_s, 1.99 + GDK_BackSpace, GDK_Return, 1.100 + GDK_space, GDK_F12 1.101 + }; 1.102 + 1.103 + for (guint i = 0; i < G_N_ELEMENTS(auiKeyval); i++) 1.104 + { 1.105 + GdkKeymapKey * pstKeys; 1.106 + int iKeys; 1.107 + 1.108 + if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), 1.109 + auiKeyval[i], 1.110 + &pstKeys, 1.111 + &iKeys)) 1.112 + { 1.113 + *puiAt(i) = pstKeys[0].keycode; 1.114 + g_free(pstKeys); 1.115 + } 1.116 + else 1.117 + { 1.118 + *puiAt(i) = 0; 1.119 + } 1.120 + } 1.121 +} 1.122 + 1.123 +Keymap * JoypadConfig::poCreateKeymap() const 1.124 +{ 1.125 + Keymap * poKeymap = new Keymap(); 1.126 + 1.127 + poKeymap->vRegister(m_uiUp, KeyUp ); 1.128 + poKeymap->vRegister(m_uiDown, KeyDown ); 1.129 + poKeymap->vRegister(m_uiLeft, KeyLeft ); 1.130 + poKeymap->vRegister(m_uiRight, KeyRight ); 1.131 + poKeymap->vRegister(m_uiA, KeyA ); 1.132 + poKeymap->vRegister(m_uiB, KeyB ); 1.133 + poKeymap->vRegister(m_uiL, KeyL ); 1.134 + poKeymap->vRegister(m_uiR, KeyR ); 1.135 + poKeymap->vRegister(m_uiSelect, KeySelect ); 1.136 + poKeymap->vRegister(m_uiStart, KeyStart ); 1.137 + poKeymap->vRegister(m_uiSpeed, KeySpeed ); 1.138 + poKeymap->vRegister(m_uiCapture, KeyCapture ); 1.139 + 1.140 + return poKeymap; 1.141 +} 1.142 + 1.143 +JoypadConfigDialog::JoypadConfigDialog(GtkDialog * _pstDialog, 1.144 + const Glib::RefPtr<Gnome::Glade::Xml> & _poXml) : 1.145 + Gtk::Dialog(_pstDialog) 1.146 +{ 1.147 + m_puiCurrentKeyCode = NULL; 1.148 + 1.149 + memset(&m_oConfig, 0, sizeof(m_oConfig)); 1.150 + 1.151 + m_poOkButton = dynamic_cast<Gtk::Button *>(_poXml->get_widget("JoypadOkButton")); 1.152 + 1.153 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadUpEntry"))); 1.154 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadDownEntry"))); 1.155 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadLeftEntry"))); 1.156 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadRightEntry"))); 1.157 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadAEntry"))); 1.158 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadBEntry"))); 1.159 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadLEntry"))); 1.160 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadREntry"))); 1.161 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadSelectEntry"))); 1.162 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadStartEntry"))); 1.163 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadSpeedEntry"))); 1.164 + m_oEntries.push_back(dynamic_cast<Gtk::Entry *>(_poXml->get_widget("JoypadCaptureEntry"))); 1.165 + 1.166 + for (guint i = 0; i < m_oEntries.size(); i++) 1.167 + { 1.168 + Gtk::Entry * poEntry = m_oEntries[i]; 1.169 + 1.170 + poEntry->signal_focus_in_event().connect(SigC::bind<guint>( 1.171 + SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusIn), 1.172 + i)); 1.173 + poEntry->signal_focus_out_event().connect(SigC::slot(*this, &JoypadConfigDialog::bOnEntryFocusOut)); 1.174 + } 1.175 + 1.176 + vUpdateEntries(); 1.177 +} 1.178 + 1.179 +JoypadConfigDialog::~JoypadConfigDialog() 1.180 +{ 1.181 +} 1.182 + 1.183 +void JoypadConfigDialog::vSetConfig(const JoypadConfig & _roConfig) 1.184 +{ 1.185 + m_oConfig = _roConfig; 1.186 + vUpdateEntries(); 1.187 +} 1.188 + 1.189 +void JoypadConfigDialog::vUpdateEntries() 1.190 +{ 1.191 + for (guint i = 0; i < m_oEntries.size(); i++) 1.192 + { 1.193 + guint uiKeyval = 0; 1.194 + gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(), 1.195 + *m_oConfig.puiAt(i), 1.196 + (GdkModifierType)0, 1.197 + 0, 1.198 + &uiKeyval, 1.199 + NULL, 1.200 + NULL, 1.201 + NULL); 1.202 + const char * csName = gdk_keyval_name(uiKeyval); 1.203 + if (csName == NULL) 1.204 + { 1.205 + m_oEntries[i]->set_text(_("<Undefined>")); 1.206 + } 1.207 + else 1.208 + { 1.209 + m_oEntries[i]->set_text(csName); 1.210 + } 1.211 + } 1.212 +} 1.213 + 1.214 +bool JoypadConfigDialog::bOnEntryFocusIn(GdkEventFocus * _pstEvent, 1.215 + guint _uiEntry) 1.216 +{ 1.217 + m_uiCurrentEntry = _uiEntry; 1.218 + m_puiCurrentKeyCode = m_oConfig.puiAt(_uiEntry); 1.219 + 1.220 + return false; 1.221 +} 1.222 + 1.223 +bool JoypadConfigDialog::bOnEntryFocusOut(GdkEventFocus * _pstEvent) 1.224 +{ 1.225 + m_puiCurrentKeyCode = NULL; 1.226 + 1.227 + return false; 1.228 +} 1.229 + 1.230 +bool JoypadConfigDialog::on_key_press_event(GdkEventKey * _pstEvent) 1.231 +{ 1.232 + if (m_puiCurrentKeyCode == NULL) 1.233 + { 1.234 + return Gtk::Dialog::on_key_press_event(_pstEvent); 1.235 + } 1.236 + 1.237 + *m_puiCurrentKeyCode = 0; 1.238 + int iFound = m_oConfig.iFind(_pstEvent->hardware_keycode); 1.239 + if (iFound >= 0) 1.240 + { 1.241 + *m_oConfig.puiAt(iFound) = 0; 1.242 + m_oEntries[iFound]->set_text(_("<Undefined>")); 1.243 + } 1.244 + 1.245 + *m_puiCurrentKeyCode = _pstEvent->hardware_keycode; 1.246 + 1.247 + guint uiKeyval = 0; 1.248 + gdk_keymap_translate_keyboard_state(gdk_keymap_get_default(), 1.249 + _pstEvent->hardware_keycode, 1.250 + (GdkModifierType)0, 1.251 + 0, 1.252 + &uiKeyval, 1.253 + NULL, 1.254 + NULL, 1.255 + NULL); 1.256 + 1.257 + const char * csName = gdk_keyval_name(uiKeyval); 1.258 + if (csName == NULL) 1.259 + { 1.260 + m_oEntries[m_uiCurrentEntry]->set_text(_("<Undefined>")); 1.261 + } 1.262 + else 1.263 + { 1.264 + m_oEntries[m_uiCurrentEntry]->set_text(csName); 1.265 + } 1.266 + 1.267 + if (m_uiCurrentEntry + 1 < m_oEntries.size()) 1.268 + { 1.269 + m_oEntries[m_uiCurrentEntry + 1]->grab_focus(); 1.270 + } 1.271 + else 1.272 + { 1.273 + m_poOkButton->grab_focus(); 1.274 + } 1.275 + 1.276 + return true; 1.277 +} 1.278 + 1.279 +} // namespace VBA