some progress concerning IMGUI widgets (added simple text input)

This commit is contained in:
2010-09-11 02:28:50 +02:00
parent 54ea3c21d4
commit 3f962163b3
13 changed files with 299 additions and 74 deletions
+12
View File
@@ -57,6 +57,12 @@ int ControllerBase::OnInit (int argc, char* argv[]) {
uistate.activeitem = 0;
uistate.hotitem = 0;
uistate.kbditem = 0;
uistate.last_keysym = SDLK_CLEAR;
uistate.last_unicode = 0;
uistate.lastwidget = 0;
ControllerInstance = this;
return 0;
@@ -158,6 +164,12 @@ void ControllerBase::IMGUIFinish () {
/** \brief Keyboard processing */
bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) {
mButtonStates.set(keysym.sym, true);
uistate.last_keysym = keysym.sym;
// Only when Unicode processing is activated store the unicode value
if (SDL_EnableUNICODE(-1)) {
uistate.last_unicode = keysym.unicode;
}
if (mView->mOverlayManager.SendKeyDown (keysym))
return true;
+11
View File
@@ -16,6 +16,12 @@ class Module;
struct UIState {
int hotitem;
int activeitem;
int kbditem;
SDLKey last_keysym;
Uint16 last_unicode;
int lastwidget;
};
/** \brief Defines the number of keys (keyboard + mous) that we can bind to.
@@ -67,6 +73,11 @@ class ControllerBase : public Module {
void IMGUIPrepare () {
uistate.hotitem = 0;
}
void IMGUIClear () {
LogMessage ("Called IMGUIClear()");
uistate.hotitem = 0;
uistate.kbditem = 0;
}
void IMGUIFinish ();
UIState uistate;
+196 -3
View File
@@ -30,6 +30,15 @@ bool regionhit (int x, int y, int w, int h) {
return true;
}
/** \brief Draws a label with a given caption at the position (vertically centered)
*
* This function draws the label at the horizontal x position and centers the
* label with regard to the height of the rendered caption.
*
* \TODO [med] The vertical alignment around the center of the vertical height
* of the string is rather unfortunate as different contents will be rendered
* at different vertical positions (e.g. "aaa" and "ggg")
*/
void Label (int id, const char* caption, int x, int y) {
if (caption != NULL) {
float width, height;
@@ -43,6 +52,9 @@ void Label (int id, const char* caption, int x, int y) {
}
/** \brief Draws a button at the given position
*
* The area defined by the parameters defines the clickable area. However
* decorations might be drawn on a smaller or bigger area.
*
* \returns true if it was clicked
*
@@ -51,6 +63,8 @@ bool Button (int id, const char* caption, int x, int y, int w, int h) {
controller = EngineGetController();
assert (controller);
// LogMessage ("id = %d hotitem = %d activeitem = %d kbditem = %d", id, controller->uistate.hotitem, controller->uistate.activeitem, controller->uistate.kbditem);
// Check for hotness
if (regionhit (x, y, w, h)) {
controller->uistate.hotitem = id;
@@ -59,11 +73,22 @@ bool Button (int id, const char* caption, int x, int y, int w, int h) {
controller->uistate.activeitem = id;
}
// If nothing is selected
if (controller->uistate.hotitem != 0) {
controller->uistate.kbditem = controller->uistate.hotitem;
}
if (controller->uistate.kbditem == 0) {
controller->uistate.hotitem = id;
controller->uistate.kbditem = id;
}
// Render
glColor3f (0.2, 0.2, 0.2);
DrawRect2D (x + 4, y + 4, w, h);
if (controller->uistate.hotitem == id) {
if (controller->uistate.hotitem == id
|| controller->uistate.kbditem == id) {
if (controller->uistate.activeitem == id) {
glColor3f (0.8, 0.8, 0.8);
DrawRect2D (x, y, w, h);
@@ -87,11 +112,179 @@ bool Button (int id, const char* caption, int x, int y, int w, int h) {
view->DrawGLString(x + w * 0.5 - width * 0.5, y + h * 0.5 - height * 0.5, caption);
}
// Logic
// Mouse Logic
if (controller->GetButtonState(MouseButtonLeft) == false
&& controller->uistate.hotitem == id
&& controller->uistate.activeitem == id)
&& controller->uistate.activeitem == id) {
controller->uistate.lastwidget = id;
return true;
}
// Keyboard Logic
if (controller->uistate.kbditem == id) {
// We have to make sure, that we always clear the uistate.last_keysym
// value, otherwise the same action might be repeated over and over.
switch (controller->uistate.last_keysym) {
case SDLK_DOWN:
controller->uistate.kbditem = 0;
controller->uistate.hotitem = 0;
controller->uistate.last_keysym = SDLK_CLEAR;
break;
case SDLK_UP:
controller->uistate.kbditem = controller->uistate.lastwidget;
controller->uistate.hotitem = controller->uistate.lastwidget;
controller->uistate.last_keysym = SDLK_CLEAR;
break;
case SDLK_RETURN:
controller->uistate.last_keysym = SDLK_CLEAR;
// As we (probably) exit the current set of widgets, we have to clear
// the uistate.kbditem value.
controller->uistate.kbditem = 0;
return true;
break;
}
}
controller->uistate.lastwidget = id;
return false;
}
/** \brief An Edit widget that allows editing of a string consisting of one line
*
* \TODO [med] vertical alignment (especially of text) is a bit whacky
*
* \returns true if it was clicked
*
*/
bool LineEdit (int id, int x, int y, std::string &text_value, const int &maxlength) {
controller = EngineGetController();
assert (controller);
int w = maxlength * 16;
int h = 16;
// LogMessage ("id = %d hotitem = %d activeitem = %d kbditem = %d", id, controller->uistate.hotitem, controller->uistate.activeitem, controller->uistate.kbditem);
// Check for hotness
if (regionhit (x, y, w, h)) {
controller->uistate.hotitem = id;
if (controller->uistate.activeitem == 0
&& controller->GetButtonState(MouseButtonLeft))
controller->uistate.activeitem = id;
}
// If nothing is selected
if (controller->uistate.hotitem != 0) {
controller->uistate.kbditem = controller->uistate.hotitem;
}
if (controller->uistate.kbditem == 0) {
controller->uistate.hotitem = id;
controller->uistate.kbditem = id;
}
// Render
glColor3f (0.2, 0.2, 0.2);
DrawRect2D (x + 4, y + 4, w, h);
if (controller->uistate.hotitem == id
|| controller->uistate.kbditem == id) {
if (controller->uistate.activeitem == id) {
glColor3f (0.8, 0.8, 0.8);
DrawRect2D (x, y, w, h);
} else {
glColor3f (0.7, 0.7, 0.7);
DrawRect2D (x, y, w, h);
}
} else {
glColor3f (0.4, 0.4, 0.4);
DrawRect2D (x, y, w, h);
}
// Rendering of the current value
float width, height;
view = EngineGetView ();
assert (view);
glColor3f (1., 1., 1.);
std::string text_output = text_value;
if (controller->uistate.kbditem == id && SDL_GetTicks() >> 9 & 1)
text_output += "_";
view->DrawGLStringMeasure(text_value.c_str(), &width, &height);
view->DrawGLString(x + 16, y + 12, text_output.c_str());
// Keyboard Logic
if (controller->uistate.kbditem == id) {
switch (controller->uistate.last_keysym) {
case SDLK_DOWN:
controller->uistate.kbditem = 0;
controller->uistate.hotitem = 0;
controller->uistate.last_keysym = SDLK_CLEAR;
break;
case SDLK_UP:
controller->uistate.kbditem = controller->uistate.lastwidget;
controller->uistate.hotitem = controller->uistate.lastwidget;
controller->uistate.last_keysym = SDLK_CLEAR;
break;
case SDLK_CLEAR:
controller->uistate.last_keysym = SDLK_CLEAR;
controller->uistate.last_unicode = 0;
return false;
break;
case SDLK_ESCAPE:
controller->uistate.last_keysym = SDLK_CLEAR;
controller->uistate.last_unicode = 0;
controller->uistate.hotitem = 0;
controller->uistate.kbditem = 0;
return false;
break;
case SDLK_RETURN:
controller->uistate.last_keysym = SDLK_CLEAR;
controller->uistate.last_unicode = 0;
controller->uistate.hotitem = 0;
controller->uistate.kbditem = 0;
return true;
break;
case SDLK_BACKSPACE:
if (text_value.size() > 0) {
text_value = text_value.substr(0, text_value.size() - 1);
controller->uistate.last_keysym = SDLK_CLEAR;
controller->uistate.last_unicode = 0;
return true;
}
break;
default:
// The raw input processing
if (maxlength > 0 && text_value.size() < maxlength) {
if (controller->uistate.last_unicode) {
if ((controller->uistate.last_unicode & 0xFF80) == 0) {
text_value += controller->uistate.last_unicode & 0x7F;
controller->uistate.last_unicode = 0;
return true;
} else {
LogWarning ("Input key not supported!");
return false;
}
}
}
break;
}
}
controller->uistate.lastwidget = id;
// Mouse Logic
if (controller->GetButtonState(MouseButtonLeft) == false
&& controller->uistate.hotitem == id
&& controller->uistate.activeitem == id) {
controller->uistate.kbditem = id;
}
return false;
}
+4
View File
@@ -1,6 +1,8 @@
#ifndef _IMGUICONTROLS_H
#define _IMGUICONTROLS_H
#include <string>
namespace Engine {
namespace GUI {
@@ -20,6 +22,8 @@ void Label (int id, const char* caption, int x, int y);
*/
bool Button (int id, const char* caption, int x, int y, int w, int h);
bool LineEdit (int id, int x, int y, std::string &text_value, const int &maxlength);
};
};
+1 -1
View File
@@ -73,7 +73,7 @@ class ModelBase : public Module {
void SendEntityCollisionEvent (const unsigned int reference_entity_id,
const unsigned int incidence_entity_id, float collision_time, vector3d normal);
void SetGameState (const unsigned int &state) {
virtual void SetGameState (const unsigned int &state) {
mLastGameState = mGameState;
mGameState = state;
};
+13 -3
View File
@@ -146,7 +146,7 @@ void ViewBase::UpdateCamera () {
mCamera->Update ();
}
void ViewBase::Draw () {
void ViewBase::PreDraw() {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -166,7 +166,13 @@ void ViewBase::Draw () {
last_fps_update = 0;
frame_counter = 0;
}
}
void ViewBase::Draw () {
// Perform pre-Draw actions
PreDraw ();
// Actual Drawing
UpdateCamera ();
if (mDrawGrid)
@@ -179,7 +185,11 @@ void ViewBase::Draw () {
mOverlayManager.Draw();
// and update the screen
// Perform post-Draw actions
PostDraw();
}
void ViewBase::PostDraw() {
SDL_GL_SwapBuffers ();
}
+5
View File
@@ -27,6 +27,11 @@ class ViewBase : public Module{
/** \brief Resizes the View */
void Resize (int width, int height);
/** \brief Performs actions before drawing (e.g. timer stuff) */
void PreDraw();
/** \brief Performs actions after drawing (e.g. swapping of buffers, etc.) */
void PostDraw();
/** \brief Performs all drawing */
virtual void Draw ();