TNB Library
TnbDrawingContainer.h
[詳解]
1#pragma once
11#include "TnbSimpleVector.h"
12#include "TnbBitmapDrawer.h"
13
14
15
16//TNB Library
17namespace TNB
18{
19
20
21
69{
70 DEFSUPER(IDrawable);
71public:
72
75 {
76 m_appendPos.x = 0;
77 m_appendPos.y = 0;
78 }
79
81 virtual ~CDrawingContainer(void)
82 {
83 m_drawers.RemoveAll();
84 }
85
91 virtual IDrawable* Clone(void) const
92 {
94 pNew->m_appendPos = m_appendPos;
95 pNew->m_drawers.RemoveAll();
96 size_t l = m_drawers.GetSize();
97 pNew->m_drawers.SetSize(l);
98 loop ( i, l )
99 {
100 pNew->m_drawers[i] = m_drawers[i];
101 }
102 return pNew;
103 }
104
113 virtual bool GetSize(SIZE& _size) const
114 {
115 RECT rc;
116 if ( GetAllRect(rc) )
117 {
118 _size.cx = rc.right;
119 _size.cy = rc.bottom;
120 return true;
121 }
122 return false;
123 }
124
134 virtual bool Resize(const SIZE& size)
135 {
136 loop ( i, m_drawers.GetSize() )
137 {
138 TParam& A = m_drawers[i];
139 SIZE sz = A.orgSize;
140 if ( sz.cx + A.pos.x > size.cx )
141 {
142 sz.cx = size.cx - A.pos.x;
143 if ( sz.cx < 0 )
144 {
145 sz.cx = 0;
146 }
147 }
148 if ( sz.cy + A.pos.y > size.cy )
149 {
150 sz.cy = size.cy - A.pos.y;
151 if ( sz.cy < 0 )
152 {
153 sz.cy = 0;
154 }
155 }
156 A.pDrawer->Resize(sz);
157 }
158 return true;
159 }
160
165 void DefaultSize(void)
166 {
167 loop ( i, m_drawers.GetSize() )
168 {
169 TParam& A = m_drawers[i];
170 A.pDrawer->Resize(A.orgSize);
171 }
172 }
173
181 CBitmapHandle CreateBitmap(COLORREF color = CLR_INVALID) const
182 {
183 return CBitmapDrawer::ToBitmap(*this, color);
184 }
185
193 virtual void Draw(HDC dc, int x = 0, int y = 0) const
194 {
195 DrawEx(dc, x, y, 0);
196 }
197
206 virtual void DrawEx(HDC dc, int x, int y, LPARAM lParam) const
207 {
208 if ( dc == NULL )
209 {
210 loop ( i, m_drawers.GetSize() )
211 {
212 const TParam& A = m_drawers.At(i);
213 A.pDrawer->DrawEx(dc, x + A.pos.x, y + A.pos.y, lParam);
214 }
215 }
216 else
217 {
218 loop ( i, m_drawers.GetSize() )
219 {
220 const TParam& A = m_drawers.At(i);
221 SIZE size;
222 if ( A.pDrawer->GetSize(size) )
223 {
224 RECT rc = { x + A.pos.x, y + A.pos.y, x + A.pos.x + size.cx, y + A.pos.y + size.cy };
225 if ( ::RectVisible(dc, &rc) )
226 {
227 A.pDrawer->DrawEx(dc, rc.left, rc.top, lParam);
228 }
229 }
230 }
231 }
232 }
233
239 size_t GetCount(void) const
240 {
241 return m_drawers.GetSize();
242 }
243
250 const IDrawable* At(INDEX index) const
251 {
252 if ( m_drawers.IsInRange(index) )
253 {
254 return m_drawers.At(index).pDrawer;
255 }
256 return NULL;
257 }
258
265 bool RemoveAt(INDEX index)
266 {
267 return m_drawers.Remove(index);
268 }
269
274 void RemoveAll(void)
275 {
276 m_drawers.RemoveAll();
277 m_appendPos.x = 0;
278 m_appendPos.y = 0;
279 }
280
288// CBitmapHandle GetAtBitmap(INDEX index) const
289// {
290// if ( m_drawers.IsInRange(index) )
291// {
292// const TParam& A = m_drawers.At(index);
293// return A.pDrawer->GetBitmap();
294// }
295// return CBitmapHandle();
296// }
297
306 bool GetAtRect(INDEX index, RECT& _rect) const
307 {
308 if ( ! m_drawers.IsInRange(index) ) { return false; }
309 const TParam& A = m_drawers.At(index);
310 SIZE size;
311 bool r = A.pDrawer->GetSize(size);
312 if ( r )
313 {
314 ::SetRect(&_rect, A.pos.x, A.pos.y, A.pos.x + size.cx, A.pos.y + size.cy);
315 }
316 else
317 {
318 ::SetRectEmpty(&_rect);
319 }
320 return r;
321 }
322
330 bool GetAllRect(RECT& _rect) const
331 {
332 bool r = true;
333 ::SetRectEmpty(&_rect);
334 loop ( i, m_drawers.GetSize() )
335 {
336 RECT rc;
337 r &= GetAtRect(i, rc);
338 ::UnionRect(&_rect, &_rect, &rc);
339 }
340 return r;
341 }
342
349 HRGN GetAllRgn(void) const
350 {
351 if ( GetCount() == 0 ) { return NULL; }
352 HRGN rgn = ::CreateRectRgn(0, 0, 0, 0);
353 loop ( i, GetCount() )
354 {
355 RECT r;
356 if ( GetAtRect(i, r) )
357 {
358 HRGN rgnTemp = ::CreateRectRgnIndirect(&r);
359 ::CombineRgn(rgn, rgn, rgnTemp, RGN_OR);
360 _DeleteObject(rgnTemp);
361 }
362 }
363 return rgn;
364 }
365
374 void DrawAt(INDEX index, HDC dc, int x = 0, int y = 0) const
375 {
376 if ( m_drawers.IsInRange(index) )
377 {
378 const TParam& A = m_drawers.At(index);
379 A.pDrawer->Draw(dc, x, y);
380 }
381 }
382
388 const POINT& GetAppendPosition(void) const
389 {
390 return m_appendPos;
391 }
392
399 void OffsetAppendPosition(int x, int y)
400 {
401 m_appendPos.x += x;
402 m_appendPos.y += y;
403 }
404
411 void SetAppendPosition(int x, int y)
412 {
413 m_appendPos.x = x;
414 m_appendPos.y = y;
415 }
416
419 {
424 STAY
425 };
426
439 bool MoveAppendPosition(INDEX index, EDirection dire = DOWN, int x = 0, int y = 0)
440 {
441 if ( m_drawers.IsInRange(index) )
442 {
443 const TParam& A = m_drawers.At(index);
444 SIZE size;
445 if ( A.pDrawer->GetSize(size) )
446 {
447 m_appendPos.x = A.pos.x + x;
448 m_appendPos.y = A.pos.y + y;
449 m_Offset(dire, size.cx, size.cy);
450 return true;
451 }
452 }
453 return false;
454 }
455
458 {
462 //
466 //
468 };
469
481 {
482 TParam p;
483 if ( pDraw != NULL && pDraw->GetSize(p.orgSize) )
484 {
485 p.pos = m_appendPos;
486 p.pDrawer = pDraw;
487 INDEX r = m_drawers.Add(p);
488 if ( r != INVALID_INDEX )
489 {
490 m_Offset(dire, p.orgSize.cx, p.orgSize.cy);
491 return r;
492 }
493 }
494 delete pDraw;
495 return INVALID_INDEX;
496 };
497
512 INDEX AddNewPointer(INDEX index, ELocation locate, IDrawable* pDraw, EDirection dire = DOWN)
513 {
514 if ( ! m_Locate(index, locate, pDraw) ) { return INVALID_INDEX; }
515 return AddNewPointer(pDraw, dire);
516 }
517
527 INDEX Add(const IDrawable& draw, EDirection dire = DOWN)
528 {
529 return AddNewPointer(draw.Clone(), dire);
530 };
531
545 INDEX Add(INDEX index, ELocation locate, const IDrawable& draw, EDirection dire = DOWN)
546 {
547 return AddNewPointer(index, locate, draw.Clone(), dire);
548 };
549
560 {
561 return AddNewPointer(new CBitmapDrawer(bmp), dire);
562 }
563
577 INDEX AddBitmap(INDEX index, ELocation locate, CBitmapHandle bmp, EDirection dire = DOWN)
578 {
579 return AddNewPointer(index, locate, new CBitmapDrawer(bmp), dire);
580 }
581
608 int AppendBitmaps(LPCTSTR lpszPath, INDEX maxIndex, EDirection dire = DOWN)
609 {
610 RemoveAll();
611 WIN32_FIND_DATA wfd;
612 SIZE nullSize = { 0, 0 };
613 CWorkMemT<TCHAR> str(MAX_PATH + 1);
614 INDEX lastValidIndex = INVALID_INDEX;
615 int foundCount = 0;
616 loop( i, maxIndex + 1 )
617 {
618 if ( ! STRLIB::PrintF(str, MAX_PATH, lpszPath, i) )
619 {
620 return -1;
621 }
622 IDrawable* pDraw = NULL;
623 HANDLE hFind = ::FindFirstFile(str, &wfd);
624 if ( hFind != INVALID_HANDLE_VALUE )
625 {
626 ::FindClose(hFind);
627 CWorkMemT<TCHAR> s(MAX_PATH + 1);
628 STRLIB::Copy(s, str);
629 INT_PTR l = STRLIB::PathIndexOf(s) + 1;
630 STRLIB::Copy(&s[l], wfd.cFileName);
631 CBitmapHandle bmp(s);
632 if ( ! bmp.IsNull() )
633 {
634 pDraw = new CBitmapDrawer(bmp);
635 lastValidIndex = i;
636 foundCount++;
637 }
638 }
639 if ( pDraw == NULL )
640 {
641 pDraw = new CNullDrawer(nullSize);
642 }
643 INDEX idx = AddNewPointer(pDraw, dire);
644 ASSERTLIB( idx == i );
645 }
646 if ( lastValidIndex != INVALID_INDEX )
647 {
648 while ( RemoveAt(lastValidIndex + 1 ));
649 }
650 else
651 {
652 RemoveAll();
653 }
654 return foundCount;
655 }
656
657private:
658
660 struct TParam
661 {
662 POINT pos;
663 SIZE orgSize;
664 IDrawable::Ptr pDrawer;
665 };
666 CSimpleVectorT<TParam> m_drawers;
667 POINT m_appendPos;
668 // OFFSET
669 void m_Offset(EDirection dire, int x, int y)
670 {
671 if ( dire == DOWN )
672 {
673 m_appendPos.y += y;
674 }
675 else if ( dire == RIGHT )
676 {
677 m_appendPos.x += x;
678 }
679 else if ( dire == RETURN )
680 {
681 m_appendPos.x = 0;
682 m_appendPos.y += y;
683 }
684 else if ( dire == VRETURN )
685 {
686 m_appendPos.x += x;
687 m_appendPos.y = 0;
688 }
689 }
695 bool m_Locate(INDEX index, ELocation locate, const IDrawable* pDraw)
696 {
697 if ( ! m_drawers.IsInRange(index) ) { return false; }
698 const TParam& A = m_drawers.At(index);
699 SIZE baseSize;
700 if ( ! A.pDrawer->GetSize(baseSize) ) { return false; }
701 SIZE mySize;
702 if ( ! pDraw->GetSize(mySize) ) { return false; }
703 m_appendPos.x = A.pos.x;
704 m_appendPos.y = A.pos.y;
705 // Y調整
706 if ( locate == RIGHTCENTER || locate == CENTER )
707 {
708 m_appendPos.y += (baseSize.cy - mySize.cy) / 2;
709 }
710 else if( locate == RIGHTBOTTOM )
711 {
712 m_appendPos.y += baseSize.cy - mySize.cy;
713 }
714 else if ( locate != RIGHTTOP )//下に追加
715 {
716 m_appendPos.y += baseSize.cy;
717 }
718 // X調整
719 if ( locate == DOWNCENTER || locate == CENTER )
720 {
721 m_appendPos.x += (baseSize.cx - mySize.cx) / 2;
722 }
723 else if( locate == DOWNRIGHT )
724 {
725 m_appendPos.x += baseSize.cx - mySize.cx;
726 }
727 else if ( locate != DOWNLEFT )//右に追加
728 {
729 m_appendPos.x += baseSize.cx;
730 }
731 return true;
732 }
733};
734
735
736
737};
738
ビットマップ描画関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
簡易配列型情報管理関係のヘッダ
ビットマップ描画クラス
static CBitmapHandle ToBitmap(const IDrawable &draw, COLORREF color=CLR_INVALID)
[作成] ビットマップ作成.
HBITMAP型ハンドルハンドル
描画コンテナクラス
@ CENTER
中央に追加。
@ RIGHTBOTTOM
下あわせで、右に追加。
@ DOWNLEFT
左あわせで、下に追加。
@ DOWNCENTER
中央あわせで、下に追加。
@ RIGHTCENTER
中央あわせで、右に追加。
@ DOWNRIGHT
右あわせで、下に追加。
@ RIGHTTOP
上あわせで、右に追加。
void SetAppendPosition(int x, int y)
[操作] 追加相対座標設定.
bool GetAllRect(RECT &_rect) const
[取得] 矩形範囲取得.
void RemoveAll(void)
[削除] すべて削除.
virtual void Draw(HDC dc, int x=0, int y=0) const
[描画] 描画.
INDEX AddBitmap(CBitmapHandle bmp, EDirection dire=DOWN)
[追加] ビットマップ描画情報追加.
CDrawingContainer(void)
コンストラクタ
const IDrawable * At(INDEX index) const
[参照] 指定INDEXの参照.
INDEX Add(const IDrawable &draw, EDirection dire=DOWN)
[追加] 描画情報追加.
INDEX AddNewPointer(INDEX index, ELocation locate, IDrawable *pDraw, EDirection dire=DOWN)
[追加] 描画情報追加.
bool RemoveAt(INDEX index)
[削除] 指定INDEXの削除.
void OffsetAppendPosition(int x, int y)
[操作] 追加相対座標移動.
virtual ~CDrawingContainer(void)
デストラクタ
virtual bool Resize(const SIZE &size)
[設定] サイズ設定.
bool MoveAppendPosition(INDEX index, EDirection dire=DOWN, int x=0, int y=0)
[移動] 追加相対座標設定.
INDEX Add(INDEX index, ELocation locate, const IDrawable &draw, EDirection dire=DOWN)
[追加] 描画情報追加.
INDEX AddNewPointer(IDrawable *pDraw, EDirection dire=DOWN)
[追加] 描画情報追加.
INDEX AddBitmap(INDEX index, ELocation locate, CBitmapHandle bmp, EDirection dire=DOWN)
[追加] ビットマップ描画情報追加.
@ RETURN
下へ移動して一番左へ
@ VRETURN
右へ移動して一番上へ
CBitmapHandle CreateBitmap(COLORREF color=CLR_INVALID) const
[作成] ビットマップ作成.
HRGN GetAllRgn(void) const
[取得] 領域範囲取得.
void DrawAt(INDEX index, HDC dc, int x=0, int y=0) const
[描画] 描画.
bool GetAtRect(INDEX index, RECT &_rect) const
[取得] ビットマップ取得.
virtual bool GetSize(SIZE &_size) const
[取得] サイズ取得.
const POINT & GetAppendPosition(void) const
[取得] 追加相対座標取得.
virtual IDrawable * Clone(void) const
[作成] クローン作成.
void DefaultSize(void)
[設定] サイズリセット.
size_t GetCount(void) const
[取得] 格納数取得.
int AppendBitmaps(LPCTSTR lpszPath, INDEX maxIndex, EDirection dire=DOWN)
[追加] 一括ビットマップファイル読込み.
virtual void DrawEx(HDC dc, int x, int y, LPARAM lParam) const
[描画] 描画.
NULL描画クラス
Definition: TnbDrawable.h:106
bool IsNull(void) const
[確認] NULLチェック
void RemoveAll(void)
[削除] 空化
size_t GetSize(void) const
[取得] サイズ取得
bool Remove(INDEX index)
[削除] 要素一つ削除.
void SetSize(size_t s)
[設定] サイズ設定
bool IsInRange(INDEX index) const
[確認] INDEXの有効確認.
const TYP & At(INDEX index) const
[取得] 要素の参照取得.
INDEX Add(const TYP &t)
[追加] 要素一つ追加.
INT_PTR PathIndexOf(LPCSTR lpszText)
[検索] パス区切り検索(ASCII/SJIS用)
Definition: TnbStrLib.h:310
void Copy(LPSTR _dst, LPCSTR src)
[複製] 文字列コピー(ASCII/SJIS用)
Definition: TnbStrLib.h:89
bool PrintF(LPSTR _pWork, size_t iLen, LPCSTR lpFmt,...)
[作成] 書式付き文字列作成(ASCII/SJIS用)
Definition: TnbDef.h:1101
TNB Library
Definition: TnbDoxyTitle.txt:2
描画情報インターフェース
Definition: TnbDrawable.h:37
virtual bool GetSize(SIZE &_size) const =0
[取得] サイズ取得.
virtual IDrawable * Clone(void) const =0
[作成] クローン作成.