Skip to content

Commit

Permalink
Merge pull request #22 from FaultyFunctions/master
Browse files Browse the repository at this point in the history
Fix cursor scaling issue + add insert/overtype mode to shell
  • Loading branch information
nickavv authored Jan 12, 2021
2 parents 29fe5c9 + edc8c56 commit fece175
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions rt-shell/objects/obj_shell/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ savedConsoleString = "";
scrollPosition = 0;
maxScrollPosition = 0;
commandSubmitted = false; // Need to update scroll position one frame after a command is submitted
insertMode = true;

historyPos = 0;
history = [];
Expand Down
36 changes: 22 additions & 14 deletions rt-shell/objects/obj_shell/Draw_64.gml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
if (isOpen) {
draw_set_font(consoleFont);
// pre-calculate one "em" of height
var lineHeight = string_height("M");
// pre-calculate one "em" of width & height
var emWidth = string_width("M");
var emHeight = string_height("M");

if (!surface_exists(shellSurface)) {
shellSurface = surface_create(display_get_gui_width(), display_get_gui_height());
Expand Down Expand Up @@ -37,10 +38,10 @@ if (isOpen) {

// Add some blank space if our output is too short so things appear to come from
// the bottom of the panel
if (outputHeight < visibleHeight - lineHeight) {
yOffset += visibleHeight - outputHeight - lineHeight;
if (outputHeight < visibleHeight - emHeight) {
yOffset += visibleHeight - outputHeight - emHeight;
} else {
yOffset -= lineHeight;
yOffset -= emHeight;
}

// Draw output history
Expand All @@ -65,11 +66,18 @@ if (isOpen) {
draw_set_color(fontColor);
draw_text(shellOriginX + promptXOffset, yOffset, consoleString);

// Draw a flashing text prompt
if (delayFrames > 1 || current_time % 1000 < 600) {
draw_text(shellOriginX + promptXOffset + string_width(string_copy(consoleString + " ", 1, cursorPos - 1)) - 3, yOffset, "|");
} else if (keyboard_check(vk_anykey)) {
draw_text(shellOriginX + promptXOffset + string_width(string_copy(consoleString + " ", 1, cursorPos - 1)) - 3, yOffset, "|");
// Draw text cursor
var cursorPosX = shellOriginX + promptXOffset + string_width(string_copy(consoleString + " ", 1, cursorPos - 1));
if (insertMode) {
if (delayFrames > 1 || current_time % 1000 < 600) {
draw_line_width(cursorPosX, yOffset, cursorPosX, yOffset + emHeight, 1);
} else if (keyboard_check(vk_anykey)) {
draw_line_width(cursorPosX, yOffset, cursorPosX, yOffset + emHeight, 1);
}
} else {
draw_line_width(cursorPosX + (emWidth / 2) - 1, yOffset, cursorPosX + (emWidth / 2) - 1, yOffset + emHeight, emWidth);
draw_set_color(promptColor);
draw_text(cursorPosX, yOffset, string_copy(consoleString, cursorPos, 1));
}

// Draw current suggestion & argument hints
Expand Down Expand Up @@ -145,9 +153,9 @@ if (isOpen) {

var suggestionOffsetX = string_width(string_copy(consoleString, 1, string_last_pos(" ", consoleString)));
var x1 = shellOriginX + promptXOffset - consolePadding + suggestionOffsetX;
var y1 = (screenAnchorPointV == "bottom") ? shellOriginY + height - (lineHeight * 1.5) - (suggestionsAmount * lineHeight) : shellOriginY + height;
var y1 = (screenAnchorPointV == "bottom") ? shellOriginY + height - (emHeight * 1.5) - (suggestionsAmount * emHeight) : shellOriginY + height;
var x2 = x1 + autocompleteMaxWidth + font_get_size(consoleFont);
var y2 = (screenAnchorPointV == "bottom") ? shellOriginY + height - (lineHeight * 1.5) : y1 + (suggestionsAmount * lineHeight);
var y2 = (screenAnchorPointV == "bottom") ? shellOriginY + height - (emHeight * 1.5) : y1 + (suggestionsAmount * emHeight);

autocompleteOriginX = x1;
autocompleteOriginY = y1;
Expand Down Expand Up @@ -176,7 +184,7 @@ if (isOpen) {
for (var i = 0; i < array_length(filteredSuggestions); i++) {
if (i < suggestionsAmount) {
// Enable mouse detection
if (point_in_rectangle(device_mouse_x_to_gui(0) - 1, device_mouse_y_to_gui(0) - 1, x1, y1 + (i * lineHeight), x2, y1 + (i * lineHeight) + lineHeight - 1)) {
if (point_in_rectangle(device_mouse_x_to_gui(0) - 1, device_mouse_y_to_gui(0) - 1, x1, y1 + (i * emHeight), x2, y1 + (i * emHeight) + emHeight - 1)) {
if (device_mouse_x_to_gui(0) != mousePreviousX || device_mouse_y_to_gui(0) != mousePreviousY) {
suggestionIndex = i + autocompleteScrollPosition;
mousePreviousX = device_mouse_x_to_gui(0);
Expand All @@ -199,7 +207,7 @@ if (isOpen) {
draw_set_color(fontColorSecondary);
}

draw_text(x1 + consolePadding, y1 + (i * lineHeight), filteredSuggestions[i + autocompleteScrollPosition]);
draw_text(x1 + consolePadding, y1 + (i * emHeight), filteredSuggestions[i + autocompleteScrollPosition]);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions rt-shell/objects/obj_shell/Step_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ if (!isOpen) {
scrollPosition = maxScrollPosition;
} else if (keyboard_string != "") {
var t = keyboard_string;
if (!insertMode) { consoleString = string_delete(consoleString, cursorPos, string_length(t)); }
consoleString = string_insert(t, consoleString, cursorPos);
cursorPos += string_length(t);
keyboard_string = "";
Expand Down Expand Up @@ -129,6 +130,8 @@ if (!isOpen) {
if (isAutocompleteOpen) {
self.calculate_scroll_from_suggestion_index()
}
} else if (keyboard_check_pressed(vk_insert)) {
insertMode = !insertMode;
}

// Handle scrolling
Expand Down

0 comments on commit fece175

Please sign in to comment.