Skip to content

Commit

Permalink
Fix various problems with inputs, the mod example and text coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
vaartis committed Jun 13, 2022
1 parent a5cd88a commit 5a967ef
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/lua/components/interaction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function InteractionSystem:update(dt)

pressed_e = true
elseif event.type == EventType.TextEntered then
local ch = utf8.char(event.text.unicode)
local ch = event.text.unicode

-- Convert the character to it's number equivalent
pressed_number = tonumber(ch)
Expand Down
27 changes: 15 additions & 12 deletions src/lua/terminal/instance_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ function M.InstanceMenuLine:initialize(args)
local text = lume.format("{1}. {2}", {line_counter, instance.name})
line_counter = line_counter + 1

local text_obj = Text.new("", StaticFonts.main_font, StaticFonts.font_size)
text_obj.fill_color = self._character.color
text_obj.character_size = self._character.font_size

table.insert(
self._decrypted_lines_texts,
{
text = text,
text_object = Text.new("", StaticFonts.main_font, StaticFonts.font_size),
text_object = text_obj,
next = instance.next,
-- Only exists for mods
mod = instance.mod,
Expand Down Expand Up @@ -125,10 +129,16 @@ function M.InstanceMenuLine:handle_interaction(event)
if not lines.inputing_text then lines.inputting_text = true end

if event.type == EventType.TextEntered then
local char = utf8.char(event.text.unicode)
local char = event.text.unicode

self._password_input_text.text = self._password_input_text.text .. char
self._letters_output = self._letters_output + 1

self:calculate_longest_line()

-- Because SFML considers backspace and return as inputting text, it has to be handled here
if char == "\b" then
return true
elseif event.type == EventType.KeyPressed then
if event.key.code == KeyboardKey.Backspace then
if #self._password_input_text.text > self._password_input_text.initial_length then
-- Remove the last character
self._password_input_text.text = self._password_input_text.text:sub(1, -2)
Expand All @@ -137,21 +147,14 @@ function M.InstanceMenuLine:handle_interaction(event)

self:calculate_longest_line()
end
elseif char == "\r" then
elseif event.key.code == KeyboardKey.Return then
if #self._password_input_text.text > self._password_input_text.initial_length then
-- Finish input
lines.reset_after_text_input()
self._done_input = true

return true
end
else
self._password_input_text.text = self._password_input_text.text .. char
self._letters_output = self._letters_output + 1
end

self:calculate_longest_line()

return true
end
end
Expand Down
5 changes: 2 additions & 3 deletions src/lua/terminal/lines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ end

function M.VariantInputLine:handle_interaction(event)
if event.type == EventType.TextEntered then
local ch = utf8.char(event.text.unicode)

local ch = event.text.unicode
-- Convert the character to it's number equivalent
local chnum = tonumber(ch)

Expand Down Expand Up @@ -514,7 +513,7 @@ function M.TextInputLine:handle_interaction(event)
return true
end
elseif event.type == EventType.TextEntered then
local char = utf8.char(event.text.unicode)
local char = event.text.unicode

local allowed = false
for _, filter in ipairs(self._filters) do
Expand Down
2 changes: 1 addition & 1 deletion src/lua/terminal/mod_lines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function M.ModWrapperLine:initialize(data)
error(err)
end

if type("loaded") == "function" then
if type(loaded) == "function" then
into[name] = loaded()
else
into[name] = loaded
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ int main(int argc, char **argv) {
"set_current_state", [&](CurrentState new_state) { current_state = new_state; },
"get_current_state", [&]() { return current_state; },
// Apparently lua doesn't have a good equivalent
"isalpha", [](int num) { return std::isalpha(num) != 0; },
"iscntrl", [](int num) { return std::iscntrl(num) != 0; },
"isalpha", [](const std::string &num) { return std::all_of(num.begin(), num.end(), [](int ch) { return std::isalpha(ch); }); },
"iscntrl", [](const std::string &num) { return std::all_of(num.begin(), num.end(), [](int ch) { return std::iscntrl(ch); }); },
"loaded_mods", loaded_mods,
"synchronize_saves", []() {
#if SOMEONE_EMSCRIPTEN
Expand Down
4 changes: 2 additions & 2 deletions src/usertypes/sfml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ void register_sfml_usertypes(sol::state &lua, StaticFonts &fonts) {
sol::base_classes, sol::bases<sf::Drawable, sf::Transformable>(),
"texture", sol::property(&sf::Sprite::getTexture, [](sf::Sprite &sprite, sf::Texture *texture) { sprite.setTexture(texture); }),
"texture_rect", sol::property(&sf::Sprite::getTextureRect, &sf::Sprite::setTextureRect),
"global_bounds", sol::property(&sf::Sprite::getGlobalBounds)
//"color", sol::property(&sf::Sprite::getColor, &sf::Sprite::setColor)
"global_bounds", sol::property(&sf::Sprite::getGlobalBounds),
"color", sol::property(&sf::Sprite::getColor, &sf::Sprite::setColor)
);

auto event_type = lua.new_usertype<sf::Event>(
Expand Down
6 changes: 6 additions & 0 deletions src/web_stubs/SFML/Graphics/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace sf {
class Sprite : public Transformable, public Drawable {
Texture *texture = nullptr;

Color color = Color::White;
IntRect textureRect;
public:
Sprite() = default;
Expand All @@ -33,6 +34,9 @@ class Sprite : public Transformable, public Drawable {
textureRect = rect;
}

sf::Color &getColor() { return color; }
void setColor(sf::Color other) { color = other; }

IntRect getGlobalBounds() {
IntRect result;

Expand Down Expand Up @@ -81,6 +85,7 @@ class Sprite : public Transformable, public Drawable {

auto unscaledOrigin = getUnscaledOrigin();

GPU_SetRGBA(texture->texture, color.r, color.g, color.b, color.a);
GPU_BlitRectX(
texture->texture,
&texRect,
Expand All @@ -91,6 +96,7 @@ class Sprite : public Transformable, public Drawable {
unscaledOrigin.y,
flip
);
GPU_UnsetColor(texture->texture);
}

Texture *getTexture() {
Expand Down
4 changes: 2 additions & 2 deletions src/web_stubs/SFML/Window/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Event {
};

struct TextEvent {
uint32_t unicode;
std::string unicode;
};

SDL_Event sdlEvent;
Expand Down Expand Up @@ -164,7 +164,7 @@ class RenderWindow : public RenderTarget {
}
case SDL_TEXTINPUT: {
theEvent.type = Event::TextEntered;
theEvent.text.unicode = sdlEvent.text.text[0];
theEvent.text.unicode = sdlEvent.text.text;

break;
}
Expand Down

0 comments on commit 5a967ef

Please sign in to comment.