summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2MoverModuleTable.cpp
blob: 80741e1e0c4fd4f69ad63651f965187d8c233e21 (plain)
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
#include "GlobalFunc.h"
#include "T2.h"
#include "T2BitImage.h"
#include "T2DlgItemVScr.h"
#include "T2ElevatorModule.h"
#include "T2FloorNumberTable.h"
#include "T2Mover.h"
#include "T2MoverModuleList.h"
#include "T2MoverModuleTable.h"

/*virtual*/ BOOL T2MoverModuleTable::Create(const char* inWindowName, DWORD inStyle, const RECT& inRect, CWnd* inParentWnd, UINT inID) {
    return T2DlgItem::Create(inWindowName, inStyle, inRect, inParentWnd, inID);
}

T2MoverModuleTable::T2MoverModuleTable(T2TowerDoc* inDoc, T2ImageObj* inImageObj, CPalette* inPalette)
    : T2DlgItemImageTable(inDoc, inImageObj, inPalette)
    , mMover(NULL)
    , mMoverModuleList(NULL)
{
    mImage = NULL;
    SetRect(&mRect, 0, 0, 0, 0);
    T2_APP->app_vfA4(true);
}

/*virtual*/ T2MoverModuleTable::~T2MoverModuleTable() {
    LArrayIterator iterator(*mMoverModuleList);
    T2MoverModule *theModule;

    while (iterator.Next(&theModule))
        theModule->RemoveListener(this);

    if (mImage)
        delete mImage;
}

/*virtual*/ BOOL T2MoverModuleTable::OnT2DlgItemCreate(CREATESTRUCT* inCreateStruct) {
    T2DlgItem::OnT2DlgItemCreate(inCreateStruct);

    GetClientRect(&mRect);

    RECT rect;
    rect.left = mRect.right - 16;
    rect.top = mRect.top;
    rect.right = mRect.right;
    rect.bottom = mRect.bottom;

    T2DlgItemVScr *theScr = new T2DlgItemVScr(mTowerDoc, mImageObj, mPalette);
    theScr->Create("", WS_VISIBLE | WS_CHILD, rect, this, 100);

    rect.left = mRect.left;
    rect.top = mRect.top;
    rect.right = mRect.left + 24;
    rect.bottom = mRect.bottom;

    T2FloorNumberTable *theTable = new T2FloorNumberTable(mTowerDoc, mImageObj, mPalette);
    theTable->Create("", WS_VISIBLE | WS_CHILD, rect, this, 101);

    mImage = new T2BitImage(mRect);
    mImage->Clear(0);
    return false;
}

/*virtual*/ void T2MoverModuleTable::SpendTime() {
}

void T2MoverModuleTable::SetMover(T2Mover* inMover) {
    mMover = inMover;
    if (!mMover)
        return;

    mMoverModuleList = mMover->GetModuleList();
    if (mMoverModuleList) {
        LArrayIterator iterator(*mMoverModuleList);
        T2MoverModule *theModule;

        while (iterator.Next(&theModule))
            theModule->AddListener(this);

        InsertCols(mMoverModuleList->GetItemCount(), 0, NULL);
        InsertRows(mMover->GetLength(), 0, NULL);
    }
}

T2ElevatorModule* T2MoverModuleTable::GetModule(int inIndex) {
    T2ElevatorModule *theModule = NULL;
    LArrayIterator iterator(*mMoverModuleList);
    int index = 0;

    while (iterator.Next(&theModule)) {
        if (theModule->IsUsed()) {
            index++;
            if (index >= inIndex)
                return theModule;
        }
    }

    return NULL;
}

int T2MoverModuleTable::GetCellContent(const TableCellT& inCell) {
    int result = 0;

    if (mMoverModuleList) {
        T2ElevatorModule *theModule = GetModule(inCell.col);
        if (theModule) {
            int floor = mRows - inCell.row;

            if (result == 0 && theModule->GetPosition() == floor)
                result = 3010 + theModule->GetPatIndex();

            if (result == 0) {
                if (theModule->GetDirection() == kElevatorDirection1) {
                    if (theModule->GetTopTurn() == floor)
                        result = 3007;
                } else if (theModule->GetDirection() == kElevatorDirection2) {
                    if (theModule->GetBottomTurn() == floor)
                        result = 3006;
                }
            }

            if (result == 0) {
                if (theModule->GetDirection() == kElevatorDirection1) {
                    if (theModule->IsOnStopPos(floor, ERequestUpDown_0))
                        result = 3004;
                    else if (theModule->IsOnStopPos(floor, ERequestUpDown_1))
                        result = 3005;
                } else if (theModule->GetDirection() == kElevatorDirection2) {
                    if (theModule->IsOnStopPos(floor, ERequestUpDown_1))
                        result = 3005;
                    else if (theModule->IsOnStopPos(floor, ERequestUpDown_0))
                        result = 3004;
                }
            }

            if (result == 0) {
                if (theModule->GetDirection() == kElevatorDirection1) {
                    if (theModule->IsOffStopPos(floor, ERequestUpDown_0))
                        result = 3002;
                    else if (theModule->IsOffStopPos(floor, ERequestUpDown_1))
                        result = 3003;
                } else if (theModule->GetDirection() == kElevatorDirection2) {
                    if (theModule->IsOffStopPos(floor, ERequestUpDown_1))
                        result = 3003;
                    else if (theModule->IsOffStopPos(floor, ERequestUpDown_0))
                        result = 3002;
                }
            }

            if (result == 0) {
                if (theModule->GetHomePosition() == floor)
                    result = 3001;
            }

            if (result == 0)
                result = 3000;
        }
    }

    return result;
}

int T2MoverModuleTable::GetVScrValue() {
    T2DlgItemVScr *theScr = (T2DlgItemVScr *) GetDlgItem(100);
    return theScr->GetValue();
}

/*virtual*/ void T2MoverModuleTable::DrawCellSelf(CDC* pDC, const TableCellT& inCell, BOOL inSelected) {
    RECT cellRect;
    if (FetchLocalCellFrame(inCell, cellRect)) {
        int content = GetCellContent(inCell);
        if (content != 0) {
            int y = GetVScrValue() * mRowHeight;
            OffsetRect(&cellRect, 24, -y);
            BOOL doesIntersect = IntersectRect(&cellRect, &cellRect, &mRect);

            T2BitImage *theImage = new T2BitImage(AfxGetResourceHandle(), content, true);
            RECT theSrcRect;
            theSrcRect.left = 0;
            theSrcRect.top = 0;
            theSrcRect.right = theImage->mBitmap.header.biWidth;
            theSrcRect.bottom = theImage->mBitmap.header.biHeight;

            if (doesIntersect) {
                theImage->CopyImage(*mImage, theSrcRect, cellRect, SRCCOPY);
                if (inSelected)
                    InvalidateRect(NULL);
            }

            // memory leak???
        }
    }
}

/*virtual*/ void T2MoverModuleTable::ListenToMessage(UINT inMessage, void *ioData) {
    switch (inMessage) {
        case 1000:
        case 1001:
            HomeNotification *info = (HomeNotification *) ioData;
            T2MoverModule *module = NULL;
            int i = 0;
            LArrayIterator theIter(*mMoverModuleList);

            while (theIter.Next(&module)) {
                if (module->IsUsed()) {
                    i++;
                    if (module == info->theModule) {
                        int position = -1;
                        switch (inMessage) {
                            case 1000:
                                break;
                            case 1001:
                                position = module->GetPosition();
                                break;
                        }

                        RefreshMoverCell(position, i);
                        break;
                    }
                }
            }
            break;
    }
}

void T2MoverModuleTable::RefreshMoverCell(int inFloor, unsigned int inIndex) {
    if (mMover) {
        if (inFloor < 0) {
            TableCellT topCell, bottomCell;
            RECT topFrame, bottomFrame;
            topCell.row = 1;
            topCell.col = inIndex;
            bottomCell.row = mRows;
            bottomCell.col = inIndex;
            if (FetchLocalCellFrame(topCell, topFrame) && FetchLocalCellFrame(bottomCell, bottomFrame)) {
                RECT rect;
                SetRect(&rect, topFrame.left, topFrame.top, bottomFrame.right, bottomFrame.bottom);

                BOOL isVisible = IntersectRect(&rect, &rect, &mRect);
                if (isVisible)
                    InvalidateRect(&rect);
            }
        } else {
            TableCellT theCell;
            RECT frame;
            theCell.row = mRows - inFloor;
            theCell.col = inIndex;
            if (FetchLocalCellFrame(theCell, frame)) {
                BOOL isVisible = IntersectRect(&frame, &frame, &mRect);
                if (isVisible)
                    InvalidateRect(&frame);
            }
        }
    }
}

void T2MoverModuleTable::DrawMoverCellByIndex(unsigned int inIndex) {
    TableCellT cell;
    cell.col = inIndex;

    for (unsigned int i = 1; i <= mRows; i++) {
        cell.row = i;
        DrawCellSelf(NULL, cell, false);
    }
}

/*virtual*/ void T2MoverModuleTable::ClickCell(const TableCellT& inCell, const POINT& inPt) {
    T2TowerDoc *theDoc = GetCurrentT2TowerDoc();
    if (mMoverModuleList && theDoc) {
        int moverIndex = inCell.col;
        T2ElevatorModule *theModule = GetModule(moverIndex);

        if (theModule) {
            HomeNotification info;
            info.theModule = theModule;
            info.whichMover = moverIndex;
            info.whichFloor = mRows - inCell.row;
            info.theTable = this;
            BroadcastMessage('home', &info);
        }
    }
}

/*virtual*/ BOOL T2MoverModuleTable::OnCommand(WPARAM inWParam, LPARAM inLParam) {
    BOOL result = false;

    if (LOWORD(inWParam) == 100 || LOWORD(inWParam) == 101) {
        InvalidateRect(NULL);
        result = true;
    }

    return result;
}

/*virtual*/ BOOL T2MoverModuleTable::OnT2DlgItemEraseBkgnd(CDC* pDC) {
    CRect theRect;
    GetClientRect(theRect);
    theRect.right -= 15;
    theRect.left += 23;

    int saved = pDC->SaveDC();
    pDC->SelectPalette(mPalette, false);
    pDC->RealizePalette();

    CPen thePen;
    CBrush theBrush, theNullBrush;

    if (!mMoverModuleList) {
        thePen.CreatePen(PS_SOLID, 0, PALETTEINDEX(255));
        theBrush.CreateSolidBrush(RGB(255, 255, 255));
        pDC->SelectObject(theBrush);
        pDC->SelectObject(thePen);
        pDC->Rectangle(theRect);
    } else {
        thePen.CreatePen(PS_SOLID, 0, PALETTEINDEX(255));
        theNullBrush.CreateStockObject(NULL_BRUSH);
        pDC->SelectObject(theNullBrush);
        pDC->SelectObject(thePen);
        pDC->Rectangle(theRect);

        theRect.top++;
        theRect.left++;
        theRect.right--;
        theRect.bottom--;

        unsigned int height, width;
        GetTableSize(height, width);

        for (unsigned int x = 1; x <= width; x++) {
            for (unsigned int y = 1; y <= height; y++) {
                TableCellT cell;
                cell.row = y;
                cell.col = x;
                DrawCellSelf(pDC, cell, false);
            }
        }

        pDC->IntersectClipRect(theRect);
        StretchDIBits(pDC->m_hDC, theRect.left, theRect.bottom, theRect.right - theRect.left, theRect.bottom - theRect.top, 0, 0, mImage->mBitmap.header.biWidth, mImage->mBitmap.header.biHeight, mImage->mData, (BITMAPINFO *) &mImage->mBitmap, DIB_PAL_COLORS, SRCCOPY);

        theRect.top--;
        theRect.left--;
        theRect.right++;
        theRect.bottom++;
        InvalidateRect(&theRect);
    }

    pDC->RestoreDC(saved);
    return true;
}

/*virtual*/ void T2MoverModuleTable::OnT2DlgItemLButtonDown(UINT inFlags, CPoint inPt) {
    POINT pt = inPt;
    ClientToView(&pt, 1);

    TableCellT cell;
    FetchCellHitBy(pt, cell);
    ClickCell(cell, inPt);
}