Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
1584 lines (1355 loc) · 39.7 KB
/
Copy pathmain.cpp
File metadata and controls
1584 lines (1355 loc) · 39.7 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////
//
// MCRedstoneSimulator
// 我的世界红石电路模拟器
//
// by huidong <mailhuid@163.com>
//
// 最后修改:2022.08.22
//
#include "resource.h"
#include "HiEasyX.h"
#include "map_operation.h"
#include "painter.h"
#include <stdio.h>
#include <conio.h>
#include <direct.h>
// 版本信息
const WCHAR strVersion[] = L"Version 2.1";
// 缩放限制
#define MAX_ZOOM 2.0
#define MIN_ZOOM 0.1
// 全局按钮位置信息
RECT rctHelpBtn;
RECT rctSaveBtn;
RECT rctResizeBtn;
// 窗口句柄
HWND hGraphicsWnd; // 主绘图窗口
HWND hToolBarWnd; // 工具栏窗口
// 选择文件
// isSave标志着是否为保存模式
const WCHAR* SelectFile(bool isSave = false)
{
OPENFILENAME ofn;
static WCHAR szFile[256];
static WCHAR szFileTitle[256];
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = GetConsoleWindow();
ofn.lpstrFilter = L"Redstone map File(*.rsp)\0*.rsp\0All File(*.*)\0*.*\0";
ofn.lpstrDefExt = L"rsp";
ofn.nFilterIndex = 1;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = szFileTitle;
ofn.nMaxFileTitle = sizeof(szFileTitle);
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
char oldpath[1024] = { 0 };
_getcwd(oldpath, 1024);
wchar_t woldpath[128] = { 0 };
MultiByteToWideChar(CP_ACP, 0, oldpath, -1, woldpath, 1024);
if (isSave)
{
if (GetSaveFileName(&ofn))
{
SetCurrentDirectory(woldpath);
return szFile;
}
}
else
{
if (GetOpenFileName(&ofn))
{
SetCurrentDirectory(woldpath);
return szFile;
}
}
return _T("");
}
// 图片拉伸
// width, height 拉伸后的图片大小
// img 原图像
void ImageToSize(int width, int height, IMAGE* img)
{
IMAGE* pOldImage = GetWorkingImage();
SetWorkingImage(img);
IMAGE temp_image(width, height);
StretchBlt(
GetImageHDC(&temp_image), 0, 0, width, height,
GetImageHDC(img), 0, 0,
getwidth(), getheight(),
SRCCOPY
);
Resize(img, width, height);
putimage(0, 0, &temp_image);
SetWorkingImage(pOldImage);
}
/*
* 参考自http://tieba.baidu.com/pos/5218523817
* 函数名:zoomImage(IMAGE* pImg,int width,int height)
* 参数说明:pImg是原图指针,width1和height1是目标图片的尺寸。
* 函数功能:将图片进行缩放,返回目标图片 可以自定义长与宽,也可以只给长自动计算宽
* 返回目标图片
*/
IMAGE zoomImage(IMAGE* pImg, int newWidth, int newHeight = 0)
{
// 防止越界
if (newWidth < 0 || newHeight < 0) {
newWidth = pImg->getwidth();
newHeight = pImg->getheight();
}
// 当参数只有一个时按比例自动缩放
if (newHeight == 0) {
// 此处需要注意先*再/。不然当目标图片小于原图时会出错
newHeight = newWidth * pImg->getheight() / pImg->getwidth();
}
// 获取需要进行缩放的图片
IMAGE newImg(newWidth, newHeight);
// 分别对原图像和目标图像获取指针
DWORD* oldDr = GetImageBuffer(pImg);
DWORD* newDr = GetImageBuffer(&newImg);
// 赋值 使用双线性插值算法
for (int i = 0; i < newHeight - 1; i++) {
for (int j = 0; j < newWidth - 1; j++) {
int t = i * newWidth + j;
int xt = j * pImg->getwidth() / newWidth;
int yt = i * pImg->getheight() / newHeight;
byte r = GetRValue(oldDr[xt + yt * pImg->getwidth()]);
byte g = GetGValue(oldDr[xt + yt * pImg->getwidth()]);
byte b = GetBValue(oldDr[xt + yt * pImg->getwidth()]);
newDr[i * newWidth + j] = RGB(r, g, b);
/* COLORREF c = RGB(r, g, b);
float h, s, l;
RGBtoHSL(c, &h, &s, &l);
if (abs(h - 0.16) < 0.1)
l += 0.3;
if (h < 0.1 || h > 0.9)
l /= 2;
if (l > 1)
l = 1;
newDr[i * newWidth + j] = HSLtoRGB(h, s, l);*/
//if (r > 255) r = 255;
//if (g > 255) g = 255;
//if (g > 255) g = 255;
}
}
return newImg;
}
// 从命令中分离参数
// cmd 原命令
// chCmdsArray_out 输出参数列表
// nArgsNum_out 输出参数数量
void GetArguments(const char* cmd, char*** chCmdsArray_out, int* nArgsNum_out)
{
// 参数数量
int nArgsNum = 1;
for (int i = 0; i < (int)strlen(cmd); i++)
if (cmd[i] == ' ')
nArgsNum++;
int nMaxCmdSize = (int)strlen(cmd) + 1;
char** chCmdsArray = new char* [nArgsNum];
for (int i = 0; i < nArgsNum; i++)
{
chCmdsArray[i] = new char[nMaxCmdSize];
memset(chCmdsArray[i], 0, nMaxCmdSize);
}
// 分离每个参数
for (int nIndex = 0, nMainIndex = 0, nArgNum = 0; nIndex < (int)strlen(cmd); nIndex++, nMainIndex++)
{
if (cmd[nMainIndex] == ' ')
{
nIndex = -1;
nArgNum++;
continue;
}
chCmdsArray[nArgNum][nIndex] = cmd[nMainIndex];
}
*chCmdsArray_out = chCmdsArray;
*nArgsNum_out = nArgsNum;
}
// 帮助菜单
void HelpMenu()
{
system("start ./res/help/help.html");
printf("已打开帮助文档,请稍后页面打开。\n");
}
// 从字符串转换成 ID 号
// str 表示原字符串
// type_out 表示 ID 类型,为 0 表示物品,为 1 表示朝向
// id_out 表示 ID
// 返回输入的字符串是否合法
bool GetIdFromString(const char* str, int* type_out, int* id_out)
{
const int OBJECT = 0;
const int DIRECTION = 1;
// object
if (strcmp(str, "null") == 0)
{
*type_out = OBJECT;
*id_out = RS_NULL;
}
else if (strcmp(str, "powder") == 0)
{
*type_out = OBJECT;
*id_out = RS_POWDER;
}
else if (strcmp(str, "rod") == 0)
{
*type_out = OBJECT;
*id_out = RS_ROD;
}
else if (strcmp(str, "button") == 0)
{
*type_out = OBJECT;
*id_out = RS_BUTTON;
}
else if (strcmp(str, "torche") == 0)
{
*type_out = OBJECT;
*id_out = RS_TORCHE;
}
else if (strcmp(str, "light") == 0)
{
*type_out = OBJECT;
*id_out = RS_LIGHT;
}
else if (strcmp(str, "relay") == 0)
{
*type_out = OBJECT;
*id_out = RS_RELAY;
}
else if (strcmp(str, "cross") == 0)
{
*type_out = OBJECT;
*id_out = RS_CROSS;
}
// direction
else if (strcmp(str, "up") == 0)
{
*type_out = DIRECTION;
*id_out = RS_TO_UP;
}
else if (strcmp(str, "down") == 0)
{
*type_out = DIRECTION;
*id_out = RS_TO_DOWN;
}
else if (strcmp(str, "left") == 0)
{
*type_out = DIRECTION;
*id_out = RS_TO_LEFT;
}
else if (strcmp(str, "right") == 0)
{
*type_out = DIRECTION;
*id_out = RS_TO_RIGHT;
}
// 错误的输入
else
{
return false;
}
return true;
}
// 检验坐标合法性(是否超出地图)
bool PointIsInMap(RsMap* map, int x, int y)
{
return x >= 0 && x < map->w&& y >= 0 && y < map->h;
}
// 处理图像
void Render(
RsMap* map,
POINT* pChange, // 地图变更点
int nChangeCount, // 变更数量
bool redraw, // 是否重绘(依据变更点重绘)
bool resize, // 地图尺寸是否更新
bool zoom_changed,
int offset_x,
int offset_y,
double zoom,
bool bShowXY,
bool bShowRuler
)
{
static IMAGE imgMap, imgRulerX, imgRulerY;
static IMAGE imgMap_zoomed, imgRulerX_zoomed, imgRulerY_zoomed;
bool draw = redraw || resize;
if (draw)
GetRsMapImage(&imgMap, &imgRulerX, &imgRulerY, map, pChange, nChangeCount, redraw, resize, bShowXY, bShowRuler);
if (draw || zoom_changed)
{
// 缩放图像
imgMap_zoomed = imgMap;
imgRulerX_zoomed = imgRulerX;
imgRulerY_zoomed = imgRulerY;
IMAGE* p[3] = { &imgMap_zoomed,&imgRulerX_zoomed,&imgRulerY_zoomed };
for (int i = 0; i < 3; i++)
*p[i] = zoomImage(
p[i],
(int)(p[i]->getwidth() * zoom),
(int)(p[i]->getheight() * zoom)
);
}
// 输出
BEGIN_TASK_WND(hGraphicsWnd);
{
cleardevice();
// 输出图像
// 存在标尺时,需要将图像偏移加上标尺的大小
// 干脆任何时候都加上吧,这样计算鼠标坐标的时候不需要分情况
int render_offset_x = nMapOutX + offset_x + (int)(nRulerWidth * zoom);
int render_offset_y = nMapOutY + offset_y + (int)(nRulerHeight * zoom);
putimage(render_offset_x, render_offset_y, &imgMap_zoomed);
// 另一种渲染方式
if (false)
{
int put_offset_x = (render_offset_x - offset_x);
int put_offset_y = (render_offset_y - offset_y);
int ww = getwidth();
int hh = getheight();
int reserve_w = ww - put_offset_x;
int reserve_h = hh - put_offset_y;
IMAGE* pOld = GetWorkingImage();
IMAGE ii;
SetWorkingImage(&imgMap_zoomed);
getimage(&ii, -offset_x, -offset_y, reserve_w, reserve_h);
SetWorkingImage(pOld);
putimage(put_offset_x, put_offset_y, &ii);
}
// 复制绘制好的标尺
if (bShowXY && bShowRuler)
{
putimage(nMapOutX + offset_x + (int)(nRulerWidth * zoom), nMapOutY, &imgRulerX_zoomed);
putimage(nMapOutX, nMapOutY + offset_y + (int)(nRulerHeight * zoom), &imgRulerY_zoomed);
}
// 绘制顶栏
int w = getwidth();
setfillcolor(RGB(50, 50, 50));
solidrectangle(0, 0, w, nMapOutY);
line(0, nMapOutY, w, nMapOutY);
WCHAR strName[] = L"Minecraft Redstone Simulator";
outtextxy((w - textwidth(strName)) / 2, 3, strName);
// 按钮绘制
setfillcolor(BLUE);
RECT pBtns[3] = { rctSaveBtn,rctResizeBtn,rctHelpBtn };
WCHAR pStrs[3][12] = { L"SAVE",L"RESIZE",L"HELP" };
for (int i = 0; i < 3; i++)
{
fillrectangle_RECT(pBtns[i]);
outtextxy(pBtns[i].left + 4, pBtns[i].top + 3, pStrs[i]);
}
}
END_TASK();
FLUSH_DRAW();
}
// 点击一个按钮(阻塞)
void ClickButton(RsMap* map, int x, int y, int offset_x, int offset_y, double zoom, bool bShowXY, bool bShowRuler)
{
int delay = 1000;
if (map->map[y][x].nType == RS_BUTTON)
{
// 模拟按下再弹起
for (int i = 0; i <= 1; i++)
{
RsMap old;
POINT* pChange = new POINT[map->w * map->h];
int nChangeCount = 0;
CopyRsMap(&old, map);
map->map[y][x].bPowered = !i;
RunRsMap(map);
CmpRsMapForRedraw(&old, map, pChange, &nChangeCount);
Render(map, pChange, nChangeCount, true, false, false, offset_x, offset_y, zoom, bShowXY, bShowRuler); // 手动重绘
delete[] pChange;
pChange = nullptr;
if (!i)
{
Sleep(delay);
}
}
}
}
// 判读一字符串是否完全为数字
bool isAllNum(const char* str)
{
for (int i = 0; i < (int)strlen(str); i++)
if (!isNum(str[i]))
return false;
return true;
}
// 处理用户命令输入(阻塞)
void ProcessCommand(RsMap* map, int* offset_x, int* offset_y, double* zoom, bool* p_bShowXY, bool* p_bShowRuler)
{
// 地图移动的单位大小(像素)
const int offset_unit_size = 10;
// 命令最长长度
int nMaxInputdSize = 1024;
char* chCmd = new char[nMaxInputdSize];
memset(chCmd, 0, nMaxInputdSize);
gets_s(chCmd, nMaxInputdSize);
// 参数数量
static int nArgsNum = 0;
static char** chCmdsArray = nullptr;
// 清理上一次的内存
if (chCmdsArray != nullptr)
{
for (int i = 0; i < nArgsNum; i++)
delete[] chCmdsArray[i];
delete[] chCmdsArray;
}
// 解析参数
GetArguments(chCmd, &chCmdsArray, &nArgsNum);
delete[] chCmd;
// 设置是否开启显示坐标
if (nArgsNum == 1 && strcmp(chCmdsArray[0], "xy") == 0)
{
*p_bShowXY = !(*p_bShowXY);
}
// 切换坐标显示模式
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "xy_mode") == 0)
{
*p_bShowRuler = !(*p_bShowRuler);
}
// 帮助
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "help") == 0)
{
HelpMenu();
return;
}
// 清屏
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "cls") == 0)
{
system("cls");
return;
}
// 直接输入坐标表示在某位置放置红石粉,如果那个位置已有物品,则清除那个物品
else if (nArgsNum == 2 && isAllNum(chCmdsArray[0]) && isAllNum(chCmdsArray[1]))
{
int x = atoi(chCmdsArray[0]);
int y = atoi(chCmdsArray[1]);
if (!PointIsInMap(map, x, y))
{
printf("坐标越界。\n");
return;
}
if (map->map[y][x].nType != RS_NULL)
{
PutObjectToRsMap(map, x, y, RS_NULL);
}
else
{
PutObjectToRsMap(map, x, y, RS_POWDER);
}
}
// 坐标 坐标 物品 (朝向)
// 两种情况:
// 在某位置放置某物品,如果那个位置已有物品,则覆盖那个物品。红石中继器有朝向设定,如果留空则表示向上
// 设置某位置的物品的朝向(针对红石中继器)
else if ((nArgsNum == 3 || nArgsNum == 4) && isAllNum(chCmdsArray[0]) && isAllNum(chCmdsArray[1]) && !isAllNum(chCmdsArray[2]))
{
int type;
int id;
if (!GetIdFromString(chCmdsArray[2], &type, &id))
{
printf("方块名称错误。\n");
return;
}
int x = atoi(chCmdsArray[0]);
int y = atoi(chCmdsArray[1]);
if (!PointIsInMap(map, x, y))
{
printf("坐标越界。\n");
return;
}
if (type == 0)
{
PutObjectToRsMap(map, x, y, id);
// 如有四个参数,说明还有一个朝向
if (nArgsNum == 4)
{
int type2;
int direction;
if (!GetIdFromString(chCmdsArray[3], &type2, &direction))
{
printf("命令执行不完全后因语法错误而终止了。错误原因:第四个参数未能指定方块朝向。\n");
return;
}
if (type2 == 1)
{
map->map[y][x].nTowards = direction;
}
else
{
printf("命令执行不完全后因语法错误而终止了。错误原因:第四个参数未能指定方块朝向。\n");
return;
}
}
}
else if (type == 1)
{
if (map->map[y][x].nType == RS_NULL)
{
printf("该坐标并无物品方块。\n");
return;
}
else
{
map->map[y][x].nTowards = id;
}
}
}
// . 坐标x 坐标y
else if (nArgsNum == 3 && strcmp(chCmdsArray[0], ".") == 0 && isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]))
{
int x = atoi(chCmdsArray[1]);
int y = atoi(chCmdsArray[2]);
if (!PointIsInMap(map, x, y))
{
printf("坐标越界。\n");
return;
}
switch (map->map[y][x].nType)
{
case RS_NULL:
printf("该坐标并无物品方块。\n");
return;
break;
case RS_ROD:
map->map[y][x].bPowered = !map->map[y][x].bPowered;
break;
case RS_BUTTON:
ClickButton(map, x, y, *offset_x, *offset_y, *zoom, *p_bShowXY, *p_bShowRuler);
break;
default:
printf("该物品不支持点击操作。\n");
return;
break;
}
}
// resize 宽 高
else if (nArgsNum == 3 && strcmp(chCmdsArray[0], "resize") == 0 && isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]))
{
int w = atoi(chCmdsArray[1]);
int h = atoi(chCmdsArray[2]);
if (w <= 0 || h <= 0)
{
printf("地图大小不得小于等于0\n");
return;
}
ResizeRsMap(map, w, h);
}
// save 保存项目
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "save") == 0)
{
if (SaveProject(*map, SelectFile(true)))
{
printf("保存成功。\n");
return;
}
else
{
printf("保存失败。\n");
return;
}
}
// 将地图画面的xy偏移都回到0
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "reset_map_offset") == 0)
{
*offset_x = 0;
*offset_y = 0;
}
// 移动地图
else if (nArgsNum == 2 && strcmp(chCmdsArray[0], "up") == 0 && isAllNum(chCmdsArray[1]))
{
*offset_y -= atoi(chCmdsArray[1]) * offset_unit_size;
}
// 移动地图
else if (nArgsNum == 2 && strcmp(chCmdsArray[0], "down") == 0 && isAllNum(chCmdsArray[1]))
{
*offset_y += atoi(chCmdsArray[1]) * offset_unit_size;
}
// 移动地图
else if (nArgsNum == 2 && strcmp(chCmdsArray[0], "left") == 0 && isAllNum(chCmdsArray[1]))
{
*offset_x -= atoi(chCmdsArray[1]) * offset_unit_size;
}
// 移动地图
else if (nArgsNum == 2 && strcmp(chCmdsArray[0], "right") == 0 && isAllNum(chCmdsArray[1]))
{
*offset_x += atoi(chCmdsArray[1]) * offset_unit_size;
}
// 缩放地图
else if (nArgsNum == 2 && strcmp(chCmdsArray[0], "zoom") == 0 && isAllNum(chCmdsArray[1]))
{
*zoom = atoi(chCmdsArray[1]) / 100.0;
}
// 缩放地图
else if (nArgsNum == 2 && strcmp(chCmdsArray[0], "zoom+") == 0 && isAllNum(chCmdsArray[1]))
{
*zoom += atoi(chCmdsArray[1]) / 100.0;
}
// 缩放地图
else if (nArgsNum == 2 && strcmp(chCmdsArray[0], "zoom-") == 0 && isAllNum(chCmdsArray[1]))
{
*zoom -= atoi(chCmdsArray[1]) / 100.0;
}
// 得到地图偏移量
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "get_map_offset") == 0)
{
printf("x轴偏移量:%d个单位(即%d像素)\ny轴偏移量:%d个单位(即%d像素)\n",
*offset_x / offset_unit_size, *offset_x, *offset_y / offset_unit_size, *offset_y);
}
// 得到地图缩放量
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "get_map_zoom") == 0)
{
printf("缩放量:%d%%\n", (int)(*zoom * 100));
}
// import l l
else if (nArgsNum == 3 && strcmp(chCmdsArray[0], "import") == 0 && isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]))
{
int x = atoi(chCmdsArray[1]);
int y = atoi(chCmdsArray[2]);
/*if (!PointIsInMap(map, l, l))
{
printf("坐标越界。\n");
return;
}*/
printf("请选择要导入的项目。\n");
const WCHAR* map_file = SelectFile();
RsMap import_map = OpenProject(map_file);
char warning[512] = { 0 };
sprintf_s(warning, 512,
"你正在导入一个项目。以下是导入项目的信息:\n"
"导入子项目到主项目的左上角位置:( %d, %d )。\n"
"子项目地图大小:宽 %d 格,高 %d 格。\n"
"子项目将覆盖主项目的以下范围:( %d, %d ) 到 ( %d, %d ),如果超出主项目的地图大小则自动将超出的部分裁剪。\n"
"\n\n"
"是否继续导入此项目?点击【确定】来确定导入此项目,否则点击【取消】终止此操作。如果你担心造成不可逆的后果,可以先保存当前项目,再进行导入。\n"
, x, y, import_map.w, import_map.h, x, y, x + import_map.w, y + import_map.h);
wchar_t wstr[512] = { 0 };
MultiByteToWideChar(CP_ACP, 0, warning, (int)strlen(warning), wstr, (int)strlen(warning));
if (MessageBox(GetConsoleWindow(), wstr, L"确定?", MB_OKCANCEL) == IDOK)
{
ImportProject(map, import_map, x, y);
}
DeleteRsMap(&import_map);
printf("导入完毕。\n");
}
// line 坐标x1 坐标y1 坐标x2 坐标y2 (物品) : 画红石直线(不能弯曲),如果在末尾加上物品名表示以某方块填充直线。
else if (
(nArgsNum == 5 || nArgsNum == 6) && strcmp(chCmdsArray[0], "line") == 0 &&
isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]) && isAllNum(chCmdsArray[3]) && isAllNum(chCmdsArray[4]) ||
((nArgsNum == 4 || nArgsNum == 5) && isAllNum(chCmdsArray[0]) && isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]) && isAllNum(chCmdsArray[3]))
)
{
int start = 1;
if (nArgsNum == 4 || (nArgsNum == 5 && !isAllNum(chCmdsArray[4])))
{
start = 0;
}
int x1 = atoi(chCmdsArray[start]);
int y1 = atoi(chCmdsArray[start + 1]);
int x2 = atoi(chCmdsArray[start + 2]);
int y2 = atoi(chCmdsArray[start + 3]);
int id = RS_POWDER;
if (!PointIsInMap(map, x1, y1) || !PointIsInMap(map, x2, y2))
{
printf("坐标越界。\n");
return;
}
if (nArgsNum == start + 5)
{
int type;
if (!GetIdFromString(chCmdsArray[start + 4], &type, &id) || type != 0)
{
printf("最后一个参数应该是物品id。\n");
return;
}
}
if (!LineRsMap(map, x1, y1, x2, y2, id))
{
printf("(%d, %d) 到 (%d, %d) 有弯曲,无法绘制直线。\n", x1, y1, x2, y2);
return;
}
}
// clear 坐标x1 坐标y1 坐标x2 坐标y2 : 以空气方块覆盖 (x1,y1) 到 (x2,y2) 的所有方块。
else if (nArgsNum == 5 && strcmp(chCmdsArray[0], "clear") == 0 &&
isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]) && isAllNum(chCmdsArray[3]) && isAllNum(chCmdsArray[4]))
{
int x1 = atoi(chCmdsArray[1]);
int y1 = atoi(chCmdsArray[2]);
int x2 = atoi(chCmdsArray[3]);
int y2 = atoi(chCmdsArray[4]);
if (!PointIsInMap(map, x1, y1) || !PointIsInMap(map, x2, y2))
{
printf("坐标越界。\n");
return;
}
ClearRsMap(map, x1, y1, x2, y2);
}
// exit 退出程序
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "exit") == 0)
{
if (MessageBox(GetConsoleWindow(), L"确定要退出程序吗?", L"退出程序", MB_OKCANCEL) == IDOK)
{
exit(0);
}
}
// cmd_window_top 使cmd顶置
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "cmd_window_top") == 0)
{
SetWindowPos(GetConsoleWindow(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
// cmd_window_no_top 使cmd不顶置
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "cmd_window_no_top") == 0)
{
SetWindowPos(GetConsoleWindow(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
// map_window_top 使绘图窗口顶置
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "map_window_top") == 0)
{
SetWindowPos(GetHWnd(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
// map_window_no_top 使绘图窗口不顶置
else if (nArgsNum == 1 && strcmp(chCmdsArray[0], "map_window_no_top") == 0)
{
SetWindowPos(GetHWnd(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
// resize_cmd_window 设置cmd窗口大小
else if (nArgsNum == 3 && strcmp(chCmdsArray[0], "resize_cmd_window") == 0 && isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]))
{
int w = atoi(chCmdsArray[1]);
int h = atoi(chCmdsArray[2]);
char cmd[64] = { 0 };
sprintf_s(cmd, 64, "mode con cols=%d lines=%d", w, h);
system(cmd);
}
// resize_map_window 设置绘图窗口大小
else if (nArgsNum == 3 && strcmp(chCmdsArray[0], "resize_map_window") == 0 && isAllNum(chCmdsArray[1]) && isAllNum(chCmdsArray[2]))
{
int w = atoi(chCmdsArray[1]);
int h = atoi(chCmdsArray[2]);
Resize(nullptr, w, h);
}
else
{
printf("未知命令。\n");
return;
}
printf("执行完毕。\n");
}
// 阻塞处理 CMD 消息
void CommandMessageLoop(RsMap* map, int* offset_x, int* offset_y, double* zoom, bool* p_bShowXY, bool* p_bShowRuler)
{
while (true)
{
ProcessCommand(map, offset_x, offset_y, zoom, p_bShowXY, p_bShowRuler);
}
}
// 显示帮助窗口
void HelpBox()
{
int w = 820, h = 420;
HWND hHelpWnd = HiEasyX::initgraph_win32(w, h, EW_NORMAL, L"帮助", nullptr, hGraphicsWnd);
DisableResizing(true);
RECT rctOkBtn = { w / 2 - 25,h - 35,w / 2 + 25,h - 8 };
BEGIN_TASK();
setbkcolor(CLASSICGRAY);
cleardevice();
setbkmode(TRANSPARENT);
settextcolor(BLUE);
settextstyle(46, 0, L"宋体");
outtextxy(30, 20, L"MCRedStoneSimulator 使用说明");
settextcolor(BLACK);
settextstyle(22, 0, L"微软雅黑");
outtextxy(30, 80, L"本程序是 huidong <mailhuid@163.com> 制作的红石(电路)模拟软件");
outtextxy(30, 110, L"您可以在 ToolBar 中选择工具或元件,并在显示有电路图的窗口中使用它们来编辑电路图。");
outtextxy(30, 140, L"在程序的命令行窗口中您可以键入指令对电路项目进行操作,输入“help”指令可以查看指令表。");
outtextxy(30, 200, L"左键单击:操作元件 Ctrl + 左键:清除元件 在 ToolBar 中:");
outtextxy(30, 230, L"左键拖动:移动地图 Ctrl + 右键:旋转元件 选择到鼠标时,右键单击可以操作元件");
outtextxy(30, 260, L"鼠标滚轮:缩放地图");
outtextxy(410, 260, L"选择到元件时,右键按下可以放置元件");
outtextxy(30, 310, L"作者博客:http://huidong.xyz EasyX 主站:https://easyx.cn");
outtextxy(30, 340, L"Github 项目地址:https://github.com/zouhuidong/MinecraftRedstoneSimulator");
setfillcolor(BLUE);
fillrectangle_RECT(rctOkBtn);
settextcolor(WHITE);
outtextxy(rctOkBtn.left + 14, rctOkBtn.top + 3, L"OK");
END_TASK();
FLUSH_DRAW();
// 消息响应
while (HiEasyX::isAliveWindow(hHelpWnd))
{
bool end = false;
BEGIN_TASK();
while (MouseHit())
{
ExMessage msg = getmessage(EM_MOUSE);
if (msg.message == WM_LBUTTONUP)
{
if (isInRect(msg.x, msg.y, rctOkBtn))
{
end = true;
break;
}
}
}
END_TASK();
if (end)
{
HiEasyX::closegraph_win32(hHelpWnd);
break;
}
Sleep(10);
}
}
WCHAR strMapSize[2][12]; // 在输入框中存储地图大小数据
#define IDC_EDIT1 100
#define IDC_EDIT2 101
#define IDC_BUTTON 102
bool ResizeBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, HINSTANCE hInstance)
{
static HWND hEdit[2];
static HWND hBtn;
switch (msg)
{
case WM_CREATE:
hEdit[0] = CreateWindow(L"edit", strMapSize[0],
WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
60, 30, 40, 20,
hWnd, (HMENU)IDC_EDIT1, hInstance, 0);
hEdit[1] = CreateWindow(L"edit", strMapSize[1],
WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
60, 55, 40, 20,
hWnd, (HMENU)IDC_EDIT2, hInstance, 0);
hBtn = CreateWindow(L"button", L"Done",
WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
160, 40, 60, 30,
hWnd, (HMENU)IDC_BUTTON, hInstance, 0);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_BUTTON:
{
GetWindowText(hEdit[0], strMapSize[0], 12);
GetWindowText(hEdit[1], strMapSize[1], 12);
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
}
break;
case WM_PAINT:
BEGIN_TASK();
setbkcolor(CLASSICGRAY);
settextcolor(BLACK);
cleardevice();
outtextxy(30, 30, L"宽:");
outtextxy(30, 55, L"高:");
settextcolor(GRAY);
settextstyle(14, 0, L"宋体");
//outtextxy(30, 80, L"地图大小超出 50x50 或导致卡顿");
END_TASK();
break;
}
return true;
}
// 显示设置地图大小窗口
// 返回是否成功设置了一个新的大小
bool ResizeBox(RsMap* pMap, HWND hParent)
{