From 08d87fa9757a05b66325b9ea77123d098143ddbd Mon Sep 17 00:00:00 2001 From: krzys-h Date: Wed, 24 May 2017 12:39:05 +0200 Subject: [PATCH] Make initial scoreboard values editable --- src/app/app.cpp | 5 ++-- src/app/app.h | 5 +++- src/level/robotmain.cpp | 59 ++++++++++++++++++++++++++++++++++------ src/level/robotmain.h | 1 + src/level/scoreboard.cpp | 5 ++++ src/level/scoreboard.h | 1 + src/ui/controls/edit.cpp | 31 +++++++++++++-------- 7 files changed, 83 insertions(+), 24 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 806a17c7..f019ccc0 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -1854,9 +1854,10 @@ bool CApplication::GetSceneTestMode() return m_sceneTest; } -void CApplication::SetTextInput(bool textInputEnabled) +void CApplication::SetTextInput(bool textInputEnabled, int id) { - if (textInputEnabled) + m_textInputEnabled[id] = textInputEnabled; + if (std::any_of(m_textInputEnabled.begin(), m_textInputEnabled.end(), [](std::pair v) { return v.second; })) { SDL_StartTextInput(); } diff --git a/src/app/app.h b/src/app/app.h index 1b3b3a21..ac8769c7 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -35,6 +35,7 @@ #include #include +#include class CEventQueue; @@ -245,7 +246,7 @@ public: //! Enable/disable text input, this toggles the on-screen keyboard on some platforms /** This also allows for writing in CJK languages (not tested!), see https://wiki.libsdl.org/Tutorials/TextInput for detailed explanation */ - void SetTextInput(bool textInputEnabled); + void SetTextInput(bool textInputEnabled, int id); //! Moves (warps) the mouse cursor to the specified position (in interface coords) void MoveMouse(Math::Point pos); @@ -403,6 +404,8 @@ protected: //! Static buffer for putenv locale static char m_languageLocale[50]; + + std::map m_textInputEnabled; }; template<> CApplication* CSingleton::m_instance; diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 7f4181f1..30f55e3c 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -2563,6 +2563,7 @@ bool CRobotMain::EventFrame(const Event &event) if (!m_codeBattleStarted && m_userPause == nullptr) { m_codeBattleStarted = true; + ApplyCodeBattleInterface(); CreateCodeBattleInterface(); SetCodeBattleSpectatorMode(true); @@ -5793,7 +5794,7 @@ void CRobotMain::CreateCodeBattleInterface() int numTeams = m_scoreboard ? GetAllTeams().size() : 0; assert(numTeams < EVENT_SCOREBOARD_MAX-EVENT_SCOREBOARD+1); - float textHeight = m_engine->GetText()->GetAscent(Gfx::FONT_COLOBOT, Gfx::FONT_SIZE_SMALL); + float textHeight = m_engine->GetText()->GetHeight(Gfx::FONT_COLOBOT, Gfx::FONT_SIZE_SMALL); ddim.x = 100.0f/640.0f; ddim.y = 100.0f/480.0f + numTeams * textHeight; @@ -5825,18 +5826,59 @@ void CRobotMain::CreateCodeBattleInterface() pos.y += ddim.y; ddim.y = textHeight; - for (int i = 0; i < numTeams; i++) + int i = 0; + auto teams = GetAllTeams(); + for (auto it = teams.rbegin(); it != teams.rend(); ++it) { - Ui::CLabel* pl; - pl = pw->CreateLabel(pos, ddim, 0, static_cast(EVENT_SCOREBOARD+2*(numTeams-i-1)+0), "XXXXX"); + int team = *it; + Ui::CControl* pl; + ddim.x = 55.0f/640.0f; + pl = m_codeBattleStarted + ? static_cast(pw->CreateLabel(pos, ddim, 0, static_cast(EVENT_SCOREBOARD+2*(numTeams-i-1)+0), "XXXXX")) + : static_cast(pw->CreateEdit( pos, ddim, 0, static_cast(EVENT_SCOREBOARD+2*(numTeams-i-1)+0))); pl->SetTextAlign(Gfx::TEXT_ALIGN_LEFT); - pl = pw->CreateLabel(pos, ddim, 0, static_cast(EVENT_SCOREBOARD+2*(numTeams-i-1)+1), "???"); + pl->SetFontSize(m_codeBattleStarted ? Gfx::FONT_SIZE_SMALL : Gfx::FONT_SIZE_SMALL*0.75f); + m_codeBattleStarted ? pl->SetName(GetTeamName(team)) : static_cast(pl)->SetText(GetTeamName(team)); + pos.x += 57.5f/640.0f; + ddim.x = 22.5f/640.0f; + pl = m_codeBattleStarted + ? static_cast(pw->CreateLabel(pos, ddim, 0, static_cast(EVENT_SCOREBOARD+2*(numTeams-i-1)+1), "???")) + : static_cast(pw->CreateEdit( pos, ddim, 0, static_cast(EVENT_SCOREBOARD+2*(numTeams-i-1)+1))); pl->SetTextAlign(Gfx::TEXT_ALIGN_RIGHT); + pl->SetFontSize(m_codeBattleStarted ? Gfx::FONT_SIZE_SMALL : Gfx::FONT_SIZE_SMALL*0.75f); + m_codeBattleStarted ? pl->SetName(StrUtils::ToString(m_scoreboard->GetScore(team))) : static_cast(pl)->SetText(StrUtils::ToString(m_scoreboard->GetScore(team))); + pos.x -= 57.5f/640.0f; pos.y += ddim.y; + i++; } } } +void CRobotMain::ApplyCodeBattleInterface() +{ + assert(GetMissionType() == MISSION_CODE_BATTLE); + if (!m_scoreboard) return; + + Ui::CWindow* pw = static_cast(m_interface->SearchControl(EVENT_WINDOW6)); + assert(pw != nullptr); + + int i = 0; + for (int team : GetAllTeams()) + { + Ui::CEdit* pl; + + pl = static_cast(pw->SearchControl(static_cast(EVENT_SCOREBOARD+2*i+0))); + assert(pl != nullptr); + m_teamNames[team] = pl->GetText(pl->GetTextLength()); + + pl = static_cast(pw->SearchControl(static_cast(EVENT_SCOREBOARD+2*i+1))); + assert(pl != nullptr); + m_scoreboard->SetScore(team, StrUtils::FromString(pl->GetText(pl->GetTextLength()))); + + i++; + } +} + void CRobotMain::UpdateCodeBattleInterface() { assert(GetMissionType() == MISSION_CODE_BATTLE); @@ -5848,19 +5890,18 @@ void CRobotMain::UpdateCodeBattleInterface() int i = 0; for (int team : GetAllTeams()) { - Ui::CLabel* pl; + Ui::CControl* pl; - pl = static_cast(pw->SearchControl(static_cast(EVENT_SCOREBOARD+2*i+0))); + pl = pw->SearchControl(static_cast(EVENT_SCOREBOARD+2*i+0)); assert(pl != nullptr); pl->SetName(GetTeamName(team)); - pl = static_cast(pw->SearchControl(static_cast(EVENT_SCOREBOARD+2*i+1))); + pl = pw->SearchControl(static_cast(EVENT_SCOREBOARD+2*i+1)); assert(pl != nullptr); pl->SetName(StrUtils::ToString(m_scoreboard->GetScore(team))); i++; } - } void CRobotMain::DestroyCodeBattleInterface() diff --git a/src/level/robotmain.h b/src/level/robotmain.h index a683e206..d4e18525 100644 --- a/src/level/robotmain.h +++ b/src/level/robotmain.h @@ -518,6 +518,7 @@ protected: //@{ void CreateCodeBattleInterface(); void UpdateCodeBattleInterface(); + void ApplyCodeBattleInterface(); void DestroyCodeBattleInterface(); void SetCodeBattleSpectatorMode(bool mode); //@} diff --git a/src/level/scoreboard.cpp b/src/level/scoreboard.cpp index 0f388c69..7e4b5b04 100644 --- a/src/level/scoreboard.cpp +++ b/src/level/scoreboard.cpp @@ -106,3 +106,8 @@ int CScoreboard::GetScore(int team) { return m_score[team]; } + +void CScoreboard::SetScore(int team, int points) +{ + m_score[team] = points; +} diff --git a/src/level/scoreboard.h b/src/level/scoreboard.h index 6768a595..58a5acb5 100644 --- a/src/level/scoreboard.h +++ b/src/level/scoreboard.h @@ -115,6 +115,7 @@ public: void AddPoints(int team, int points); int GetScore(int team); + void SetScore(int team, int score); private: std::vector> m_rulesKill = {}; diff --git a/src/ui/controls/edit.cpp b/src/ui/controls/edit.cpp index af04d93d..1269284d 100644 --- a/src/ui/controls/edit.cpp +++ b/src/ui/controls/edit.cpp @@ -94,7 +94,7 @@ bool IsBreaker(char c) bool IsDelimiter(char c) { return IsSpace( c ) || IsBreaker( c ); -} +} //! Object's constructor. CEdit::CEdit() @@ -148,7 +148,7 @@ CEdit::~CEdit() if (m_bFocus) { - CApplication::GetInstancePointer()->SetTextInput(false); + CApplication::GetInstancePointer()->SetTextInput(false, EVENT_OBJECT_PEN3); } } @@ -520,13 +520,20 @@ bool CEdit::EventProcess(const Event &event) MouseClick(event.mousePos); if ( m_bEdit || m_bHilite ) m_bCapture = true; } - m_bFocus = true; - UpdateFocus(); + + if (!m_bFocus) + { + m_bFocus = true; + UpdateFocus(); + } } else { - m_bFocus = false; - UpdateFocus(); + if (m_bFocus) + { + m_bFocus = false; + UpdateFocus(); + } } } @@ -730,7 +737,7 @@ int CEdit::MouseDetect(Math::Point mouse) if ( i >= m_lineFirst+m_lineVisible ) break; - pos.x = m_pos.x+(10.0f/640.0f); + pos.x = m_pos.x+(7.5f/640.0f)*(m_fontSize/Gfx::FONT_SIZE_SMALL); if ( m_bAutoIndent ) { pos.x += indentLength*m_lineIndent[i]; @@ -945,7 +952,7 @@ void CEdit::Draw() if ( i >= m_lineFirst+m_lineVisible ) break; - pos.x = m_pos.x+(10.0f/640.0f); + pos.x = m_pos.x+(7.5f/640.0f)*(m_fontSize/Gfx::FONT_SIZE_SMALL); if ( m_bAutoIndent ) { const char *s = "\t"; // line | dotted @@ -1107,7 +1114,7 @@ void CEdit::Draw() { if ( i == m_lineTotal-1 || m_cursor1 < m_lineOffset[i+1] ) { - pos.x = m_pos.x+(10.0f/640.0f); + pos.x = m_pos.x+(7.5f/640.0f)*(m_fontSize/Gfx::FONT_SIZE_SMALL); if ( m_bAutoIndent ) { pos.x += indentLength*m_lineIndent[i]; @@ -2767,7 +2774,7 @@ void CEdit::DeleteOne(int dir) } m_len -= hole; m_cursor2 = m_cursor1; -} +} // Delete word @@ -3003,7 +3010,7 @@ void CEdit::Justif() { bDual = false; - width = m_dim.x-(10.0f/640.0f)*2.0f-(m_bMulti?MARGX*2.0f+SCROLL_WIDTH:0.0f); + width = m_dim.x-(7.5f/640.0f)*(m_fontSize/Gfx::FONT_SIZE_SMALL)*2.0f-(m_bMulti?MARGX*2.0f+SCROLL_WIDTH:0.0f); if ( m_bAutoIndent ) { width -= indentLength*m_lineIndent[m_lineTotal-1]; @@ -3276,7 +3283,7 @@ void CEdit::SetFocus(CControl* control) void CEdit::UpdateFocus() { - CApplication::GetInstancePointer()->SetTextInput(m_bFocus); + CApplication::GetInstancePointer()->SetTextInput(m_bFocus, m_eventType); } }