NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
ut_TextWriterBase.h
1#ifndef NW4R_UT_TEXT_WRITER_BASE_H
2#define NW4R_UT_TEXT_WRITER_BASE_H
3#include <nw4r/types_nw4r.h>
4
5#include <nw4r/ut/ut_CharWriter.h>
6#include <nw4r/ut/ut_TagProcessorBase.h>
7
8#include <nw4r/math.h>
9
10#include <cstdio>
11#include <cwchar>
12
13namespace nw4r {
14namespace ut {
15
16template <typename T> class TextWriterBase : public CharWriter {
17public:
18 enum DrawFlag {
19 // Align text lines
20 DRAWFLAG_ALIGN_TEXT_BASELINE = 0,
21 DRAWFLAG_ALIGN_TEXT_CENTER = (1 << 0),
22 DRAWFLAG_ALIGN_TEXT_RIGHT = (1 << 1),
23
24 // Align text block (horizontal)
25 DRAWFLAG_ALIGN_H_BASELINE = 0,
26 DRAWFLAG_ALIGN_H_CENTER = (1 << 4),
27 DRAWFLAG_ALIGN_H_RIGHT = (1 << 5),
28
29 // Align text block (vertical)
30 DRAWFLAG_ALIGN_V_BASELINE = 0,
31 DRAWFLAG_ALIGN_V_CENTER = (1 << 8),
32 DRAWFLAG_ALIGN_V_TOP = (1 << 9),
33
34 // Mask constants
35 DRAWFLAG_MASK_ALIGN_TEXT = DRAWFLAG_ALIGN_TEXT_BASELINE |
36 DRAWFLAG_ALIGN_TEXT_CENTER |
37 DRAWFLAG_ALIGN_TEXT_RIGHT,
38
39 DRAWFLAG_MASK_ALIGN_H = DRAWFLAG_ALIGN_H_BASELINE |
40 DRAWFLAG_ALIGN_H_CENTER |
41 DRAWFLAG_ALIGN_H_RIGHT,
42
43 DRAWFLAG_MASK_ALIGN_V = DRAWFLAG_ALIGN_V_BASELINE |
44 DRAWFLAG_ALIGN_V_CENTER | DRAWFLAG_ALIGN_V_TOP,
45 };
46
47public:
48 TextWriterBase();
49 ~TextWriterBase();
50
51 f32 GetWidthLimit() const {
52 return mWidthLimit;
53 }
54 void SetWidthLimit(f32 limit) {
55 mWidthLimit = limit;
56 }
57 void ResetWidthLimit() {
58 mWidthLimit = NW4R_MATH_FLT_MAX;
59 }
60
61 f32 GetCharSpace() const {
62 return mCharSpace;
63 }
64 void SetCharSpace(f32 space) {
65 mCharSpace = space;
66 }
67
68 f32 GetLineSpace() const {
69 return mLineSpace;
70 }
71 void SetLineSpace(f32 space) {
72 mLineSpace = space;
73 }
74
75 int GetTabWidth() const {
76 return mTabWidth;
77 }
78 void SetTabWidth(int width) {
79 mTabWidth = width;
80 }
81
82 ulong GetDrawFlag() const {
83 return mDrawFlag;
84 }
85 void SetDrawFlag(ulong flag) {
86 mDrawFlag = flag;
87 }
88
89 TagProcessorBase<T>* GetTagProcessor() const {
90 return mTagProcessor;
91 }
92 void SetTagProcessor(TagProcessorBase<T>* pProcessor) {
93 mTagProcessor = pProcessor;
94 }
95 void ResetTagProcessor() {
96 mTagProcessor = &mDefaultTagProcessor;
97 }
98
99 f32 GetLineHeight() const;
100
101 f32 CalcLineWidth(const T* pStr, int len);
102 f32 CalcStringWidth(const T* pStr, int len) const;
103 void CalcStringRect(Rect* pRect, const T* pStr, int len) const;
104
105 int VSNPrintf(T* buffer, ulong count, const T* pStr, std::va_list args);
106 f32 VPrintf(const T* pStr, std::va_list args);
107 f32 Print(const T* pStr, int len);
108
109 static T* GetBuffer() {
110 return mFormatBuffer;
111 }
112 static T* SetBuffer(T* pBuffer, ulong size) {
113 T* pOldBuffer = mFormatBuffer;
114 mFormatBuffer = pBuffer;
115 mFormatBufferSize = size;
116 return pOldBuffer;
117 }
118
119 static ulong GetBufferSize() {
120 return mFormatBufferSize;
121 }
122
123private:
124 static const int DEFAULT_FORMAT_BUFFER_SIZE = 256;
125
126 static const ulong DRAWFLAG_MASK_ALL = DRAWFLAG_MASK_ALIGN_TEXT |
127 DRAWFLAG_MASK_ALIGN_H |
128 DRAWFLAG_MASK_ALIGN_V;
129
130private:
131 bool IsDrawFlagSet(ulong mask, ulong flag) const {
132 return (mDrawFlag & mask) == flag;
133 }
134
135 bool CalcLineRectImpl(Rect* pRect, const T** ppStr, int len);
136 void CalcStringRectImpl(Rect* pRect, const T* pStr, int len);
137
138 f32 PrintImpl(const T* pStr, int len);
139 f32 AdjustCursor(f32* pX, f32* pY, const T* pStr, int len);
140
141private:
142 f32 mWidthLimit; // at 0x4C
143 f32 mCharSpace; // at 0x50
144 f32 mLineSpace; // at 0x54
145 int mTabWidth; // at 0x58
146 ulong mDrawFlag; // at 0x5C
147 TagProcessorBase<T>* mTagProcessor; // at 0x60
148
149 static T* mFormatBuffer;
150 static ulong mFormatBufferSize;
151 static TagProcessorBase<T> mDefaultTagProcessor;
152};
153
154template <>
155inline int TextWriterBase<char>::VSNPrintf(char* pBuffer, ulong count,
156 const char* pStr,
157 std::va_list args) {
158
159 return std::vsnprintf(pBuffer, count, pStr, args);
160}
161
162template <>
163inline int TextWriterBase<wchar_t>::VSNPrintf(wchar_t* pBuffer, ulong count,
164 const wchar_t* pStr,
165 std::va_list args) {
166
167 return std::vswprintf(pBuffer, count, pStr, args);
168}
169
170} // namespace ut
171} // namespace nw4r
172
173#endif
Debugging library which includes various utilities used by the rest of nw4r.
Definition ut_list.cpp:4