Code Search for Developers
 
 
  

kkeys.cpp from Spatial Knowledge Experiments at Krugle


Show kkeys.cpp syntax highlighted

/*
    Copyright (C) 2003, 2004 by Luca Cappa
    Written by Luca Cappa groton@users.sourceforge.net
  
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
  
    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "cssysdef.h"

#include "csutil/sysfunc.h"

#include "csqint.h"

#include "imesh/thing.h"

#include "csutil/event.h"
#include "csutil/scanstr.h"

#include "csgeom/math3d.h"

#include "cstool/collider.h"
#include "cstool/cspixmap.h"

#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"

#include "iengine/light.h"

#include "ivaria/reporter.h"
#include "ivaria/view.h"
#include "ivaria/conin.h"
#include "ivaria/conout.h"

#include "iutil/event.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"

#include "iaws/aws.h"
#include "iaws/awscnvs.h"
#include "iaws/awsparm.h"


//
//Include for this file.
#include "kcommandprocessor.h"
#include "ske.h"
#include "kappstate.h"

//?? TO DO: convert this file to an XML configuration file!!!

///
struct csKeyMap
{
  int m_key, m_shift, m_alt, m_ctrl;
  char* m_cmd;
  bool m_needStatus, m_isOn;
  
  csKeyMap *m_next, *m_prev;
};

csKeyMap* g_mapping = 0;

//===========================================================================
// Everything for key p_mapping and binding.
//===========================================================================

void MapKey (const char* p_keyName, csKeyMap* p_map)
{
  p_map->m_shift = 0;
  p_map->m_alt = 0;
  p_map->m_ctrl = 0;
  p_map->m_needStatus = false;
  p_map->m_isOn = false;
  char* l_dash = strchr (p_keyName, '-');//Search for the dash key (ie '-')
  
  //?????BUG: please, dont use the '-' char !!!!
  while (l_dash)
  {
    *l_dash = 0;//remove the '-' char.
    if (!strcmp (p_keyName, "shift")) 
      p_map->m_shift = 1;
    else if (!strcmp (p_keyName, "alt")) 
      p_map->m_alt = 1;
    else if (!strcmp (p_keyName, "ctrl")) 
      p_map->m_ctrl = 1;
    else if (!strcmp (p_keyName, "status")) 
      p_map->m_needStatus = true;
    else 
      SKE::Report (CS_REPORTER_SEVERITY_NOTIFY, "Bad modifier '%s'!",
        p_keyName);

    *l_dash = '-';//restore the '-' char.
    p_keyName = l_dash + 1;
    l_dash = strchr (l_dash + 1, '-');
  }//while

  if (!strcmp (p_keyName, "tab")) p_map->m_key = CSKEY_TAB;
  else if (!strcmp (p_keyName, "space")) p_map->m_key = ' ';
  else if (!strcmp (p_keyName, "esc")) p_map->m_key = CSKEY_ESC;
  else if (!strcmp (p_keyName, "enter")) p_map->m_key = CSKEY_ENTER;
  else if (!strcmp (p_keyName, "bs")) p_map->m_key = CSKEY_BACKSPACE;
  else if (!strcmp (p_keyName, "up")) p_map->m_key = CSKEY_UP;
  else if (!strcmp (p_keyName, "down")) p_map->m_key = CSKEY_DOWN;
  else if (!strcmp (p_keyName, "right")) p_map->m_key = CSKEY_RIGHT;
  else if (!strcmp (p_keyName, "left")) p_map->m_key = CSKEY_LEFT;
  else if (!strcmp (p_keyName, "pgup")) p_map->m_key = CSKEY_PGUP;
  else if (!strcmp (p_keyName, "pgdn")) p_map->m_key = CSKEY_PGDN;
  else if (!strcmp (p_keyName, "home")) p_map->m_key = CSKEY_HOME;
  else if (!strcmp (p_keyName, "end")) p_map->m_key = CSKEY_END;
  else if (!strcmp (p_keyName, "ins")) p_map->m_key = CSKEY_INS;
  else if (!strcmp (p_keyName, "del")) p_map->m_key = CSKEY_DEL;
  else if (!strcmp (p_keyName, "f1")) p_map->m_key = CSKEY_F1;
  else if (!strcmp (p_keyName, "f2")) p_map->m_key = CSKEY_F2;
  else if (!strcmp (p_keyName, "f3")) p_map->m_key = CSKEY_F3;
  else if (!strcmp (p_keyName, "f4")) p_map->m_key = CSKEY_F4;
  else if (!strcmp (p_keyName, "f5")) p_map->m_key = CSKEY_F5;
  else if (!strcmp (p_keyName, "f6")) p_map->m_key = CSKEY_F6;
  else if (!strcmp (p_keyName, "f7")) p_map->m_key = CSKEY_F7;
  else if (!strcmp (p_keyName, "f8")) p_map->m_key = CSKEY_F8;
  else if (!strcmp (p_keyName, "f9")) p_map->m_key = CSKEY_F9;
  else if (!strcmp (p_keyName, "f10")) p_map->m_key = CSKEY_F10;
  else if (!strcmp (p_keyName, "f11")) p_map->m_key = CSKEY_F11;
  else if (!strcmp (p_keyName, "f12")) p_map->m_key = CSKEY_F12;
  else if (!strcmp (p_keyName, "+")) p_map->m_key = CSKEY_PADPLUS;
  else if (!strcmp (p_keyName, "-")) p_map->m_key = CSKEY_PADMINUS;
  else if (!strcmp (p_keyName, "*")) p_map->m_key = CSKEY_PADMULT;
  else if (!strcmp (p_keyName, "/")) p_map->m_key = CSKEY_PADDIV;
  else if (*(p_keyName + 1) != 0) 
    g_ske->Report (CS_REPORTER_SEVERITY_NOTIFY, "Bad key '%s'!", p_keyName);
  else if ((*p_keyName >= 'A' && *p_keyName <= 'Z') || strchr ("!@#$%^&*()_+", *p_keyName))
  {
    p_map->m_shift = 1;
    p_map->m_key = *p_keyName;
  }//else if
  else
    p_map->m_key = *p_keyName;
}

char* KeyName (csKeyMap* p_map)
{
  static char l_buf[256];
  l_buf[0] = 0;
  
  if (p_map->m_needStatus) strcat (l_buf, "status-");
  if (p_map->m_shift) strcat (l_buf, "shift-");
  if (p_map->m_ctrl) strcat (l_buf, "ctrl-");
  if (p_map->m_alt) strcat (l_buf, "alt-");
  
  switch (p_map->m_key)
  {
    case CSKEY_TAB: strcat (l_buf, "tab"); break;
    case ' ': strcat (l_buf, "space"); break;
    case CSKEY_ESC: strcat (l_buf, "esc"); break;
    case CSKEY_ENTER: strcat (l_buf, "enter"); break;
    case CSKEY_BACKSPACE: strcat (l_buf, "bs"); break;
    case CSKEY_UP: strcat (l_buf, "up"); break;
    case CSKEY_DOWN: strcat (l_buf, "down"); break;
    case CSKEY_RIGHT: strcat (l_buf, "right"); break;
    case CSKEY_LEFT: strcat (l_buf, "left"); break;
    case CSKEY_PGUP: strcat (l_buf, "pgup"); break;
    case CSKEY_PGDN: strcat (l_buf, "pgdn"); break;
    case CSKEY_HOME: strcat (l_buf, "home"); break;
    case CSKEY_END: strcat (l_buf, "end"); break;
    case CSKEY_INS: strcat (l_buf, "ins"); break;
    case CSKEY_DEL: strcat (l_buf, "del"); break;
    case CSKEY_F1: strcat (l_buf, "f1"); break;
    case CSKEY_F2: strcat (l_buf, "f2"); break;
    case CSKEY_F3: strcat (l_buf, "f3"); break;
    case CSKEY_F4: strcat (l_buf, "f4"); break;
    case CSKEY_F5: strcat (l_buf, "f5"); break;
    case CSKEY_F6: strcat (l_buf, "f6"); break;
    case CSKEY_F7: strcat (l_buf, "f7"); break;
    case CSKEY_F8: strcat (l_buf, "f8"); break;
    case CSKEY_F9: strcat (l_buf, "f9"); break;
    case CSKEY_F10: strcat (l_buf, "f10"); break;
    case CSKEY_F11: strcat (l_buf, "f11"); break;
    case CSKEY_F12: strcat (l_buf, "f12"); break;
    case CSKEY_PADPLUS: strcat (l_buf, "+"); break;
    case CSKEY_PADMINUS: strcat (l_buf, "-"); break;
    case CSKEY_PADMULT: strcat (l_buf, "*"); break;
    case CSKEY_PADDIV: strcat (l_buf, "/"); break;
    default:
    {
      char* l_s = strchr (l_buf, 0);
      *l_s++ = p_map->m_key;
      *l_s = 0;
    }
  }
  return l_buf;
}

csKeyMap* FindMapping (const char* p_keyName)
{
  csKeyMap l_map;
  MapKey (p_keyName, &l_map);

  csKeyMap* l_m = g_mapping;
  while (l_m)
  {
    if (l_map.m_key == l_m->m_key && 
      l_map.m_shift == l_m->m_shift &&
      l_map.m_ctrl == l_m->m_ctrl &&
      l_map.m_alt == l_m->m_alt && 
      l_map.m_needStatus == l_m->m_needStatus)
      return l_m;
    l_m = l_m->m_next;
  }
  return 0;
}

void BindKey (const char* p_arg)
{
  if (!p_arg)
  {
    csKeyMap* p_map = g_mapping;
    while (p_map)
    {
      g_ske->Report (CS_REPORTER_SEVERITY_NOTIFY,
      	"Key '%s' bound to '%s'.", KeyName (p_map), p_map->m_cmd);
      p_map = p_map->m_next;
    }
    return;
  }
  
  char* space = strchr (p_arg, ' ');
  if (space)
  {
    *space = 0;
    
    csKeyMap* p_map;
    /*//?? removing this code we can add more mapping for the same action
    // = find_p_mapping (arg);
    if (p_map)
    {
      delete [] p_map->cmd;
    }
    else*/
    {
      p_map = new csKeyMap ();
      p_map->m_next = g_mapping;
      p_map->m_prev = 0;
      if (g_mapping) 
        g_mapping->m_prev = p_map;
      g_mapping = p_map;
      MapKey (p_arg, p_map);
    }
    p_map->m_cmd = new char [strlen (space+1)+1];
    strcpy (p_map->m_cmd, space+1);
    *space = ' ';
  }
  else
  {
    csKeyMap* p_map = FindMapping (p_arg);
    if (p_map) 
      g_ske->Report (CS_REPORTER_SEVERITY_NOTIFY,
    	"Key bound to '%s'!", p_map->m_cmd);
    else 
      g_ske->Report (CS_REPORTER_SEVERITY_NOTIFY, "Key not bound!");
  }//else
}

void FreeKeyMap ()
{
  csKeyMap *l_prev, *l_cur = g_mapping;
  
  while (l_cur)
  {
    l_prev = l_cur;
    l_cur = l_cur->m_next;
    delete [] l_prev->m_cmd;
    delete l_prev;
  }//while
  
  g_mapping = 0;
}

void SKE::EatKeyPress (iEvent *p_event)
{
  uint32 l_key = 0;
  p_event->Retrieve ("keyCodeRaw", l_key);

  uint8 l_evType = 0;
  p_event->Retrieve ("keyEventType", l_evType);
  
  bool l_status = (l_evType == csKeyEventTypeDown);

  if (m_consoleOutput && m_consoleOutput->GetVisible ())
  {
    if (m_consoleInput)
      m_consoleInput->HandleEvent (*p_event);
    //@@KLUDGE
    if (l_key != CSKEY_ESC)
      return;
  }

  csKeyModifiers l_modifiers;
  csKeyEventHelper::GetModifiers (p_event, l_modifiers);

  bool l_shift = l_modifiers.modifiers[csKeyModifierTypeShift] != 0;
  bool l_alt = l_modifiers.modifiers[csKeyModifierTypeAlt] != 0;
  bool l_ctrl = l_modifiers.modifiers[csKeyModifierTypeCtrl] != 0;

  csKeyMap *l_m = g_mapping;
  while (l_m)
  {
    if (l_key == l_m->m_key
     && l_shift == l_m->m_shift && l_alt == l_m->m_alt && l_ctrl == l_m->m_ctrl)
    {
      if (l_m->m_needStatus)
      {
        // Don't perform the command again if the key is already down
        if (l_m->m_isOn != l_status)
        {
          char l_buf [256];
          sprintf (l_buf,"%s %d", l_m->m_cmd, l_status);
          KCommandProcessor::_PerformLine (l_buf);
          l_m->m_isOn = l_status;
        }//if
      }//if
      else
      {
        if (l_status)
          KCommandProcessor::_PerformLine (l_m->m_cmd);
      }//else
    }
    else if (!l_status && l_key == l_m->m_key && l_m->m_isOn && l_m->m_needStatus)
    {
      if (l_key == l_m->m_key || l_shift != l_m->m_shift ||
        l_alt != l_m->m_alt || l_ctrl != l_m->m_ctrl)
      {
        char l_buf [256];
        sprintf (l_buf,"%s 0", l_m->m_cmd);
        KCommandProcessor::_PerformLine (l_buf);
        l_m->m_isOn = 0;
      }//if
    }//else
    l_m = l_m->m_next;
  }//while
}






See more files for this project here

Spatial Knowledge Experiments

A simulation of 3D virtual worlds for psychological experiments

Project homepage: http://sourceforge.net/projects/ske
Programming language(s): C,C++,Perl
License: other

  isense/
  joystick/
  Jamfile
  KImageCardinalDirection.h
  SKE.cpp
  SKE.h
  eulerangles.c
  eulerangles.h
  ikdraggable.cpp
  ikdraggable.h
  ikdraghandler.h
  ikmission.h
  ikxmlreader.h
  ikxmlwriter.h
  kappstate.cpp
  kappstate.h
  kbasedraggable.cpp
  kbasedraggable.h
  kbasedraghandler.cpp
  kbasedraghandler.h
  kblock.cpp
  kblock.h
  kblockId.h
  kblockid.cpp
  kcamera.cpp
  kcamera.h
  kcollision.cpp
  kcommandprocessor.cpp
  kcommandprocessor.h
  kconstant.cpp
  kconstant.h
  kcursor3d.cpp
  kcursor3d.h
  kdata.cpp
  kdata.h
  kdatalist.cpp
  kdatalist.h
  kdragmanager.cpp
  kdragmanager.h
  keventhandlermanager.cpp
  keventhandlermanager.h
  kflags.cpp
  kflags.h
  kimagecardinaldirection.cpp
  kkeys.cpp
  kkeys.h
  kkeystate.cpp
  kkeystate.h
  klandmark.cpp
  klandmark.h
  klandmarklist.cpp
  klandmarklist.h
  klight.cpp
  klight.h
  klightlist.cpp
  klightlist.h
  kmap.cpp
  kmap.h
  kmission.cpp
  kmission.h
  kmissiontype.cpp
  kmissiontype.h
  kmode.cpp
  kmode.h
  kmousemanager.cpp
  kmousemanager.h
  kobject3d.cpp
  kobject3d.h
  korientation.cpp
  korientation.h
  kpath.cpp
  kpath.h
  kpathfollower.cpp
  kpathfollower.h
  kplayer.cpp
  kplayer.h
  kposition.cpp
  kposition.h
  kquaternion.cpp
  kquaternion.h
  ksaveddatamanager.cpp
  ksaveddatamanager.h
  ksegment3.h
  ksign.cpp
  ksign.h
  ksprite3d.cpp
  ksprite3d.h
  ksprite3dlist.cpp
  ksprite3dlist.h
  kterrain.cpp
  kterrain.h
  kthing.cpp
  kthing.h
  kutil.cpp
  kutil.h
  kwireframe.cpp
  kwireframe.h
  kxmlhelper.h
  movement1d.h
  movement3d.h
  quattypes.h