Skip to content

Commit 92d1328

Browse files
authored
Merge pull request #3010 from asarium/lua/cmdBriefColors
Add scripting API to access briefing color tags
2 parents a313549 + c2f4d5f commit 92d1328

File tree

6 files changed

+141
-10
lines changed

6 files changed

+141
-10
lines changed

code/mission/missionbriefcommon.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
#ifndef __MISSIONBRIEFCOMMON_H__
1313
#define __MISSIONBRIEFCOMMON_H__
1414

15-
#include "anim/packunpack.h"
1615
#include "globalincs/globals.h"
16+
17+
#include "anim/packunpack.h"
1718
#include "graphics/generic.h"
1819
#include "hud/hud.h"
20+
#include "utils/unicode.h"
1921

2022
#define MAX_TEXT_STREAMS 2 // how many concurrent streams of text can be displayed
2123

@@ -321,4 +323,6 @@ void cmd_brief_reset();
321323

322324
int brief_time_to_advance(int stage_num);
323325

326+
bool brief_verify_color_tag(unicode::codepoint_t color_tag);
327+
324328
#endif

code/scpui/RocketRenderingInterface.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,11 @@ bool RocketRenderingInterface::LoadTexture(TextureHandle& texture_handle, Vector
143143

144144
std::unique_ptr<Texture> tex(new Texture());
145145
// If there is a file that ends with an animation extension, try to load that
146-
if (generic_anim_exists(filename.c_str())) {
147-
// Load as animation
148-
generic_anim_init(&tex->animation, filename);
149-
if (generic_anim_stream(&tex->animation) == 0) {
150-
tex->is_animation = true;
151-
152-
texture_dimensions.x = tex->animation.width;
153-
texture_dimensions.y = tex->animation.height;
154-
}
146+
if (generic_anim_init_and_stream(&tex->animation, filename.c_str(), BM_TYPE_NONE, true) == 0) {
147+
tex->is_animation = true;
148+
149+
texture_dimensions.x = tex->animation.width;
150+
texture_dimensions.y = tex->animation.height;
155151
}
156152

157153
if (!tex->is_animation) {

code/scripting/api/libs/ui.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
//
33
#include "ui.h"
44

5+
#include "globalincs/alphacolors.h"
6+
57
#include "cmdline/cmdline.h"
68
#include "gamesnd/eventmusic.h"
79
#include "menuui/barracks.h"
810
#include "menuui/mainhallmenu.h"
911
#include "menuui/optionsmenu.h"
1012
#include "menuui/playermenu.h"
1113
#include "menuui/readyroom.h"
14+
#include "mission/missionbriefcommon.h"
1215
#include "mission/missioncampaign.h"
1316
#include "missionui/missionscreencommon.h"
1417
#include "playerman/managepilot.h"
1518
#include "scpui/SoundPlugin.h"
1619
#include "scpui/rocket_ui.h"
1720
#include "scripting/api/objs/cmd_brief.h"
21+
#include "scripting/api/objs/color.h"
1822
#include "scripting/api/objs/player.h"
1923
#include "scripting/lua/LuaTable.h"
2024

@@ -386,6 +390,46 @@ ADE_LIB_DERIV(l_UserInterface_CmdBrief,
386390
"API for the new UI system. This should not be used by other code and may be removed in the future!",
387391
l_UserInterface);
388392

393+
ADE_VIRTVAR(ColorTags,
394+
l_UserInterface_CmdBrief,
395+
nullptr,
396+
"The available tagged colors",
397+
ade_type_map("string", "color"),
398+
"A mapping from tag string to color value")
399+
{
400+
using namespace luacpp;
401+
402+
LuaTable mapping = LuaTable::create(L);
403+
404+
for (const auto& tagged : Tagged_Colors) {
405+
SCP_string tag;
406+
tag.resize(1, tagged.first);
407+
408+
mapping.addValue(tag, l_Color.Set(*tagged.second));
409+
}
410+
411+
return ade_set_args(L, "t", mapping);
412+
}
413+
414+
ADE_VIRTVAR(DefaultTextColorTag,
415+
l_UserInterface_CmdBrief,
416+
nullptr,
417+
"Gets the default color tag string for the command briefing. Index into ColorTags.",
418+
"string",
419+
"The default color tag")
420+
{
421+
SCP_string tagStr;
422+
423+
auto defaultColor = default_command_briefing_color;
424+
425+
if (defaultColor == '\0' || !brief_verify_color_tag(defaultColor)) {
426+
defaultColor = Color_Tags[0];
427+
}
428+
tagStr.resize(1, defaultColor);
429+
430+
return ade_set_args(L, "s", tagStr);
431+
}
432+
389433
ADE_FUNC(getBriefing,
390434
l_UserInterface_CmdBrief,
391435
nullptr,

code/scripting/api/objs/color.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
#include "color.h"
3+
4+
namespace scripting {
5+
namespace api {
6+
7+
ADE_OBJ(l_Color, color, "color", "A color value");
8+
9+
// That member syntax is pretty weird to be honest...
10+
static int colorVarHelper(lua_State* L, ubyte color::*member)
11+
{
12+
color* clr = nullptr;
13+
float newVal = -1.0f;
14+
15+
if (!ade_get_args(L, "o|f", l_Color.GetPtr(&clr), &newVal)) {
16+
return ade_set_args(L, "f", -1.0f);
17+
}
18+
19+
if (ADE_SETTING_VAR) {
20+
if (newVal < 0.0f || newVal > 255.0f) {
21+
LuaError(L, "Invalid color value %f!", newVal);
22+
CLAMP(newVal, 0.0f, 255.0f);
23+
}
24+
25+
clr->*member = static_cast<ubyte>(newVal);
26+
}
27+
28+
return ade_set_args(L, "i", clr->*member);
29+
}
30+
31+
ADE_VIRTVAR(Red,
32+
l_Color,
33+
"number",
34+
"The 'red' value of the color in the range from 0 to 255",
35+
"number",
36+
"The 'red' value")
37+
{
38+
return colorVarHelper(L, &color::red);
39+
}
40+
41+
ADE_VIRTVAR(Green,
42+
l_Color,
43+
"number",
44+
"The 'green' value of the color in the range from 0 to 255",
45+
"number",
46+
"The 'green' value")
47+
{
48+
return colorVarHelper(L, &color::green);
49+
}
50+
51+
ADE_VIRTVAR(Blue,
52+
l_Color,
53+
"number",
54+
"The 'blue' value of the color in the range from 0 to 255",
55+
"number",
56+
"The 'blue' value")
57+
{
58+
return colorVarHelper(L, &color::blue);
59+
}
60+
61+
ADE_VIRTVAR(Alpha,
62+
l_Color,
63+
"number",
64+
"The 'alpha' or opacity value of the color in the range from 0 to 255. 0 is totally transparent, 255 is completely "
65+
"opaque.",
66+
"number",
67+
"The 'alpha' value")
68+
{
69+
return colorVarHelper(L, &color::alpha);
70+
}
71+
72+
} // namespace api
73+
} // namespace scripting

code/scripting/api/objs/color.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include "graphics/2d.h"
4+
#include "scripting/ade_api.h"
5+
6+
namespace scripting {
7+
namespace api {
8+
9+
DECLARE_ADE_OBJ(l_Color, color);
10+
11+
}
12+
} // namespace scripting

code/source_groups.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,8 @@ add_file_folder("Scripting\\\\Api\\\\Objs"
11971197
scripting/api/objs/cmd_brief.h
11981198
scripting/api/objs/cockpit_display.cpp
11991199
scripting/api/objs/cockpit_display.h
1200+
scripting/api/objs/color.cpp
1201+
scripting/api/objs/color.h
12001202
scripting/api/objs/control_info.cpp
12011203
scripting/api/objs/control_info.h
12021204
scripting/api/objs/controls.cpp

0 commit comments

Comments
 (0)