Skip to content
Snippets Groups Projects
Commit b7585f1a authored by dg's avatar dg
Browse files

Strings can have font style inlined: #{normal}# #{bold}# #{italic}# #{underline}#

git-svn-id: http://svn.net-core.org/repos/t-engine4@1160 51575b47-30f0-44d4-a5cc-537603b46e54
parent df93ac29
No related branches found
No related tags found
No related merge requests found
......@@ -60,8 +60,8 @@ function _M:setCurrent()
end
--- Registers a click zone that when clicked will call the object's "onClick" method
function _M:registerZone(x, y, w, h, fct, mode)
table.insert(self.areas, 1, {x1=x,y1=y,x2=x+w,y2=y+h, fct=fct, mode})
function _M:registerZone(x, y, w, h, fct, mode, name)
table.insert(self.areas, 1, {x1=x,y1=y,x2=x+w,y2=y+h, fct=fct, mode, name=name})
end
function _M:registerZones(t)
......
......@@ -103,7 +103,7 @@ function table.reverse(t)
end
function table.listify(t)
local tt = {}
for k, e in pairs(t) do tt[#tt+1] = {k, e} print("listify", #tt, k, e) end
for k, e in pairs(t) do tt[#tt+1] = {k, e} end
return tt
end
......@@ -162,10 +162,12 @@ local Puid_cap = "UID:" * lpeg.C(lpeg.R"09"^1) * ":" * lpeg.C(lpeg.R"09")
local Pcolorname = (lpeg.R"AZ" + "_")^3
local Pcode = (lpeg.R"af" + lpeg.R"09" + lpeg.R"AF")
local Pcolorcode = Pcode * Pcode
local Pfontstyle = "{" * (lpeg.P"bold" + lpeg.P"italic" + lpeg.P"underline" + lpeg.P"normal") * "}"
local Pfontstyle_cap = "{" * lpeg.C(lpeg.P"bold" + lpeg.P"italic" + lpeg.P"underline" + lpeg.P"normal") * "}"
local Pcolorcodefull = Pcolorcode * Pcolorcode * Pcolorcode
function string.removeColorCodes(str)
return str:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", "")
return str:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", "")
end
function string.splitLine(str, max_width, font)
......@@ -173,7 +175,7 @@ function string.splitLine(str, max_width, font)
local lines = {}
local cur_line, cur_size = "", 0
for _, v in ipairs(str:split(lpeg.S"\n ")) do
local shortv = v:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", "")
local shortv = v:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", "")
local w, h = font:size(shortv)
if cur_size + space_w + w < max_width then
......@@ -247,7 +249,7 @@ end
local tmps = core.display.newSurface(1, 1)
getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g, b)
local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", true)
local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", true)
r = r or 255
g = g or 255
b = b or 255
......@@ -257,6 +259,7 @@ getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g,
local nr, ng, nb = lpeg.match("#" * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * "#", v)
local col = lpeg.match("#" * lpeg.C(Pcolorname) * "#", v)
local uid, mo = lpeg.match("#" * Puid_cap * "#", v)
local fontstyle = lpeg.match("#" * Pfontstyle_cap * "#", v)
if nr and ng and nb then
oldr, oldg, oldb = r, g, b
r, g, b = nr:parseHex(), ng:parseHex(), nb:parseHex()
......@@ -278,6 +281,8 @@ getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g,
if h > max_h then max_h = h end
x = x + (w or 0)
end
elseif fontstyle then
font:setStyle(fontstyle)
else
local w, h = font:size(v)
if h > max_h then max_h = h end
......@@ -296,7 +301,7 @@ end
getmetatable(tmps).__index.drawColorStringBlended = function(s, font, str, x, y, r, g, b)
local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", true)
local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", true)
r = r or 255
g = g or 255
b = b or 255
......@@ -306,6 +311,7 @@ getmetatable(tmps).__index.drawColorStringBlended = function(s, font, str, x, y,
local nr, ng, nb = lpeg.match("#" * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * "#", v)
local col = lpeg.match("#" * lpeg.C(Pcolorname) * "#", v)
local uid, mo = lpeg.match("#" * Puid_cap * "#", v)
local fontstyle = lpeg.match("#" * Pfontstyle_cap * "#", v)
if nr and ng and nb then
oldr, oldg, oldb = r, g, b
r, g, b = nr:parseHex(), ng:parseHex(), nb:parseHex()
......@@ -327,6 +333,8 @@ getmetatable(tmps).__index.drawColorStringBlended = function(s, font, str, x, y,
if h > max_h then max_h = h end
x = x + (w or 0)
end
elseif fontstyle then
font:setStyle(fontstyle)
else
local w, h = font:size(v)
if h > max_h then max_h = h end
......@@ -586,3 +594,22 @@ function util.uuid()
local uuid = tpl:gsub("[xy]", function(c) if c=='y' then return rng.table(y) else return rng.table(x) end end)
return uuid
end
function util.browserOpenUrl(url)
local tries = {
"rundll32 url.dll,FileProtocolHandler %s", -- Windows
"open %s", -- OSX
"xdg-open %s", -- Linux - portable way
"gnome-open %s", -- Linux - Gnome
"kde-open %s", -- Linux - Kde
"firefox %s", -- Linux - try to find something
"mozilla-firefox %s", -- Linux - try to find something
}
while #tries > 0 do
local urlbase = table.remove(tries, 1)
urlbase = urlbase:format(url)
print("Trying to run URL with command: ", urlbase)
if os.execute(urlbase) == 0 then return true end
end
return false
end
......@@ -45,12 +45,12 @@ function _M:run()
self.mod_list = Module:listModules()
self.save_list = Module:listSavefiles()
-- Setup display
self:selectStepMain()
self:checkLogged()
self:engineVersion()
-- Setup display
self:selectStepMain()
-- Ok everything is good to go, activate the game in the engine!
self:setCurrent()
......@@ -61,8 +61,15 @@ end
function _M:checkLogged()
if profile.auth then
self.s_log = core.display.drawStringBlendedNewSurface(self.profile_font, "Online Profile: "..profile.auth.name.."[http://te4.org/players/"..profile.auth.page.."]", 255, 255, 0)
self.logged_url = "http://te4.org/players/"..profile.auth.page
local str = "Online Profile: "..profile.auth.name.."[#LIGHT_BLUE##{underline}#"..self.logged_url.."#LAST##{normal}#]"
local plain = str:removeColorCodes()
local w, h = self.profile_font:size(plain)
self.s_log = core.display.newSurface(w, h)
self.s_log:erase(0, 0, 0)
self.s_log:drawColorStringBlended(self.profile_font, str, 0, 0, 255, 255, 0)
else
self.logged_url = nil
self.s_log = nil
end
end
......@@ -212,6 +219,7 @@ function _M:selectStepMain()
self.news.text = env.text
print("Latest engine version available: ", env.version[4], env.version[1], env.version[2], env.version[3])
self.latest_engine_version = env.version
if env.link then self.news.link = env.link end
else
self.news = nil
end
......@@ -233,7 +241,11 @@ Now go and have some fun!]]
}
end
self.tooltip:set("#AQUAMARINE#%s#WHITE#\n---\n%s", self.news.title, self.news.text)
if self.news.link then
self.tooltip:set("#AQUAMARINE#%s#WHITE#\n---\n%s\n---\n#LIGHT_BLUE##{underline}#%s#LAST##{normal}#", self.news.title, self.news.text, self.news.link)
else
self.tooltip:set("#AQUAMARINE#%s#WHITE#\n---\n%s", self.news.title, self.news.text)
end
end
self.step.do_tooltip = true
......@@ -248,6 +260,19 @@ Now go and have some fun!]]
self.step:setKeyHandling()
self.step:setMouseHandling()
if self.s_log then
local w, h = self.s_log:getSize()
self.mouse:registerZone(self.w - w, self.h - h, w, h, function(button)
if button == "left" then util.browserOpenUrl(self.logged_url) end
end, {button=true})
end
if self.news.link then
self.mouse:registerZone(5, self.tooltip.h - 30, self.tooltip.w, 30, function(button)
if button == "left" then util.browserOpenUrl(self.news.link) end
end, {button=true})
end
end
function _M:selectStepNew()
......
......@@ -505,6 +505,18 @@ static int sdl_font_lineskip(lua_State *L)
return 1;
}
static int sdl_font_style(lua_State *L)
{
TTF_Font **f = (TTF_Font**)auxiliar_checkclass(L, "sdl{font}", 1);
const char *style = luaL_checkstring(L, 2);
if (!strcmp(style, "normal")) TTF_SetFontStyle(*f, 0);
else if (!strcmp(style, "bold")) TTF_SetFontStyle(*f, TTF_STYLE_BOLD);
else if (!strcmp(style, "italic")) TTF_SetFontStyle(*f, TTF_STYLE_ITALIC);
else if (!strcmp(style, "underline")) TTF_SetFontStyle(*f, TTF_STYLE_UNDERLINE);
return 0;
}
static int sdl_surface_drawstring(lua_State *L)
{
SDL_Surface **s = (SDL_Surface**)auxiliar_checkclass(L, "sdl{surface}", 1);
......@@ -1378,6 +1390,7 @@ static const struct luaL_reg sdl_font_reg[] =
{"size", sdl_font_size},
{"height", sdl_font_height},
{"lineSkip", sdl_font_lineskip},
{"setStyle", sdl_font_style},
{NULL, NULL},
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment