#include #include #include "InputMenu.h" #include "constant.h" extern FONT *joystix; int* InputMenu::getX() { return x; } int* InputMenu::getY() { return y; } void InputMenu::previous() { value = (value + length - 1) % length; } void InputMenu::next() { value = (value + 1) % length; } int InputMenu::accelerate(int ascii) { for (int i = 0; i < length; i++) if (tolower(command[i][acceleratorKey[i]]) == tolower(ascii)) return i; return -1; } void InputMenu::drawCommands() { char singleton[2]; singleton[1] = '\0'; for (int i = 0; i < length; i++) { textout_ex(canvas, joystix, command[i], x[i], y[i], 15, 16); singleton[0] = command[i][acceleratorKey[i]]; textout_ex(canvas, joystix, singleton, x[i] + acceleratorKey[i] * charWidth, y[i], interactivityColor, -1); } } void InputMenu::drawSelect() { char singleton[2]; singleton[1] = '\0'; textout_ex(canvas, joystix, command[value], x[value], y[value], 15, interactivityColor); singleton[0] = command[value][acceleratorKey[value]]; textout_ex(canvas, joystix, singleton, x[value] + acceleratorKey[value] * charWidth, y[value], 16, -1); } void InputMenu::drawUnselect() { char singleton[2]; singleton[1] = '\0'; textout_ex(canvas, joystix, command[value], x[value], y[value], 15, 16); singleton[0] = command[value][acceleratorKey[value]]; textout_ex(canvas, joystix, singleton, x[value] + acceleratorKey[value] * charWidth, y[value], interactivityColor, -1); } int InputMenu::getValue() { return value; } void InputMenu::setValue(int desiredValue) { value = desiredValue; } bool InputMenu::acknowledgeKeyStroke(int keyStroke) { const int scanCode = keyStroke >> 8; const int accelerated = accelerate(scancode_to_ascii(scanCode)); switch (scanCode) { case KEY_2_PAD: case KEY_6_PAD: case KEY_RIGHT: case KEY_DOWN: next(); return true; case KEY_4_PAD: case KEY_8_PAD: case KEY_LEFT: case KEY_UP: previous(); 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; } break; default: if (accelerated != -1) { value = accelerated; if (blocking) commit(accelerated); return true; } } return false; } bool InputMenu::acknowledgeMouse() { bool mouseIndicatesCommand = false; const int mousePosition = mouse_pos; const int mouseButtons = mouse_b; for (int i = 0; i < length; i++) if ((mousePosition >> 16) >= x[i] && (mousePosition >> 16) < x[i] + strlen(command[i]) * charWidth && (mousePosition & 0xffff) >= y[i] && (mousePosition & 0xffff) < y[i] + charHeight) { value = i; mouseIndicatesCommand = true; } if (blocking && mouseIndicatesCommand && (oldMouseB & 0x1) && !(mouseButtons & 0x1)) commit(value); oldMouseB = mouseButtons; return mouseIndicatesCommand; } void InputMenu::shift(int xOffset, int yOffset) { for (int i = 0; i < length; i++) x[i] += xOffset; for (int i = 0; i < length; i++) y[i] += yOffset; } bool InputMenu::within(int xTarget, int yTarget) { for (int i = 0; i < length; i++) if (xTarget >= x[i] && xTarget < x[i] + strlen(command[i]) * charWidth && yTarget >= y[i] && yTarget < y[i] + charHeight) return true; return false; } void InputMenu::draw() { drawCommands(); drawSelect(); } void InputMenu::erase() { for (int i = 0; i < length; i++) rectfill(canvas, x[i], y[i], x[i] + strlen(command[i]) * charWidth - 1, y[i] + charHeight - 1, 0); }