From debff52b66a936290c9f65d40c22fc4c3d38efc8 Mon Sep 17 00:00:00 2001 From: pkeus Date: Tue, 28 Mar 2017 16:41:50 +0000 Subject: [PATCH] Improved TTextField widget: set cursor position when clicking, more efficient calculation of cursor position Fixed several cppcheck messages git-svn-id: https://svn.code.sf.net/p/extremetuxracer/code/trunk@693 0420edf4-82e4-42fc-9478-35b55e6d67a3 --- src/common.cpp | 4 ++-- src/common.h | 2 +- src/event.cpp | 4 ++-- src/event_select.cpp | 4 ++-- src/game_ctrl.cpp | 6 +++--- src/game_ctrl.h | 2 +- src/gui.cpp | 44 +++++++++++++++++++++++++------------------ src/gui.h | 5 +++-- src/help.cpp | 4 ++-- src/keyframe.h | 2 +- src/mathlib.h | 2 +- src/newplayer.cpp | 6 +++--- src/ogl.h | 2 +- src/ogl_test.cpp | 2 +- src/particles.cpp | 37 ++++++++++++++++++------------------ src/particles.h | 18 +++++++++--------- src/quadtree.cpp | 4 ++-- src/quadtree.h | 4 ++-- src/race_select.cpp | 4 ++-- src/regist.cpp | 4 ++-- src/score.cpp | 4 ++-- src/splash_screen.cpp | 2 +- src/tools.cpp | 8 ++++---- src/tux.cpp | 10 +++++----- src/winsys.cpp | 4 ++-- src/winsys.h | 2 +- 26 files changed, 99 insertions(+), 91 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index a76629d..46401f4 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -86,9 +86,9 @@ void PrintVector4(const TVector4d& v) { std::cout << v.x << " " << v.y << " " << v.z << " " << v.w << '\n'; } -void PrintColor(const sf::Color& v) { +void PrintColor(const sf::Color& c) { std::cout.precision(3); - std::cout << v.r << " " << v.g << " " << v.b << '\n'; + std::cout << c.r << " " << c.g << " " << c.b << '\n'; } void PrintVector2(const TVector2d& v) { diff --git a/src/common.h b/src/common.h index 674e6a6..e683916 100644 --- a/src/common.h +++ b/src/common.h @@ -107,7 +107,7 @@ bool DirExists(const char *dirname); void Message(const char *msg, const char *desc); void Message(const char *msg); void Message(const std::string& a, const std::string& b); -void Message(const std::string& a); +void Message(const std::string& msg); void SaveMessages(); // -------------------------------------------------------------------- diff --git a/src/event.cpp b/src/event.cpp index 849f857..92a1a90 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -183,12 +183,12 @@ int resultlevel(std::size_t num, std::size_t numraces) { return q + 1; } -void CEvent::Loop(float timestep) { +void CEvent::Loop(float time_step) { ScopedRenderMode rm(GUI); Winsys.clear(); if (param.ui_snow) { - update_ui_snow(timestep); + update_ui_snow(time_step); draw_ui_snow(); } DrawGUIBackground(Winsys.scale); diff --git a/src/event_select.cpp b/src/event_select.cpp index 7d9b282..0d7cc82 100644 --- a/src/event_select.cpp +++ b/src/event_select.cpp @@ -120,12 +120,12 @@ void CEventSelect::Enter() { Music.Play(param.menu_music, true); } -void CEventSelect::Loop(float timestep) { +void CEventSelect::Loop(float time_step) { ScopedRenderMode rm(GUI); Winsys.clear(); if (param.ui_snow) { - update_ui_snow(timestep); + update_ui_snow(time_step); draw_ui_snow(); } diff --git a/src/game_ctrl.cpp b/src/game_ctrl.cpp index 1515a32..fee954c 100644 --- a/src/game_ctrl.cpp +++ b/src/game_ctrl.cpp @@ -271,9 +271,9 @@ const std::string& CPlayers::GetDirectAvatarName(std::size_t avatar) const { // Character Administration // ******************************************************************** -CKeyframe* TCharacter::GetKeyframe(TFrameType type_) { - if (type_ < 0 || type_ >= NUM_FRAME_TYPES) return nullptr; - return &frames[type_]; +CKeyframe* TCharacter::GetKeyframe(TFrameType frametype) { + if (frametype < 0 || frametype >= NUM_FRAME_TYPES) return nullptr; + return &frames[frametype]; } diff --git a/src/game_ctrl.h b/src/game_ctrl.h index 20f8976..12bacc9 100644 --- a/src/game_ctrl.h +++ b/src/game_ctrl.h @@ -153,7 +153,7 @@ struct TCharacter { int type; bool finishframesok; - CKeyframe* GetKeyframe(TFrameType type); + CKeyframe* GetKeyframe(TFrameType frametype); }; class CCharacter { diff --git a/src/gui.cpp b/src/gui.cpp index 3fd05b3..9084b16 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -213,7 +213,7 @@ TTextField::TTextField(int x, int y, int width, int height, const sf::String& te : TWidget(x, y, width, height) , text(text_, FT.getCurrentFont(), FT.AutoSizeN(5)) , frame(sf::Vector2f(width-6.f, height-6.f)) - , cursorShape(sf::Vector2f(2.f, 26.f * Winsys.scale)) + , cursorShape(sf::Vector2f(2.f, 30.f * Winsys.scale)) , maxLng(32) , time(0.0) , cursor(false) { @@ -233,10 +233,10 @@ void TTextField::Draw() const { Winsys.draw(cursorShape); } -void TTextField::TextEnter(char key) { - if (key != '\b') { +void TTextField::TextEnter(char c) { + if (c != '\b') { sf::String string = text.getString(); - string.insert(cursorPos, key); + string.insert(cursorPos, c); text.setString(string); SetCursorPos(cursorPos+1); } @@ -244,16 +244,7 @@ void TTextField::TextEnter(char key) { void TTextField::SetCursorPos(std::size_t new_pos) { cursorPos = new_pos; - - float x = mouseRect.left + 20 - 2; - if (cursorPos != 0) { - sf::String temp = text.getString(); - - FT.AutoSizeN(5); - x += FT.GetTextWidth(temp.substring(0, cursorPos)); - } - - cursorShape.setPosition(x, mouseRect.top + 9); + cursorShape.setPosition(text.findCharacterPos(cursorPos).x, mouseRect.top + 9); } void TTextField::Focussed() { @@ -268,6 +259,23 @@ void TTextField::Focussed() { } } +bool TTextField::Click(int x, int y) { + if (TWidget::Click(x, y)) { + cursorPos = 0; + float first = text.findCharacterPos(cursorPos).x; + for (;;) { + float second = text.findCharacterPos(cursorPos + 1).x; + if ((first + second) / 2.f >= x || cursorPos >= text.getString().getSize()) + break; + cursorPos++; + first = second; + } + cursorShape.setPosition(text.findCharacterPos(cursorPos).x, mouseRect.top + 9); + return true; + } + return false; +} + static void eraseFromText(sf::Text& text, std::size_t pos) { sf::String str = text.getString(); str.erase(pos, 1); @@ -641,13 +649,13 @@ void DrawGUIFrame() { Winsys.draw(top_right); } -void DrawGUIBackground(float logoScale) { +void DrawGUIBackground(float scale) { DrawGUIFrame(); static sf::Sprite logo(Tex.GetSFTexture(T_TITLE)); - logoScale *= 0.5f; - logo.setScale(logoScale, logoScale); - logo.setPosition((Winsys.resolution.width - logo.getTextureRect().width*logoScale)/2, 5); + scale *= 0.5f; + logo.setScale(scale, scale); + logo.setPosition((Winsys.resolution.width - logo.getTextureRect().width*scale)/2, 5); Winsys.draw(logo); } diff --git a/src/gui.h b/src/gui.h index 5633902..5a406ab 100644 --- a/src/gui.h +++ b/src/gui.h @@ -115,7 +115,8 @@ class TTextField : public TWidget { public: TTextField(int x, int y, int width, int height, const sf::String& text_); void Draw() const; - void TextEnter(char text); + void TextEnter(char c); + bool Click(int x, int y); void Key(sf::Keyboard::Key key, bool released); void Focussed(); void UpdateCursor(float timestep); @@ -129,7 +130,7 @@ class TCheckbox : public TWidget { public: bool checked; - TCheckbox(int x, int y, int width_, const sf::String& tag_); + TCheckbox(int x, int y, int width, const sf::String& tag_); void Draw() const; void SetPosition(int x, int y); void SetChecked(bool c) { checked = c; } diff --git a/src/help.cpp b/src/help.cpp index bf560f4..28592e9 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -68,12 +68,12 @@ void CHelp::Enter() { footnote = AddLabel(Trans.Text(65), CENTER, AutoYPosN(90), colWhite); } -void CHelp::Loop(float timestep) { +void CHelp::Loop(float time_step) { ScopedRenderMode rm(GUI); Winsys.clear(); if (param.ui_snow) { - update_ui_snow(timestep); + update_ui_snow(time_step); draw_ui_snow(); } diff --git a/src/keyframe.h b/src/keyframe.h index a994053..75443da 100644 --- a/src/keyframe.h +++ b/src/keyframe.h @@ -60,7 +60,7 @@ public: void Update(float timestep); void UpdateTest(float timestep, CCharShape *shape); bool Load(const std::string& dir, const std::string& filename); - void CalcKeyframe(std::size_t idx, CCharShape *shape, const TVector3d& refpos) const; + void CalcKeyframe(std::size_t idx, CCharShape *shape, const TVector3d& refpos_) const; // test and editing TKeyframe *GetFrame(std::size_t idx); diff --git a/src/mathlib.h b/src/mathlib.h index 978f1af..546cb82 100644 --- a/src/mathlib.h +++ b/src/mathlib.h @@ -56,7 +56,7 @@ TMatrix<4, 4> RotateAboutVectorMatrix(const TVector3d& u, double angle); TQuaternion MultiplyQuaternions(const TQuaternion& q, const TQuaternion& r); TQuaternion ConjugateQuaternion(const TQuaternion& q); TMatrix<4, 4> MakeMatrixFromQuaternion(const TQuaternion& q); -TQuaternion MakeQuaternionFromMatrix(const TMatrix<4, 4>& mat); +TQuaternion MakeQuaternionFromMatrix(const TMatrix<4, 4>& m); TQuaternion MakeRotationQuaternion(const TVector3d& s, const TVector3d& t); TQuaternion InterpolateQuaternions(const TQuaternion& q, TQuaternion r, double t); TVector3d RotateVector(const TQuaternion& q, const TVector3d& v); diff --git a/src/newplayer.cpp b/src/newplayer.cpp index 7fd320b..1a70ecb 100644 --- a/src/newplayer.cpp +++ b/src/newplayer.cpp @@ -108,18 +108,18 @@ void CNewPlayer::Enter() { textfield = AddTextField(emptyString, area.left, frametop, framewidth, frameheight); } -void CNewPlayer::Loop(float timestep) { +void CNewPlayer::Loop(float time_step) { sf::Color col; ScopedRenderMode rm(GUI); Winsys.clear(); if (param.ui_snow) { - update_ui_snow(timestep); + update_ui_snow(time_step); draw_ui_snow(); } - textfield->UpdateCursor(timestep); + textfield->UpdateCursor(time_step); DrawGUIBackground(Winsys.scale); diff --git a/src/ogl.h b/src/ogl.h index b60806b..a2a039f 100644 --- a/src/ogl.h +++ b/src/ogl.h @@ -48,7 +48,7 @@ void check_gl_error(); void InitOpenglExtensions(); void PrintGLInfo(); -void set_material_diffuse(const sf::Color& specular_colour); +void set_material_diffuse(const sf::Color& diffuse_colour); void set_material(const sf::Color& diffuse_colour, const sf::Color& specular_colour, float specular_exp); diff --git a/src/ogl_test.cpp b/src/ogl_test.cpp index 657236c..1288a1d 100644 --- a/src/ogl_test.cpp +++ b/src/ogl_test.cpp @@ -50,7 +50,7 @@ void COglTest::Keyb(sf::Keyboard::Key key, bool release, int x, int y) { } } -void COglTest::Loop(float timestep) { +void COglTest::Loop(float time_step) { // ------------- 3d scenery --------------------------------------- ScopedRenderMode rm(TUX); ClearRenderContext(colDDBackgr); diff --git a/src/particles.cpp b/src/particles.cpp index 62bee1e..30d3b57 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -473,27 +473,26 @@ void TFlake::Draw(const TPlane& lp, const TPlane& rp, bool rotate_flake, float d TFlakeArea::TFlakeArea( std::size_t num_flakes, - float _xrange, - float _ytop, - float _yrange, - float _zback, - float _zrange, - float _minSize, - float _maxSize, - float _speed, - bool rotate) { - xrange = _xrange; - ytop = _ytop; - yrange = _yrange; - zback = _zback; - zrange = _zrange; - minSize = _minSize; - maxSize = _maxSize; - speed = _speed; + float xrange_, + float ytop_, + float yrange_, + float zback_, + float zrange_, + float minSize_, + float maxSize_, + float speed_, + bool rotate) + : flakes(num_flakes) { + xrange = xrange_; + ytop = ytop_; + yrange = yrange_; + zback = zback_; + zrange = zrange_; + minSize = minSize_; + maxSize = maxSize_; + speed = speed_; rotate_flake = rotate; left = right = bottom = top = front = back = 0.f; - - flakes.resize(num_flakes); } void TFlakeArea::Draw(const CControl *ctrl) const { diff --git a/src/particles.h b/src/particles.h index f75c52f..8631936 100644 --- a/src/particles.h +++ b/src/particles.h @@ -75,14 +75,14 @@ struct TFlakeArea { TFlakeArea( std::size_t num_flakes, - float xrange, - float ytop, - float yrange, - float zback, - float zrange, - float minSize, - float maxSize, - float speed, + float xrange_, + float ytop_, + float yrange_, + float zback_, + float zrange_, + float minSize_, + float maxSize_, + float speed_, bool rotate); void Draw(const CControl* ctrl) const; void Update(float timestep, float xcoeff, float ycoeff, float zcoeff); @@ -143,7 +143,7 @@ struct TCurtain { float base_speed, float start_angle, float min_height, - int curt_texture); + int dense); void SetStartParams(const CControl* ctrl); void Draw() const; void Update(float timestep, const TVector3d& drift, const CControl* ctrl); diff --git a/src/quadtree.cpp b/src/quadtree.cpp index f7c0860..faa0adc 100644 --- a/src/quadtree.cpp +++ b/src/quadtree.cpp @@ -1050,8 +1050,8 @@ void quadsquare::SetScale(double x, double z) { } CourseFields* quadsquare::Fields; -void quadsquare::SetFields(CourseFields* t) { - Fields = t; +void quadsquare::SetFields(CourseFields* fields) { + Fields = fields; } float HeightMapInfo::Sample(int x, int z) const { diff --git a/src/quadtree.h b/src/quadtree.h index e13fd16..6c272b4 100644 --- a/src/quadtree.h +++ b/src/quadtree.h @@ -100,7 +100,7 @@ struct quadsquare { void SetFields(CourseFields* fields); private: - quadsquare* EnableDescendant(int count, int stack[], + quadsquare* EnableDescendant(int count, int path[], const quadcornerdata& cd); quadsquare* GetNeighbor(int dir, const quadcornerdata &cd); clip_result_t ClipSquare(const quadcornerdata &cd); @@ -113,7 +113,7 @@ private: void StaticCullAux(const quadcornerdata &cd, float ThresholdDetail, int TargetLevel); void CreateChild(int index, const quadcornerdata &cd); - void SetupCornerData(quadcornerdata *q, const quadcornerdata &pd, + void SetupCornerData(quadcornerdata *q, const quadcornerdata &cd, int ChildIndex); void UpdateAux(const quadcornerdata &cd, const float ViewerLocation[3], float CenterError, clip_result_t vis); diff --git a/src/race_select.cpp b/src/race_select.cpp index 918da6e..16c78d0 100644 --- a/src/race_select.cpp +++ b/src/race_select.cpp @@ -185,12 +185,12 @@ void CRaceSelect::Enter() { SetFocus(course); } -void CRaceSelect::Loop(float timestep) { +void CRaceSelect::Loop(float time_step) { ScopedRenderMode rm(GUI); Winsys.clear(); if (param.ui_snow) { - update_ui_snow(timestep); + update_ui_snow(time_step); draw_ui_snow(); } diff --git a/src/regist.cpp b/src/regist.cpp index 1a7114e..ef66e9b 100644 --- a/src/regist.cpp +++ b/src/regist.cpp @@ -120,12 +120,12 @@ void CRegist::Enter() { sCharFrame = AddFramedText(area.left + framewidth + arrowwidth, area.top, framewidth, frameheight, 3, colMBackgr, "", FT.GetSize()); } -void CRegist::Loop(float timestep) { +void CRegist::Loop(float time_step) { ScopedRenderMode rm(GUI); Winsys.clear(); if (param.ui_snow) { - update_ui_snow(timestep); + update_ui_snow(time_step); draw_ui_snow(); } diff --git a/src/score.cpp b/src/score.cpp index 527274f..606cd51 100644 --- a/src/score.cpp +++ b/src/score.cpp @@ -245,12 +245,12 @@ void CScore::Enter() { courseName = AddFramedText(area.left, frametop - 2 + frameheight + 20, framewidth, frameheight, 3, colMBackgr, "", FT.GetSize(), true); } -void CScore::Loop(float timestep) { +void CScore::Loop(float time_step) { ScopedRenderMode rm(GUI); Winsys.clear(); if (param.ui_snow) { - update_ui_snow(timestep); + update_ui_snow(time_step); draw_ui_snow(); } diff --git a/src/splash_screen.cpp b/src/splash_screen.cpp index 566303d..5d44eee 100644 --- a/src/splash_screen.cpp +++ b/src/splash_screen.cpp @@ -44,7 +44,7 @@ void CSplashScreen::Enter() { Music.Play(param.menu_music, true); } -void CSplashScreen::Loop(float timestep) { +void CSplashScreen::Loop(float time_step) { ScopedRenderMode rm(GUI); Winsys.clear(); Trans.LoadTranslations(param.language); // Before first texts are being displayed diff --git a/src/tools.cpp b/src/tools.cpp index 4182b96..d92c145 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -279,16 +279,16 @@ void CTools::Motion(int x, int y) { } } -void CTools::Loop(float timestep) { +void CTools::Loop(float time_step) { switch (tool_mode) { case 0: - RenderChar(timestep); + RenderChar(time_step); break; case 1: - RenderSingleFrame(timestep); + RenderSingleFrame(time_step); break; case 2: - RenderSequence(timestep); + RenderSequence(time_step); break; } } diff --git a/src/tux.cpp b/src/tux.cpp index 547e5f8..29c0322 100644 --- a/src/tux.cpp +++ b/src/tux.cpp @@ -485,14 +485,14 @@ bool CCharShape::Load(const std::string& dir, const std::string& filename, bool return true; } -TVector3d CCharShape::AdjustRollvector(const CControl *ctrl, const TVector3d& vel_, const TVector3d& zvec) { +TVector3d CCharShape::AdjustRollvector(const CControl *ctrl, const TVector3d& vel, const TVector3d& zvec) { TMatrix<4, 4> rot_mat; - TVector3d vel = ProjectToPlane(zvec, vel_); - vel.Norm(); + TVector3d v = ProjectToPlane(zvec, vel); + v.Norm(); if (ctrl->is_braking) { - rot_mat = RotateAboutVectorMatrix(vel, ctrl->turn_fact * BRAKING_ROLL_ANGLE); + rot_mat = RotateAboutVectorMatrix(v, ctrl->turn_fact * BRAKING_ROLL_ANGLE); } else { - rot_mat = RotateAboutVectorMatrix(vel, ctrl->turn_fact * MAX_ROLL_ANGLE); + rot_mat = RotateAboutVectorMatrix(v, ctrl->turn_fact * MAX_ROLL_ANGLE); } return TransformVector(rot_mat, zvec); } diff --git a/src/winsys.cpp b/src/winsys.cpp index 0d78c86..657b705 100644 --- a/src/winsys.cpp +++ b/src/winsys.cpp @@ -76,7 +76,7 @@ float CWinsys::CalcScreenScale() const { else return (resolution.height / 768.f); } -void CWinsys::SetupVideoMode(const TScreenRes& resolution_) { +void CWinsys::SetupVideoMode(const TScreenRes& res) { int bpp = 32; switch (param.bpp_mode) { case 16: @@ -93,7 +93,7 @@ void CWinsys::SetupVideoMode(const TScreenRes& resolution_) { if (param.fullscreen) style |= sf::Style::Fullscreen; - resolution = resolution_; + resolution = res; ResetRenderMode(); diff --git a/src/winsys.h b/src/winsys.h index b709e18..2957c75 100644 --- a/src/winsys.h +++ b/src/winsys.h @@ -49,7 +49,7 @@ public: const TScreenRes& GetResolution(std::size_t idx) const; std::string GetResName(std::size_t idx) const; void Init(); - void SetupVideoMode(const TScreenRes& resolution); + void SetupVideoMode(const TScreenRes& res); void SetupVideoMode(std::size_t idx); void SetupVideoMode(int width, int height); void KeyRepeat(bool repeat);