1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: GuiControlBase.cpp
4
5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Rev: 46365 $
14 *---------------------------------------------------------------------------*/
15
16 #include <stdarg.h>
17 #include "GuiControlBase.h"
18
19 namespace
20 {
wstrlen(const wchar_t * pText)21 s32 wstrlen(const wchar_t* pText)
22 {
23 s32 length = 0;
24
25 while (pText[length] != '\0')
26 {
27 length++;
28 }
29
30 return length;
31 }
32 }
33
34 namespace scene
35 {
36
37 // Constructor.
ControlBase(ControlType type,u32 id,s32 x,s32 y,u32 width,u32 height,const wchar_t * pText,void * pExtraData,f32 fontScale)38 ControlBase::ControlBase(ControlType type, u32 id, s32 x, s32 y, u32 width, u32 height, const wchar_t* pText, void* pExtraData, f32 fontScale)
39 {
40 m_type = type;
41 m_id = id;
42 m_x = x;
43 m_y = y;
44 m_width = width;
45 m_height = height;
46 m_pText = NULL;
47 m_textLength = 0;
48 m_textBufferLength = 0;
49 m_TextScale = fontScale;
50 m_textWidth = 0;
51 m_textHeight = 0;
52 m_textAlign = TEXT_ALIGN_LEFT;
53 m_borderWidth = 1.0f;
54 m_state = CONTROL_STATE_DEFAULT;
55 m_pExtraData = pExtraData;
56 m_pManager = NULL;
57
58 SetText(pText);
59 }
60
61 // Destructor.
~ControlBase()62 ControlBase::~ControlBase()
63 {
64 SetText(NULL);
65 }
66
67 // Sets text.
SetText(const wchar_t * pText)68 void ControlBase::SetText(const wchar_t* pText)
69 {
70 if (pText)
71 {
72 s32 length = wstrlen(pText);
73
74 if (length >= m_textBufferLength)
75 {
76 delete[] m_pText;
77
78 m_pText = new wchar_t[length + 1];
79 m_textBufferLength = length + 1;
80 }
81
82 m_pText[length] = '\0';
83 memcpy(m_pText, pText, length * sizeof (m_pText[0]));
84
85 m_textLength = length;
86
87 // Calculate the size
88 {
89 // Text writer
90 TextWriter* pTextWriter = scene::GetTextWriter();
91
92 pTextWriter->SetFontScale(m_TextScale);
93 m_textWidth = pTextWriter->CalculateStringWidth(m_pText);
94 m_textHeight = pTextWriter->CalculateStringHeight(m_pText);
95 }
96 }
97 else
98 {
99 delete[] m_pText;
100
101 m_pText = NULL;
102 m_textLength = 0;
103 m_textBufferLength = 0;
104 m_textWidth = 0;
105 m_textHeight = 0;
106 }
107 }
108
109 // Sets text.
SetFormattedText(const wchar_t * pFormat,...)110 void ControlBase::SetFormattedText(const wchar_t* pFormat, ...)
111 {
112 // Buffer
113 wchar_t buffer[NN_DBG_PRINTF_BUFFER_LENGTH + 1] = {0};
114 // Argument list
115 va_list pArgs;
116
117 va_start(pArgs, pFormat);
118 {
119 vswprintf(buffer, NN_DBG_PRINTF_BUFFER_LENGTH, pFormat, pArgs);
120 }
121 va_end(pArgs);
122
123 SetText(buffer);
124 }
125
126 // Calculate the start position for text rendering.
CalculateTextX(s32 offsetL,s32 offsetR)127 s32 ControlBase::CalculateTextX(s32 offsetL, s32 offsetR)
128 {
129 s32 x = 0;
130
131 switch (m_textAlign)
132 {
133 case TEXT_ALIGN_LEFT:
134 {
135 x = m_x + offsetL;
136 }
137 break;
138 case TEXT_ALIGN_RIGHT:
139 {
140 x = m_x - offsetR + m_width - m_textWidth;
141 }
142 break;
143 case TEXT_ALIGN_CENTER:
144 {
145 s32 offsetC = offsetL + offsetR;
146
147 x = m_x + offsetL + ((m_width - offsetC) - m_textWidth) / 2;
148 }
149 break;
150 }
151
152 return x;
153 }
154
155 // Calculate the start position for text rendering.
CalculateTextY(s32 offset)156 s32 ControlBase::CalculateTextY(s32 offset)
157 {
158 return (m_y + offset + ((m_height - offset) - m_textHeight) / 2);
159 }
160
161 }
162