1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Norbert e3d86ebce6
Merge f02b13d52b into 7546c1acab 2025-07-14 08:01:51 +00:00
Peter Nelson 7546c1acab
Codefix f220ed179d: GetUnicodeGlyph takes a unicode character. (#14438)
Previous change erroneously changed type to GlyphID, based on naming. It should actually be char32_t.
2025-07-14 08:01:42 +00:00
Peter Nelson a6143eea21
Codechange: Include more relevant headers for script_storage. (#14437) 2025-07-14 07:49:50 +01:00
npabisz f02b13d52b Add: [Script] Text effect 2025-04-27 13:30:23 -04:00
14 changed files with 265 additions and 17 deletions

View File

@ -500,6 +500,8 @@ add_files(
textbuf_type.h
texteff.cpp
texteff.hpp
texteff_cmd.cpp
texteff_cmd.h
textfile_gui.cpp
textfile_gui.h
textfile_type.h

View File

@ -46,6 +46,7 @@
#include "station_cmd.h"
#include "story_cmd.h"
#include "subsidy_cmd.h"
#include "texteff_cmd.h"
#include "terraform_cmd.h"
#include "timetable_cmd.h"
#include "town_cmd.h"

View File

@ -372,6 +372,10 @@ enum Commands : uint8_t {
CMD_UPDATE_LEAGUE_TABLE_ELEMENT_SCORE, ///< update the score of a league table element
CMD_REMOVE_LEAGUE_TABLE_ELEMENT, ///< remove a league table element
CMD_CREATE_TEXT_EFFECT, ///< create a new text effect
CMD_UPDATE_TEXT_EFFECT, ///< update text effect
CMD_REMOVE_TEXT_EFFECT, ///< remove text effect
CMD_END, ///< Must ALWAYS be on the end of this list!! (period)
};

View File

@ -43,26 +43,26 @@ SpriteFontCache::SpriteFontCache(FontSize fs) : FontCache(fs)
}
/**
* Get SpriteID associated with a GlyphID.
* @param key Glyph to find.
* @return SpriteID of glyph, or 0 if not present.
* Get SpriteID associated with a character.
* @param key Character to find.
* @return SpriteID for character, or 0 if not present.
*/
SpriteID SpriteFontCache::GetUnicodeGlyph(GlyphID key)
SpriteID SpriteFontCache::GetUnicodeGlyph(char32_t key)
{
const auto found = this->glyph_to_spriteid_map.find(key & ~SPRITE_GLYPH);
if (found == std::end(this->glyph_to_spriteid_map)) return 0;
const auto found = this->char_map.find(key);
if (found == std::end(this->char_map)) return 0;
return found->second;
}
void SpriteFontCache::SetUnicodeGlyph(char32_t key, SpriteID sprite)
{
this->glyph_to_spriteid_map[key] = sprite;
this->char_map[key] = sprite;
}
void SpriteFontCache::InitializeUnicodeGlyphMap()
{
/* Clear out existing glyph map if it exists */
this->glyph_to_spriteid_map.clear();
this->char_map.clear();
SpriteID base;
switch (this->fs) {

View File

@ -28,8 +28,8 @@ public:
bool IsBuiltInFont() override { return true; }
private:
std::unordered_map<GlyphID, SpriteID> glyph_to_spriteid_map{}; ///< Mapping of glyphs to sprite IDs.
SpriteID GetUnicodeGlyph(GlyphID key);
std::unordered_map<char32_t, SpriteID> char_map{}; ///< Mapping of characters to sprite IDs.
SpriteID GetUnicodeGlyph(char32_t key);
};
#endif /* SPRITEFONTCACHE_H */

View File

@ -40,6 +40,7 @@
#include "../station_cmd.h"
#include "../story_cmd.h"
#include "../subsidy_cmd.h"
#include "../texteff_cmd.h"
#include "../terraform_cmd.h"
#include "../timetable_cmd.h"
#include "../town_cmd.h"

View File

@ -206,6 +206,7 @@ add_files(
script_subsidylist.hpp
script_testmode.hpp
script_text.hpp
script_text_effect.hpp
script_tile.hpp
script_tilelist.hpp
script_town.hpp
@ -278,6 +279,7 @@ add_files(
script_subsidylist.cpp
script_testmode.cpp
script_text.cpp
script_text_effect.cpp
script_tile.cpp
script_tilelist.cpp
script_town.cpp

View File

@ -0,0 +1,63 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file script_text_effect.cpp Implementation of ScriptTextEffect with multiplayer support. */
#include "../../stdafx.h"
#include "script_text_effect.hpp"
#include "script_error.hpp"
#include "script_map.hpp"
#include "../script_instance.hpp"
#include "../../texteff.hpp"
#include "../../strings_func.h"
#include "../../tile_map.h"
#include "../../command_func.h"
#include "../../texteff_cmd.h"
#include "../../safeguards.h"
/* static */ TextEffectID ScriptTextEffect::CreateAtPosition(SQInteger x, SQInteger y, Text *text, ScriptTextEffectMode mode)
{
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, text != nullptr);
EnforcePrecondition(false, !text->GetEncodedText().empty());
EnforcePrecondition(false, mode == TE_RISING || mode == TE_STATIC);
return ScriptObject::Command<CMD_CREATE_TEXT_EFFECT>::Do(&ScriptInstance::DoCommandReturnTextEffectID, x, y, (TextEffectMode)mode, text->GetEncodedText());
}
/* static */ TextEffectID ScriptTextEffect::Create(TileIndex tile, Text *text, ScriptTextEffectMode mode)
{
EnforcePrecondition(false, ScriptMap::IsValidTile(tile));
int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
int y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
return CreateAtPosition(x, y, text, mode);
}
/* static */ bool ScriptTextEffect::Update(TextEffectID te_id, Text *text)
{
ScriptObjectRef counter(text);
EnforceDeityMode(false);
EnforcePrecondition(false, te_id != INVALID_TE_ID);
EnforcePrecondition(false, text != nullptr);
EnforcePrecondition(false, !text->GetEncodedText().empty());
return ScriptObject::Command<CMD_UPDATE_TEXT_EFFECT>::Do(te_id, text->GetEncodedText());
}
/* static */ bool ScriptTextEffect::Remove(TextEffectID te_id)
{
EnforceDeityMode(false);
EnforcePrecondition(false, te_id != INVALID_TE_ID);
return ScriptObject::Command<CMD_REMOVE_TEXT_EFFECT>::Do(te_id);
}

View File

@ -0,0 +1,66 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file script_text_effect.hpp Everything to display animated text in the game world. */
#ifndef SCRIPT_TEXT_EFFECT_HPP
#define SCRIPT_TEXT_EFFECT_HPP
#include "script_object.hpp"
#include "script_text.hpp"
#include "../../texteff.hpp"
/**
* Class that handles text effect display in the game world.
* @api game
*/
class ScriptTextEffect : public ScriptObject {
public:
/**
* Text effect animation modes.
*/
enum ScriptTextEffectMode {
TE_RISING = ::TE_RISING, ///< Text slowly rises upwards
TE_STATIC = ::TE_STATIC, ///< Text stays in place
};
/**
* Create animated text at a tile location.
* @param tile The tile where to show the text.
* @param text The text to display.
* @param mode The animation mode to use.
* @return True if the text effect was created successfully.
*/
static TextEffectID Create(TileIndex tile, Text *text, ScriptTextEffectMode mode);
/**
* Create animated text at the specified location.
* @param x X coordinate in the game world.
* @param y Y coordinate in the game world.
* @param text The text to display.
* @param mode The animation mode to use.
* @return True if the text effect was created successfully.
*/
static TextEffectID CreateAtPosition(SQInteger x, SQInteger y, Text *text, ScriptTextEffectMode mode);
/**
* Update animated text
* @param te_id Text effect ID.
* @param text The text to update
* @return True if the text effect was updated successfully
*/
static bool Update(TextEffectID te_id, Text *text);
/**
* Update animated text
* @param te_id Text effect ID.
* @return True if the text effect was removed successfully
*/
static bool Remove(TextEffectID te_id);
};
#endif /* SCRIPT_TEXT_EFFECT_HPP */

View File

@ -24,11 +24,14 @@
#include "api/script_event.hpp"
#include "api/script_log.hpp"
#include "../company_base.h"
#include "../company_func.h"
#include "../company_type.h"
#include "../fileio_func.h"
#include "../goal_type.h"
#include "../league_type.h"
#include "../signs_type.h"
#include "../story_type.h"
#include "../misc/endian_buffer.hpp"
#include "../texteff.hpp"
#include "../safeguards.h"
@ -315,6 +318,10 @@ void ScriptInstance::CollectGarbage()
instance.engine->InsertResult(EndianBufferReader::ToValue<LeagueTableID>(ScriptObject::GetLastCommandResData()));
}
/* static */ void ScriptInstance::DoCommandReturnTextEffectID(ScriptInstance *instance)
{
instance->engine->InsertResult(EndianBufferReader::ToValue<TextEffectID>(ScriptObject::GetLastCommandResData()));
}
ScriptStorage *ScriptInstance::GetStorage()
{

View File

@ -143,6 +143,11 @@ public:
*/
static void DoCommandReturnLeagueTableElementID(ScriptInstance &instance);
/**
* Return a TextEffectID reply for a DoCommand.
*/
static void DoCommandReturnTextEffectID(ScriptInstance *instance);
/**
* Get the controller attached to the instance.
*/

View File

@ -12,12 +12,10 @@
#include <queue>
#include "../signs_func.h"
#include "../vehicle_func.h"
#include "../command_type.h"
#include "../company_type.h"
#include "../rail_type.h"
#include "../road_type.h"
#include "../group.h"
#include "../goal_type.h"
#include "../story_type.h"
#include "script_types.hpp"
#include "script_log_types.hpp"

View File

@ -0,0 +1,75 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file texteff_cmd.cpp Command handling for text effects */
#include "stdafx.h"
#include "command_func.h"
#include "texteff.hpp"
#include "strings_func.h"
#include "tile_map.h"
#include "texteff_cmd.h"
#include "table/strings.h"
#include "safeguards.h"
#include "landscape.h"
/**
* Show a text effect at the specified location.
* @param flags operation to perform
* @param x X coordinate in the game
* @param y Y coordinate in the game
* @param mode The animation mode to use
* @param text The text to display
* @return the cost of this operation or an error
*/
std::tuple<CommandCost, TextEffectID> CmdCreateTextEffect(DoCommandFlags flags, int32_t x, int32_t y, TextEffectMode mode, const EncodedString &text)
{
if (text.empty()) return { CMD_ERROR, INVALID_TE_ID };
if (mode != TE_RISING && mode != TE_STATIC) return { CMD_ERROR, INVALID_TE_ID };
if (flags.Test(DoCommandFlag::Execute)) {
Point pt = RemapCoords2(x, y);
EncodedString encoded_text = text;
TextEffectID te_id;
if (mode == TE_RISING) {
te_id = AddTextEffect(std::move(encoded_text), pt.x, pt.y, Ticks::DAY_TICKS, TE_RISING);
} else {
te_id = AddTextEffect(std::move(encoded_text), pt.x, pt.y, 0, TE_STATIC);
}
return { CommandCost(), te_id };
}
return { CommandCost(), INVALID_TE_ID };
}
CommandCost CmdUpdateTextEffect(DoCommandFlags flags, TextEffectID te_id, const EncodedString &text)
{
if (te_id == INVALID_TE_ID) return CMD_ERROR;
if (text.empty()) return CMD_ERROR;
if (flags.Test(DoCommandFlag::Execute)) {
EncodedString encoded_text = text;
UpdateTextEffect(te_id, std::move(encoded_text));
}
return CommandCost();
}
CommandCost CmdRemoveTextEffect(DoCommandFlags flags, TextEffectID te_id)
{
if (te_id == INVALID_TE_ID) return CMD_ERROR;
if (flags.Test(DoCommandFlag::Execute)) {
RemoveTextEffect(te_id);
}
return CommandCost();
}

24
src/texteff_cmd.h 100644
View File

@ -0,0 +1,24 @@
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file texteff_cmd.h Command declarations for text effects */
#ifndef TEXTEFF_CMD_H
#define TEXTEFF_CMD_H
#include "command_type.h"
#include "texteff.hpp"
std::tuple<CommandCost, TextEffectID> CmdCreateTextEffect(DoCommandFlags flags, int32_t x, int32_t y, TextEffectMode mode, const EncodedString &text);
CommandCost CmdUpdateTextEffect(DoCommandFlags flags, TextEffectID te_id, const EncodedString &text);
CommandCost CmdRemoveTextEffect(DoCommandFlags flags, TextEffectID te_id);
DEF_CMD_TRAIT(CMD_CREATE_TEXT_EFFECT, CmdCreateTextEffect, CommandFlags({CommandFlag::Deity, CommandFlag::StrCtrl}), CMDT_OTHER_MANAGEMENT)
DEF_CMD_TRAIT(CMD_UPDATE_TEXT_EFFECT, CmdUpdateTextEffect, CommandFlags({CommandFlag::Deity, CommandFlag::StrCtrl}), CMDT_OTHER_MANAGEMENT)
DEF_CMD_TRAIT(CMD_REMOVE_TEXT_EFFECT, CmdRemoveTextEffect, CommandFlag::Deity, CMDT_OTHER_MANAGEMENT)
#endif /* TEXTEFF_CMD_H */