vbagx/source/gui/gui_keyboard.cpp

375 lines
8.8 KiB
C++
Raw Normal View History

2009-04-07 04:57:49 +02:00
/****************************************************************************
2009-04-10 09:51:55 +02:00
* libwiigui
2009-04-07 04:57:49 +02:00
*
2009-04-10 09:51:55 +02:00
* Tantric 2009
2009-04-07 04:57:49 +02:00
*
* gui_keyboard.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
2009-07-08 07:56:58 +02:00
static char tmptxt[MAX_KEYBOARD_DISPLAY];
static char * GetDisplayText(char * t)
{
if(!t)
return NULL;
int len = strlen(t);
if(len < MAX_KEYBOARD_DISPLAY)
return t;
strncpy(tmptxt, &t[len-MAX_KEYBOARD_DISPLAY], MAX_KEYBOARD_DISPLAY);
tmptxt[MAX_KEYBOARD_DISPLAY-1] = 0;
return &tmptxt[0];
}
2009-04-07 04:57:49 +02:00
/**
* Constructor for the GuiKeyboard class.
*/
GuiKeyboard::GuiKeyboard(char * t, u32 max)
{
width = 540;
height = 400;
shift = 0;
caps = 0;
selectable = true;
focus = 0; // allow focus
alignmentHor = ALIGN_CENTRE;
alignmentVert = ALIGN_MIDDLE;
strncpy(kbtextstr, t, max);
kbtextstr[max] = 0;
kbtextmaxlen = max;
Key thekeys[4][11] = {
{
{'1','!'},
{'2','@'},
{'3','#'},
{'4','$'},
{'5','%'},
{'6','^'},
{'7','&'},
{'8','*'},
{'9','('},
{'0',')'},
{'\0','\0'}
},
{
{'q','Q'},
{'w','W'},
{'e','E'},
{'r','R'},
{'t','T'},
{'y','Y'},
{'u','U'},
{'i','I'},
{'o','O'},
{'p','P'},
{'-','_'}
},
{
{'a','A'},
{'s','S'},
{'d','D'},
{'f','F'},
{'g','G'},
{'h','H'},
{'j','J'},
{'k','K'},
{'l','L'},
2009-10-10 00:27:26 +02:00
{';',':'},
2009-04-07 04:57:49 +02:00
{'\'','"'}
},
{
{'z','Z'},
{'x','X'},
{'c','C'},
{'v','V'},
{'b','B'},
{'n','N'},
{'m','M'},
{',','<'},
{'.','>'},
{'/','?'},
{'\0','\0'}
}
};
memcpy(keys, thekeys, sizeof(thekeys));
keyTextbox = new GuiImageData(keyboard_textbox_png);
keyTextboxImg = new GuiImage(keyTextbox);
keyTextboxImg->SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
keyTextboxImg->SetPosition(0, 0);
this->Append(keyTextboxImg);
2009-07-08 07:56:58 +02:00
kbText = new GuiText(GetDisplayText(kbtextstr), 22, (GXColor){0, 0, 0, 0xff});
2009-04-07 04:57:49 +02:00
kbText->SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
kbText->SetPosition(0, 13);
this->Append(kbText);
key = new GuiImageData(keyboard_key_png);
keyOver = new GuiImageData(keyboard_key_over_png);
keyMedium = new GuiImageData(keyboard_mediumkey_png);
keyMediumOver = new GuiImageData(keyboard_mediumkey_over_png);
keyLarge = new GuiImageData(keyboard_largekey_png);
keyLargeOver = new GuiImageData(keyboard_largekey_over_png);
keySoundOver = new GuiSound(button_over_pcm, button_over_pcm_size, SOUND_PCM);
keySoundClick = new GuiSound(button_click_pcm, button_click_pcm_size, SOUND_PCM);
trigA = new GuiTrigger;
trigA->SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
trig2 = new GuiTrigger;
trig2->SetSimpleTrigger(-1, WPAD_BUTTON_2, 0);
2009-04-07 04:57:49 +02:00
keyBackImg = new GuiImage(keyMedium);
keyBackOverImg = new GuiImage(keyMediumOver);
keyBackText = new GuiText("Back", 22, (GXColor){0, 0, 0, 0xff});
keyBack = new GuiButton(keyMedium->GetWidth(), keyMedium->GetHeight());
keyBack->SetImage(keyBackImg);
keyBack->SetImageOver(keyBackOverImg);
keyBack->SetLabel(keyBackText);
keyBack->SetSoundOver(keySoundOver);
keyBack->SetSoundClick(keySoundClick);
keyBack->SetTrigger(trigA);
keyBack->SetTrigger(trig2);
2009-04-07 04:57:49 +02:00
keyBack->SetPosition(10*42+40, 0*42+80);
keyBack->SetEffectGrow();
this->Append(keyBack);
keyCapsImg = new GuiImage(keyMedium);
keyCapsOverImg = new GuiImage(keyMediumOver);
keyCapsText = new GuiText("Caps", 22, (GXColor){0, 0, 0, 0xff});
keyCaps = new GuiButton(keyMedium->GetWidth(), keyMedium->GetHeight());
keyCaps->SetImage(keyCapsImg);
keyCaps->SetImageOver(keyCapsOverImg);
keyCaps->SetLabel(keyCapsText);
keyCaps->SetSoundOver(keySoundOver);
keyCaps->SetSoundClick(keySoundClick);
keyCaps->SetTrigger(trigA);
keyCaps->SetTrigger(trig2);
2009-04-07 04:57:49 +02:00
keyCaps->SetPosition(0, 2*42+80);
keyCaps->SetEffectGrow();
this->Append(keyCaps);
keyShiftImg = new GuiImage(keyMedium);
keyShiftOverImg = new GuiImage(keyMediumOver);
keyShiftText = new GuiText("Shift", 22, (GXColor){0, 0, 0, 0xff});
keyShift = new GuiButton(keyMedium->GetWidth(), keyMedium->GetHeight());
keyShift->SetImage(keyShiftImg);
keyShift->SetImageOver(keyShiftOverImg);
keyShift->SetLabel(keyShiftText);
keyShift->SetSoundOver(keySoundOver);
keyShift->SetSoundClick(keySoundClick);
keyShift->SetTrigger(trigA);
keyShift->SetTrigger(trig2);
2009-04-07 04:57:49 +02:00
keyShift->SetPosition(21, 3*42+80);
keyShift->SetEffectGrow();
this->Append(keyShift);
keySpaceImg = new GuiImage(keyLarge);
keySpaceOverImg = new GuiImage(keyLargeOver);
keySpace = new GuiButton(keyLarge->GetWidth(), keyLarge->GetHeight());
keySpace->SetImage(keySpaceImg);
keySpace->SetImageOver(keySpaceOverImg);
keySpace->SetSoundOver(keySoundOver);
keySpace->SetSoundClick(keySoundClick);
keySpace->SetTrigger(trigA);
keySpace->SetTrigger(trig2);
2009-04-07 04:57:49 +02:00
keySpace->SetPosition(0, 4*42+80);
keySpace->SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
keySpace->SetEffectGrow();
this->Append(keySpace);
2009-07-08 07:56:58 +02:00
char txt[2] = { 0, 0 };
2009-04-07 04:57:49 +02:00
for(int i=0; i<4; i++)
{
for(int j=0; j<11; j++)
{
if(keys[i][j].ch != '\0')
{
2009-07-08 07:56:58 +02:00
txt[0] = keys[i][j].ch;
2009-04-07 04:57:49 +02:00
keyImg[i][j] = new GuiImage(key);
keyImgOver[i][j] = new GuiImage(keyOver);
2009-07-08 07:56:58 +02:00
keyTxt[i][j] = new GuiText(txt, 22, (GXColor){0, 0, 0, 0xff});
2009-04-07 04:57:49 +02:00
keyTxt[i][j]->SetAlignment(ALIGN_CENTRE, ALIGN_BOTTOM);
2010-04-12 07:57:42 +02:00
keyTxt[i][j]->SetPosition(0, -8);
2009-04-07 04:57:49 +02:00
keyBtn[i][j] = new GuiButton(key->GetWidth(), key->GetHeight());
keyBtn[i][j]->SetImage(keyImg[i][j]);
keyBtn[i][j]->SetImageOver(keyImgOver[i][j]);
keyBtn[i][j]->SetSoundOver(keySoundOver);
keyBtn[i][j]->SetSoundClick(keySoundClick);
keyBtn[i][j]->SetTrigger(trigA);
keyBtn[i][j]->SetTrigger(trig2);
2009-04-07 04:57:49 +02:00
keyBtn[i][j]->SetLabel(keyTxt[i][j]);
keyBtn[i][j]->SetPosition(j*42+21*i+40, i*42+80);
keyBtn[i][j]->SetEffectGrow();
this->Append(keyBtn[i][j]);
}
}
}
}
/**
* Destructor for the GuiKeyboard class.
*/
GuiKeyboard::~GuiKeyboard()
{
delete kbText;
delete keyTextbox;
delete keyTextboxImg;
delete keyCapsText;
delete keyCapsImg;
delete keyCapsOverImg;
delete keyCaps;
delete keyShiftText;
delete keyShiftImg;
delete keyShiftOverImg;
delete keyShift;
delete keyBackText;
delete keyBackImg;
delete keyBackOverImg;
delete keyBack;
delete keySpaceImg;
delete keySpaceOverImg;
delete keySpace;
delete key;
delete keyOver;
delete keyMedium;
delete keyMediumOver;
delete keyLarge;
delete keyLargeOver;
delete keySoundOver;
delete keySoundClick;
delete trigA;
delete trig2;
2009-04-07 04:57:49 +02:00
for(int i=0; i<4; i++)
{
for(int j=0; j<11; j++)
{
if(keys[i][j].ch != '\0')
{
delete keyImg[i][j];
delete keyImgOver[i][j];
delete keyTxt[i][j];
delete keyBtn[i][j];
}
}
}
}
void GuiKeyboard::Update(GuiTrigger * t)
{
if(_elements.size() == 0 || (state == STATE_DISABLED && parentElement))
return;
for (u8 i = 0; i < _elements.size(); i++)
{
try { _elements.at(i)->Update(t); }
catch (const std::exception& e) { }
2009-04-07 04:57:49 +02:00
}
2009-07-08 07:56:58 +02:00
bool update = false;
2009-04-07 04:57:49 +02:00
if(keySpace->GetState() == STATE_CLICKED)
{
if(strlen(kbtextstr) < kbtextmaxlen)
{
kbtextstr[strlen(kbtextstr)] = ' ';
kbText->SetText(kbtextstr);
}
keySpace->SetState(STATE_SELECTED, t->chan);
2009-04-07 04:57:49 +02:00
}
else if(keyBack->GetState() == STATE_CLICKED)
{
2010-10-28 00:09:23 +02:00
if(strlen(kbtextstr) > 0)
{
kbtextstr[strlen(kbtextstr)-1] = 0;
kbText->SetText(GetDisplayText(kbtextstr));
}
keyBack->SetState(STATE_SELECTED, t->chan);
2009-04-07 04:57:49 +02:00
}
else if(keyShift->GetState() == STATE_CLICKED)
{
shift ^= 1;
keyShift->SetState(STATE_SELECTED, t->chan);
2009-07-08 07:56:58 +02:00
update = true;
2009-04-07 04:57:49 +02:00
}
else if(keyCaps->GetState() == STATE_CLICKED)
{
caps ^= 1;
keyCaps->SetState(STATE_SELECTED, t->chan);
2009-07-08 07:56:58 +02:00
update = true;
2009-04-07 04:57:49 +02:00
}
char txt[2] = { 0, 0 };
2009-07-16 19:47:47 +02:00
startloop:
2009-04-07 04:57:49 +02:00
for(int i=0; i<4; i++)
{
for(int j=0; j<11; j++)
{
if(keys[i][j].ch != '\0')
{
2009-07-08 07:56:58 +02:00
if(update)
{
if(shift || caps)
txt[0] = keys[i][j].chShift;
else
txt[0] = keys[i][j].ch;
2009-04-07 04:57:49 +02:00
2009-07-08 07:56:58 +02:00
keyTxt[i][j]->SetText(txt);
}
2009-04-07 04:57:49 +02:00
if(keyBtn[i][j]->GetState() == STATE_CLICKED)
{
if(strlen(kbtextstr) < kbtextmaxlen)
{
if(shift || caps)
{
kbtextstr[strlen(kbtextstr)] = keys[i][j].chShift;
}
else
{
kbtextstr[strlen(kbtextstr)] = keys[i][j].ch;
}
}
2009-07-08 07:56:58 +02:00
kbText->SetText(GetDisplayText(kbtextstr));
keyBtn[i][j]->SetState(STATE_SELECTED, t->chan);
2009-07-16 19:47:47 +02:00
if(shift)
{
shift ^= 1;
update = true;
goto startloop;
}
2009-04-07 04:57:49 +02:00
}
}
}
}
this->ToggleFocus(t);
if(focus) // only send actions to this window if it's in focus
{
// pad/joystick navigation
if(t->Right())
this->MoveSelectionHor(1);
else if(t->Left())
this->MoveSelectionHor(-1);
else if(t->Down())
this->MoveSelectionVert(1);
else if(t->Up())
this->MoveSelectionVert(-1);
}
}