/** * @brief Called when the node got the focus */ void uiTextEntryNode::onFocusGained (uiNode_t* node) { assert(editedCvar == nullptr); /* skip '*cvar ' */ const char* cvarRef = "*cvar:"; editedCvar = Cvar_Get(&((const char*)node->text)[strlen(cvarRef)]); assert(editedCvar); Q_strncpyz(cvarValueBackup, editedCvar->string, sizeof(cvarValueBackup)); isAborted = false; EXTRADATA(node).cursorPosition = UTF8_strlen(editedCvar->string); #if SDL_VERSION_ATLEAST(2,0,0) SDL_StartTextInput(); vec2_t pos; UI_GetNodeAbsPos(node, pos); SDL_Rect r = {static_cast<int>(pos[0]), static_cast<int>(pos[1]), static_cast<int>(node->box.size[0]), static_cast<int>(node->box.size[1])}; SDL_SetTextInputRect(&r); #else #ifdef ANDROID char buf[MAX_CVAR_EDITING_LENGTH]; Q_strncpyz(buf, editedCvar->string, sizeof(buf)); SDL_ANDROID_GetScreenKeyboardTextInput(buf, sizeof(buf)); Cvar_ForceSet(editedCvar->name, buf); UI_TextEntryNodeValidateEdition(node); UI_RemoveFocus(); #endif #endif }
/** * @brief Check call to SDL_SetTextInputRect with invalid data * * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect */ int keyboard_setTextInputRectNegative(void *arg) { /* Some platforms set also an error message; prepare for checking it */ #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA const char *expectedError = "Parameter 'rect' is invalid"; const char *error; SDL_ClearError(); SDLTest_AssertPass("Call to SDL_ClearError()"); #endif /* NULL refRect */ SDL_SetTextInputRect(NULL); SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)"); /* Some platforms set also an error message; so check it */ #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_COCOA error = SDL_GetError(); SDLTest_AssertPass("Call to SDL_GetError()"); SDLTest_AssertCheck(error != NULL, "Validate that error message was not NULL"); if (error != NULL) { SDLTest_AssertCheck(SDL_strcmp(error, expectedError) == 0, "Validate error message, expected: '%s', got: '%s'", expectedError, error); } SDL_ClearError(); SDLTest_AssertPass("Call to SDL_ClearError()"); #endif return TEST_COMPLETED; }
void CSDL_Ext::startTextInput(SDL_Rect * where) { if (SDL_IsTextInputActive() == SDL_FALSE) { SDL_StartTextInput(); } SDL_SetTextInputRect(where); }
void j1Input::StartTextInput(SDL_Rect* rect) { text_input = true; SDL_StartTextInput(); if(rect != NULL) SDL_SetTextInputRect(rect); }
void TextBox::OnFocusAcquired() { assert(SDL_IsTextInputActive() == SDL_FALSE); m_caret.StartAnimation(); SetBorderColor(SDLColor(64, 64, 128, 0)); auto loc = GetLocation(); SDL_SetTextInputRect(&loc); SDL_StartTextInput(); }
void Redraw() { int w = 0, h = textRect.h; SDL_Rect cursorRect, underlineRect; SDL_FillRect(screen, &textRect, backColor); #ifdef HAVE_SDL_TTF if (strlen(text)) { RenderText(screen, font, text, textRect.x, textRect.y, textColor); TTF_SizeUTF8(font, text, &w, &h); } #endif markedRect.x = textRect.x + w; markedRect.w = textRect.w - w; if (markedRect.w < 0) { SDL_Flip(screen); // Stop text input because we cannot hold any more characters SDL_StopTextInput(); return; } else { SDL_StartTextInput(); } cursorRect = markedRect; cursorRect.w = 2; cursorRect.h = h; SDL_FillRect(screen, &markedRect, backColor); if (markedText) { #ifdef HAVE_SDL_TTF RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor); TTF_SizeUTF8(font, markedText, &w, &h); #endif underlineRect = markedRect; underlineRect.y += (h - 2); underlineRect.h = 2; underlineRect.w = w; cursorRect.x += w + 1; SDL_FillRect(screen, &underlineRect, lineColor); } SDL_FillRect(screen, &cursorRect, lineColor); SDL_Flip(screen); SDL_SetTextInputRect(&markedRect); }
void window::set_text_input_rectangle(const math::rectangle& rectangle) { SDL_Rect sdl_rectangle; sdl_rectangle.x = rectangle.get_x(); sdl_rectangle.y = rectangle.get_y(); sdl_rectangle.w = rectangle.get_width(); sdl_rectangle.h = rectangle.get_height(); SDL_SetTextInputRect(&sdl_rectangle); }
void SDL2EventHandler::setTextArea(const Rect& area) { SDL_Rect r; r.x = area.x; r.y = area.y; r.w = area.w; r.h = area.h; SDL_SetTextInputRect(&r); }
/* Internal function to test SDL_SetTextInputRect */ void _testSetTextInputRect(SDL_Rect refRect) { SDL_Rect testRect; testRect = refRect; SDL_SetTextInputRect(&testRect); SDLTest_AssertPass("Call to SDL_SetTextInputRect with refRect(x:%i,y:%i,w:%i,h:%i)", refRect.x, refRect.y, refRect.w, refRect.h); SDLTest_AssertCheck( (refRect.x == testRect.x) && (refRect.y == testRect.y) && (refRect.w == testRect.w) && (refRect.h == testRect.h), "Check that input data was not modified, expected: x:%i,y:%i,w:%i,h:%i, got: x:%i,y:%i,w:%i,h:%i", refRect.x, refRect.y, refRect.w, refRect.h, testRect.x, testRect.y, testRect.w, testRect.h); }
static mrb_value mrb_sdl2_keyboard_text_input_rect(mrb_state *mrb, mrb_value mod) { mrb_value rect; mrb_get_args(mrb, "o", &rect); SDL_Rect const * const r = mrb_sdl2_rect_get_ptr(mrb, rect); if (NULL == r) { return mrb_nil_value(); } SDL_Rect tmp = *r; SDL_SetTextInputRect(&tmp); return mod; }
void SDL2InputBackend::startTextInput(const Rect & box, TextInputHandler * handler) { SDL_Rect rect; rect.x = box.left; rect.y = box.right; rect.w = box.width(); rect.h = box.height(); SDL_SetTextInputRect(&rect); if(!m_textHandler) { SDL_StartTextInput(); } else if(handler != m_textHandler && !m_editText.empty()) { m_textHandler->editingText(std::string(), 0, 0); handler->editingText(m_editText, m_editCursorPos, m_editCursorLength); } m_textHandler = handler; }
void UITextBox::Setup( const std::string& id, FloatRect position, SDL_Color bgColor, SDL_Color selectedColor, SDL_Color textColor, TTF_Font* font, int maxChars ) { Logger::Out( "Setup " + id, "UITextBox::Setup" ); m_id = id; m_position = position; m_textColor = textColor; m_bgColor = bgColor; m_defaultBgColor = bgColor; m_selectedBgColor = selectedColor; m_font = font; GenerateTexture(); SDL_Rect inputRect = position.ToSDLRect(); SDL_SetTextInputRect( &inputRect ); m_maxChars = maxChars; }
void Keyboard::setTextInput(bool enable, double x, double y, double w, double h) { // SDL_SetTextInputRect expects coordinates in window-space but setTextInput // takes pixels, so we should convert. auto window = Module::getInstance<window::Window>(M_WINDOW); if (window) { window->pixelToWindowCoords(&x, &y); window->pixelToWindowCoords(&w, &h); } SDL_Rect rect = {(int) x, (int) y, (int) w, (int) h}; SDL_SetTextInputRect(&rect); setTextInput(enable); }
Input::Input(Window & window): Label(window, "") { textColor(DEFAULT_COLOR_TEXT); bgColor(DEFAULT_COLOR_INPUT_BG); position(0, 0); size(DEFAULT_INPUT_WIDTH, DEFAULT_INPUT_HEIGHT); showBg(true); layout(LayoutLeft, LayoutMiddle); padding(1, 3, 2, 2); _charLimit = 256; _bGrating = false; _asciiOnly = false; _passwdOnly = false; _passwdMaskChar = DEFAULT_PASSWORD_CHAR; SDL_SetTextInputRect(&_rc); }
/** * Let the user insert text into an edit field object. * NOTE: The dlg[objnum].txt must point to an an array that is big enough * for dlg[objnum].w characters! */ static void SDLGui_EditField(SGOBJ *dlg, int objnum) { size_t cursorPos; /* Position of the cursor in the edit field */ int blinkState = 0; /* Used for cursor blinking */ int bStopEditing = false; /* true if user wants to exit the edit field */ char *txt; /* Shortcut for dlg[objnum].txt */ SDL_Rect rect; Uint32 grey, cursorCol; SDL_Event event; /* Enable text input to get unicode characters with SDL_TEXTINPUT event */ SDL_SetTextInputRect(&rect); SDL_StartTextInput(); grey = SDL_MapRGB(pSdlGuiScrn->format, 181, 183, 170); cursorCol = SDL_MapRGB(pSdlGuiScrn->format, 147, 145, 170); rect.x = (dlg[0].x + dlg[objnum].x) * sdlgui_fontwidth; rect.y = (dlg[0].y + dlg[objnum].y) * sdlgui_fontheight; rect.w = (dlg[objnum].w + 1) * sdlgui_fontwidth - 1; rect.h = dlg[objnum].h * sdlgui_fontheight; txt = dlg[objnum].txt; cursorPos = strlen(txt); do { /* Look for events */ if (SDL_PollEvent(&event) == 0) { /* No event: Wait some time for cursor blinking */ SDL_Delay(250); blinkState ^= 1; } else { /* Handle events */ do { switch (event.type) { case SDL_QUIT: /* User wants to quit */ bQuitProgram = true; bStopEditing = true; break; case SDL_MOUSEBUTTONDOWN: /* Mouse pressed -> stop editing */ bStopEditing = true; break; case SDL_KEYDOWN: /* Key pressed */ switch (event.key.keysym.sym) { case SDLK_RETURN: case SDLK_KP_ENTER: bStopEditing = true; break; case SDLK_LEFT: if (cursorPos > 0) cursorPos -= 1; break; case SDLK_RIGHT: if (cursorPos < strlen(txt)) cursorPos += 1; break; case SDLK_BACKSPACE: if (cursorPos > 0) { memmove(&txt[cursorPos-1], &txt[cursorPos], strlen(&txt[cursorPos])+1); cursorPos -= 1; } break; case SDLK_DELETE: if (cursorPos < strlen(txt)) memmove(&txt[cursorPos], &txt[cursorPos+1], strlen(&txt[cursorPos+1])+1); break; default: /* Get other keys from SDL_TEXTINPUT event */ break; } break; case SDL_TEXTINPUT: if (strlen(txt) < (size_t)dlg[objnum].w) { memmove(&txt[cursorPos+1], &txt[cursorPos], strlen(&txt[cursorPos])+1); txt[cursorPos] = event.text.text[0]; cursorPos += 1; } break; } break; } while (SDL_PollEvent(&event)); blinkState = 1; } /* Redraw the text field: */ SDL_FillRect(pSdlGuiScrn, &rect, grey); /* Draw background */ /* Draw the cursor: */ if (blinkState && !bStopEditing) { SDL_Rect cursorrect; cursorrect.x = rect.x + cursorPos * sdlgui_fontwidth; cursorrect.y = rect.y; cursorrect.w = sdlgui_fontwidth; cursorrect.h = rect.h; SDL_FillRect(pSdlGuiScrn, &cursorrect, cursorCol); } SDLGui_Text(rect.x, rect.y, dlg[objnum].txt); /* Draw text */ SDL_UpdateRects(pSdlGuiScrn, 1, &rect); } while (!bStopEditing); SDL_StopTextInput(); }
int Game::run(int screenWidth, int screenHeight, int GAME_SPEED, bool intro){ //Initialize SDL if(SDL_Init(SDL_INIT_EVERYTHING) != 0){ std::cerr<<"SDL_Init failed: "<<SDL_GetError()<<std::endl; return 1; } //Create a Window _window = SDL_CreateWindow("Guns Blood N' Ammo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight, SDL_WINDOW_OPENGL); if(_window == NULL){ std::cerr<<"Failed to create a SDL_Window: "<<SDL_GetError()<<std::endl; return 2; } _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED); if(_renderer == NULL){ std::cerr<<"Failed to create a SDL_Renderer: "<<SDL_GetError()<<std::endl; return 3; } if(TTF_Init() != 0){ std::cerr<<"TTF_Init failed: "<<SDL_GetError()<<std::endl; return 4; } SDL_ShowCursor(SDL_DISABLE); std::vector<const char*> menuItems, helpItems; menuItems.push_back("Guns Blood N' Ammo"); menuItems.push_back("New Game"); menuItems.push_back("Help"); menuItems.push_back("Options"); menuItems.push_back("Highscore"); menuItems.push_back("Exit"); helpItems.push_back("Help"); helpItems.push_back("WASD to control the player"); helpItems.push_back("Left mouse button to shoot"); helpItems.push_back("Kill all enemies to proceed"); helpItems.push_back("Press Space to return."); std::vector<const char*> optionItems = {"Options", "Crosshair Color: ", "Back"}; std::vector<const char*> highscoreItems = {"Highscore"}; ifstream ifs("highscore.txt"); string time; int counter = 1; while(ifs>>time){ char* output0 = new char[100]; strcpy(output0, std::to_string(counter++).c_str()); strcat(output0, ". "); strcat(output0, time.c_str()); strcat(output0, " s - "); string str; getline(ifs, str, '\n'); strcat(output0, str.c_str()); highscoreItems.push_back(output0); } ifs.close(); _menu = new Menu(menuItems, helpItems, optionItems, highscoreItems); SDL_Event input; char key; const static int MENU = 0; const static int GAMEPLAY = 1; const static int EXIT = 2; const static int GAMEOVER = 3; const static int NEXTLEVEL = 4; const static int WINNING = 5; _mouseL = _mouseR = false; if(intro){ SDL_Surface* image = IMG_Load("GBA.png"); SDL_Texture* image_ = SDL_CreateTextureFromSurface(_renderer, image); SDL_RenderCopy(_renderer, image_, NULL, NULL); SDL_RenderPresent(_renderer); SDL_Delay(2000); SDL_FreeSurface(image); SDL_DestroyTexture(image_); } int gameState = 0; while(gameState != EXIT){ auto fps_start = std::chrono::high_resolution_clock::now(); auto refresh_start = std::chrono::high_resolution_clock::now(); int frames = 0; if(_levels.empty()){ vector<pair<int, int>> enemies = { make_pair(840, 200), make_pair(775, 500), make_pair(320, 800) }; vector<pair<int, int>> enemies2 = { make_pair(16*64, 5*64), make_pair(30*64, 10*64)}; vector<pair<int, int>> enemies3 = {make_pair(16*64, 19*64), make_pair(17*64, 19*64), make_pair(16*64, 20*64), make_pair(17*64, 20*64)}; _levels= {new World(256, 256, enemies, "LEVEL1.TXT", "level1.png"), new World(128,128, enemies2, "LEVEL2.TXT", "level2.png"), new World(480+64,480+64, enemies3, "LEVEL3.TXT", "level3.png")}; _currentLevel = _levels.begin(); } if(gameState == NEXTLEVEL){ ++_currentLevel; if(_currentLevel == _levels.end()){ gameState = WINNING; } else gameState = GAMEPLAY; } while(gameState == MENU){ std::chrono::duration<double> elapsed = std::chrono::high_resolution_clock::now() - fps_start; if(elapsed.count() >= 1){ std::cout<<"FPS: "<<frames<<std::endl; frames = 0; fps_start = std::chrono::high_resolution_clock::now(); } else{ frames++; } if(SDL_PollEvent(&input)){ switch(input.type){ case SDL_QUIT: gameState = EXIT; break; case SDL_KEYDOWN: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = true; break; case SDL_KEYUP: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = false; break; } } if(gameState != EXIT) gameState = _menu->input(_keys); elapsed = std::chrono::duration_cast<chrono::milliseconds>(std::chrono::high_resolution_clock::now() - refresh_start); if(elapsed.count()*1000 >= GAME_SPEED/10){ _menu->update(); refresh_start = std::chrono::high_resolution_clock::now(); } _menu->draw(*_renderer); SDL_RenderPresent(_renderer); } while(gameState == GAMEPLAY){ std::chrono::duration<double> elapsed = std::chrono::high_resolution_clock::now() - fps_start; if(elapsed.count() >= 1){ _currtime += 1; std::cout<<"FPS: "<<frames<<std::endl; frames = 0; fps_start = std::chrono::high_resolution_clock::now(); } else{ frames++; } while(SDL_PollEvent(&input)){ switch(input.type){ case SDL_QUIT: gameState = EXIT; break; case SDL_KEYDOWN: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = true; break; case SDL_KEYUP: key = input.key.keysym.sym; if(isalpha(key)) key = toupper(input.key.keysym.sym); _keys[key] = false; break; case SDL_MOUSEMOTION: _mouseX = input.motion.x; _mouseY = input.motion.y; break; case SDL_MOUSEBUTTONDOWN: if(input.button.button == SDL_BUTTON_LEFT){ _mouseL = true; } if(input.button.button == SDL_BUTTON_RIGHT){ _mouseR = true; } break; case SDL_MOUSEBUTTONUP: if(input.button.button == SDL_BUTTON_LEFT){ _mouseL = false; } if(input.button.button == SDL_BUTTON_RIGHT){ _mouseR = false; } break; } } if(gameState != EXIT) gameState = (*_currentLevel)->input(_keys, _mouseX, _mouseY, _mouseR, _mouseL); elapsed = std::chrono::duration_cast<chrono::milliseconds>(std::chrono::high_resolution_clock::now() - refresh_start); if(elapsed.count()*1000 >= GAME_SPEED/10){ (*_currentLevel)->update(); refresh_start = std::chrono::high_resolution_clock::now(); } (*_currentLevel)->draw(*_renderer, _currtime); SDL_RenderPresent(_renderer); } if(gameState == GAMEOVER){ SDL_Surface* image = IMG_Load("gameover.png"); SDL_Texture* image_ = SDL_CreateTextureFromSurface(_renderer, image); SDL_RenderCopy(_renderer, image_, NULL, NULL); SDL_RenderPresent(_renderer); SDL_Delay(2000); SDL_FreeSurface(image); SDL_DestroyTexture(image_); _levels.clear(); gameState = 0; } if(gameState == WINNING){ SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255); SDL_RenderClear(_renderer); SDL_Color color = {255, 255, 255, 255}; SDL_Color bgColor = {0, 0, 0, 255}; TTF_Font* font = TTF_OpenFont("BloodLust.ttf", 72); SDL_Surface* surface = NULL; if(!(surface = TTF_RenderText_Shaded(font, "Victory", color, bgColor))){ std::cerr<<TTF_GetError()<<std::endl; } SDL_Texture* texture = NULL; texture = SDL_CreateTextureFromSurface(_renderer, surface); SDL_Rect pos = {640/2 - surface->w/2, 480/4, surface->w, surface->h}; SDL_RenderCopy(_renderer, texture, NULL, &pos); SDL_FreeSurface(surface); TTF_CloseFont(font); font = TTF_OpenFont("BloodLust.ttf", 48); char output[100]; strcpy(output,"Time: "); strcat(output,std::to_string(_currtime).c_str()); strcat(output," seconds."); const char* pointer = output; SDL_Surface* text_surface = NULL; SDL_Texture* text_texture = NULL; if(!(text_surface = TTF_RenderText_Shaded(font, pointer, color, bgColor))){ std::cerr<<TTF_GetError()<<std::endl; } if((text_texture = SDL_CreateTextureFromSurface(_renderer, text_surface)) == NULL){ std::cerr<<TTF_GetError()<<std::endl; } SDL_Rect pos2 = {640/2 - text_surface->w/2, 480/4*2, text_surface->w, text_surface->h}; SDL_RenderCopy(_renderer, text_texture, NULL, &pos2); SDL_FreeSurface(text_surface); vector<pair<int, string>> v; ifstream ifs("highscore.txt"); int i; while(ifs>>i){ string str; getline(ifs, str, '\n'); v.push_back(make_pair(i, str)); } if(_currtime <= v.at(v.size()-1).first){ SDL_StartTextInput(); SDL_Rect pos3 = {0, 0, 256, 32}; string text = ""; bool done = SDL_FALSE; string tecken; char output2[100]; SDL_SetTextInputRect(&pos3); while (!done) { SDL_Event textInputEvent; if (SDL_PollEvent(&textInputEvent)) { switch (textInputEvent.type) { case SDL_QUIT: /* Quit */ done = SDL_TRUE; break; case SDL_TEXTINPUT: /* Add new text onto the end of our text */ tecken.assign(textInputEvent.text.text, textInputEvent.text.text+1); text += tecken; std::cout<<text<<std::endl; break; case SDL_KEYDOWN: if(textInputEvent.key.keysym.sym == SDLK_BACKSPACE){ text = text.substr(0, text.length() - 1); } if(textInputEvent.key.keysym.sym == SDLK_RETURN){ //write to file done = SDL_TRUE; } break; } SDL_RenderClear(_renderer); SDL_RenderCopy(_renderer, texture, NULL, &pos); SDL_RenderCopy(_renderer, text_texture, NULL, &pos2); strcpy(output2,"Name: "); strcat(output2,text.c_str()); const char* pointer2 = output2; SDL_Surface* text_surface2 = NULL; SDL_Texture* text_texture2 = NULL; if(!(text_surface2 = TTF_RenderText_Shaded(font, pointer2, color, bgColor))){ std::cerr<<TTF_GetError()<<std::endl; } if((text_texture2 = SDL_CreateTextureFromSurface(_renderer, text_surface2)) == NULL){ std::cerr<<TTF_GetError()<<std::endl; } SDL_Rect pos4 = {640/2 - text_surface2->w/2, 480/4*3, text_surface2->w, text_surface2->h}; SDL_RenderCopy(_renderer, text_texture2, NULL, &pos4); SDL_RenderPresent(_renderer); SDL_FreeSurface(text_surface2); SDL_DestroyTexture(text_texture2); } } SDL_StopTextInput(); SDL_DestroyTexture(texture); SDL_DestroyTexture(text_texture); v.at(v.size()-1).first = _currtime; v.at(v.size()-1).second = text; sort(v.begin(), v.end()); ofstream highscore; highscore.open("highscore.txt"); for(auto it = v.cbegin(); it != v.cend(); ++it){ highscore<<it->first<<' '<<it->second<<"\n"; } highscore.close(); _menu->updateHighscore(); } else{ SDL_RenderPresent(_renderer); SDL_Delay(4000); } _levels.clear(); SDL_FlushEvents(SDL_QUIT, SDL_LASTEVENT); gameState = MENU; _currtime = 0; } }
void SDLAsyncRunner::RunClientLoop(client::IRenderer *renderer, client::IAudioDevice *audio) { client::AsyncRenderer *asyncRenderer = new client::AsyncRenderer(renderer, DispatchQueue::GetThreadQueue()); modState = 0; ClientThread *cliThread = new ClientThread(); cliThread->runner = this; cliThread->renderer = asyncRenderer; cliThread->audio = audio; cliThread->Start(); bool editing = false; bool absoluteMouseCoord = true; SPLog("Main event loop started"); try{ while(cliThread->IsAlive()) { modState = SDLRunner::GetModState(); if(currentView){ class SDLEventDispatch: public ConcurrentDispatch { SDLAsyncRunner *runner; SDL_Event ev; public: SDLEventDispatch(SDLAsyncRunner *runner, const SDL_Event& ev): runner(runner), ev(ev){ } virtual void Run() { View *view = runner->currentView; if(view){ runner->ProcessEvent(ev, view); } } }; SDL_Event event; if(SDL_WaitEvent(&event)){ { SDLEventDispatch *disp = new SDLEventDispatch(this, event); // FIXME: cliQueue may be deleted.. if(cliQueue){ disp->StartOn(cliQueue); disp->Release(); }else{ delete disp; } } while(SDL_PollEvent(&event)) { SDLEventDispatch *disp = new SDLEventDispatch(this, event); // FIXME: cliQueue may be deleted.. if(cliQueue){ disp->StartOn(cliQueue); disp->Release(); }else{ delete disp; } } } }else{ SDL_Event event; SDL_WaitEvent(&event); if(event.type == SDL_QUIT){ break; } } PulledState state; { AutoLocker guard(&stateMutex); state = this->state; } bool ed = state.acceptsTextInput; if(ed && !editing) { SDL_StartTextInput(); }else if(!ed && editing){ SDL_StopTextInput(); } editing = ed; if(editing){ SDL_SetTextInputRect(&state.textInputRect); } bool ab = state.needsAbsoluteMouseCoord; if(ab != absoluteMouseCoord) { absoluteMouseCoord = ab; SDL_SetRelativeMouseMode(absoluteMouseCoord?SDL_FALSE:SDL_TRUE); } DispatchQueue::GetThreadQueue()->ProcessQueue(); } }catch(const std::exception& ex){ rendererErrorOccured = true; SPLog("Renderer error:\n%s", ex.what()); cliThread->MarkForAutoDeletion(); SPLog("Main event loop terminated"); throw; } SPLog("Main event loop ended"); if(!clientError.empty()){ SPLog("Client reported an error: \n%s", clientError.c_str()); SPRaise("Client error:\n%s", clientError.c_str()); } }
void idris_SDL_setTextInputRect(int x, int y, int w, int h) { SDL_Rect rect = {x, y, w, h}; SDL_SetTextInputRect(&rect); }
/** * @brief Check call to SDL_SetTextInputRect * * @sa http://wiki.libsdl.org/moin.cgi/SDL_SetTextInputRect */ int keyboard_setTextInputRect(void *arg) { SDL_Rect refRect; /* Normal visible refRect, origin inside */ refRect.x = SDLTest_RandomIntegerInRange(1, 50);; refRect.y = SDLTest_RandomIntegerInRange(1, 50);; refRect.w = SDLTest_RandomIntegerInRange(10, 50); refRect.h = SDLTest_RandomIntegerInRange(10, 50); _testSetTextInputRect(refRect); /* Normal visible refRect, origin 0,0 */ refRect.x = 0; refRect.y = 0; refRect.w = SDLTest_RandomIntegerInRange(10, 50); refRect.h = SDLTest_RandomIntegerInRange(10, 50); _testSetTextInputRect(refRect); /* 1Pixel refRect */ refRect.x = SDLTest_RandomIntegerInRange(10, 50);; refRect.y = SDLTest_RandomIntegerInRange(10, 50);; refRect.w = 1; refRect.h = 1; _testSetTextInputRect(refRect); /* 0pixel refRect */ refRect.x = 1; refRect.y = 1; refRect.w = 1; refRect.h = 0; _testSetTextInputRect(refRect); /* 0pixel refRect */ refRect.x = 1; refRect.y = 1; refRect.w = 0; refRect.h = 1; _testSetTextInputRect(refRect); /* 0pixel refRect */ refRect.x = 1; refRect.y = 1; refRect.w = 0; refRect.h = 0; _testSetTextInputRect(refRect); /* 0pixel refRect */ refRect.x = 0; refRect.y = 0; refRect.w = 0; refRect.h = 0; _testSetTextInputRect(refRect); /* negative refRect */ refRect.x = SDLTest_RandomIntegerInRange(-200, -100);; refRect.y = SDLTest_RandomIntegerInRange(-200, -100);; refRect.w = 50; refRect.h = 50; _testSetTextInputRect(refRect); /* oversized refRect */ refRect.x = SDLTest_RandomIntegerInRange(1, 50);; refRect.y = SDLTest_RandomIntegerInRange(1, 50);; refRect.w = 5000; refRect.h = 5000; _testSetTextInputRect(refRect); /* NULL refRect */ SDL_SetTextInputRect(NULL); SDLTest_AssertPass("Call to SDL_SetTextInputRect(NULL)"); return TEST_COMPLETED; }
/** * Open the window and initialize sdl/opengl * @return True if the window opened successfully */ bool System::InitEngine() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) { ReportError("Erreur lors de l'initialisation de la SDL : " + std::string(SDL_GetError())); SDL_Quit(); return false; } else Log("SDL Initialized !"); if (TTF_Init() < 0) { TTF_Quit(); SDL_Quit(); return false; } else Log("SDL_TTF Initialized !"); //Set sdl gl attribute SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,4); SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,24); #ifdef _DEBUG SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); #endif _mainwindowPtr = SDL_CreateWindow("Rarium", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN); _width = 1280; _height = 720; if (!_mainwindowPtr) /* Die if creation failed */ { Log("SDL Window creation failed : " + std::string(SDL_GetError())); TTF_Quit(); SDL_Quit(); return false; } else Log("Window created"); _maincontext = SDL_GL_CreateContext(_mainwindowPtr); SDL_GL_SetSwapInterval(1); //SDL_ShowCursor(SDL_DISABLE); SDL_FlushEvents(SDL_FIRSTEVENT,SDL_LASTEVENT); PluginManager::GetRef().LoadPlugins(); SDL_StartTextInput(); static SDL_Rect rect; rect.x = 0; rect.y = 0; rect.w = 1280; rect.h = 720; SDL_SetTextInputRect(&rect); return true; }
/** * Let the user insert text into an edit field object. * NOTE: The dlg[objnum].txt must point to an an array that is big enough * for dlg[objnum].w characters! */ static void SDLGui_EditField(SGOBJ *dlg, int objnum) { size_t cursorPos; /* Position of the cursor in the edit field */ int blinkState = 0; /* Used for cursor blinking */ int bStopEditing = false; /* true if user wants to exit the edit field */ char *txt; /* Shortcut for dlg[objnum].txt */ SDL_Rect rect; SDL_Event event; #if !WITH_SDL2 int nOldUnicodeMode; #endif rect.x = (dlg[0].x + dlg[objnum].x) * sdlgui_fontwidth; rect.y = (dlg[0].y + dlg[objnum].y) * sdlgui_fontheight; rect.w = (dlg[objnum].w + 1) * sdlgui_fontwidth - 1; rect.h = dlg[objnum].h * sdlgui_fontheight; #if WITH_SDL2 SDL_SetTextInputRect(&rect); SDL_StartTextInput(); #else /* Enable unicode translation to get shifted etc chars with SDL_PollEvent */ nOldUnicodeMode = SDL_EnableUNICODE(true); #endif txt = dlg[objnum].txt; cursorPos = strlen(txt); do { /* Look for events */ if (SDL_PollEvent(&event) == 0) { /* No event: Wait some time for cursor blinking */ SDL_Delay(250); blinkState ^= 1; } else { /* Handle events */ do { switch (event.type) { case SDL_QUIT: /* User wants to quit */ bQuitProgram = true; bStopEditing = true; break; case SDL_MOUSEBUTTONDOWN: /* Mouse pressed -> stop editing */ bStopEditing = true; break; #if WITH_SDL2 case SDL_TEXTINPUT: if (strlen(txt) < (size_t)dlg[objnum].w) { memmove(&txt[cursorPos+1], &txt[cursorPos], strlen(&txt[cursorPos])+1); txt[cursorPos] = event.text.text[0]; cursorPos += 1; } break; #endif case SDL_KEYDOWN: /* Key pressed */ switch (event.key.keysym.sym) { case SDLK_RETURN: case SDLK_KP_ENTER: bStopEditing = true; break; case SDLK_LEFT: if (cursorPos > 0) cursorPos -= 1; break; case SDLK_RIGHT: if (cursorPos < strlen(txt)) cursorPos += 1; break; case SDLK_BACKSPACE: if (cursorPos > 0) { memmove(&txt[cursorPos-1], &txt[cursorPos], strlen(&txt[cursorPos])+1); cursorPos -= 1; } break; case SDLK_DELETE: if (cursorPos < strlen(txt)) memmove(&txt[cursorPos], &txt[cursorPos+1], strlen(&txt[cursorPos+1])+1); break; default: #if !WITH_SDL2 /* If it is a "good" key then insert it into the text field */ if (event.key.keysym.unicode >= 32 && event.key.keysym.unicode < 128 && event.key.keysym.unicode != PATHSEP) { if (strlen(txt) < (size_t)dlg[objnum].w) { memmove(&txt[cursorPos+1], &txt[cursorPos], strlen(&txt[cursorPos])+1); txt[cursorPos] = event.key.keysym.unicode; cursorPos += 1; } } #endif break; } break; } } while (SDL_PollEvent(&event)); blinkState = 1; } /* Redraw the text field: */ SDL_FillRect(pSdlGuiScrn, &rect, colors.midgrey); /* Draw background */ /* Draw the cursor: */ if (blinkState && !bStopEditing) { SDL_Rect cursorrect; cursorrect.x = rect.x + cursorPos * sdlgui_fontwidth; cursorrect.y = rect.y; cursorrect.w = sdlgui_fontwidth; cursorrect.h = rect.h; SDL_FillRect(pSdlGuiScrn, &cursorrect, colors.cursor); } SDLGui_Text(rect.x, rect.y, dlg[objnum].txt); /* Draw text */ SDL_UpdateRects(pSdlGuiScrn, 1, &rect); } while (!bStopEditing); #if WITH_SDL2 SDL_StopTextInput(); #else SDL_EnableUNICODE(nOldUnicodeMode); #endif }
inline void text_input_rect(SDL_Rect rect) noexcept { SDL_SetTextInputRect(&rect); }