-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_operation.cpp
193 lines (148 loc) · 4.44 KB
/
import_operation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <cstdio>
#include <cstring>
#include "libtcod.hpp"
#include "gui/gui.hpp"
#include "app.h"
#include "message_box.h"
#include "misc.h"
#include "import_operation.h"
ImportOperation::ImportOperation() {
}
ImportOperation::~ImportOperation() {
}
void ImportOperation::start() {
app->gui->importTextButton->setVisible(true);
app->gui->importForeButton->setVisible(true);
app->gui->importBackButton->setVisible(true);
}
void ImportOperation::update() {
}
void ImportOperation::end() {
}
void doImportTextCbk(Widget *wid, void *data) {
App *app = AppUser::app;
MessageBox msgBox("Import Text", "Enter filename to import from", 2, true);
if(!app->filename.empty()) {
std::string baseFilename = getBaseFilename(app->filename.c_str());
baseFilename.append(".txt");
msgBox.setString(baseFilename.c_str());
}
msgBox.show();
// If user presses ESCAPE or cancel
if(msgBox.getButtonPressed() == 0 || msgBox.getButtonPressed() == 2) {
return;
}
if(msgBox.getButtonPressed() == 1 && msgBox.getString() != NULL) {
std::string importFilename = msgBox.getString();
FILE *fp = fopen(importFilename.c_str(), "r");
// Check if file exists
if(fp == NULL) {
printf("The file %s could not be opened.\n", app->filename.c_str());
MessageBox msgBox2("Error", "The file could not be opened", 1);
msgBox2.show();
return;
}
app->addUndo();
int x = 0;
int y = 0;
for(int i = 0; ; i++) {
char c = getc(fp);
// If we reach the end of the file then exit the loop
if(c == EOF || y >= app->canvasHeight) {
y = app->canvasHeight;
x = app->canvasWidth;
break;
}
if(c == '\n') {
y++;
x = 0;
continue;
}
if(x < app->canvasWidth) {
app->canvasCon->setChar(x, y, c);
}
x++;
}
fclose(fp);
}
}
void doImportForeCbk(Widget *wid, void *data) {
App *app = AppUser::app;
MessageBox msgBox("Import Foreground Png", "Enter filename to import from", 2, true);
if(!app->filename.empty()) {
std::string baseFilename = getBaseFilename(app->filename.c_str());
baseFilename.append("-fore.png");
msgBox.setString(baseFilename.c_str());
}
msgBox.show();
// If user presses ESCAPE or cancel
if(msgBox.getButtonPressed() == 0 || msgBox.getButtonPressed() == 2) {
return;
}
if(msgBox.getButtonPressed() == 1 && msgBox.getString() != NULL) {
std::string importFilename = msgBox.getString();
FILE *fp = fopen(importFilename.c_str(), "r");
// Check if file exists
if(fp == NULL) {
printf("The file %s could not be opened.\n", app->filename.c_str());
MessageBox msgBox2("Error", "The file could not be opened", 1);
msgBox2.show();
return;
}
fclose(fp);
TCODImage importImg = TCODImage(importFilename.c_str());
int w, h;
importImg.getSize(&w, &h);
if(w != app->canvasWidth || h != app->canvasHeight) {
MessageBox msgBox("Error", "The file does not have the same dimensions as the current image", 1);
msgBox.show();
return;
}
app->addUndo();
for(int y = 0; y < app->canvasHeight; y++) {
for(int x = 0; x < app->canvasWidth; x++) {
app->canvasCon->setCharForeground(x, y, importImg.getPixel(x, y));
}
}
}
}
void doImportBackCbk(Widget *wid, void *data) {
App *app = AppUser::app;
MessageBox msgBox("Import Background Png", "Enter filename to import from", 2, true);
if(!app->filename.empty()) {
std::string baseFilename = getBaseFilename(app->filename.c_str());
baseFilename.append("-back.png");
msgBox.setString(baseFilename.c_str());
}
msgBox.show();
// If user presses ESCAPE or cancel
if(msgBox.getButtonPressed() == 0 || msgBox.getButtonPressed() == 2) {
return;
}
if(msgBox.getButtonPressed() == 1 && msgBox.getString() != NULL) {
std::string importFilename = msgBox.getString();
FILE *fp = fopen(importFilename.c_str(), "r");
// Check if file exists
if(fp == NULL) {
printf("The file %s could not be opened.\n", app->filename.c_str());
MessageBox msgBox2("Error", "The file could not be opened", 1);
msgBox2.show();
return;
}
fclose(fp);
TCODImage importImg = TCODImage(importFilename.c_str());
int w, h;
importImg.getSize(&w, &h);
if(w != app->canvasWidth || h != app->canvasHeight) {
MessageBox msgBox("Error", "The file does not have the same dimensions as the current image", 1);
msgBox.show();
return;
}
app->addUndo();
for(int y = 0; y < app->canvasHeight; y++) {
for(int x = 0; x < app->canvasWidth; x++) {
app->canvasCon->setCharBackground(x, y, importImg.getPixel(x, y));
}
}
}
}