This commit is contained in:
yukun 2019-12-18 13:26:59 +01:00
parent 6bbf08be56
commit 680ed3a1ad
3 changed files with 96 additions and 16 deletions

View File

@ -9,78 +9,98 @@ int main() {
vkvm::initialize(0); vkvm::initialize(0);
Terminal terminal = Terminal(); Terminal terminal = Terminal();
terminal.init();
std::cout << terminal.getString() << std::endl;
vkvm::registerEvent(vkvm::KeyDown, [&terminal]() { vkvm::registerEvent(vkvm::KeyDown, [&terminal]() {
vkvm::KeyCode keycode = vkvm::getLastPressedKey(); vkvm::KeyCode keycode = vkvm::getLastPressedKey();
std::cout << keycode << std::endl; std::cout << keycode << std::endl;
if(((vkvm::KeyCode::ShiftLeft & 0xfff) == keycode) ||((vkvm::KeyCode::ShiftRight & 0xfff) == keycode)){ if((vkvm::KeyCode::ShiftLeft == keycode) ||(vkvm::KeyCode::ShiftRight == keycode)){
terminal.shiftpressed(); terminal.shiftpressed();
} }
if(((vkvm::KeyCode::Zero & 0xfff) <= keycode) && (keycode <= (vkvm::Nine & 0xfff))){ if((vkvm::KeyCode::Zero <= keycode) && (keycode <=vkvm::Nine)){
char ch = keycode - (vkvm::KeyCode::Zero & 0xfff) + '0'; char ch = keycode - vkvm::KeyCode::Zero + '0';
std::cout << ch << std::endl; std::cout << ch << std::endl;
terminal.setString(ch); terminal.setString(ch);
vkvm::setText(terminal.getString()); vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl; std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText); vkvm::callEvent(vkvm::EventType::RenderText);
} }
if(((vkvm::KeyCode::A & 0xfff) <= keycode) && (keycode <= (vkvm::KeyCode::Z & 0xfff))){ if((vkvm::KeyCode::A <= keycode) && (keycode <= vkvm::KeyCode::Z)){
int status = terminal.getstatus(); int status = terminal.getstatus();
char ch; char ch;
if(status == 0){ if(status == 0){
ch = keycode - (vkvm::KeyCode::A & 0xfff) + 'a'; ch = keycode - vkvm::KeyCode::A + 'a';
} }
else{ else{
ch = keycode - (vkvm::KeyCode::A & 0xfff) + 'A'; ch = keycode - vkvm::KeyCode::A + 'A';
} }
std::cout << ch << std::endl; std::cout << ch << std::endl;
terminal.setString(ch); terminal.setString(ch);
vkvm::setText(terminal.getString()); vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl; std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText); vkvm::callEvent(vkvm::EventType::RenderText);
} }
if((vkvm::KeyCode::Space & 0xfff) == keycode){ if(vkvm::KeyCode::Space == keycode){
char ch = keycode - (vkvm::KeyCode::Space & 0xfff) + ' '; char ch = keycode - vkvm::KeyCode::Space + ' ';
std::cout << ch << std::endl; std::cout << ch << std::endl;
terminal.setString(ch); terminal.setString(ch);
vkvm::setText(terminal.getString()); vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl; std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText); vkvm::callEvent(vkvm::EventType::RenderText);
} }
if((vkvm::KeyCode::Backspace & 0xfff) == keycode){ if(vkvm::KeyCode::Backspace == keycode){
char ch = keycode - (vkvm::KeyCode::Backspace & 0xfff) + ' '; char ch = keycode - vkvm::KeyCode::Backspace + ' ';
std::cout << ch << std::endl; std::cout << ch << std::endl;
terminal.subString(); terminal.subString();
vkvm::setText(terminal.getString()); vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl; std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText); vkvm::callEvent(vkvm::EventType::RenderText);
} }
if((vkvm::KeyCode::Tab & 0xfff) == keycode){ if(vkvm::KeyCode::Tab == keycode){
char ch = keycode - (vkvm::KeyCode::Tab & 0xfff) + ' '; char ch = keycode - vkvm::KeyCode::Tab + ' ';
for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) {
std::cout << ch << std::endl; std::cout << ch << std::endl;
terminal.setString(ch); terminal.setString(ch);
} }
vkvm::setText(terminal.getString()); vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl; std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText); vkvm::callEvent(vkvm::EventType::RenderText);
} }
if((vkvm::KeyCode::Enter & 0xfff) == keycode){ if(vkvm::KeyCode::Enter == keycode){
char ch = '\n'; char ch = '\n';
std::cout << ch << std::endl; std::cout << ch << std::endl;
terminal.setString(ch); terminal.setString(ch);
vkvm::setText(terminal.getString()); vkvm::setText(terminal.getString());
std::cout << terminal.getString() << std::endl;
std::cout<< vkvm::getText()<< std::endl; std::cout<< vkvm::getText()<< std::endl;
vkvm::callEvent(vkvm::EventType::RenderText); vkvm::callEvent(vkvm::EventType::RenderText);
} }
if(vkvm::KeyCode::Arrow_Left == keycode){
terminal.moveleft();
std::cout << terminal.getString() << std::endl;
}
if(vkvm::KeyCode::Arrow_Right == keycode){
terminal.moveright();
std::cout << terminal.getString() << std::endl;
}
}); });

View File

@ -1,6 +1,7 @@
// //
// Created by yukun on 29.11.19. // Created by yukun on 29.11.19.
// //
#include <iostream>
#include "vkvm.hpp" #include "vkvm.hpp"
#include "Terminal.h" #include "Terminal.h"
@ -8,12 +9,41 @@ std::string Terminal::getString() {
return s; return s;
} }
void Terminal::init(){
s = s + cursor;
}
void Terminal::setString(char c) { void Terminal::setString(char c) {
s = s + c; // s.erase(s.end() - 1);
// s = s + c + cursor;
int i = s.find(cursor);
std::cout << i << std::endl;
std::cout<< s.length() -1 <<std::endl;
if(i < s.length() -1 ){
s= s + s[s.length() - 1];
int l = s.length() - 1;
for(; l > i + 1; l--){
s[l] = s[l-1];
}
s[i+1] = c;
}
else{
s = s + cursor;
std::cout << "else1 " + s << std::endl;
s[s.length()-2] = c;
std::cout << "else2 " + s << std::endl;
}
} }
void Terminal::subString(){ void Terminal::subString(){
s = s.substr(0, s.length() - 1); // if(s.length() > 1)
// s.erase(s.end() - 1);
// s.erase(s.end() - 1);
// s = s + cursor;
int i = s.find(cursor);
s.erase(i-1, 1);
} }
void Terminal::shiftpressed() { void Terminal::shiftpressed() {
@ -30,3 +60,28 @@ int Terminal::getstatus() {
return status; return status;
} }
void Terminal::moveleft(){
int i = s.find(cursor);
if(i > 0){
char c = s[i-1];
s[i-1] = cursor;
s[i] = c;
}
}
void Terminal::moveright() {
int i = s.find(cursor);
if(i < (s.length() - 1)){
char c = s[i+1];
s[i+1] = cursor;
s[i] = c;
}
}
void Terminal::movedown() {
}
void Terminal::moveup() {
}

View File

@ -9,15 +9,20 @@
class Terminal { class Terminal {
public: public:
void init();
std::string getString(); std::string getString();
void setString(char c); void setString(char c);
void subString(); void subString();
void shiftpressed(); void shiftpressed();
void shiftup(); void shiftup();
int getstatus(); int getstatus();
void moveleft();
void moveright();
void moveup();
void movedown();
private: private:
std::string s=""; std::string s="";
int status = 0; int status = 0;
char cursor = -127;
}; };
#endif //TERMINAL_TERMINAL_H #endif //TERMINAL_TERMINAL_H