Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Tardigrade-nx authored Jul 19, 2021
2 parents fab9e90 + ae134ee commit b997483
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 4 deletions.
Binary file added res/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,6 @@ extern SDL_Texture *g_iconCancel;
extern SDL_Texture *g_iconFloppy;
extern SDL_Texture *g_iconImage;
extern SDL_Texture *g_iconFileText;
extern SDL_Texture *g_iconPlus;

#endif
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SDL_Texture *g_iconCancel = NULL;
SDL_Texture *g_iconFloppy = NULL;
SDL_Texture *g_iconImage = NULL;
SDL_Texture *g_iconFileText = NULL;
SDL_Texture *g_iconPlus = NULL;

//------------------------------------------------------------------------------

Expand Down Expand Up @@ -66,6 +67,7 @@ int main(int argc, char* args[])
g_iconFloppy = SDLUtils::loadTexture(std::string(RES_PATH) + "/floppy.png");
g_iconImage = SDLUtils::loadTexture(std::string(RES_PATH) + "/image.png");
g_iconFileText = SDLUtils::loadTexture(std::string(RES_PATH) + "/file-text.png");
g_iconPlus = SDLUtils::loadTexture(std::string(RES_PATH) + "/plus.png");

// Load fonts
g_font = SDLUtils::loadFont(std::string(RES_PATH) + "/" + FONT_NAME, FONT_SIZE);
Expand Down Expand Up @@ -109,6 +111,7 @@ int main(int argc, char* args[])
if (g_iconFloppy != NULL) { SDL_DestroyTexture(g_iconFloppy); g_iconFloppy = NULL; }
if (g_iconImage != NULL) { SDL_DestroyTexture(g_iconImage); g_iconImage = NULL; }
if (g_iconFileText != NULL) { SDL_DestroyTexture(g_iconFileText); g_iconFileText = NULL; }
if (g_iconPlus != NULL) { SDL_DestroyTexture(g_iconPlus); g_iconPlus = NULL; }

// Quit SDL
SDLUtils::close();
Expand Down
97 changes: 93 additions & 4 deletions src/textEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,25 @@ void TextEditor::keyPressed(const SDL_Event &event)
// Open action menu
Dialog dialog("Actions:");
dialog.addOption("Save", 0, g_iconFloppy);
if (m_textSelectionStart.y != -1 && m_textSelectionEnd.y != -1 && (m_textSelectionStart.x != m_textSelectionEnd.x || m_textSelectionStart.y != m_textSelectionEnd.y))
{
dialog.addOption("Copy", 4, g_iconCopy);
dialog.addOption("Cut", 5, g_iconCut);
}
if (! m_clipboard.empty())
dialog.addOption("Paste", 6, g_iconPaste);
dialog.addOption("Delete line", 1, g_iconTrash);
dialog.addOption("Duplicate line", 2, g_iconCopy);
dialog.addOption("Duplicate line", 2, g_iconPlus);
dialog.addOption("Quit", 3, g_iconQuit);
switch (dialog.execute())
{
case 0: save(); break;
case 1: deleteLine(); break;
case 2: duplicateLine(); break;
case 3: quit(); break;
case 4: copy(); break;
case 5: cut(); break;
case 6: paste(); break;
default: break;
}
return;
Expand Down Expand Up @@ -312,6 +322,9 @@ void TextEditor::adjustCamera(void)
// Callbacks for virtual keyboard
void TextEditor::keyboardInputChar(const std::string &p_string)
{
// Remove selected text if any
if (m_textSelectionStart.y != -1 && m_textSelectionEnd.y != -1 && (m_textSelectionStart.x != m_textSelectionEnd.x || m_textSelectionStart.y != m_textSelectionEnd.y))
removeSelectedText();
// Selection = none
unselectText();
// Insert character
Expand All @@ -325,6 +338,9 @@ void TextEditor::keyboardInputChar(const std::string &p_string)

void TextEditor::keyboardInputEnter(void)
{
// Remove selected text if any
if (m_textSelectionStart.y != -1 && m_textSelectionEnd.y != -1 && (m_textSelectionStart.x != m_textSelectionEnd.x || m_textSelectionStart.y != m_textSelectionEnd.y))
removeSelectedText();
// Selection = none
unselectText();
// Insert new line
Expand Down Expand Up @@ -440,6 +456,79 @@ void TextEditor::quit(void)

//------------------------------------------------------------------------------

// Copy selected text in clipboard
void TextEditor::copy(void)
{
// If start point is after end point, swap them
SDL_Point start, end;
getSortedSelectionPoints(start, end);
// Copy selected text to clipboard
m_clipboard.clear();
for (int indLine = start.y; indLine <= end.y; ++indLine)
{
if (start.y == indLine && end.y == indLine)
m_clipboard.push_back(m_lines[indLine].substr(start.x, end.x - start.x));
else if (start.y == indLine)
m_clipboard.push_back(m_lines[indLine].substr(start.x));
else if (end.y == indLine)
m_clipboard.push_back(m_lines[indLine].substr(0, end.x));
else
m_clipboard.push_back(m_lines[indLine]);
}
}

//------------------------------------------------------------------------------

// Cut selected text in clipboard
void TextEditor::cut(void)
{
// Cut = copy then remove
copy();
removeSelectedText();
}

//------------------------------------------------------------------------------

// Paste clipboard into text
void TextEditor::paste(void)
{
// Remove selected text if any
if (m_textSelectionStart.y != -1 && m_textSelectionEnd.y != -1 && (m_textSelectionStart.x != m_textSelectionEnd.x || m_textSelectionStart.y != m_textSelectionEnd.y))
removeSelectedText();
// Rest of the line after insertion
std::string rest = m_lines[m_inputTextCursor.y].substr(m_inputTextCursor.x);
m_lines[m_inputTextCursor.y].erase(m_inputTextCursor.x);
// Insert clipboard lines into text
int indLine = m_inputTextCursor.y;
for (auto it = m_clipboard.begin(); it != m_clipboard.end(); ++it)
{
if (it == m_clipboard.begin())
{
m_lines[indLine].append(*it);
}
else
{
++indLine;
m_lines.insert(m_lines.begin() + indLine, *it);
}
}
// Move cursor to the end of the pasted part
m_inputTextCursor.y = indLine;
m_inputTextCursor.x = m_lines[indLine].size();
m_oldX = m_inputTextCursor.x;
// Restore end of first line
m_lines[indLine].append(rest);
// Update everything
unselectText();
m_nbItems = m_lines.size();
adjustScrollbar();
adjustCamera();
m_hasModifications = true;
g_hasChanged = true;
}

//------------------------------------------------------------------------------

// Delete current line
void TextEditor::deleteLine(void)
{
Expand Down Expand Up @@ -575,14 +664,14 @@ void TextEditor::removeSelectedText(void)
}
}
// Update everything
m_inputTextCursor = m_textSelectionStart;
m_inputTextCursor = start;
m_oldX = m_inputTextCursor.x;
unselectText();
m_nbItems = m_lines.size();
m_hasModifications = true;
g_hasChanged = true;
adjustScrollbar();
adjustCamera();
m_hasModifications = true;
g_hasChanged = true;
}

//------------------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions src/textEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ class TextEditor : public IWindow
// Quit, with a warning if unsaved modifications
void quit(void);

// Copy selected text in clipboard
void copy(void);

// Cut selected text in clipboard
void cut(void);

// Paste clipboard into text
void paste(void);

// Delete current line
void deleteLine(void);

Expand All @@ -79,6 +88,9 @@ class TextEditor : public IWindow
// Start and end for text selection
SDL_Point m_textSelectionStart;
SDL_Point m_textSelectionEnd;

// Clipboard containing lines, for copying or moving
std::vector<std::string> m_clipboard;
};

#endif

0 comments on commit b997483

Please sign in to comment.