Skip to content

Commit 2e2abcb

Browse files
authored
Marvin console improvements (#385)
* moveable cursor for marvin console
1 parent fb34da9 commit 2e2abcb

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

game/marvin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,16 @@ Marvin::CmdVal Marvin::recognize(std::string_view inp) {
211211
return suggestion;
212212
}
213213

214-
void Marvin::autoComplete(std::string& v) {
214+
bool Marvin::autoComplete(std::string& v) {
215215
auto ret = recognize(v);
216216
if(ret.cmd.type==C_Incomplete && !ret.complete.empty()) {
217217
for(auto& i:ret.complete)
218218
v.push_back(i);
219219
if(ret.fullword)
220220
v.push_back(' ');
221+
return true;
221222
}
223+
return false;
222224
}
223225

224226
bool Marvin::exec(std::string_view v) {

game/marvin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Marvin {
1111

1212
Tempest::Signal<void(std::string_view)> print;
1313

14-
void autoComplete(std::string& v);
14+
bool autoComplete(std::string& v);
1515
bool exec(std::string_view v);
1616

1717
private:

game/ui/consolewidget.cpp

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ void ConsoleWidget::close() {
8282

8383
ov->takeWidget(this);
8484
delete ov;
85-
log.back().clear();
8685
}
8786

8887
int ConsoleWidget::exec() {
@@ -116,7 +115,7 @@ void ConsoleWidget::paintEvent(PaintEvent& e) {
116115
fnt.drawText(p, margins().left, y, log[i]);
117116
y-=fnt.pixelSize();
118117
if(i+1==log.size()) {
119-
int x = margins().left + fnt.textSize(log[i]).w;
118+
int x = margins().left + fnt.textSize(log[i].data(),log[i].data()+cursPos).w;
120119
float a = float(Application::tickCount()%2000)/2000.f;
121120
p.setBrush(Color(1,1,1,a));
122121
p.drawRect(x,y,1,fnt.pixelSize());
@@ -137,36 +136,64 @@ void ConsoleWidget::keyDownEvent(KeyEvent& e) {
137136
close();
138137
}
139138

139+
if(e.key==Event::K_Left) {
140+
if (cursPos>0)
141+
cursPos--;
142+
return;
143+
}
144+
if(e.key==Event::K_Right) {
145+
if(cursPos<log.back().size())
146+
cursPos++;
147+
return;
148+
}
140149
if(e.key==Event::K_Up) {
141150
histPos++;
142-
if(histPos<cmdHist.size())
143-
log.back() = cmdHist[cmdHist.size()-1-histPos]; else
144-
histPos = size_t(cmdHist.size()-1);
151+
if(histPos<cmdHist.size()) {
152+
if(histPos==0)
153+
currCmd = log.back();
154+
log.back() = cmdHist[cmdHist.size()-1-histPos];
155+
cursPos = log.back().size();
156+
}
157+
else {
158+
histPos = size_t(cmdHist.size()-1);
159+
}
145160
return;
146161
}
147162
if(e.key==Event::K_Down) {
148163
if(histPos==size_t(-1))
149164
return;
150165
histPos--;
151166
if(histPos<cmdHist.size())
152-
log.back() = cmdHist[cmdHist.size()-1-histPos]; else
153-
log.back() = "";
167+
log.back() = cmdHist[cmdHist.size()-1-histPos];
168+
else
169+
log.back() = currCmd;
170+
cursPos = log.back().size();
154171
return;
155172
}
156173

157174
if(e.key==Event::K_C && (e.modifier&Event::M_Command)==Event::M_Command) {
158175
log.emplace_back("");
176+
cursPos = 0;
177+
currCmd = "";
178+
histPos = size_t(-1);
159179
return;
160180
}
161181

162182
if(e.key==Event::K_Back) {
163-
if(log.back().size()>0)
164-
log.back().pop_back();
183+
if(0<cursPos && cursPos<=log.back().size()) {
184+
log.back().erase(--cursPos,1);
185+
}
186+
return;
187+
}
188+
if(e.key==Event::K_Delete) {
189+
if(log.back().size()>cursPos) {
190+
log.back().erase(cursPos,1);
191+
}
165192
return;
166193
}
167194
if(e.key==Event::K_Tab) {
168-
if(log.back().size()>0)
169-
marvin.autoComplete(log.back());
195+
if(log.back().size()>0 && marvin.autoComplete(log.back()))
196+
cursPos = log.back().size();
170197
return;
171198
}
172199
if(e.key==Event::K_Return) {
@@ -175,6 +202,9 @@ void ConsoleWidget::keyDownEvent(KeyEvent& e) {
175202
if(!marvin.exec(log.back()))
176203
log.back() = "Unknown command : " + log.back();
177204
log.emplace_back("");
205+
cursPos = 0;
206+
currCmd = "";
207+
histPos = size_t(-1);
178208
}
179209
return;
180210
}
@@ -185,8 +215,8 @@ void ConsoleWidget::keyDownEvent(KeyEvent& e) {
185215
ch = char(e.code);
186216
if(ch=='\0')
187217
return;
188-
log.back().push_back(ch);
189-
histPos = size_t(-1);
218+
log.back().insert(cursPos,1,ch);
219+
cursPos++;
190220
}
191221

192222
void ConsoleWidget::keyRepeatEvent(KeyEvent& e) {

game/ui/consolewidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class ConsoleWidget : public Tempest::Widget {
2929
const Tempest::Texture2d* background = nullptr;
3030
Tempest::Shortcut closeSk;
3131
std::vector<std::string> log, cmdHist;
32+
std::string currCmd;
3233
size_t histPos = size_t(-1);
34+
size_t cursPos = 0;
3335
Marvin marvin;
3436
};
3537

0 commit comments

Comments
 (0)