Browse Source

Added mouse scrolling support.

Used in Console to scroll the history.
Thomas Buck 10 years ago
parent
commit
78e55f04a8
7 changed files with 59 additions and 31 deletions
  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 View File

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

+ 2
- 0
include/OpenRaider.h View File

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

+ 0
- 2
include/utils/strings.h View File

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

+ 43
- 11
src/Console.cpp View File

29
     mPartialInput = NULL;
29
     mPartialInput = NULL;
30
     mHistoryPointer = 0;
30
     mHistoryPointer = 0;
31
     mUnfinishedInput = NULL;
31
     mUnfinishedInput = NULL;
32
+    mLineOffset = 0;
32
 }
33
 }
33
 
34
 
34
 Console::~Console() {
35
 Console::~Console() {
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
 void Console::display() {
91
 void Console::display() {
81
     Window *window = gOpenRaider->mWindow;
92
     Window *window = gOpenRaider->mWindow;
82
     unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
93
     unsigned char color[4] = {0xFF, 0xFF, 0xFF, 0xFF};
84
     if (mVisible) {
95
     if (mVisible) {
85
         // Calculate line drawing geometry
96
         // Calculate line drawing geometry
86
         // Depends on window height, so recalculate every time
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
         // Draw half-transparent *overlay*
100
         // Draw half-transparent *overlay*
98
         glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
101
         glColor4f(0.0f, 0.0f, 0.0f, 0.5f);
100
         glRecti(0, 0, window->mWidth, window->mHeight / 2);
103
         glRecti(0, 0, window->mWidth, window->mHeight / 2);
101
         glEnable(GL_TEXTURE_2D);
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
         // Draw output log
116
         // Draw output log
106
         int end = lineCount;
117
         int end = lineCount;
114
         }
125
         }
115
         for (int i = 0; i < end; i++) {
126
         for (int i = 0; i < end; i++) {
116
             gOpenRaider->mWindow->drawText(10, ((i + drawOffset) * lineSteps) + firstLine,
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
         // Draw current input
131
         // Draw current input
135
             mHistory.push_back(bufferString("> %s", mInputBuffer));
146
             mHistory.push_back(bufferString("> %s", mInputBuffer));
136
             mCommandHistory.push_back(bufferString("%s", mInputBuffer));
147
             mCommandHistory.push_back(bufferString("%s", mInputBuffer));
137
             gOpenRaider->command(mInputBuffer);
148
             gOpenRaider->command(mInputBuffer);
149
+        } else {
150
+            mHistory.push_back(bufferString("> "));
138
         }
151
         }
139
 
152
 
140
         // Clear partial and input buffer
153
         // Clear partial and input buffer
200
 void Console::handleText(char *text, bool notFinished) {
213
 void Console::handleText(char *text, bool notFinished) {
201
     //printf("Text: %s (%s)\n", text, (notFinished ? "not finished" : "finished"));
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
     if (!notFinished) {
219
     if (!notFinished) {
204
         // Finished entering character
220
         // Finished entering character
205
         // delete previous partial character, if present
221
         // delete previous partial character, if present
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 View File

133
     if (strcmp(command, "set") == 0) {
133
     if (strcmp(command, "set") == 0) {
134
         if (args->size() != 2) {
134
         if (args->size() != 2) {
135
             mConsole->print("Invalid use of set-command ");
135
             mConsole->print("Invalid use of set-command ");
136
-            printStringVector(args);
137
-            printf("\n");
138
             return -2;
136
             return -2;
139
         } else {
137
         } else {
140
             return set(args->at(0), args->at(1));
138
             return set(args->at(0), args->at(1));
142
     } else if (strcmp(command, "bind") == 0) {
140
     } else if (strcmp(command, "bind") == 0) {
143
         if (args->size() != 2) {
141
         if (args->size() != 2) {
144
             mConsole->print("Invalid use of bind-command ");
142
             mConsole->print("Invalid use of bind-command ");
145
-            printStringVector(args);
146
-            printf("\n");
147
             return -3;
143
             return -3;
148
         } else {
144
         } else {
149
             return bind(args->at(0), args->at(1));
145
             return bind(args->at(0), args->at(1));
162
             return help(args->at(0));
158
             return help(args->at(0));
163
         } else {
159
         } else {
164
             mConsole->print("Invalid use of help-command ");
160
             mConsole->print("Invalid use of help-command ");
165
-            printStringVector(args);
166
-            printf("\n");
167
             return -4;
161
             return -4;
168
         }
162
         }
169
     } else {
163
     } else {
170
         mConsole->print("Unknown command: %s ", command);
164
         mConsole->print("Unknown command: %s ", command);
171
-        printStringVector(args);
172
-        printf("\n");
173
         return -1;
165
         return -1;
174
     }
166
     }
175
 
167
 
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 View File

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

+ 0
- 10
src/utils/strings.cpp View File

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
 int readBool(const char *value, bool *var) {
61
 int readBool(const char *value, bool *var) {
72
     if ((strcmp(value, "1") == 0) || (strcmp(value, "true") == 0) || (strcmp(value, "TRUE") == 0)) {
62
     if ((strcmp(value, "1") == 0) || (strcmp(value, "true") == 0) || (strcmp(value, "TRUE") == 0)) {
73
         *var = true;
63
         *var = true;

Loading…
Cancel
Save