| 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 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/ui/UIHotspot.cpp $ |
| 21 | * $Revision: 16 $ |
| 22 | * $Date: 4/26/99 9:11p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Hotspot code. |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/ui/UIHotspot.cpp $ |
| 28 | * |
| 29 | * 16 4/26/99 9:11p Matt |
| 30 | * Took out saturation used by old newui. |
| 31 | * |
| 32 | * 15 4/24/99 8:23p Samir |
| 33 | * set hotspot hotkey after gadget is created. |
| 34 | * |
| 35 | * 14 4/14/99 1:53a Jeff |
| 36 | * fixed case mismatched #includes |
| 37 | * |
| 38 | * 13 10/13/98 3:47p Samir |
| 39 | * hotspots now handle spacebats |
| 40 | * |
| 41 | * 12 10/01/98 12:57p Samir |
| 42 | * fixed hotspots so their width and height touch text, no outer border. |
| 43 | * |
| 44 | * 11 9/30/98 4:33p Samir |
| 45 | * disabled gadgets display dim text. |
| 46 | * |
| 47 | * 10 6/05/98 5:58p Jeff |
| 48 | * Added a way to change the items for the states of a HotSpot |
| 49 | * |
| 50 | * 9 5/01/98 6:23p Samir |
| 51 | * saturate hotspots when active. |
| 52 | * |
| 53 | * 8 3/09/98 3:43p Samir |
| 54 | * Hotspot selected when mouse button goes up. |
| 55 | * |
| 56 | * 7 1/30/98 7:05p Samir |
| 57 | * Each gadget now has their own coordinate system. |
| 58 | * |
| 59 | * 6 1/23/98 5:34p Samir |
| 60 | * Allow for nonpersistant UIItems. |
| 61 | * |
| 62 | * 5 1/18/98 4:22p Samir |
| 63 | * Implemented new UIItem system. |
| 64 | * |
| 65 | * 4 1/05/98 4:36p Samir |
| 66 | * Moved centering and fittext control flags to UIGadget class. |
| 67 | * |
| 68 | * 3 1/02/98 2:13p Samir |
| 69 | * Set alpha to full for item with focus. |
| 70 | * |
| 71 | * 2 1/02/98 12:47p Samir |
| 72 | * Added selection and bettering centering? |
| 73 | * |
| 74 | * 1 12/30/97 4:36p Samir |
| 75 | * Initial revision |
| 76 | * |
| 77 | * $NoKeywords: $ |
| 78 | */ |
| 79 | |
| 80 | #include "UIlib.h" |
| 81 | |
| 82 | // ---------------------------------------------------------------------------- |
| 83 | // UIHotspot |
| 84 | // This is simply a region within the parent window that when clicked on |
| 85 | // or key pressed, does something. |
| 86 | |
| 87 | UIHotspot::UIHotspot() { |
| 88 | m_ItemOff = NULL; |
| 89 | m_ItemOn = NULL; |
| 90 | } |
| 91 | |
| 92 | UIHotspot::~UIHotspot() {} |
| 93 | |
| 94 | // optional key, when pressed, triggers hotspot. |
| 95 | void UIHotspot::Create(UIWindow *wnd, int id, int key, UIItem *itemoff, UIItem *itemon, int x, int y, int w, int h, |
| 96 | int flags) { |
| 97 | ASSERT(itemoff); |
| 98 | ASSERT(itemon); |
| 99 | |
| 100 | if (itemoff) |
| 101 | m_ItemOff = itemoff->CopyUIItem(); |
| 102 | if (itemon) |
| 103 | m_ItemOn = itemon->CopyUIItem(); |
| 104 | |
| 105 | m_ItemCur = m_ItemOff; |
| 106 | |
| 107 | UIGadget::Create(wnd, id, x, y, w, h, flags); |
| 108 | |
| 109 | if (key) { |
| 110 | UIGadget::SetHotkey(key); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void UIHotspot::SetStates(UIItem *itemoff, UIItem *itemon) { |
| 115 | ASSERT(itemoff); |
| 116 | ASSERT(itemon); |
| 117 | |
| 118 | // determine which state it currently set, so we can set it on the new state |
| 119 | bool currstate = false; |
| 120 | |
| 121 | if (m_ItemOn == m_ItemCur) |
| 122 | currstate = true; |
| 123 | |
| 124 | if (m_ItemOff) |
| 125 | delete m_ItemOff; |
| 126 | if (m_ItemOn) |
| 127 | delete m_ItemOn; |
| 128 | |
| 129 | m_ItemOff = itemoff->CopyUIItem(); |
| 130 | m_ItemOn = itemon->CopyUIItem(); |
| 131 | |
| 132 | if (currstate) |
| 133 | m_ItemCur = m_ItemOn; |
| 134 | else |
| 135 | m_ItemCur = m_ItemOff; |
| 136 | } |
| 137 | |
| 138 | // override: behavior when gadget is destroyed. |
| 139 | void UIHotspot::OnDestroy() { |
| 140 | UIGadget::OnDestroy(); |
| 141 | |
| 142 | if (m_ItemOff) |
| 143 | delete m_ItemOff; |
| 144 | if (m_ItemOn) |
| 145 | delete m_ItemOn; |
| 146 | |
| 147 | m_ItemOff = NULL; |
| 148 | m_ItemOn = NULL; |
| 149 | } |
| 150 | |
| 151 | // behavior when key is pressed. |
| 152 | void UIHotspot::OnKeyDown(int key) { |
| 153 | if (key == KEY_SPACEBAR) { |
| 154 | OnSelect(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // behavior when mouse button is pressed. |
| 159 | void UIHotspot::OnMouseBtnDown(int btn) { |
| 160 | if (btn == UILMSEBTN) { |
| 161 | LOCK_FOCUS(this); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // override: behavior when mouse button is released. |
| 166 | void UIHotspot::OnMouseBtnUp(int btn) { |
| 167 | if (btn == UILMSEBTN) { |
| 168 | if (HasFocus()) { |
| 169 | if (PT_IN_GADGET(UIGadget::m_Wnd, this, UI_input.mx, UI_input.my)) { |
| 170 | OnSelect(); |
| 171 | } |
| 172 | } |
| 173 | UNLOCK_FOCUS(this); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // override: behavior when gadget loses input focus. |
| 178 | void UIHotspot::OnLostFocus() { m_ItemCur = m_ItemOff; } |
| 179 | |
| 180 | // override: behavior when gadget gains input focus. |
| 181 | void UIHotspot::OnGainFocus() { m_ItemCur = m_ItemOn; } |
| 182 | |
| 183 | // override: called when resized or before drawing. |
| 184 | void UIHotspot::OnFormat() { |
| 185 | if (m_Flags & UIF_FIT) { |
| 186 | m_W = m_ItemCur->width(); |
| 187 | m_H = m_ItemCur->height(); |
| 188 | } |
| 189 | |
| 190 | UIGadget::OnFormat(); |
| 191 | } |
| 192 | |
| 193 | // behavior when gadget is being drawn. |
| 194 | void UIHotspot::OnDraw() { |
| 195 | // draw simple background. |
| 196 | if (m_ItemCur) { |
| 197 | int x = (m_W / 2 - m_ItemCur->width() / 2); |
| 198 | int y = (m_H / 2 - m_ItemCur->height() / 2); |
| 199 | if (IsDisabled()) |
| 200 | m_ItemCur->draw(x, y, uiDrawFaded); |
| 201 | else { |
| 202 | m_ItemCur->draw(x, y); |
| 203 | } |
| 204 | } |
| 205 | } |