mirror of
https://github.com/ArthurSonzogni/Diagon.git
synced 2026-08-29 16:40:45 +08:00
Add Drawing primitives.
This commit is contained in:
+5
-2
@@ -46,7 +46,10 @@ add_executable(${main}
|
||||
# Sequence
|
||||
add_subdirectory(sequence)
|
||||
target_link_libraries(${main} sequence)
|
||||
target_link_libraries(${main} antlr4_static)
|
||||
|
||||
# Scren
|
||||
add_subdirectory(screen)
|
||||
target_link_libraries(${main} screen)
|
||||
|
||||
# ANTLR
|
||||
target_link_libraries(${main} ${ANTLR_LIBRARIES})
|
||||
target_link_libraries(${main} antlr4_static)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
add_library(screen Screen.cpp)
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <sstream>
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
|
||||
#include "screen/Screen.h"
|
||||
|
||||
std::string to_string(const std::wstring& s) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
return converter.to_bytes(s);
|
||||
}
|
||||
|
||||
std::wstring to_wstring(const std::string& s) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
return converter.from_bytes(s);
|
||||
}
|
||||
|
||||
Screen::Screen(int width, int height)
|
||||
: width_(width),
|
||||
height_(height),
|
||||
lines_(height, std::wstring(width, U' ')) {}
|
||||
|
||||
void Screen::DrawText(int x, int y, const std::wstring& text) {
|
||||
for (auto& c : text)
|
||||
lines_[y][x++] = c;
|
||||
}
|
||||
|
||||
void Screen::DrawBox(int x, int y, int w, int h) {
|
||||
lines_[y][x + w - 1] = U'┐';
|
||||
lines_[y + h - 1][x + w - 1] = U'┘';
|
||||
lines_[y][x] = U'┌';
|
||||
lines_[y + h - 1][x] = U'└';
|
||||
for (int xx = 1; xx < w - 1; ++xx) {
|
||||
lines_[y][x + xx] = U'─';
|
||||
lines_[y + h - 1][x + xx] = U'─';
|
||||
}
|
||||
for (int yy = 1; yy < h - 1; ++yy) {
|
||||
lines_[y + yy][x] = U'│';
|
||||
lines_[y + yy][x + w - 1] = U'│';
|
||||
}
|
||||
}
|
||||
|
||||
void Screen::DrawBoxedText(int x, int y, const std::wstring& text) {
|
||||
DrawText(x + 1, y + 1, text);
|
||||
DrawBox(x, y, text.size() + 2, 3);
|
||||
}
|
||||
|
||||
std::string Screen::ToString() {
|
||||
std::stringstream ss;
|
||||
for(int y = 0; y<height_; ++y) {
|
||||
ss << to_string(lines_[y]) << '\n';
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef SCREEN_H
|
||||
#define SCREEN_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
std::string to_string(const std::wstring& s);
|
||||
std::wstring to_wstring(const std::string& s);
|
||||
|
||||
class Screen {
|
||||
public:
|
||||
Screen(int width, int height);
|
||||
void DrawText(int x, int y, const std::wstring& text);
|
||||
void DrawBox(int x, int y, int w, int h);
|
||||
void DrawBoxedText(int x, int y, const std::wstring& text);
|
||||
std::string ToString();
|
||||
|
||||
private:
|
||||
int width_;
|
||||
int height_;
|
||||
std::vector<std::wstring> lines_;
|
||||
};
|
||||
|
||||
#endif /* end of include guard: SCREEN_H */
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "sequence/SequenceLexer.h"
|
||||
#include "sequence/SequenceParser.h"
|
||||
#include "screen/Screen.h"
|
||||
|
||||
void SequenceImpl::Process(const std::string& input) {
|
||||
ComputeInternalRepresentation(input);
|
||||
@@ -44,12 +45,12 @@ void SequenceImpl::UniformizeInternalRepresentation() {
|
||||
|
||||
void SequenceImpl::UniformizeActors() {
|
||||
// Look at missing Actors.
|
||||
std::set<std::string> used_actors;
|
||||
std::set<std::wstring> used_actors;
|
||||
for (auto& message : messages) {
|
||||
used_actors.insert(message.from);
|
||||
used_actors.insert(message.to);
|
||||
}
|
||||
std::set<std::string> declared_actors;
|
||||
std::set<std::wstring> declared_actors;
|
||||
for (auto& actor : actors) {
|
||||
declared_actors.insert(actor.name);
|
||||
}
|
||||
@@ -169,15 +170,15 @@ void SequenceImpl::AddMessage(SequenceParser::MessageContext* message_context) {
|
||||
message.id = GetMessageID(message_id);
|
||||
}
|
||||
|
||||
message.from = message_context->Words(0)->getSymbol()->getText();
|
||||
message.to = message_context->Words(1)->getSymbol()->getText();
|
||||
message.from = to_wstring(message_context->Words(0)->getSymbol()->getText());
|
||||
message.to = to_wstring(message_context->Words(1)->getSymbol()->getText());
|
||||
message.messages = GetMessageText(message_context->messageText());
|
||||
messages.push_back(message);
|
||||
}
|
||||
|
||||
void SequenceImpl::AddActor(SequenceParser::ActorContext* actor_context) {
|
||||
Actor actor;
|
||||
actor.name = actor_context->Words()->getSymbol()->getText();
|
||||
actor.name = to_wstring(actor_context->Words()->getSymbol()->getText());
|
||||
|
||||
for (const auto& message_id : actor_context->Number()) {
|
||||
actor.message_id.push_back(std::stoi(message_id->getSymbol()->getText()));
|
||||
@@ -189,45 +190,56 @@ int SequenceImpl::GetMessageID(SequenceParser::MessageIDContext* message_id) {
|
||||
return std::stoi(message_id->Number()->getSymbol()->getText());
|
||||
}
|
||||
|
||||
std::vector<std::string> SequenceImpl::GetMessageText(
|
||||
std::vector<std::wstring> SequenceImpl::GetMessageText(
|
||||
SequenceParser::MessageTextContext* message_text) {
|
||||
std::vector<std::string> messages;
|
||||
std::vector<std::wstring> messages;
|
||||
if (message_text->getRuleIndex() == 0) {
|
||||
messages.push_back(
|
||||
messages.push_back(to_wstring(
|
||||
static_cast<SequenceParser::SingleLineTextContext*>(message_text)
|
||||
->Words()
|
||||
->getSymbol()
|
||||
->getText());
|
||||
->getText()));
|
||||
} else {
|
||||
auto m = static_cast<SequenceParser::MultiLineTextContext*>(message_text);
|
||||
for (const auto& line : m->Words()) {
|
||||
messages.push_back(line->getSymbol()->getText());
|
||||
messages.push_back(to_wstring(line->getSymbol()->getText()));
|
||||
}
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
void SequenceImpl::Draw() {
|
||||
std::stringstream ss;
|
||||
int width = 100;
|
||||
int height = 100;
|
||||
std::vector<std::wstring> lines(height, std::wstring(width, U' '));
|
||||
|
||||
Screen screen(width, height);
|
||||
|
||||
int x = 0;
|
||||
for (auto& actor : actors) {
|
||||
ss << "Actor " << actor.name << std::endl;
|
||||
for (auto& i : actor.message_id) {
|
||||
ss << " " << i << std::endl;
|
||||
}
|
||||
screen.DrawBoxedText(x, 0, actor.name);
|
||||
x += actor.name.size() + 2;
|
||||
}
|
||||
|
||||
for (auto& message : messages) {
|
||||
ss << "Message " << std::endl;
|
||||
ss << " From " << message.from << std::endl;
|
||||
ss << " To " << message.to << std::endl;
|
||||
ss << " Id " << message.id << std::endl;
|
||||
for (auto& i : message.messages) {
|
||||
ss << " " << i << std::endl;
|
||||
}
|
||||
}
|
||||
output_ = screen.ToString();
|
||||
|
||||
//for (auto& actor : actors) {
|
||||
//ss << "Actor " << actor.name << std::endl;
|
||||
//for (auto& i : actor.message_id) {
|
||||
//ss << " " << i << std::endl;
|
||||
//}
|
||||
//}
|
||||
|
||||
//for (auto& message : messages) {
|
||||
//ss << "Message " << std::endl;
|
||||
//ss << " From " << message.from << std::endl;
|
||||
//ss << " To " << message.to << std::endl;
|
||||
//ss << " Id " << message.id << std::endl;
|
||||
//for (auto& i : message.messages) {
|
||||
//ss << " " << i << std::endl;
|
||||
//}
|
||||
//}
|
||||
|
||||
output_ = ss.str();
|
||||
}
|
||||
|
||||
std::string SequenceImpl::Output() {
|
||||
|
||||
@@ -17,16 +17,16 @@ class SequenceImpl : public Sequence {
|
||||
|
||||
private:
|
||||
struct Actor {
|
||||
std::string name;
|
||||
std::wstring name;
|
||||
std::vector<int> message_id;
|
||||
};
|
||||
std::vector<Actor> actors;
|
||||
|
||||
struct Message {
|
||||
std::string from;
|
||||
std::string to;
|
||||
std::wstring from;
|
||||
std::wstring to;
|
||||
int id = -1;
|
||||
std::vector<std::string> messages;
|
||||
std::vector<std::wstring> messages;
|
||||
};
|
||||
std::vector<Message> messages;
|
||||
|
||||
@@ -36,7 +36,7 @@ class SequenceImpl : public Sequence {
|
||||
void AddMessage(SequenceParser::MessageContext* message);
|
||||
void AddActor(SequenceParser::ActorContext* actor_context);
|
||||
int GetMessageID(SequenceParser::MessageIDContext* message_id);
|
||||
std::vector<std::string> GetMessageText(
|
||||
std::vector<std::wstring> GetMessageText(
|
||||
SequenceParser::MessageTextContext* message_text);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user