The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
lua_color.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 - 2025
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13*/
14
15#include "color_range.hpp"
20#include "log.hpp"
21#include "game_config.hpp"
22
23static lg::log_domain log_scripting_lua("scripting/lua");
24#define LOG_LUA LOG_STREAM(info, log_scripting_lua)
25#define ERR_LUA LOG_STREAM(err, log_scripting_lua)
26
27static const char colorKey[] = "color range";
28
29static color_range& LuaW_checkcolor(lua_State *L, int index)
30{
31 return *static_cast<color_range*>(luaL_checkudata(L, index, colorKey));
32}
33
34
35static color_range* luaW_pushcolor(lua_State *L, const color_range& color)
36{
37 color_range* res = new(L) color_range(color);
38 luaL_setmetatable(L, colorKey);
39 return res;
40}
41
42static int luaW_pushsinglecolor(lua_State *L, const color_t& color)
43{
44 lua_createtable(L, 0, 4);
45 luaW_table_set(L, -1, "r", color.r);
46 luaW_table_set(L, -1, "g", color.g);
47 luaW_table_set(L, -1, "b", color.b);
48 luaW_table_set(L, -1, "a", color.a);
49 return 1;
50}
51
52static int impl_color_collect(lua_State *L)
53{
54 color_range *c = static_cast<color_range *>(luaL_checkudata(L, 1, colorKey));
55 c->color_range::~color_range();
56 return 0;
57}
58
59/**
60 * Checks two color units for equality. (__eq metamethod)
61 */
62static int impl_color_equality(lua_State* L)
63{
64 color_range& left = LuaW_checkcolor(L, 1);
65 color_range& right = LuaW_checkcolor(L, 2);
66 const bool equal = left == right;
67 lua_pushboolean(L, equal);
68 return 1;
69}
70
71/**
72 * Turns a lua color to string. (__tostring metamethod)
73 */
74static int impl_color_tostring(lua_State* L)
75{
77 //TODO: is this the best way to call tostring?
78 lua_push(L, c.debug());
79 return 1;
80}
81
82
83/**
84 * - Arg 1: userdata (ignored).
85 * - Arg 2: string containing the name of the color.
86 * - Ret 1: color_range containing the color.
87 */
88static int impl_get_color(lua_State *L)
89{
90 std::string color_id = luaL_checkstring(L, 2);
92 return 1;
93}
94
95static int impl_color_get(lua_State *L)
96{
98 char const *m = luaL_checkstring(L, 2);
99
100 if(strcmp(m, "min") == 0) {
101 return luaW_pushsinglecolor(L, c.min());
102 }
103 if(strcmp(m, "max") == 0) {
104 return luaW_pushsinglecolor(L, c.max());
105 }
106 if(strcmp(m, "mid") == 0) {
107 return luaW_pushsinglecolor(L, c.mid());
108 }
109 if(strcmp(m, "minimap") == 0) {
110 return luaW_pushsinglecolor(L, c.rep());
111 }
112 if(strcmp(m, "font_color") == 0) {
113 return luaW_pushsinglecolor(L, c.font_color());
114 }
115 // returns a string which can be used in Pango's foreground= attribute
116 if(strcmp(m, "pango_color") == 0) {
117 lua_push(L, c.font_color().to_hex_string());
118 return 1;
119 }
120 return 0;
121}
122
123static int impl_color_dir(lua_State* L)
124{
125 static const std::vector<std::string> keys{"min", "max", "mid", "minimap", "font_color", "pango_color"};
126 lua_push(L, keys);
127 return 1;
128}
129
130static int impl_color_set(lua_State *L)
131{
132 return luaL_argerror(L, 2, "color objects canot be modified");
133}
134
135static int impl_colors_table_dir(lua_State* L)
136{
137 std::vector<std::string> all_colours;
138 for(const auto& [key, value] : game_config::team_rgb_range) {
139 if(std::all_of(key.begin(), key.end(), [](char c) {return utils::portable_isdigit(c);})) {
140 // These colors are deprecated, don't show them
141 continue;
142 }
143 all_colours.push_back(key);
144 }
145 lua_push(L, all_colours);
146 return 1;
147}
148
149namespace lua_colors {
150 std::string register_metatables(lua_State* L)
151 {
152 std::ostringstream cmd_out;
153
154 // Create the color metatable.
155 cmd_out << "Adding color metatable...\n";
156
157 luaL_newmetatable(L, colorKey);
158 lua_pushcfunction(L, impl_color_collect);
159 lua_setfield(L, -2, "__gc");
160 lua_pushcfunction(L, impl_color_equality);
161 lua_setfield(L, -2, "__eq");
162 lua_pushcfunction(L, impl_color_tostring);
163 lua_setfield(L, -2, "__tostring");
164 lua_pushcfunction(L, impl_color_get);
165 lua_setfield(L, -2, "__index");
166 lua_pushcfunction(L, impl_color_set);
167 lua_setfield(L, -2, "__newindex");
168 lua_pushcfunction(L, impl_color_dir);
169 lua_setfield(L, -2, "__dir");
170 lua_pushstring(L, "color range");
171 lua_setfield(L, -2, "__metatable");
172
173
174 // Create the colors variable with its metatable.
175 cmd_out << "Adding wesnoth.colors table...\n";
176
177 lua_getglobal(L, "wesnoth");
178 lua_newuserdatauv(L, 0, 0);
179 lua_createtable(L, 0, 2);
180 lua_pushcfunction(L, impl_get_color);
181 lua_setfield(L, -2, "__index");
182 lua_pushcfunction(L, impl_colors_table_dir);
183 lua_setfield(L, -2, "__dir");
184 lua_pushstring(L, "colors table");
185 lua_setfield(L, -2, "__metatable");
186 lua_pushboolean(L, true);
187 lua_setfield(L, -2, "__dir_tablelike");
188 lua_setmetatable(L, -2);
189 lua_setfield(L, -2, "colors");
190 lua_pop(L, 1);
191
192 return cmd_out.str();
193 }
194}
Representing the "team color" information, this C++ class contains a subset of WML's [color_range] ta...
Standard logging facilities (interface).
static color_range * luaW_pushcolor(lua_State *L, const color_range &color)
Definition lua_color.cpp:35
static int impl_color_set(lua_State *L)
static int impl_color_tostring(lua_State *L)
Turns a lua color to string.
Definition lua_color.cpp:74
static lg::log_domain log_scripting_lua("scripting/lua")
static int impl_color_equality(lua_State *L)
Checks two color units for equality.
Definition lua_color.cpp:62
static int luaW_pushsinglecolor(lua_State *L, const color_t &color)
Definition lua_color.cpp:42
static color_range & LuaW_checkcolor(lua_State *L, int index)
Definition lua_color.cpp:29
static int impl_color_get(lua_State *L)
Definition lua_color.cpp:95
static int impl_color_dir(lua_State *L)
static int impl_colors_table_dir(lua_State *L)
static int impl_get_color(lua_State *L)
Definition lua_color.cpp:88
static const char colorKey[]
Definition lua_color.cpp:27
static int impl_color_collect(lua_State *L)
Definition lua_color.cpp:52
std::map< std::string, color_range, std::less<> > team_rgb_range
Colors defined by WML [color_range] tags.
const color_range & color_info(std::string_view name)
std::string register_metatables(lua_State *L)
void lua_push(lua_State *L, const T &val)
void luaW_table_set(lua_State *L, int index, std::string_view k, const T &value)
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51
#define c