[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[netPanzer-CVS] netpanzer/src/Lib/View cInputField.cpp cInputFi...
From: |
Hankin Chick |
Subject: |
[netPanzer-CVS] netpanzer/src/Lib/View cInputField.cpp cInputFi... |
Date: |
Fri, 14 Nov 2003 06:28:29 -0500 |
CVSROOT: /cvsroot/netpanzer
Module name: netpanzer
Branch:
Changes by: Hankin Chick <address@hidden> 03/11/14 06:28:29
Modified files:
src/Lib/View : cInputField.cpp cInputField.hpp
Log message:
added insert,repeat,strings longer than width to cInputField
Patches:
Index: netpanzer/src/Lib/View/cInputField.cpp
diff -u netpanzer/src/Lib/View/cInputField.cpp:1.15
netpanzer/src/Lib/View/cInputField.cpp:1.16
--- netpanzer/src/Lib/View/cInputField.cpp:1.15 Tue Nov 11 08:52:40 2003
+++ netpanzer/src/Lib/View/cInputField.cpp Fri Nov 14 06:28:28 2003
@@ -18,10 +18,13 @@
#include <config.h>
#include <string.h>
+#include <ctype.h>
#include "Color.hpp"
#include "Exception.hpp"
#include "cInputField.hpp"
#include "TimerInterface.hpp"
+#include "KeyboardInterface.hpp"
+#include "Log.hpp"
////////////////////////////////////////////////////////////////////////////
// cInputFieldString definitions.
@@ -29,10 +32,12 @@
// init
//--------------------------------------------------------------------------
-void cInputFieldString::init(const char *string, int maxCharCount)
+void cInputFieldString::init(const char *string, int maxCharCount, int
maxWidth)
{
cInputFieldString::maxCharCount = maxCharCount;
+ cInputFieldString::maxWidth = maxWidth;
assert(maxCharCount > 0);
+ assert(maxWidth > 0);
cInputFieldString::string = new char [maxCharCount + 1];
@@ -47,6 +52,8 @@
// setString
void cInputFieldString::setString(const char *string)
{
+// XXX notify cinputfield to resetString positions
+
strncpy(this->string, string, maxCharCount);
} // end setString
@@ -73,8 +80,18 @@
destString = 0;
excludedCharacters = 0;
returnaction = 0;
+ strDisplayStart = 0;
+ depressedKey = 0;
+ depressedKeyTimeNext = 0;
+ insertMode = true;
} // end reset
+void cInputField::resetString()
+{
+ strDisplayStart = 0;
+ cursorPos = 0;
+}
+
// setPos
//--------------------------------------------------------------------------
void cInputField::setPos(iXY pos)
@@ -94,10 +111,11 @@
this->destString = string->string;
this->maxCharCount = string->maxCharCount;
+ this->maxWidth = string->maxWidth;
iXY size;
// XXX (8 is hardcoded here...)
- size.x = maxCharCount * 8 + 8;
+ size.x = string->maxWidth * 8 + 8;
size.y = Surface::getFontHeight() + 4;
bounds.max = bounds.min + size;
@@ -123,14 +141,22 @@
//--------------------------------------------------------------------------
void cInputField::addChar(int newChar)
{
+ pressKey(newChar);
// Check if the character should be excluded.
if (strchr(excludedCharacters, newChar) == 0) {
- size_t length = strlen(destString) + 1;
// Add the character.
+ if(insertMode) {
+ // ins, move stuff forward
+ if((cursorPos+1)<maxCharCount) {
+ memmove(destString+cursorPos+1,destString+cursorPos,
+ maxCharCount-(cursorPos+1));
+ }
+ }
+ int replacedChar=destString[cursorPos];
destString[cursorPos] = newChar;
- if (length <= maxCharCount) {
- destString[length] = '\0';
+ if (cursorPos <= maxCharCount) {
+ if(replacedChar=='\0') destString[cursorPos+1] = '\0';
} else {
destString[maxCharCount] = '\0';
}
@@ -152,6 +178,7 @@
//--------------------------------------------------------------------------
void cInputField::addExtendedChar(int newExtendedChar)
{
+ pressKey(newExtendedChar);
// Process the extendedChar accordingly.
switch (newExtendedChar) {
case SDLK_HOME: {
@@ -186,7 +213,9 @@
}
break;
- case SDLK_INSERT: {}
+ case SDLK_INSERT: {
+ insertMode^=1;
+ }
break;
case SDLK_DELETE: {
@@ -230,10 +259,11 @@
void cInputField::draw(Surface &dest)
{
checkCursor();
+ checkRepeat();
inputFieldSurface.fill(Color::black);
inputFieldSurface.drawButtonBorder(Color::white, Color::gray64);
- inputFieldSurface.bltString(4, 2, destString, Color::white);
+ inputFieldSurface.bltString(4, 2, destString+strDisplayStart,
Color::white);
inputFieldSurface.blt(dest, pos);
} // draw
@@ -242,10 +272,11 @@
void cInputField::drawHighlighted(Surface &dest)
{
checkCursor();
+ checkRepeat();
inputFieldSurface.fill(Color::black);
inputFieldSurface.drawButtonBorder(Color::darkGray, Color::white);
- inputFieldSurface.bltStringShadowed(4, 2, destString, Color::white,
Color::black);
+ inputFieldSurface.bltStringShadowed(4, 2, destString+strDisplayStart,
Color::white, Color::black);
static float timeForBlink = 0.0f;
if ((timeForBlink += TimerInterface::getTimeSlice()) > 0.25f) {
@@ -253,7 +284,8 @@
timeForBlink = 0.0f;
}
} else {
- if (cursorPos >= maxCharCount) {
+ int cursorPos=cInputField::cursorPos-strDisplayStart;
+ if ((size_t)cursorPos >= maxCharCount) {
// XXX hardcoded CHAR_PIXX (8)
inputFieldSurface.bltString(((cursorPos - 1) * 8) + 4, 2, "_",
Color::red);
} else {
@@ -275,4 +307,39 @@
if (cursorPos > strlen(destString)) {
cursorPos = strlen(destString);
}
+ if(((size_t)strDisplayStart)>cursorPos) {
+ strDisplayStart=cursorPos;
+ }
+ else if(((size_t)(strDisplayStart+maxWidth))<=cursorPos) {
+ strDisplayStart=cursorPos-maxWidth;
+ }
} // end checkCursor
+
+// check repeat and insert characters as needed
+void cInputField::checkRepeat()
+{
+ if(depressedKey==0) { return; }
+ Uint32 ticks=SDL_GetTicks();
+ if(depressedKeyTimeNext>ticks) {
+ return;
+ }
+ if(KeyboardInterface::getKeyState(depressedKey)!=true) {
+ // we've let go of this key.
+ depressedKey=0;
+ return;
+ }
+
+ if(isprint(depressedKey)) {
+ addChar(depressedKey);
+ }
+ else { addExtendedChar(depressedKey); }
+
+ depressedKeyTimeNext=ticks+50;
+}
+
+void cInputField::pressKey(int ch)
+{
+ depressedKey=ch;
+ depressedKeyTimeNext=SDL_GetTicks()+250;
+}
+
Index: netpanzer/src/Lib/View/cInputField.hpp
diff -u netpanzer/src/Lib/View/cInputField.hpp:1.10
netpanzer/src/Lib/View/cInputField.hpp:1.11
--- netpanzer/src/Lib/View/cInputField.hpp:1.10 Tue Nov 11 08:52:40 2003
+++ netpanzer/src/Lib/View/cInputField.hpp Fri Nov 14 06:28:29 2003
@@ -29,6 +29,7 @@
private:
int maxCharCount;
+ int maxWidth; // max width in the gui
char *string;
public:
@@ -50,7 +51,10 @@
}
}
- void init(const char *string, int maxCharCount);
+ void init(const char *string, int maxCharCount,int maxWidth);
+ void init(const char *string, int maxCharCount) {
+ init(string,maxCharCount,maxCharCount);
+ }
void setString(const char *string);
void reset();
@@ -110,10 +114,16 @@
void draw(Surface &dest);
void drawHighlighted(Surface &dest);
void checkCursor();
+ void resetString(); // reset string position
private:
iXY pos;
size_t maxCharCount;
+ int strDisplayStart; // which char to start displaying from
+ int maxWidth; // max width in the gui
+ int depressedKey; // currently depressed key
+ unsigned int depressedKeyTimeNext; // tick to repeat the key
+ bool insertMode;
char *destString;
char *excludedCharacters;
iRect bounds;
@@ -121,6 +131,8 @@
Surface inputFieldSurface;
ACTION_FUNC_PTR returnaction;
+ void pressKey(int ch);
+ void checkRepeat();
void reset();
}; // end cInputField