| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <cstring> |
| 20 | |
| 21 | #include "UIlib.h" |
| 22 | #include "grtext.h" |
| 23 | #include "mem.h" |
| 24 | #include "pserror.h" |
| 25 | #include "uisys.h" |
| 26 | |
| 27 | //@@void *UIItem::operator new(size_t mem, UIItem *item, tUIResClass class_type) |
| 28 | //@@{ |
| 29 | //@@// hopefully, this will initialize the proper object and return a UIItem equivalent. |
| 30 | //@@ if (!item) |
| 31 | //@@ return NULL; |
| 32 | //@@ |
| 33 | //@@ if (class_type == uiTextItem) |
| 34 | //@@ return new(item) UITextItem; |
| 35 | //@@ else if (class_type == uiBitmapItem) |
| 36 | //@@ return new(item) UIBitmapItem; |
| 37 | //@@ else { |
| 38 | //@@ Int3(); // Samir! |
| 39 | //@@ return NULL; |
| 40 | //@@ } |
| 41 | //@@} |
| 42 | |
| 43 | // UITextItem |
| 44 | // used by user interface system, contains information about how to render |
| 45 | // text. allows for alpha, color and different fonts. |
| 46 | |
| 47 | int UITextItem::m_Sat = 0; |
| 48 | int UITextItem::m_DefaultFont = 0; |
| 49 | char UITextItem::dummy_str[4] = ""; |
| 50 | |
| 51 | UITextItem::UITextItem(const char *text, ddgr_color color, uint8_t alpha) { |
| 52 | int slen = strlen(text); |
| 53 | if (slen > 0) { |
| 54 | m_Text = mem_strdup(text); |
| 55 | if (!m_Text) { |
| 56 | Error("Memory allocation failed in UITextItem."); |
| 57 | } |
| 58 | } else { |
| 59 | m_Text = &UITextItem::dummy_str[0]; |
| 60 | dummy_str[0] = 0; |
| 61 | } |
| 62 | |
| 63 | strcpy(m_Text, text); |
| 64 | |
| 65 | m_Color = color; |
| 66 | m_Alpha = alpha; |
| 67 | m_Font = m_DefaultFont; |
| 68 | } |
| 69 | |
| 70 | UITextItem::UITextItem(int font, const char *text, ddgr_color color, uint8_t alpha) { |
| 71 | int slen = strlen(text); |
| 72 | if (slen > 0) { |
| 73 | m_Text = mem_strdup(text); |
| 74 | if (!m_Text) { |
| 75 | Error("Memory allocation failed in UITextItem."); |
| 76 | } |
| 77 | } else { |
| 78 | m_Text = &UITextItem::dummy_str[0]; |
| 79 | dummy_str[0] = 0; |
| 80 | } |
| 81 | |
| 82 | strcpy(m_Text, text); |
| 83 | |
| 84 | m_Color = color; |
| 85 | m_Alpha = alpha; |
| 86 | m_Font = font; |
| 87 | } |
| 88 | |
| 89 | UITextItem::~UITextItem() { |
| 90 | if (m_Text && m_Text != (&UITextItem::dummy_str[0])) { |
| 91 | mem_free(m_Text); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Inheritable functions |
| 96 | bool UITextItem::draw(int x, int y, tUIDrawClass draw_class) { |
| 97 | int i; |
| 98 | |
| 99 | ui_DrawSetFont(m_Font); |
| 100 | ui_SetCharAlpha(m_Alpha); |
| 101 | |
| 102 | if (draw_class == uiDrawAlphaSaturate) { |
| 103 | ui_DrawSetTextType(UI_TEXTTYPE_SATURATE); |
| 104 | ui_DrawString(m_Color, x, y, m_Text); |
| 105 | for (i = 0; i < UITextItem::m_Sat; i++) |
| 106 | ui_DrawString(m_Color, x, y, m_Text); |
| 107 | } else if (draw_class == uiDrawFaded) { |
| 108 | ui_DrawSetTextType(0); |
| 109 | ui_SetCharAlpha(m_Alpha / 2); |
| 110 | ui_DrawString(m_Color, x, y, m_Text); |
| 111 | ui_DrawSetTextType(UI_TEXTTYPE_SATURATE); |
| 112 | for (i = 0; i < UITextItem::m_Sat; i++) |
| 113 | ui_DrawString(m_Color, x, y, m_Text); |
| 114 | } else { |
| 115 | ui_DrawSetTextType(0); |
| 116 | ui_DrawString(m_Color, x, y, m_Text); |
| 117 | ui_DrawSetTextType(UI_TEXTTYPE_SATURATE); |
| 118 | for (i = 0; i < UITextItem::m_Sat; i++) |
| 119 | ui_DrawString(m_Color, x, y, m_Text); |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | int UITextItem::width() { |
| 126 | ui_DrawSetFont(m_Font); |
| 127 | return (m_Text ? ui_GetTextWidth(m_Text) : 0); |
| 128 | } |
| 129 | |
| 130 | int UITextItem::height() { |
| 131 | ui_DrawSetFont(m_Font); |
| 132 | return (m_Text ? ui_GetTextHeight(m_Text) : 0); |
| 133 | } |
| 134 | |
| 135 | // operators |
| 136 | UIItem *UITextItem::CopyUIItem() { |
| 137 | UITextItem *newitem = ::new UITextItem(); |
| 138 | if (newitem) { |
| 139 | *newitem = *this; |
| 140 | } |
| 141 | return newitem; |
| 142 | } |
| 143 | |
| 144 | const UITextItem &UITextItem::operator=(const UITextItem &item) { |
| 145 | if (m_Text && m_Text != (&UITextItem::dummy_str[0])) { |
| 146 | mem_free(m_Text); |
| 147 | } |
| 148 | |
| 149 | if (strlen(item.m_Text) > 0) { |
| 150 | m_Text = mem_strdup(item.m_Text); |
| 151 | if (!m_Text) |
| 152 | Error("Memory allocation failed in UITextItem."); |
| 153 | } else { |
| 154 | m_Text = &UITextItem::dummy_str[0]; |
| 155 | dummy_str[0] = 0; |
| 156 | } |
| 157 | |
| 158 | if (m_Text != item.m_Text) { |
| 159 | strcpy(m_Text, item.m_Text); |
| 160 | } |
| 161 | m_Color = item.m_Color; |
| 162 | m_Alpha = item.m_Alpha; |
| 163 | m_Font = item.m_Font; |
| 164 | |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | // UISnazzyTextItem |
| 169 | // rich text item. |
| 170 | |
| 171 | #define UISTIF_INCREASING 0x80000000 |
| 172 | |
| 173 | UISnazzyTextItem::UISnazzyTextItem(unsigned flags, const char *text, ddgr_color color, uint8_t alpha) |
| 174 | : UITextItem(text, color, alpha) { |
| 175 | m_flags = 0; // must do, so that set_flags works. |
| 176 | set_flags(flags); |
| 177 | } |
| 178 | |
| 179 | UISnazzyTextItem::UISnazzyTextItem(unsigned flags, int font, const char *text, ddgr_color color, uint8_t alpha) |
| 180 | : UITextItem(font, text, color, alpha) { |
| 181 | m_flags = 0; // must do, so that set_flags works. |
| 182 | set_flags(flags); |
| 183 | } |
| 184 | |
| 185 | void UISnazzyTextItem::set_flags(unsigned flags) { |
| 186 | // reset old stuff |
| 187 | if (m_flags & UISNAZZYTEXTF_BLINKING) { |
| 188 | m_data.f = 0; |
| 189 | set_color((ddgr_color)m_internaldata.i); |
| 190 | } |
| 191 | |
| 192 | // set new stuff |
| 193 | m_flags = flags & (~UISNAZZYTEXTF_RESERVED); |
| 194 | |
| 195 | if (m_flags & UISNAZZYTEXTF_BLINKING) { |
| 196 | m_data.f = 1.0f; |
| 197 | m_internaldata.i = (int)get_color(); |
| 198 | } else { |
| 199 | m_data.i = 0; |
| 200 | m_internaldata.i = 0; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | bool UISnazzyTextItem::draw(int x, int y, tUIDrawClass draw_class) { |
| 205 | if (m_flags & UISNAZZYTEXTF_BLINKING) { |
| 206 | int r, r2, g, g2, b, b2; |
| 207 | if (m_flags & UISTIF_INCREASING) |
| 208 | m_data.f += ((UIFrameTime) * 1.5f); |
| 209 | else |
| 210 | m_data.f -= ((UIFrameTime) * 1.5f); |
| 211 | |
| 212 | if (m_data.f < 0.0f) { |
| 213 | m_data.f = 0.0; |
| 214 | m_flags |= UISTIF_INCREASING; |
| 215 | } else if (m_data.f > 1.0f) { |
| 216 | m_data.f = 1.0f; |
| 217 | m_flags &= (~UISTIF_INCREASING); |
| 218 | } |
| 219 | r = GR_COLOR_RED(m_internaldata.i); |
| 220 | g = GR_COLOR_GREEN(m_internaldata.i); |
| 221 | b = GR_COLOR_BLUE(m_internaldata.i); |
| 222 | r2 = r + 96; |
| 223 | if (r2 > 255) |
| 224 | r2 = 255; |
| 225 | g2 = g + 96; |
| 226 | if (g2 > 255) |
| 227 | g2 = 255; |
| 228 | b2 = b + 96; |
| 229 | if (b2 > 255) |
| 230 | b2 = 255; |
| 231 | r2 = r2 - r; |
| 232 | g2 = g2 - g; |
| 233 | b2 = b2 - b; |
| 234 | |
| 235 | set_color(GR_RGB(r + (int)(r2 * m_data.f), g + (int)(g2 * m_data.f), b + (int)(b2 * m_data.f))); |
| 236 | // set_alpha((uint8_t)(m_data.f*m_internaldata.i)); |
| 237 | // mprintf(0, "%x\n", (unsigned)get_color()); |
| 238 | } |
| 239 | |
| 240 | return UITextItem::draw(x, y, draw_class); |
| 241 | } |
| 242 | |
| 243 | UIItem *UISnazzyTextItem::CopyUIItem() { |
| 244 | UISnazzyTextItem *newitem = ::new UISnazzyTextItem(); |
| 245 | if (newitem) { |
| 246 | *newitem = *this; |
| 247 | } |
| 248 | return newitem; |
| 249 | } |
| 250 | |
| 251 | const UISnazzyTextItem &UISnazzyTextItem::operator=(const UISnazzyTextItem &item) { |
| 252 | // reproduce UITextItem's = operator |
| 253 | if (m_Text && m_Text != (&UITextItem::dummy_str[0])) { |
| 254 | mem_free(m_Text); |
| 255 | } |
| 256 | |
| 257 | if (strlen(item.m_Text) > 0) { |
| 258 | m_Text = mem_strdup(item.m_Text); |
| 259 | if (!m_Text) |
| 260 | Error("Memory allocation failed in UITextItem."); |
| 261 | } else { |
| 262 | m_Text = &UITextItem::dummy_str[0]; |
| 263 | dummy_str[0] = 0; |
| 264 | } |
| 265 | |
| 266 | strcpy(m_Text, item.m_Text); |
| 267 | |
| 268 | set_color(item.get_color()); |
| 269 | set_alpha(item.get_alpha()); |
| 270 | set_font(item.get_font()); |
| 271 | |
| 272 | // add snazzy enhancements |
| 273 | m_internaldata = item.m_internaldata; |
| 274 | m_data = item.m_data; |
| 275 | m_flags = item.m_flags; |
| 276 | |
| 277 | return *this; |
| 278 | } |
| 279 | |
| 280 | // UIBitmapItem |
| 281 | // used by user interface system, contains information about how to render |
| 282 | // text. allows for alpha, color and different fonts. |
| 283 | |
| 284 | // Inheritable functions |
| 285 | bool UIBitmapItem::draw(int x, int y, tUIDrawClass draw_class) { |
| 286 | ui_DrawBitmap(this, x, y, m_Alpha); |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | int UIBitmapItem::width() { return (m_IsValid ? ui_GetBitmapWidth(this) : 0); } |
| 291 | |
| 292 | int UIBitmapItem::height() { return (m_IsValid ? ui_GetBitmapHeight(this) : 0); } |
| 293 | |
| 294 | // operators |
| 295 | UIItem *UIBitmapItem::CopyUIItem() { |
| 296 | UIBitmapItem *newitem = ::new UIBitmapItem(); |
| 297 | if (newitem) { |
| 298 | *newitem = *this; |
| 299 | } |
| 300 | return newitem; |
| 301 | } |
| 302 | |
| 303 | const UIBitmapItem &UIBitmapItem::operator=(const UIBitmapItem &item) { |
| 304 | m_IsValid = item.m_IsValid; |
| 305 | m_IsChunked = item.m_IsChunked; |
| 306 | |
| 307 | if (m_IsChunked) |
| 308 | m_Bitmap.chunk = item.m_Bitmap.chunk; |
| 309 | else |
| 310 | m_Bitmap.handle = item.m_Bitmap.handle; |
| 311 | |
| 312 | m_Alpha = item.m_Alpha; |
| 313 | |
| 314 | return *this; |
| 315 | } |
| 316 | |
| 317 | bool UIPrimativeItem::draw(int x, int y, int w, int h) { |
| 318 | ui_DrawSetAlpha(alpha); |
| 319 | ui_DrawRect(color, x, y, x + w, y + h); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | UIItem *UIPrimativeItem::CopyUIItem() { |
| 324 | UIPrimativeItem *newitem = ::new UIPrimativeItem(color, alpha); |
| 325 | return newitem; |
| 326 | } |