~jadedctrl/gem-xwx-moe

~jadedctrl/gem-xwx-moe/gemujo_ludo/mods.niaj/fasado/subtitles/CornerSubtitleDisplay.lua
 ..
0 --[[
1 Subtitles — adds subtitles to Minetest.
2
3 Copyright © 2022‒2023, Silver Sandstone <@SilverSandstone@craftodon.social>
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.
22 ]]
23
24
25 --- Provides the CornerSubtitleDisplay class, which displays subtitles in a corner of the screen.
26
27
28 local S = subtitles.S;
29
30
31 --- Displays subtitles in a corner of the screen.
32 -- @type CornerSubtitleDisplay
33 subtitles.CornerSubtitleDisplay = subtitles.BaseTextSubtitleDisplay:extend();
34
35 subtitles.CornerSubtitleDisplay.NAME = 'corner';
36 subtitles.CornerSubtitleDisplay.ICON = 'subtitles_mode_corner.png';
37 subtitles.CornerSubtitleDisplay.TITLE = S('Text in corner');
38 subtitles.CornerSubtitleDisplay.DESCRIPTION = S('Displays a list of subtitles in the corner of the screen.');
39
40 function subtitles.CornerSubtitleDisplay:init()
41 self.super.init(self);
42
43 self.margin = 56;
44 self.size = {x = 200, y = 30};
45 self.offset = {x = -(self.margin + self.size.x / 2), y = -self.margin};
46 self.alignment = {x = 0, y = -1};
47 end;
48
49 function subtitles.CornerSubtitleDisplay:get_huds(entry)
50 local function _value_to_shade(value)
51 local shade = math.min(255, math.floor(127.5 + value * 127.5));
52 return subtitles.util.rgb_to_number(shade, shade, shade);
53 end;
54
55 local offset = entry:get_offset();
56 local huds = {};
57 local text_offset = -2;
58 local min_pan = 0.125;
59 local text_colour = _value_to_shade(entry:get_distance_shade());
60 huds.text =
61 {
62 hud_elem_type = 'text';
63 name = S('Subtitle');
64 text = entry.sound:get_description();
65 offset = {x = offset.x, y = offset.y + text_offset};
66 scale = self.size;
67 number = text_colour;
68 size = {x = self.font_size, y = self.font_size};
69 style = self.style;
70 z_index = self.z_index + 1;
71 };
72 huds.background =
73 {
74 hud_elem_type = 'image';
75 name = S('Subtitle background');
76 text = ('subtitles_subtitle_background.png^[resize:%dx%d'):format(self.size.x, self.size.y);
77 offset = offset;
78 scale = {x = 1, y = 1};
79 };
80
81 local relative_pos = entry.relative_pos;
82 if entry.relative_pos then
83 local pan = entry:get_pan();
84 local arrow_offset = 16;
85 huds.direction =
86 {
87 hud_elem_type = 'text';
88 name = S('Subtitle direction');
89 text = '';
90 offset = {x = offset.x, y = offset.y + text_offset};
91 scale = self.size;
92 number = _value_to_shade(math.abs(pan * (1 + min_pan)) - min_pan);
93 size = {x = self.font_size, y = self.font_size};
94 style = self.style;
95 z_index = self.z_index + 1;
96 };
97 if pan < -min_pan then
98 huds.direction.text = '<';
99 huds.direction.offset.x = offset.x - self.size.x / 2 + arrow_offset;
100 elseif pan > min_pan then
101 huds.direction.text = '>';
102 huds.direction.offset.x = offset.x + self.size.x / 2 - arrow_offset;
103 else
104 huds.direction.number = nil;
105 end;
106 end;
107
108 return huds;
109 end;
110
111 subtitles.CornerSubtitleDisplay:register();