Skip to content

Commit

Permalink
Merge pull request #26 from FaultyFunctions/master
Browse files Browse the repository at this point in the history
Close #23, Close #25 - Fix clear script, fix scrolling to first line, & smooth scrolling options
  • Loading branch information
nickavv authored Jan 13, 2021
2 parents 6ec1567 + a6d1a6f commit 611fb27
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ You can see examples of various ways to customize the shell's appearance on the
| `keyRepeatInitialDelay` | The amount of time in frames to wait after pressing and holding a key before it begins to repeat. | 25 |
| `keyRepeatDelay` | The amount of time in frames to wait between each repeat of a held key. | 4 |
| `scrollSpeed` | The number of pixels at a time to scroll the console when moving the mouse wheel. | 16 |
| `scrollSmoothness` | How smooth you want the scrolling to be when moving the mouse wheel. A number between 0 - 1. | 0.5 |

## Licensing

Expand Down
11 changes: 11 additions & 0 deletions rt-shell/objects/obj_shell/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ consoleString = "";
savedConsoleString = "";
scrollPosition = 0;
maxScrollPosition = 0;
targetScrollPosition = 0;
commandSubmitted = false; // Need to update scroll position one frame after a command is submitted
insertMode = true;

Expand Down Expand Up @@ -321,4 +322,14 @@ function array_contains(array, element) {
}
}
return false;
}

/// @param value
/// @param min_input
/// @param max_input
/// @param min_output
/// @param max_output
function remap(value, min_input, max_input, min_output, max_output) {
var _t = (value - min_input) / (max_input - min_input);
return lerp(min_output, max_output, _t);
}
16 changes: 7 additions & 9 deletions rt-shell/objects/obj_shell/Draw_64.gml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ if (isOpen) {
// Updating this here removes jitter as the scroll bar draws one frame in the old position
// and then jumps to the bottom. This fixes that
if (commandSubmitted) {
maxScrollPosition = max(0, outputHeight - visibleHeight);
scrollPosition = maxScrollPosition;
maxScrollPosition = max(0, outputHeight - visibleHeight + emHeight);
if (scrollSmoothness == 0) { scrollPosition = maxScrollPosition; }
targetScrollPosition = maxScrollPosition;
commandSubmitted = false;
}



surface_set_target(scrollSurface);
draw_clear_alpha(c_black, 0.0);
var yOffset = -scrollPosition;
Expand All @@ -42,10 +41,9 @@ if (isOpen) {
// the bottom of the panel
if (outputHeight < visibleHeight - emHeight) {
yOffset += visibleHeight - outputHeight - emHeight;
} else {
yOffset -= emHeight;
}


// Draw output history
for (var i = 0; i < array_length(output); i++) {
var outputStr = output[i];
Expand Down Expand Up @@ -127,7 +125,7 @@ if (isOpen) {
draw_surface(scrollSurface, 0, shellOriginY + 1 + consolePaddingV);

// Draw scrollbar
if (outputHeight > visibleHeight) {
if (outputHeight > visibleHeight - emHeight) {
var x1 = shellOriginX + width - consolePaddingH - scrollbarWidth;
var y1 = shellOriginY + consolePaddingV + 1;
var x2 = x1 + scrollbarWidth;
Expand All @@ -138,8 +136,8 @@ if (isOpen) {

var containerHeight = y2 - y1;

var scrollProgress = scrollPosition / (outputHeight - visibleHeight);
var scrollbarHeight = (visibleHeight / outputHeight) * containerHeight;
var scrollProgress = scrollPosition / (outputHeight - visibleHeight + emHeight);
var scrollbarHeight = (visibleHeight / (outputHeight + emHeight)) * containerHeight;
var scrollbarPosition = (containerHeight - scrollbarHeight) * scrollProgress;

y1 = y1 + scrollbarPosition;
Expand Down
3 changes: 2 additions & 1 deletion rt-shell/objects/obj_shell/Other_10.gml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ variable_global_set("sh_clear", function(args) {
return "";
} else {
array_push(output, ">" + consoleString);
var _newLinesCount = floor(height / string_height(prompt)) - 1;
draw_set_font(consoleFont);
var _newLinesCount = floor(visibleHeight / string_height(prompt));
repeat(_newLinesCount) {
array_push(output, "\n");
}
Expand Down
32 changes: 19 additions & 13 deletions rt-shell/objects/obj_shell/Step_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ if (!isOpen) {
}
} else {
var prevConsoleString = consoleString;
maxScrollPosition = max(0, outputHeight - visibleHeight);

if (keyboard_check_pressed(vk_escape)) {
if (isAutocompleteOpen) {
Expand All @@ -15,20 +14,20 @@ if (!isOpen) {
} else if (self.keyboardCheckDelay(vk_backspace)) {
consoleString = string_delete(consoleString, cursorPos - 1, 1);
cursorPos = max(1, cursorPos - 1);
scrollPosition = maxScrollPosition;
targetScrollPosition = maxScrollPosition;
} else if (self.keyboardCheckDelay(vk_delete)) {
consoleString = string_delete(consoleString, cursorPos, 1);
scrollPosition = maxScrollPosition;
targetScrollPosition = 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 = "";
scrollPosition = maxScrollPosition;
targetScrollPosition = maxScrollPosition;
} else if (self.keyboardCheckDelay(vk_left)) {
cursorPos = max(1, cursorPos - 1);
scrollPosition = maxScrollPosition;
targetScrollPosition = maxScrollPosition;
} else if (self.keyboardCheckDelay(vk_right)) {
if (cursorPos == string_length(consoleString) + 1 &&
array_length(filteredSuggestions) != 0) {
Expand All @@ -37,7 +36,7 @@ if (!isOpen) {
} else {
cursorPos = min(string_length(consoleString) + 1, cursorPos + 1);
}
scrollPosition = maxScrollPosition;
targetScrollPosition = maxScrollPosition;
} else if (self.keyComboPressed(historyUpModifiers, historyUpKey)) {
if (historyPos == array_length(history)) {
savedConsoleString = consoleString;
Expand All @@ -47,7 +46,7 @@ if (!isOpen) {
consoleString = array_get(history, historyPos);
cursorPos = string_length(consoleString) + 1;
}
scrollPosition = maxScrollPosition;
targetScrollPosition = maxScrollPosition;
} else if (self.keyComboPressed(historyDownModifiers, historyDownKey)) {
if (historyPos < array_length(history)) {
historyPos = min(array_length(history), historyPos + 1);
Expand All @@ -58,7 +57,7 @@ if (!isOpen) {
}
cursorPos = string_length(consoleString) + 1;
}
scrollPosition = maxScrollPosition;
targetScrollPosition = maxScrollPosition;
} else if (keyboard_check_pressed(vk_enter)) {
if (isAutocompleteOpen) {
self.confirmCurrentSuggestion();
Expand Down Expand Up @@ -146,23 +145,30 @@ if (!isOpen) {
}
} else if (point_in_rectangle(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), shellOriginX, shellOriginY, shellOriginX + width, shellOriginY + height)) {
if (mouse_wheel_down()) {
scrollPosition += scrollSpeed;
targetScrollPosition = targetScrollPosition + scrollSpeed;
}
if (mouse_wheel_up()) {
scrollPosition -= scrollSpeed;
targetScrollPosition = targetScrollPosition - scrollSpeed;
}
}
} else {
if (point_in_rectangle(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), shellOriginX, shellOriginY, shellOriginX + width, shellOriginY + height)) {
if (mouse_wheel_down()) {
scrollPosition += scrollSpeed;
targetScrollPosition = targetScrollPosition + scrollSpeed;
}
if (mouse_wheel_up()) {
scrollPosition -= scrollSpeed;
targetScrollPosition = targetScrollPosition - scrollSpeed;
}
}
}
scrollPosition = clamp(scrollPosition, 0, maxScrollPosition);

// Updating scrolling
var lerpValue = (scrollSmoothness == 0) ? 1 : remap(scrollSmoothness, 1, 0, 0.08, 0.4);
scrollPosition = lerp(scrollPosition, targetScrollPosition, lerpValue);
scrollPosition = clamp(scrollPosition, 0, maxScrollPosition)
if (scrollPosition == 0 || scrollPosition == maxScrollPosition) {
targetScrollPosition = clamp(targetScrollPosition, 0, maxScrollPosition);
}

if (consoleString != prevConsoleString) {
// If the text at the prompt has changed, update the list of possible
Expand Down
1 change: 1 addition & 0 deletions rt-shell/objects/obj_shell/obj_shell.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 611fb27

Please sign in to comment.