Преглед изворни кода

Added mouse scrolling support.

Used in Console to scroll the history.
Thomas Buck пре 10 година
родитељ
комит
78e55f04a8
7 измењених фајлова са 59 додато и 31 уклоњено
  1. 4
    0
      include/Console.h
  2. 2
    0
      include/OpenRaider.h
  3. 0
    2
      include/utils/strings.h
  4. 43
    11
      src/Console.cpp
  5. 6
    8
      src/OpenRaider.cpp
  6. 4
    0
      src/WindowSDL.cpp
  7. 0
    10
      src/utils/strings.cpp

+ 4
- 0
include/Console.h Прегледај датотеку

@@ -41,6 +41,8 @@ public:
41 41
 
42 42
     void handleText(char *text, bool notFinished);
43 43
 
44
+    void handleMouseScroll(int xrel, int yrel);
45
+
44 46
 private:
45 47
 
46 48
     void moveInHistory(bool up);
@@ -54,6 +56,8 @@ private:
54 56
     size_t mHistoryPointer;
55 57
     std::vector<char *> mCommandHistory;
56 58
     char *mUnfinishedInput;
59
+
60
+    unsigned int mLineOffset;
57 61
 };
58 62
 
59 63
 #endif

+ 2
- 0
include/OpenRaider.h Прегледај датотеку

@@ -66,6 +66,8 @@ public:
66 66
 
67 67
     void handleMouseMotion(int xrel, int yrel);
68 68
 
69
+    void handleMouseScroll(int xrel, int yrel);
70
+
69 71
     Window *mWindow;
70 72
     Sound *mSound;
71 73
     Menu *mMenu;

+ 0
- 2
include/utils/strings.h Прегледај датотеку

@@ -16,8 +16,6 @@ char *stringRemoveQuotes(const char *s);
16 16
 
17 17
 char *stringReplace(const char *s, const char *search, const char *replace);
18 18
 
19
-void printStringVector(std::vector<char *> *args);
20
-
21 19
 int readBool(const char *value, bool *var);
22 20
 
23 21
 /*!

+ 43
- 11
src/Console.cpp Прегледај датотеку

@@ -29,6 +29,7 @@ Console::Console() {
29 29
     mPartialInput = NULL;
30 30
     mHistoryPointer = 0;
31 31
     mUnfinishedInput = NULL;
32
+    mLineOffset = 0;
32 33
 }
33 34
 
34 35
 Console::~Console() {
@@ -77,6 +78,16 @@ void Console::print(const char *s, ...) {
77 78
     }
78 79
 }
79 80
 
81
+#define LINE_GEOMETRY(window) unsigned int firstLine = 35; \
82
+        unsigned int lastLine = (window->mHeight / 2) - 55; \
83
+        unsigned int inputLine = (window->mHeight / 2) - 30; \
84
+        unsigned int lineSteps = 20; \
85
+        unsigned int lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
86
+        while (((lineCount * lineSteps) + firstLine) < inputLine) { \
87
+            lineSteps++; \
88
+            lineCount = (lastLine - firstLine + lineSteps) / lineSteps; \
89
+        }
90
+
80 91
 void Console::display() {
81 92
     Window *window = gOpenRaider->mWindow;
82 93
     unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
@@ -84,15 +95,7 @@ void Console::display() {
84 95
     if (mVisible) {
85 96
         // Calculate line drawing geometry
86 97
         // Depends on window height, so recalculate every time
87
-        unsigned int firstLine = 35;
88
-        unsigned int lastLine = (window->mHeight / 2) - 55;
89
-        unsigned int inputLine = (window->mHeight / 2) - 30;
90
-        unsigned int lineSteps = 20;
91
-        unsigned int lineCount = (lastLine - firstLine + lineSteps) / lineSteps;
92
-        while (((lineCount * lineSteps) + firstLine) < inputLine) {
93
-            lineSteps++;
94
-            lineCount = (lastLine - firstLine + lineSteps) / lineSteps;
95
-        }
98
+        LINE_GEOMETRY(window);
96 99
 
97 100
         // Draw half-transparent *overlay*
98 101
         glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
@@ -100,7 +103,15 @@ void Console::display() {
100 103
         glRecti(0, 0, window->mWidth, window->mHeight / 2);
101 104
         glEnable(GL_TEXTURE_2D);
102 105
 
103
-        gOpenRaider->mWindow->drawText(10, 10, 0.70f, color, "%s uptime %lus", VERSION, systemTimerGet() / 1000);
106
+        int scrollIndicator;
107
+        if (mHistory.size() > lineCount) {
108
+            scrollIndicator = (mHistory.size() - lineCount - mLineOffset) * 100 / (mHistory.size() - lineCount);
109
+        } else {
110
+            scrollIndicator = 100;
111
+        }
112
+
113
+        gOpenRaider->mWindow->drawText(10, 10, 0.70f, color,
114
+                "%s uptime %lus scroll %d%%", VERSION, systemTimerGet() / 1000, scrollIndicator);
104 115
 
105 116
         // Draw output log
106 117
         int end = lineCount;
@@ -114,7 +125,7 @@ void Console::display() {
114 125
         }
115 126
         for (int i = 0; i < end; i++) {
116 127
             gOpenRaider->mWindow->drawText(10, ((i + drawOffset) * lineSteps) + firstLine,
117
-                    0.75f, color, "%s", mHistory[i + historyOffset]);
128
+                    0.75f, color, "%s", mHistory[i + historyOffset - mLineOffset]);
118 129
         }
119 130
 
120 131
         // Draw current input
@@ -135,6 +146,8 @@ void Console::handleKeyboard(KeyboardButton key, bool pressed) {
135 146
             mHistory.push_back(bufferString("> %s", mInputBuffer));
136 147
             mCommandHistory.push_back(bufferString("%s", mInputBuffer));
137 148
             gOpenRaider->command(mInputBuffer);
149
+        } else {
150
+            mHistory.push_back(bufferString("> "));
138 151
         }
139 152
 
140 153
         // Clear partial and input buffer
@@ -200,6 +213,9 @@ void Console::moveInHistory(bool up) {
200 213
 void Console::handleText(char *text, bool notFinished) {
201 214
     //printf("Text: %s (%s)\n", text, (notFinished ? "not finished" : "finished"));
202 215
 
216
+    // Always scroll to bottom when text input is received
217
+    mLineOffset = 0;
218
+
203 219
     if (!notFinished) {
204 220
         // Finished entering character
205 221
         // delete previous partial character, if present
@@ -228,3 +244,19 @@ void Console::handleText(char *text, bool notFinished) {
228 244
     }
229 245
 }
230 246
 
247
+void Console::handleMouseScroll(int xrel, int yrel) {
248
+    LINE_GEOMETRY(gOpenRaider->mWindow);
249
+
250
+    if (mHistory.size() > lineCount) {
251
+        if (yrel > 0) {
252
+            if (mLineOffset < (mHistory.size() - lineCount)) {
253
+                mLineOffset++;
254
+            }
255
+        } else if (yrel < 0) {
256
+            if (mLineOffset > 0) {
257
+                mLineOffset--;
258
+            }
259
+        }
260
+    }
261
+}
262
+

+ 6
- 8
src/OpenRaider.cpp Прегледај датотеку

@@ -133,8 +133,6 @@ int OpenRaider::command(const char *command, std::vector<char *> *args) {
133 133
     if (strcmp(command, "set") == 0) {
134 134
         if (args->size() != 2) {
135 135
             mConsole->print("Invalid use of set-command ");
136
-            printStringVector(args);
137
-            printf("\n");
138 136
             return -2;
139 137
         } else {
140 138
             return set(args->at(0), args->at(1));
@@ -142,8 +140,6 @@ int OpenRaider::command(const char *command, std::vector<char *> *args) {
142 140
     } else if (strcmp(command, "bind") == 0) {
143 141
         if (args->size() != 2) {
144 142
             mConsole->print("Invalid use of bind-command ");
145
-            printStringVector(args);
146
-            printf("\n");
147 143
             return -3;
148 144
         } else {
149 145
             return bind(args->at(0), args->at(1));
@@ -162,14 +158,10 @@ int OpenRaider::command(const char *command, std::vector<char *> *args) {
162 158
             return help(args->at(0));
163 159
         } else {
164 160
             mConsole->print("Invalid use of help-command ");
165
-            printStringVector(args);
166
-            printf("\n");
167 161
             return -4;
168 162
         }
169 163
     } else {
170 164
         mConsole->print("Unknown command: %s ", command);
171
-        printStringVector(args);
172
-        printf("\n");
173 165
         return -1;
174 166
     }
175 167
 
@@ -660,3 +652,9 @@ void OpenRaider::handleMouseMotion(int xrel, int yrel) {
660 652
 
661 653
 }
662 654
 
655
+void OpenRaider::handleMouseScroll(int xrel, int yrel) {
656
+    if ((mConsole->isVisible()) && (!mMenu->isVisible())) {
657
+        mConsole->handleMouseScroll(xrel, yrel);
658
+    }
659
+}
660
+

+ 4
- 0
src/WindowSDL.cpp Прегледај датотеку

@@ -197,6 +197,10 @@ void WindowSDL::eventHandling() {
197 197
                 gOpenRaider->handleMouseClick(event.button.x, event.button.y, button, (event.type == SDL_MOUSEBUTTONUP));
198 198
                 break;
199 199
 
200
+            case SDL_MOUSEWHEEL:
201
+                gOpenRaider->handleMouseScroll(event.wheel.x, event.wheel.y);
202
+                break;
203
+
200 204
             case SDL_TEXTINPUT:
201 205
             case SDL_TEXTEDITING:
202 206
                 gOpenRaider->handleText(event.text.text, (event.type == SDL_TEXTEDITING));

+ 0
- 10
src/utils/strings.cpp Прегледај датотеку

@@ -58,16 +58,6 @@ char *stringReplace(const char *s, const char *search, const char *replace) {
58 58
     }
59 59
 }
60 60
 
61
-void printStringVector(std::vector<char *> *args) {
62
-    printf("(");
63
-    for (std::vector<char *>::size_type i = 0; i < args->size(); i++) {
64
-        printf("%s", args->at(i));
65
-        if (i < (args->size() - 1))
66
-            printf(" ");
67
-    }
68
-    printf(")");
69
-}
70
-
71 61
 int readBool(const char *value, bool *var) {
72 62
     if ((strcmp(value, "1") == 0) || (strcmp(value, "true") == 0) || (strcmp(value, "TRUE") == 0)) {
73 63
         *var = true;

Loading…
Откажи
Сачувај