#include #include #include "InputScrollingMenu.h" #include "constant.h" using namespace std; extern FONT *joystix; int InputScrollingMenu::getScrollPosition() { return scrollPosition; } void InputScrollingMenu::setCommand(int desiredLength, const char * const *desiredCommand) { length = desiredLength; command = desiredCommand; setValue(0); } void InputScrollingMenu::setScrollPosition(int desiredScrollPosition) { scrollPosition = desiredScrollPosition; if (scrollPosition >= length) scrollPosition = length - 1; if (scrollPosition < 0) scrollPosition = 0; if (onScroll) (*onScroll)(); } void InputScrollingMenu::previous() { setValue((value + length - 1) % length); } void InputScrollingMenu::next() { setValue((value + 1) % length); } void InputScrollingMenu::scrollUp() { setScrollPosition(scrollPosition - 1); } void InputScrollingMenu::scrollDown() { setScrollPosition(scrollPosition + 1); } int InputScrollingMenu::getValue() { return value; } void InputScrollingMenu::setValue(int desiredValue) { value = desiredValue; if (value - scrollPosition < scrollDiff) setScrollPosition(scrollPosition - scrollStep); else if (displayLength - value + scrollPosition <= scrollDiff) setScrollPosition(scrollPosition + scrollStep); if (value < scrollPosition || value >= scrollPosition + displayLength) setScrollPosition(value - displayLength / 2); } bool InputScrollingMenu::acknowledgeKeyStroke(int keyStroke) { switch (keyStroke >> 8) { case KEY_1_PAD: case KEY_END: setValue(length - 1); return true; case KEY_2_PAD: case KEY_6_PAD: case KEY_RIGHT: case KEY_DOWN: next(); return true; case KEY_3_PAD: case KEY_PGDN: setValue((value + displayLength) % length); return true; case KEY_4_PAD: case KEY_8_PAD: case KEY_LEFT: case KEY_UP: previous(); return true; case KEY_7_PAD: case KEY_HOME: setValue(0); return true; case KEY_9_PAD: case KEY_PGUP: setValue(((value - displayLength) % length + length) % length); return true; case KEY_ESC: if (blocking) { commit(-1); return true; } break; case KEY_ENTER: case KEY_ENTER_PAD: if (blocking) { commit(value); return true; } } return false; } bool InputScrollingMenu::acknowledgeMouse() { bool mouseIndicatesCommand = false; const int mousePosition = mouse_pos; const int mouseButtons = mouse_b; if ((mouseButtons & 0x1) && !(oldMouseB & 0x1)) for (int i = scrollPosition; i < scrollPosition + displayLength && i < length; i++) if ((mousePosition >> 16) >= x && (mousePosition >> 16) < x + commandMaxCharCount * charWidth && (mousePosition & 0xffff) >= y + (i - scrollPosition) * charHeight && (mousePosition & 0xffff) < y + (i - scrollPosition) * charHeight + charHeight) { setValue(i); mouseIndicatesCommand = true; break; // setValue has set the variables used in this loop! } if (blocking && mouseIndicatesCommand) commit(value); oldMouseB = mouseButtons; return mouseIndicatesCommand; } void InputScrollingMenu::shift(int xOffset, int yOffset) { x += xOffset; y += yOffset; } bool InputScrollingMenu::within(int xTarget, int yTarget) { return xTarget >= x && xTarget < x + commandMaxCharCount * charWidth && yTarget >= y && yTarget < y + displayLength * charHeight; } void InputScrollingMenu::draw() { rectfill(canvas, x, y, x + commandMaxCharCount * charWidth - 1, y + displayLength * charHeight - 1, 16); for (int i = scrollPosition; i < scrollPosition + displayLength && i < length; i++) { char truncated[commandMaxCharCount + 1]; rectfill(canvas, x, y + (i - scrollPosition) * charHeight, x + commandMaxCharCount * charWidth - 1, y + (i - scrollPosition) * charHeight + charHeight - 1, i == value ? interactivityColor : 16); textout_ex(canvas, joystix, strncpy(truncated, command[i], commandMaxCharCount), x, y + (i - scrollPosition) * charHeight, 15, -1); } } void InputScrollingMenu::erase() { rectfill(canvas, x, y, x + commandMaxCharCount * charWidth - 1, y + displayLength * charHeight - 1, 0); }